blob: eeeb14b399ae0ee06d3ee459e681584ef4748fe2 [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
Steven Morelande9f8aad2022-01-31 19:27:21 +0000245static bool isValidLiteralChar(char c) {
246 return !(c <= 0x1f || // control characters are < 0x20
247 c >= 0x7f || // DEL is 0x7f
248 c == '\\'); // Disallow backslashes for future proofing.
249}
250
Jooyung Han535c5e82020-12-29 15:16:59 +0900251bool ParseFloating(std::string_view sv, double* parsed) {
252 // float literal should be parsed successfully.
Jooyung Han71a1b582020-12-25 23:58:41 +0900253 android::base::ConsumeSuffix(&sv, "f");
Jooyung Han535c5e82020-12-29 15:16:59 +0900254 return android::base::ParseDouble(std::string(sv).data(), parsed);
255}
256
257bool ParseFloating(std::string_view sv, float* parsed) {
258 // we only care about float literal (with suffix "f").
259 if (!android::base::ConsumeSuffix(&sv, "f")) {
260 return false;
Jooyung Han71a1b582020-12-25 23:58:41 +0900261 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900262 return android::base::ParseFloat(std::string(sv).data(), parsed);
Jooyung Han71a1b582020-12-25 23:58:41 +0900263}
264
Will McVickerd7d18df2019-09-12 13:40:50 -0700265bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
266 // Verify the unary type here
267 switch (type) {
268 case Type::BOOLEAN: // fall-through
269 case Type::INT8: // fall-through
270 case Type::INT32: // fall-through
271 case Type::INT64:
272 return true;
273 case Type::FLOATING:
274 return (op == "+" || op == "-");
275 default:
276 return false;
277 }
278}
279
280bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
281 switch (t1) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900282 case Type::ARRAY:
283 if (t2 == Type::ARRAY) {
284 return true;
285 }
286 break;
Will McVickerd7d18df2019-09-12 13:40:50 -0700287 case Type::STRING:
288 if (t2 == Type::STRING) {
289 return true;
290 }
291 break;
292 case Type::BOOLEAN: // fall-through
293 case Type::INT8: // fall-through
294 case Type::INT32: // fall-through
295 case Type::INT64:
296 switch (t2) {
297 case Type::BOOLEAN: // fall-through
298 case Type::INT8: // fall-through
299 case Type::INT32: // fall-through
300 case Type::INT64:
301 return true;
302 break;
303 default:
304 break;
305 }
306 break;
307 default:
308 break;
309 }
310
311 return false;
312}
313
314// Returns the promoted kind for both operands
315AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
316 Type right) {
317 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000318 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
319 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700320
321 // Kinds in concern: bool, (u)int[8|32|64]
322 if (left == right) return left; // easy case
323 if (left == Type::BOOLEAN) return right;
324 if (right == Type::BOOLEAN) return left;
325
326 return left < right ? right : left;
327}
328
329// Returns the promoted integral type where INT32 is the smallest type
330AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
331 return (Type::INT32 < in) ? in : Type::INT32;
332}
333
Steven Moreland541788d2020-05-21 22:05:52 +0000334AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
335 AidlLocation location = specifier.GetLocation();
336
337 // allocation of int[0] is a bit wasteful in Java
338 if (specifier.IsArray()) {
339 return nullptr;
340 }
341
342 const std::string name = specifier.GetName();
343 if (name == "boolean") {
344 return Boolean(location, false);
345 }
Jooyung Handd0d78f2021-12-06 10:24:28 +0900346 if (name == "char") {
347 return Character(location, "'\\0'"); // literal to be used in backends
348 }
Steven Moreland541788d2020-05-21 22:05:52 +0000349 if (name == "byte" || name == "int" || name == "long") {
350 return Integral(location, "0");
351 }
352 if (name == "float") {
353 return Floating(location, "0.0f");
354 }
355 if (name == "double") {
356 return Floating(location, "0.0");
357 }
358 return nullptr;
359}
360
Will McVickerefd970d2019-09-25 15:28:30 -0700361AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
362 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
363}
364
Jooyung Handd0d78f2021-12-06 10:24:28 +0900365AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location,
366 const std::string& value) {
Xin Li169d4ba2022-02-07 23:07:39 -0800367 static const char* kZeroString = "'\\0'";
368
369 // We should have better supports for escapes in the future, but for now
370 // allow only what is needed for defaults.
371 if (value != kZeroString) {
372 AIDL_FATAL_IF(value.size() != 3 || value[0] != '\'' || value[2] != '\'', location) << value;
373
374 if (!isValidLiteralChar(value[1])) {
375 AIDL_ERROR(location) << "Invalid character literal " << value[1];
376 return new AidlConstantValue(location, Type::ERROR, value);
377 }
Steven Moreland8c9b7ec2022-01-26 22:50:12 +0000378 }
Xin Li169d4ba2022-02-07 23:07:39 -0800379
Jooyung Handd0d78f2021-12-06 10:24:28 +0900380 return new AidlConstantValue(location, Type::CHARACTER, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700381}
382
383AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
384 const std::string& value) {
385 return new AidlConstantValue(location, Type::FLOATING, value);
386}
387
Will McVickerd7d18df2019-09-12 13:40:50 -0700388bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000389 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700390}
391
Will McVickerd7d18df2019-09-12 13:40:50 -0700392bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
393 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700394 if (parsed_value == nullptr || parsed_type == nullptr) {
395 return false;
396 }
397
Steven Morelandb7d58652021-10-25 15:10:02 -0700398 std::string_view value_view = value;
399 const bool is_byte = ConsumeSuffix(&value_view, "u8");
400 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
401 const std::string value_substr = std::string(value_view);
402
403 *parsed_value = 0;
404 *parsed_type = Type::ERROR;
405
406 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700407
Steven Morelandcef22662020-07-08 20:54:28 +0000408 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700409 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
410 // handle that when computing constant expressions, then we need to
411 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
412 // so we parse as an unsigned int when possible and then cast to a signed
413 // int. One example of this is in ICameraService.aidl where a constant int
414 // is used for bit manipulations which ideally should be handled with an
415 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000416 //
417 // Note, for historical consistency, we need to consider small hex values
418 // as an integral type. Recognizing them as INT8 could break some files,
419 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700420 if (is_byte) {
421 uint8_t raw_value8;
422 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
423 return false;
424 }
425 *parsed_value = static_cast<int8_t>(raw_value8);
426 *parsed_type = Type::INT8;
427 } else if (uint32_t raw_value32;
428 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
429 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700430 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700431 } else if (uint64_t raw_value64;
432 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
433 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700434 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000435 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000436 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700437 }
438 return true;
439 }
440
Steven Morelandcef22662020-07-08 20:54:28 +0000441 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 return false;
443 }
444
Steven Morelandb7d58652021-10-25 15:10:02 -0700445 if (is_byte) {
446 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
447 return false;
448 }
449 *parsed_value = static_cast<int8_t>(*parsed_value);
450 *parsed_type = Type::INT8;
451 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000452 *parsed_type = Type::INT64;
453 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 // guess literal type.
455 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
456 *parsed_type = Type::INT8;
457 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
458 *parsed_type = Type::INT32;
459 } else {
460 *parsed_type = Type::INT64;
461 }
462 }
463 return true;
464}
465
466AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000467 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700468
469 Type parsed_type;
470 int64_t parsed_value = 0;
471 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
472 if (!success) {
473 return nullptr;
474 }
475
476 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700477}
478
479AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700480 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000481 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900482 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900483 std::vector<std::string> str_values;
484 for (const auto& v : *values) {
485 str_values.push_back(v->value_);
486 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900487 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
488 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700489}
490
Will McVickerd7d18df2019-09-12 13:40:50 -0700491AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Steven Moreland797b06b2022-01-26 02:27:23 +0000492 AIDL_FATAL_IF(value.size() == 0, "If this is unquoted, we need to update the index log");
493 AIDL_FATAL_IF(value[0] != '\"', "If this is unquoted, we need to update the index log");
494
Steven Morelande9f8aad2022-01-31 19:27:21 +0000495 for (size_t i = 0; i < value.length(); ++i) {
496 if (!isValidLiteralChar(value[i])) {
Steven Moreland797b06b2022-01-26 02:27:23 +0000497 AIDL_ERROR(location) << "Found invalid character '" << value[i] << "' at index " << i - 1
498 << " in string constant '" << value << "'";
Steven Morelande9f8aad2022-01-31 19:27:21 +0000499 return new AidlConstantValue(location, Type::ERROR, value);
500 }
501 }
502
Will McVickerefd970d2019-09-25 15:28:30 -0700503 return new AidlConstantValue(location, Type::STRING, value);
504}
505
Will McVickerd7d18df2019-09-12 13:40:50 -0700506string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
507 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700508 if (type.IsGeneric()) {
509 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
510 return "";
511 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700512 if (!is_evaluated_) {
513 // TODO(b/142722772) CheckValid() should be called before ValueString()
514 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900515 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700516 if (!success) {
517 // the detailed error message shall be printed in evaluate
518 return "";
519 }
Will McVickerefd970d2019-09-25 15:28:30 -0700520 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700521 if (!is_valid_) {
522 AIDL_ERROR(this) << "Invalid constant value: " + value_;
523 return "";
524 }
Jooyung Han690f5842020-12-04 13:02:04 +0900525
526 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900527 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900528 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
529 if (!enum_type) {
530 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900531 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900532 return "";
533 }
534 if (type_ != Type::REF) {
535 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
536 << enum_type->GetCanonicalName();
537 return "";
538 }
539 return decorator(type, value_);
540 }
541
Jooyung Hanaeb01672021-11-30 17:29:22 +0900542 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700543 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700544
Will McVickerd7d18df2019-09-12 13:40:50 -0700545 switch (final_type_) {
546 case Type::CHARACTER:
547 if (type_string == "char") {
548 return decorator(type, final_string_value_);
549 }
550 err = -1;
551 break;
552 case Type::STRING:
553 if (type_string == "String") {
554 return decorator(type, final_string_value_);
555 }
556 err = -1;
557 break;
558 case Type::BOOLEAN: // fall-through
559 case Type::INT8: // fall-through
560 case Type::INT32: // fall-through
561 case Type::INT64:
562 if (type_string == "byte") {
563 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
564 err = -1;
565 break;
566 }
567 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
568 } else if (type_string == "int") {
569 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
570 err = -1;
571 break;
572 }
573 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
574 } else if (type_string == "long") {
575 return decorator(type, std::to_string(final_value_));
576 } else if (type_string == "boolean") {
577 return decorator(type, final_value_ ? "true" : "false");
578 }
579 err = -1;
580 break;
581 case Type::ARRAY: {
582 if (!type.IsArray()) {
583 err = -1;
584 break;
585 }
586 vector<string> value_strings;
587 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700588 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700589
Will McVickerefd970d2019-09-25 15:28:30 -0700590 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900591 string value_string;
592 type.ViewAsArrayBase([&](const auto& base_type) {
593 value_string = value->ValueString(base_type, decorator);
594 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700595 if (value_string.empty()) {
596 success = false;
597 break;
598 }
599 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700600 }
601 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700602 err = -1;
603 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700604 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900605 if (type.IsFixedSizeArray()) {
606 auto size =
607 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
Jooyung Hane76bcc22022-01-23 22:49:45 +0900608 if (values_.size() != static_cast<size_t>(size)) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900609 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
610 << values_.size() << " elements";
611 err = -1;
612 break;
613 }
614 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900615 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700616 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700617 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700618 if (type_string == "double") {
619 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900620 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700621 AIDL_ERROR(this) << "Could not parse " << value_;
622 err = -1;
623 break;
624 }
Will McVickerefd970d2019-09-25 15:28:30 -0700625 return decorator(type, std::to_string(parsed_value));
626 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900627 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700628 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900629 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700630 AIDL_ERROR(this) << "Could not parse " << value_;
631 err = -1;
632 break;
633 }
Will McVickerefd970d2019-09-25 15:28:30 -0700634 return decorator(type, std::to_string(parsed_value) + "f");
635 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700636 err = -1;
637 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700638 }
Will McVickerefd970d2019-09-25 15:28:30 -0700639 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700640 err = -1;
641 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700642 }
643
Steven Moreland21780812020-09-11 01:29:45 +0000644 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700645 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
646 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700647 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700648}
649
650bool AidlConstantValue::CheckValid() const {
651 // Nothing needs to be checked here. The constant value will be validated in
652 // the constructor or in the evaluate() function.
653 if (is_evaluated_) return is_valid_;
654
655 switch (type_) {
656 case Type::BOOLEAN: // fall-through
657 case Type::INT8: // fall-through
658 case Type::INT32: // fall-through
659 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700660 case Type::CHARACTER: // fall-through
661 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900662 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700663 case Type::FLOATING: // fall-through
664 case Type::UNARY: // fall-through
665 case Type::BINARY:
666 is_valid_ = true;
667 break;
Jooyung Han29813842020-12-08 01:28:03 +0900668 case Type::ARRAY:
669 is_valid_ = true;
670 for (const auto& v : values_) is_valid_ &= v->CheckValid();
671 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800672 case Type::ERROR:
673 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700674 default:
675 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
676 return false;
677 }
678
679 return true;
680}
681
Devin Moore2a45d0a2022-01-21 22:58:52 +0000682bool AidlConstantValue::Evaluate() const {
683 if (CheckValid()) {
684 return evaluate();
685 } else {
686 return false;
687 }
688}
689
Jooyung Han74675c22020-12-15 08:39:57 +0900690bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700691 if (is_evaluated_) {
692 return is_valid_;
693 }
694 int err = 0;
695 is_evaluated_ = true;
696
697 switch (type_) {
698 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700699 Type array_type = Type::ERROR;
700 bool success = true;
701 for (const auto& value : values_) {
702 success = value->CheckValid();
703 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900704 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700705 if (!success) {
706 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
707 break;
708 }
709 if (array_type == Type::ERROR) {
710 array_type = value->final_type_;
711 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
712 value->final_type_)) {
713 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
714 << ". Expecting type compatible with " << ToString(array_type);
715 success = false;
716 break;
717 }
718 } else {
719 break;
720 }
721 }
722 if (!success) {
723 err = -1;
724 break;
725 }
726 final_type_ = type_;
727 break;
728 }
729 case Type::BOOLEAN:
730 if ((value_ != "true") && (value_ != "false")) {
731 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
732 err = -1;
733 break;
734 }
735 final_value_ = (value_ == "true") ? 1 : 0;
736 final_type_ = type_;
737 break;
738 case Type::INT8: // fall-through
739 case Type::INT32: // fall-through
740 case Type::INT64:
741 // Parsing happens in the constructor
742 final_type_ = type_;
743 break;
744 case Type::CHARACTER: // fall-through
745 case Type::STRING:
746 final_string_value_ = value_;
747 final_type_ = type_;
748 break;
749 case Type::FLOATING:
750 // Just parse on the fly in ValueString
751 final_type_ = type_;
752 break;
753 default:
754 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
755 err = -1;
756 }
757
758 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700759}
760
761string AidlConstantValue::ToString(Type type) {
762 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700763 case Type::BOOLEAN:
764 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700765 case Type::INT8:
766 return "an int8 literal";
767 case Type::INT32:
768 return "an int32 literal";
769 case Type::INT64:
770 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800771 case Type::ARRAY:
772 return "a literal array";
773 case Type::CHARACTER:
774 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700775 case Type::STRING:
776 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900777 case Type::REF:
778 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800779 case Type::FLOATING:
780 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700781 case Type::UNARY:
782 return "a unary expression";
783 case Type::BINARY:
784 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800785 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000786 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800787 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700788 default:
Steven Moreland21780812020-09-11 01:29:45 +0000789 AIDL_FATAL(AIDL_LOCATION_HERE)
790 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700791 return ""; // not reached
792 }
793}
794
Jooyung Hand0c8af02021-01-06 18:08:01 +0900795AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
796 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900797 const auto pos = value.find_last_of('.');
798 if (pos == string::npos) {
799 field_name_ = value;
800 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900801 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
802 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900803 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900804 field_name_ = value.substr(pos + 1);
805 }
806}
807
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900808const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900809 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900810
811 const AidlDefinedType* defined_type;
812 if (ref_type_) {
813 defined_type = ref_type_->GetDefinedType();
814 } else {
815 defined_type = scope;
816 }
817
818 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900819 // This can happen when "const reference" is used in an unsupported way,
820 // but missed in checks there. It works as a safety net.
821 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900822 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900823 }
824
Jooyung Han690f5842020-12-04 13:02:04 +0900825 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
826 for (const auto& e : enum_decl->GetEnumerators()) {
827 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900828 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900829 }
830 }
831 } else {
832 for (const auto& c : defined_type->GetConstantDeclarations()) {
833 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900834 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900835 }
836 }
837 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900838 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900839 return nullptr;
840}
841
842bool AidlConstantReference::CheckValid() const {
843 if (is_evaluated_) return is_valid_;
844 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
845 is_valid_ = resolved_->CheckValid();
846 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900847}
848
Jooyung Han74675c22020-12-15 08:39:57 +0900849bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900850 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900851 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
852 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900853
Jooyung Han74675c22020-12-15 08:39:57 +0900854 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900855 is_valid_ = resolved_->is_valid_;
856 final_type_ = resolved_->final_type_;
857 if (is_valid_) {
858 if (final_type_ == Type::STRING) {
859 final_string_value_ = resolved_->final_string_value_;
860 } else {
861 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900862 }
863 }
Jooyung Han29813842020-12-08 01:28:03 +0900864 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900865}
866
Will McVickerd7d18df2019-09-12 13:40:50 -0700867bool AidlUnaryConstExpression::CheckValid() const {
868 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000869 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700870
871 is_valid_ = unary_->CheckValid();
872 if (!is_valid_) {
873 final_type_ = Type::ERROR;
874 return false;
875 }
876
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800877 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700878}
879
Jooyung Han74675c22020-12-15 08:39:57 +0900880bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700881 if (is_evaluated_) {
882 return is_valid_;
883 }
884 is_evaluated_ = true;
885
886 // Recursively evaluate the expression tree
887 if (!unary_->is_evaluated_) {
888 // TODO(b/142722772) CheckValid() should be called before ValueString()
889 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900890 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700891 if (!success) {
892 is_valid_ = false;
893 return false;
894 }
895 }
Devin Moorec233fb82020-04-07 11:13:44 -0700896 if (!IsCompatibleType(unary_->final_type_, op_)) {
897 AIDL_ERROR(unary_) << "'" << op_ << "'"
898 << " is not compatible with " << ToString(unary_->final_type_)
899 << ": " + value_;
900 is_valid_ = false;
901 return false;
902 }
903 if (!unary_->is_valid_) {
904 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700905 is_valid_ = false;
906 return false;
907 }
908 final_type_ = unary_->final_type_;
909
910 if (final_type_ == Type::FLOATING) {
911 // don't do anything here. ValueString() will handle everything.
912 is_valid_ = true;
913 return true;
914 }
915
Steven Morelande1ff67e2020-07-16 23:22:36 +0000916#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800917 return is_valid_ = \
918 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700919
920 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
921 is_valid_ = false; return false;)
922}
923
Will McVickerd7d18df2019-09-12 13:40:50 -0700924bool AidlBinaryConstExpression::CheckValid() const {
925 bool success = false;
926 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000927 AIDL_FATAL_IF(left_val_ == nullptr, this);
928 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700929
930 success = left_val_->CheckValid();
931 if (!success) {
932 final_type_ = Type::ERROR;
933 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
934 }
935
936 success = right_val_->CheckValid();
937 if (!success) {
938 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
939 final_type_ = Type::ERROR;
940 }
941
942 if (final_type_ == Type::ERROR) {
943 is_valid_ = false;
944 return false;
945 }
946
947 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800948 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700949}
950
Jooyung Han74675c22020-12-15 08:39:57 +0900951bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700952 if (is_evaluated_) {
953 return is_valid_;
954 }
955 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900956 AIDL_FATAL_IF(left_val_ == nullptr, this);
957 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700958
959 // Recursively evaluate the binary expression tree
960 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
961 // TODO(b/142722772) CheckValid() should be called before ValueString()
962 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900963 success &= left_val_->evaluate();
964 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700965 if (!success) {
966 is_valid_ = false;
967 return false;
968 }
969 }
970 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
971 is_valid_ = false;
972 return false;
973 }
974 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
975 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000976 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
977 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
978 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700979 return false;
980 }
981
982 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
983
984 // Handle String case first
985 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000986 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700987 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000988 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700989 final_type_ = Type::ERROR;
990 is_valid_ = false;
991 return false;
992 }
993
994 // Remove trailing " from lhs
995 const string& lhs = left_val_->final_string_value_;
996 if (lhs.back() != '"') {
997 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
998 final_type_ = Type::ERROR;
999 is_valid_ = false;
1000 return false;
1001 }
1002 const string& rhs = right_val_->final_string_value_;
1003 // Remove starting " from rhs
1004 if (rhs.front() != '"') {
1005 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
1006 final_type_ = Type::ERROR;
1007 is_valid_ = false;
1008 return false;
1009 }
1010
1011 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
1012 final_type_ = Type::STRING;
1013 return true;
1014 }
1015
Will McVickerd7d18df2019-09-12 13:40:50 -07001016 // CASE: + - * / % | ^ & < > <= >= == !=
1017 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -07001018 // promoted kind for both operands.
1019 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1020 IntegralPromotion(right_val_->final_type_));
1021 // result kind.
1022 final_type_ = isArithmeticOrBitflip
1023 ? promoted // arithmetic or bitflip operators generates promoted type
1024 : Type::BOOLEAN; // comparison operators generates bool
1025
Devin Moore1f0360d2020-12-21 12:12:48 -08001026#define CASE_BINARY_COMMON(__type__) \
1027 return is_valid_ = \
1028 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1029 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001030
1031 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1032 is_valid_ = false; return false;)
1033 }
1034
1035 // CASE: << >>
1036 string newOp = op_;
1037 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001038 // promoted kind for both operands.
1039 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1040 IntegralPromotion(right_val_->final_type_));
1041 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001042 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001043 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001044 // is defined as shifting into the other direction.
1045 newOp = OPEQ("<<") ? ">>" : "<<";
1046 numBits = -numBits;
1047 }
1048
Devin Moore1f0360d2020-12-21 12:12:48 -08001049#define CASE_SHIFT(__type__) \
1050 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1051 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001052
1053 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1054 is_valid_ = false; return false;)
1055 }
1056
1057 // CASE: && ||
1058 if (OP_IS_BIN_LOGICAL) {
1059 final_type_ = Type::BOOLEAN;
1060 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001061 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1062 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001063 }
1064
1065 SHOULD_NOT_REACH();
1066 is_valid_ = false;
1067 return false;
1068}
1069
Jooyung Handd0d78f2021-12-06 10:24:28 +09001070// Constructor for integer(byte, int, long)
1071// Keep parsed integer & literal
Will McVickerd7d18df2019-09-12 13:40:50 -07001072AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1073 int64_t parsed_value, const string& checked_value)
1074 : AidlNode(location),
1075 type_(parsed_type),
1076 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001077 final_type_(parsed_type),
1078 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001079 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1080 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001081}
Will McVickerefd970d2019-09-25 15:28:30 -07001082
Jooyung Handd0d78f2021-12-06 10:24:28 +09001083// Constructor for non-integer(String, char, boolean, float, double)
1084// Keep literal as it is. (e.g. String literal has double quotes at both ends)
Will McVickerefd970d2019-09-25 15:28:30 -07001085AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001086 const string& checked_value)
1087 : AidlNode(location),
1088 type_(type),
1089 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001090 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001091 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001092 switch (type_) {
1093 case Type::INT8:
1094 case Type::INT32:
1095 case Type::INT64:
1096 case Type::ARRAY:
1097 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1098 break;
1099 default:
1100 break;
1101 }
1102}
1103
Jooyung Handd0d78f2021-12-06 10:24:28 +09001104// Constructor for array
Will McVickerd7d18df2019-09-12 13:40:50 -07001105AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001106 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1107 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001108 : AidlNode(location),
1109 type_(type),
1110 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001111 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001112 is_valid_(false),
1113 is_evaluated_(false),
1114 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001115 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001116}
1117
1118AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1119 std::unique_ptr<AidlConstantValue> rval)
1120 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1121 unary_(std::move(rval)),
1122 op_(op) {
1123 final_type_ = Type::UNARY;
1124}
1125
1126AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1127 std::unique_ptr<AidlConstantValue> lval,
1128 const string& op,
1129 std::unique_ptr<AidlConstantValue> rval)
1130 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1131 left_val_(std::move(lval)),
1132 right_val_(std::move(rval)),
1133 op_(op) {
1134 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001135}