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