blob: 214c759e6e2a972cdc8e3a19e622b048f18ab070 [file] [log] [blame]
Will McVickerefd970d2019-09-25 15:28:30 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Will McVickerefd970d2019-09-25 15:28:30 -070017#include "aidl_language.h"
Steven Moreland4bcb05c2019-11-27 18:57:47 -080018#include "aidl_typenames.h"
Will McVickerefd970d2019-09-25 15:28:30 -070019#include "logging.h"
20
21#include <stdlib.h>
22#include <algorithm>
23#include <iostream>
24#include <memory>
25
26#include <android-base/parsedouble.h>
27#include <android-base/parseint.h>
28#include <android-base/strings.h>
29
Will McVickerd7d18df2019-09-12 13:40:50 -070030using android::base::ConsumeSuffix;
Steven Morelandcef22662020-07-08 20:54:28 +000031using android::base::EndsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070032using android::base::Join;
Steven Morelandcef22662020-07-08 20:54:28 +000033using android::base::StartsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070034using std::string;
35using std::unique_ptr;
36using std::vector;
37
Will McVickerd7d18df2019-09-12 13:40:50 -070038#define SHOULD_NOT_REACH() CHECK(false) << LOG(FATAL) << ": should not reach here: "
39#define OPEQ(__y__) (string(op_) == string(__y__))
Steven Morelande1ff67e2020-07-16 23:22:36 +000040#define COMPUTE_UNARY(__op__) \
41 if (op == string(#__op__)) { \
42 *out = __op__ val; \
43 return true; \
44 }
Will McVickerd7d18df2019-09-12 13:40:50 -070045#define COMPUTE_BINARY(__op__) \
Steven Morelande1ff67e2020-07-16 23:22:36 +000046 if (op == string(#__op__)) { \
47 *out = lval __op__ rval; \
48 return true; \
49 }
Will McVickerd7d18df2019-09-12 13:40:50 -070050#define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%"))
51#define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&"))
52#define OP_IS_BIN_COMP \
53 (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!="))
54#define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<"))
55#define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&"))
56
57// NOLINT to suppress missing parentheses warnings about __def__.
58#define SWITCH_KIND(__cond__, __action__, __def__) \
59 switch (__cond__) { \
60 case Type::BOOLEAN: \
61 __action__(bool); \
62 case Type::INT8: \
63 __action__(int8_t); \
64 case Type::INT32: \
65 __action__(int32_t); \
66 case Type::INT64: \
67 __action__(int64_t); \
68 default: \
69 __def__; /* NOLINT */ \
70 }
71
72template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +000073bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -070074 COMPUTE_UNARY(+)
75 COMPUTE_UNARY(-)
76 COMPUTE_UNARY(!)
77 COMPUTE_UNARY(~)
Steven Moreland720a3cc2020-07-16 23:44:59 +000078 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
79 return false;
80}
81template <>
82bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) {
83 COMPUTE_UNARY(+)
84 COMPUTE_UNARY(-)
85 COMPUTE_UNARY(!)
Yifan Hongf17e3a72020-02-20 17:34:58 -080086
Steven Moreland720a3cc2020-07-16 23:44:59 +000087 if (op == "~") {
88 AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true.";
89 return false;
90 }
Steven Morelande1ff67e2020-07-16 23:22:36 +000091 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
92 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -070093}
94
95template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +000096bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval,
97 int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -070098 COMPUTE_BINARY(+)
99 COMPUTE_BINARY(-)
100 COMPUTE_BINARY(*)
101 COMPUTE_BINARY(/)
102 COMPUTE_BINARY(%)
103 COMPUTE_BINARY(|)
104 COMPUTE_BINARY(^)
105 COMPUTE_BINARY(&)
106 // comparison operators: return 0 or 1 by nature.
107 COMPUTE_BINARY(==)
108 COMPUTE_BINARY(!=)
109 COMPUTE_BINARY(<)
110 COMPUTE_BINARY(>)
111 COMPUTE_BINARY(<=)
112 COMPUTE_BINARY(>=)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000113
114 AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval;
115 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700116}
117
118template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000119bool handleShift(const AidlConstantValue& context, T lval, const string& op, int64_t rval,
120 int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700121 // just cast rval to int64_t and it should fit.
122 COMPUTE_BINARY(>>)
123 COMPUTE_BINARY(<<)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000124
125 AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval;
126 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700127}
128
Steven Morelande1ff67e2020-07-16 23:22:36 +0000129bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval,
130 int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700131 COMPUTE_BINARY(||);
132 COMPUTE_BINARY(&&);
Steven Morelande1ff67e2020-07-16 23:22:36 +0000133
134 AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval;
Will McVickerd7d18df2019-09-12 13:40:50 -0700135 return false;
136}
137
Will McVickerefd970d2019-09-25 15:28:30 -0700138static bool isValidLiteralChar(char c) {
139 return !(c <= 0x1f || // control characters are < 0x20
140 c >= 0x7f || // DEL is 0x7f
141 c == '\\'); // Disallow backslashes for future proofing.
142}
143
Will McVickerd7d18df2019-09-12 13:40:50 -0700144bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
145 // Verify the unary type here
146 switch (type) {
147 case Type::BOOLEAN: // fall-through
148 case Type::INT8: // fall-through
149 case Type::INT32: // fall-through
150 case Type::INT64:
151 return true;
152 case Type::FLOATING:
153 return (op == "+" || op == "-");
154 default:
155 return false;
156 }
157}
158
159bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
160 switch (t1) {
161 case Type::STRING:
162 if (t2 == Type::STRING) {
163 return true;
164 }
165 break;
166 case Type::BOOLEAN: // fall-through
167 case Type::INT8: // fall-through
168 case Type::INT32: // fall-through
169 case Type::INT64:
170 switch (t2) {
171 case Type::BOOLEAN: // fall-through
172 case Type::INT8: // fall-through
173 case Type::INT32: // fall-through
174 case Type::INT64:
175 return true;
176 break;
177 default:
178 break;
179 }
180 break;
181 default:
182 break;
183 }
184
185 return false;
186}
187
188// Returns the promoted kind for both operands
189AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
190 Type right) {
191 // These are handled as special cases
192 CHECK(left != Type::STRING && right != Type::STRING);
193 CHECK(left != Type::FLOATING && right != Type::FLOATING);
194
195 // Kinds in concern: bool, (u)int[8|32|64]
196 if (left == right) return left; // easy case
197 if (left == Type::BOOLEAN) return right;
198 if (right == Type::BOOLEAN) return left;
199
200 return left < right ? right : left;
201}
202
203// Returns the promoted integral type where INT32 is the smallest type
204AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
205 return (Type::INT32 < in) ? in : Type::INT32;
206}
207
208template <typename T>
209T AidlConstantValue::cast() const {
210 CHECK(is_evaluated_ == true);
211
212#define CASE_CAST_T(__type__) return static_cast<T>(static_cast<__type__>(final_value_));
213
214 SWITCH_KIND(final_type_, CASE_CAST_T, SHOULD_NOT_REACH(); return 0;);
215}
216
Steven Moreland541788d2020-05-21 22:05:52 +0000217AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
218 AidlLocation location = specifier.GetLocation();
219
220 // allocation of int[0] is a bit wasteful in Java
221 if (specifier.IsArray()) {
222 return nullptr;
223 }
224
225 const std::string name = specifier.GetName();
226 if (name == "boolean") {
227 return Boolean(location, false);
228 }
229 if (name == "byte" || name == "int" || name == "long") {
230 return Integral(location, "0");
231 }
232 if (name == "float") {
233 return Floating(location, "0.0f");
234 }
235 if (name == "double") {
236 return Floating(location, "0.0");
237 }
238 return nullptr;
239}
240
Will McVickerefd970d2019-09-25 15:28:30 -0700241AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
242 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
243}
244
245AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800246 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700247 if (!isValidLiteralChar(value)) {
248 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800249 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700250 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800251 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700252}
253
254AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
255 const std::string& value) {
256 return new AidlConstantValue(location, Type::FLOATING, value);
257}
258
Will McVickerd7d18df2019-09-12 13:40:50 -0700259bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000260 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700261}
262
Will McVickerd7d18df2019-09-12 13:40:50 -0700263bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
264 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700265 if (parsed_value == nullptr || parsed_type == nullptr) {
266 return false;
267 }
268
Steven Morelandcef22662020-07-08 20:54:28 +0000269 const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L');
270 const std::string value_substr = isLong ? value.substr(0, value.size() - 1) : value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700271
Steven Morelandcef22662020-07-08 20:54:28 +0000272 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700273 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
274 // handle that when computing constant expressions, then we need to
275 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
276 // so we parse as an unsigned int when possible and then cast to a signed
277 // int. One example of this is in ICameraService.aidl where a constant int
278 // is used for bit manipulations which ideally should be handled with an
279 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000280 //
281 // Note, for historical consistency, we need to consider small hex values
282 // as an integral type. Recognizing them as INT8 could break some files,
283 // even though it would simplify this code.
284 if (uint32_t rawValue32;
285 !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700286 *parsed_value = static_cast<int32_t>(rawValue32);
287 *parsed_type = Type::INT32;
Steven Morelandcef22662020-07-08 20:54:28 +0000288 } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) {
289 *parsed_value = static_cast<int64_t>(rawValue64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700290 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000291 } else {
292 *parsed_value = 0;
293 *parsed_type = Type::ERROR;
294 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700295 }
296 return true;
297 }
298
Steven Morelandcef22662020-07-08 20:54:28 +0000299 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
300 *parsed_value = 0;
Will McVickerd7d18df2019-09-12 13:40:50 -0700301 *parsed_type = Type::ERROR;
302 return false;
303 }
304
Steven Morelandcef22662020-07-08 20:54:28 +0000305 if (isLong) {
306 *parsed_type = Type::INT64;
307 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700308 // guess literal type.
309 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
310 *parsed_type = Type::INT8;
311 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
312 *parsed_type = Type::INT32;
313 } else {
314 *parsed_type = Type::INT64;
315 }
316 }
317 return true;
318}
319
320AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
321 CHECK(!value.empty());
322
323 Type parsed_type;
324 int64_t parsed_value = 0;
325 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
326 if (!success) {
327 return nullptr;
328 }
329
330 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700331}
332
333AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700334 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland541788d2020-05-21 22:05:52 +0000335 CHECK(values != nullptr) << location;
Will McVickerd7d18df2019-09-12 13:40:50 -0700336 return new AidlConstantValue(location, Type::ARRAY, std::move(values));
Will McVickerefd970d2019-09-25 15:28:30 -0700337}
338
Will McVickerd7d18df2019-09-12 13:40:50 -0700339AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700340 for (size_t i = 0; i < value.length(); ++i) {
341 if (!isValidLiteralChar(value[i])) {
342 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
343 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800344 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700345 }
346 }
347
348 return new AidlConstantValue(location, Type::STRING, value);
349}
350
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700351AidlConstantValue* AidlConstantValue::ShallowIntegralCopy(const AidlConstantValue& other) {
Daniel Norman3cce7cd2020-02-07 13:25:12 -0800352 // TODO(b/141313220) Perform a full copy instead of parsing+unparsing
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700353 AidlTypeSpecifier type = AidlTypeSpecifier(AIDL_LOCATION_HERE, "long", false, nullptr, "");
Steven Moreland65606612019-11-10 21:21:25 -0800354 // TODO(b/142722772) CheckValid() should be called before ValueString()
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800355 if (!other.CheckValid() || !other.evaluate(type)) {
Steven Moreland59e53e42019-11-26 20:38:08 -0800356 AIDL_ERROR(other) << "Failed to parse expression as integer: " << other.value_;
357 return nullptr;
358 }
359 const std::string& value = other.ValueString(type, AidlConstantValueDecorator);
360 if (value.empty()) {
361 return nullptr; // error already logged
Daniel Normanb28684e2019-10-17 15:31:39 -0700362 }
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700363
Steven Moreland59e53e42019-11-26 20:38:08 -0800364 AidlConstantValue* result = Integral(AIDL_LOCATION_HERE, value);
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700365 if (result == nullptr) {
366 AIDL_FATAL(other) << "Unable to perform ShallowIntegralCopy.";
367 }
368 return result;
Daniel Normanb28684e2019-10-17 15:31:39 -0700369}
370
Will McVickerd7d18df2019-09-12 13:40:50 -0700371string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
372 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700373 if (type.IsGeneric()) {
374 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
375 return "";
376 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700377 if (!is_evaluated_) {
378 // TODO(b/142722772) CheckValid() should be called before ValueString()
379 bool success = CheckValid();
380 success &= evaluate(type);
381 if (!success) {
382 // the detailed error message shall be printed in evaluate
383 return "";
384 }
Will McVickerefd970d2019-09-25 15:28:30 -0700385 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700386 if (!is_valid_) {
387 AIDL_ERROR(this) << "Invalid constant value: " + value_;
388 return "";
389 }
390 const string& type_string = type.GetName();
391 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700392
Will McVickerd7d18df2019-09-12 13:40:50 -0700393 switch (final_type_) {
394 case Type::CHARACTER:
395 if (type_string == "char") {
396 return decorator(type, final_string_value_);
397 }
398 err = -1;
399 break;
400 case Type::STRING:
401 if (type_string == "String") {
402 return decorator(type, final_string_value_);
403 }
404 err = -1;
405 break;
406 case Type::BOOLEAN: // fall-through
407 case Type::INT8: // fall-through
408 case Type::INT32: // fall-through
409 case Type::INT64:
410 if (type_string == "byte") {
411 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
412 err = -1;
413 break;
414 }
415 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
416 } else if (type_string == "int") {
417 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
418 err = -1;
419 break;
420 }
421 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
422 } else if (type_string == "long") {
423 return decorator(type, std::to_string(final_value_));
424 } else if (type_string == "boolean") {
425 return decorator(type, final_value_ ? "true" : "false");
426 }
427 err = -1;
428 break;
429 case Type::ARRAY: {
430 if (!type.IsArray()) {
431 err = -1;
432 break;
433 }
434 vector<string> value_strings;
435 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700436 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700437
Will McVickerefd970d2019-09-25 15:28:30 -0700438 for (const auto& value : values_) {
439 const AidlTypeSpecifier& array_base = type.ArrayBase();
Will McVickerd7d18df2019-09-12 13:40:50 -0700440 const string value_string = value->ValueString(array_base, decorator);
441 if (value_string.empty()) {
442 success = false;
443 break;
444 }
445 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700446 }
447 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700448 err = -1;
449 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700450 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700451
452 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700453 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 case Type::FLOATING: {
455 std::string_view raw_view(value_.c_str());
456 bool is_float_literal = ConsumeSuffix(&raw_view, "f");
457 std::string stripped_value = std::string(raw_view);
Will McVickerefd970d2019-09-25 15:28:30 -0700458
459 if (type_string == "double") {
460 double parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700461 if (!android::base::ParseDouble(stripped_value, &parsed_value)) {
462 AIDL_ERROR(this) << "Could not parse " << value_;
463 err = -1;
464 break;
465 }
Will McVickerefd970d2019-09-25 15:28:30 -0700466 return decorator(type, std::to_string(parsed_value));
467 }
468 if (is_float_literal && type_string == "float") {
469 float parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 if (!android::base::ParseFloat(stripped_value, &parsed_value)) {
471 AIDL_ERROR(this) << "Could not parse " << value_;
472 err = -1;
473 break;
474 }
Will McVickerefd970d2019-09-25 15:28:30 -0700475 return decorator(type, std::to_string(parsed_value) + "f");
476 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700477 err = -1;
478 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700479 }
Will McVickerefd970d2019-09-25 15:28:30 -0700480 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700481 err = -1;
482 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700483 }
484
Will McVickerd7d18df2019-09-12 13:40:50 -0700485 CHECK(err != 0);
486 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string;
Will McVickerefd970d2019-09-25 15:28:30 -0700487 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700488}
489
490bool AidlConstantValue::CheckValid() const {
491 // Nothing needs to be checked here. The constant value will be validated in
492 // the constructor or in the evaluate() function.
493 if (is_evaluated_) return is_valid_;
494
495 switch (type_) {
496 case Type::BOOLEAN: // fall-through
497 case Type::INT8: // fall-through
498 case Type::INT32: // fall-through
499 case Type::INT64: // fall-through
500 case Type::ARRAY: // fall-through
501 case Type::CHARACTER: // fall-through
502 case Type::STRING: // fall-through
503 case Type::FLOATING: // fall-through
504 case Type::UNARY: // fall-through
505 case Type::BINARY:
506 is_valid_ = true;
507 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800508 case Type::ERROR:
509 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700510 default:
511 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
512 return false;
513 }
514
515 return true;
516}
517
518bool AidlConstantValue::evaluate(const AidlTypeSpecifier& type) const {
519 if (is_evaluated_) {
520 return is_valid_;
521 }
522 int err = 0;
523 is_evaluated_ = true;
524
525 switch (type_) {
526 case Type::ARRAY: {
527 if (!type.IsArray()) {
528 AIDL_ERROR(this) << "Invalid constant array type: " << type.GetName();
529 err = -1;
530 break;
531 }
532 Type array_type = Type::ERROR;
533 bool success = true;
534 for (const auto& value : values_) {
535 success = value->CheckValid();
536 if (success) {
537 success = value->evaluate(type.ArrayBase());
538 if (!success) {
539 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
540 break;
541 }
542 if (array_type == Type::ERROR) {
543 array_type = value->final_type_;
544 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
545 value->final_type_)) {
546 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
547 << ". Expecting type compatible with " << ToString(array_type);
548 success = false;
549 break;
550 }
551 } else {
552 break;
553 }
554 }
555 if (!success) {
556 err = -1;
557 break;
558 }
559 final_type_ = type_;
560 break;
561 }
562 case Type::BOOLEAN:
563 if ((value_ != "true") && (value_ != "false")) {
564 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
565 err = -1;
566 break;
567 }
568 final_value_ = (value_ == "true") ? 1 : 0;
569 final_type_ = type_;
570 break;
571 case Type::INT8: // fall-through
572 case Type::INT32: // fall-through
573 case Type::INT64:
574 // Parsing happens in the constructor
575 final_type_ = type_;
576 break;
577 case Type::CHARACTER: // fall-through
578 case Type::STRING:
579 final_string_value_ = value_;
580 final_type_ = type_;
581 break;
582 case Type::FLOATING:
583 // Just parse on the fly in ValueString
584 final_type_ = type_;
585 break;
586 default:
587 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
588 err = -1;
589 }
590
591 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700592}
593
594string AidlConstantValue::ToString(Type type) {
595 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700596 case Type::BOOLEAN:
597 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700598 case Type::INT8:
599 return "an int8 literal";
600 case Type::INT32:
601 return "an int32 literal";
602 case Type::INT64:
603 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800604 case Type::ARRAY:
605 return "a literal array";
606 case Type::CHARACTER:
607 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700608 case Type::STRING:
609 return "a literal string";
Steven Morelanda923a722019-11-26 20:08:30 -0800610 case Type::FLOATING:
611 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700612 case Type::UNARY:
613 return "a unary expression";
614 case Type::BINARY:
615 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800616 case Type::ERROR:
617 LOG(FATAL) << "aidl internal error: error type failed to halt program";
618 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700619 default:
620 LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type);
621 return ""; // not reached
622 }
623}
624
Will McVickerd7d18df2019-09-12 13:40:50 -0700625bool AidlUnaryConstExpression::CheckValid() const {
626 if (is_evaluated_) return is_valid_;
627 CHECK(unary_ != nullptr);
628
629 is_valid_ = unary_->CheckValid();
630 if (!is_valid_) {
631 final_type_ = Type::ERROR;
632 return false;
633 }
634
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800635 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700636}
637
638bool AidlUnaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
639 if (is_evaluated_) {
640 return is_valid_;
641 }
642 is_evaluated_ = true;
643
644 // Recursively evaluate the expression tree
645 if (!unary_->is_evaluated_) {
646 // TODO(b/142722772) CheckValid() should be called before ValueString()
647 bool success = CheckValid();
648 success &= unary_->evaluate(type);
649 if (!success) {
650 is_valid_ = false;
651 return false;
652 }
653 }
Devin Moorec233fb82020-04-07 11:13:44 -0700654 if (!IsCompatibleType(unary_->final_type_, op_)) {
655 AIDL_ERROR(unary_) << "'" << op_ << "'"
656 << " is not compatible with " << ToString(unary_->final_type_)
657 << ": " + value_;
658 is_valid_ = false;
659 return false;
660 }
661 if (!unary_->is_valid_) {
662 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700663 is_valid_ = false;
664 return false;
665 }
666 final_type_ = unary_->final_type_;
667
668 if (final_type_ == Type::FLOATING) {
669 // don't do anything here. ValueString() will handle everything.
670 is_valid_ = true;
671 return true;
672 }
673
Steven Morelande1ff67e2020-07-16 23:22:36 +0000674#define CASE_UNARY(__type__) \
675 return handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700676
677 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
678 is_valid_ = false; return false;)
679}
680
Will McVickerd7d18df2019-09-12 13:40:50 -0700681bool AidlBinaryConstExpression::CheckValid() const {
682 bool success = false;
683 if (is_evaluated_) return is_valid_;
684 CHECK(left_val_ != nullptr);
685 CHECK(right_val_ != nullptr);
686
687 success = left_val_->CheckValid();
688 if (!success) {
689 final_type_ = Type::ERROR;
690 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
691 }
692
693 success = right_val_->CheckValid();
694 if (!success) {
695 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
696 final_type_ = Type::ERROR;
697 }
698
699 if (final_type_ == Type::ERROR) {
700 is_valid_ = false;
701 return false;
702 }
703
704 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800705 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700706}
707
708bool AidlBinaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
709 if (is_evaluated_) {
710 return is_valid_;
711 }
712 is_evaluated_ = true;
713 CHECK(left_val_ != nullptr);
714 CHECK(right_val_ != nullptr);
715
716 // Recursively evaluate the binary expression tree
717 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
718 // TODO(b/142722772) CheckValid() should be called before ValueString()
719 bool success = CheckValid();
720 success &= left_val_->evaluate(type);
721 success &= right_val_->evaluate(type);
722 if (!success) {
723 is_valid_ = false;
724 return false;
725 }
726 }
727 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
728 is_valid_ = false;
729 return false;
730 }
731 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
732 if (!is_valid_) {
733 return false;
734 }
735
736 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
737
738 // Handle String case first
739 if (left_val_->final_type_ == Type::STRING) {
740 if (!OPEQ("+")) {
741 // invalid operation on strings
742 final_type_ = Type::ERROR;
743 is_valid_ = false;
744 return false;
745 }
746
747 // Remove trailing " from lhs
748 const string& lhs = left_val_->final_string_value_;
749 if (lhs.back() != '"') {
750 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
751 final_type_ = Type::ERROR;
752 is_valid_ = false;
753 return false;
754 }
755 const string& rhs = right_val_->final_string_value_;
756 // Remove starting " from rhs
757 if (rhs.front() != '"') {
758 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
759 final_type_ = Type::ERROR;
760 is_valid_ = false;
761 return false;
762 }
763
764 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
765 final_type_ = Type::STRING;
766 return true;
767 }
768
Will McVickerd7d18df2019-09-12 13:40:50 -0700769 // CASE: + - * / % | ^ & < > <= >= == !=
770 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Steven Moreland485a51a2019-11-26 20:52:58 -0800771 if ((op_ == "/" || op_ == "%") && right_val_->final_value_ == 0) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700772 final_type_ = Type::ERROR;
773 is_valid_ = false;
Steven Moreland485a51a2019-11-26 20:52:58 -0800774 AIDL_ERROR(this) << "Cannot do division operation with zero for expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700775 return false;
776 }
777
778 // promoted kind for both operands.
779 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
780 IntegralPromotion(right_val_->final_type_));
781 // result kind.
782 final_type_ = isArithmeticOrBitflip
783 ? promoted // arithmetic or bitflip operators generates promoted type
784 : Type::BOOLEAN; // comparison operators generates bool
785
Steven Morelande1ff67e2020-07-16 23:22:36 +0000786#define CASE_BINARY_COMMON(__type__) \
787 return handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
788 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700789
790 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
791 is_valid_ = false; return false;)
792 }
793
794 // CASE: << >>
795 string newOp = op_;
796 if (OP_IS_BIN_SHIFT) {
797 final_type_ = IntegralPromotion(left_val_->final_type_);
798 // instead of promoting rval, simply casting it to int64 should also be good.
799 int64_t numBits = right_val_->cast<int64_t>();
800 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -0800801 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -0700802 // is defined as shifting into the other direction.
803 newOp = OPEQ("<<") ? ">>" : "<<";
804 numBits = -numBits;
805 }
806
Steven Morelande1ff67e2020-07-16 23:22:36 +0000807#define CASE_SHIFT(__type__) \
808 return handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, numBits, \
809 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700810
811 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
812 is_valid_ = false; return false;)
813 }
814
815 // CASE: && ||
816 if (OP_IS_BIN_LOGICAL) {
817 final_type_ = Type::BOOLEAN;
818 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +0000819 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
820 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700821 }
822
823 SHOULD_NOT_REACH();
824 is_valid_ = false;
825 return false;
826}
827
Will McVickerd7d18df2019-09-12 13:40:50 -0700828AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
829 int64_t parsed_value, const string& checked_value)
830 : AidlNode(location),
831 type_(parsed_type),
832 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -0700833 final_type_(parsed_type),
834 final_value_(parsed_value) {
Steven Moreland541788d2020-05-21 22:05:52 +0000835 CHECK(!value_.empty() || type_ == Type::ERROR) << location;
836 CHECK(type_ == Type::INT8 || type_ == Type::INT32 || type_ == Type::INT64) << location;
Will McVickerd7d18df2019-09-12 13:40:50 -0700837}
Will McVickerefd970d2019-09-25 15:28:30 -0700838
839AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700840 const string& checked_value)
841 : AidlNode(location),
842 type_(type),
843 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -0700844 final_type_(type) {
Steven Moreland541788d2020-05-21 22:05:52 +0000845 CHECK(!value_.empty() || type_ == Type::ERROR) << location;
Will McVickerd7d18df2019-09-12 13:40:50 -0700846 switch (type_) {
847 case Type::INT8:
848 case Type::INT32:
849 case Type::INT64:
850 case Type::ARRAY:
851 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
852 break;
853 default:
854 break;
855 }
856}
857
858AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
859 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values)
860 : AidlNode(location),
861 type_(type),
862 values_(std::move(*values)),
863 is_valid_(false),
864 is_evaluated_(false),
865 final_type_(type) {
866 CHECK(type_ == Type::ARRAY);
867}
868
869AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
870 std::unique_ptr<AidlConstantValue> rval)
871 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
872 unary_(std::move(rval)),
873 op_(op) {
874 final_type_ = Type::UNARY;
875}
876
877AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
878 std::unique_ptr<AidlConstantValue> lval,
879 const string& op,
880 std::unique_ptr<AidlConstantValue> rval)
881 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
882 left_val_(std::move(lval)),
883 right_val_(std::move(rval)),
884 op_(op) {
885 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -0700886}