blob: b6c119cbbba636f7925932f9c512903cc897df98 [file] [log] [blame]
Christopher Wiley89e35862015-08-30 10:57:07 -07001#ifndef AIDL_AIDL_LANGUAGE_H_
2#define AIDL_AIDL_LANGUAGE_H_
Adam Lesinskiffa16862014-01-23 18:17:42 -08003
Casey Dahlinbc7a50a2015-09-28 19:20:50 -07004#include <memory>
Casey Dahlindd691812015-09-09 17:59:06 -07005#include <string>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -07006#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -07007
Christopher Wiley7c3a1eb2015-09-12 10:19:52 -07008#include <base/macros.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +00009
Christopher Wiley4a2884b2015-10-07 11:27:45 -070010#include <io_delegate.h>
11
Casey Dahlin89d44842015-09-24 18:45:54 -070012struct yy_buffer_state;
13typedef yy_buffer_state* YY_BUFFER_STATE;
14
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070015enum which_extra_text {
Adam Lesinskiffa16862014-01-23 18:17:42 -080016 NO_EXTRA_TEXT = 0,
17 SHORT_COMMENT,
18 LONG_COMMENT,
19 COPY_TEXT,
20 WHITESPACE
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070021};
Adam Lesinskiffa16862014-01-23 18:17:42 -080022
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070023struct extra_text_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -080024 unsigned lineno;
25 which_extra_text which;
26 char* data;
27 unsigned len;
28 struct extra_text_type* next;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070029};
Adam Lesinskiffa16862014-01-23 18:17:42 -080030
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070031struct buffer_type {
Casey Dahlin082f1d12015-09-21 14:06:25 -070032 unsigned lineno;
33 unsigned token;
34 char *data;
35 extra_text_type* extra;
36
37 std::string Literal() const {
Casey Dahlina834dd42015-09-23 11:52:15 -070038 return data ? std::string(data) : "";
Casey Dahlin082f1d12015-09-21 14:06:25 -070039 }
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070040};
Adam Lesinskiffa16862014-01-23 18:17:42 -080041
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070042class AidlNode {
43 public:
44 AidlNode() = default;
45 virtual ~AidlNode() = default;
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(AidlNode);
49};
50
Casey Dahlin0ee37582015-09-30 16:31:55 -070051class AidlType : public AidlNode {
52 public:
Casey Dahlinf2d23f72015-10-02 16:19:19 -070053 AidlType(const std::string& name, unsigned line,
Casey Dahlinf7a421c2015-10-05 17:24:28 -070054 const std::string& comments, bool is_array);
Casey Dahlin0ee37582015-09-30 16:31:55 -070055 virtual ~AidlType() = default;
56
Casey Dahlinf2d23f72015-10-02 16:19:19 -070057 const std::string& GetName() const { return name_; }
58 unsigned GetLine() const { return line_; }
Casey Dahlinf7a421c2015-10-05 17:24:28 -070059 bool IsArray() const { return is_array_; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -070060 const std::string& GetComments() const { return comments_; }
Casey Dahlin0ee37582015-09-30 16:31:55 -070061
Casey Dahlin70078e62015-09-30 17:01:30 -070062 std::string ToString() const;
Casey Dahlin0ee37582015-09-30 16:31:55 -070063
64 private:
Casey Dahlinf2d23f72015-10-02 16:19:19 -070065 std::string name_;
66 unsigned line_;
Casey Dahlinf7a421c2015-10-05 17:24:28 -070067 bool is_array_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -070068 std::string comments_;
69
Casey Dahlin0ee37582015-09-30 16:31:55 -070070 DISALLOW_COPY_AND_ASSIGN(AidlType);
71};
72
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070073class AidlArgument : public AidlNode {
74 public:
Casey Dahlinc378c992015-09-29 16:50:40 -070075 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
76
Casey Dahlinf2d23f72015-10-02 16:19:19 -070077 AidlArgument(AidlArgument::Direction direction, AidlType* type,
Casey Dahlin308f9d42015-10-05 18:48:42 -070078 std::string name, unsigned line);
79 AidlArgument(AidlType *type, std::string name, unsigned line);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070080 virtual ~AidlArgument() = default;
81
Casey Dahlinc378c992015-09-29 16:50:40 -070082 Direction GetDirection() const { return direction_; }
83 bool DirectionWasSpecified() const { return direction_specified_; }
Casey Dahlind127b502015-09-30 12:51:08 -070084 std::string GetName() const { return name_; }
85 int GetLine() const { return line_; }
Casey Dahlin0ee37582015-09-30 16:31:55 -070086 const AidlType& GetType() const { return *type_; }
Casey Dahlind127b502015-09-30 12:51:08 -070087
Casey Dahlinc378c992015-09-29 16:50:40 -070088 std::string ToString() const;
89
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070090 private:
Casey Dahlin0ee37582015-09-30 16:31:55 -070091 std::unique_ptr<AidlType> type_;
Casey Dahlinc378c992015-09-29 16:50:40 -070092 Direction direction_;
93 bool direction_specified_;
Casey Dahlind127b502015-09-30 12:51:08 -070094 std::string name_;
Casey Dahlin308f9d42015-10-05 18:48:42 -070095 unsigned line_;
Casey Dahlinc378c992015-09-29 16:50:40 -070096
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070097 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -070098};
Adam Lesinskiffa16862014-01-23 18:17:42 -080099
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700100class AidlMethod {
101 public:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700102 AidlMethod(bool oneway, AidlType* type, std::string name,
103 std::vector<std::unique_ptr<AidlArgument>>* args,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700104 unsigned line, const std::string& comments);
Casey Dahlinf4a93112015-10-05 16:58:09 -0700105 AidlMethod(bool oneway, AidlType* type, std::string name,
106 std::vector<std::unique_ptr<AidlArgument>>* args,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700107 unsigned line, const std::string& comments, int id);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700108 virtual ~AidlMethod() = default;
109
Casey Dahlinf4a93112015-10-05 16:58:09 -0700110 const std::string& GetComments() const { return comments_; }
111 const AidlType& GetType() const { return *type_; }
112 bool IsOneway() const { return oneway_; }
113 const std::string& GetName() const { return name_; }
114 unsigned GetLine() const { return line_; }
115 bool HasId() const { return has_id_; }
116 int GetId() { return id_; }
117 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700118
Casey Dahlinf4a93112015-10-05 16:58:09 -0700119 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
120 return arguments_;
121 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700122
123 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700124 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700125 std::string comments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700126 std::unique_ptr<AidlType> type_;
127 std::string name_;
128 unsigned line_;
129 std::vector<std::unique_ptr<AidlArgument>> arguments_;
130 bool has_id_;
131 int id_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700132
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700133 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700134};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800135
136enum {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700137 USER_DATA_TYPE = 12,
138 INTERFACE_TYPE_BINDER
Adam Lesinskiffa16862014-01-23 18:17:42 -0800139};
140
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700141class AidlDocumentItem : public AidlNode {
142 public:
143 AidlDocumentItem() = default;
144 virtual ~AidlDocumentItem() = default;
145
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700146 AidlDocumentItem* next = nullptr;
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700147 unsigned item_type;
148
149 private:
150 DISALLOW_COPY_AND_ASSIGN(AidlDocumentItem);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700151};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700153class AidlParcelable : public AidlDocumentItem {
154 public:
155 AidlParcelable() = default;
156 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800157
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700158 std::string GetName() const { return name.data; }
159 unsigned GetLine() const { return name.lineno; }
160
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700161 buffer_type keyword_token; // only the first one
162 char* package;
163 buffer_type name;
164 buffer_type semicolon_token;
165 bool parcelable;
166
167 private:
168 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700169};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800170
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700171class AidlInterface : public AidlDocumentItem {
172 public:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700173 AidlInterface(const std::string& name, unsigned line,
174 const std::string& comments, bool oneway_,
175 std::vector<std::unique_ptr<AidlMethod>>* methods,
176 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700177 virtual ~AidlInterface() = default;
178
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700179 const std::string& GetName() const { return name_; }
180 unsigned GetLine() const { return line_; }
181 const std::string& GetComments() const { return comments_; }
182 bool IsOneway() const { return oneway_; }
183 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
184 { return methods_; }
185 const std::string& GetPackage() const { return package_; }
186 std::string GetCanonicalName() const { return package_ + "." + name_; }
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700187
188 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700189 std::string name_;
190 std::string comments_;
191 unsigned line_;
192 bool oneway_;
193 std::vector<std::unique_ptr<AidlMethod>> methods_;
194 std::string package_;
195
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700196 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700197};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800198
Adam Lesinskiffa16862014-01-23 18:17:42 -0800199
Adam Lesinskiffa16862014-01-23 18:17:42 -0800200void init_buffer_type(buffer_type* buf, int lineno);
201
Casey Dahlin0edf3422015-10-07 12:34:59 -0700202class AidlImport : public AidlNode {
203 public:
204 AidlImport(const std::string& from, const std::string& needed_class,
205 unsigned line);
206 virtual ~AidlImport() = default;
207
208 const std::string& GetFileFrom() const { return from_; }
209 const std::string& GetFilename() const { return filename_; }
210 const std::string& GetNeededClass() const { return needed_class_; }
211 unsigned GetLine() const { return line_; }
212 const AidlDocumentItem* GetDocument() { return document_.get(); };
213 void SetDocument(AidlDocumentItem* doc) {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700214 document_ = std::unique_ptr<AidlDocumentItem>(doc);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700215 }
216
217 void SetFilename(const std::string& filename) { filename_ = filename; }
218
219 private:
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700220 std::unique_ptr<AidlDocumentItem> document_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700221 std::string from_;
222 std::string filename_;
223 std::string needed_class_;
224 unsigned line_;
225
226 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700227};
228
229class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700230 public:
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700231 explicit Parser(const android::aidl::IoDelegate& io_delegate);
Casey Dahline2507492015-09-14 17:11:20 -0700232 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700233
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700234 // Parse contents of file |filename|.
235 bool ParseFile(const std::string& filename);
Casey Dahlin89d44842015-09-24 18:45:54 -0700236
Casey Dahlindd691812015-09-09 17:59:06 -0700237 void ReportError(const std::string& err);
238
Casey Dahlin3c6df362015-10-06 15:48:35 -0700239 bool FoundNoErrors() const { return error_ == 0; }
240 const std::string& FileName() const { return filename_; }
241 const std::string& Package() const { return package_; }
242 void *Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700243
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700244 void SetDocument(AidlDocumentItem *items) { document_ = items; };
Casey Dahline2507492015-09-14 17:11:20 -0700245
Casey Dahlin3c6df362015-10-06 15:48:35 -0700246 void AddImport(std::vector<std::string>* terms, unsigned line);
247 void SetPackage(std::vector<std::string>* terms);
248
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700249 AidlDocumentItem *GetDocument() const { return document_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700250 const std::vector<std::unique_ptr<AidlImport>>& GetImports() { return imports_; }
251
252 void ReleaseImports(std::vector<std::unique_ptr<AidlImport>>* ret) {
253 *ret = std::move(imports_);
254 imports_.clear();
255 }
Casey Dahlindd691812015-09-09 17:59:06 -0700256
257 private:
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700258 const android::aidl::IoDelegate& io_delegate_;
Casey Dahlindd691812015-09-09 17:59:06 -0700259 int error_ = 0;
260 std::string filename_;
261 std::string package_;
262 void *scanner_ = nullptr;
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700263 AidlDocumentItem* document_ = nullptr;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700264 std::vector<std::unique_ptr<AidlImport>> imports_;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700265 std::unique_ptr<std::string> raw_buffer_;
Casey Dahlin89d44842015-09-24 18:45:54 -0700266 YY_BUFFER_STATE buffer_;
Casey Dahlindd691812015-09-09 17:59:06 -0700267
Casey Dahline2507492015-09-14 17:11:20 -0700268 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700269};
270
Christopher Wiley89e35862015-08-30 10:57:07 -0700271#endif // AIDL_AIDL_LANGUAGE_H_