blob: 6c4b177d9e0525768a11e9e0f81c408631ecf260 [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 Hanaeb01672021-11-30 17:29:22 +0900465 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900466 std::vector<std::string> str_values;
467 for (const auto& v : *values) {
468 str_values.push_back(v->value_);
469 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900470 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
471 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700472}
473
Will McVickerd7d18df2019-09-12 13:40:50 -0700474AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700475 for (size_t i = 0; i < value.length(); ++i) {
476 if (!isValidLiteralChar(value[i])) {
477 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
478 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800479 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700480 }
481 }
482
483 return new AidlConstantValue(location, Type::STRING, value);
484}
485
Will McVickerd7d18df2019-09-12 13:40:50 -0700486string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
487 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700488 if (type.IsGeneric()) {
489 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
490 return "";
491 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700492 if (!is_evaluated_) {
493 // TODO(b/142722772) CheckValid() should be called before ValueString()
494 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900495 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700496 if (!success) {
497 // the detailed error message shall be printed in evaluate
498 return "";
499 }
Will McVickerefd970d2019-09-25 15:28:30 -0700500 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700501 if (!is_valid_) {
502 AIDL_ERROR(this) << "Invalid constant value: " + value_;
503 return "";
504 }
Jooyung Han690f5842020-12-04 13:02:04 +0900505
506 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900507 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900508 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
509 if (!enum_type) {
510 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900511 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900512 return "";
513 }
514 if (type_ != Type::REF) {
515 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
516 << enum_type->GetCanonicalName();
517 return "";
518 }
519 return decorator(type, value_);
520 }
521
Jooyung Hanaeb01672021-11-30 17:29:22 +0900522 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700523 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700524
Will McVickerd7d18df2019-09-12 13:40:50 -0700525 switch (final_type_) {
526 case Type::CHARACTER:
527 if (type_string == "char") {
528 return decorator(type, final_string_value_);
529 }
530 err = -1;
531 break;
532 case Type::STRING:
533 if (type_string == "String") {
534 return decorator(type, final_string_value_);
535 }
536 err = -1;
537 break;
538 case Type::BOOLEAN: // fall-through
539 case Type::INT8: // fall-through
540 case Type::INT32: // fall-through
541 case Type::INT64:
542 if (type_string == "byte") {
543 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
544 err = -1;
545 break;
546 }
547 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
548 } else if (type_string == "int") {
549 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
550 err = -1;
551 break;
552 }
553 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
554 } else if (type_string == "long") {
555 return decorator(type, std::to_string(final_value_));
556 } else if (type_string == "boolean") {
557 return decorator(type, final_value_ ? "true" : "false");
558 }
559 err = -1;
560 break;
561 case Type::ARRAY: {
562 if (!type.IsArray()) {
563 err = -1;
564 break;
565 }
566 vector<string> value_strings;
567 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700568 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700569
Will McVickerefd970d2019-09-25 15:28:30 -0700570 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900571 string value_string;
572 type.ViewAsArrayBase([&](const auto& base_type) {
573 value_string = value->ValueString(base_type, decorator);
574 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700575 if (value_string.empty()) {
576 success = false;
577 break;
578 }
579 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700580 }
581 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700582 err = -1;
583 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700584 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900585 return decorator(type, 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 Han9fafb8d2021-11-30 13:19:33 +0900763 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
764 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900765 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900766 field_name_ = value.substr(pos + 1);
767 }
768}
769
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900770const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900771 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900772
773 const AidlDefinedType* defined_type;
774 if (ref_type_) {
775 defined_type = ref_type_->GetDefinedType();
776 } else {
777 defined_type = scope;
778 }
779
780 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900781 // This can happen when "const reference" is used in an unsupported way,
782 // but missed in checks there. It works as a safety net.
783 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900784 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900785 }
786
Jooyung Han690f5842020-12-04 13:02:04 +0900787 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
788 for (const auto& e : enum_decl->GetEnumerators()) {
789 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900790 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900791 }
792 }
793 } else {
794 for (const auto& c : defined_type->GetConstantDeclarations()) {
795 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900796 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900797 }
798 }
799 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900800 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900801 return nullptr;
802}
803
804bool AidlConstantReference::CheckValid() const {
805 if (is_evaluated_) return is_valid_;
806 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
807 is_valid_ = resolved_->CheckValid();
808 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900809}
810
Jooyung Han74675c22020-12-15 08:39:57 +0900811bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900812 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900813 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
814 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900815
Jooyung Han74675c22020-12-15 08:39:57 +0900816 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900817 is_valid_ = resolved_->is_valid_;
818 final_type_ = resolved_->final_type_;
819 if (is_valid_) {
820 if (final_type_ == Type::STRING) {
821 final_string_value_ = resolved_->final_string_value_;
822 } else {
823 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900824 }
825 }
Jooyung Han29813842020-12-08 01:28:03 +0900826 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900827}
828
Will McVickerd7d18df2019-09-12 13:40:50 -0700829bool AidlUnaryConstExpression::CheckValid() const {
830 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000831 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700832
833 is_valid_ = unary_->CheckValid();
834 if (!is_valid_) {
835 final_type_ = Type::ERROR;
836 return false;
837 }
838
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800839 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700840}
841
Jooyung Han74675c22020-12-15 08:39:57 +0900842bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700843 if (is_evaluated_) {
844 return is_valid_;
845 }
846 is_evaluated_ = true;
847
848 // Recursively evaluate the expression tree
849 if (!unary_->is_evaluated_) {
850 // TODO(b/142722772) CheckValid() should be called before ValueString()
851 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900852 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700853 if (!success) {
854 is_valid_ = false;
855 return false;
856 }
857 }
Devin Moorec233fb82020-04-07 11:13:44 -0700858 if (!IsCompatibleType(unary_->final_type_, op_)) {
859 AIDL_ERROR(unary_) << "'" << op_ << "'"
860 << " is not compatible with " << ToString(unary_->final_type_)
861 << ": " + value_;
862 is_valid_ = false;
863 return false;
864 }
865 if (!unary_->is_valid_) {
866 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700867 is_valid_ = false;
868 return false;
869 }
870 final_type_ = unary_->final_type_;
871
872 if (final_type_ == Type::FLOATING) {
873 // don't do anything here. ValueString() will handle everything.
874 is_valid_ = true;
875 return true;
876 }
877
Steven Morelande1ff67e2020-07-16 23:22:36 +0000878#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800879 return is_valid_ = \
880 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700881
882 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
883 is_valid_ = false; return false;)
884}
885
Will McVickerd7d18df2019-09-12 13:40:50 -0700886bool AidlBinaryConstExpression::CheckValid() const {
887 bool success = false;
888 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000889 AIDL_FATAL_IF(left_val_ == nullptr, this);
890 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700891
892 success = left_val_->CheckValid();
893 if (!success) {
894 final_type_ = Type::ERROR;
895 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
896 }
897
898 success = right_val_->CheckValid();
899 if (!success) {
900 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
901 final_type_ = Type::ERROR;
902 }
903
904 if (final_type_ == Type::ERROR) {
905 is_valid_ = false;
906 return false;
907 }
908
909 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800910 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700911}
912
Jooyung Han74675c22020-12-15 08:39:57 +0900913bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700914 if (is_evaluated_) {
915 return is_valid_;
916 }
917 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900918 AIDL_FATAL_IF(left_val_ == nullptr, this);
919 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700920
921 // Recursively evaluate the binary expression tree
922 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
923 // TODO(b/142722772) CheckValid() should be called before ValueString()
924 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900925 success &= left_val_->evaluate();
926 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700927 if (!success) {
928 is_valid_ = false;
929 return false;
930 }
931 }
932 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
933 is_valid_ = false;
934 return false;
935 }
936 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
937 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000938 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
939 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
940 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700941 return false;
942 }
943
944 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
945
946 // Handle String case first
947 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000948 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700949 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000950 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700951 final_type_ = Type::ERROR;
952 is_valid_ = false;
953 return false;
954 }
955
956 // Remove trailing " from lhs
957 const string& lhs = left_val_->final_string_value_;
958 if (lhs.back() != '"') {
959 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
960 final_type_ = Type::ERROR;
961 is_valid_ = false;
962 return false;
963 }
964 const string& rhs = right_val_->final_string_value_;
965 // Remove starting " from rhs
966 if (rhs.front() != '"') {
967 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
968 final_type_ = Type::ERROR;
969 is_valid_ = false;
970 return false;
971 }
972
973 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
974 final_type_ = Type::STRING;
975 return true;
976 }
977
Will McVickerd7d18df2019-09-12 13:40:50 -0700978 // CASE: + - * / % | ^ & < > <= >= == !=
979 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700980 // promoted kind for both operands.
981 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
982 IntegralPromotion(right_val_->final_type_));
983 // result kind.
984 final_type_ = isArithmeticOrBitflip
985 ? promoted // arithmetic or bitflip operators generates promoted type
986 : Type::BOOLEAN; // comparison operators generates bool
987
Devin Moore1f0360d2020-12-21 12:12:48 -0800988#define CASE_BINARY_COMMON(__type__) \
989 return is_valid_ = \
990 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
991 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700992
993 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
994 is_valid_ = false; return false;)
995 }
996
997 // CASE: << >>
998 string newOp = op_;
999 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001000 // promoted kind for both operands.
1001 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1002 IntegralPromotion(right_val_->final_type_));
1003 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001004 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001005 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001006 // is defined as shifting into the other direction.
1007 newOp = OPEQ("<<") ? ">>" : "<<";
1008 numBits = -numBits;
1009 }
1010
Devin Moore1f0360d2020-12-21 12:12:48 -08001011#define CASE_SHIFT(__type__) \
1012 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1013 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001014
1015 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1016 is_valid_ = false; return false;)
1017 }
1018
1019 // CASE: && ||
1020 if (OP_IS_BIN_LOGICAL) {
1021 final_type_ = Type::BOOLEAN;
1022 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001023 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1024 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001025 }
1026
1027 SHOULD_NOT_REACH();
1028 is_valid_ = false;
1029 return false;
1030}
1031
Will McVickerd7d18df2019-09-12 13:40:50 -07001032AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1033 int64_t parsed_value, const string& checked_value)
1034 : AidlNode(location),
1035 type_(parsed_type),
1036 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001037 final_type_(parsed_type),
1038 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001039 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1040 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001041}
Will McVickerefd970d2019-09-25 15:28:30 -07001042
1043AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001044 const string& checked_value)
1045 : AidlNode(location),
1046 type_(type),
1047 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001048 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001049 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001050 switch (type_) {
1051 case Type::INT8:
1052 case Type::INT32:
1053 case Type::INT64:
1054 case Type::ARRAY:
1055 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1056 break;
1057 default:
1058 break;
1059 }
1060}
1061
1062AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001063 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1064 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001065 : AidlNode(location),
1066 type_(type),
1067 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001068 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001069 is_valid_(false),
1070 is_evaluated_(false),
1071 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001072 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001073}
1074
1075AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1076 std::unique_ptr<AidlConstantValue> rval)
1077 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1078 unary_(std::move(rval)),
1079 op_(op) {
1080 final_type_ = Type::UNARY;
1081}
1082
1083AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1084 std::unique_ptr<AidlConstantValue> lval,
1085 const string& op,
1086 std::unique_ptr<AidlConstantValue> rval)
1087 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1088 left_val_(std::move(lval)),
1089 right_val_(std::move(rval)),
1090 op_(op) {
1091 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001092}