Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "aidl_language.h" |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 2 | #include "aidl_typenames.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 3 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 5 | #include <stdlib.h> |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 6 | #include <string.h> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 7 | #include <cassert> |
| 8 | #include <iostream> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 9 | #include <string> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 10 | #include <utility> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 11 | |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame] | 12 | #include <android-base/parseint.h> |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 13 | #include <android-base/strings.h> |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 14 | |
Ying Wang | 3000e75 | 2016-01-11 18:05:59 -0800 | [diff] [blame] | 15 | #include "aidl_language_y.h" |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 17 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 18 | #ifdef _WIN32 |
| 19 | int isatty(int fd) |
| 20 | { |
| 21 | return (fd == 0); |
| 22 | } |
| 23 | #endif |
| 24 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 25 | using android::aidl::IoDelegate; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 26 | using android::base::Join; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 27 | using android::base::Split; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 28 | using std::cerr; |
| 29 | using std::endl; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 30 | using std::pair; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 31 | using std::string; |
| 32 | using std::unique_ptr; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 33 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 34 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 35 | void yylex_init(void **); |
| 36 | void yylex_destroy(void *); |
| 37 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 38 | int yyparse(Parser*); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 39 | YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 40 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 41 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 42 | AidlToken::AidlToken(const std::string& text, const std::string& comments) |
| 43 | : text_(text), |
| 44 | comments_(comments) {} |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 45 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 46 | AidlTypeSpecifier::AidlTypeSpecifier(const string& unresolved_name, bool is_array, |
| 47 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, |
| 48 | unsigned line, const string& comments) |
| 49 | : unresolved_name_(unresolved_name), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 50 | is_array_(is_array), |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 51 | type_params_(type_params), |
| 52 | line_(line), |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 53 | comments_(comments) {} |
| 54 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 55 | string AidlTypeSpecifier::ToString() const { |
| 56 | string ret = GetName(); |
| 57 | if (IsGeneric()) { |
| 58 | vector<string> arg_names; |
| 59 | for (const auto& ta : GetTypeParameters()) { |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 60 | arg_names.emplace_back(ta->ToString()); |
| 61 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 62 | ret += "<" + Join(arg_names, ",") + ">"; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 63 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 64 | if (IsArray()) { |
| 65 | ret += "[]"; |
| 66 | } |
| 67 | return ret; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 70 | bool AidlTypeSpecifier::Resolve(android::aidl::AidlTypenames& typenames) { |
| 71 | assert(!IsResolved()); |
| 72 | pair<string, bool> result = typenames.ResolveTypename(unresolved_name_); |
| 73 | if (result.second) { |
| 74 | fully_qualified_name_ = result.first; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 75 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 76 | return result.second; |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 79 | AidlVariableDeclaration::AidlVariableDeclaration(AidlTypeSpecifier* type, std::string name, |
| 80 | unsigned line) |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 81 | : type_(type), name_(name), line_(line) {} |
| 82 | |
| 83 | string AidlVariableDeclaration::ToString() const { |
| 84 | return type_->ToString() + " " + name_; |
| 85 | } |
| 86 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 87 | AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlTypeSpecifier* type, |
| 88 | std::string name, unsigned line) |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 89 | : AidlVariableDeclaration(type, name, line), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 90 | direction_(direction), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 91 | direction_specified_(true) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 92 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 93 | AidlArgument::AidlArgument(AidlTypeSpecifier* type, std::string name, unsigned line) |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 94 | : AidlVariableDeclaration(type, name, line), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 95 | direction_(AidlArgument::IN_DIR), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 96 | direction_specified_(false) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 97 | |
| 98 | string AidlArgument::ToString() const { |
| 99 | string ret; |
| 100 | |
| 101 | if (direction_specified_) { |
| 102 | switch(direction_) { |
| 103 | case AidlArgument::IN_DIR: |
| 104 | ret += "in "; |
| 105 | break; |
| 106 | case AidlArgument::OUT_DIR: |
| 107 | ret += "out "; |
| 108 | break; |
| 109 | case AidlArgument::INOUT_DIR: |
| 110 | ret += "inout "; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 115 | ret += AidlVariableDeclaration::ToString(); |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 119 | |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 120 | AidlIntConstant::AidlIntConstant(std::string name, int32_t value) |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 121 | : name_(name), |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 122 | value_(value), |
| 123 | is_valid_(true) {} |
| 124 | |
| 125 | AidlIntConstant::AidlIntConstant(std::string name, |
| 126 | std::string value, |
| 127 | unsigned line_number) |
| 128 | : name_(name) { |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame] | 129 | uint32_t unsigned_val; |
| 130 | if (!android::base::ParseUint(value.c_str(), &unsigned_val)) { |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 131 | is_valid_ = false; |
| 132 | LOG(ERROR) << "Found invalid int value '" << value |
| 133 | << "' on line " << line_number; |
| 134 | } else { |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame] | 135 | // Converting from unsigned to signed integer. |
| 136 | value_ = unsigned_val; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 137 | is_valid_ = true; |
| 138 | } |
| 139 | } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 140 | |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 141 | AidlStringConstant::AidlStringConstant(std::string name, |
| 142 | std::string value, |
| 143 | unsigned line_number) |
| 144 | : name_(name), |
| 145 | value_(value) { |
| 146 | is_valid_ = true; |
| 147 | for (size_t i = 0; i < value_.length(); ++i) { |
| 148 | const char& c = value_[i]; |
| 149 | if (c <= 0x1f || // control characters are < 0x20 |
| 150 | c >= 0x7f || // DEL is 0x7f |
| 151 | c == '\\') { // Disallow backslashes for future proofing. |
| 152 | LOG(ERROR) << "Found invalid character at index " << i |
| 153 | << " in string constant '" << value_ |
| 154 | << "' beginning on line " << line_number; |
| 155 | is_valid_ = false; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 161 | AidlMethod::AidlMethod(bool oneway, AidlTypeSpecifier* type, std::string name, |
| 162 | std::vector<std::unique_ptr<AidlArgument>>* args, unsigned line, |
| 163 | const std::string& comments, int id) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 164 | : oneway_(oneway), |
| 165 | comments_(comments), |
| 166 | type_(type), |
| 167 | name_(name), |
| 168 | line_(line), |
| 169 | arguments_(std::move(*args)), |
| 170 | id_(id) { |
| 171 | has_id_ = true; |
| 172 | delete args; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 173 | for (const unique_ptr<AidlArgument>& a : arguments_) { |
| 174 | if (a->IsIn()) { in_arguments_.push_back(a.get()); } |
| 175 | if (a->IsOut()) { out_arguments_.push_back(a.get()); } |
| 176 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 179 | AidlMethod::AidlMethod(bool oneway, AidlTypeSpecifier* type, std::string name, |
| 180 | std::vector<std::unique_ptr<AidlArgument>>* args, unsigned line, |
| 181 | const std::string& comments) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 182 | : AidlMethod(oneway, type, name, args, line, comments, 0) { |
| 183 | has_id_ = false; |
| 184 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 185 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 186 | Parser::Parser(const IoDelegate& io_delegate, android::aidl::AidlTypenames* typenames) |
| 187 | : io_delegate_(io_delegate), typenames_(typenames) { |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 188 | yylex_init(&scanner_); |
| 189 | } |
| 190 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 191 | AidlDefinedType::AidlDefinedType(std::string name, unsigned line, const std::string& comments, |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 192 | const std::vector<std::string>& package) |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 193 | : name_(name), line_(line), comments_(comments), package_(package) {} |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 194 | |
| 195 | std::string AidlDefinedType::GetPackage() const { |
| 196 | return Join(package_, '.'); |
| 197 | } |
| 198 | |
| 199 | std::string AidlDefinedType::GetCanonicalName() const { |
| 200 | if (package_.empty()) { |
| 201 | return GetName(); |
| 202 | } |
| 203 | return GetPackage() + "." + GetName(); |
| 204 | } |
| 205 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 206 | AidlParcelable::AidlParcelable(AidlQualifiedName* name, unsigned line, |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 207 | const std::vector<std::string>& package, |
| 208 | const std::string& cpp_header) |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 209 | : AidlDefinedType(name->GetDotName(), line, "" /*comments*/, package), |
| 210 | name_(name), |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 211 | cpp_header_(cpp_header) { |
| 212 | // Strip off quotation marks if we actually have a cpp header. |
| 213 | if (cpp_header_.length() >= 2) { |
| 214 | cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2); |
| 215 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 218 | AidlStructuredParcelable::AidlStructuredParcelable( |
| 219 | AidlQualifiedName* name, unsigned line, const std::vector<std::string>& package, |
| 220 | std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables) |
| 221 | : AidlParcelable(name, line, package, "" /*cpp_header*/), variables_(std::move(*variables)) {} |
| 222 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 223 | AidlInterface::AidlInterface(const std::string& name, unsigned line, |
| 224 | const std::string& comments, bool oneway, |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 225 | std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 226 | const std::vector<std::string>& package) |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 227 | : AidlDefinedType(name, line, comments, package), |
| 228 | oneway_(oneway) { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 229 | for (auto& member : *members) { |
| 230 | AidlMember* local = member.release(); |
| 231 | AidlMethod* method = local->AsMethod(); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 232 | AidlIntConstant* int_constant = local->AsIntConstant(); |
| 233 | AidlStringConstant* string_constant = local->AsStringConstant(); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 234 | |
| 235 | if (method) { |
| 236 | methods_.emplace_back(method); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 237 | } else if (int_constant) { |
| 238 | int_constants_.emplace_back(int_constant); |
| 239 | } else if (string_constant) { |
| 240 | string_constants_.emplace_back(string_constant); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 241 | } else { |
| 242 | LOG(FATAL) << "Member is neither method nor constant!"; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | delete members; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 249 | AidlDefinedType* AidlDocument::ReleaseDefinedType() { |
| 250 | if (defined_types_.size() == 0) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 251 | return nullptr; |
| 252 | } |
| 253 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 254 | if (defined_types_.size() > 1) { |
| 255 | LOG(ERROR) << "AIDL only supports compiling one defined type per file."; |
| 256 | return nullptr; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 259 | return defined_types_[0].release(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 262 | AidlQualifiedName::AidlQualifiedName(std::string term, |
| 263 | std::string comments) |
| 264 | : terms_({term}), |
| 265 | comments_(comments) { |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 266 | if (term.find('.') != string::npos) { |
| 267 | terms_ = Split(term, "."); |
| 268 | for (const auto& term: terms_) { |
| 269 | if (term.empty()) { |
| 270 | LOG(FATAL) << "Malformed qualified identifier: '" << term << "'"; |
| 271 | } |
| 272 | } |
| 273 | } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 276 | void AidlQualifiedName::AddTerm(const std::string& term) { |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 277 | terms_.push_back(term); |
| 278 | } |
| 279 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 280 | AidlImport::AidlImport(const std::string& from, |
| 281 | const std::string& needed_class, unsigned line) |
| 282 | : from_(from), |
| 283 | needed_class_(needed_class), |
| 284 | line_(line) {} |
| 285 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 286 | Parser::~Parser() { |
| 287 | if (raw_buffer_) { |
| 288 | yy_delete_buffer(buffer_, scanner_); |
| 289 | raw_buffer_.reset(); |
| 290 | } |
| 291 | yylex_destroy(scanner_); |
| 292 | } |
| 293 | |
| 294 | bool Parser::ParseFile(const string& filename) { |
| 295 | // Make sure we can read the file first, before trashing previous state. |
| 296 | unique_ptr<string> new_buffer = io_delegate_.GetFileContents(filename); |
| 297 | if (!new_buffer) { |
| 298 | LOG(ERROR) << "Error while opening file for parsing: '" << filename << "'"; |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // Throw away old parsing state if we have any. |
| 303 | if (raw_buffer_) { |
| 304 | yy_delete_buffer(buffer_, scanner_); |
| 305 | raw_buffer_.reset(); |
| 306 | } |
| 307 | |
| 308 | raw_buffer_ = std::move(new_buffer); |
| 309 | // We're going to scan this buffer in place, and yacc demands we put two |
| 310 | // nulls at the end. |
| 311 | raw_buffer_->append(2u, '\0'); |
| 312 | filename_ = filename; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 313 | package_.reset(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 314 | error_ = 0; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 315 | document_.reset(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 316 | |
| 317 | buffer_ = yy_scan_buffer(&(*raw_buffer_)[0], raw_buffer_->length(), scanner_); |
| 318 | |
Steven Moreland | 2ca4fcb | 2018-06-27 16:01:01 -0700 | [diff] [blame] | 319 | if (yy::parser(this).parse() != 0 || error_ != 0) |
| 320 | return false; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 321 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 322 | if (document_.get() == nullptr) { |
| 323 | LOG(ERROR) << "Parser succeeded but yielded no document!"; |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 329 | std::vector<std::string> Parser::Package() const { |
| 330 | if (!package_) { |
| 331 | return {}; |
| 332 | } |
| 333 | return package_->GetTerms(); |
| 334 | } |
| 335 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 336 | void Parser::AddImport(AidlQualifiedName* name, unsigned line) { |
| 337 | imports_.emplace_back(new AidlImport(this->FileName(), |
| 338 | name->GetDotName(), line)); |
| 339 | delete name; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 340 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame^] | 341 | |
| 342 | bool Parser::Resolve() { |
| 343 | bool success = true; |
| 344 | for (AidlTypeSpecifier* typespec : unresolved_typespecs_) { |
| 345 | if (!typespec->Resolve(*typenames_)) { |
| 346 | LOG(ERROR) << "Failed to resolve '" << typespec->GetUnresolvedName() << "' at " |
| 347 | << this->FileName() << ":" << typespec->GetLine(); |
| 348 | success = false; |
| 349 | // don't stop to show more errors if any |
| 350 | } |
| 351 | } |
| 352 | return success; |
| 353 | } |