1 /* 2 * Copyright (C) 2019, 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 #pragma once 18 19 #include "aidl_typenames.h" 20 #include "code_writer.h" 21 #include "io_delegate.h" 22 #include "options.h" 23 24 #include <memory> 25 #include <regex> 26 #include <string> 27 #include <unordered_set> 28 #include <vector> 29 30 #include <android-base/macros.h> 31 #include <android-base/strings.h> 32 33 struct yy_buffer_state; 34 typedef yy_buffer_state* YY_BUFFER_STATE; 35 36 using android::aidl::AidlTypenames; 37 using android::aidl::CodeWriter; 38 using android::aidl::Options; 39 using std::shared_ptr; 40 using std::string; 41 using std::unique_ptr; 42 using std::vector; 43 class AidlNode; 44 45 namespace android { 46 namespace aidl { 47 namespace mappings { 48 std::string dump_location(const AidlNode& method); 49 } // namespace mappings 50 namespace java { 51 std::string dump_location(const AidlNode& method); 52 } // namespace java 53 } // namespace aidl 54 } // namespace android 55 56 class AidlToken { 57 public: 58 AidlToken(const std::string& text, const std::string& comments); 59 GetText()60 const std::string& GetText() const { return text_; } GetComments()61 const std::string& GetComments() const { return comments_; } 62 63 private: 64 std::string text_; 65 std::string comments_; 66 67 DISALLOW_COPY_AND_ASSIGN(AidlToken); 68 }; 69 70 class AidlLocation { 71 public: 72 struct Point { 73 int line; 74 int column; 75 }; 76 77 AidlLocation(const std::string& file, Point begin, Point end); 78 79 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l); 80 friend class AidlNode; 81 82 private: 83 const std::string file_; 84 Point begin_; 85 Point end_; 86 }; 87 88 #define AIDL_LOCATION_HERE \ 89 AidlLocation { \ 90 __FILE__, {__LINE__, 0}, { __LINE__, 0 } \ 91 } 92 93 std::ostream& operator<<(std::ostream& os, const AidlLocation& l); 94 95 // Anything that is locatable in a .aidl file. 96 class AidlNode { 97 public: 98 AidlNode(const AidlLocation& location); 99 100 AidlNode(const AidlNode&) = default; 101 AidlNode(AidlNode&&) = default; 102 virtual ~AidlNode() = default; 103 104 // DO NOT ADD. This is intentionally omitted. Nothing should refer to the location 105 // for a functional purpose. It is only for error messages. 106 // NO const AidlLocation& GetLocation() const { return location_; } NO 107 108 // To be able to print AidlLocation (nothing else should use this information) 109 friend class AidlError; 110 friend std::string android::aidl::mappings::dump_location(const AidlNode&); 111 friend std::string android::aidl::java::dump_location(const AidlNode&); 112 113 private: 114 std::string PrintLine() const; 115 std::string PrintLocation() const; 116 const AidlLocation location_; 117 }; 118 119 // Generic point for printing any error in the AIDL compiler. 120 class AidlError { 121 public: AidlError(bool fatal,const std::string & filename)122 AidlError(bool fatal, const std::string& filename) : AidlError(fatal) { os_ << filename << ": "; } AidlError(bool fatal,const AidlLocation & location)123 AidlError(bool fatal, const AidlLocation& location) : AidlError(fatal) { 124 os_ << location << ": "; 125 } AidlError(bool fatal,const AidlNode & node)126 AidlError(bool fatal, const AidlNode& node) : AidlError(fatal, node.location_) {} AidlError(bool fatal,const AidlNode * node)127 AidlError(bool fatal, const AidlNode* node) : AidlError(fatal, *node) {} 128 129 template <typename T> AidlError(bool fatal,const std::unique_ptr<T> & node)130 AidlError(bool fatal, const std::unique_ptr<T>& node) : AidlError(fatal, *node) {} ~AidlError()131 ~AidlError() { 132 os_ << std::endl; 133 if (fatal_) abort(); 134 } 135 136 std::ostream& os_; 137 hadError()138 static bool hadError() { return sHadError; } 139 140 private: 141 AidlError(bool fatal); 142 143 bool fatal_; 144 145 static bool sHadError; 146 147 DISALLOW_COPY_AND_ASSIGN(AidlError); 148 }; 149 150 #define AIDL_ERROR(CONTEXT) ::AidlError(false /*fatal*/, (CONTEXT)).os_ 151 #define AIDL_FATAL(CONTEXT) ::AidlError(true /*fatal*/, (CONTEXT)).os_ 152 #define AIDL_FATAL_IF(CONDITION, CONTEXT) \ 153 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": " 154 155 namespace android { 156 namespace aidl { 157 158 class AidlTypenames; 159 160 } // namespace aidl 161 } // namespace android 162 163 // unique_ptr<AidlTypeSpecifier> for type arugment, 164 // std::string for type parameter(T, U, and so on). 165 template <typename T> 166 class AidlParameterizable { 167 public: AidlParameterizable(std::vector<T> * type_params)168 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {} 169 virtual ~AidlParameterizable() = default; IsGeneric()170 bool IsGeneric() const { return type_params_ != nullptr; } GetTypeParameters()171 const std::vector<T>& GetTypeParameters() const { return *type_params_; } 172 bool CheckValid() const; 173 174 virtual const AidlNode& AsAidlNode() const = 0; 175 176 protected: 177 AidlParameterizable(const AidlParameterizable&); 178 179 private: 180 const unique_ptr<std::vector<T>> type_params_; 181 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value || 182 std::is_same<T, std::string>::value); 183 }; 184 template <> 185 bool AidlParameterizable<std::string>::CheckValid() const; 186 187 class AidlConstantValue; 188 class AidlConstantDeclaration; 189 190 // Transforms a value string into a language specific form. Raw value as produced by 191 // AidlConstantValue. 192 using ConstantValueDecorator = 193 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>; 194 195 class AidlAnnotation : public AidlNode { 196 public: 197 static AidlAnnotation* Parse( 198 const AidlLocation& location, const string& name, 199 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list); 200 201 AidlAnnotation(const AidlAnnotation&) = default; 202 AidlAnnotation(AidlAnnotation&&) = default; 203 virtual ~AidlAnnotation() = default; 204 bool CheckValid() const; 205 GetName()206 const string& GetName() const { return name_; } 207 string ToString(const ConstantValueDecorator& decorator) const; 208 std::map<std::string, std::string> AnnotationParams( 209 const ConstantValueDecorator& decorator) const; GetComments()210 const string& GetComments() const { return comments_; } SetComments(const string & comments)211 void SetComments(const string& comments) { comments_ = comments; } 212 213 private: 214 AidlAnnotation(const AidlLocation& location, const string& name); 215 AidlAnnotation(const AidlLocation& location, const string& name, 216 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters); 217 const string name_; 218 string comments_; 219 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_; 220 }; 221 222 static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { 223 return lhs.GetName() < rhs.GetName(); 224 } 225 static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { 226 return lhs.GetName() == rhs.GetName(); 227 } 228 229 class AidlAnnotatable : public AidlNode { 230 public: 231 AidlAnnotatable(const AidlLocation& location); 232 233 AidlAnnotatable(const AidlAnnotatable&) = default; 234 AidlAnnotatable(AidlAnnotatable&&) = default; 235 virtual ~AidlAnnotatable() = default; 236 Annotate(vector<AidlAnnotation> && annotations)237 void Annotate(vector<AidlAnnotation>&& annotations) { 238 for (auto& annotation : annotations) { 239 annotations_.emplace_back(std::move(annotation)); 240 } 241 } 242 bool IsNullable() const; 243 bool IsUtf8InCpp() const; 244 bool IsVintfStability() const; 245 bool IsStableApiParcelable(Options::Language lang) const; 246 bool IsHide() const; 247 248 void DumpAnnotations(CodeWriter* writer) const; 249 250 const AidlAnnotation* UnsupportedAppUsage() const; 251 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const; 252 std::string ToString() const; 253 GetAnnotations()254 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; } 255 bool CheckValidAnnotations() const; 256 257 private: 258 vector<AidlAnnotation> annotations_; 259 }; 260 261 class AidlQualifiedName; 262 263 // AidlTypeSpecifier represents a reference to either a built-in type, 264 // a defined type, or a variant (e.g., array of generic) of a type. 265 class AidlTypeSpecifier final : public AidlAnnotatable, 266 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> { 267 public: 268 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array, 269 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments); 270 virtual ~AidlTypeSpecifier() = default; 271 272 // Copy of this type which is not an array. 273 AidlTypeSpecifier ArrayBase() const; 274 275 // Returns the full-qualified name of the base type. 276 // int -> int 277 // int[] -> int 278 // List<String> -> List 279 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar) GetName()280 const string& GetName() const { 281 if (IsResolved()) { 282 return fully_qualified_name_; 283 } else { 284 return GetUnresolvedName(); 285 } 286 } 287 288 // Returns string representation of this type specifier. 289 // This is GetBaseTypeName() + array modifier or generic type parameters 290 string ToString() const; 291 292 std::string Signature() const; 293 GetUnresolvedName()294 const string& GetUnresolvedName() const { return unresolved_name_; } 295 296 bool IsHidden() const; 297 GetComments()298 const string& GetComments() const { return comments_; } 299 GetSplitName()300 const std::vector<std::string> GetSplitName() const { return split_name_; } 301 SetComments(const string & comment)302 void SetComments(const string& comment) { comments_ = comment; } 303 IsResolved()304 bool IsResolved() const { return fully_qualified_name_ != ""; } 305 IsArray()306 bool IsArray() const { return is_array_; } 307 308 // Resolve the base type name to a fully-qualified name. Return false if the 309 // resolution fails. 310 bool Resolve(const AidlTypenames& typenames); 311 312 bool CheckValid(const AidlTypenames& typenames) const; 313 bool LanguageSpecificCheckValid(Options::Language lang) const; AsAidlNode()314 const AidlNode& AsAidlNode() const override { return *this; } 315 316 private: 317 AidlTypeSpecifier(const AidlTypeSpecifier&) = default; 318 319 const string unresolved_name_; 320 string fully_qualified_name_; 321 bool is_array_; 322 string comments_; 323 vector<string> split_name_; 324 }; 325 326 // Returns the universal value unaltered. 327 std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value); 328 329 class AidlConstantValue; 330 class AidlVariableDeclaration : public AidlNode { 331 public: 332 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, 333 const std::string& name); 334 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, 335 const std::string& name, AidlConstantValue* default_value); 336 virtual ~AidlVariableDeclaration() = default; 337 GetName()338 std::string GetName() const { return name_; } GetType()339 const AidlTypeSpecifier& GetType() const { return *type_; } GetDefaultValue()340 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); } 341 GetMutableType()342 AidlTypeSpecifier* GetMutableType() { return type_.get(); } 343 344 bool CheckValid(const AidlTypenames& typenames) const; 345 std::string ToString() const; 346 std::string Signature() const; 347 348 std::string ValueString(const ConstantValueDecorator& decorator) const; 349 350 private: 351 std::unique_ptr<AidlTypeSpecifier> type_; 352 std::string name_; 353 std::unique_ptr<AidlConstantValue> default_value_; 354 355 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration); 356 }; 357 358 class AidlArgument : public AidlVariableDeclaration { 359 public: 360 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 }; 361 362 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, 363 AidlTypeSpecifier* type, const std::string& name); 364 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name); 365 virtual ~AidlArgument() = default; 366 GetDirection()367 Direction GetDirection() const { return direction_; } IsOut()368 bool IsOut() const { return direction_ & OUT_DIR; } IsIn()369 bool IsIn() const { return direction_ & IN_DIR; } DirectionWasSpecified()370 bool DirectionWasSpecified() const { return direction_specified_; } 371 string GetDirectionSpecifier() const; 372 373 std::string ToString() const; 374 std::string Signature() const; 375 376 private: 377 Direction direction_; 378 bool direction_specified_; 379 380 DISALLOW_COPY_AND_ASSIGN(AidlArgument); 381 }; 382 383 class AidlMethod; 384 class AidlConstantDeclaration; 385 class AidlEnumDeclaration; 386 class AidlMember : public AidlNode { 387 public: 388 AidlMember(const AidlLocation& location); 389 virtual ~AidlMember() = default; 390 AsMethod()391 virtual AidlMethod* AsMethod() { return nullptr; } AsConstantDeclaration()392 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; } 393 394 private: 395 DISALLOW_COPY_AND_ASSIGN(AidlMember); 396 }; 397 398 class AidlUnaryConstExpression; 399 class AidlBinaryConstExpression; 400 401 class AidlConstantValue : public AidlNode { 402 public: 403 enum class Type { 404 // WARNING: Don't change this order! The order is used to determine type 405 // promotion during a binary expression. 406 BOOLEAN, 407 INT8, 408 INT32, 409 INT64, 410 ARRAY, 411 CHARACTER, 412 STRING, 413 FLOATING, 414 UNARY, 415 BINARY, 416 ERROR, 417 }; 418 419 /* 420 * Return the value casted to the given type. 421 */ 422 template <typename T> 423 T cast() const; 424 425 virtual ~AidlConstantValue() = default; 426 427 static AidlConstantValue* Boolean(const AidlLocation& location, bool value); 428 static AidlConstantValue* Character(const AidlLocation& location, char value); 429 // example: 123, -5498, maybe any size 430 static AidlConstantValue* Integral(const AidlLocation& location, const string& value); 431 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value); 432 static AidlConstantValue* Array(const AidlLocation& location, 433 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); 434 // example: "\"asdf\"" 435 static AidlConstantValue* String(const AidlLocation& location, const string& value); 436 437 // Construct an AidlConstantValue by evaluating the other integral constant's 438 // value string. This does not preserve the structure of the copied constant. 439 // Returns nullptr and logs if value cannot be copied. 440 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other); 441 GetType()442 Type GetType() const { return final_type_; } 443 444 virtual bool CheckValid() const; 445 446 // Raw value of type (currently valid in C++ and Java). Empty string on error. 447 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const; 448 449 private: 450 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value, 451 const string& checked_value); 452 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value); 453 AidlConstantValue(const AidlLocation& location, Type type, 454 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); 455 static string ToString(Type type); 456 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type); 457 static bool IsHex(const string& value); 458 459 virtual bool evaluate(const AidlTypeSpecifier& type) const; 460 461 const Type type_ = Type::ERROR; 462 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY 463 const string value_; // otherwise 464 465 // State for tracking evaluation of expressions 466 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate 467 mutable bool is_evaluated_ = false; // whether evaluate has been called 468 mutable Type final_type_; 469 mutable int64_t final_value_; 470 mutable string final_string_value_ = ""; 471 472 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue); 473 474 friend AidlUnaryConstExpression; 475 friend AidlBinaryConstExpression; 476 }; 477 478 class AidlUnaryConstExpression : public AidlConstantValue { 479 public: 480 AidlUnaryConstExpression(const AidlLocation& location, const string& op, 481 std::unique_ptr<AidlConstantValue> rval); 482 483 static bool IsCompatibleType(Type type, const string& op); 484 bool CheckValid() const override; 485 private: 486 bool evaluate(const AidlTypeSpecifier& type) const override; 487 488 std::unique_ptr<AidlConstantValue> unary_; 489 const string op_; 490 }; 491 492 class AidlBinaryConstExpression : public AidlConstantValue { 493 public: 494 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval, 495 const string& op, std::unique_ptr<AidlConstantValue> rval); 496 497 bool CheckValid() const override; 498 499 static bool AreCompatibleTypes(Type t1, Type t2); 500 // Returns the promoted kind for both operands 501 static Type UsualArithmeticConversion(Type left, Type right); 502 // Returns the promoted integral type where INT32 is the smallest type 503 static Type IntegralPromotion(Type in); 504 505 private: 506 bool evaluate(const AidlTypeSpecifier& type) const override; 507 508 std::unique_ptr<AidlConstantValue> left_val_; 509 std::unique_ptr<AidlConstantValue> right_val_; 510 const string op_; 511 }; 512 513 struct AidlAnnotationParameter { 514 std::string name; 515 std::unique_ptr<AidlConstantValue> value; 516 }; 517 518 class AidlConstantDeclaration : public AidlMember { 519 public: 520 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier, 521 const string& name, AidlConstantValue* value); 522 virtual ~AidlConstantDeclaration() = default; 523 GetType()524 const AidlTypeSpecifier& GetType() const { return *type_; } GetMutableType()525 AidlTypeSpecifier* GetMutableType() { return type_.get(); } GetName()526 const string& GetName() const { return name_; } GetValue()527 const AidlConstantValue& GetValue() const { return *value_; } 528 bool CheckValid(const AidlTypenames& typenames) const; 529 530 string ToString() const; 531 string Signature() const; ValueString(const ConstantValueDecorator & decorator)532 string ValueString(const ConstantValueDecorator& decorator) const { 533 return value_->ValueString(GetType(), decorator); 534 } 535 AsConstantDeclaration()536 AidlConstantDeclaration* AsConstantDeclaration() override { return this; } 537 538 private: 539 const unique_ptr<AidlTypeSpecifier> type_; 540 const string name_; 541 unique_ptr<AidlConstantValue> value_; 542 543 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration); 544 }; 545 546 class AidlMethod : public AidlMember { 547 public: 548 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, 549 vector<unique_ptr<AidlArgument>>* args, const string& comments); 550 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, 551 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id, 552 bool is_user_defined = true); 553 virtual ~AidlMethod() = default; 554 AsMethod()555 AidlMethod* AsMethod() override { return this; } 556 bool IsHidden() const; GetComments()557 const string& GetComments() const { return comments_; } GetType()558 const AidlTypeSpecifier& GetType() const { return *type_; } GetMutableType()559 AidlTypeSpecifier* GetMutableType() { return type_.get(); } 560 561 // set if this method is part of an interface that is marked oneway ApplyInterfaceOneway(bool oneway)562 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; } IsOneway()563 bool IsOneway() const { return oneway_; } 564 GetName()565 const std::string& GetName() const { return name_; } HasId()566 bool HasId() const { return has_id_; } GetId()567 int GetId() const { return id_; } SetId(unsigned id)568 void SetId(unsigned id) { id_ = id; } 569 IsUserDefined()570 bool IsUserDefined() const { return is_user_defined_; } 571 GetArguments()572 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const { 573 return arguments_; 574 } 575 // An inout parameter will appear in both GetInArguments() 576 // and GetOutArguments(). AidlMethod retains ownership of the argument 577 // pointers returned in this way. GetInArguments()578 const std::vector<const AidlArgument*>& GetInArguments() const { 579 return in_arguments_; 580 } GetOutArguments()581 const std::vector<const AidlArgument*>& GetOutArguments() const { 582 return out_arguments_; 583 } 584 585 // name + type parameter types 586 // i.e, foo(int, String) 587 std::string Signature() const; 588 589 // return type + name + type parameter types + annotations 590 // i.e, boolean foo(int, @Nullable String) 591 std::string ToString() const; 592 593 private: 594 bool oneway_; 595 std::string comments_; 596 std::unique_ptr<AidlTypeSpecifier> type_; 597 std::string name_; 598 const std::vector<std::unique_ptr<AidlArgument>> arguments_; 599 std::vector<const AidlArgument*> in_arguments_; 600 std::vector<const AidlArgument*> out_arguments_; 601 bool has_id_; 602 int id_; 603 bool is_user_defined_ = true; 604 605 DISALLOW_COPY_AND_ASSIGN(AidlMethod); 606 }; 607 608 class AidlDefinedType; 609 class AidlInterface; 610 class AidlParcelable; 611 class AidlStructuredParcelable; 612 613 class AidlQualifiedName : public AidlNode { 614 public: 615 AidlQualifiedName(const AidlLocation& location, const std::string& term, 616 const std::string& comments); 617 virtual ~AidlQualifiedName() = default; 618 GetTerms()619 const std::vector<std::string>& GetTerms() const { return terms_; } GetComments()620 const std::string& GetComments() const { return comments_; } GetDotName()621 std::string GetDotName() const { return android::base::Join(terms_, '.'); } GetColonName()622 std::string GetColonName() const { return android::base::Join(terms_, "::"); } 623 624 void AddTerm(const std::string& term); 625 626 private: 627 std::vector<std::string> terms_; 628 std::string comments_; 629 630 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName); 631 }; 632 633 class AidlInterface; 634 class AidlParcelable; 635 class AidlStructuredParcelable; 636 // AidlDefinedType represents either an interface, parcelable, or enum that is 637 // defined in the source file. 638 class AidlDefinedType : public AidlAnnotatable { 639 public: 640 AidlDefinedType(const AidlLocation& location, const std::string& name, 641 const std::string& comments, const std::vector<std::string>& package); 642 virtual ~AidlDefinedType() = default; 643 GetName()644 const std::string& GetName() const { return name_; }; 645 bool IsHidden() const; GetComments()646 const std::string& GetComments() const { return comments_; } SetComments(const std::string comments)647 void SetComments(const std::string comments) { comments_ = comments; } 648 649 /* dot joined package, example: "android.package.foo" */ 650 std::string GetPackage() const; 651 /* dot joined package and name, example: "android.package.foo.IBar" */ 652 std::string GetCanonicalName() const; GetSplitPackage()653 const std::vector<std::string>& GetSplitPackage() const { return package_; } 654 655 virtual std::string GetPreprocessDeclarationName() const = 0; 656 AsStructuredParcelable()657 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; } AsParcelable()658 virtual const AidlParcelable* AsParcelable() const { return nullptr; } AsEnumDeclaration()659 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; } AsInterface()660 virtual const AidlInterface* AsInterface() const { return nullptr; } AsParameterizable()661 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; } CheckValid(const AidlTypenames &)662 virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); } 663 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0; AsStructuredParcelable()664 AidlStructuredParcelable* AsStructuredParcelable() { 665 return const_cast<AidlStructuredParcelable*>( 666 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable()); 667 } AsParcelable()668 AidlParcelable* AsParcelable() { 669 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable()); 670 } AsEnumDeclaration()671 AidlEnumDeclaration* AsEnumDeclaration() { 672 return const_cast<AidlEnumDeclaration*>( 673 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration()); 674 } AsInterface()675 AidlInterface* AsInterface() { 676 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface()); 677 } 678 AsParameterizable()679 AidlParameterizable<std::string>* AsParameterizable() { 680 return const_cast<AidlParameterizable<std::string>*>( 681 const_cast<const AidlDefinedType*>(this)->AsParameterizable()); 682 } 683 AsUnstructuredParcelable()684 const AidlParcelable* AsUnstructuredParcelable() const { 685 if (this->AsStructuredParcelable() != nullptr) return nullptr; 686 return this->AsParcelable(); 687 } AsUnstructuredParcelable()688 AidlParcelable* AsUnstructuredParcelable() { 689 return const_cast<AidlParcelable*>( 690 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable()); 691 } 692 693 virtual void Dump(CodeWriter* writer) const = 0; 694 void DumpHeader(CodeWriter* writer) const; 695 696 private: 697 std::string name_; 698 std::string comments_; 699 const std::vector<std::string> package_; 700 701 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType); 702 }; 703 704 class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> { 705 public: 706 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, 707 const std::vector<std::string>& package, const std::string& comments, 708 const std::string& cpp_header = "", 709 std::vector<std::string>* type_params = nullptr); 710 virtual ~AidlParcelable() = default; 711 712 // C++ uses "::" instead of "." to refer to a inner class. GetCppName()713 std::string GetCppName() const { return name_->GetColonName(); } GetCppHeader()714 std::string GetCppHeader() const { return cpp_header_; } 715 716 bool CheckValid(const AidlTypenames& typenames) const override; 717 bool LanguageSpecificCheckValid(Options::Language lang) const override; AsParcelable()718 const AidlParcelable* AsParcelable() const override { return this; } AsParameterizable()719 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; } AsAidlNode()720 const AidlNode& AsAidlNode() const override { return *this; } GetPreprocessDeclarationName()721 std::string GetPreprocessDeclarationName() const override { return "parcelable"; } 722 723 void Dump(CodeWriter* writer) const override; 724 725 private: 726 std::unique_ptr<AidlQualifiedName> name_; 727 std::string cpp_header_; 728 729 DISALLOW_COPY_AND_ASSIGN(AidlParcelable); 730 }; 731 732 class AidlStructuredParcelable : public AidlParcelable { 733 public: 734 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name, 735 const std::vector<std::string>& package, const std::string& comments, 736 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables); 737 GetFields()738 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const { 739 return variables_; 740 } 741 AsStructuredParcelable()742 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; } GetPreprocessDeclarationName()743 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; } 744 745 void Dump(CodeWriter* writer) const override; 746 747 bool CheckValid(const AidlTypenames& typenames) const override; 748 bool LanguageSpecificCheckValid(Options::Language lang) const override; 749 750 private: 751 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_; 752 753 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable); 754 }; 755 756 class AidlEnumerator : public AidlNode { 757 public: 758 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value, 759 const std::string& comments); 760 virtual ~AidlEnumerator() = default; 761 GetName()762 const std::string& GetName() const { return name_; } GetValue()763 AidlConstantValue* GetValue() const { return value_.get(); } GetComments()764 const std::string& GetComments() const { return comments_; } 765 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const; 766 767 string ValueString(const AidlTypeSpecifier& backing_type, 768 const ConstantValueDecorator& decorator) const; 769 SetValue(std::unique_ptr<AidlConstantValue> value)770 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); } 771 772 private: 773 const std::string name_; 774 unique_ptr<AidlConstantValue> value_; 775 const std::string comments_; 776 777 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator); 778 }; 779 780 class AidlEnumDeclaration : public AidlDefinedType { 781 public: 782 AidlEnumDeclaration(const AidlLocation& location, const string& name, 783 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, 784 const std::vector<std::string>& package, const std::string& comments); 785 virtual ~AidlEnumDeclaration() = default; 786 787 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type); GetBackingType()788 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; } GetEnumerators()789 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const { 790 return enumerators_; 791 } 792 bool Autofill(); 793 bool CheckValid(const AidlTypenames& typenames) const override; LanguageSpecificCheckValid(Options::Language)794 bool LanguageSpecificCheckValid(Options::Language) const override { return true; } GetPreprocessDeclarationName()795 std::string GetPreprocessDeclarationName() const override { return "enum"; } 796 void Dump(CodeWriter* writer) const override; 797 AsEnumDeclaration()798 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; } 799 800 private: 801 const std::string name_; 802 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_; 803 std::unique_ptr<const AidlTypeSpecifier> backing_type_; 804 805 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration); 806 }; 807 808 class AidlInterface final : public AidlDefinedType { 809 public: 810 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments, 811 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members, 812 const std::vector<std::string>& package); 813 virtual ~AidlInterface() = default; 814 GetMethods()815 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const 816 { return methods_; } GetMutableMethods()817 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; } GetConstantDeclarations()818 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const { 819 return constants_; 820 } 821 AsInterface()822 const AidlInterface* AsInterface() const override { return this; } GetPreprocessDeclarationName()823 std::string GetPreprocessDeclarationName() const override { return "interface"; } 824 825 void Dump(CodeWriter* writer) const override; 826 827 bool CheckValid(const AidlTypenames& typenames) const override; 828 bool LanguageSpecificCheckValid(Options::Language lang) const override; 829 830 private: 831 std::vector<std::unique_ptr<AidlMethod>> methods_; 832 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_; 833 834 DISALLOW_COPY_AND_ASSIGN(AidlInterface); 835 }; 836 837 class AidlImport : public AidlNode { 838 public: 839 AidlImport(const AidlLocation& location, const std::string& needed_class); 840 virtual ~AidlImport() = default; 841 GetFilename()842 const std::string& GetFilename() const { return filename_; } GetNeededClass()843 const std::string& GetNeededClass() const { return needed_class_; } 844 845 private: 846 std::string filename_; 847 std::string needed_class_; 848 849 DISALLOW_COPY_AND_ASSIGN(AidlImport); 850 }; 851 852 class Parser { 853 public: 854 ~Parser(); 855 856 // Parse contents of file |filename|. Should only be called once. 857 static std::unique_ptr<Parser> Parse(const std::string& filename, 858 const android::aidl::IoDelegate& io_delegate, 859 AidlTypenames& typenames); 860 AddError()861 void AddError() { error_++; } HasError()862 bool HasError() { return error_ != 0; } 863 FileName()864 const std::string& FileName() const { return filename_; } Scanner()865 void* Scanner() const { return scanner_; } 866 867 void AddImport(std::unique_ptr<AidlImport>&& import); GetImports()868 const std::vector<std::unique_ptr<AidlImport>>& GetImports() { 869 return imports_; 870 } 871 SetPackage(unique_ptr<AidlQualifiedName> name)872 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); } 873 std::vector<std::string> Package() const; 874 DeferResolution(AidlTypeSpecifier * typespec)875 void DeferResolution(AidlTypeSpecifier* typespec) { 876 unresolved_typespecs_.emplace_back(typespec); 877 } 878 GetUnresolvedTypespecs()879 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; } 880 881 bool Resolve(); 882 AddDefinedType(unique_ptr<AidlDefinedType> type)883 void AddDefinedType(unique_ptr<AidlDefinedType> type) { 884 // Parser does NOT own AidlDefinedType, it just has references to the types 885 // that it encountered while parsing the input file. 886 defined_types_.emplace_back(type.get()); 887 888 // AidlDefinedType IS owned by AidlTypenames 889 if (!typenames_.AddDefinedType(std::move(type))) { 890 AddError(); 891 } 892 } 893 GetDefinedTypes()894 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; } 895 896 private: 897 explicit Parser(const std::string& filename, std::string& raw_buffer, 898 android::aidl::AidlTypenames& typenames); 899 900 std::string filename_; 901 std::unique_ptr<AidlQualifiedName> package_; 902 AidlTypenames& typenames_; 903 904 void* scanner_ = nullptr; 905 YY_BUFFER_STATE buffer_; 906 int error_ = 0; 907 908 std::vector<std::unique_ptr<AidlImport>> imports_; 909 vector<AidlDefinedType*> defined_types_; 910 vector<AidlTypeSpecifier*> unresolved_typespecs_; 911 912 DISALLOW_COPY_AND_ASSIGN(Parser); 913 }; 914