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