blob: 47fff32639b0e419ca2e1202032bbac9b88112ae [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
Jooyung Han535c5e82020-12-29 15:16:59 +0900245bool ParseFloating(std::string_view sv, double* parsed) {
246 // float literal should be parsed successfully.
Jooyung Han71a1b582020-12-25 23:58:41 +0900247 android::base::ConsumeSuffix(&sv, "f");
Jooyung Han535c5e82020-12-29 15:16:59 +0900248 return android::base::ParseDouble(std::string(sv).data(), parsed);
249}
250
251bool ParseFloating(std::string_view sv, float* parsed) {
252 // we only care about float literal (with suffix "f").
253 if (!android::base::ConsumeSuffix(&sv, "f")) {
254 return false;
Jooyung Han71a1b582020-12-25 23:58:41 +0900255 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900256 return android::base::ParseFloat(std::string(sv).data(), parsed);
Jooyung Han71a1b582020-12-25 23:58:41 +0900257}
258
Will McVickerd7d18df2019-09-12 13:40:50 -0700259bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
260 // Verify the unary type here
261 switch (type) {
262 case Type::BOOLEAN: // fall-through
263 case Type::INT8: // fall-through
264 case Type::INT32: // fall-through
265 case Type::INT64:
266 return true;
267 case Type::FLOATING:
268 return (op == "+" || op == "-");
269 default:
270 return false;
271 }
272}
273
274bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
275 switch (t1) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900276 case Type::ARRAY:
277 if (t2 == Type::ARRAY) {
278 return true;
279 }
280 break;
Will McVickerd7d18df2019-09-12 13:40:50 -0700281 case Type::STRING:
282 if (t2 == Type::STRING) {
283 return true;
284 }
285 break;
286 case Type::BOOLEAN: // fall-through
287 case Type::INT8: // fall-through
288 case Type::INT32: // fall-through
289 case Type::INT64:
290 switch (t2) {
291 case Type::BOOLEAN: // fall-through
292 case Type::INT8: // fall-through
293 case Type::INT32: // fall-through
294 case Type::INT64:
295 return true;
296 break;
297 default:
298 break;
299 }
300 break;
301 default:
302 break;
303 }
304
305 return false;
306}
307
308// Returns the promoted kind for both operands
309AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
310 Type right) {
311 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000312 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
313 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700314
315 // Kinds in concern: bool, (u)int[8|32|64]
316 if (left == right) return left; // easy case
317 if (left == Type::BOOLEAN) return right;
318 if (right == Type::BOOLEAN) return left;
319
320 return left < right ? right : left;
321}
322
323// Returns the promoted integral type where INT32 is the smallest type
324AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
325 return (Type::INT32 < in) ? in : Type::INT32;
326}
327
Steven Moreland541788d2020-05-21 22:05:52 +0000328AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
329 AidlLocation location = specifier.GetLocation();
330
331 // allocation of int[0] is a bit wasteful in Java
332 if (specifier.IsArray()) {
333 return nullptr;
334 }
335
336 const std::string name = specifier.GetName();
337 if (name == "boolean") {
338 return Boolean(location, false);
339 }
340 if (name == "byte" || name == "int" || name == "long") {
341 return Integral(location, "0");
342 }
343 if (name == "float") {
344 return Floating(location, "0.0f");
345 }
346 if (name == "double") {
347 return Floating(location, "0.0");
348 }
349 return nullptr;
350}
351
Will McVickerefd970d2019-09-25 15:28:30 -0700352AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
353 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
354}
355
356AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800357 const std::string explicit_value = string("'") + value + "'";
Steven Moreland8c9b7ec2022-01-26 22:50:12 +0000358 if (!isValidLiteralChar(value)) {
359 AIDL_ERROR(location) << "Invalid character literal " << value;
360 return new AidlConstantValue(location, Type::ERROR, explicit_value);
361 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800362 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700363}
364
365AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
366 const std::string& value) {
367 return new AidlConstantValue(location, Type::FLOATING, value);
368}
369
Will McVickerd7d18df2019-09-12 13:40:50 -0700370bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000371 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700372}
373
Will McVickerd7d18df2019-09-12 13:40:50 -0700374bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
375 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700376 if (parsed_value == nullptr || parsed_type == nullptr) {
377 return false;
378 }
379
Steven Morelandb7d58652021-10-25 15:10:02 -0700380 std::string_view value_view = value;
381 const bool is_byte = ConsumeSuffix(&value_view, "u8");
382 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
383 const std::string value_substr = std::string(value_view);
384
385 *parsed_value = 0;
386 *parsed_type = Type::ERROR;
387
388 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700389
Steven Morelandcef22662020-07-08 20:54:28 +0000390 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700391 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
392 // handle that when computing constant expressions, then we need to
393 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
394 // so we parse as an unsigned int when possible and then cast to a signed
395 // int. One example of this is in ICameraService.aidl where a constant int
396 // is used for bit manipulations which ideally should be handled with an
397 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000398 //
399 // Note, for historical consistency, we need to consider small hex values
400 // as an integral type. Recognizing them as INT8 could break some files,
401 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700402 if (is_byte) {
403 uint8_t raw_value8;
404 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
405 return false;
406 }
407 *parsed_value = static_cast<int8_t>(raw_value8);
408 *parsed_type = Type::INT8;
409 } else if (uint32_t raw_value32;
410 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
411 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700412 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700413 } else if (uint64_t raw_value64;
414 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
415 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700416 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000417 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000418 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700419 }
420 return true;
421 }
422
Steven Morelandcef22662020-07-08 20:54:28 +0000423 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700424 return false;
425 }
426
Steven Morelandb7d58652021-10-25 15:10:02 -0700427 if (is_byte) {
428 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
429 return false;
430 }
431 *parsed_value = static_cast<int8_t>(*parsed_value);
432 *parsed_type = Type::INT8;
433 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000434 *parsed_type = Type::INT64;
435 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700436 // guess literal type.
437 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
438 *parsed_type = Type::INT8;
439 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
440 *parsed_type = Type::INT32;
441 } else {
442 *parsed_type = Type::INT64;
443 }
444 }
445 return true;
446}
447
448AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000449 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700450
451 Type parsed_type;
452 int64_t parsed_value = 0;
453 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
454 if (!success) {
455 return nullptr;
456 }
457
458 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700459}
460
461AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700462 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000463 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900464 // Reconstruct literal value
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 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900469 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
470 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700471}
472
Will McVickerd7d18df2019-09-12 13:40:50 -0700473AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700474 return new AidlConstantValue(location, Type::STRING, value);
475}
476
Will McVickerd7d18df2019-09-12 13:40:50 -0700477string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
478 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700479 if (type.IsGeneric()) {
480 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
481 return "";
482 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700483 if (!is_evaluated_) {
484 // TODO(b/142722772) CheckValid() should be called before ValueString()
485 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900486 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700487 if (!success) {
488 // the detailed error message shall be printed in evaluate
489 return "";
490 }
Will McVickerefd970d2019-09-25 15:28:30 -0700491 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700492 if (!is_valid_) {
493 AIDL_ERROR(this) << "Invalid constant value: " + value_;
494 return "";
495 }
Jooyung Han690f5842020-12-04 13:02:04 +0900496
497 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900498 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900499 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
500 if (!enum_type) {
501 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900502 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900503 return "";
504 }
505 if (type_ != Type::REF) {
506 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
507 << enum_type->GetCanonicalName();
508 return "";
509 }
510 return decorator(type, value_);
511 }
512
Jooyung Hanaeb01672021-11-30 17:29:22 +0900513 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700514 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700515
Will McVickerd7d18df2019-09-12 13:40:50 -0700516 switch (final_type_) {
517 case Type::CHARACTER:
518 if (type_string == "char") {
519 return decorator(type, final_string_value_);
520 }
521 err = -1;
522 break;
523 case Type::STRING:
524 if (type_string == "String") {
525 return decorator(type, final_string_value_);
526 }
527 err = -1;
528 break;
529 case Type::BOOLEAN: // fall-through
530 case Type::INT8: // fall-through
531 case Type::INT32: // fall-through
532 case Type::INT64:
533 if (type_string == "byte") {
534 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
535 err = -1;
536 break;
537 }
538 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
539 } else if (type_string == "int") {
540 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
541 err = -1;
542 break;
543 }
544 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
545 } else if (type_string == "long") {
546 return decorator(type, std::to_string(final_value_));
547 } else if (type_string == "boolean") {
548 return decorator(type, final_value_ ? "true" : "false");
549 }
550 err = -1;
551 break;
552 case Type::ARRAY: {
553 if (!type.IsArray()) {
554 err = -1;
555 break;
556 }
557 vector<string> value_strings;
558 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700559 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700560
Will McVickerefd970d2019-09-25 15:28:30 -0700561 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900562 string value_string;
563 type.ViewAsArrayBase([&](const auto& base_type) {
564 value_string = value->ValueString(base_type, decorator);
565 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700566 if (value_string.empty()) {
567 success = false;
568 break;
569 }
570 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700571 }
572 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700573 err = -1;
574 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700575 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900576 if (type.IsFixedSizeArray()) {
577 auto size =
578 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
Jooyung Hane76bcc22022-01-23 22:49:45 +0900579 if (values_.size() != static_cast<size_t>(size)) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900580 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
581 << values_.size() << " elements";
582 err = -1;
583 break;
584 }
585 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900586 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700587 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700588 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700589 if (type_string == "double") {
590 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900591 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700592 AIDL_ERROR(this) << "Could not parse " << value_;
593 err = -1;
594 break;
595 }
Will McVickerefd970d2019-09-25 15:28:30 -0700596 return decorator(type, std::to_string(parsed_value));
597 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900598 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700599 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900600 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700601 AIDL_ERROR(this) << "Could not parse " << value_;
602 err = -1;
603 break;
604 }
Will McVickerefd970d2019-09-25 15:28:30 -0700605 return decorator(type, std::to_string(parsed_value) + "f");
606 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700607 err = -1;
608 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700609 }
Will McVickerefd970d2019-09-25 15:28:30 -0700610 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700611 err = -1;
612 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700613 }
614
Steven Moreland21780812020-09-11 01:29:45 +0000615 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700616 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
617 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700618 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700619}
620
621bool AidlConstantValue::CheckValid() const {
622 // Nothing needs to be checked here. The constant value will be validated in
623 // the constructor or in the evaluate() function.
624 if (is_evaluated_) return is_valid_;
625
626 switch (type_) {
627 case Type::BOOLEAN: // fall-through
628 case Type::INT8: // fall-through
629 case Type::INT32: // fall-through
630 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700631 case Type::CHARACTER: // fall-through
632 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900633 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700634 case Type::FLOATING: // fall-through
635 case Type::UNARY: // fall-through
636 case Type::BINARY:
637 is_valid_ = true;
638 break;
Jooyung Han29813842020-12-08 01:28:03 +0900639 case Type::ARRAY:
640 is_valid_ = true;
641 for (const auto& v : values_) is_valid_ &= v->CheckValid();
642 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800643 case Type::ERROR:
644 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700645 default:
646 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
647 return false;
648 }
649
650 return true;
651}
652
Jooyung Han74675c22020-12-15 08:39:57 +0900653bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700654 if (is_evaluated_) {
655 return is_valid_;
656 }
657 int err = 0;
658 is_evaluated_ = true;
659
660 switch (type_) {
661 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700662 Type array_type = Type::ERROR;
663 bool success = true;
664 for (const auto& value : values_) {
665 success = value->CheckValid();
666 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900667 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700668 if (!success) {
669 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
670 break;
671 }
672 if (array_type == Type::ERROR) {
673 array_type = value->final_type_;
674 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
675 value->final_type_)) {
676 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
677 << ". Expecting type compatible with " << ToString(array_type);
678 success = false;
679 break;
680 }
681 } else {
682 break;
683 }
684 }
685 if (!success) {
686 err = -1;
687 break;
688 }
689 final_type_ = type_;
690 break;
691 }
692 case Type::BOOLEAN:
693 if ((value_ != "true") && (value_ != "false")) {
694 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
695 err = -1;
696 break;
697 }
698 final_value_ = (value_ == "true") ? 1 : 0;
699 final_type_ = type_;
700 break;
701 case Type::INT8: // fall-through
702 case Type::INT32: // fall-through
703 case Type::INT64:
704 // Parsing happens in the constructor
705 final_type_ = type_;
706 break;
707 case Type::CHARACTER: // fall-through
708 case Type::STRING:
709 final_string_value_ = value_;
710 final_type_ = type_;
711 break;
712 case Type::FLOATING:
713 // Just parse on the fly in ValueString
714 final_type_ = type_;
715 break;
716 default:
717 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
718 err = -1;
719 }
720
721 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700722}
723
724string AidlConstantValue::ToString(Type type) {
725 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700726 case Type::BOOLEAN:
727 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700728 case Type::INT8:
729 return "an int8 literal";
730 case Type::INT32:
731 return "an int32 literal";
732 case Type::INT64:
733 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800734 case Type::ARRAY:
735 return "a literal array";
736 case Type::CHARACTER:
737 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700738 case Type::STRING:
739 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900740 case Type::REF:
741 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800742 case Type::FLOATING:
743 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700744 case Type::UNARY:
745 return "a unary expression";
746 case Type::BINARY:
747 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800748 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000749 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800750 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700751 default:
Steven Moreland21780812020-09-11 01:29:45 +0000752 AIDL_FATAL(AIDL_LOCATION_HERE)
753 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700754 return ""; // not reached
755 }
756}
757
Jooyung Hand0c8af02021-01-06 18:08:01 +0900758AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
759 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900760 const auto pos = value.find_last_of('.');
761 if (pos == string::npos) {
762 field_name_ = value;
763 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900764 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
765 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900766 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900767 field_name_ = value.substr(pos + 1);
768 }
769}
770
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900771const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900772 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900773
774 const AidlDefinedType* defined_type;
775 if (ref_type_) {
776 defined_type = ref_type_->GetDefinedType();
777 } else {
778 defined_type = scope;
779 }
780
781 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900782 // This can happen when "const reference" is used in an unsupported way,
783 // but missed in checks there. It works as a safety net.
784 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900785 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900786 }
787
Jooyung Han690f5842020-12-04 13:02:04 +0900788 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
789 for (const auto& e : enum_decl->GetEnumerators()) {
790 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900791 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900792 }
793 }
794 } else {
795 for (const auto& c : defined_type->GetConstantDeclarations()) {
796 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900797 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900798 }
799 }
800 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900801 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900802 return nullptr;
803}
804
805bool AidlConstantReference::CheckValid() const {
806 if (is_evaluated_) return is_valid_;
807 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
808 is_valid_ = resolved_->CheckValid();
809 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900810}
811
Jooyung Han74675c22020-12-15 08:39:57 +0900812bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900813 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900814 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
815 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900816
Jooyung Han74675c22020-12-15 08:39:57 +0900817 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900818 is_valid_ = resolved_->is_valid_;
819 final_type_ = resolved_->final_type_;
820 if (is_valid_) {
821 if (final_type_ == Type::STRING) {
822 final_string_value_ = resolved_->final_string_value_;
823 } else {
824 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900825 }
826 }
Jooyung Han29813842020-12-08 01:28:03 +0900827 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900828}
829
Will McVickerd7d18df2019-09-12 13:40:50 -0700830bool AidlUnaryConstExpression::CheckValid() const {
831 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000832 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700833
834 is_valid_ = unary_->CheckValid();
835 if (!is_valid_) {
836 final_type_ = Type::ERROR;
837 return false;
838 }
839
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800840 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700841}
842
Jooyung Han74675c22020-12-15 08:39:57 +0900843bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700844 if (is_evaluated_) {
845 return is_valid_;
846 }
847 is_evaluated_ = true;
848
849 // Recursively evaluate the expression tree
850 if (!unary_->is_evaluated_) {
851 // TODO(b/142722772) CheckValid() should be called before ValueString()
852 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900853 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700854 if (!success) {
855 is_valid_ = false;
856 return false;
857 }
858 }
Devin Moorec233fb82020-04-07 11:13:44 -0700859 if (!IsCompatibleType(unary_->final_type_, op_)) {
860 AIDL_ERROR(unary_) << "'" << op_ << "'"
861 << " is not compatible with " << ToString(unary_->final_type_)
862 << ": " + value_;
863 is_valid_ = false;
864 return false;
865 }
866 if (!unary_->is_valid_) {
867 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700868 is_valid_ = false;
869 return false;
870 }
871 final_type_ = unary_->final_type_;
872
873 if (final_type_ == Type::FLOATING) {
874 // don't do anything here. ValueString() will handle everything.
875 is_valid_ = true;
876 return true;
877 }
878
Steven Morelande1ff67e2020-07-16 23:22:36 +0000879#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800880 return is_valid_ = \
881 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700882
883 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
884 is_valid_ = false; return false;)
885}
886
Will McVickerd7d18df2019-09-12 13:40:50 -0700887bool AidlBinaryConstExpression::CheckValid() const {
888 bool success = false;
889 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000890 AIDL_FATAL_IF(left_val_ == nullptr, this);
891 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700892
893 success = left_val_->CheckValid();
894 if (!success) {
895 final_type_ = Type::ERROR;
896 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
897 }
898
899 success = right_val_->CheckValid();
900 if (!success) {
901 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
902 final_type_ = Type::ERROR;
903 }
904
905 if (final_type_ == Type::ERROR) {
906 is_valid_ = false;
907 return false;
908 }
909
910 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800911 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700912}
913
Jooyung Han74675c22020-12-15 08:39:57 +0900914bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700915 if (is_evaluated_) {
916 return is_valid_;
917 }
918 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900919 AIDL_FATAL_IF(left_val_ == nullptr, this);
920 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700921
922 // Recursively evaluate the binary expression tree
923 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
924 // TODO(b/142722772) CheckValid() should be called before ValueString()
925 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900926 success &= left_val_->evaluate();
927 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700928 if (!success) {
929 is_valid_ = false;
930 return false;
931 }
932 }
933 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
934 is_valid_ = false;
935 return false;
936 }
937 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
938 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000939 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
940 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
941 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700942 return false;
943 }
944
945 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
946
947 // Handle String case first
948 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000949 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700950 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000951 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700952 final_type_ = Type::ERROR;
953 is_valid_ = false;
954 return false;
955 }
956
957 // Remove trailing " from lhs
958 const string& lhs = left_val_->final_string_value_;
959 if (lhs.back() != '"') {
960 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
961 final_type_ = Type::ERROR;
962 is_valid_ = false;
963 return false;
964 }
965 const string& rhs = right_val_->final_string_value_;
966 // Remove starting " from rhs
967 if (rhs.front() != '"') {
968 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
969 final_type_ = Type::ERROR;
970 is_valid_ = false;
971 return false;
972 }
973
974 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
975 final_type_ = Type::STRING;
976 return true;
977 }
978
Will McVickerd7d18df2019-09-12 13:40:50 -0700979 // CASE: + - * / % | ^ & < > <= >= == !=
980 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700981 // promoted kind for both operands.
982 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
983 IntegralPromotion(right_val_->final_type_));
984 // result kind.
985 final_type_ = isArithmeticOrBitflip
986 ? promoted // arithmetic or bitflip operators generates promoted type
987 : Type::BOOLEAN; // comparison operators generates bool
988
Devin Moore1f0360d2020-12-21 12:12:48 -0800989#define CASE_BINARY_COMMON(__type__) \
990 return is_valid_ = \
991 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
992 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700993
994 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
995 is_valid_ = false; return false;)
996 }
997
998 // CASE: << >>
999 string newOp = op_;
1000 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001001 // promoted kind for both operands.
1002 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1003 IntegralPromotion(right_val_->final_type_));
1004 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001005 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001006 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001007 // is defined as shifting into the other direction.
1008 newOp = OPEQ("<<") ? ">>" : "<<";
1009 numBits = -numBits;
1010 }
1011
Devin Moore1f0360d2020-12-21 12:12:48 -08001012#define CASE_SHIFT(__type__) \
1013 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1014 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001015
1016 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1017 is_valid_ = false; return false;)
1018 }
1019
1020 // CASE: && ||
1021 if (OP_IS_BIN_LOGICAL) {
1022 final_type_ = Type::BOOLEAN;
1023 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001024 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1025 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001026 }
1027
1028 SHOULD_NOT_REACH();
1029 is_valid_ = false;
1030 return false;
1031}
1032
Will McVickerd7d18df2019-09-12 13:40:50 -07001033AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1034 int64_t parsed_value, const string& checked_value)
1035 : AidlNode(location),
1036 type_(parsed_type),
1037 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001038 final_type_(parsed_type),
1039 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001040 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1041 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001042}
Will McVickerefd970d2019-09-25 15:28:30 -07001043
1044AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001045 const string& checked_value)
1046 : AidlNode(location),
1047 type_(type),
1048 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001049 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001050 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001051 switch (type_) {
1052 case Type::INT8:
1053 case Type::INT32:
1054 case Type::INT64:
1055 case Type::ARRAY:
1056 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1057 break;
1058 default:
1059 break;
1060 }
1061}
1062
1063AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001064 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1065 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001066 : AidlNode(location),
1067 type_(type),
1068 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001069 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001070 is_valid_(false),
1071 is_evaluated_(false),
1072 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001073 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001074}
1075
1076AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1077 std::unique_ptr<AidlConstantValue> rval)
1078 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1079 unary_(std::move(rval)),
1080 op_(op) {
1081 final_type_ = Type::UNARY;
1082}
1083
1084AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1085 std::unique_ptr<AidlConstantValue> lval,
1086 const string& op,
1087 std::unique_ptr<AidlConstantValue> rval)
1088 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1089 left_val_(std::move(lval)),
1090 right_val_(std::move(rval)),
1091 op_(op) {
1092 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001093}