diff options
Diffstat (limited to 'tools/intergen/model/include/model')
-rw-r--r-- | tools/intergen/model/include/model/api.h | 89 | ||||
-rw-r--r-- | tools/intergen/model/include/model/builtin_type.h | 150 | ||||
-rw-r--r-- | tools/intergen/model/include/model/builtin_type_registry.h | 91 | ||||
-rw-r--r-- | tools/intergen/model/include/model/composite_type.h | 257 | ||||
-rw-r--r-- | tools/intergen/model/include/model/constant.h | 164 | ||||
-rw-r--r-- | tools/intergen/model/include/model/constants_creator.h | 56 | ||||
-rw-r--r-- | tools/intergen/model/include/model/function.h | 126 | ||||
-rw-r--r-- | tools/intergen/model/include/model/interface.h | 144 | ||||
-rw-r--r-- | tools/intergen/model/include/model/model_filter.h | 57 | ||||
-rw-r--r-- | tools/intergen/model/include/model/scope.h | 52 | ||||
-rw-r--r-- | tools/intergen/model/include/model/type.h | 92 | ||||
-rw-r--r-- | tools/intergen/model/include/model/type_registry.h | 152 |
12 files changed, 1430 insertions, 0 deletions
diff --git a/tools/intergen/model/include/model/api.h b/tools/intergen/model/include/model/api.h new file mode 100644 index 0000000000..6deb18d1d0 --- /dev/null +++ b/tools/intergen/model/include/model/api.h @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef API_H_ +#define API_H_ +#include <vector> +#include <map> + +#include "model/builtin_type_registry.h" +#include "model/interface.h" +#include "utils/macro.h" +#include "utils/stl_utils.h" + +namespace pugi { +class xml_document; +} // namespace pugi + +namespace codegen { +class ModelFilter; + +/* + * Represents single parsed XML file that defines an API + */ +class API { + public: + // Constructs object that represents whole API that contain + // all the defined interfaces. + // |model_filter| provied information on which entites to exclude + // from API. If |auto_generate_func_ids| is true FunctionID enum + // is generated automatically. + API(const ModelFilter* model_filter, bool auto_generate_func_ids); + // Follows parsed |xmldoc| collecting and validating API definitions + // Returns false and prints to cerr on error + bool init(const pugi::xml_document& xmldoc); + ~API(); + // List of all interfaces collected from xml document + const std::vector<Interface*>& interfaces() const; + // Get interface by name + // returns NULL if there is no interface with given name + const Interface* InterfaceByName(const std::string& name) const; + private: + // Types + typedef std::map<std::string, size_t> InterfacesIndex; + private: + // Methods + bool AddInterfaces(const pugi::xml_node& xmldoc); + private: + // Fields + const ModelFilter* model_filter_; + bool auto_generate_func_ids_; + BuiltinTypeRegistry builtin_type_registry_; + std::vector<Interface*> interfaces_; + utils::StdContainerDeleter<std::vector<Interface*> > interfaces_deleter_; + InterfacesIndex interfaces_index_; + DISALLOW_COPY_AND_ASSIGN(API); +}; + +} // namespace codegen + +#endif /* API_H_ */ diff --git a/tools/intergen/model/include/model/builtin_type.h b/tools/intergen/model/include/model/builtin_type.h new file mode 100644 index 0000000000..a9951a253e --- /dev/null +++ b/tools/intergen/model/include/model/builtin_type.h @@ -0,0 +1,150 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BUILTIN_TYPE_H_ +#define BUILTIN_TYPE_H_ + +#include <stdint.h> +#include <set> +#include <string> +#include <vector> + +#include "model/constants_creator.h" +#include "model/type.h" +#include "utils/common_types.h" + +namespace codegen { + +/* + * Boolean type + */ +class Boolean : public Type, public ConstantsCreator { + public: + // Types + class Constant; + public: + // Methods + Boolean(); + ~Boolean(); + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + // codegen::SupportsLiterals methods + virtual const codegen::Constant* ConstantFor( + const std::string& literal) const; + + public: + // Complete inner class declarations + + private: + // Fields + std::vector<Constant> constants_; +}; + +/* + * Integer type + */ +class Integer : public Type, public ConstantsCreator { + public: + // Types + typedef BasicRange<int64_t> Range; + class Constant; + public: + // Methods + Integer(const Range& range); + ~Integer(); + const Range& range() const; + bool operator<(const Integer& that) const; + + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + // codegen::SupportsLiterals methods + virtual const codegen::Constant* ConstantFor( + const std::string& literal) const; + private: + // Fields + Range range_; + mutable std::set<Constant> constants_; +}; + +/* + * Float type + */ +class Float : public Type, public ConstantsCreator { + public: + // Types + typedef BasicRange<double> Range; + class Constant; + public: + // Methods + Float(const Range& range); + ~Float(); + const Range& range() const; + bool operator<(const Float& that) const; + + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + // codegen::SupportsLiterals methods + virtual const codegen::Constant* ConstantFor( + const std::string& literal) const; + private: + // Fields + Range range_; + mutable std::set<Constant> constants_; +}; + +/* + * String type + */ +class String : public Type { + public: + // Types + typedef BasicRange<int64_t> Range; +public: + // Methods + String(const Range& range); + bool operator<(const String& that) const; + const Range& length_range() const; + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + +private: + // Fields + Range length_range_; +}; + +} // namespace codegen + +#endif /* BUILTIN_TYPE_H_ */ diff --git a/tools/intergen/model/include/model/builtin_type_registry.h b/tools/intergen/model/include/model/builtin_type_registry.h new file mode 100644 index 0000000000..1730984cc4 --- /dev/null +++ b/tools/intergen/model/include/model/builtin_type_registry.h @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BUILTIN_TYPE_REGISTRY_H_ +#define BUILTIN_TYPE_REGISTRY_H_ + +#include <stdint.h> +#include <set> +#include <string> + +#include "model/builtin_type.h" +#include "model/type.h" + +namespace pugi { +class xml_node; +} // namespace pugi + +namespace codegen { + +/* + * Factory and owner of all built-in types graph. + * Types have restrictions which are considered to be part of type + * (not part of struct field / function parameter definition) + */ +class BuiltinTypeRegistry { + public: + // List of all built-in types managed by this type registry + enum BuiltInType { + kBoolean, + kString, + kInteger, + kFloat, + kNotABuiltInType + }; + BuiltinTypeRegistry(); + ~BuiltinTypeRegistry(); + // Checks whether |type_name| is a built-in type name and returns + // appropriate BuiltInType value. + // Returns kNotABuiltInType if |type_name| contains unknown type name. + BuiltInType BuiltInTypeByName(const std::string& type_name); + // Creates type object based on it's |type_id| and type-dependent + // xml |params| and stores it into |type|. + // If |params| doesn't contain all required parameters, + // |type| is left untouched and false is returned. + bool GetType(BuiltInType type_id, const pugi::xml_node& params, + const Type** type); + private: + // Caching factories for supported types + const Boolean* GetBoolean(); + const Integer* GetInteger(const Integer::Range& range); + const Float* GetFloat(const Float::Range& range); + const String* GetString(const String::Range& length_range); + private: + Boolean boolean_; + std::set<Integer> integers_; + std::set<Float> floats_; + std::set<String> strings_; +}; + +} // namespace codegen + +#endif /* BUILTIN_TYPE_REGISTRY_H_ */ diff --git a/tools/intergen/model/include/model/composite_type.h b/tools/intergen/model/include/model/composite_type.h new file mode 100644 index 0000000000..ae60a53a77 --- /dev/null +++ b/tools/intergen/model/include/model/composite_type.h @@ -0,0 +1,257 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef COMPOSITE_TYPE_H_ +#define COMPOSITE_TYPE_H_ + +#include <stdint.h> +#include <map> +#include <string> +#include <list> + +#include "model/constants_creator.h" +#include "model/scope.h" +#include "model/type.h" +#include "utils/common_types.h" +#include "utils/macro.h" + + +namespace codegen { +class Interface; + +class Array : public Type { + public: + // Types + typedef BasicRange<int64_t> Range; + public: + // Methods + Array(const Type* type, const Range& range); + ~Array(); + const Type* type() const; + const Range& range() const; + bool operator<(const Array& that) const; + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + + private: + // Fields + const Type* type_; + Range range_; +}; + +class Map : public Type { + public: + // Types + typedef BasicRange<int64_t> Range; + public: + // Methods + Map(const Type* type, const Range& range); + ~Map(); + const Type* type() const; + const Range& range() const; + bool operator<(const Map& that) const; + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + + private: + // Fields + const Type* type_; + Range range_; +}; + +class Enum : public Type, public ConstantsCreator { + public: + // Types + class Constant; + typedef std::list<Enum::Constant> ConstantsList; + typedef std::map<std::string, Constant*> ConstantsByName; +public: + // Constants + // Name of enum that enlists IDs of all interface functions + static const char* kFunctionIdEnumName; + public: + // Methods + Enum(const Interface* interface, + const std::string& name, + Scope scope, + InternalScope internal_scope, + const Description& description); + ~Enum(); + const Interface& interface() const; + const std::string& name() const; + const ConstantsList& constants() const; + const ConstantsByName& constants_by_name() const; + const Scope& scope() const; + const InternalScope& internal_scope() const; + const Description& description() const; + + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + // codegen::ConstantsCreator methods + virtual const codegen::Constant* ConstantFor( + const std::string& literal) const; + bool AddConstant(const std::string& name, Scope scope, + const std::string& internal_name, + const Description& description, + const std::string& design_description); + bool AddConstant(const std::string& name, const int64_t value, Scope scope, + const std::string& internal_name, + const Description& description, + const std::string& design_description); + private: + // Fields + const Interface* interface_; + std::string name_; + ConstantsList constants_; + Scope scope_; + InternalScope internal_scope_; + Description description_; + + ConstantsByName constants_by_name_; + int64_t last_constant_value_; + DISALLOW_COPY_AND_ASSIGN(Enum); +}; + +/* + * Struct type + */ +class Struct : public Type { + public: + // Types + class Field; + typedef std::vector<Field> FieldsList; + public: + // Methods + Struct(const Interface* interface, + const std::string& name, + const Type* frankenmap, + Scope scope, + const Description& description); + ~Struct(); + const Interface& interface() const; + // Returns frankenstruct type if this struct is frankenstruct + const Type* frankenstruct() const; + const std::string& name() const; + const Description& description() const; + const FieldsList& fields() const; + const Scope& scope() const; + bool AddField(const Type* type, const std::string& name, bool mandatory, + Scope scope, const Constant* default_value, + const Description& description, Platform platform); + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + + private: + // Fields + const Interface* interface_; + std::string name_; + const Type* frankenmap_; + Scope scope_; + Description description_; + FieldsList fields_; + DISALLOW_COPY_AND_ASSIGN(Struct); +}; + +class Struct::Field { + public: + // Methods + const Type* type() const; + const std::string& name() const; + bool is_mandatory() const; + const Constant* default_value() const; + const Scope& scope() const; + const Description& description() const; + + private: + // Methods + Field(const Type* type, const std::string& name, bool mandatory, Scope scope, + const Constant* default_value, const Description& description, Platform platform); + private: + // Fields + const Type* type_; + std::string name_; + bool mandatory_; + const Constant* default_value_; + Scope scope_; + Description description_; + Platform platform_; + friend class Struct; +}; + +/* + * Nullable type decorator + */ +class NullableType : public Type { + public: + // Methods + NullableType(const Type* type); + const Type* type() const; + bool operator<(const NullableType& that) const; + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + private: + const Type* type_; +}; + +/* + * Typedef type + */ +class Typedef : public Type { + public: + Typedef(const Interface* interface, + const std::string& name, + const Type* type, + const Description& description); + + // codegen::Type methods + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const; + virtual const ConstantsCreator* SupportsConstants() const; + const Description& description() const; + const Interface& interface() const; + const std::string& name() const; + const Type* type() const; + + private: + const Interface* interface_; + std::string name_; + const Type* type_; + Description description_; +}; + +} // namespace codegen + +#endif /* COMPOSITE_TYPE_H_ */ diff --git a/tools/intergen/model/include/model/constant.h b/tools/intergen/model/include/model/constant.h new file mode 100644 index 0000000000..b3659905c0 --- /dev/null +++ b/tools/intergen/model/include/model/constant.h @@ -0,0 +1,164 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef CONSTANT_H_ +#define CONSTANT_H_ + +#include <string> + +#include "model/builtin_type.h" +#include "model/composite_type.h" +#include "utils/common_types.h" + +namespace codegen { +class Type; +class ConstantCodeGenerator; + +class Constant { + public: + virtual const Type* type() const = 0; + virtual void Apply(ConstantCodeGenerator* generator) const = 0; + virtual ~Constant(); +}; + +class Boolean::Constant : public codegen::Constant { + public: + ~Constant(); + bool value() const; + bool is_valid() const; + bool operator==(const Boolean::Constant& that) const; + // codegen::Constant methods + virtual const Boolean* type() const; + virtual void Apply(ConstantCodeGenerator* generator) const; + private: + // Methods + Constant(const Boolean* type, const std::string& literal); + + private: + const Boolean* type_; + bool value_; + bool valid_; + friend class Boolean; +}; + +class Integer::Constant : public codegen::Constant { + public: + ~Constant(); + // Methods + int64_t value() const; + bool is_valid() const; + bool operator<(const Constant& that) const; + // codegen::Constant methods + virtual const Integer* type() const; + virtual void Apply(ConstantCodeGenerator* generator) const; + + private: + // Methods + Constant(const Integer* type, const std::string& literal); + + private: + // Fields + const Integer* type_; + int64_t value_; + bool valid_; + friend class Integer; +}; + +class Float::Constant : public codegen::Constant { + public: + ~Constant(); + // Methods + double value() const; + bool is_valid() const; + bool operator<(const Constant& that) const; + // codegen::Constant methods + virtual const Float* type() const; + virtual void Apply(ConstantCodeGenerator* generator) const; + + private: + // Methods + Constant(const Float* type, const std::string& literal); + + private: + // Fields + const Float* type_; + double value_; + bool valid_; + friend class Float; +}; + +class Enum::Constant : public codegen::Constant { + public: + // Methods + ~Constant(); + const std::string& name() const; + int64_t value() const; + bool is_value_explicit() const; + const Scope& scope() const; + const std::string& internal_name() const; + const Description& description() const; + const std::string& design_description() const; + + // codegen::Constant methods + virtual const Enum* type() const; + virtual void Apply(ConstantCodeGenerator* generator) const; + + private: + // Methods + Constant(Enum* enm, const std::string& name, const int64_t value, + bool value_explicit, Scope scope, const std::string& internal_name, + const Description& description, const std::string& design_description); + private: + // Fields + Enum* type_; + std::string name_; + int64_t value_; + bool value_explicit_; + Scope scope_; + std::string internal_name_; + Description description_; + std::string design_description_; + friend class Enum; +}; + +class ConstantCodeGenerator { + public: + virtual void GenerateCodeForBooleanConstant(const Boolean::Constant* boolean); + virtual void GenerateCodeForIntegerConstant(const Integer::Constant* integer); + virtual void GenerateCodeForFloatConstant(const Float::Constant* flt); + virtual void GenerateCodeForEnumConstant(const Enum::Constant* enm); + virtual ~ConstantCodeGenerator(); +}; + +} // namespace codegen + +#endif /* CONSTANT_H_ */ diff --git a/tools/intergen/model/include/model/constants_creator.h b/tools/intergen/model/include/model/constants_creator.h new file mode 100644 index 0000000000..963598418a --- /dev/null +++ b/tools/intergen/model/include/model/constants_creator.h @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef CONSTANTS_CREATOR_H_ +#define CONSTANTS_CREATOR_H_ + +#include <string> + +namespace codegen { +class Constant; + +/* + * Interface for all Types that can have values represented as string literals. + */ +class ConstantsCreator { + public: + // Creates Constant value from given string literal. + // Exact Constant type and literal parsing rules must be defined by Type + // Implementing this interface + // Shall return NULL if literal does not represent valid value. + virtual const Constant* ConstantFor(const std::string& literal) const = 0; + virtual ~ConstantsCreator(); +}; + +} // namespace codegen + +#endif /* CONSTANTS_CREATOR_H_ */ diff --git a/tools/intergen/model/include/model/function.h b/tools/intergen/model/include/model/function.h new file mode 100644 index 0000000000..bbe32a076f --- /dev/null +++ b/tools/intergen/model/include/model/function.h @@ -0,0 +1,126 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FUNCTION_H_ +#define FUNCTION_H_ +#include <map> +#include <string> + +#include "model/constant.h" +#include "model/scope.h" +#include "model/type.h" +#include "utils/common_types.h" +#include "utils/macro.h" +#include "utils/stl_utils.h" + +namespace codegen { +class Interface; + +/* + * Class representing single RPC message and it's parameters, + * can be request, response or notification. + * Message parameters are passed as structures so message is based + * on it's implementation + */ +class FunctionMessage: public Struct { + public: + // Types + enum MessageType { + kRequest, + kResponse, + kNotification, + kUnknownMessageType + }; + // Procudes string given MessageType to be used in class names + static const char* MessageTypeToString(MessageType message_type); + + // Message parameters are passed as structs so every parameter + // is a struct field + typedef Field Parameter; + typedef FieldsList ParametersList; + public: + // Methods + + // Returns message type named by string literal. + // Return kUnknownMessageType if literal names unknown message type + static MessageType MessageTypeFromLiteral(const std::string& literal); + // Creates Function message and associates it with given |interface| + FunctionMessage(const Interface* interface, const std::string& name, + const Enum::Constant* id, MessageType message_type, + const Description& description, Scope scope); + ~FunctionMessage(); + // Returns function ID which is one of the "FunctionID" enum constants + const Enum::Constant* id() const; + // List of function parameters + const ParametersList& parameters() const; + // Constructs message parameter and stores it + bool AddParameter(const std::string& name, const Type* type, + const Constant* default_value, bool is_mandatory, + const Description& description, Scope scope, + Platform platform); + const Interface* interface() const; + +private: + const Interface* interface_; + const Enum::Constant* id_; + MessageType message_type_; + DISALLOW_COPY_AND_ASSIGN(FunctionMessage); +}; + +typedef FunctionMessage Request; +typedef FunctionMessage Response; +typedef FunctionMessage Notification; + +/* + * A container that bounds a request type to it's corresponding response + * type + */ +class Function { + public: + Function(const Request* request, const Response* response); + + const Request& request() const { + return *request_; + } + + const Response& response() const { + return *response_; + } + + private: + const Request* request_; + const Response* response_; +}; + +} // namespace codegen + +#endif /* FUNCTION_H_ */ diff --git a/tools/intergen/model/include/model/interface.h b/tools/intergen/model/include/model/interface.h new file mode 100644 index 0000000000..dc9398eb3e --- /dev/null +++ b/tools/intergen/model/include/model/interface.h @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef INTERFACE_H_ +#define INTERFACE_H_ + +#include <vector> + +#include "model/function.h" +#include "model/composite_type.h" +#include "model/type_registry.h" +#include "utils/stl_utils.h" + +namespace pugi { +class xml_node; +} // namespace pugi + +namespace codegen { +class API; +class BuiltinTypeRegistry; +class Interface; +class ModelFilter; + +/* + * An interface found in XML document. Holds a registry of all the + * structs, enums and functions found in interface definition + */ +class Interface { + public: + // Types + typedef std::map<std::string, FunctionMessage*> MessagesMap; + typedef std::vector<Function> FunctionsList; + typedef std::vector<const Request*> RequestList; + typedef std::vector<const Response*> ResponseList; + typedef std::vector<const Notification*> NotificationList; + typedef TypeRegistry::EnumList EnumList; + typedef TypeRegistry::StructList StructList; + typedef TypeRegistry::TypedefList TypedefList; + public: + // Methods + Interface(const API* api, + bool auto_generate_function_ids, + BuiltinTypeRegistry* builtin_type_registry, + const ModelFilter* model_filter); + ~Interface(); + // API this interface belongs to + const API& api() const; + // Name of the interface + const std::string& name() const; + // List of all functions (requests and appropriate responses) in the interface + const FunctionsList& functions() const; + // List of all requests in the interface + RequestList all_requests() const; + // List of all the responses (including generic) in the interface + ResponseList all_responses() const; + // List of all the notifications in the interface + const NotificationList& notifications() const; + // List of generic responses that are not connected to particular request + const ResponseList& generic_responses() const; + // List of all the enums defined in the interface (exept FunctionID special purpose enum) + const EnumList& enums() const; + // List of all structs defined in the interface + const StructList& structs() const; + // List of all typedefs defined in the interface + const TypedefList& typedefs() const; + // Special-purpose enum that assigns numerical ID's for all API messages + const Enum* function_id_enum() const; + + // Follows parsed |xml| document validating and constructin type tree + bool init(const pugi::xml_node& xml); + + // Finds a type with |name| defined in this interface + const Type* GetNamedType(const std::string& name) const; + + private: + // Methods + // Finds (or creates, depending on generation prefs) function id + // enum constant given |function_id| + const Enum::Constant* GetFunctionIdEnumConstant( + const std::string& function_id); + // Find and add all the functions from the given |xml_interafce| + bool AddFunctions(const pugi::xml_node& xml_interface); + // Validate and add single function message + bool AddFunctionMessage(MessagesMap* list, + FunctionMessage::MessageType message_type, + const pugi::xml_node& xml_message); + // Validate and add all the function message parameters + bool AddFunctionMessageParameters(FunctionMessage* function_message, + const pugi::xml_node& xml_message); + bool SeparateFunctionMessages(); + private: + // Fields + std::string name_; + const API* api_; + BuiltinTypeRegistry* builtin_type_registry_; + const ModelFilter* model_filter_; + bool auto_generate_function_ids_; + Enum function_ids_enum_; + TypeRegistry type_registry_; + MessagesMap requests_; + utils::StdMapDeleter<MessagesMap> requests_deleter_; + MessagesMap responses_; + utils::StdMapDeleter<MessagesMap> responses_deleter_; + MessagesMap notifications_; + utils::StdMapDeleter<MessagesMap> notifications_deleter_; + FunctionsList functions_list_; + ResponseList generic_responses_list_; + NotificationList notifications_list_; + private: + DISALLOW_COPY_AND_ASSIGN(Interface); +}; + +} // namespace codegen + +#endif /* INTERFACE_H_ */ diff --git a/tools/intergen/model/include/model/model_filter.h b/tools/intergen/model/include/model/model_filter.h new file mode 100644 index 0000000000..e0e4f1f6cf --- /dev/null +++ b/tools/intergen/model/include/model/model_filter.h @@ -0,0 +1,57 @@ +/* Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MODEL_FILTER_H +#define MODEL_FILTER_H + +#include <set> + +#include "model/scope.h" + +namespace codegen { + +/* + * Class represents modifications that should be done to parsed model while + * building it from xml + */ +class ModelFilter { +public: + // Creates filter that skips all entities marked with scope + ModelFilter(const std::set<std::string>& filtered_scope_names); + // Tells whether entity with this scope should be skipped + bool ShouldFilterScope(const Scope& scope) const; +private: + const std::set<Scope> filtered_scopes_; +}; + +} // namespace codegen + +#endif // MODEL_FILTER_H diff --git a/tools/intergen/model/include/model/scope.h b/tools/intergen/model/include/model/scope.h new file mode 100644 index 0000000000..92dbbd5d5a --- /dev/null +++ b/tools/intergen/model/include/model/scope.h @@ -0,0 +1,52 @@ +/** +* Copyright (c) 2014, Ford Motor Company +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following +* disclaimer in the documentation and/or other materials provided with the +* distribution. +* +* Neither the name of the Ford Motor Company nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef SCOPE_H_ +#define SCOPE_H_ +#include <string> +#include <iosfwd> + +namespace codegen { + +// Here are defined types for different scope qualifier found in XML specification +// It is not yet clear what they are and how to use them so they are just strings +typedef std::string Scope; +typedef std::string InternalScope; +typedef std::string Platform; + +Scope ScopeFromLiteral(const std::string& literal); +InternalScope InternalScopeFromLiteral(const std::string& literal); +Platform PlatformFromLiteral(const std::string& literal); + +} // namespace codegen + +#endif /* SCOPE_H_ */ diff --git a/tools/intergen/model/include/model/type.h b/tools/intergen/model/include/model/type.h new file mode 100644 index 0000000000..25aa55b652 --- /dev/null +++ b/tools/intergen/model/include/model/type.h @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TYPE_H_ +#define TYPE_H_ + + +namespace codegen { + +class Boolean; +class Integer; +class Float; +class String; +class Enum; +class Array; +class Map; +class Struct; +class NullableType; +class Typedef; + +class ConstantsCreator; + +/* + * An interface for Type visitor that is used to generate code + * for one of the types (built-in or composite) + * An object implementing TypeCodeGenerator should be applied to + * the abstract Type, which in turn calls one of the methods below + * methods depending on it's exact type. + */ +class TypeCodeGenerator { + public: + // Called to generate code for exact type + virtual void GenerateCodeForBoolean(const Boolean* boolean); + virtual void GenerateCodeForInteger(const Integer* integer); + virtual void GenerateCodeForFloat(const Float* flt); + virtual void GenerateCodeForString(const String* string); + virtual void GenerateCodeForEnum(const Enum* enm); + virtual void GenerateCodeForArray(const Array* array); + virtual void GenerateCodeForMap(const Map* map); + virtual void GenerateCodeForNullable(const NullableType* nullable); + virtual void GenerateCodeForStruct(const Struct* strct); + virtual void GenerateCodeForTypedef(const Typedef* tdef); + virtual ~TypeCodeGenerator(); +}; + +/* + * An interface for all the types that can be defined by XML interface specification. + */ +class Type { + public: + // Accepts |code_generator| visitor calling one of it's methods + // (depending on exact type) with this object + virtual TypeCodeGenerator* Apply(TypeCodeGenerator* code_generator) const = 0; + // Get factory that creates constants from given string literals + // Returns NULL if type values can not be represented with string literals + virtual const ConstantsCreator* SupportsConstants() const = 0; + virtual ~Type(); +}; + + +} // namespace codegen + +#endif /* TYPE_H_ */ diff --git a/tools/intergen/model/include/model/type_registry.h b/tools/intergen/model/include/model/type_registry.h new file mode 100644 index 0000000000..6263d28fe4 --- /dev/null +++ b/tools/intergen/model/include/model/type_registry.h @@ -0,0 +1,152 @@ +/** + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TYPE_REGISTRY_H_ +#define TYPE_REGISTRY_H_ + +#include <map> +#include <set> +#include <string> +#include <list> + +#include "model/composite_type.h" +#include "model/type.h" +#include "utils/macro.h" +#include "utils/stl_utils.h" + +namespace pugi { +class xml_node; +} // namespace pugi + +namespace codegen { +class BuiltinTypeRegistry; +class ModelFilter; + +/* + * Type registry is a container holding definitions of all the structs and + * enums found in a single interface xml document element. + */ +class TypeRegistry { + public: + // Types + typedef std::list<Enum*> EnumList; + typedef std::map<std::string, const Enum*> EnumByName; + typedef std::list<Struct*> StructList; + typedef std::map<std::string, const Struct*> StructByName; + typedef std::list<Typedef*> TypedefList; + typedef std::map<std::string, const Typedef*> TypedefByName; + public: + // Methods + TypeRegistry(const Interface* interface, + BuiltinTypeRegistry* builtin_type_registry, + Enum* function_ids_enum, + const ModelFilter* model_filter, + bool create_function_id_enum); + ~TypeRegistry(); + // Follows given xml_node collectin all the struct and enum definitons + // Returns false and prints to cerr if invalid type definition was found + bool init(const pugi::xml_node& xml); + // Finds (or creates) Type object corresponding to type name and parameters + // found in |params|. + // Returns false if unknown type or invalid params were found + bool GetCompositeType(const pugi::xml_node& params, const Type** type); + // Finds type delcare in this interface with given name. + // Type can be enum, struct or typedef + // Returns NULL no type with such name registered + const Type* GetType(const std::string& name) const; + + // Returns list of all enums keeping order of definitions in xml + const EnumList& enums() const; + // Returns list of all structs keeping order of definitions in xml + const StructList& structs() const; + + // Returns list of all typedefs keeping order of definitions in xml + const TypedefList& typedefs() const; + + // Tells if param which properties are passed in |param| is mandatory + static bool IsMandatoryParam(const pugi::xml_node& param); + + private: + // methods + bool AddEnums(const pugi::xml_node& xml); + bool AddStructsAndTypedefs(const pugi::xml_node& xml); + bool AddEnum(const pugi::xml_node& xml_enum); + bool AddStruct(const pugi::xml_node& xml_struct); + bool AddTypedef(const pugi::xml_node& xml_typedef); + bool AddEnumConstants(Enum* enm, const pugi::xml_node& xml_enum); + bool AddStructureFields(Struct* strct, const pugi::xml_node& xml_struct); + // Create container (map or array) |type| object based on xml |params| + // if |get_array| is true array is created, otherwise map is created + // Returns false and prints to cerr on error + bool GetContainer(const pugi::xml_node& params, const Type** type, + bool get_array, bool container_nullable); + bool GetNonArray(const pugi::xml_node& params, + const Type** type, + bool nullable); + bool GetNullable(const Type* original_type, const Type** type); + bool GetEnum(const std::string& name, const Type** type) const; + bool GetStruct(const std::string& name, const Type** type) const; + bool GetTypedef(const std::string& name, const Type** type) const; + bool GetExternalType(const std::string& full_type_name, + const Type** type) const; + bool IsRegisteredEnum(const std::string& enum_name) const; + bool IsRegisteredStruct(const std::string& struct_name) const; + bool IsRegisteredTypedef(const std::string& typedef_name) const; + // Returns true if given type is defined in external interface + bool IsExternalType(const std::string& full_type_name) const; + private: + // fields + const Interface* interface_; + BuiltinTypeRegistry* builtin_type_registry_; + // Special FunctionID enum. Enum is filled by TypeRegistry if it is + // defined in input file. Otherwise it is filled when interface is parsed. + Enum* function_ids_enum_; + const ModelFilter* model_filter_; + std::set<Array> arrays_; + std::set<Map> maps_; + std::set<NullableType> nullables_; + EnumList enums_; + utils::StdContainerDeleter<EnumList> enums_deleter_; + EnumByName enum_by_name_; + StructList structs_; + utils::StdContainerDeleter<StructList> structs_deleter_; + StructByName struct_by_name_; + TypedefList typedefs_; + utils::StdContainerDeleter<TypedefList> typedefs_deleter_; + TypedefByName typedef_by_name_; + private: + DISALLOW_COPY_AND_ASSIGN(TypeRegistry); +}; + +} // namespace codegen + +#endif /* TYPE_REGISTRY_H_ */ |