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