/** * Tencent is pleased to support the open source community by making Tars available. * * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * https://opensource.org/licenses/BSD-3-Clause * * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ #ifndef _ELEMENT_H #define _ELEMENT_H #include #include #include #include #include #include #include #include #include "errno.h" #include "util/tc_autoptr.h" using namespace std; void yyerror(char const *msg); /** * 语法元素基类 */ class GrammarBase : virtual public tars::TC_HandleBase { public: virtual ~GrammarBase(){}; }; typedef tars::TC_AutoPtr GrammarBasePtr; /** * 解析过程中的字符串 */ class StringGrammar : public GrammarBase { public: StringGrammar() { } string v; }; typedef tars::TC_AutoPtr StringGrammarPtr; /** * 解析过程中的整形数 */ class IntergerGrammar : public GrammarBase { public: IntergerGrammar():v(0) { } long long v; }; typedef tars::TC_AutoPtr IntergerGrammarPtr; /** * 解析过程中的浮点数 */ class FloatGrammar : public GrammarBase { public: FloatGrammar():v(0.0f) { } double v; }; typedef tars::TC_AutoPtr FloatGrammarPtr; /** * 解析过程中的bool类型 */ class BoolGrammar : public GrammarBase { public: BoolGrammar():v(false) { } bool v; }; typedef tars::TC_AutoPtr BoolGrammarPtr; /** * 解析过程中的常类型 */ class ConstGrammar : public GrammarBase { public: ConstGrammar():t(VALUE) { } enum { VALUE, STRING, BOOL, ENUM }; int t; string v; }; typedef tars::TC_AutoPtr ConstGrammarPtr; /////////////////////////////////////////////// /** * 类型基类 */ class Type : virtual public GrammarBase { public: /** * 构造函数 * @param Type */ Type():_size(0),_array(false),_pointer(false){}; //是否简单类型 virtual bool isSimple() const { return false;} //是否数组类型 virtual bool isArray() const { return _array;} virtual void setArray(int size) {_array = true;_size = size;} virtual int getSize() const {return _size;}; //是否指针类型 virtual bool isPointer() const { return _pointer;} virtual void setPointer(bool b) {_pointer = b;} public: int _size; bool _array; bool _pointer; }; typedef tars::TC_AutoPtr TypePtr; /** * 内建类型 */ class Builtin : public Type { public: enum Kind { KindVoid, KindBool, KindByte, KindShort, KindInt, KindLong, KindFloat, KindDouble, KindString, KindVector, KindMap }; /** * 构造函数 * @param kind */ Builtin(Kind kind,bool isUnsigned); /** * 类型 * * @return Kind */ Kind kind() const; /** * 是否是简单类型 * * @return bool */ bool isSimple() const; /** * 是否是unsined类型 * * @return bool */ bool isUnsigned() const {return _isUnsigned;} /** * 设置是unsined类型 * * @return bool */ void setUnsigned(bool isUnsigned = false) {_isUnsigned = isUnsigned;} /** * 缺省值 * * @return string */ string def() const; /** * 字符串标示 */ static const char* builtinTable[]; protected: Kind _kind; bool _isUnsigned; }; typedef tars::TC_AutoPtr BuiltinPtr; /** * Vector类型 */ class Vector : public Type { public: /** * 构造函数 * @param ptr */ Vector(const TypePtr& ptr); /** * 获取类型 * * @return TypePtr& */ TypePtr& getTypePtr() {return _ptr;} protected: TypePtr _ptr; }; typedef tars::TC_AutoPtr VectorPtr; /** * Map类型 */ class Map : public Type { public: /** * 构造函数 * @param pleft * @param pright */ Map(const TypePtr& pleft, const TypePtr& pright); /** * 获取左类型 * * @return TypePtr& */ TypePtr& getLeftTypePtr() {return _pleft;} /** * 获取右类型 * * @return TypePtr& */ TypePtr& getRightTypePtr() {return _pright;} protected: TypePtr _pleft; TypePtr _pright; }; typedef tars::TC_AutoPtr MapPtr; /** * 变量定义 */ class TypeId : public GrammarBase { public: /** * 构造函数 * @param ptr * @param id */ TypeId(const TypePtr& ptr, const string& id); /** * 变量名称 * * @return string */ string getId() const { return _id; } /** * 变量类型 * * @return Type* */ TypePtr& getTypePtr() { return _ptr;} /** * 是否需要该参数 * * @return bool */ bool isRequire() const { return _bRequire; } /** * 设置必选字段 * @param tag */ void setRequire(int tag); /** * 设置可选字段 * 只有基本类型才有缺省值 */ void setDefault(const string &def); /** * * 只有基本类型才有缺省值 */ void disableDefault(); /** * 设置可选字段 * @param tag */ void setOptional(int tag); /** * tag标识 */ int getTag() const { return _tag; } /** * 缺省值 * * @return string */ string def() const { return _default; } /** * 是否有缺省值 * * @return bool */ bool hasDefault() const { return _bHasDefault; } protected: TypePtr _ptr; string _id; bool _bRequire; int _tag; bool _bHasDefault; string _default; public: int _size; bool _array; }; typedef tars::TC_AutoPtr TypeIdPtr; //////////////////////////////////////////////////// // class Namespace; typedef tars::TC_AutoPtr NamespacePtr; /** * 容器基类, 所有可以包含其他元素的元素都从该基类继承 */ class Container : virtual public GrammarBase { public: /** * 构造函数 * @param id */ Container(const string &id) : _id(id) { } /** * 生成名字空间 * @param id * * @return NamespacePtr */ NamespacePtr createNamespace(const string &id); /** * 获取ID * * @return string */ string getId() const { return _id;} /** * 获取所有的名字空间 * * @return vector& */ vector &getAllNamespacePtr() { return _ns; } protected: string _id; vector _ns; }; typedef tars::TC_AutoPtr ContainerPtr; //////////////////////////////////////////////////// // class Const : public GrammarBase { public: /** * * @param tPtr * @param cPtr */ Const(TypeIdPtr &tPtr, ConstGrammarPtr &cPtr) : _tPtr(tPtr), _cPtr(cPtr) { } /** * * * @return TypePtr& */ TypeIdPtr &getTypeIdPtr() { return _tPtr; } /** * * * @return ConstGrammarPtr */ ConstGrammarPtr getConstGrammarPtr() { return _cPtr; } protected: TypeIdPtr _tPtr; ConstGrammarPtr _cPtr; }; typedef tars::TC_AutoPtr ConstPtr; ///////////////////////////////////////////////////////// // class Enum : virtual public Container, virtual public Type { public: Enum(const string &id, const string &sid) : Container(id), _sid(sid) { } /** * 添加成员变量 * @param ptid */ void addMember(const TypeIdPtr &ptr); /** * 获取所有的成员变量 * * @return vector& */ vector& getAllMemberPtr() {return _members;} /** * 获取结构的名称 * * @return string */ string getSid() const { return _sid; } /** * 是否是简单类型 * * @return bool */ virtual bool isSimple() const { return true; } protected: /** * 每个变量名称 */ vector _members; /** * 包含名字的空间的名称 */ string _sid; }; typedef tars::TC_AutoPtr EnumPtr; //////////////////////////////////////////////////// // /** * 结构 */ class Struct : virtual public Container, virtual public Type { public: /** * 构造函数 * @param id */ Struct(const string& id, const string &sid) : Container(id), _sid(sid) { } /** * 添加成员变量 * @param ptid */ void addTypeId(const TypeIdPtr &ptr); /** * 获取所有的成员变量 * * @return vector& */ vector& getAllMemberPtr() {return _members;} /** * 获取结构的名称 * * @return string */ string getSid() const { return _sid; } /** * 增加小于memeber * @param member */ void addKey(const string &member); vector getKey() { return _key; } protected: vector _members; vector _key; string _sid; }; typedef tars::TC_AutoPtr StructPtr; //////////////////////////////////////////////////// // /** * 参数描述 */ class ParamDecl : public GrammarBase { public: /** * 构造 * @param typeIdPtr * @param v */ ParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k) : _typeIdPtr(typeIdPtr), _v(v), _k(k) { } /** * 变量声明 * * @return TypeIdPtr& */ TypeIdPtr& getTypeIdPtr() { return _typeIdPtr; } /** * 是否是输出参数 * * @return bool */ bool isOut() const { return _v; } /** * 是否是需要路由的字段 * * @return bool */ bool isRouteKey() const { return _k; } protected: TypeIdPtr _typeIdPtr; bool _v; bool _k; }; typedef tars::TC_AutoPtr ParamDeclPtr; /////////////////////////////////////////////////////// // /** * 操作类 */ class Operation : public Container { public: /** * 构造函数 * @param id * @param typePtr */ Operation(const string &id, const TypePtr &typePtr) : Container(id), _itag(0) { _retPtr = new TypeId(typePtr, "_ret"); _retPtr->setRequire(_itag); } /** * 生成一个参数 * @param typeIdPtr * @param v * @param k * * @return ParamDeclPtr */ ParamDeclPtr createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k); /** * 获取返回值类型 * * @return TypePtr& */ TypeIdPtr &getReturnPtr() { return _retPtr; } /** * 获取所有参数 * * @return vector& */ vector &getAllParamDeclPtr() { return _ps; } protected: int _itag; TypeIdPtr _retPtr; vector _ps; }; typedef tars::TC_AutoPtr OperationPtr; /////////////////////////////////////////////////////// // /** * 接口描述 */ class Interface : public Container { public: /** * 构造 * @param id */ Interface(const string &id) : Container(id) { } /** * 生成一个操作 * @param id * @param typePtr * * @return OperationPtr */ OperationPtr createOperation(const string &id, const TypePtr &typePtr); /** * 获取所有操作 * * @return vector& */ vector &getAllOperationPtr() { return _ops; } protected: vector _ops; }; typedef tars::TC_AutoPtr InterfacePtr; ///////////////////////////////////////////////////////// // /** * 名字空间 */ class Namespace : public Container { public: /** * 构造函数 * @param id */ Namespace(const string &id) : Container(id) { } /** * 生成接口 * @param id * * @return InterfacePtr */ InterfacePtr createInterface(const string &id); /** * 生成结构 * @param id * * @return StructPtr */ StructPtr createStruct(const string& id); /** * 生成枚举类型 * @param id * * @return EnumPtr */ EnumPtr createEnum(const string &id); /** * * @param pPtr * @param cPtr * * @return ConstPtr */ ConstPtr createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr); /** * 是否有接口 * * @return bool */ bool hasInterface() const { return !_is.empty(); } /** * 获取所有的接口 * * @return vector& */ vector &getAllInterfacePtr() { return _is; } /** * 获取所有的结构 * * @return vector& */ vector &getAllStructPtr() { return _ss; } /** * 生成枚举类型 * * @return vector& */ vector &getAllEnumPtr() { return _es; } /** * 常量类型 * * @return vector& */ vector &getAllConstPtr() { return _cs; } protected: vector _is; vector _ss; vector _es; vector _cs; }; ///////////////////////////////////////////////////////////////// /** * 上下文 */ class Context : public tars::TC_HandleBase { public: /** * 构造函数 */ Context(const string &file) : _currline(1), _filename(file) { } /** * 下一行 */ void nextLine() { _currline++; } /** * 目前的行 * * @return size_t */ size_t getCurrLine() { return _currline; } /** * 当前文件名 * * @return string */ string getFileName() { return _filename; } /** * 添加include的文件 * @param incl */ void addInclude(const string &incl); /** * 添加属于这个文件的名字空间 * @param c */ void addNamespacePtr(const NamespacePtr &c) { _namespaces.push_back(c); } /** * 获取includes的文件 * * @return vector */ vector getIncludes() { return _includes; } /** * 获取名字空间 * * @return vector */ vector getNamespaces() { return _namespaces; } protected: size_t _currline; string _filename; vector _includes; vector _namespaces; }; typedef tars::TC_AutoPtr ContextPtr; #endif