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