blob: 244ec4be5d1701df24a1cfdf72c44f2ae177ea15 [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 Moreland8c9b7ec2022-01-26 22:50:12 +0000364 if (!isValidLiteralChar(value)) {
365 AIDL_ERROR(location) << "Invalid character literal " << value;
366 return new AidlConstantValue(location, Type::ERROR, explicit_value);
367 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800368 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700369}
370
371AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
372 const std::string& value) {
373 return new AidlConstantValue(location, Type::FLOATING, value);
374}
375
Will McVickerd7d18df2019-09-12 13:40:50 -0700376bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000377 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700378}
379
Will McVickerd7d18df2019-09-12 13:40:50 -0700380bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
381 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700382 if (parsed_value == nullptr || parsed_type == nullptr) {
383 return false;
384 }
385
Steven Morelandb7d58652021-10-25 15:10:02 -0700386 std::string_view value_view = value;
387 const bool is_byte = ConsumeSuffix(&value_view, "u8");
388 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
389 const std::string value_substr = std::string(value_view);
390
391 *parsed_value = 0;
392 *parsed_type = Type::ERROR;
393
394 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700395
Steven Morelandcef22662020-07-08 20:54:28 +0000396 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700397 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
398 // handle that when computing constant expressions, then we need to
399 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
400 // so we parse as an unsigned int when possible and then cast to a signed
401 // int. One example of this is in ICameraService.aidl where a constant int
402 // is used for bit manipulations which ideally should be handled with an
403 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000404 //
405 // Note, for historical consistency, we need to consider small hex values
406 // as an integral type. Recognizing them as INT8 could break some files,
407 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700408 if (is_byte) {
409 uint8_t raw_value8;
410 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
411 return false;
412 }
413 *parsed_value = static_cast<int8_t>(raw_value8);
414 *parsed_type = Type::INT8;
415 } else if (uint32_t raw_value32;
416 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
417 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700418 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700419 } else if (uint64_t raw_value64;
420 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
421 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700422 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000423 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000424 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700425 }
426 return true;
427 }
428
Steven Morelandcef22662020-07-08 20:54:28 +0000429 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700430 return false;
431 }
432
Steven Morelandb7d58652021-10-25 15:10:02 -0700433 if (is_byte) {
434 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
435 return false;
436 }
437 *parsed_value = static_cast<int8_t>(*parsed_value);
438 *parsed_type = Type::INT8;
439 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000440 *parsed_type = Type::INT64;
441 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 // guess literal type.
443 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
444 *parsed_type = Type::INT8;
445 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
446 *parsed_type = Type::INT32;
447 } else {
448 *parsed_type = Type::INT64;
449 }
450 }
451 return true;
452}
453
454AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000455 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700456
457 Type parsed_type;
458 int64_t parsed_value = 0;
459 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
460 if (!success) {
461 return nullptr;
462 }
463
464 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700465}
466
467AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000469 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900470 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900471 std::vector<std::string> str_values;
472 for (const auto& v : *values) {
473 str_values.push_back(v->value_);
474 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900475 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
476 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700477}
478
Will McVickerd7d18df2019-09-12 13:40:50 -0700479AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700480 for (size_t i = 0; i < value.length(); ++i) {
481 if (!isValidLiteralChar(value[i])) {
482 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
483 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800484 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700485 }
486 }
487
488 return new AidlConstantValue(location, Type::STRING, value);
489}
490
Will McVickerd7d18df2019-09-12 13:40:50 -0700491string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
492 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700493 if (type.IsGeneric()) {
494 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
495 return "";
496 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700497 if (!is_evaluated_) {
498 // TODO(b/142722772) CheckValid() should be called before ValueString()
499 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900500 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700501 if (!success) {
502 // the detailed error message shall be printed in evaluate
503 return "";
504 }
Will McVickerefd970d2019-09-25 15:28:30 -0700505 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700506 if (!is_valid_) {
507 AIDL_ERROR(this) << "Invalid constant value: " + value_;
508 return "";
509 }
Jooyung Han690f5842020-12-04 13:02:04 +0900510
511 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900512 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900513 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
514 if (!enum_type) {
515 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900516 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900517 return "";
518 }
519 if (type_ != Type::REF) {
520 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
521 << enum_type->GetCanonicalName();
522 return "";
523 }
524 return decorator(type, value_);
525 }
526
Jooyung Hanaeb01672021-11-30 17:29:22 +0900527 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700528 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700529
Will McVickerd7d18df2019-09-12 13:40:50 -0700530 switch (final_type_) {
531 case Type::CHARACTER:
532 if (type_string == "char") {
533 return decorator(type, final_string_value_);
534 }
535 err = -1;
536 break;
537 case Type::STRING:
538 if (type_string == "String") {
539 return decorator(type, final_string_value_);
540 }
541 err = -1;
542 break;
543 case Type::BOOLEAN: // fall-through
544 case Type::INT8: // fall-through
545 case Type::INT32: // fall-through
546 case Type::INT64:
547 if (type_string == "byte") {
548 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
549 err = -1;
550 break;
551 }
552 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
553 } else if (type_string == "int") {
554 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
555 err = -1;
556 break;
557 }
558 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
559 } else if (type_string == "long") {
560 return decorator(type, std::to_string(final_value_));
561 } else if (type_string == "boolean") {
562 return decorator(type, final_value_ ? "true" : "false");
563 }
564 err = -1;
565 break;
566 case Type::ARRAY: {
567 if (!type.IsArray()) {
568 err = -1;
569 break;
570 }
571 vector<string> value_strings;
572 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700573 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700574
Will McVickerefd970d2019-09-25 15:28:30 -0700575 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900576 string value_string;
577 type.ViewAsArrayBase([&](const auto& base_type) {
578 value_string = value->ValueString(base_type, decorator);
579 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700580 if (value_string.empty()) {
581 success = false;
582 break;
583 }
584 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700585 }
586 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700587 err = -1;
588 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700589 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900590 if (type.IsFixedSizeArray()) {
591 auto size =
592 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
Jooyung Hane76bcc22022-01-23 22:49:45 +0900593 if (values_.size() != static_cast<size_t>(size)) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900594 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
595 << values_.size() << " elements";
596 err = -1;
597 break;
598 }
599 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900600 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700601 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700602 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700603 if (type_string == "double") {
604 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900605 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700606 AIDL_ERROR(this) << "Could not parse " << value_;
607 err = -1;
608 break;
609 }
Will McVickerefd970d2019-09-25 15:28:30 -0700610 return decorator(type, std::to_string(parsed_value));
611 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900612 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700613 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900614 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700615 AIDL_ERROR(this) << "Could not parse " << value_;
616 err = -1;
617 break;
618 }
Will McVickerefd970d2019-09-25 15:28:30 -0700619 return decorator(type, std::to_string(parsed_value) + "f");
620 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700621 err = -1;
622 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700623 }
Will McVickerefd970d2019-09-25 15:28:30 -0700624 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700625 err = -1;
626 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700627 }
628
Steven Moreland21780812020-09-11 01:29:45 +0000629 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700630 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
631 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700632 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700633}
634
635bool AidlConstantValue::CheckValid() const {
636 // Nothing needs to be checked here. The constant value will be validated in
637 // the constructor or in the evaluate() function.
638 if (is_evaluated_) return is_valid_;
639
640 switch (type_) {
641 case Type::BOOLEAN: // fall-through
642 case Type::INT8: // fall-through
643 case Type::INT32: // fall-through
644 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700645 case Type::CHARACTER: // fall-through
646 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900647 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700648 case Type::FLOATING: // fall-through
649 case Type::UNARY: // fall-through
650 case Type::BINARY:
651 is_valid_ = true;
652 break;
Jooyung Han29813842020-12-08 01:28:03 +0900653 case Type::ARRAY:
654 is_valid_ = true;
655 for (const auto& v : values_) is_valid_ &= v->CheckValid();
656 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800657 case Type::ERROR:
658 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700659 default:
660 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
661 return false;
662 }
663
664 return true;
665}
666
Jooyung Han74675c22020-12-15 08:39:57 +0900667bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700668 if (is_evaluated_) {
669 return is_valid_;
670 }
671 int err = 0;
672 is_evaluated_ = true;
673
674 switch (type_) {
675 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700676 Type array_type = Type::ERROR;
677 bool success = true;
678 for (const auto& value : values_) {
679 success = value->CheckValid();
680 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900681 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700682 if (!success) {
683 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
684 break;
685 }
686 if (array_type == Type::ERROR) {
687 array_type = value->final_type_;
688 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
689 value->final_type_)) {
690 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
691 << ". Expecting type compatible with " << ToString(array_type);
692 success = false;
693 break;
694 }
695 } else {
696 break;
697 }
698 }
699 if (!success) {
700 err = -1;
701 break;
702 }
703 final_type_ = type_;
704 break;
705 }
706 case Type::BOOLEAN:
707 if ((value_ != "true") && (value_ != "false")) {
708 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
709 err = -1;
710 break;
711 }
712 final_value_ = (value_ == "true") ? 1 : 0;
713 final_type_ = type_;
714 break;
715 case Type::INT8: // fall-through
716 case Type::INT32: // fall-through
717 case Type::INT64:
718 // Parsing happens in the constructor
719 final_type_ = type_;
720 break;
721 case Type::CHARACTER: // fall-through
722 case Type::STRING:
723 final_string_value_ = value_;
724 final_type_ = type_;
725 break;
726 case Type::FLOATING:
727 // Just parse on the fly in ValueString
728 final_type_ = type_;
729 break;
730 default:
731 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
732 err = -1;
733 }
734
735 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700736}
737
738string AidlConstantValue::ToString(Type type) {
739 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700740 case Type::BOOLEAN:
741 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700742 case Type::INT8:
743 return "an int8 literal";
744 case Type::INT32:
745 return "an int32 literal";
746 case Type::INT64:
747 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800748 case Type::ARRAY:
749 return "a literal array";
750 case Type::CHARACTER:
751 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700752 case Type::STRING:
753 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900754 case Type::REF:
755 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800756 case Type::FLOATING:
757 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700758 case Type::UNARY:
759 return "a unary expression";
760 case Type::BINARY:
761 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800762 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000763 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800764 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700765 default:
Steven Moreland21780812020-09-11 01:29:45 +0000766 AIDL_FATAL(AIDL_LOCATION_HERE)
767 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700768 return ""; // not reached
769 }
770}
771
Jooyung Hand0c8af02021-01-06 18:08:01 +0900772AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
773 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900774 const auto pos = value.find_last_of('.');
775 if (pos == string::npos) {
776 field_name_ = value;
777 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900778 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
779 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900780 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900781 field_name_ = value.substr(pos + 1);
782 }
783}
784
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900785const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900786 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900787
788 const AidlDefinedType* defined_type;
789 if (ref_type_) {
790 defined_type = ref_type_->GetDefinedType();
791 } else {
792 defined_type = scope;
793 }
794
795 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900796 // This can happen when "const reference" is used in an unsupported way,
797 // but missed in checks there. It works as a safety net.
798 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900799 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900800 }
801
Jooyung Han690f5842020-12-04 13:02:04 +0900802 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
803 for (const auto& e : enum_decl->GetEnumerators()) {
804 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900805 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900806 }
807 }
808 } else {
809 for (const auto& c : defined_type->GetConstantDeclarations()) {
810 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900811 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900812 }
813 }
814 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900815 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900816 return nullptr;
817}
818
819bool AidlConstantReference::CheckValid() const {
820 if (is_evaluated_) return is_valid_;
821 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
822 is_valid_ = resolved_->CheckValid();
823 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900824}
825
Jooyung Han74675c22020-12-15 08:39:57 +0900826bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900827 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900828 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
829 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900830
Jooyung Han74675c22020-12-15 08:39:57 +0900831 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900832 is_valid_ = resolved_->is_valid_;
833 final_type_ = resolved_->final_type_;
834 if (is_valid_) {
835 if (final_type_ == Type::STRING) {
836 final_string_value_ = resolved_->final_string_value_;
837 } else {
838 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900839 }
840 }
Jooyung Han29813842020-12-08 01:28:03 +0900841 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900842}
843
Will McVickerd7d18df2019-09-12 13:40:50 -0700844bool AidlUnaryConstExpression::CheckValid() const {
845 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000846 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700847
848 is_valid_ = unary_->CheckValid();
849 if (!is_valid_) {
850 final_type_ = Type::ERROR;
851 return false;
852 }
853
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800854 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700855}
856
Jooyung Han74675c22020-12-15 08:39:57 +0900857bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700858 if (is_evaluated_) {
859 return is_valid_;
860 }
861 is_evaluated_ = true;
862
863 // Recursively evaluate the expression tree
864 if (!unary_->is_evaluated_) {
865 // TODO(b/142722772) CheckValid() should be called before ValueString()
866 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900867 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700868 if (!success) {
869 is_valid_ = false;
870 return false;
871 }
872 }
Devin Moorec233fb82020-04-07 11:13:44 -0700873 if (!IsCompatibleType(unary_->final_type_, op_)) {
874 AIDL_ERROR(unary_) << "'" << op_ << "'"
875 << " is not compatible with " << ToString(unary_->final_type_)
876 << ": " + value_;
877 is_valid_ = false;
878 return false;
879 }
880 if (!unary_->is_valid_) {
881 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700882 is_valid_ = false;
883 return false;
884 }
885 final_type_ = unary_->final_type_;
886
887 if (final_type_ == Type::FLOATING) {
888 // don't do anything here. ValueString() will handle everything.
889 is_valid_ = true;
890 return true;
891 }
892
Steven Morelande1ff67e2020-07-16 23:22:36 +0000893#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800894 return is_valid_ = \
895 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700896
897 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
898 is_valid_ = false; return false;)
899}
900
Will McVickerd7d18df2019-09-12 13:40:50 -0700901bool AidlBinaryConstExpression::CheckValid() const {
902 bool success = false;
903 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000904 AIDL_FATAL_IF(left_val_ == nullptr, this);
905 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700906
907 success = left_val_->CheckValid();
908 if (!success) {
909 final_type_ = Type::ERROR;
910 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
911 }
912
913 success = right_val_->CheckValid();
914 if (!success) {
915 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
916 final_type_ = Type::ERROR;
917 }
918
919 if (final_type_ == Type::ERROR) {
920 is_valid_ = false;
921 return false;
922 }
923
924 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800925 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700926}
927
Jooyung Han74675c22020-12-15 08:39:57 +0900928bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700929 if (is_evaluated_) {
930 return is_valid_;
931 }
932 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900933 AIDL_FATAL_IF(left_val_ == nullptr, this);
934 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700935
936 // Recursively evaluate the binary expression tree
937 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
938 // TODO(b/142722772) CheckValid() should be called before ValueString()
939 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900940 success &= left_val_->evaluate();
941 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700942 if (!success) {
943 is_valid_ = false;
944 return false;
945 }
946 }
947 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
948 is_valid_ = false;
949 return false;
950 }
951 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
952 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000953 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
954 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
955 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700956 return false;
957 }
958
959 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
960
961 // Handle String case first
962 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000963 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700964 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000965 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700966 final_type_ = Type::ERROR;
967 is_valid_ = false;
968 return false;
969 }
970
971 // Remove trailing " from lhs
972 const string& lhs = left_val_->final_string_value_;
973 if (lhs.back() != '"') {
974 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
975 final_type_ = Type::ERROR;
976 is_valid_ = false;
977 return false;
978 }
979 const string& rhs = right_val_->final_string_value_;
980 // Remove starting " from rhs
981 if (rhs.front() != '"') {
982 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
983 final_type_ = Type::ERROR;
984 is_valid_ = false;
985 return false;
986 }
987
988 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
989 final_type_ = Type::STRING;
990 return true;
991 }
992
Will McVickerd7d18df2019-09-12 13:40:50 -0700993 // CASE: + - * / % | ^ & < > <= >= == !=
994 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700995 // promoted kind for both operands.
996 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
997 IntegralPromotion(right_val_->final_type_));
998 // result kind.
999 final_type_ = isArithmeticOrBitflip
1000 ? promoted // arithmetic or bitflip operators generates promoted type
1001 : Type::BOOLEAN; // comparison operators generates bool
1002
Devin Moore1f0360d2020-12-21 12:12:48 -08001003#define CASE_BINARY_COMMON(__type__) \
1004 return is_valid_ = \
1005 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1006 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001007
1008 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1009 is_valid_ = false; return false;)
1010 }
1011
1012 // CASE: << >>
1013 string newOp = op_;
1014 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001015 // promoted kind for both operands.
1016 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1017 IntegralPromotion(right_val_->final_type_));
1018 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001019 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001020 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001021 // is defined as shifting into the other direction.
1022 newOp = OPEQ("<<") ? ">>" : "<<";
1023 numBits = -numBits;
1024 }
1025
Devin Moore1f0360d2020-12-21 12:12:48 -08001026#define CASE_SHIFT(__type__) \
1027 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1028 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001029
1030 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1031 is_valid_ = false; return false;)
1032 }
1033
1034 // CASE: && ||
1035 if (OP_IS_BIN_LOGICAL) {
1036 final_type_ = Type::BOOLEAN;
1037 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001038 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1039 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001040 }
1041
1042 SHOULD_NOT_REACH();
1043 is_valid_ = false;
1044 return false;
1045}
1046
Will McVickerd7d18df2019-09-12 13:40:50 -07001047AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1048 int64_t parsed_value, const string& checked_value)
1049 : AidlNode(location),
1050 type_(parsed_type),
1051 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001052 final_type_(parsed_type),
1053 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001054 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1055 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001056}
Will McVickerefd970d2019-09-25 15:28:30 -07001057
1058AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001059 const string& checked_value)
1060 : AidlNode(location),
1061 type_(type),
1062 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001063 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001064 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001065 switch (type_) {
1066 case Type::INT8:
1067 case Type::INT32:
1068 case Type::INT64:
1069 case Type::ARRAY:
1070 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1071 break;
1072 default:
1073 break;
1074 }
1075}
1076
1077AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001078 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1079 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001080 : AidlNode(location),
1081 type_(type),
1082 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001083 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001084 is_valid_(false),
1085 is_evaluated_(false),
1086 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001087 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001088}
1089
1090AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1091 std::unique_ptr<AidlConstantValue> rval)
1092 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1093 unary_(std::move(rval)),
1094 op_(op) {
1095 final_type_ = Type::UNARY;
1096}
1097
1098AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1099 std::unique_ptr<AidlConstantValue> lval,
1100 const string& op,
1101 std::unique_ptr<AidlConstantValue> rval)
1102 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1103 left_val_(std::move(lval)),
1104 right_val_(std::move(rval)),
1105 op_(op) {
1106 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001107}