blob: 14afa1229c0d4cfa83164693f55d81b2805f1723 [file] [log] [blame]
Will McVickerefd970d2019-09-25 15:28:30 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Will McVickerefd970d2019-09-25 15:28:30 -070017#include "aidl_language.h"
Steven Moreland4bcb05c2019-11-27 18:57:47 -080018#include "aidl_typenames.h"
Will McVickerefd970d2019-09-25 15:28:30 -070019#include "logging.h"
20
21#include <stdlib.h>
22#include <algorithm>
23#include <iostream>
Steven Moreland0521bf32020-09-09 22:44:07 +000024#include <limits>
Will McVickerefd970d2019-09-25 15:28:30 -070025#include <memory>
26
27#include <android-base/parsedouble.h>
28#include <android-base/parseint.h>
29#include <android-base/strings.h>
30
Will McVickerd7d18df2019-09-12 13:40:50 -070031using android::base::ConsumeSuffix;
Steven Morelandcef22662020-07-08 20:54:28 +000032using android::base::EndsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070033using android::base::Join;
Steven Morelandcef22662020-07-08 20:54:28 +000034using android::base::StartsWith;
Will McVickerefd970d2019-09-25 15:28:30 -070035using std::string;
36using std::unique_ptr;
37using std::vector;
38
Steven Moreland0521bf32020-09-09 22:44:07 +000039template <typename T>
Devin Moorecff93692020-09-24 10:39:57 -070040constexpr int CLZ(T x) {
Devin Mooree2de9e42020-10-02 08:55:08 -070041 // __builtin_clz(0) is undefined
42 if (x == 0) return sizeof(T) * 8;
Devin Moorecff93692020-09-24 10:39:57 -070043 return (sizeof(T) == sizeof(uint64_t)) ? __builtin_clzl(x) : __builtin_clz(x);
44}
45
46template <typename T>
Steven Moreland0521bf32020-09-09 22:44:07 +000047class OverflowGuard {
48 public:
49 OverflowGuard(T value) : mValue(value) {}
50 bool Overflowed() const { return mOverflowed; }
51
52 T operator+() { return +mValue; }
53 T operator-() {
54 if (isMin()) {
55 mOverflowed = true;
56 return 0;
57 }
58 return -mValue;
59 }
60 T operator!() { return !mValue; }
61 T operator~() { return ~mValue; }
62
63 T operator+(T o) {
64 T out;
65 mOverflowed = __builtin_add_overflow(mValue, o, &out);
66 return out;
67 }
68 T operator-(T o) {
69 T out;
70 mOverflowed = __builtin_sub_overflow(mValue, o, &out);
71 return out;
72 }
73 T operator*(T o) {
74 T out;
75#ifdef _WIN32
76 // ___mulodi4 not on windows https://bugs.llvm.org/show_bug.cgi?id=46669
77 // we should still get an error here from ubsan, but the nice error
78 // is needed on linux for aidl_parser_fuzzer, where we are more
79 // concerned about overflows elsewhere in the compiler in addition to
80 // those in interfaces.
81 out = mValue * o;
82#else
83 mOverflowed = __builtin_mul_overflow(mValue, o, &out);
84#endif
85 return out;
86 }
87 T operator/(T o) {
88 if (o == 0 || (isMin() && o == -1)) {
89 mOverflowed = true;
90 return 0;
91 }
92 return mValue / o;
93 }
94 T operator%(T o) {
95 if (o == 0 || (isMin() && o == -1)) {
96 mOverflowed = true;
97 return 0;
98 }
99 return mValue % o;
100 }
101 T operator|(T o) { return mValue | o; }
102 T operator^(T o) { return mValue ^ o; }
103 T operator&(T o) { return mValue & o; }
104 T operator<(T o) { return mValue < o; }
105 T operator>(T o) { return mValue > o; }
106 T operator<=(T o) { return mValue <= o; }
107 T operator>=(T o) { return mValue >= o; }
108 T operator==(T o) { return mValue == o; }
109 T operator!=(T o) { return mValue != o; }
110 T operator>>(T o) {
Devin Moorea9e64de2020-09-29 11:29:42 -0700111 if (o < 0 || o >= static_cast<T>(sizeof(T) * 8) || mValue < 0) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000112 mOverflowed = true;
113 return 0;
114 }
115 return mValue >> o;
116 }
117 T operator<<(T o) {
Devin Mooree2de9e42020-10-02 08:55:08 -0700118 if (o < 0 || mValue < 0 || o > CLZ(mValue) || o >= static_cast<T>(sizeof(T) * 8)) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000119 mOverflowed = true;
120 return 0;
121 }
122 return mValue << o;
123 }
124 T operator||(T o) { return mValue || o; }
125 T operator&&(T o) { return mValue && o; }
126
127 private:
128 bool isMin() { return mValue == std::numeric_limits<T>::min(); }
129
130 T mValue;
131 bool mOverflowed = false;
132};
133
134template <typename T>
135bool processGuard(const OverflowGuard<T>& guard, const AidlConstantValue& context) {
136 if (guard.Overflowed()) {
137 AIDL_ERROR(context) << "Constant expression computation overflows.";
138 return false;
139 }
140 return true;
141}
142
143// TODO: factor out all these macros
Steven Moreland21780812020-09-11 01:29:45 +0000144#define SHOULD_NOT_REACH() AIDL_FATAL(AIDL_LOCATION_HERE) << "Should not reach."
Will McVickerd7d18df2019-09-12 13:40:50 -0700145#define OPEQ(__y__) (string(op_) == string(__y__))
Steven Moreland0521bf32020-09-09 22:44:07 +0000146#define COMPUTE_UNARY(T, __op__) \
147 if (op == string(#__op__)) { \
148 OverflowGuard<T> guard(val); \
149 *out = __op__ guard; \
150 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000151 }
Steven Moreland0521bf32020-09-09 22:44:07 +0000152#define COMPUTE_BINARY(T, __op__) \
153 if (op == string(#__op__)) { \
154 OverflowGuard<T> guard(lval); \
155 *out = guard __op__ rval; \
156 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000157 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700158#define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%"))
159#define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&"))
160#define OP_IS_BIN_COMP \
161 (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!="))
162#define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<"))
163#define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&"))
164
165// NOLINT to suppress missing parentheses warnings about __def__.
166#define SWITCH_KIND(__cond__, __action__, __def__) \
167 switch (__cond__) { \
168 case Type::BOOLEAN: \
169 __action__(bool); \
170 case Type::INT8: \
171 __action__(int8_t); \
172 case Type::INT32: \
173 __action__(int32_t); \
174 case Type::INT64: \
175 __action__(int64_t); \
176 default: \
177 __def__; /* NOLINT */ \
178 }
179
180template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000181bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000182 COMPUTE_UNARY(T, +)
183 COMPUTE_UNARY(T, -)
184 COMPUTE_UNARY(T, !)
185 COMPUTE_UNARY(T, ~)
Steven Moreland720a3cc2020-07-16 23:44:59 +0000186 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
187 return false;
188}
189template <>
190bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000191 COMPUTE_UNARY(bool, +)
192 COMPUTE_UNARY(bool, -)
193 COMPUTE_UNARY(bool, !)
Yifan Hongf17e3a72020-02-20 17:34:58 -0800194
Steven Moreland720a3cc2020-07-16 23:44:59 +0000195 if (op == "~") {
196 AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true.";
197 return false;
198 }
Steven Morelande1ff67e2020-07-16 23:22:36 +0000199 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
200 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700201}
202
203template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000204bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval,
205 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000206 COMPUTE_BINARY(T, +)
207 COMPUTE_BINARY(T, -)
208 COMPUTE_BINARY(T, *)
209 COMPUTE_BINARY(T, /)
210 COMPUTE_BINARY(T, %)
211 COMPUTE_BINARY(T, |)
212 COMPUTE_BINARY(T, ^)
213 COMPUTE_BINARY(T, &)
Will McVickerd7d18df2019-09-12 13:40:50 -0700214 // comparison operators: return 0 or 1 by nature.
Steven Moreland0521bf32020-09-09 22:44:07 +0000215 COMPUTE_BINARY(T, ==)
216 COMPUTE_BINARY(T, !=)
217 COMPUTE_BINARY(T, <)
218 COMPUTE_BINARY(T, >)
219 COMPUTE_BINARY(T, <=)
220 COMPUTE_BINARY(T, >=)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000221
222 AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval;
223 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700224}
225
226template <class T>
Devin Moore04823022020-09-11 10:43:35 -0700227bool handleShift(const AidlConstantValue& context, T lval, const string& op, T rval, int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700228 // just cast rval to int64_t and it should fit.
Steven Moreland0521bf32020-09-09 22:44:07 +0000229 COMPUTE_BINARY(T, >>)
230 COMPUTE_BINARY(T, <<)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000231
232 AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval;
233 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700234}
235
Steven Morelande1ff67e2020-07-16 23:22:36 +0000236bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval,
237 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000238 COMPUTE_BINARY(bool, ||);
239 COMPUTE_BINARY(bool, &&);
Steven Morelande1ff67e2020-07-16 23:22:36 +0000240
241 AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval;
Will McVickerd7d18df2019-09-12 13:40:50 -0700242 return false;
243}
244
Will McVickerefd970d2019-09-25 15:28:30 -0700245static bool isValidLiteralChar(char c) {
246 return !(c <= 0x1f || // control characters are < 0x20
247 c >= 0x7f || // DEL is 0x7f
248 c == '\\'); // Disallow backslashes for future proofing.
249}
250
Jooyung Han535c5e82020-12-29 15:16:59 +0900251bool ParseFloating(std::string_view sv, double* parsed) {
252 // float literal should be parsed successfully.
Jooyung Han71a1b582020-12-25 23:58:41 +0900253 android::base::ConsumeSuffix(&sv, "f");
Jooyung Han535c5e82020-12-29 15:16:59 +0900254 return android::base::ParseDouble(std::string(sv).data(), parsed);
255}
256
257bool ParseFloating(std::string_view sv, float* parsed) {
258 // we only care about float literal (with suffix "f").
259 if (!android::base::ConsumeSuffix(&sv, "f")) {
260 return false;
Jooyung Han71a1b582020-12-25 23:58:41 +0900261 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900262 return android::base::ParseFloat(std::string(sv).data(), parsed);
Jooyung Han71a1b582020-12-25 23:58:41 +0900263}
264
Will McVickerd7d18df2019-09-12 13:40:50 -0700265bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
266 // Verify the unary type here
267 switch (type) {
268 case Type::BOOLEAN: // fall-through
269 case Type::INT8: // fall-through
270 case Type::INT32: // fall-through
271 case Type::INT64:
272 return true;
273 case Type::FLOATING:
274 return (op == "+" || op == "-");
275 default:
276 return false;
277 }
278}
279
280bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
281 switch (t1) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900282 case Type::ARRAY:
283 if (t2 == Type::ARRAY) {
284 return true;
285 }
286 break;
Will McVickerd7d18df2019-09-12 13:40:50 -0700287 case Type::STRING:
288 if (t2 == Type::STRING) {
289 return true;
290 }
291 break;
292 case Type::BOOLEAN: // fall-through
293 case Type::INT8: // fall-through
294 case Type::INT32: // fall-through
295 case Type::INT64:
296 switch (t2) {
297 case Type::BOOLEAN: // fall-through
298 case Type::INT8: // fall-through
299 case Type::INT32: // fall-through
300 case Type::INT64:
301 return true;
302 break;
303 default:
304 break;
305 }
306 break;
307 default:
308 break;
309 }
310
311 return false;
312}
313
314// Returns the promoted kind for both operands
315AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
316 Type right) {
317 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000318 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
319 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700320
321 // Kinds in concern: bool, (u)int[8|32|64]
322 if (left == right) return left; // easy case
323 if (left == Type::BOOLEAN) return right;
324 if (right == Type::BOOLEAN) return left;
325
326 return left < right ? right : left;
327}
328
329// Returns the promoted integral type where INT32 is the smallest type
330AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
331 return (Type::INT32 < in) ? in : Type::INT32;
332}
333
Steven Moreland541788d2020-05-21 22:05:52 +0000334AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
335 AidlLocation location = specifier.GetLocation();
336
Jooyung Han0cc99632021-11-30 17:19:05 +0900337 // Initialize non-nullable fixed-size arrays with {}("empty list").
338 // Each backend will handle it differently. For example, in Rust, it can be mapped to
339 // "Default::default()".
340 if (specifier.IsFixedSizeArray() && !specifier.IsNullable()) {
341 return Array(location, std::make_unique<std::vector<std::unique_ptr<AidlConstantValue>>>());
342 }
Steven Moreland541788d2020-05-21 22:05:52 +0000343 // allocation of int[0] is a bit wasteful in Java
344 if (specifier.IsArray()) {
345 return nullptr;
346 }
347
348 const std::string name = specifier.GetName();
349 if (name == "boolean") {
350 return Boolean(location, false);
351 }
352 if (name == "byte" || name == "int" || name == "long") {
353 return Integral(location, "0");
354 }
355 if (name == "float") {
356 return Floating(location, "0.0f");
357 }
358 if (name == "double") {
359 return Floating(location, "0.0");
360 }
361 return nullptr;
362}
363
Will McVickerefd970d2019-09-25 15:28:30 -0700364AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
365 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
366}
367
368AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800369 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700370 if (!isValidLiteralChar(value)) {
371 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800372 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700373 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800374 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700375}
376
377AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
378 const std::string& value) {
379 return new AidlConstantValue(location, Type::FLOATING, value);
380}
381
Will McVickerd7d18df2019-09-12 13:40:50 -0700382bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000383 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700384}
385
Will McVickerd7d18df2019-09-12 13:40:50 -0700386bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
387 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700388 if (parsed_value == nullptr || parsed_type == nullptr) {
389 return false;
390 }
391
Steven Morelandb7d58652021-10-25 15:10:02 -0700392 std::string_view value_view = value;
393 const bool is_byte = ConsumeSuffix(&value_view, "u8");
394 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
395 const std::string value_substr = std::string(value_view);
396
397 *parsed_value = 0;
398 *parsed_type = Type::ERROR;
399
400 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700401
Steven Morelandcef22662020-07-08 20:54:28 +0000402 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700403 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
404 // handle that when computing constant expressions, then we need to
405 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
406 // so we parse as an unsigned int when possible and then cast to a signed
407 // int. One example of this is in ICameraService.aidl where a constant int
408 // is used for bit manipulations which ideally should be handled with an
409 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000410 //
411 // Note, for historical consistency, we need to consider small hex values
412 // as an integral type. Recognizing them as INT8 could break some files,
413 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700414 if (is_byte) {
415 uint8_t raw_value8;
416 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
417 return false;
418 }
419 *parsed_value = static_cast<int8_t>(raw_value8);
420 *parsed_type = Type::INT8;
421 } else if (uint32_t raw_value32;
422 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
423 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700424 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700425 } else if (uint64_t raw_value64;
426 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
427 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700428 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000429 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000430 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700431 }
432 return true;
433 }
434
Steven Morelandcef22662020-07-08 20:54:28 +0000435 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700436 return false;
437 }
438
Steven Morelandb7d58652021-10-25 15:10:02 -0700439 if (is_byte) {
440 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
441 return false;
442 }
443 *parsed_value = static_cast<int8_t>(*parsed_value);
444 *parsed_type = Type::INT8;
445 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000446 *parsed_type = Type::INT64;
447 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700448 // guess literal type.
449 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
450 *parsed_type = Type::INT8;
451 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
452 *parsed_type = Type::INT32;
453 } else {
454 *parsed_type = Type::INT64;
455 }
456 }
457 return true;
458}
459
460AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000461 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700462
463 Type parsed_type;
464 int64_t parsed_value = 0;
465 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
466 if (!success) {
467 return nullptr;
468 }
469
470 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700471}
472
473AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700474 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000475 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900476 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900477 std::vector<std::string> str_values;
478 for (const auto& v : *values) {
479 str_values.push_back(v->value_);
480 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900481 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
482 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700483}
484
Will McVickerd7d18df2019-09-12 13:40:50 -0700485AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700486 for (size_t i = 0; i < value.length(); ++i) {
487 if (!isValidLiteralChar(value[i])) {
488 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
489 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800490 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700491 }
492 }
493
494 return new AidlConstantValue(location, Type::STRING, value);
495}
496
Will McVickerd7d18df2019-09-12 13:40:50 -0700497string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
498 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700499 if (type.IsGeneric()) {
500 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
501 return "";
502 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700503 if (!is_evaluated_) {
504 // TODO(b/142722772) CheckValid() should be called before ValueString()
505 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900506 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700507 if (!success) {
508 // the detailed error message shall be printed in evaluate
509 return "";
510 }
Will McVickerefd970d2019-09-25 15:28:30 -0700511 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700512 if (!is_valid_) {
513 AIDL_ERROR(this) << "Invalid constant value: " + value_;
514 return "";
515 }
Jooyung Han690f5842020-12-04 13:02:04 +0900516
517 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900518 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900519 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
520 if (!enum_type) {
521 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900522 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900523 return "";
524 }
525 if (type_ != Type::REF) {
526 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
527 << enum_type->GetCanonicalName();
528 return "";
529 }
530 return decorator(type, value_);
531 }
532
Jooyung Hanaeb01672021-11-30 17:29:22 +0900533 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700534 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700535
Will McVickerd7d18df2019-09-12 13:40:50 -0700536 switch (final_type_) {
537 case Type::CHARACTER:
538 if (type_string == "char") {
539 return decorator(type, final_string_value_);
540 }
541 err = -1;
542 break;
543 case Type::STRING:
544 if (type_string == "String") {
545 return decorator(type, final_string_value_);
546 }
547 err = -1;
548 break;
549 case Type::BOOLEAN: // fall-through
550 case Type::INT8: // fall-through
551 case Type::INT32: // fall-through
552 case Type::INT64:
553 if (type_string == "byte") {
554 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
555 err = -1;
556 break;
557 }
558 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
559 } else if (type_string == "int") {
560 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
561 err = -1;
562 break;
563 }
564 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
565 } else if (type_string == "long") {
566 return decorator(type, std::to_string(final_value_));
567 } else if (type_string == "boolean") {
568 return decorator(type, final_value_ ? "true" : "false");
569 }
570 err = -1;
571 break;
572 case Type::ARRAY: {
573 if (!type.IsArray()) {
574 err = -1;
575 break;
576 }
577 vector<string> value_strings;
578 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700579 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700580
Will McVickerefd970d2019-09-25 15:28:30 -0700581 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900582 string value_string;
583 type.ViewAsArrayBase([&](const auto& base_type) {
584 value_string = value->ValueString(base_type, decorator);
585 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 if (value_string.empty()) {
587 success = false;
588 break;
589 }
590 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700591 }
592 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700593 err = -1;
594 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700595 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900596 if (type.IsFixedSizeArray()) {
597 auto size =
598 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
599 if (values_.size() > static_cast<size_t>(size)) {
600 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
601 << values_.size() << " elements";
602 err = -1;
603 break;
604 }
605 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900606 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700607 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700608 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700609 if (type_string == "double") {
610 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900611 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700612 AIDL_ERROR(this) << "Could not parse " << value_;
613 err = -1;
614 break;
615 }
Will McVickerefd970d2019-09-25 15:28:30 -0700616 return decorator(type, std::to_string(parsed_value));
617 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900618 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700619 float 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) + "f");
626 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700627 err = -1;
628 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700629 }
Will McVickerefd970d2019-09-25 15:28:30 -0700630 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700631 err = -1;
632 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700633 }
634
Steven Moreland21780812020-09-11 01:29:45 +0000635 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700636 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
637 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700638 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700639}
640
641bool AidlConstantValue::CheckValid() const {
642 // Nothing needs to be checked here. The constant value will be validated in
643 // the constructor or in the evaluate() function.
644 if (is_evaluated_) return is_valid_;
645
646 switch (type_) {
647 case Type::BOOLEAN: // fall-through
648 case Type::INT8: // fall-through
649 case Type::INT32: // fall-through
650 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700651 case Type::CHARACTER: // fall-through
652 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900653 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700654 case Type::FLOATING: // fall-through
655 case Type::UNARY: // fall-through
656 case Type::BINARY:
657 is_valid_ = true;
658 break;
Jooyung Han29813842020-12-08 01:28:03 +0900659 case Type::ARRAY:
660 is_valid_ = true;
661 for (const auto& v : values_) is_valid_ &= v->CheckValid();
662 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800663 case Type::ERROR:
664 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700665 default:
666 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
667 return false;
668 }
669
670 return true;
671}
672
Jooyung Han74675c22020-12-15 08:39:57 +0900673bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700674 if (is_evaluated_) {
675 return is_valid_;
676 }
677 int err = 0;
678 is_evaluated_ = true;
679
680 switch (type_) {
681 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700682 Type array_type = Type::ERROR;
683 bool success = true;
684 for (const auto& value : values_) {
685 success = value->CheckValid();
686 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900687 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700688 if (!success) {
689 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
690 break;
691 }
692 if (array_type == Type::ERROR) {
693 array_type = value->final_type_;
694 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
695 value->final_type_)) {
696 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
697 << ". Expecting type compatible with " << ToString(array_type);
698 success = false;
699 break;
700 }
701 } else {
702 break;
703 }
704 }
705 if (!success) {
706 err = -1;
707 break;
708 }
709 final_type_ = type_;
710 break;
711 }
712 case Type::BOOLEAN:
713 if ((value_ != "true") && (value_ != "false")) {
714 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
715 err = -1;
716 break;
717 }
718 final_value_ = (value_ == "true") ? 1 : 0;
719 final_type_ = type_;
720 break;
721 case Type::INT8: // fall-through
722 case Type::INT32: // fall-through
723 case Type::INT64:
724 // Parsing happens in the constructor
725 final_type_ = type_;
726 break;
727 case Type::CHARACTER: // fall-through
728 case Type::STRING:
729 final_string_value_ = value_;
730 final_type_ = type_;
731 break;
732 case Type::FLOATING:
733 // Just parse on the fly in ValueString
734 final_type_ = type_;
735 break;
736 default:
737 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
738 err = -1;
739 }
740
741 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700742}
743
744string AidlConstantValue::ToString(Type type) {
745 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700746 case Type::BOOLEAN:
747 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700748 case Type::INT8:
749 return "an int8 literal";
750 case Type::INT32:
751 return "an int32 literal";
752 case Type::INT64:
753 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800754 case Type::ARRAY:
755 return "a literal array";
756 case Type::CHARACTER:
757 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700758 case Type::STRING:
759 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900760 case Type::REF:
761 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800762 case Type::FLOATING:
763 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700764 case Type::UNARY:
765 return "a unary expression";
766 case Type::BINARY:
767 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800768 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000769 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800770 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700771 default:
Steven Moreland21780812020-09-11 01:29:45 +0000772 AIDL_FATAL(AIDL_LOCATION_HERE)
773 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700774 return ""; // not reached
775 }
776}
777
Jooyung Hand0c8af02021-01-06 18:08:01 +0900778AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
779 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900780 const auto pos = value.find_last_of('.');
781 if (pos == string::npos) {
782 field_name_ = value;
783 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900784 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
785 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900786 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900787 field_name_ = value.substr(pos + 1);
788 }
789}
790
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900791const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900792 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900793
794 const AidlDefinedType* defined_type;
795 if (ref_type_) {
796 defined_type = ref_type_->GetDefinedType();
797 } else {
798 defined_type = scope;
799 }
800
801 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900802 // This can happen when "const reference" is used in an unsupported way,
803 // but missed in checks there. It works as a safety net.
804 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900805 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900806 }
807
Jooyung Han690f5842020-12-04 13:02:04 +0900808 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
809 for (const auto& e : enum_decl->GetEnumerators()) {
810 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900811 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900812 }
813 }
814 } else {
815 for (const auto& c : defined_type->GetConstantDeclarations()) {
816 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900817 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900818 }
819 }
820 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900821 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900822 return nullptr;
823}
824
825bool AidlConstantReference::CheckValid() const {
826 if (is_evaluated_) return is_valid_;
827 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
828 is_valid_ = resolved_->CheckValid();
829 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900830}
831
Jooyung Han74675c22020-12-15 08:39:57 +0900832bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900833 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900834 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
835 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900836
Jooyung Han74675c22020-12-15 08:39:57 +0900837 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900838 is_valid_ = resolved_->is_valid_;
839 final_type_ = resolved_->final_type_;
840 if (is_valid_) {
841 if (final_type_ == Type::STRING) {
842 final_string_value_ = resolved_->final_string_value_;
843 } else {
844 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900845 }
846 }
Jooyung Han29813842020-12-08 01:28:03 +0900847 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900848}
849
Will McVickerd7d18df2019-09-12 13:40:50 -0700850bool AidlUnaryConstExpression::CheckValid() const {
851 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000852 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700853
854 is_valid_ = unary_->CheckValid();
855 if (!is_valid_) {
856 final_type_ = Type::ERROR;
857 return false;
858 }
859
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800860 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700861}
862
Jooyung Han74675c22020-12-15 08:39:57 +0900863bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700864 if (is_evaluated_) {
865 return is_valid_;
866 }
867 is_evaluated_ = true;
868
869 // Recursively evaluate the expression tree
870 if (!unary_->is_evaluated_) {
871 // TODO(b/142722772) CheckValid() should be called before ValueString()
872 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900873 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700874 if (!success) {
875 is_valid_ = false;
876 return false;
877 }
878 }
Devin Moorec233fb82020-04-07 11:13:44 -0700879 if (!IsCompatibleType(unary_->final_type_, op_)) {
880 AIDL_ERROR(unary_) << "'" << op_ << "'"
881 << " is not compatible with " << ToString(unary_->final_type_)
882 << ": " + value_;
883 is_valid_ = false;
884 return false;
885 }
886 if (!unary_->is_valid_) {
887 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700888 is_valid_ = false;
889 return false;
890 }
891 final_type_ = unary_->final_type_;
892
893 if (final_type_ == Type::FLOATING) {
894 // don't do anything here. ValueString() will handle everything.
895 is_valid_ = true;
896 return true;
897 }
898
Steven Morelande1ff67e2020-07-16 23:22:36 +0000899#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800900 return is_valid_ = \
901 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700902
903 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
904 is_valid_ = false; return false;)
905}
906
Will McVickerd7d18df2019-09-12 13:40:50 -0700907bool AidlBinaryConstExpression::CheckValid() const {
908 bool success = false;
909 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000910 AIDL_FATAL_IF(left_val_ == nullptr, this);
911 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700912
913 success = left_val_->CheckValid();
914 if (!success) {
915 final_type_ = Type::ERROR;
916 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
917 }
918
919 success = right_val_->CheckValid();
920 if (!success) {
921 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
922 final_type_ = Type::ERROR;
923 }
924
925 if (final_type_ == Type::ERROR) {
926 is_valid_ = false;
927 return false;
928 }
929
930 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800931 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700932}
933
Jooyung Han74675c22020-12-15 08:39:57 +0900934bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700935 if (is_evaluated_) {
936 return is_valid_;
937 }
938 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900939 AIDL_FATAL_IF(left_val_ == nullptr, this);
940 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700941
942 // Recursively evaluate the binary expression tree
943 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
944 // TODO(b/142722772) CheckValid() should be called before ValueString()
945 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900946 success &= left_val_->evaluate();
947 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700948 if (!success) {
949 is_valid_ = false;
950 return false;
951 }
952 }
953 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
954 is_valid_ = false;
955 return false;
956 }
957 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
958 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000959 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
960 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
961 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700962 return false;
963 }
964
965 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
966
967 // Handle String case first
968 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000969 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700970 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000971 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700972 final_type_ = Type::ERROR;
973 is_valid_ = false;
974 return false;
975 }
976
977 // Remove trailing " from lhs
978 const string& lhs = left_val_->final_string_value_;
979 if (lhs.back() != '"') {
980 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
981 final_type_ = Type::ERROR;
982 is_valid_ = false;
983 return false;
984 }
985 const string& rhs = right_val_->final_string_value_;
986 // Remove starting " from rhs
987 if (rhs.front() != '"') {
988 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
989 final_type_ = Type::ERROR;
990 is_valid_ = false;
991 return false;
992 }
993
994 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
995 final_type_ = Type::STRING;
996 return true;
997 }
998
Will McVickerd7d18df2019-09-12 13:40:50 -0700999 // CASE: + - * / % | ^ & < > <= >= == !=
1000 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -07001001 // promoted kind for both operands.
1002 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1003 IntegralPromotion(right_val_->final_type_));
1004 // result kind.
1005 final_type_ = isArithmeticOrBitflip
1006 ? promoted // arithmetic or bitflip operators generates promoted type
1007 : Type::BOOLEAN; // comparison operators generates bool
1008
Devin Moore1f0360d2020-12-21 12:12:48 -08001009#define CASE_BINARY_COMMON(__type__) \
1010 return is_valid_ = \
1011 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1012 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001013
1014 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1015 is_valid_ = false; return false;)
1016 }
1017
1018 // CASE: << >>
1019 string newOp = op_;
1020 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001021 // promoted kind for both operands.
1022 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1023 IntegralPromotion(right_val_->final_type_));
1024 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001025 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001026 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001027 // is defined as shifting into the other direction.
1028 newOp = OPEQ("<<") ? ">>" : "<<";
1029 numBits = -numBits;
1030 }
1031
Devin Moore1f0360d2020-12-21 12:12:48 -08001032#define CASE_SHIFT(__type__) \
1033 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1034 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001035
1036 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1037 is_valid_ = false; return false;)
1038 }
1039
1040 // CASE: && ||
1041 if (OP_IS_BIN_LOGICAL) {
1042 final_type_ = Type::BOOLEAN;
1043 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001044 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1045 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001046 }
1047
1048 SHOULD_NOT_REACH();
1049 is_valid_ = false;
1050 return false;
1051}
1052
Will McVickerd7d18df2019-09-12 13:40:50 -07001053AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1054 int64_t parsed_value, const string& checked_value)
1055 : AidlNode(location),
1056 type_(parsed_type),
1057 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001058 final_type_(parsed_type),
1059 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001060 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1061 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001062}
Will McVickerefd970d2019-09-25 15:28:30 -07001063
1064AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001065 const string& checked_value)
1066 : AidlNode(location),
1067 type_(type),
1068 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001069 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001070 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001071 switch (type_) {
1072 case Type::INT8:
1073 case Type::INT32:
1074 case Type::INT64:
1075 case Type::ARRAY:
1076 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1077 break;
1078 default:
1079 break;
1080 }
1081}
1082
1083AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001084 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1085 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001086 : AidlNode(location),
1087 type_(type),
1088 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001089 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001090 is_valid_(false),
1091 is_evaluated_(false),
1092 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001093 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001094}
1095
1096AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1097 std::unique_ptr<AidlConstantValue> rval)
1098 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1099 unary_(std::move(rval)),
1100 op_(op) {
1101 final_type_ = Type::UNARY;
1102}
1103
1104AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1105 std::unique_ptr<AidlConstantValue> lval,
1106 const string& op,
1107 std::unique_ptr<AidlConstantValue> rval)
1108 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1109 left_val_(std::move(lval)),
1110 right_val_(std::move(rval)),
1111 op_(op) {
1112 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001113}