Home | History | Annotate | Download | only in aidl
      1 /*
      2  * Copyright (C) 2015, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef AIDL_AST_CPP_H_
     18 #define AIDL_AST_CPP_H_
     19 
     20 #include <memory>
     21 #include <string>
     22 #include <vector>
     23 
     24 #include <android-base/macros.h>
     25 
     26 namespace android {
     27 namespace aidl {
     28 class CodeWriter;
     29 }  // namespace aidl
     30 }  // namespace android
     31 
     32 namespace android {
     33 namespace aidl {
     34 namespace cpp {
     35 
     36 class AstNode {
     37  public:
     38   AstNode() = default;
     39   virtual ~AstNode() = default;
     40   virtual void Write(CodeWriter* to) const = 0;
     41 };  // class AstNode
     42 
     43 class Declaration : public AstNode {
     44  public:
     45   Declaration() = default;
     46   virtual ~Declaration() = default;
     47 
     48  private:
     49   DISALLOW_COPY_AND_ASSIGN(Declaration);
     50 };  // class Declaration
     51 
     52 class ClassDecl : public Declaration {
     53  public:
     54   ClassDecl(const std::string& name,
     55             const std::string& parent);
     56   ClassDecl(const std::string& name,
     57             const std::string& parent,
     58             std::vector<std::unique_ptr<Declaration>> public_members,
     59             std::vector<std::unique_ptr<Declaration>> private_members);
     60   virtual ~ClassDecl() = default;
     61 
     62   void Write(CodeWriter* to) const override;
     63 
     64   void AddPublic(std::unique_ptr<Declaration> member);
     65   void AddPrivate(std::unique_ptr<Declaration> member);
     66 
     67  private:
     68   std::string name_;
     69   std::string parent_;
     70   std::vector<std::unique_ptr<Declaration>> public_members_;
     71   std::vector<std::unique_ptr<Declaration>> private_members_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(ClassDecl);
     74 };  // class ClassDecl
     75 
     76 class Enum : public Declaration {
     77  public:
     78   Enum(const std::string& name, const std::string& base_type);
     79   explicit Enum(const std::string& name);
     80   virtual ~Enum() = default;
     81 
     82   bool HasValues() const { return !fields_.empty(); }
     83   void Write(CodeWriter* to) const override;
     84 
     85   void AddValue(const std::string& key, const std::string& value);
     86 
     87  private:
     88   struct EnumField {
     89     EnumField(const std::string& k, const std::string& v);
     90     const std::string key;
     91     const std::string value;
     92   };
     93 
     94   std::string enum_name_;
     95   std::string underlying_type_;
     96   std::vector<EnumField> fields_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(Enum);
     99 };  // class Enum
    100 
    101 class ArgList : public AstNode {
    102  public:
    103   ArgList() = default;
    104   explicit ArgList(const std::string& single_argument);
    105   explicit ArgList(const std::vector<std::string>& arg_list);
    106   explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
    107   ArgList(ArgList&& arg_list);
    108   virtual ~ArgList() = default;
    109 
    110   void Write(CodeWriter* to) const override;
    111 
    112  private:
    113   std::vector<std::unique_ptr<AstNode>> arguments_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(ArgList);
    116 };  // class ArgList
    117 
    118 class ConstructorDecl : public Declaration {
    119  public:
    120   enum Modifiers {
    121     IS_VIRTUAL = 1 << 0,
    122     IS_DEFAULT = 1 << 1,
    123     IS_EXPLICIT = 1 << 2,
    124   };
    125 
    126   ConstructorDecl(const std::string& name,
    127                   ArgList&& arg_list);
    128   ConstructorDecl(const std::string& name,
    129                   ArgList&& arg_list,
    130                   uint32_t modifiers);
    131 
    132   virtual ~ConstructorDecl() = default;
    133 
    134   void Write(CodeWriter* to) const override;
    135 
    136  private:
    137   const std::string name_;
    138   const ArgList arguments_;
    139   const uint32_t modifiers_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(ConstructorDecl);
    142 };  // class ConstructorDecl
    143 
    144 class MacroDecl : public Declaration {
    145  public:
    146   MacroDecl(const std::string& name, ArgList&& arg_list);
    147   virtual ~MacroDecl() = default;
    148 
    149   void Write(CodeWriter* to) const override;
    150 
    151  private:
    152   const std::string name_;
    153   const ArgList arguments_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(MacroDecl);
    156 };  // class MacroDecl
    157 
    158 class MethodDecl : public Declaration {
    159  public:
    160   enum Modifiers {
    161     IS_CONST = 1 << 0,
    162     IS_VIRTUAL = 1 << 1,
    163     IS_OVERRIDE = 1 << 2,
    164     IS_PURE_VIRTUAL = 1 << 3,
    165     IS_STATIC = 1 << 4,
    166   };
    167 
    168   MethodDecl(const std::string& return_type,
    169              const std::string& name,
    170              ArgList&& arg_list);
    171   MethodDecl(const std::string& return_type,
    172              const std::string& name,
    173              ArgList&& arg_list,
    174              uint32_t modifiers);
    175   virtual ~MethodDecl() = default;
    176 
    177   void Write(CodeWriter* to) const override;
    178 
    179  private:
    180   const std::string return_type_;
    181   const std::string name_;
    182   const ArgList arguments_;
    183   bool is_const_ = false;
    184   bool is_virtual_ = false;
    185   bool is_override_ = false;
    186   bool is_pure_virtual_ = false;
    187   bool is_static_ = true;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(MethodDecl);
    190 };  // class MethodDecl
    191 
    192 class StatementBlock : public Declaration {
    193  public:
    194   StatementBlock() = default;
    195   virtual ~StatementBlock() = default;
    196 
    197   void AddStatement(std::unique_ptr<AstNode> statement);
    198   void AddStatement(AstNode* statement);  // Takes ownership
    199   void AddLiteral(const std::string& expression, bool add_semicolon = true);
    200   bool Empty() const { return statements_.empty(); }
    201 
    202   void Write(CodeWriter* to) const override;
    203 
    204  private:
    205   std::vector<std::unique_ptr<AstNode>> statements_;
    206 
    207   DISALLOW_COPY_AND_ASSIGN(StatementBlock);
    208 };  // class StatementBlock
    209 
    210 class ConstructorImpl : public Declaration {
    211  public:
    212   ConstructorImpl(const std::string& class_name,
    213                   ArgList&& arg_list,
    214                   const std::vector<std::string>& initializer_list);
    215   virtual ~ConstructorImpl() = default;
    216 
    217   void Write(CodeWriter* to) const override;
    218 
    219  private:
    220   std::string class_name_;
    221   ArgList arguments_;
    222   std::vector<std::string> initializer_list_;
    223   StatementBlock body_;
    224 
    225   DISALLOW_COPY_AND_ASSIGN(ConstructorImpl);
    226 };  // class ConstructorImpl
    227 
    228 class MethodImpl : public Declaration {
    229  public:
    230   // Passing an empty class name causes the method to be declared as a normal
    231   // function (ie. no ClassName:: qualifier).
    232   MethodImpl(const std::string& return_type,
    233              const std::string& class_name,
    234              const std::string& method_name,
    235              ArgList&& arg_list,
    236              bool is_const_method = false);
    237   virtual ~MethodImpl() = default;
    238 
    239   // MethodImpl retains ownership of the statement block.
    240   StatementBlock* GetStatementBlock();
    241 
    242   void Write(CodeWriter* to) const override;
    243 
    244  private:
    245   std::string return_type_;
    246   std::string method_name_;
    247   const ArgList arguments_;
    248   StatementBlock statements_;
    249   bool is_const_method_ = false;
    250 
    251   DISALLOW_COPY_AND_ASSIGN(MethodImpl);
    252 };  // class MethodImpl
    253 
    254 class SwitchStatement : public AstNode {
    255  public:
    256   explicit SwitchStatement(const std::string& expression);
    257   virtual ~SwitchStatement() = default;
    258 
    259   // Add a case statement and return a pointer code block corresponding
    260   // to the case.  The switch statement will add a break statement
    261   // after the code block by default to prevent accidental fall-through.
    262   // Returns nullptr on duplicate value expressions (by strcmp, not value
    263   // equivalence).
    264   StatementBlock* AddCase(const std::string& value_expression);
    265   void Write(CodeWriter* to) const override;
    266 
    267  private:
    268   const std::string switch_expression_;
    269   std::vector<std::string> case_values_;
    270   std::vector<std::unique_ptr<StatementBlock>> case_logic_;
    271 
    272   DISALLOW_COPY_AND_ASSIGN(SwitchStatement);
    273 };  // class SwitchStatement
    274 
    275 class Assignment : public AstNode {
    276  public:
    277   Assignment(const std::string& left, const std::string& right);
    278   Assignment(const std::string& left, AstNode* right);
    279   ~Assignment() = default;
    280   void Write(CodeWriter* to) const override;
    281 
    282  private:
    283   const std::string lhs_;
    284   std::unique_ptr<AstNode> rhs_;
    285 
    286   DISALLOW_COPY_AND_ASSIGN(Assignment);
    287 };  // class Assignment
    288 
    289 class MethodCall : public AstNode {
    290  public:
    291   MethodCall(const std::string& method_name,
    292              const std::string& single_argument);
    293   MethodCall(const std::string& method_name, ArgList&& arg_list);
    294   ~MethodCall() = default;
    295   void Write(CodeWriter* to) const override;
    296 
    297  private:
    298   const std::string method_name_;
    299   const ArgList arguments_;
    300 
    301   DISALLOW_COPY_AND_ASSIGN(MethodCall);
    302 };  // class MethodCall
    303 
    304 class IfStatement : public AstNode {
    305  public:
    306   explicit IfStatement(AstNode* expression,
    307               bool invert_expression = false);
    308   virtual ~IfStatement() = default;
    309   StatementBlock* OnTrue() { return &on_true_; }
    310   StatementBlock* OnFalse() { return &on_false_; }
    311   void Write(CodeWriter* to) const override;
    312 
    313  private:
    314   std::unique_ptr<AstNode> expression_;
    315   bool invert_expression_ = false;
    316   StatementBlock on_true_;
    317   StatementBlock on_false_;
    318 
    319   DISALLOW_COPY_AND_ASSIGN(IfStatement);
    320 };  // class IfStatement
    321 
    322 class Statement : public AstNode {
    323  public:
    324   explicit Statement(std::unique_ptr<AstNode> expression);
    325   explicit Statement(AstNode* expression);  // Takes possession.
    326   explicit Statement(const std::string& expression);
    327   ~Statement() = default;
    328   void Write(CodeWriter* to) const override;
    329 
    330  private:
    331   std::unique_ptr<AstNode> expression_;
    332 
    333   DISALLOW_COPY_AND_ASSIGN(Statement);
    334 };  // class Statement
    335 
    336 class Comparison : public AstNode {
    337  public:
    338   Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
    339   ~Comparison() = default;
    340   void Write(CodeWriter* to) const override;
    341 
    342  private:
    343   std::unique_ptr<AstNode> left_;
    344   std::unique_ptr<AstNode> right_;
    345   const std::string operator_;
    346 
    347   DISALLOW_COPY_AND_ASSIGN(Comparison);
    348 };  // class Comparison
    349 
    350 class LiteralExpression : public AstNode {
    351  public:
    352   explicit LiteralExpression(const std::string& expression);
    353   ~LiteralExpression() = default;
    354   void Write(CodeWriter* to) const override;
    355 
    356  private:
    357   const std::string expression_;
    358 
    359   DISALLOW_COPY_AND_ASSIGN(LiteralExpression);
    360 };  // class LiteralExpression
    361 
    362 class CppNamespace : public Declaration {
    363  public:
    364   CppNamespace(const std::string& name,
    365                std::vector<std::unique_ptr<Declaration>> declarations);
    366   CppNamespace(const std::string& name,
    367                std::unique_ptr<Declaration> declaration);
    368   explicit CppNamespace(const std::string& name);
    369   virtual ~CppNamespace() = default;
    370 
    371   void Write(CodeWriter* to) const override;
    372 
    373  private:
    374   std::vector<std::unique_ptr<Declaration>> declarations_;
    375   std::string name_;
    376 
    377   DISALLOW_COPY_AND_ASSIGN(CppNamespace);
    378 };  // class CppNamespace
    379 
    380 class Document : public AstNode {
    381  public:
    382   Document(const std::vector<std::string>& include_list,
    383            std::unique_ptr<CppNamespace> a_namespace);
    384 
    385   void Write(CodeWriter* to) const override;
    386 
    387  private:
    388   std::vector<std::string> include_list_;
    389   std::unique_ptr<CppNamespace> namespace_;
    390 
    391   DISALLOW_COPY_AND_ASSIGN(Document);
    392 };  // class Document
    393 
    394 class CppHeader final : public Document {
    395  public:
    396   CppHeader(const std::string& include_guard,
    397             const std::vector<std::string>& include_list,
    398             std::unique_ptr<CppNamespace> a_namespace);
    399   void Write(CodeWriter* to) const override;
    400 
    401  private:
    402   const std::string include_guard_;
    403 
    404   DISALLOW_COPY_AND_ASSIGN(CppHeader);
    405 };  // class CppHeader
    406 
    407 class CppSource final : public Document {
    408  public:
    409   CppSource(const std::vector<std::string>& include_list,
    410             std::unique_ptr<CppNamespace> a_namespace);
    411 
    412  private:
    413   DISALLOW_COPY_AND_ASSIGN(CppSource);
    414 };  // class CppSource
    415 
    416 }  // namespace cpp
    417 }  // namespace aidl
    418 }  // namespace android
    419 
    420 #endif // AIDL_AST_CPP_H_
    421