blob: 0521cea6564fe27c6fcdc3eee2bc544111a182c6 [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) {
282 case Type::STRING:
283 if (t2 == Type::STRING) {
284 return true;
285 }
286 break;
287 case Type::BOOLEAN: // fall-through
288 case Type::INT8: // fall-through
289 case Type::INT32: // fall-through
290 case Type::INT64:
291 switch (t2) {
292 case Type::BOOLEAN: // fall-through
293 case Type::INT8: // fall-through
294 case Type::INT32: // fall-through
295 case Type::INT64:
296 return true;
297 break;
298 default:
299 break;
300 }
301 break;
302 default:
303 break;
304 }
305
306 return false;
307}
308
309// Returns the promoted kind for both operands
310AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
311 Type right) {
312 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000313 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
314 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700315
316 // Kinds in concern: bool, (u)int[8|32|64]
317 if (left == right) return left; // easy case
318 if (left == Type::BOOLEAN) return right;
319 if (right == Type::BOOLEAN) return left;
320
321 return left < right ? right : left;
322}
323
324// Returns the promoted integral type where INT32 is the smallest type
325AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
326 return (Type::INT32 < in) ? in : Type::INT32;
327}
328
Steven Moreland541788d2020-05-21 22:05:52 +0000329AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
330 AidlLocation location = specifier.GetLocation();
331
332 // allocation of int[0] is a bit wasteful in Java
333 if (specifier.IsArray()) {
334 return nullptr;
335 }
336
337 const std::string name = specifier.GetName();
338 if (name == "boolean") {
339 return Boolean(location, false);
340 }
341 if (name == "byte" || name == "int" || name == "long") {
342 return Integral(location, "0");
343 }
344 if (name == "float") {
345 return Floating(location, "0.0f");
346 }
347 if (name == "double") {
348 return Floating(location, "0.0");
349 }
350 return nullptr;
351}
352
Will McVickerefd970d2019-09-25 15:28:30 -0700353AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
354 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
355}
356
357AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800358 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700359 if (!isValidLiteralChar(value)) {
360 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800361 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700362 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800363 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700364}
365
366AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
367 const std::string& value) {
368 return new AidlConstantValue(location, Type::FLOATING, value);
369}
370
Will McVickerd7d18df2019-09-12 13:40:50 -0700371bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000372 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700373}
374
Will McVickerd7d18df2019-09-12 13:40:50 -0700375bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
376 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700377 if (parsed_value == nullptr || parsed_type == nullptr) {
378 return false;
379 }
380
Steven Morelandb7d58652021-10-25 15:10:02 -0700381 std::string_view value_view = value;
382 const bool is_byte = ConsumeSuffix(&value_view, "u8");
383 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
384 const std::string value_substr = std::string(value_view);
385
386 *parsed_value = 0;
387 *parsed_type = Type::ERROR;
388
389 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700390
Steven Morelandcef22662020-07-08 20:54:28 +0000391 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700392 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
393 // handle that when computing constant expressions, then we need to
394 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
395 // so we parse as an unsigned int when possible and then cast to a signed
396 // int. One example of this is in ICameraService.aidl where a constant int
397 // is used for bit manipulations which ideally should be handled with an
398 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000399 //
400 // Note, for historical consistency, we need to consider small hex values
401 // as an integral type. Recognizing them as INT8 could break some files,
402 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700403 if (is_byte) {
404 uint8_t raw_value8;
405 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
406 return false;
407 }
408 *parsed_value = static_cast<int8_t>(raw_value8);
409 *parsed_type = Type::INT8;
410 } else if (uint32_t raw_value32;
411 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
412 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700413 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700414 } else if (uint64_t raw_value64;
415 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
416 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700417 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000418 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000419 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700420 }
421 return true;
422 }
423
Steven Morelandcef22662020-07-08 20:54:28 +0000424 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700425 return false;
426 }
427
Steven Morelandb7d58652021-10-25 15:10:02 -0700428 if (is_byte) {
429 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
430 return false;
431 }
432 *parsed_value = static_cast<int8_t>(*parsed_value);
433 *parsed_type = Type::INT8;
434 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000435 *parsed_type = Type::INT64;
436 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700437 // guess literal type.
438 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
439 *parsed_type = Type::INT8;
440 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
441 *parsed_type = Type::INT32;
442 } else {
443 *parsed_type = Type::INT64;
444 }
445 }
446 return true;
447}
448
449AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000450 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700451
452 Type parsed_type;
453 int64_t parsed_value = 0;
454 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
455 if (!success) {
456 return nullptr;
457 }
458
459 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700460}
461
462AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700463 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000464 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Han29813842020-12-08 01:28:03 +0900465 std::vector<std::string> str_values;
466 for (const auto& v : *values) {
467 str_values.push_back(v->value_);
468 }
469 return new AidlConstantValue(location, Type::ARRAY, std::move(values), Join(str_values, ", "));
Will McVickerefd970d2019-09-25 15:28:30 -0700470}
471
Will McVickerd7d18df2019-09-12 13:40:50 -0700472AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700473 for (size_t i = 0; i < value.length(); ++i) {
474 if (!isValidLiteralChar(value[i])) {
475 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
476 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800477 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700478 }
479 }
480
481 return new AidlConstantValue(location, Type::STRING, value);
482}
483
Will McVickerd7d18df2019-09-12 13:40:50 -0700484string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
485 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700486 if (type.IsGeneric()) {
487 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
488 return "";
489 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700490 if (!is_evaluated_) {
491 // TODO(b/142722772) CheckValid() should be called before ValueString()
492 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900493 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700494 if (!success) {
495 // the detailed error message shall be printed in evaluate
496 return "";
497 }
Will McVickerefd970d2019-09-25 15:28:30 -0700498 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700499 if (!is_valid_) {
500 AIDL_ERROR(this) << "Invalid constant value: " + value_;
501 return "";
502 }
Jooyung Han690f5842020-12-04 13:02:04 +0900503
504 const AidlDefinedType* defined_type = type.GetDefinedType();
505 if (defined_type && !type.IsArray()) {
506 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
507 if (!enum_type) {
508 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900509 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900510 return "";
511 }
512 if (type_ != Type::REF) {
513 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
514 << enum_type->GetCanonicalName();
515 return "";
516 }
517 return decorator(type, value_);
518 }
519
Will McVickerd7d18df2019-09-12 13:40:50 -0700520 const string& type_string = type.GetName();
521 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700522
Will McVickerd7d18df2019-09-12 13:40:50 -0700523 switch (final_type_) {
524 case Type::CHARACTER:
525 if (type_string == "char") {
526 return decorator(type, final_string_value_);
527 }
528 err = -1;
529 break;
530 case Type::STRING:
531 if (type_string == "String") {
532 return decorator(type, final_string_value_);
533 }
534 err = -1;
535 break;
536 case Type::BOOLEAN: // fall-through
537 case Type::INT8: // fall-through
538 case Type::INT32: // fall-through
539 case Type::INT64:
540 if (type_string == "byte") {
541 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
542 err = -1;
543 break;
544 }
545 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
546 } else if (type_string == "int") {
547 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
548 err = -1;
549 break;
550 }
551 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
552 } else if (type_string == "long") {
553 return decorator(type, std::to_string(final_value_));
554 } else if (type_string == "boolean") {
555 return decorator(type, final_value_ ? "true" : "false");
556 }
557 err = -1;
558 break;
559 case Type::ARRAY: {
560 if (!type.IsArray()) {
561 err = -1;
562 break;
563 }
564 vector<string> value_strings;
565 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700566 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700567
Will McVickerefd970d2019-09-25 15:28:30 -0700568 for (const auto& value : values_) {
Steven Moreland0cac8662021-10-08 16:43:29 -0700569 string value_string;
570 type.ViewAsArrayBase([&](const AidlTypeSpecifier& base) {
571 value_string = value->ValueString(base, decorator);
572 });
573
Will McVickerd7d18df2019-09-12 13:40:50 -0700574 if (value_string.empty()) {
575 success = false;
576 break;
577 }
578 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700579 }
580 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700581 err = -1;
582 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700583 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700584
585 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700586 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700587 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700588 if (type_string == "double") {
589 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900590 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700591 AIDL_ERROR(this) << "Could not parse " << value_;
592 err = -1;
593 break;
594 }
Will McVickerefd970d2019-09-25 15:28:30 -0700595 return decorator(type, std::to_string(parsed_value));
596 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900597 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700598 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900599 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700600 AIDL_ERROR(this) << "Could not parse " << value_;
601 err = -1;
602 break;
603 }
Will McVickerefd970d2019-09-25 15:28:30 -0700604 return decorator(type, std::to_string(parsed_value) + "f");
605 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700606 err = -1;
607 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700608 }
Will McVickerefd970d2019-09-25 15:28:30 -0700609 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700610 err = -1;
611 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700612 }
613
Steven Moreland21780812020-09-11 01:29:45 +0000614 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700615 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
616 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700617 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700618}
619
620bool AidlConstantValue::CheckValid() const {
621 // Nothing needs to be checked here. The constant value will be validated in
622 // the constructor or in the evaluate() function.
623 if (is_evaluated_) return is_valid_;
624
625 switch (type_) {
626 case Type::BOOLEAN: // fall-through
627 case Type::INT8: // fall-through
628 case Type::INT32: // fall-through
629 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700630 case Type::CHARACTER: // fall-through
631 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900632 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700633 case Type::FLOATING: // fall-through
634 case Type::UNARY: // fall-through
635 case Type::BINARY:
636 is_valid_ = true;
637 break;
Jooyung Han29813842020-12-08 01:28:03 +0900638 case Type::ARRAY:
639 is_valid_ = true;
640 for (const auto& v : values_) is_valid_ &= v->CheckValid();
641 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800642 case Type::ERROR:
643 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700644 default:
645 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
646 return false;
647 }
648
649 return true;
650}
651
Jooyung Han74675c22020-12-15 08:39:57 +0900652bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700653 if (is_evaluated_) {
654 return is_valid_;
655 }
656 int err = 0;
657 is_evaluated_ = true;
658
659 switch (type_) {
660 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700661 Type array_type = Type::ERROR;
662 bool success = true;
663 for (const auto& value : values_) {
664 success = value->CheckValid();
665 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900666 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700667 if (!success) {
668 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
669 break;
670 }
671 if (array_type == Type::ERROR) {
672 array_type = value->final_type_;
673 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
674 value->final_type_)) {
675 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
676 << ". Expecting type compatible with " << ToString(array_type);
677 success = false;
678 break;
679 }
680 } else {
681 break;
682 }
683 }
684 if (!success) {
685 err = -1;
686 break;
687 }
688 final_type_ = type_;
689 break;
690 }
691 case Type::BOOLEAN:
692 if ((value_ != "true") && (value_ != "false")) {
693 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
694 err = -1;
695 break;
696 }
697 final_value_ = (value_ == "true") ? 1 : 0;
698 final_type_ = type_;
699 break;
700 case Type::INT8: // fall-through
701 case Type::INT32: // fall-through
702 case Type::INT64:
703 // Parsing happens in the constructor
704 final_type_ = type_;
705 break;
706 case Type::CHARACTER: // fall-through
707 case Type::STRING:
708 final_string_value_ = value_;
709 final_type_ = type_;
710 break;
711 case Type::FLOATING:
712 // Just parse on the fly in ValueString
713 final_type_ = type_;
714 break;
715 default:
716 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
717 err = -1;
718 }
719
720 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700721}
722
723string AidlConstantValue::ToString(Type type) {
724 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700725 case Type::BOOLEAN:
726 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700727 case Type::INT8:
728 return "an int8 literal";
729 case Type::INT32:
730 return "an int32 literal";
731 case Type::INT64:
732 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800733 case Type::ARRAY:
734 return "a literal array";
735 case Type::CHARACTER:
736 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700737 case Type::STRING:
738 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900739 case Type::REF:
740 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800741 case Type::FLOATING:
742 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700743 case Type::UNARY:
744 return "a unary expression";
745 case Type::BINARY:
746 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800747 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000748 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800749 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700750 default:
Steven Moreland21780812020-09-11 01:29:45 +0000751 AIDL_FATAL(AIDL_LOCATION_HERE)
752 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700753 return ""; // not reached
754 }
755}
756
Jooyung Hand0c8af02021-01-06 18:08:01 +0900757AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
758 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900759 const auto pos = value.find_last_of('.');
760 if (pos == string::npos) {
761 field_name_ = value;
762 } else {
Jooyung Han8451a202021-01-16 03:07:06 +0900763 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), false, nullptr,
764 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900765 field_name_ = value.substr(pos + 1);
766 }
767}
768
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900769const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900770 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900771
772 const AidlDefinedType* defined_type;
773 if (ref_type_) {
774 defined_type = ref_type_->GetDefinedType();
775 } else {
776 defined_type = scope;
777 }
778
779 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900780 // This can happen when "const reference" is used in an unsupported way,
781 // but missed in checks there. It works as a safety net.
782 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900783 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900784 }
785
Jooyung Han690f5842020-12-04 13:02:04 +0900786 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
787 for (const auto& e : enum_decl->GetEnumerators()) {
788 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900789 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900790 }
791 }
792 } else {
793 for (const auto& c : defined_type->GetConstantDeclarations()) {
794 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900795 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900796 }
797 }
798 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900799 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900800 return nullptr;
801}
802
803bool AidlConstantReference::CheckValid() const {
804 if (is_evaluated_) return is_valid_;
805 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
806 is_valid_ = resolved_->CheckValid();
807 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900808}
809
Jooyung Han74675c22020-12-15 08:39:57 +0900810bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900811 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900812 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
813 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900814
Jooyung Han74675c22020-12-15 08:39:57 +0900815 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900816 is_valid_ = resolved_->is_valid_;
817 final_type_ = resolved_->final_type_;
818 if (is_valid_) {
819 if (final_type_ == Type::STRING) {
820 final_string_value_ = resolved_->final_string_value_;
821 } else {
822 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900823 }
824 }
Jooyung Han29813842020-12-08 01:28:03 +0900825 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900826}
827
Will McVickerd7d18df2019-09-12 13:40:50 -0700828bool AidlUnaryConstExpression::CheckValid() const {
829 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000830 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700831
832 is_valid_ = unary_->CheckValid();
833 if (!is_valid_) {
834 final_type_ = Type::ERROR;
835 return false;
836 }
837
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800838 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700839}
840
Jooyung Han74675c22020-12-15 08:39:57 +0900841bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700842 if (is_evaluated_) {
843 return is_valid_;
844 }
845 is_evaluated_ = true;
846
847 // Recursively evaluate the expression tree
848 if (!unary_->is_evaluated_) {
849 // TODO(b/142722772) CheckValid() should be called before ValueString()
850 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900851 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700852 if (!success) {
853 is_valid_ = false;
854 return false;
855 }
856 }
Devin Moorec233fb82020-04-07 11:13:44 -0700857 if (!IsCompatibleType(unary_->final_type_, op_)) {
858 AIDL_ERROR(unary_) << "'" << op_ << "'"
859 << " is not compatible with " << ToString(unary_->final_type_)
860 << ": " + value_;
861 is_valid_ = false;
862 return false;
863 }
864 if (!unary_->is_valid_) {
865 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700866 is_valid_ = false;
867 return false;
868 }
869 final_type_ = unary_->final_type_;
870
871 if (final_type_ == Type::FLOATING) {
872 // don't do anything here. ValueString() will handle everything.
873 is_valid_ = true;
874 return true;
875 }
876
Steven Morelande1ff67e2020-07-16 23:22:36 +0000877#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800878 return is_valid_ = \
879 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700880
881 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
882 is_valid_ = false; return false;)
883}
884
Will McVickerd7d18df2019-09-12 13:40:50 -0700885bool AidlBinaryConstExpression::CheckValid() const {
886 bool success = false;
887 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000888 AIDL_FATAL_IF(left_val_ == nullptr, this);
889 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700890
891 success = left_val_->CheckValid();
892 if (!success) {
893 final_type_ = Type::ERROR;
894 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
895 }
896
897 success = right_val_->CheckValid();
898 if (!success) {
899 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
900 final_type_ = Type::ERROR;
901 }
902
903 if (final_type_ == Type::ERROR) {
904 is_valid_ = false;
905 return false;
906 }
907
908 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800909 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700910}
911
Jooyung Han74675c22020-12-15 08:39:57 +0900912bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700913 if (is_evaluated_) {
914 return is_valid_;
915 }
916 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900917 AIDL_FATAL_IF(left_val_ == nullptr, this);
918 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700919
920 // Recursively evaluate the binary expression tree
921 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
922 // TODO(b/142722772) CheckValid() should be called before ValueString()
923 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900924 success &= left_val_->evaluate();
925 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700926 if (!success) {
927 is_valid_ = false;
928 return false;
929 }
930 }
931 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
932 is_valid_ = false;
933 return false;
934 }
935 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
936 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000937 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
938 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
939 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700940 return false;
941 }
942
943 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
944
945 // Handle String case first
946 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000947 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700948 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000949 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700950 final_type_ = Type::ERROR;
951 is_valid_ = false;
952 return false;
953 }
954
955 // Remove trailing " from lhs
956 const string& lhs = left_val_->final_string_value_;
957 if (lhs.back() != '"') {
958 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
959 final_type_ = Type::ERROR;
960 is_valid_ = false;
961 return false;
962 }
963 const string& rhs = right_val_->final_string_value_;
964 // Remove starting " from rhs
965 if (rhs.front() != '"') {
966 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
967 final_type_ = Type::ERROR;
968 is_valid_ = false;
969 return false;
970 }
971
972 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
973 final_type_ = Type::STRING;
974 return true;
975 }
976
Will McVickerd7d18df2019-09-12 13:40:50 -0700977 // CASE: + - * / % | ^ & < > <= >= == !=
978 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700979 // promoted kind for both operands.
980 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
981 IntegralPromotion(right_val_->final_type_));
982 // result kind.
983 final_type_ = isArithmeticOrBitflip
984 ? promoted // arithmetic or bitflip operators generates promoted type
985 : Type::BOOLEAN; // comparison operators generates bool
986
Devin Moore1f0360d2020-12-21 12:12:48 -0800987#define CASE_BINARY_COMMON(__type__) \
988 return is_valid_ = \
989 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
990 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700991
992 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
993 is_valid_ = false; return false;)
994 }
995
996 // CASE: << >>
997 string newOp = op_;
998 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -0700999 // promoted kind for both operands.
1000 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1001 IntegralPromotion(right_val_->final_type_));
1002 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001003 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001004 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001005 // is defined as shifting into the other direction.
1006 newOp = OPEQ("<<") ? ">>" : "<<";
1007 numBits = -numBits;
1008 }
1009
Devin Moore1f0360d2020-12-21 12:12:48 -08001010#define CASE_SHIFT(__type__) \
1011 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1012 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001013
1014 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1015 is_valid_ = false; return false;)
1016 }
1017
1018 // CASE: && ||
1019 if (OP_IS_BIN_LOGICAL) {
1020 final_type_ = Type::BOOLEAN;
1021 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001022 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1023 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001024 }
1025
1026 SHOULD_NOT_REACH();
1027 is_valid_ = false;
1028 return false;
1029}
1030
Will McVickerd7d18df2019-09-12 13:40:50 -07001031AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1032 int64_t parsed_value, const string& checked_value)
1033 : AidlNode(location),
1034 type_(parsed_type),
1035 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001036 final_type_(parsed_type),
1037 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001038 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1039 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001040}
Will McVickerefd970d2019-09-25 15:28:30 -07001041
1042AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001043 const string& checked_value)
1044 : AidlNode(location),
1045 type_(type),
1046 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001047 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001048 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001049 switch (type_) {
1050 case Type::INT8:
1051 case Type::INT32:
1052 case Type::INT64:
1053 case Type::ARRAY:
1054 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1055 break;
1056 default:
1057 break;
1058 }
1059}
1060
1061AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001062 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1063 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001064 : AidlNode(location),
1065 type_(type),
1066 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001067 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001068 is_valid_(false),
1069 is_evaluated_(false),
1070 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001071 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001072}
1073
1074AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1075 std::unique_ptr<AidlConstantValue> rval)
1076 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1077 unary_(std::move(rval)),
1078 op_(op) {
1079 final_type_ = Type::UNARY;
1080}
1081
1082AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1083 std::unique_ptr<AidlConstantValue> lval,
1084 const string& op,
1085 std::unique_ptr<AidlConstantValue> rval)
1086 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1087 left_val_(std::move(lval)),
1088 right_val_(std::move(rval)),
1089 op_(op) {
1090 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001091}