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