Home | History | Annotate | Download | only in aidl
      1 #ifndef AIDL_AIDL_LANGUAGE_H_
      2 #define AIDL_AIDL_LANGUAGE_H_
      3 
      4 #include <memory>
      5 #include <string>
      6 #include <vector>
      7 
      8 #include <android-base/macros.h>
      9 #include <android-base/strings.h>
     10 
     11 #include <io_delegate.h>
     12 
     13 struct yy_buffer_state;
     14 typedef yy_buffer_state* YY_BUFFER_STATE;
     15 
     16 class AidlToken {
     17  public:
     18   AidlToken(const std::string& text, const std::string& comments);
     19 
     20   const std::string& GetText() const { return text_; }
     21   const std::string& GetComments() const { return comments_; }
     22 
     23  private:
     24   std::string text_;
     25   std::string comments_;
     26 
     27   DISALLOW_COPY_AND_ASSIGN(AidlToken);
     28 };
     29 
     30 class AidlNode {
     31  public:
     32   AidlNode() = default;
     33   virtual ~AidlNode() = default;
     34 
     35  private:
     36   DISALLOW_COPY_AND_ASSIGN(AidlNode);
     37 };
     38 
     39 namespace android {
     40 namespace aidl {
     41 
     42 class ValidatableType;
     43 
     44 }  // namespace aidl
     45 }  // namespace android
     46 
     47 class AidlType : public AidlNode {
     48  public:
     49   enum Annotation : uint32_t {
     50     AnnotationNone = 0,
     51     AnnotationNullable = 1 << 0,
     52     AnnotationUtf8 = 1 << 1,
     53     AnnotationUtf8InCpp = 1 << 2,
     54   };
     55 
     56   AidlType(const std::string& name, unsigned line,
     57            const std::string& comments, bool is_array);
     58   virtual ~AidlType() = default;
     59 
     60   const std::string& GetName() const { return name_; }
     61   unsigned GetLine() const { return line_; }
     62   bool IsArray() const { return is_array_; }
     63   const std::string& GetComments() const { return comments_; }
     64 
     65   std::string ToString() const;
     66 
     67   void SetLanguageType(const android::aidl::ValidatableType* language_type) {
     68     language_type_ = language_type;
     69   }
     70 
     71   template<typename T>
     72   const T* GetLanguageType() const {
     73     return reinterpret_cast<const T*>(language_type_);
     74   }
     75 
     76   void Annotate(AidlType::Annotation annotation) { annotations_ = annotation; }
     77   bool IsNullable() const {
     78     return annotations_ & AnnotationNullable;
     79   }
     80   bool IsUtf8() const {
     81     return annotations_ & AnnotationUtf8;
     82   }
     83   bool IsUtf8InCpp() const {
     84     return annotations_ & AnnotationUtf8InCpp;
     85   }
     86 
     87  private:
     88   std::string name_;
     89   unsigned line_;
     90   bool is_array_;
     91   std::string comments_;
     92   const android::aidl::ValidatableType* language_type_ = nullptr;
     93   Annotation annotations_ = AnnotationNone;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(AidlType);
     96 };
     97 
     98 class AidlArgument : public AidlNode {
     99  public:
    100   enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
    101 
    102   AidlArgument(AidlArgument::Direction direction, AidlType* type,
    103                std::string name, unsigned line);
    104   AidlArgument(AidlType* type, std::string name, unsigned line);
    105   virtual ~AidlArgument() = default;
    106 
    107   Direction GetDirection() const { return direction_; }
    108   bool IsOut() const { return direction_ & OUT_DIR; }
    109   bool IsIn() const { return direction_ & IN_DIR; }
    110   bool DirectionWasSpecified() const { return direction_specified_; }
    111 
    112   std::string GetName() const { return name_; }
    113   int GetLine() const { return line_; }
    114   const AidlType& GetType() const { return *type_; }
    115   AidlType* GetMutableType() { return type_.get(); }
    116 
    117   std::string ToString() const;
    118 
    119  private:
    120   std::unique_ptr<AidlType> type_;
    121   Direction direction_;
    122   bool direction_specified_;
    123   std::string name_;
    124   unsigned line_;
    125 
    126   DISALLOW_COPY_AND_ASSIGN(AidlArgument);
    127 };
    128 
    129 class AidlMethod;
    130 class AidlConstant;
    131 class AidlMember : public AidlNode {
    132  public:
    133   AidlMember() = default;
    134   virtual ~AidlMember() = default;
    135 
    136   virtual AidlMethod* AsMethod() { return nullptr; }
    137   virtual AidlConstant* AsConstant() { return nullptr; }
    138 
    139  private:
    140   DISALLOW_COPY_AND_ASSIGN(AidlMember);
    141 };
    142 
    143 class AidlConstant : public AidlMember {
    144  public:
    145   AidlConstant(std::string name, int32_t value);
    146   virtual ~AidlConstant() = default;
    147 
    148   const std::string& GetName() const { return name_; }
    149   int GetValue() const { return value_; }
    150 
    151   AidlConstant* AsConstant() override { return this; }
    152 
    153  private:
    154   std::string name_;
    155   int32_t value_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(AidlConstant);
    158 };
    159 
    160 class AidlMethod : public AidlMember {
    161  public:
    162   AidlMethod(bool oneway, AidlType* type, std::string name,
    163              std::vector<std::unique_ptr<AidlArgument>>* args,
    164              unsigned line, const std::string& comments);
    165   AidlMethod(bool oneway, AidlType* type, std::string name,
    166              std::vector<std::unique_ptr<AidlArgument>>* args,
    167              unsigned line, const std::string& comments, int id);
    168   virtual ~AidlMethod() = default;
    169 
    170   AidlMethod* AsMethod() override { return this; }
    171 
    172   const std::string& GetComments() const { return comments_; }
    173   const AidlType& GetType() const { return *type_; }
    174   AidlType* GetMutableType() { return type_.get(); }
    175   bool IsOneway() const { return oneway_; }
    176   const std::string& GetName() const { return name_; }
    177   unsigned GetLine() const { return line_; }
    178   bool HasId() const { return has_id_; }
    179   int GetId() { return id_; }
    180   void SetId(unsigned id) { id_ = id; }
    181 
    182   const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
    183     return arguments_;
    184   }
    185   // An inout parameter will appear in both GetInArguments()
    186   // and GetOutArguments().  AidlMethod retains ownership of the argument
    187   // pointers returned in this way.
    188   const std::vector<const AidlArgument*>& GetInArguments() const {
    189     return in_arguments_;
    190   }
    191   const std::vector<const AidlArgument*>& GetOutArguments() const {
    192     return out_arguments_;
    193   }
    194 
    195  private:
    196   bool oneway_;
    197   std::string comments_;
    198   std::unique_ptr<AidlType> type_;
    199   std::string name_;
    200   unsigned line_;
    201   const std::vector<std::unique_ptr<AidlArgument>> arguments_;
    202   std::vector<const AidlArgument*> in_arguments_;
    203   std::vector<const AidlArgument*> out_arguments_;
    204   bool has_id_;
    205   int id_;
    206 
    207   DISALLOW_COPY_AND_ASSIGN(AidlMethod);
    208 };
    209 
    210 class AidlParcelable;
    211 class AidlInterface;
    212 class AidlDocument : public AidlNode {
    213  public:
    214   AidlDocument() = default;
    215   AidlDocument(AidlInterface* interface);
    216   virtual ~AidlDocument() = default;
    217 
    218   const AidlInterface* GetInterface() const { return interface_.get(); }
    219   AidlInterface* ReleaseInterface() { return interface_.release(); }
    220 
    221   const std::vector<std::unique_ptr<AidlParcelable>>& GetParcelables() const {
    222     return parcelables_;
    223   }
    224 
    225   void AddParcelable(AidlParcelable* parcelable) {
    226     parcelables_.push_back(std::unique_ptr<AidlParcelable>(parcelable));
    227   }
    228 
    229  private:
    230   std::vector<std::unique_ptr<AidlParcelable>> parcelables_;
    231   std::unique_ptr<AidlInterface> interface_;
    232 
    233   DISALLOW_COPY_AND_ASSIGN(AidlDocument);
    234 };
    235 
    236 class AidlQualifiedName : public AidlNode {
    237  public:
    238   AidlQualifiedName(std::string term, std::string comments);
    239   virtual ~AidlQualifiedName() = default;
    240 
    241   const std::vector<std::string>& GetTerms() const { return terms_; }
    242   const std::string& GetComments() const { return comments_; }
    243   std::string GetDotName() const { return android::base::Join(terms_, '.'); }
    244 
    245   void AddTerm(std::string term);
    246 
    247  private:
    248   std::vector<std::string> terms_;
    249   std::string comments_;
    250 
    251   DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
    252 };
    253 
    254 class AidlParcelable : public AidlNode {
    255  public:
    256   AidlParcelable(AidlQualifiedName* name, unsigned line,
    257                  const std::vector<std::string>& package,
    258                  const std::string& cpp_header = "");
    259   virtual ~AidlParcelable() = default;
    260 
    261   std::string GetName() const { return name_->GetDotName(); }
    262   unsigned GetLine() const { return line_; }
    263   std::string GetPackage() const;
    264   const std::vector<std::string>& GetSplitPackage() const { return package_; }
    265   std::string GetCppHeader() const { return cpp_header_; }
    266   std::string GetCanonicalName() const;
    267 
    268  private:
    269   std::unique_ptr<AidlQualifiedName> name_;
    270   unsigned line_;
    271   const std::vector<std::string> package_;
    272   std::string cpp_header_;
    273 
    274   DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
    275 };
    276 
    277 class AidlInterface : public AidlNode {
    278  public:
    279   AidlInterface(const std::string& name, unsigned line,
    280                 const std::string& comments, bool oneway_,
    281                 std::vector<std::unique_ptr<AidlMember>>* members,
    282                 const std::vector<std::string>& package);
    283   virtual ~AidlInterface() = default;
    284 
    285   const std::string& GetName() const { return name_; }
    286   unsigned GetLine() const { return line_; }
    287   const std::string& GetComments() const { return comments_; }
    288   bool IsOneway() const { return oneway_; }
    289   const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
    290       { return methods_; }
    291   const std::vector<std::unique_ptr<AidlConstant>>& GetConstants() const
    292       { return constants_; }
    293   std::string GetPackage() const;
    294   std::string GetCanonicalName() const;
    295   const std::vector<std::string>& GetSplitPackage() const { return package_; }
    296 
    297   void SetLanguageType(const android::aidl::ValidatableType* language_type) {
    298     language_type_ = language_type;
    299   }
    300 
    301   template<typename T>
    302   const T* GetLanguageType() const {
    303     return reinterpret_cast<const T*>(language_type_);
    304   }
    305 
    306  private:
    307   std::string name_;
    308   std::string comments_;
    309   unsigned line_;
    310   bool oneway_;
    311   std::vector<std::unique_ptr<AidlMethod>> methods_;
    312   std::vector<std::unique_ptr<AidlConstant>> constants_;
    313   std::vector<std::string> package_;
    314 
    315   const android::aidl::ValidatableType* language_type_ = nullptr;
    316 
    317   DISALLOW_COPY_AND_ASSIGN(AidlInterface);
    318 };
    319 
    320 class AidlImport : public AidlNode {
    321  public:
    322   AidlImport(const std::string& from, const std::string& needed_class,
    323              unsigned line);
    324   virtual ~AidlImport() = default;
    325 
    326   const std::string& GetFileFrom() const { return from_; }
    327   const std::string& GetFilename() const { return filename_; }
    328   const std::string& GetNeededClass() const { return needed_class_; }
    329   unsigned GetLine() const { return line_; }
    330 
    331   void SetFilename(const std::string& filename) { filename_ = filename; }
    332 
    333  private:
    334   std::string from_;
    335   std::string filename_;
    336   std::string needed_class_;
    337   unsigned line_;
    338 
    339   DISALLOW_COPY_AND_ASSIGN(AidlImport);
    340 };
    341 
    342 class Parser {
    343  public:
    344   explicit Parser(const android::aidl::IoDelegate& io_delegate);
    345   ~Parser();
    346 
    347   // Parse contents of file |filename|.
    348   bool ParseFile(const std::string& filename);
    349 
    350   void ReportError(const std::string& err, unsigned line);
    351 
    352   bool FoundNoErrors() const { return error_ == 0; }
    353   const std::string& FileName() const { return filename_; }
    354   void* Scanner() const { return scanner_; }
    355 
    356   void SetDocument(AidlDocument* doc) { document_.reset(doc); };
    357 
    358   void AddImport(AidlQualifiedName* name, unsigned line);
    359 
    360   std::vector<std::string> Package() const;
    361   void SetPackage(AidlQualifiedName* name) { package_.reset(name); }
    362 
    363   AidlDocument* GetDocument() const { return document_.get(); }
    364   AidlDocument* ReleaseDocument() { return document_.release(); }
    365   const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
    366     return imports_;
    367   }
    368 
    369   void ReleaseImports(std::vector<std::unique_ptr<AidlImport>>* ret) {
    370       *ret = std::move(imports_);
    371       imports_.clear();
    372   }
    373 
    374  private:
    375   const android::aidl::IoDelegate& io_delegate_;
    376   int error_ = 0;
    377   std::string filename_;
    378   std::unique_ptr<AidlQualifiedName> package_;
    379   void* scanner_ = nullptr;
    380   std::unique_ptr<AidlDocument> document_;
    381   std::vector<std::unique_ptr<AidlImport>> imports_;
    382   std::unique_ptr<std::string> raw_buffer_;
    383   YY_BUFFER_STATE buffer_;
    384 
    385   DISALLOW_COPY_AND_ASSIGN(Parser);
    386 };
    387 
    388 #endif // AIDL_AIDL_LANGUAGE_H_
    389