blob: 2754150803910e4512adc9d5dffaed92ca1faca0 [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>
Steven Moreland0521bf32020-09-09 22:44:07 +000024#include <limits>
Will McVickerefd970d2019-09-25 15:28:30 -070025#include <memory>
26
27#include <android-base/parsedouble.h>
28#include <android-base/parseint.h>
29#include <android-base/strings.h>
30
Will McVickerd7d18df2019-09-12 13:40:50 -070031using android::base::ConsumeSuffix;
Steven Morelandcef22662020-07-08 20:54:28 +000032using android::base::EndsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070033using android::base::Join;
Steven Morelandcef22662020-07-08 20:54:28 +000034using android::base::StartsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070035using std::string;
36using std::unique_ptr;
37using std::vector;
38
Steven Moreland0521bf32020-09-09 22:44:07 +000039template <typename T>
Devin Moorecff93692020-09-24 10:39:57 -070040constexpr int CLZ(T x) {
Devin Mooree2de9e42020-10-02 08:55:08 -070041 // __builtin_clz(0) is undefined
42 if (x == 0) return sizeof(T) * 8;
Devin Moorecff93692020-09-24 10:39:57 -070043 return (sizeof(T) == sizeof(uint64_t)) ? __builtin_clzl(x) : __builtin_clz(x);
44}
45
46template <typename T>
Steven Moreland0521bf32020-09-09 22:44:07 +000047class OverflowGuard {
48 public:
49 OverflowGuard(T value) : mValue(value) {}
50 bool Overflowed() const { return mOverflowed; }
51
52 T operator+() { return +mValue; }
53 T operator-() {
54 if (isMin()) {
55 mOverflowed = true;
56 return 0;
57 }
58 return -mValue;
59 }
60 T operator!() { return !mValue; }
61 T operator~() { return ~mValue; }
62
63 T operator+(T o) {
64 T out;
65 mOverflowed = __builtin_add_overflow(mValue, o, &out);
66 return out;
67 }
68 T operator-(T o) {
69 T out;
70 mOverflowed = __builtin_sub_overflow(mValue, o, &out);
71 return out;
72 }
73 T operator*(T o) {
74 T out;
75#ifdef _WIN32
76 // ___mulodi4 not on windows https://bugs.llvm.org/show_bug.cgi?id=46669
77 // we should still get an error here from ubsan, but the nice error
78 // is needed on linux for aidl_parser_fuzzer, where we are more
79 // concerned about overflows elsewhere in the compiler in addition to
80 // those in interfaces.
81 out = mValue * o;
82#else
83 mOverflowed = __builtin_mul_overflow(mValue, o, &out);
84#endif
85 return out;
86 }
87 T operator/(T o) {
88 if (o == 0 || (isMin() && o == -1)) {
89 mOverflowed = true;
90 return 0;
91 }
92 return mValue / o;
93 }
94 T operator%(T o) {
95 if (o == 0 || (isMin() && o == -1)) {
96 mOverflowed = true;
97 return 0;
98 }
99 return mValue % o;
100 }
101 T operator|(T o) { return mValue | o; }
102 T operator^(T o) { return mValue ^ o; }
103 T operator&(T o) { return mValue & o; }
104 T operator<(T o) { return mValue < o; }
105 T operator>(T o) { return mValue > o; }
106 T operator<=(T o) { return mValue <= o; }
107 T operator>=(T o) { return mValue >= o; }
108 T operator==(T o) { return mValue == o; }
109 T operator!=(T o) { return mValue != o; }
110 T operator>>(T o) {
Devin Moorea9e64de2020-09-29 11:29:42 -0700111 if (o < 0 || o >= static_cast<T>(sizeof(T) * 8) || mValue < 0) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000112 mOverflowed = true;
113 return 0;
114 }
115 return mValue >> o;
116 }
117 T operator<<(T o) {
Devin Mooree2de9e42020-10-02 08:55:08 -0700118 if (o < 0 || mValue < 0 || o > CLZ(mValue) || o >= static_cast<T>(sizeof(T) * 8)) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000119 mOverflowed = true;
120 return 0;
121 }
122 return mValue << o;
123 }
124 T operator||(T o) { return mValue || o; }
125 T operator&&(T o) { return mValue && o; }
126
127 private:
128 bool isMin() { return mValue == std::numeric_limits<T>::min(); }
129
130 T mValue;
131 bool mOverflowed = false;
132};
133
134template <typename T>
135bool processGuard(const OverflowGuard<T>& guard, const AidlConstantValue& context) {
136 if (guard.Overflowed()) {
137 AIDL_ERROR(context) << "Constant expression computation overflows.";
138 return false;
139 }
140 return true;
141}
142
143// TODO: factor out all these macros
Steven Moreland21780812020-09-11 01:29:45 +0000144#define SHOULD_NOT_REACH() AIDL_FATAL(AIDL_LOCATION_HERE) << "Should not reach."
Will McVickerd7d18df2019-09-12 13:40:50 -0700145#define OPEQ(__y__) (string(op_) == string(__y__))
Steven Moreland0521bf32020-09-09 22:44:07 +0000146#define COMPUTE_UNARY(T, __op__) \
147 if (op == string(#__op__)) { \
148 OverflowGuard<T> guard(val); \
149 *out = __op__ guard; \
150 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000151 }
Steven Moreland0521bf32020-09-09 22:44:07 +0000152#define COMPUTE_BINARY(T, __op__) \
153 if (op == string(#__op__)) { \
154 OverflowGuard<T> guard(lval); \
155 *out = guard __op__ rval; \
156 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000157 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700158#define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%"))
159#define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&"))
160#define OP_IS_BIN_COMP \
161 (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!="))
162#define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<"))
163#define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&"))
164
165// NOLINT to suppress missing parentheses warnings about __def__.
166#define SWITCH_KIND(__cond__, __action__, __def__) \
167 switch (__cond__) { \
168 case Type::BOOLEAN: \
169 __action__(bool); \
170 case Type::INT8: \
171 __action__(int8_t); \
172 case Type::INT32: \
173 __action__(int32_t); \
174 case Type::INT64: \
175 __action__(int64_t); \
176 default: \
177 __def__; /* NOLINT */ \
178 }
179
180template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000181bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000182 COMPUTE_UNARY(T, +)
183 COMPUTE_UNARY(T, -)
184 COMPUTE_UNARY(T, !)
185 COMPUTE_UNARY(T, ~)
Steven Moreland720a3cc2020-07-16 23:44:59 +0000186 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
187 return false;
188}
189template <>
190bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000191 COMPUTE_UNARY(bool, +)
192 COMPUTE_UNARY(bool, -)
193 COMPUTE_UNARY(bool, !)
Yifan Hongf17e3a72020-02-20 17:34:58 -0800194
Steven Moreland720a3cc2020-07-16 23:44:59 +0000195 if (op == "~") {
196 AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true.";
197 return false;
198 }
Steven Morelande1ff67e2020-07-16 23:22:36 +0000199 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
200 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700201}
202
203template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000204bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval,
205 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000206 COMPUTE_BINARY(T, +)
207 COMPUTE_BINARY(T, -)
208 COMPUTE_BINARY(T, *)
209 COMPUTE_BINARY(T, /)
210 COMPUTE_BINARY(T, %)
211 COMPUTE_BINARY(T, |)
212 COMPUTE_BINARY(T, ^)
213 COMPUTE_BINARY(T, &)
Will McVickerd7d18df2019-09-12 13:40:50 -0700214 // comparison operators: return 0 or 1 by nature.
Steven Moreland0521bf32020-09-09 22:44:07 +0000215 COMPUTE_BINARY(T, ==)
216 COMPUTE_BINARY(T, !=)
217 COMPUTE_BINARY(T, <)
218 COMPUTE_BINARY(T, >)
219 COMPUTE_BINARY(T, <=)
220 COMPUTE_BINARY(T, >=)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000221
222 AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval;
223 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700224}
225
226template <class T>
Devin Moore04823022020-09-11 10:43:35 -0700227bool handleShift(const AidlConstantValue& context, T lval, const string& op, T rval, int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700228 // just cast rval to int64_t and it should fit.
Steven Moreland0521bf32020-09-09 22:44:07 +0000229 COMPUTE_BINARY(T, >>)
230 COMPUTE_BINARY(T, <<)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000231
232 AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval;
233 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700234}
235
Steven Morelande1ff67e2020-07-16 23:22:36 +0000236bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval,
237 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000238 COMPUTE_BINARY(bool, ||);
239 COMPUTE_BINARY(bool, &&);
Steven Morelande1ff67e2020-07-16 23:22:36 +0000240
241 AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval;
Will McVickerd7d18df2019-09-12 13:40:50 -0700242 return false;
243}
244
Will McVickerefd970d2019-09-25 15:28:30 -0700245static bool isValidLiteralChar(char c) {
246 return !(c <= 0x1f || // control characters are < 0x20
247 c >= 0x7f || // DEL is 0x7f
248 c == '\\'); // Disallow backslashes for future proofing.
249}
250
Jooyung Han535c5e82020-12-29 15:16:59 +0900251bool ParseFloating(std::string_view sv, double* parsed) {
252 // float literal should be parsed successfully.
Jooyung Han71a1b582020-12-25 23:58:41 +0900253 android::base::ConsumeSuffix(&sv, "f");
Jooyung Han535c5e82020-12-29 15:16:59 +0900254 return android::base::ParseDouble(std::string(sv).data(), parsed);
255}
256
257bool ParseFloating(std::string_view sv, float* parsed) {
258 // we only care about float literal (with suffix "f").
259 if (!android::base::ConsumeSuffix(&sv, "f")) {
260 return false;
Jooyung Han71a1b582020-12-25 23:58:41 +0900261 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900262 return android::base::ParseFloat(std::string(sv).data(), parsed);
Jooyung Han71a1b582020-12-25 23:58:41 +0900263}
264
Will McVickerd7d18df2019-09-12 13:40:50 -0700265bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
266 // Verify the unary type here
267 switch (type) {
268 case Type::BOOLEAN: // fall-through
269 case Type::INT8: // fall-through
270 case Type::INT32: // fall-through
271 case Type::INT64:
272 return true;
273 case Type::FLOATING:
274 return (op == "+" || op == "-");
275 default:
276 return false;
277 }
278}
279
280bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
281 switch (t1) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900282 case Type::ARRAY:
283 if (t2 == Type::ARRAY) {
284 return true;
285 }
286 break;
Will McVickerd7d18df2019-09-12 13:40:50 -0700287 case Type::STRING:
288 if (t2 == Type::STRING) {
289 return true;
290 }
291 break;
292 case Type::BOOLEAN: // fall-through
293 case Type::INT8: // fall-through
294 case Type::INT32: // fall-through
295 case Type::INT64:
296 switch (t2) {
297 case Type::BOOLEAN: // fall-through
298 case Type::INT8: // fall-through
299 case Type::INT32: // fall-through
300 case Type::INT64:
301 return true;
302 break;
303 default:
304 break;
305 }
306 break;
307 default:
308 break;
309 }
310
311 return false;
312}
313
314// Returns the promoted kind for both operands
315AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
316 Type right) {
317 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000318 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
319 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700320
321 // Kinds in concern: bool, (u)int[8|32|64]
322 if (left == right) return left; // easy case
323 if (left == Type::BOOLEAN) return right;
324 if (right == Type::BOOLEAN) return left;
325
326 return left < right ? right : left;
327}
328
329// Returns the promoted integral type where INT32 is the smallest type
330AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
331 return (Type::INT32 < in) ? in : Type::INT32;
332}
333
Steven Moreland541788d2020-05-21 22:05:52 +0000334AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
335 AidlLocation location = specifier.GetLocation();
336
337 // allocation of int[0] is a bit wasteful in Java
338 if (specifier.IsArray()) {
339 return nullptr;
340 }
341
342 const std::string name = specifier.GetName();
343 if (name == "boolean") {
344 return Boolean(location, false);
345 }
346 if (name == "byte" || name == "int" || name == "long") {
347 return Integral(location, "0");
348 }
349 if (name == "float") {
350 return Floating(location, "0.0f");
351 }
352 if (name == "double") {
353 return Floating(location, "0.0");
354 }
355 return nullptr;
356}
357
Will McVickerefd970d2019-09-25 15:28:30 -0700358AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
359 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
360}
361
362AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800363 const std::string explicit_value = string("'") + value + "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800364 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700365}
366
367AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
368 const std::string& value) {
369 return new AidlConstantValue(location, Type::FLOATING, value);
370}
371
Will McVickerd7d18df2019-09-12 13:40:50 -0700372bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000373 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700374}
375
Will McVickerd7d18df2019-09-12 13:40:50 -0700376bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
377 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700378 if (parsed_value == nullptr || parsed_type == nullptr) {
379 return false;
380 }
381
Steven Morelandb7d58652021-10-25 15:10:02 -0700382 std::string_view value_view = value;
383 const bool is_byte = ConsumeSuffix(&value_view, "u8");
384 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
385 const std::string value_substr = std::string(value_view);
386
387 *parsed_value = 0;
388 *parsed_type = Type::ERROR;
389
390 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700391
Steven Morelandcef22662020-07-08 20:54:28 +0000392 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700393 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
394 // handle that when computing constant expressions, then we need to
395 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
396 // so we parse as an unsigned int when possible and then cast to a signed
397 // int. One example of this is in ICameraService.aidl where a constant int
398 // is used for bit manipulations which ideally should be handled with an
399 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000400 //
401 // Note, for historical consistency, we need to consider small hex values
402 // as an integral type. Recognizing them as INT8 could break some files,
403 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700404 if (is_byte) {
405 uint8_t raw_value8;
406 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
407 return false;
408 }
409 *parsed_value = static_cast<int8_t>(raw_value8);
410 *parsed_type = Type::INT8;
411 } else if (uint32_t raw_value32;
412 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
413 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700414 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700415 } else if (uint64_t raw_value64;
416 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
417 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700418 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000419 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000420 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700421 }
422 return true;
423 }
424
Steven Morelandcef22662020-07-08 20:54:28 +0000425 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700426 return false;
427 }
428
Steven Morelandb7d58652021-10-25 15:10:02 -0700429 if (is_byte) {
430 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
431 return false;
432 }
433 *parsed_value = static_cast<int8_t>(*parsed_value);
434 *parsed_type = Type::INT8;
435 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000436 *parsed_type = Type::INT64;
437 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700438 // guess literal type.
439 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
440 *parsed_type = Type::INT8;
441 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
442 *parsed_type = Type::INT32;
443 } else {
444 *parsed_type = Type::INT64;
445 }
446 }
447 return true;
448}
449
450AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000451 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700452
453 Type parsed_type;
454 int64_t parsed_value = 0;
455 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
456 if (!success) {
457 return nullptr;
458 }
459
460 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700461}
462
463AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000465 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900466 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900467 std::vector<std::string> str_values;
468 for (const auto& v : *values) {
469 str_values.push_back(v->value_);
470 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900471 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
472 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700473}
474
Will McVickerd7d18df2019-09-12 13:40:50 -0700475AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700476 for (size_t i = 0; i < value.length(); ++i) {
477 if (!isValidLiteralChar(value[i])) {
478 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
479 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800480 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700481 }
482 }
483
484 return new AidlConstantValue(location, Type::STRING, value);
485}
486
Will McVickerd7d18df2019-09-12 13:40:50 -0700487string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
488 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700489 if (type.IsGeneric()) {
490 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
491 return "";
492 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 if (!is_evaluated_) {
494 // TODO(b/142722772) CheckValid() should be called before ValueString()
495 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900496 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700497 if (!success) {
498 // the detailed error message shall be printed in evaluate
499 return "";
500 }
Will McVickerefd970d2019-09-25 15:28:30 -0700501 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700502 if (!is_valid_) {
503 AIDL_ERROR(this) << "Invalid constant value: " + value_;
504 return "";
505 }
Jooyung Han690f5842020-12-04 13:02:04 +0900506
507 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900508 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900509 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
510 if (!enum_type) {
511 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900512 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900513 return "";
514 }
515 if (type_ != Type::REF) {
516 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
517 << enum_type->GetCanonicalName();
518 return "";
519 }
520 return decorator(type, value_);
521 }
522
Jooyung Hanaeb01672021-11-30 17:29:22 +0900523 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700524 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700525
Will McVickerd7d18df2019-09-12 13:40:50 -0700526 switch (final_type_) {
527 case Type::CHARACTER:
528 if (type_string == "char") {
529 return decorator(type, final_string_value_);
530 }
531 err = -1;
532 break;
533 case Type::STRING:
534 if (type_string == "String") {
535 return decorator(type, final_string_value_);
536 }
537 err = -1;
538 break;
539 case Type::BOOLEAN: // fall-through
540 case Type::INT8: // fall-through
541 case Type::INT32: // fall-through
542 case Type::INT64:
543 if (type_string == "byte") {
544 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
545 err = -1;
546 break;
547 }
548 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
549 } else if (type_string == "int") {
550 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
551 err = -1;
552 break;
553 }
554 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
555 } else if (type_string == "long") {
556 return decorator(type, std::to_string(final_value_));
557 } else if (type_string == "boolean") {
558 return decorator(type, final_value_ ? "true" : "false");
559 }
560 err = -1;
561 break;
562 case Type::ARRAY: {
563 if (!type.IsArray()) {
564 err = -1;
565 break;
566 }
567 vector<string> value_strings;
568 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700569 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700570
Will McVickerefd970d2019-09-25 15:28:30 -0700571 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900572 string value_string;
573 type.ViewAsArrayBase([&](const auto& base_type) {
574 value_string = value->ValueString(base_type, decorator);
575 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700576 if (value_string.empty()) {
577 success = false;
578 break;
579 }
580 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700581 }
582 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700583 err = -1;
584 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700585 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900586 if (type.IsFixedSizeArray()) {
587 auto size =
588 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
Jooyung Hane76bcc22022-01-23 22:49:45 +0900589 if (values_.size() != static_cast<size_t>(size)) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900590 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
591 << values_.size() << " elements";
592 err = -1;
593 break;
594 }
595 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900596 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700597 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700598 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700599 if (type_string == "double") {
600 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900601 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700602 AIDL_ERROR(this) << "Could not parse " << value_;
603 err = -1;
604 break;
605 }
Will McVickerefd970d2019-09-25 15:28:30 -0700606 return decorator(type, std::to_string(parsed_value));
607 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900608 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700609 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900610 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700611 AIDL_ERROR(this) << "Could not parse " << value_;
612 err = -1;
613 break;
614 }
Will McVickerefd970d2019-09-25 15:28:30 -0700615 return decorator(type, std::to_string(parsed_value) + "f");
616 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700617 err = -1;
618 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700619 }
Will McVickerefd970d2019-09-25 15:28:30 -0700620 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700621 err = -1;
622 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700623 }
624
Steven Moreland21780812020-09-11 01:29:45 +0000625 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700626 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
627 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700628 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700629}
630
631bool AidlConstantValue::CheckValid() const {
632 // Nothing needs to be checked here. The constant value will be validated in
633 // the constructor or in the evaluate() function.
634 if (is_evaluated_) return is_valid_;
635
636 switch (type_) {
637 case Type::BOOLEAN: // fall-through
638 case Type::INT8: // fall-through
639 case Type::INT32: // fall-through
640 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700641 case Type::CHARACTER: // fall-through
642 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900643 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700644 case Type::FLOATING: // fall-through
645 case Type::UNARY: // fall-through
646 case Type::BINARY:
647 is_valid_ = true;
648 break;
Jooyung Han29813842020-12-08 01:28:03 +0900649 case Type::ARRAY:
650 is_valid_ = true;
651 for (const auto& v : values_) is_valid_ &= v->CheckValid();
652 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800653 case Type::ERROR:
654 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700655 default:
656 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
657 return false;
658 }
659
660 return true;
661}
662
Jooyung Han74675c22020-12-15 08:39:57 +0900663bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700664 if (is_evaluated_) {
665 return is_valid_;
666 }
667 int err = 0;
668 is_evaluated_ = true;
669
670 switch (type_) {
671 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700672 Type array_type = Type::ERROR;
673 bool success = true;
674 for (const auto& value : values_) {
675 success = value->CheckValid();
676 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900677 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700678 if (!success) {
679 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
680 break;
681 }
682 if (array_type == Type::ERROR) {
683 array_type = value->final_type_;
684 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
685 value->final_type_)) {
686 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
687 << ". Expecting type compatible with " << ToString(array_type);
688 success = false;
689 break;
690 }
691 } else {
692 break;
693 }
694 }
695 if (!success) {
696 err = -1;
697 break;
698 }
699 final_type_ = type_;
700 break;
701 }
702 case Type::BOOLEAN:
703 if ((value_ != "true") && (value_ != "false")) {
704 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
705 err = -1;
706 break;
707 }
708 final_value_ = (value_ == "true") ? 1 : 0;
709 final_type_ = type_;
710 break;
711 case Type::INT8: // fall-through
712 case Type::INT32: // fall-through
713 case Type::INT64:
714 // Parsing happens in the constructor
715 final_type_ = type_;
716 break;
717 case Type::CHARACTER: // fall-through
718 case Type::STRING:
719 final_string_value_ = value_;
720 final_type_ = type_;
721 break;
722 case Type::FLOATING:
723 // Just parse on the fly in ValueString
724 final_type_ = type_;
725 break;
726 default:
727 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
728 err = -1;
729 }
730
731 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700732}
733
734string AidlConstantValue::ToString(Type type) {
735 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700736 case Type::BOOLEAN:
737 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700738 case Type::INT8:
739 return "an int8 literal";
740 case Type::INT32:
741 return "an int32 literal";
742 case Type::INT64:
743 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800744 case Type::ARRAY:
745 return "a literal array";
746 case Type::CHARACTER:
747 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700748 case Type::STRING:
749 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900750 case Type::REF:
751 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800752 case Type::FLOATING:
753 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700754 case Type::UNARY:
755 return "a unary expression";
756 case Type::BINARY:
757 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800758 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000759 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800760 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700761 default:
Steven Moreland21780812020-09-11 01:29:45 +0000762 AIDL_FATAL(AIDL_LOCATION_HERE)
763 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700764 return ""; // not reached
765 }
766}
767
Jooyung Hand0c8af02021-01-06 18:08:01 +0900768AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
769 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900770 const auto pos = value.find_last_of('.');
771 if (pos == string::npos) {
772 field_name_ = value;
773 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900774 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
775 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900776 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900777 field_name_ = value.substr(pos + 1);
778 }
779}
780
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900781const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900782 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900783
784 const AidlDefinedType* defined_type;
785 if (ref_type_) {
786 defined_type = ref_type_->GetDefinedType();
787 } else {
788 defined_type = scope;
789 }
790
791 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900792 // This can happen when "const reference" is used in an unsupported way,
793 // but missed in checks there. It works as a safety net.
794 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900795 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900796 }
797
Jooyung Han690f5842020-12-04 13:02:04 +0900798 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
799 for (const auto& e : enum_decl->GetEnumerators()) {
800 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900801 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900802 }
803 }
804 } else {
805 for (const auto& c : defined_type->GetConstantDeclarations()) {
806 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900807 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900808 }
809 }
810 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900811 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900812 return nullptr;
813}
814
815bool AidlConstantReference::CheckValid() const {
816 if (is_evaluated_) return is_valid_;
817 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
818 is_valid_ = resolved_->CheckValid();
819 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900820}
821
Jooyung Han74675c22020-12-15 08:39:57 +0900822bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900823 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900824 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
825 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900826
Jooyung Han74675c22020-12-15 08:39:57 +0900827 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900828 is_valid_ = resolved_->is_valid_;
829 final_type_ = resolved_->final_type_;
830 if (is_valid_) {
831 if (final_type_ == Type::STRING) {
832 final_string_value_ = resolved_->final_string_value_;
833 } else {
834 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900835 }
836 }
Jooyung Han29813842020-12-08 01:28:03 +0900837 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900838}
839
Will McVickerd7d18df2019-09-12 13:40:50 -0700840bool AidlUnaryConstExpression::CheckValid() const {
841 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000842 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700843
844 is_valid_ = unary_->CheckValid();
845 if (!is_valid_) {
846 final_type_ = Type::ERROR;
847 return false;
848 }
849
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800850 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700851}
852
Jooyung Han74675c22020-12-15 08:39:57 +0900853bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700854 if (is_evaluated_) {
855 return is_valid_;
856 }
857 is_evaluated_ = true;
858
859 // Recursively evaluate the expression tree
860 if (!unary_->is_evaluated_) {
861 // TODO(b/142722772) CheckValid() should be called before ValueString()
862 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900863 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700864 if (!success) {
865 is_valid_ = false;
866 return false;
867 }
868 }
Devin Moorec233fb82020-04-07 11:13:44 -0700869 if (!IsCompatibleType(unary_->final_type_, op_)) {
870 AIDL_ERROR(unary_) << "'" << op_ << "'"
871 << " is not compatible with " << ToString(unary_->final_type_)
872 << ": " + value_;
873 is_valid_ = false;
874 return false;
875 }
876 if (!unary_->is_valid_) {
877 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700878 is_valid_ = false;
879 return false;
880 }
881 final_type_ = unary_->final_type_;
882
883 if (final_type_ == Type::FLOATING) {
884 // don't do anything here. ValueString() will handle everything.
885 is_valid_ = true;
886 return true;
887 }
888
Steven Morelande1ff67e2020-07-16 23:22:36 +0000889#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800890 return is_valid_ = \
891 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700892
893 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
894 is_valid_ = false; return false;)
895}
896
Will McVickerd7d18df2019-09-12 13:40:50 -0700897bool AidlBinaryConstExpression::CheckValid() const {
898 bool success = false;
899 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000900 AIDL_FATAL_IF(left_val_ == nullptr, this);
901 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700902
903 success = left_val_->CheckValid();
904 if (!success) {
905 final_type_ = Type::ERROR;
906 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
907 }
908
909 success = right_val_->CheckValid();
910 if (!success) {
911 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
912 final_type_ = Type::ERROR;
913 }
914
915 if (final_type_ == Type::ERROR) {
916 is_valid_ = false;
917 return false;
918 }
919
920 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800921 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700922}
923
Jooyung Han74675c22020-12-15 08:39:57 +0900924bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700925 if (is_evaluated_) {
926 return is_valid_;
927 }
928 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900929 AIDL_FATAL_IF(left_val_ == nullptr, this);
930 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700931
932 // Recursively evaluate the binary expression tree
933 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
934 // TODO(b/142722772) CheckValid() should be called before ValueString()
935 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900936 success &= left_val_->evaluate();
937 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700938 if (!success) {
939 is_valid_ = false;
940 return false;
941 }
942 }
943 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
944 is_valid_ = false;
945 return false;
946 }
947 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
948 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000949 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
950 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
951 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700952 return false;
953 }
954
955 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
956
957 // Handle String case first
958 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000959 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700960 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000961 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700962 final_type_ = Type::ERROR;
963 is_valid_ = false;
964 return false;
965 }
966
967 // Remove trailing " from lhs
968 const string& lhs = left_val_->final_string_value_;
969 if (lhs.back() != '"') {
970 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
971 final_type_ = Type::ERROR;
972 is_valid_ = false;
973 return false;
974 }
975 const string& rhs = right_val_->final_string_value_;
976 // Remove starting " from rhs
977 if (rhs.front() != '"') {
978 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
979 final_type_ = Type::ERROR;
980 is_valid_ = false;
981 return false;
982 }
983
984 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
985 final_type_ = Type::STRING;
986 return true;
987 }
988
Will McVickerd7d18df2019-09-12 13:40:50 -0700989 // CASE: + - * / % | ^ & < > <= >= == !=
990 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700991 // promoted kind for both operands.
992 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
993 IntegralPromotion(right_val_->final_type_));
994 // result kind.
995 final_type_ = isArithmeticOrBitflip
996 ? promoted // arithmetic or bitflip operators generates promoted type
997 : Type::BOOLEAN; // comparison operators generates bool
998
Devin Moore1f0360d2020-12-21 12:12:48 -0800999#define CASE_BINARY_COMMON(__type__) \
1000 return is_valid_ = \
1001 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1002 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001003
1004 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1005 is_valid_ = false; return false;)
1006 }
1007
1008 // CASE: << >>
1009 string newOp = op_;
1010 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001011 // promoted kind for both operands.
1012 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1013 IntegralPromotion(right_val_->final_type_));
1014 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001015 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001016 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001017 // is defined as shifting into the other direction.
1018 newOp = OPEQ("<<") ? ">>" : "<<";
1019 numBits = -numBits;
1020 }
1021
Devin Moore1f0360d2020-12-21 12:12:48 -08001022#define CASE_SHIFT(__type__) \
1023 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1024 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001025
1026 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1027 is_valid_ = false; return false;)
1028 }
1029
1030 // CASE: && ||
1031 if (OP_IS_BIN_LOGICAL) {
1032 final_type_ = Type::BOOLEAN;
1033 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001034 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1035 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001036 }
1037
1038 SHOULD_NOT_REACH();
1039 is_valid_ = false;
1040 return false;
1041}
1042
Will McVickerd7d18df2019-09-12 13:40:50 -07001043AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1044 int64_t parsed_value, const string& checked_value)
1045 : AidlNode(location),
1046 type_(parsed_type),
1047 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001048 final_type_(parsed_type),
1049 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001050 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1051 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001052}
Will McVickerefd970d2019-09-25 15:28:30 -07001053
1054AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001055 const string& checked_value)
1056 : AidlNode(location),
1057 type_(type),
1058 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001059 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001060 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001061 switch (type_) {
1062 case Type::INT8:
1063 case Type::INT32:
1064 case Type::INT64:
1065 case Type::ARRAY:
1066 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1067 break;
1068 default:
1069 break;
1070 }
1071}
1072
1073AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001074 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1075 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001076 : AidlNode(location),
1077 type_(type),
1078 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001079 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001080 is_valid_(false),
1081 is_evaluated_(false),
1082 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001083 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001084}
1085
1086AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1087 std::unique_ptr<AidlConstantValue> rval)
1088 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1089 unary_(std::move(rval)),
1090 op_(op) {
1091 final_type_ = Type::UNARY;
1092}
1093
1094AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1095 std::unique_ptr<AidlConstantValue> lval,
1096 const string& op,
1097 std::unique_ptr<AidlConstantValue> rval)
1098 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1099 left_val_(std::move(lval)),
1100 right_val_(std::move(rval)),
1101 op_(op) {
1102 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001103}