• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 #include "aidl_typenames.h"
4 #include "code_writer.h"
5 #include "io_delegate.h"
6 
7 #include <cassert>
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <android-base/macros.h>
13 #include <android-base/strings.h>
14 
15 struct yy_buffer_state;
16 typedef yy_buffer_state* YY_BUFFER_STATE;
17 
18 using android::aidl::AidlTypenames;
19 using android::aidl::CodeWriter;
20 using std::shared_ptr;
21 using std::string;
22 using std::unique_ptr;
23 using std::vector;
24 
25 class AidlNode;
26 
27 namespace android {
28 namespace aidl {
29 namespace mappings {
30 std::string dump_location(const AidlNode& method);
31 }  // namespace mappings
32 }  // namespace aidl
33 }  // namespace android
34 
35 class AidlToken {
36  public:
37   AidlToken(const std::string& text, const std::string& comments);
38 
GetText()39   const std::string& GetText() const { return text_; }
GetComments()40   const std::string& GetComments() const { return comments_; }
41 
42  private:
43   std::string text_;
44   std::string comments_;
45 
46   DISALLOW_COPY_AND_ASSIGN(AidlToken);
47 };
48 
49 class AidlLocation {
50  public:
51   struct Point {
52     unsigned int line;
53     unsigned int column;
54   };
55 
56   AidlLocation(const std::string& file, Point begin, Point end);
57 
58   friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
59   friend class AidlNode;
60 
61  private:
62   const std::string file_;
63   Point begin_;
64   Point end_;
65 };
66 
67 #define AIDL_LOCATION_HERE                   \
68   AidlLocation {                             \
69     __FILE__, {__LINE__, 0}, { __LINE__, 0 } \
70   }
71 
72 std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
73 
74 // Anything that is locatable in a .aidl file.
75 class AidlNode {
76  public:
77   AidlNode(const AidlLocation& location);
78 
79   AidlNode(const AidlNode&) = default;
80   AidlNode(AidlNode&&) = default;
81   virtual ~AidlNode() = default;
82 
83   // DO NOT ADD. This is intentionally omitted. Nothing should refer to the location
84   // for a functional purpose. It is only for error messages.
85   // NO const AidlLocation& GetLocation() const { return location_; } NO
86 
87   // To be able to print AidlLocation (nothing else should use this information)
88   friend class AidlError;
89   friend std::string android::aidl::mappings::dump_location(const AidlNode&);
90 
91  private:
92   std::string PrintLocation() const;
93   const AidlLocation location_;
94 };
95 
96 // Generic point for printing any error in the AIDL compiler.
97 class AidlError {
98  public:
AidlError(bool fatal,const std::string & filename)99   AidlError(bool fatal, const std::string& filename) : AidlError(fatal) { os_ << filename << ": "; }
AidlError(bool fatal,const AidlLocation & location)100   AidlError(bool fatal, const AidlLocation& location) : AidlError(fatal) {
101     os_ << location << ": ";
102   }
AidlError(bool fatal,const AidlNode & node)103   AidlError(bool fatal, const AidlNode& node) : AidlError(fatal, node.location_) {}
AidlError(bool fatal,const AidlNode * node)104   AidlError(bool fatal, const AidlNode* node) : AidlError(fatal, *node) {}
105 
106   template <typename T>
AidlError(bool fatal,const std::unique_ptr<T> & node)107   AidlError(bool fatal, const std::unique_ptr<T>& node) : AidlError(fatal, *node) {}
~AidlError()108   ~AidlError() {
109     os_ << std::endl;
110     if (fatal_) abort();
111   }
112 
113   std::ostream& os_;
114 
115  private:
116   AidlError(bool fatal);
117 
118   bool fatal_;
119 
120   DISALLOW_COPY_AND_ASSIGN(AidlError);
121 };
122 
123 #define AIDL_ERROR(CONTEXT) ::AidlError(false /*fatal*/, (CONTEXT)).os_
124 #define AIDL_FATAL(CONTEXT) ::AidlError(true /*fatal*/, (CONTEXT)).os_
125 #define AIDL_FATAL_IF(CONDITION, CONTEXT) \
126   if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
127 
128 namespace android {
129 namespace aidl {
130 
131 class ValidatableType;
132 class AidlTypenames;
133 
134 }  // namespace aidl
135 }  // namespace android
136 
137 class AidlAnnotation : public AidlNode {
138  public:
139   static AidlAnnotation* Parse(const AidlLocation& location, const string& name);
140 
141   AidlAnnotation(const AidlAnnotation&) = default;
142   AidlAnnotation(AidlAnnotation&&) = default;
143   virtual ~AidlAnnotation() = default;
144 
GetName()145   const string& GetName() const { return name_; }
ToString()146   string ToString() const { return "@" + name_; }
GetComments()147   const string& GetComments() const { return comments_; }
SetComments(const string & comments)148   void SetComments(const string& comments) { comments_ = comments; }
149 
150  private:
151   AidlAnnotation(const AidlLocation& location, const string& name);
152   const string name_;
153   string comments_;
154 };
155 
156 static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
157   return lhs.GetName() < rhs.GetName();
158 }
159 static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
160   return lhs.GetName() == rhs.GetName();
161 }
162 
163 class AidlAnnotatable : public AidlNode {
164  public:
165   AidlAnnotatable(const AidlLocation& location);
166 
167   AidlAnnotatable(const AidlAnnotatable&) = default;
168   AidlAnnotatable(AidlAnnotatable&&) = default;
169   virtual ~AidlAnnotatable() = default;
170 
Annotate(vector<AidlAnnotation> && annotations)171   void Annotate(vector<AidlAnnotation>&& annotations) { annotations_ = std::move(annotations); }
172   bool IsNullable() const;
173   bool IsUtf8InCpp() const;
174   bool IsUnsupportedAppUsage() const;
175   bool IsSystemApi() const;
176   bool IsStableParcelable() const;
177   std::string ToString() const;
178 
GetAnnotations()179   const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
180 
181  private:
182   vector<AidlAnnotation> annotations_;
183 };
184 
185 class AidlQualifiedName;
186 
187 // AidlTypeSpecifier represents a reference to either a built-in type,
188 // a defined type, or a variant (e.g., array of generic) of a type.
189 class AidlTypeSpecifier final : public AidlAnnotatable {
190  public:
191   AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
192                     vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
193   virtual ~AidlTypeSpecifier() = default;
194 
195   // Copy of this type which is not an array.
196   AidlTypeSpecifier ArrayBase() const;
197 
198   // Returns the full-qualified name of the base type.
199   // int -> int
200   // int[] -> int
201   // List<String> -> List
202   // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
GetName()203   const string& GetName() const {
204     if (IsResolved()) {
205       return fully_qualified_name_;
206     } else {
207       return GetUnresolvedName();
208     }
209   }
210 
211   // Returns string representation of this type specifier.
212   // This is GetBaseTypeName() + array modifieir or generic type parameters
213   string ToString() const;
214 
215   std::string Signature() const;
216 
GetUnresolvedName()217   const string& GetUnresolvedName() const { return unresolved_name_; }
218 
GetComments()219   const string& GetComments() const { return comments_; }
220 
SetComments(const string & comment)221   void SetComments(const string& comment) { comments_ = comment; }
222 
IsResolved()223   bool IsResolved() const { return fully_qualified_name_ != ""; }
224 
IsArray()225   bool IsArray() const { return is_array_; }
226 
IsGeneric()227   bool IsGeneric() const { return type_params_ != nullptr; }
228 
GetTypeParameters()229   const vector<unique_ptr<AidlTypeSpecifier>>& GetTypeParameters() const { return *type_params_; }
230 
231   // Resolve the base type name to a fully-qualified name. Return false if the
232   // resolution fails.
233   bool Resolve(android::aidl::AidlTypenames& typenames);
234 
235   bool CheckValid(const AidlTypenames& typenames) const;
236 
SetLanguageType(const android::aidl::ValidatableType * language_type)237   void SetLanguageType(const android::aidl::ValidatableType* language_type) {
238     language_type_ = language_type;
239   }
240 
241   template<typename T>
GetLanguageType()242   const T* GetLanguageType() const {
243     return reinterpret_cast<const T*>(language_type_);
244   }
245  private:
246   AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
247 
248   const string unresolved_name_;
249   string fully_qualified_name_;
250   bool is_array_;
251   const shared_ptr<vector<unique_ptr<AidlTypeSpecifier>>> type_params_;
252   string comments_;
253   const android::aidl::ValidatableType* language_type_ = nullptr;
254 };
255 
256 // Transforms a value string into a language specific form. Raw value as produced by
257 // AidlConstantValue.
258 using ConstantValueDecorator =
259     std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
260 
261 // Returns the universal value unaltered.
262 std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
263 
264 class AidlConstantValue;
265 class AidlVariableDeclaration : public AidlNode {
266  public:
267   AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
268                           const std::string& name);
269   AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
270                           const std::string& name, AidlConstantValue* default_value);
271   virtual ~AidlVariableDeclaration() = default;
272 
GetName()273   std::string GetName() const { return name_; }
GetType()274   const AidlTypeSpecifier& GetType() const { return *type_; }
GetDefaultValue()275   const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
276 
GetMutableType()277   AidlTypeSpecifier* GetMutableType() { return type_.get(); }
278 
279   bool CheckValid(const AidlTypenames& typenames) const;
280   std::string ToString() const;
281   std::string Signature() const;
282 
283   std::string ValueString(const ConstantValueDecorator& decorator) const;
284 
285  private:
286   std::unique_ptr<AidlTypeSpecifier> type_;
287   std::string name_;
288   std::unique_ptr<AidlConstantValue> default_value_;
289 
290   DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
291 };
292 
293 class AidlArgument : public AidlVariableDeclaration {
294  public:
295   enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
296 
297   AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
298                AidlTypeSpecifier* type, const std::string& name);
299   AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
300   virtual ~AidlArgument() = default;
301 
GetDirection()302   Direction GetDirection() const { return direction_; }
IsOut()303   bool IsOut() const { return direction_ & OUT_DIR; }
IsIn()304   bool IsIn() const { return direction_ & IN_DIR; }
DirectionWasSpecified()305   bool DirectionWasSpecified() const { return direction_specified_; }
306   string GetDirectionSpecifier() const;
307 
308   std::string ToString() const;
309   std::string Signature() const;
310 
311  private:
312   Direction direction_;
313   bool direction_specified_;
314 
315   DISALLOW_COPY_AND_ASSIGN(AidlArgument);
316 };
317 
318 class AidlMethod;
319 class AidlConstantDeclaration;
320 class AidlMember : public AidlNode {
321  public:
322   AidlMember(const AidlLocation& location);
323   virtual ~AidlMember() = default;
324 
AsMethod()325   virtual AidlMethod* AsMethod() { return nullptr; }
AsConstantDeclaration()326   virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
327 
328  private:
329   DISALLOW_COPY_AND_ASSIGN(AidlMember);
330 };
331 
332 class AidlConstantValue : public AidlNode {
333  public:
334   enum class Type { ERROR, ARRAY, BOOLEAN, CHARACTER, FLOATING, HEXIDECIMAL, INTEGRAL, STRING };
335 
336   virtual ~AidlConstantValue() = default;
337 
338   static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
339   static AidlConstantValue* Character(const AidlLocation& location, char value);
340   // example: "0x4f"
341   static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
342   static AidlConstantValue* Hex(const AidlLocation& location, const std::string& value);
343   // example: 123, -5498, maybe any size
344   static AidlConstantValue* Integral(const AidlLocation& location, const std::string& value);
345   static AidlConstantValue* Array(const AidlLocation& location,
346                                   std::vector<std::unique_ptr<AidlConstantValue>>* values);
347   // example: "\"asdf\""
348   static AidlConstantValue* String(const AidlLocation& location, const std::string& value);
349 
GetType()350   Type GetType() const { return type_; }
351 
352   bool CheckValid() const;
353 
354   // Raw value of type (currently valid in C++ and Java). Empty string on error.
355   string As(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
356 
357  private:
358   AidlConstantValue(const AidlLocation& location, Type type, const std::string& checked_value);
359   AidlConstantValue(const AidlLocation& location, Type type,
360                     std::vector<std::unique_ptr<AidlConstantValue>>* values);
361   static string ToString(Type type);
362 
363   const Type type_ = Type::ERROR;
364   const std::vector<std::unique_ptr<AidlConstantValue>> values_;  // if type_ == ARRAY
365   const std::string value_;                                       // otherwise
366 
367   DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
368 };
369 
370 class AidlConstantDeclaration : public AidlMember {
371  public:
372   AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
373                           const std::string& name, AidlConstantValue* value);
374   virtual ~AidlConstantDeclaration() = default;
375 
GetType()376   const AidlTypeSpecifier& GetType() const { return *type_; }
GetMutableType()377   AidlTypeSpecifier* GetMutableType() { return type_.get(); }
GetName()378   const std::string& GetName() const { return name_; }
GetValue()379   const AidlConstantValue& GetValue() const { return *value_; }
380   bool CheckValid(const AidlTypenames& typenames) const;
381 
382   std::string ToString() const;
383   std::string Signature() const;
ValueString(const ConstantValueDecorator & decorator)384   string ValueString(const ConstantValueDecorator& decorator) const {
385     return GetValue().As(GetType(), decorator);
386   }
387 
AsConstantDeclaration()388   AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
389 
390  private:
391   const unique_ptr<AidlTypeSpecifier> type_;
392   const std::string name_;
393   const unique_ptr<AidlConstantValue> value_;
394 
395   DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
396 };
397 
398 class AidlMethod : public AidlMember {
399  public:
400   AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
401              const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
402              const std::string& comments);
403   AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
404              const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
405              const std::string& comments, int id, bool is_user_defined = true);
406   virtual ~AidlMethod() = default;
407 
AsMethod()408   AidlMethod* AsMethod() override { return this; }
409 
GetComments()410   const std::string& GetComments() const { return comments_; }
GetType()411   const AidlTypeSpecifier& GetType() const { return *type_; }
GetMutableType()412   AidlTypeSpecifier* GetMutableType() { return type_.get(); }
413 
414   // set if this method is part of an interface that is marked oneway
ApplyInterfaceOneway(bool oneway)415   void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
IsOneway()416   bool IsOneway() const { return oneway_; }
417 
GetName()418   const std::string& GetName() const { return name_; }
HasId()419   bool HasId() const { return has_id_; }
GetId()420   int GetId() const { return id_; }
SetId(unsigned id)421   void SetId(unsigned id) { id_ = id; }
422 
IsUserDefined()423   bool IsUserDefined() const { return is_user_defined_; }
424 
GetArguments()425   const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
426     return arguments_;
427   }
428   // An inout parameter will appear in both GetInArguments()
429   // and GetOutArguments().  AidlMethod retains ownership of the argument
430   // pointers returned in this way.
GetInArguments()431   const std::vector<const AidlArgument*>& GetInArguments() const {
432     return in_arguments_;
433   }
GetOutArguments()434   const std::vector<const AidlArgument*>& GetOutArguments() const {
435     return out_arguments_;
436   }
437 
438   // name + type parameter types
439   // i.e, foo(int, String)
440   std::string Signature() const;
441 
442   // return type + name + type parameter types + annotations
443   // i.e, boolean foo(int, @Nullable String)
444   std::string ToString() const;
445 
446  private:
447   bool oneway_;
448   std::string comments_;
449   std::unique_ptr<AidlTypeSpecifier> type_;
450   std::string name_;
451   const std::vector<std::unique_ptr<AidlArgument>> arguments_;
452   std::vector<const AidlArgument*> in_arguments_;
453   std::vector<const AidlArgument*> out_arguments_;
454   bool has_id_;
455   int id_;
456   bool is_user_defined_ = true;
457 
458   DISALLOW_COPY_AND_ASSIGN(AidlMethod);
459 };
460 
461 class AidlDefinedType;
462 class AidlInterface;
463 class AidlParcelable;
464 class AidlStructuredParcelable;
465 
466 class AidlQualifiedName : public AidlNode {
467  public:
468   AidlQualifiedName(const AidlLocation& location, const std::string& term,
469                     const std::string& comments);
470   virtual ~AidlQualifiedName() = default;
471 
GetTerms()472   const std::vector<std::string>& GetTerms() const { return terms_; }
GetComments()473   const std::string& GetComments() const { return comments_; }
GetDotName()474   std::string GetDotName() const { return android::base::Join(terms_, '.'); }
GetColonName()475   std::string GetColonName() const { return android::base::Join(terms_, "::"); }
476 
477   void AddTerm(const std::string& term);
478 
479  private:
480   std::vector<std::string> terms_;
481   std::string comments_;
482 
483   DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
484 };
485 
486 class AidlInterface;
487 class AidlParcelable;
488 class AidlStructuredParcelable;
489 // AidlDefinedType represents either an interface or a parcelable that is
490 // defined in the source file.
491 class AidlDefinedType : public AidlAnnotatable {
492  public:
493   AidlDefinedType(const AidlLocation& location, const std::string& name,
494                   const std::string& comments, const std::vector<std::string>& package);
495   virtual ~AidlDefinedType() = default;
496 
GetName()497   const std::string& GetName() const { return name_; };
GetComments()498   const std::string& GetComments() const { return comments_; }
SetComments(const std::string comments)499   void SetComments(const std::string comments) { comments_ = comments; }
500 
501   /* dot joined package, example: "android.package.foo" */
502   std::string GetPackage() const;
503   /* dot joined package and name, example: "android.package.foo.IBar" */
504   std::string GetCanonicalName() const;
GetSplitPackage()505   const std::vector<std::string>& GetSplitPackage() const { return package_; }
506 
507   virtual std::string GetPreprocessDeclarationName() const = 0;
508 
AsStructuredParcelable()509   virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
AsParcelable()510   virtual const AidlParcelable* AsParcelable() const { return nullptr; }
AsInterface()511   virtual const AidlInterface* AsInterface() const { return nullptr; }
CheckValid(const AidlTypenames &)512   virtual bool CheckValid(const AidlTypenames&) const { return true; }
513 
AsStructuredParcelable()514   AidlStructuredParcelable* AsStructuredParcelable() {
515     return const_cast<AidlStructuredParcelable*>(
516         const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
517   }
AsParcelable()518   AidlParcelable* AsParcelable() {
519     return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
520   }
AsInterface()521   AidlInterface* AsInterface() {
522     return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
523   }
524 
AsUnstructuredParcelable()525   const AidlParcelable* AsUnstructuredParcelable() const {
526     if (this->AsStructuredParcelable() != nullptr) return nullptr;
527     return this->AsParcelable();
528   }
AsUnstructuredParcelable()529   AidlParcelable* AsUnstructuredParcelable() {
530     return const_cast<AidlParcelable*>(
531         const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
532   }
533 
SetLanguageType(const android::aidl::ValidatableType * language_type)534   void SetLanguageType(const android::aidl::ValidatableType* language_type) {
535     language_type_ = language_type;
536   }
537 
538   template <typename T>
GetLanguageType()539   const T* GetLanguageType() const {
540     return reinterpret_cast<const T*>(language_type_);
541   }
542 
543   virtual void Write(CodeWriter* writer) const = 0;
544 
545  private:
546   std::string name_;
547   std::string comments_;
548   const android::aidl::ValidatableType* language_type_ = nullptr;
549   const std::vector<std::string> package_;
550 
551   DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
552 };
553 
554 class AidlParcelable : public AidlDefinedType {
555  public:
556   AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
557                  const std::vector<std::string>& package, const std::string& comments,
558                  const std::string& cpp_header = "");
559   virtual ~AidlParcelable() = default;
560 
561   // C++ uses "::" instead of "." to refer to a inner class.
GetCppName()562   std::string GetCppName() const { return name_->GetColonName(); }
GetCppHeader()563   std::string GetCppHeader() const { return cpp_header_; }
564 
565   bool CheckValid(const AidlTypenames& typenames) const override;
566 
AsParcelable()567   const AidlParcelable* AsParcelable() const override { return this; }
GetPreprocessDeclarationName()568   std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
569 
570   void Write(CodeWriter* writer) const override;
571 
572  private:
573   std::unique_ptr<AidlQualifiedName> name_;
574   std::string cpp_header_;
575 
576   DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
577 };
578 
579 class AidlStructuredParcelable : public AidlParcelable {
580  public:
581   AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
582                            const std::vector<std::string>& package, const std::string& comments,
583                            std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
584 
GetFields()585   const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
586     return variables_;
587   }
588 
AsStructuredParcelable()589   const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
GetPreprocessDeclarationName()590   std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
591 
592   void Write(CodeWriter* writer) const override;
593 
594   bool CheckValid(const AidlTypenames& typenames) const override;
595 
596  private:
597   const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
598 
599   DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
600 };
601 
602 class AidlInterface final : public AidlDefinedType {
603  public:
604   AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
605                 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
606                 const std::vector<std::string>& package);
607   virtual ~AidlInterface() = default;
608 
GetMethods()609   const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
610       { return methods_; }
GetMutableMethods()611   std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
GetConstantDeclarations()612   const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
613     return constants_;
614   }
615 
AsInterface()616   const AidlInterface* AsInterface() const override { return this; }
GetPreprocessDeclarationName()617   std::string GetPreprocessDeclarationName() const override { return "interface"; }
618 
619   void Write(CodeWriter* writer) const override;
620 
621   bool CheckValid(const AidlTypenames& typenames) const override;
622 
623  private:
624   std::vector<std::unique_ptr<AidlMethod>> methods_;
625   std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
626 
627   DISALLOW_COPY_AND_ASSIGN(AidlInterface);
628 };
629 
630 class AidlImport : public AidlNode {
631  public:
632   AidlImport(const AidlLocation& location, const std::string& needed_class);
633   virtual ~AidlImport() = default;
634 
GetFilename()635   const std::string& GetFilename() const { return filename_; }
GetNeededClass()636   const std::string& GetNeededClass() const { return needed_class_; }
637 
638  private:
639   std::string filename_;
640   std::string needed_class_;
641 
642   DISALLOW_COPY_AND_ASSIGN(AidlImport);
643 };
644 
645 class Parser {
646  public:
647   ~Parser();
648 
649   // Parse contents of file |filename|. Should only be called once.
650   static std::unique_ptr<Parser> Parse(const std::string& filename,
651                                        const android::aidl::IoDelegate& io_delegate,
652                                        AidlTypenames& typenames);
653 
AddError()654   void AddError() { error_++; }
HasError()655   bool HasError() { return error_ != 0; }
656 
FileName()657   const std::string& FileName() const { return filename_; }
Scanner()658   void* Scanner() const { return scanner_; }
659 
660   void AddImport(AidlImport* import);
GetImports()661   const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
662     return imports_;
663   }
ReleaseImports(std::vector<std::unique_ptr<AidlImport>> * ret)664   void ReleaseImports(std::vector<std::unique_ptr<AidlImport>>* ret) {
665     *ret = std::move(imports_);
666     imports_.clear();
667   }
668 
SetPackage(unique_ptr<AidlQualifiedName> name)669   void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
670   std::vector<std::string> Package() const;
671 
DeferResolution(AidlTypeSpecifier * typespec)672   void DeferResolution(AidlTypeSpecifier* typespec) {
673     unresolved_typespecs_.emplace_back(typespec);
674   }
675 
GetUnresolvedTypespecs()676   const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
677 
678   bool Resolve();
679 
AddDefinedType(unique_ptr<AidlDefinedType> type)680   void AddDefinedType(unique_ptr<AidlDefinedType> type) {
681     // Parser does NOT own AidlDefinedType, it just has references to the types
682     // that it encountered while parsing the input file.
683     defined_types_.emplace_back(type.get());
684 
685     // AidlDefinedType IS owned by AidlTypenames
686     if (!typenames_.AddDefinedType(std::move(type))) {
687       AddError();
688     }
689   }
690 
GetDefinedTypes()691   vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
692 
693  private:
694   explicit Parser(const std::string& filename, std::string& raw_buffer,
695                   android::aidl::AidlTypenames& typenames);
696 
697   std::string filename_;
698   std::unique_ptr<AidlQualifiedName> package_;
699   AidlTypenames& typenames_;
700 
701   void* scanner_ = nullptr;
702   YY_BUFFER_STATE buffer_;
703   int error_ = 0;
704 
705   std::vector<std::unique_ptr<AidlImport>> imports_;
706   vector<AidlDefinedType*> defined_types_;
707   vector<AidlTypeSpecifier*> unresolved_typespecs_;
708 
709   DISALLOW_COPY_AND_ASSIGN(Parser);
710 };
711