aidl_language.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. #include "aidl_language.h"
  2. #include "aidl_typenames.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <iostream>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <utility>
  13. #include <android-base/parsedouble.h>
  14. #include <android-base/parseint.h>
  15. #include <android-base/strings.h>
  16. #include "aidl_language_y.h"
  17. #include "logging.h"
  18. #include "type_java.h"
  19. #include "type_namespace.h"
  20. #ifdef _WIN32
  21. int isatty(int fd)
  22. {
  23. return (fd == 0);
  24. }
  25. #endif
  26. using android::aidl::IoDelegate;
  27. using android::base::Join;
  28. using android::base::Split;
  29. using std::cerr;
  30. using std::endl;
  31. using std::pair;
  32. using std::set;
  33. using std::string;
  34. using std::unique_ptr;
  35. using std::vector;
  36. void yylex_init(void **);
  37. void yylex_destroy(void *);
  38. void yyset_in(FILE *f, void *);
  39. int yyparse(Parser*);
  40. YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *);
  41. void yy_delete_buffer(YY_BUFFER_STATE, void *);
  42. AidlToken::AidlToken(const std::string& text, const std::string& comments)
  43. : text_(text),
  44. comments_(comments) {}
  45. AidlLocation::AidlLocation(const std::string& file, Point begin, Point end)
  46. : file_(file), begin_(begin), end_(end) {}
  47. std::ostream& operator<<(std::ostream& os, const AidlLocation& l) {
  48. os << l.file_ << ":" << l.begin_.line << "." << l.begin_.column << "-";
  49. if (l.begin_.line != l.end_.line) {
  50. os << l.end_.line << ".";
  51. }
  52. os << l.end_.column;
  53. return os;
  54. }
  55. AidlNode::AidlNode(const AidlLocation& location) : location_(location) {}
  56. std::string AidlNode::PrintLocation() const {
  57. std::stringstream ss;
  58. ss << location_.file_ << ":" << location_.begin_.line;
  59. return ss.str();
  60. }
  61. AidlError::AidlError(bool fatal) : os_(std::cerr), fatal_(fatal) {
  62. os_ << "ERROR: ";
  63. }
  64. static const string kNullable("nullable");
  65. static const string kUtf8InCpp("utf8InCpp");
  66. static const string kUnsupportedAppUsage("UnsupportedAppUsage");
  67. static const string kSystemApi("SystemApi");
  68. static const string kStableParcelable("JavaOnlyStableParcelable");
  69. static const set<string> kAnnotationNames{kNullable, kUtf8InCpp, kUnsupportedAppUsage, kSystemApi,
  70. kStableParcelable};
  71. AidlAnnotation* AidlAnnotation::Parse(const AidlLocation& location, const string& name) {
  72. if (kAnnotationNames.find(name) == kAnnotationNames.end()) {
  73. std::ostringstream stream;
  74. stream << "'" << name << "' is not a recognized annotation. ";
  75. stream << "It must be one of:";
  76. for (const string& kv : kAnnotationNames) {
  77. stream << " " << kv;
  78. }
  79. stream << ".";
  80. AIDL_ERROR(location) << stream.str();
  81. return nullptr;
  82. }
  83. return new AidlAnnotation(location, name);
  84. }
  85. AidlAnnotation::AidlAnnotation(const AidlLocation& location, const string& name)
  86. : AidlNode(location), name_(name) {}
  87. static bool HasAnnotation(const vector<AidlAnnotation>& annotations, const string& name) {
  88. for (const auto& a : annotations) {
  89. if (a.GetName() == name) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
  96. bool AidlAnnotatable::IsNullable() const {
  97. return HasAnnotation(annotations_, kNullable);
  98. }
  99. bool AidlAnnotatable::IsUtf8InCpp() const {
  100. return HasAnnotation(annotations_, kUtf8InCpp);
  101. }
  102. bool AidlAnnotatable::IsUnsupportedAppUsage() const {
  103. return HasAnnotation(annotations_, kUnsupportedAppUsage);
  104. }
  105. bool AidlAnnotatable::IsSystemApi() const {
  106. return HasAnnotation(annotations_, kSystemApi);
  107. }
  108. bool AidlAnnotatable::IsStableParcelable() const {
  109. return HasAnnotation(annotations_, kStableParcelable);
  110. }
  111. string AidlAnnotatable::ToString() const {
  112. vector<string> ret;
  113. for (const auto& a : annotations_) {
  114. ret.emplace_back(a.ToString());
  115. }
  116. std::sort(ret.begin(), ret.end());
  117. return Join(ret, " ");
  118. }
  119. AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
  120. bool is_array,
  121. vector<unique_ptr<AidlTypeSpecifier>>* type_params,
  122. const string& comments)
  123. : AidlAnnotatable(location),
  124. unresolved_name_(unresolved_name),
  125. is_array_(is_array),
  126. type_params_(type_params),
  127. comments_(comments) {}
  128. AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const {
  129. AIDL_FATAL_IF(!is_array_, this);
  130. AidlTypeSpecifier arrayBase = *this;
  131. arrayBase.is_array_ = false;
  132. return arrayBase;
  133. }
  134. string AidlTypeSpecifier::ToString() const {
  135. string ret = GetName();
  136. if (IsGeneric()) {
  137. vector<string> arg_names;
  138. for (const auto& ta : GetTypeParameters()) {
  139. arg_names.emplace_back(ta->ToString());
  140. }
  141. ret += "<" + Join(arg_names, ",") + ">";
  142. }
  143. if (IsArray()) {
  144. ret += "[]";
  145. }
  146. return ret;
  147. }
  148. string AidlTypeSpecifier::Signature() const {
  149. string ret = ToString();
  150. string annotations = AidlAnnotatable::ToString();
  151. if (annotations != "") {
  152. ret = annotations + " " + ret;
  153. }
  154. return ret;
  155. }
  156. bool AidlTypeSpecifier::Resolve(android::aidl::AidlTypenames& typenames) {
  157. assert(!IsResolved());
  158. pair<string, bool> result = typenames.ResolveTypename(unresolved_name_);
  159. if (result.second) {
  160. fully_qualified_name_ = result.first;
  161. }
  162. return result.second;
  163. }
  164. bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
  165. if (IsGeneric()) {
  166. const string& type_name = GetName();
  167. const int num = GetTypeParameters().size();
  168. if (type_name == "List") {
  169. if (num > 1) {
  170. AIDL_ERROR(this) << " List cannot have type parameters more than one, but got "
  171. << "'" << ToString() << "'";
  172. return false;
  173. }
  174. } else if (type_name == "Map") {
  175. if (num != 0 && num != 2) {
  176. AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
  177. << "'" << ToString() << "'";
  178. return false;
  179. }
  180. }
  181. }
  182. if (GetName() == "void") {
  183. if (IsArray() || IsNullable() || IsUtf8InCpp()) {
  184. AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
  185. return false;
  186. }
  187. }
  188. if (IsArray()) {
  189. const auto definedType = typenames.TryGetDefinedType(GetName());
  190. if (definedType != nullptr && definedType->AsInterface() != nullptr) {
  191. AIDL_ERROR(this) << "Binder type cannot be an array";
  192. return false;
  193. }
  194. }
  195. if (IsNullable()) {
  196. if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
  197. AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/,
  204. const std::string& raw_value) {
  205. return raw_value;
  206. }
  207. AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
  208. AidlTypeSpecifier* type, const std::string& name)
  209. : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {}
  210. AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
  211. AidlTypeSpecifier* type, const std::string& name,
  212. AidlConstantValue* default_value)
  213. : AidlNode(location), type_(type), name_(name), default_value_(default_value) {}
  214. bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
  215. bool valid = true;
  216. valid &= type_->CheckValid(typenames);
  217. if (default_value_ == nullptr) return valid;
  218. valid &= default_value_->CheckValid();
  219. if (!valid) return false;
  220. return !ValueString(AidlConstantValueDecorator).empty();
  221. }
  222. string AidlVariableDeclaration::ToString() const {
  223. string ret = type_->Signature() + " " + name_;
  224. if (default_value_ != nullptr) {
  225. ret += " = " + ValueString(AidlConstantValueDecorator);
  226. }
  227. return ret;
  228. }
  229. string AidlVariableDeclaration::Signature() const {
  230. return type_->Signature() + " " + name_;
  231. }
  232. std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
  233. if (default_value_ != nullptr) {
  234. return GetDefaultValue()->As(GetType(), decorator);
  235. } else {
  236. return "";
  237. }
  238. }
  239. AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
  240. AidlTypeSpecifier* type, const std::string& name)
  241. : AidlVariableDeclaration(location, type, name),
  242. direction_(direction),
  243. direction_specified_(true) {}
  244. AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
  245. const std::string& name)
  246. : AidlVariableDeclaration(location, type, name),
  247. direction_(AidlArgument::IN_DIR),
  248. direction_specified_(false) {}
  249. string AidlArgument::GetDirectionSpecifier() const {
  250. string ret;
  251. if (direction_specified_) {
  252. switch(direction_) {
  253. case AidlArgument::IN_DIR:
  254. ret += "in ";
  255. break;
  256. case AidlArgument::OUT_DIR:
  257. ret += "out ";
  258. break;
  259. case AidlArgument::INOUT_DIR:
  260. ret += "inout ";
  261. break;
  262. }
  263. }
  264. return ret;
  265. }
  266. string AidlArgument::ToString() const {
  267. return GetDirectionSpecifier() + AidlVariableDeclaration::ToString();
  268. }
  269. std::string AidlArgument::Signature() const {
  270. class AidlInterface;
  271. class AidlInterface;
  272. class AidlParcelable;
  273. class AidlStructuredParcelable;
  274. class AidlParcelable;
  275. class AidlStructuredParcelable;
  276. return GetDirectionSpecifier() + AidlVariableDeclaration::Signature();
  277. }
  278. AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
  279. AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
  280. const std::string& checked_value)
  281. : AidlNode(location), type_(type), value_(checked_value) {
  282. CHECK(!value_.empty() || type_ == Type::ERROR);
  283. CHECK(type_ != Type::ARRAY);
  284. }
  285. AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
  286. std::vector<std::unique_ptr<AidlConstantValue>>* values)
  287. : AidlNode(location), type_(type), values_(std::move(*values)) {}
  288. static bool isValidLiteralChar(char c) {
  289. return !(c <= 0x1f || // control characters are < 0x20
  290. c >= 0x7f || // DEL is 0x7f
  291. c == '\\'); // Disallow backslashes for future proofing.
  292. }
  293. AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
  294. return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
  295. }
  296. AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
  297. if (!isValidLiteralChar(value)) {
  298. AIDL_ERROR(location) << "Invalid character literal " << value;
  299. return new AidlConstantValue(location, Type::ERROR, "");
  300. }
  301. return new AidlConstantValue(location, Type::CHARACTER, std::string("'") + value + "'");
  302. }
  303. AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
  304. const std::string& value) {
  305. return new AidlConstantValue(location, Type::FLOATING, value);
  306. }
  307. AidlConstantValue* AidlConstantValue::Hex(const AidlLocation& location, const std::string& value) {
  308. return new AidlConstantValue(location, Type::HEXIDECIMAL, value);
  309. }
  310. AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location,
  311. const std::string& value) {
  312. return new AidlConstantValue(location, Type::INTEGRAL, value);
  313. }
  314. AidlConstantValue* AidlConstantValue::Array(
  315. const AidlLocation& location, std::vector<std::unique_ptr<AidlConstantValue>>* values) {
  316. return new AidlConstantValue(location, Type::ARRAY, values);
  317. }
  318. AidlConstantValue* AidlConstantValue::String(const AidlLocation& location,
  319. const std::string& value) {
  320. for (size_t i = 0; i < value.length(); ++i) {
  321. if (!isValidLiteralChar(value[i])) {
  322. AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
  323. << value << "'";
  324. return new AidlConstantValue(location, Type::ERROR, "");
  325. }
  326. }
  327. return new AidlConstantValue(location, Type::STRING, value);
  328. }
  329. bool AidlConstantValue::CheckValid() const {
  330. // error always logged during creation
  331. return type_ != AidlConstantValue::Type::ERROR;
  332. }
  333. static string TrimIfSuffix(const string& str, const string& suffix) {
  334. if (str.size() > suffix.size() &&
  335. 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix)) {
  336. return str.substr(0, str.size() - suffix.size());
  337. }
  338. return str;
  339. }
  340. string AidlConstantValue::As(const AidlTypeSpecifier& type,
  341. const ConstantValueDecorator& decorator) const {
  342. if (type.IsGeneric()) {
  343. AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
  344. return "";
  345. }
  346. const std::string& type_string = type.GetName();
  347. if ((type_ == Type::ARRAY) != type.IsArray()) {
  348. goto mismatch_error;
  349. }
  350. switch (type_) {
  351. case AidlConstantValue::Type::ARRAY: {
  352. vector<string> raw_values;
  353. raw_values.reserve(values_.size());
  354. bool success = true;
  355. for (const auto& value : values_) {
  356. const AidlTypeSpecifier& array_base = type.ArrayBase();
  357. const std::string raw_value = value->As(array_base, decorator);
  358. success &= !raw_value.empty();
  359. raw_values.push_back(decorator(array_base, raw_value));
  360. }
  361. if (!success) {
  362. AIDL_ERROR(this) << "Default value must be a literal array of " << type_string << ".";
  363. return "";
  364. }
  365. return decorator(type, "{" + Join(raw_values, ", ") + "}");
  366. }
  367. case AidlConstantValue::Type::BOOLEAN:
  368. if (type_string == "boolean") return decorator(type, value_);
  369. goto mismatch_error;
  370. case AidlConstantValue::Type::CHARACTER:
  371. if (type_string == "char") return decorator(type, value_);
  372. goto mismatch_error;
  373. case AidlConstantValue::Type::FLOATING: {
  374. bool is_float_literal = value_.back() == 'f';
  375. const std::string raw_value = TrimIfSuffix(value_, "f");
  376. if (type_string == "double") {
  377. double parsed_value;
  378. if (!android::base::ParseDouble(raw_value, &parsed_value)) goto parse_error;
  379. return decorator(type, std::to_string(parsed_value));
  380. }
  381. if (is_float_literal && type_string == "float") {
  382. float parsed_value;
  383. if (!android::base::ParseFloat(raw_value, &parsed_value)) goto parse_error;
  384. return decorator(type, std::to_string(parsed_value) + "f");
  385. }
  386. goto mismatch_error;
  387. }
  388. case AidlConstantValue::Type::HEXIDECIMAL:
  389. // For historical reasons, a hexidecimal int needs to have the specified bits interpreted
  390. // as the signed type, so the other types are made consistent with it.
  391. if (type_string == "byte") {
  392. uint8_t unsigned_value;
  393. if (!android::base::ParseUint<uint8_t>(value_, &unsigned_value)) goto parse_error;
  394. return decorator(type, std::to_string((int8_t)unsigned_value));
  395. }
  396. if (type_string == "int") {
  397. uint32_t unsigned_value;
  398. if (!android::base::ParseUint<uint32_t>(value_, &unsigned_value)) goto parse_error;
  399. return decorator(type, std::to_string((int32_t)unsigned_value));
  400. }
  401. if (type_string == "long") {
  402. uint64_t unsigned_value;
  403. if (!android::base::ParseUint<uint64_t>(value_, &unsigned_value)) goto parse_error;
  404. return decorator(type, std::to_string((int64_t)unsigned_value));
  405. }
  406. goto mismatch_error;
  407. case AidlConstantValue::Type::INTEGRAL:
  408. if (type_string == "byte") {
  409. if (!android::base::ParseInt<int8_t>(value_, nullptr)) goto parse_error;
  410. return decorator(type, value_);
  411. }
  412. if (type_string == "int") {
  413. if (!android::base::ParseInt<int32_t>(value_, nullptr)) goto parse_error;
  414. return decorator(type, value_);
  415. }
  416. if (type_string == "long") {
  417. if (!android::base::ParseInt<int64_t>(value_, nullptr)) goto parse_error;
  418. return decorator(type, value_);
  419. }
  420. goto mismatch_error;
  421. case AidlConstantValue::Type::STRING:
  422. if (type_string == "String") return decorator(type, value_);
  423. goto mismatch_error;
  424. default:
  425. AIDL_FATAL(this) << "Unrecognized constant value type";
  426. }
  427. mismatch_error:
  428. AIDL_ERROR(this) << "Expecting type " << type_string << " but constant is " << ToString(type_);
  429. return "";
  430. parse_error:
  431. AIDL_ERROR(this) << "Could not parse " << value_ << " as " << type_string;
  432. return "";
  433. }
  434. string AidlConstantValue::ToString(Type type) {
  435. switch (type) {
  436. case Type::ARRAY:
  437. return "a literal array";
  438. case Type::BOOLEAN:
  439. return "a literal boolean";
  440. case Type::CHARACTER:
  441. return "a literal char";
  442. case Type::FLOATING:
  443. return "a floating-point literal";
  444. case Type::HEXIDECIMAL:
  445. return "a hexidecimal literal";
  446. case Type::INTEGRAL:
  447. return "an integral literal";
  448. case Type::STRING:
  449. return "a literal string";
  450. case Type::ERROR:
  451. LOG(FATAL) << "aidl internal error: error type failed to halt program";
  452. return "";
  453. default:
  454. LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type);
  455. return ""; // not reached
  456. }
  457. }
  458. AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
  459. AidlTypeSpecifier* type, const std::string& name,
  460. AidlConstantValue* value)
  461. : AidlMember(location), type_(type), name_(name), value_(value) {}
  462. bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
  463. bool valid = true;
  464. valid &= type_->CheckValid(typenames);
  465. valid &= value_->CheckValid();
  466. if (!valid) return false;
  467. const static set<string> kSupportedConstTypes = {"String", "int"};
  468. if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) {
  469. AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported.";
  470. return false;
  471. }
  472. return !ValueString(AidlConstantValueDecorator).empty();
  473. }
  474. string AidlConstantDeclaration::ToString() const {
  475. return "const " + type_->ToString() + " " + name_ + " = " +
  476. ValueString(AidlConstantValueDecorator);
  477. }
  478. string AidlConstantDeclaration::Signature() const {
  479. return type_->Signature() + " " + name_;
  480. }
  481. AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
  482. const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
  483. const std::string& comments)
  484. : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
  485. has_id_ = false;
  486. }
  487. AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
  488. const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
  489. const std::string& comments, int id, bool is_user_defined)
  490. : AidlMember(location),
  491. oneway_(oneway),
  492. comments_(comments),
  493. type_(type),
  494. name_(name),
  495. arguments_(std::move(*args)),
  496. id_(id),
  497. is_user_defined_(is_user_defined) {
  498. has_id_ = true;
  499. delete args;
  500. for (const unique_ptr<AidlArgument>& a : arguments_) {
  501. if (a->IsIn()) { in_arguments_.push_back(a.get()); }
  502. if (a->IsOut()) { out_arguments_.push_back(a.get()); }
  503. }
  504. }
  505. string AidlMethod::Signature() const {
  506. vector<string> arg_signatures;
  507. for (const auto& arg : GetArguments()) {
  508. arg_signatures.emplace_back(arg->GetType().ToString());
  509. }
  510. return GetName() + "(" + Join(arg_signatures, ", ") + ")";
  511. }
  512. string AidlMethod::ToString() const {
  513. vector<string> arg_strings;
  514. for (const auto& arg : GetArguments()) {
  515. arg_strings.emplace_back(arg->Signature());
  516. }
  517. string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" +
  518. Join(arg_strings, ", ") + ")";
  519. if (HasId()) {
  520. ret += " = " + std::to_string(GetId());
  521. }
  522. return ret;
  523. }
  524. AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
  525. const std::string& comments,
  526. const std::vector<std::string>& package)
  527. : AidlAnnotatable(location), name_(name), comments_(comments), package_(package) {}
  528. std::string AidlDefinedType::GetPackage() const {
  529. return Join(package_, '.');
  530. }
  531. std::string AidlDefinedType::GetCanonicalName() const {
  532. if (package_.empty()) {
  533. return GetName();
  534. }
  535. return GetPackage() + "." + GetName();
  536. }
  537. AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
  538. const std::vector<std::string>& package, const std::string& comments,
  539. const std::string& cpp_header)
  540. : AidlDefinedType(location, name->GetDotName(), comments, package),
  541. name_(name),
  542. cpp_header_(cpp_header) {
  543. // Strip off quotation marks if we actually have a cpp header.
  544. if (cpp_header_.length() >= 2) {
  545. cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
  546. }
  547. }
  548. bool AidlParcelable::CheckValid(const AidlTypenames&) const {
  549. static const std::set<string> allowed{kStableParcelable};
  550. for (const auto& v : GetAnnotations()) {
  551. if (allowed.find(v.GetName()) == allowed.end()) {
  552. std::ostringstream stream;
  553. stream << "Unstructured parcelable can contain only";
  554. for (const string& kv : allowed) {
  555. stream << " " << kv;
  556. }
  557. stream << ".";
  558. AIDL_ERROR(this) << stream.str();
  559. return false;
  560. }
  561. }
  562. return true;
  563. }
  564. void AidlParcelable::Write(CodeWriter* writer) const {
  565. writer->Write("parcelable %s ;\n", GetName().c_str());
  566. }
  567. AidlStructuredParcelable::AidlStructuredParcelable(
  568. const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package,
  569. const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
  570. : AidlParcelable(location, name, package, comments, "" /*cpp_header*/),
  571. variables_(std::move(*variables)) {}
  572. void AidlStructuredParcelable::Write(CodeWriter* writer) const {
  573. writer->Write("parcelable %s {\n", GetName().c_str());
  574. writer->Indent();
  575. for (const auto& field : GetFields()) {
  576. writer->Write("%s;\n", field->ToString().c_str());
  577. }
  578. writer->Dedent();
  579. writer->Write("}\n");
  580. }
  581. bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
  582. for (const auto& v : GetFields()) {
  583. if (!(v->CheckValid(typenames))) {
  584. return false;
  585. }
  586. }
  587. return true;
  588. }
  589. AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
  590. const std::string& comments, bool oneway,
  591. std::vector<std::unique_ptr<AidlMember>>* members,
  592. const std::vector<std::string>& package)
  593. : AidlDefinedType(location, name, comments, package) {
  594. for (auto& member : *members) {
  595. AidlMember* local = member.release();
  596. AidlMethod* method = local->AsMethod();
  597. AidlConstantDeclaration* constant = local->AsConstantDeclaration();
  598. CHECK(method == nullptr || constant == nullptr);
  599. if (method) {
  600. method->ApplyInterfaceOneway(oneway);
  601. methods_.emplace_back(method);
  602. } else if (constant) {
  603. constants_.emplace_back(constant);
  604. } else {
  605. AIDL_FATAL(this) << "Member is neither method nor constant!";
  606. }
  607. }
  608. delete members;
  609. }
  610. void AidlInterface::Write(CodeWriter* writer) const {
  611. writer->Write("interface %s {\n", GetName().c_str());
  612. writer->Indent();
  613. for (const auto& method : GetMethods()) {
  614. writer->Write("%s;\n", method->ToString().c_str());
  615. }
  616. for (const auto& constdecl : GetConstantDeclarations()) {
  617. writer->Write("%s;\n", constdecl->ToString().c_str());
  618. }
  619. writer->Dedent();
  620. writer->Write("}\n");
  621. }
  622. bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
  623. // Has to be a pointer due to deleting copy constructor. No idea why.
  624. map<string, const AidlMethod*> method_names;
  625. for (const auto& m : GetMethods()) {
  626. if (!m->GetType().CheckValid(typenames)) {
  627. return false;
  628. }
  629. if (m->IsOneway() && m->GetType().GetName() != "void") {
  630. AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
  631. return false;
  632. }
  633. set<string> argument_names;
  634. for (const auto& arg : m->GetArguments()) {
  635. auto it = argument_names.find(arg->GetName());
  636. if (it != argument_names.end()) {
  637. AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
  638. << arg->GetName() << "'";
  639. return false;
  640. }
  641. argument_names.insert(arg->GetName());
  642. if (!arg->GetType().CheckValid(typenames)) {
  643. return false;
  644. }
  645. if (m->IsOneway() && arg->IsOut()) {
  646. AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
  647. return false;
  648. }
  649. }
  650. auto it = method_names.find(m->GetName());
  651. // prevent duplicate methods
  652. if (it == method_names.end()) {
  653. method_names[m->GetName()] = m.get();
  654. } else {
  655. AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
  656. AIDL_ERROR(it->second) << "previously defined here.";
  657. return false;
  658. }
  659. static set<string> reserved_methods{"asBinder()", "getInterfaceVersion()",
  660. "getTransactionName(int)"};
  661. if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
  662. AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use." << endl;
  663. return false;
  664. }
  665. }
  666. bool success = true;
  667. set<string> constant_names;
  668. for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) {
  669. if (constant_names.count(constant->GetName()) > 0) {
  670. LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'";
  671. success = false;
  672. }
  673. constant_names.insert(constant->GetName());
  674. success = success && constant->CheckValid(typenames);
  675. }
  676. return success;
  677. }
  678. AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term,
  679. const std::string& comments)
  680. : AidlNode(location), terms_({term}), comments_(comments) {
  681. if (term.find('.') != string::npos) {
  682. terms_ = Split(term, ".");
  683. for (const auto& subterm : terms_) {
  684. if (subterm.empty()) {
  685. AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'";
  686. }
  687. }
  688. }
  689. }
  690. void AidlQualifiedName::AddTerm(const std::string& term) {
  691. terms_.push_back(term);
  692. }
  693. AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
  694. : AidlNode(location), needed_class_(needed_class) {}
  695. std::unique_ptr<Parser> Parser::Parse(const std::string& filename,
  696. const android::aidl::IoDelegate& io_delegate,
  697. AidlTypenames& typenames) {
  698. // Make sure we can read the file first, before trashing previous state.
  699. unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename);
  700. if (raw_buffer == nullptr) {
  701. AIDL_ERROR(filename) << "Error while opening file for parsing";
  702. return nullptr;
  703. }
  704. // We're going to scan this buffer in place, and yacc demands we put two
  705. // nulls at the end.
  706. raw_buffer->append(2u, '\0');
  707. std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames));
  708. if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) return nullptr;
  709. return parser;
  710. }
  711. std::vector<std::string> Parser::Package() const {
  712. if (!package_) {
  713. return {};
  714. }
  715. return package_->GetTerms();
  716. }
  717. void Parser::AddImport(AidlImport* import) {
  718. imports_.emplace_back(import);
  719. }
  720. bool Parser::Resolve() {
  721. bool success = true;
  722. for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
  723. if (!typespec->Resolve(typenames_)) {
  724. AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'";
  725. success = false;
  726. // don't stop to show more errors if any
  727. }
  728. }
  729. return success;
  730. }
  731. Parser::Parser(const std::string& filename, std::string& raw_buffer,
  732. android::aidl::AidlTypenames& typenames)
  733. : filename_(filename), typenames_(typenames) {
  734. yylex_init(&scanner_);
  735. buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
  736. }
  737. Parser::~Parser() {
  738. yy_delete_buffer(buffer_, scanner_);
  739. yylex_destroy(scanner_);
  740. }