blob: b3a1667d93d70f78fa578bea51fcace76803697b [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 }
346 if (name == "byte" || name == "int" || name == "long") {
347 return Integral(location, "0");
348 }
349 if (name == "float") {
350 return Floating(location, "0.0f");
351 }
352 if (name == "double") {
353 return Floating(location, "0.0");
354 }
355 return nullptr;
356}
357
Will McVickerefd970d2019-09-25 15:28:30 -0700358AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
359 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
360}
361
362AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800363 const std::string explicit_value = string("'") + value + "'";
Steven Moreland8c9b7ec2022-01-26 22:50:12 +0000364 if (!isValidLiteralChar(value)) {
365 AIDL_ERROR(location) << "Invalid character literal " << value;
366 return new AidlConstantValue(location, Type::ERROR, explicit_value);
367 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800368 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700369}
370
371AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
372 const std::string& value) {
373 return new AidlConstantValue(location, Type::FLOATING, value);
374}
375
Will McVickerd7d18df2019-09-12 13:40:50 -0700376bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000377 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700378}
379
Will McVickerd7d18df2019-09-12 13:40:50 -0700380bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
381 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700382 if (parsed_value == nullptr || parsed_type == nullptr) {
383 return false;
384 }
385
Steven Morelandb7d58652021-10-25 15:10:02 -0700386 std::string_view value_view = value;
387 const bool is_byte = ConsumeSuffix(&value_view, "u8");
388 const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L");
389 const std::string value_substr = std::string(value_view);
390
391 *parsed_value = 0;
392 *parsed_type = Type::ERROR;
393
394 if (is_byte && is_long) return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700395
Steven Morelandcef22662020-07-08 20:54:28 +0000396 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700397 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
398 // handle that when computing constant expressions, then we need to
399 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
400 // so we parse as an unsigned int when possible and then cast to a signed
401 // int. One example of this is in ICameraService.aidl where a constant int
402 // is used for bit manipulations which ideally should be handled with an
403 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000404 //
405 // Note, for historical consistency, we need to consider small hex values
406 // as an integral type. Recognizing them as INT8 could break some files,
407 // even though it would simplify this code.
Steven Morelandb7d58652021-10-25 15:10:02 -0700408 if (is_byte) {
409 uint8_t raw_value8;
410 if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) {
411 return false;
412 }
413 *parsed_value = static_cast<int8_t>(raw_value8);
414 *parsed_type = Type::INT8;
415 } else if (uint32_t raw_value32;
416 !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) {
417 *parsed_value = static_cast<int32_t>(raw_value32);
Will McVickerd7d18df2019-09-12 13:40:50 -0700418 *parsed_type = Type::INT32;
Steven Morelandb7d58652021-10-25 15:10:02 -0700419 } else if (uint64_t raw_value64;
420 android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) {
421 *parsed_value = static_cast<int64_t>(raw_value64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700422 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000423 } else {
Steven Morelandcef22662020-07-08 20:54:28 +0000424 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700425 }
426 return true;
427 }
428
Steven Morelandcef22662020-07-08 20:54:28 +0000429 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700430 return false;
431 }
432
Steven Morelandb7d58652021-10-25 15:10:02 -0700433 if (is_byte) {
434 if (*parsed_value > UINT8_MAX || *parsed_value < 0) {
435 return false;
436 }
437 *parsed_value = static_cast<int8_t>(*parsed_value);
438 *parsed_type = Type::INT8;
439 } else if (is_long) {
Steven Morelandcef22662020-07-08 20:54:28 +0000440 *parsed_type = Type::INT64;
441 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 // guess literal type.
443 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
444 *parsed_type = Type::INT8;
445 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
446 *parsed_type = Type::INT32;
447 } else {
448 *parsed_type = Type::INT64;
449 }
450 }
451 return true;
452}
453
454AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000455 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700456
457 Type parsed_type;
458 int64_t parsed_value = 0;
459 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
460 if (!success) {
461 return nullptr;
462 }
463
464 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700465}
466
467AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000469 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Hanaeb01672021-11-30 17:29:22 +0900470 // Reconstruct literal value
Jooyung Han29813842020-12-08 01:28:03 +0900471 std::vector<std::string> str_values;
472 for (const auto& v : *values) {
473 str_values.push_back(v->value_);
474 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900475 return new AidlConstantValue(location, Type::ARRAY, std::move(values),
476 "{" + Join(str_values, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700477}
478
Will McVickerd7d18df2019-09-12 13:40:50 -0700479AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Steven Moreland797b06b2022-01-26 02:27:23 +0000480 AIDL_FATAL_IF(value.size() == 0, "If this is unquoted, we need to update the index log");
481 AIDL_FATAL_IF(value[0] != '\"', "If this is unquoted, we need to update the index log");
482
Steven Morelande9f8aad2022-01-31 19:27:21 +0000483 for (size_t i = 0; i < value.length(); ++i) {
484 if (!isValidLiteralChar(value[i])) {
Steven Moreland797b06b2022-01-26 02:27:23 +0000485 AIDL_ERROR(location) << "Found invalid character '" << value[i] << "' at index " << i - 1
486 << " in string constant '" << value << "'";
Steven Morelande9f8aad2022-01-31 19:27:21 +0000487 return new AidlConstantValue(location, Type::ERROR, value);
488 }
489 }
490
Will McVickerefd970d2019-09-25 15:28:30 -0700491 return new AidlConstantValue(location, Type::STRING, value);
492}
493
Will McVickerd7d18df2019-09-12 13:40:50 -0700494string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
495 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700496 if (type.IsGeneric()) {
497 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
498 return "";
499 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 if (!is_evaluated_) {
501 // TODO(b/142722772) CheckValid() should be called before ValueString()
502 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900503 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700504 if (!success) {
505 // the detailed error message shall be printed in evaluate
506 return "";
507 }
Will McVickerefd970d2019-09-25 15:28:30 -0700508 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700509 if (!is_valid_) {
510 AIDL_ERROR(this) << "Invalid constant value: " + value_;
511 return "";
512 }
Jooyung Han690f5842020-12-04 13:02:04 +0900513
514 const AidlDefinedType* defined_type = type.GetDefinedType();
Jooyung Han981fc592021-11-06 20:24:45 +0900515 if (defined_type && final_type_ != Type::ARRAY) {
Jooyung Han690f5842020-12-04 13:02:04 +0900516 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
517 if (!enum_type) {
518 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900519 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900520 return "";
521 }
522 if (type_ != Type::REF) {
523 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
524 << enum_type->GetCanonicalName();
525 return "";
526 }
527 return decorator(type, value_);
528 }
529
Jooyung Hanaeb01672021-11-30 17:29:22 +0900530 const string& type_string = type.Signature();
Will McVickerd7d18df2019-09-12 13:40:50 -0700531 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700532
Will McVickerd7d18df2019-09-12 13:40:50 -0700533 switch (final_type_) {
534 case Type::CHARACTER:
535 if (type_string == "char") {
536 return decorator(type, final_string_value_);
537 }
538 err = -1;
539 break;
540 case Type::STRING:
541 if (type_string == "String") {
542 return decorator(type, final_string_value_);
543 }
544 err = -1;
545 break;
546 case Type::BOOLEAN: // fall-through
547 case Type::INT8: // fall-through
548 case Type::INT32: // fall-through
549 case Type::INT64:
550 if (type_string == "byte") {
551 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
552 err = -1;
553 break;
554 }
555 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
556 } else if (type_string == "int") {
557 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
558 err = -1;
559 break;
560 }
561 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
562 } else if (type_string == "long") {
563 return decorator(type, std::to_string(final_value_));
564 } else if (type_string == "boolean") {
565 return decorator(type, final_value_ ? "true" : "false");
566 }
567 err = -1;
568 break;
569 case Type::ARRAY: {
570 if (!type.IsArray()) {
571 err = -1;
572 break;
573 }
574 vector<string> value_strings;
575 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700576 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700577
Will McVickerefd970d2019-09-25 15:28:30 -0700578 for (const auto& value : values_) {
Jooyung Hanaeb01672021-11-30 17:29:22 +0900579 string value_string;
580 type.ViewAsArrayBase([&](const auto& base_type) {
581 value_string = value->ValueString(base_type, decorator);
582 });
Will McVickerd7d18df2019-09-12 13:40:50 -0700583 if (value_string.empty()) {
584 success = false;
585 break;
586 }
587 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700588 }
589 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700590 err = -1;
591 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700592 }
Jooyung Han0cc99632021-11-30 17:19:05 +0900593 if (type.IsFixedSizeArray()) {
594 auto size =
595 std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>();
Jooyung Hane76bcc22022-01-23 22:49:45 +0900596 if (values_.size() != static_cast<size_t>(size)) {
Jooyung Han0cc99632021-11-30 17:19:05 +0900597 AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with "
598 << values_.size() << " elements";
599 err = -1;
600 break;
601 }
602 }
Jooyung Hanaeb01672021-11-30 17:29:22 +0900603 return decorator(type, value_strings);
Will McVickerefd970d2019-09-25 15:28:30 -0700604 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700605 case Type::FLOATING: {
Will McVickerefd970d2019-09-25 15:28:30 -0700606 if (type_string == "double") {
607 double parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900608 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700609 AIDL_ERROR(this) << "Could not parse " << value_;
610 err = -1;
611 break;
612 }
Will McVickerefd970d2019-09-25 15:28:30 -0700613 return decorator(type, std::to_string(parsed_value));
614 }
Jooyung Han535c5e82020-12-29 15:16:59 +0900615 if (type_string == "float") {
Will McVickerefd970d2019-09-25 15:28:30 -0700616 float parsed_value;
Jooyung Han535c5e82020-12-29 15:16:59 +0900617 if (!ParseFloating(value_, &parsed_value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700618 AIDL_ERROR(this) << "Could not parse " << value_;
619 err = -1;
620 break;
621 }
Will McVickerefd970d2019-09-25 15:28:30 -0700622 return decorator(type, std::to_string(parsed_value) + "f");
623 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700624 err = -1;
625 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700626 }
Will McVickerefd970d2019-09-25 15:28:30 -0700627 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700628 err = -1;
629 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700630 }
631
Steven Moreland21780812020-09-11 01:29:45 +0000632 AIDL_FATAL_IF(err == 0, this);
Steven Morelandb7d58652021-10-25 15:10:02 -0700633 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string
634 << " (" << value_ << ")";
Will McVickerefd970d2019-09-25 15:28:30 -0700635 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700636}
637
638bool AidlConstantValue::CheckValid() const {
639 // Nothing needs to be checked here. The constant value will be validated in
640 // the constructor or in the evaluate() function.
641 if (is_evaluated_) return is_valid_;
642
643 switch (type_) {
644 case Type::BOOLEAN: // fall-through
645 case Type::INT8: // fall-through
646 case Type::INT32: // fall-through
647 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700648 case Type::CHARACTER: // fall-through
649 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900650 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700651 case Type::FLOATING: // fall-through
652 case Type::UNARY: // fall-through
653 case Type::BINARY:
654 is_valid_ = true;
655 break;
Jooyung Han29813842020-12-08 01:28:03 +0900656 case Type::ARRAY:
657 is_valid_ = true;
658 for (const auto& v : values_) is_valid_ &= v->CheckValid();
659 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800660 case Type::ERROR:
661 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700662 default:
663 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
664 return false;
665 }
666
667 return true;
668}
669
Devin Moore2a45d0a2022-01-21 22:58:52 +0000670bool AidlConstantValue::Evaluate() const {
671 if (CheckValid()) {
672 return evaluate();
673 } else {
674 return false;
675 }
676}
677
Jooyung Han74675c22020-12-15 08:39:57 +0900678bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700679 if (is_evaluated_) {
680 return is_valid_;
681 }
682 int err = 0;
683 is_evaluated_ = true;
684
685 switch (type_) {
686 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700687 Type array_type = Type::ERROR;
688 bool success = true;
689 for (const auto& value : values_) {
690 success = value->CheckValid();
691 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900692 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700693 if (!success) {
694 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
695 break;
696 }
697 if (array_type == Type::ERROR) {
698 array_type = value->final_type_;
699 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
700 value->final_type_)) {
701 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
702 << ". Expecting type compatible with " << ToString(array_type);
703 success = false;
704 break;
705 }
706 } else {
707 break;
708 }
709 }
710 if (!success) {
711 err = -1;
712 break;
713 }
714 final_type_ = type_;
715 break;
716 }
717 case Type::BOOLEAN:
718 if ((value_ != "true") && (value_ != "false")) {
719 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
720 err = -1;
721 break;
722 }
723 final_value_ = (value_ == "true") ? 1 : 0;
724 final_type_ = type_;
725 break;
726 case Type::INT8: // fall-through
727 case Type::INT32: // fall-through
728 case Type::INT64:
729 // Parsing happens in the constructor
730 final_type_ = type_;
731 break;
732 case Type::CHARACTER: // fall-through
733 case Type::STRING:
734 final_string_value_ = value_;
735 final_type_ = type_;
736 break;
737 case Type::FLOATING:
738 // Just parse on the fly in ValueString
739 final_type_ = type_;
740 break;
741 default:
742 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
743 err = -1;
744 }
745
746 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700747}
748
749string AidlConstantValue::ToString(Type type) {
750 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700751 case Type::BOOLEAN:
752 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700753 case Type::INT8:
754 return "an int8 literal";
755 case Type::INT32:
756 return "an int32 literal";
757 case Type::INT64:
758 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800759 case Type::ARRAY:
760 return "a literal array";
761 case Type::CHARACTER:
762 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700763 case Type::STRING:
764 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900765 case Type::REF:
766 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800767 case Type::FLOATING:
768 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700769 case Type::UNARY:
770 return "a unary expression";
771 case Type::BINARY:
772 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800773 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000774 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800775 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700776 default:
Steven Moreland21780812020-09-11 01:29:45 +0000777 AIDL_FATAL(AIDL_LOCATION_HERE)
778 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700779 return ""; // not reached
780 }
781}
782
Jooyung Hand0c8af02021-01-06 18:08:01 +0900783AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value)
784 : AidlConstantValue(location, Type::REF, value) {
Jooyung Han690f5842020-12-04 13:02:04 +0900785 const auto pos = value.find_last_of('.');
786 if (pos == string::npos) {
787 field_name_ = value;
788 } else {
Jooyung Han9fafb8d2021-11-30 13:19:33 +0900789 ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos),
790 /*array=*/std::nullopt, /*type_params=*/nullptr,
Jooyung Han8451a202021-01-16 03:07:06 +0900791 Comments{});
Jooyung Han690f5842020-12-04 13:02:04 +0900792 field_name_ = value.substr(pos + 1);
793 }
794}
795
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900796const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
Jooyung Han29813842020-12-08 01:28:03 +0900797 if (resolved_) return resolved_;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900798
799 const AidlDefinedType* defined_type;
800 if (ref_type_) {
801 defined_type = ref_type_->GetDefinedType();
802 } else {
803 defined_type = scope;
804 }
805
806 if (!defined_type) {
Jooyung Han690f5842020-12-04 13:02:04 +0900807 // This can happen when "const reference" is used in an unsupported way,
808 // but missed in checks there. It works as a safety net.
809 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900810 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900811 }
812
Jooyung Han690f5842020-12-04 13:02:04 +0900813 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
814 for (const auto& e : enum_decl->GetEnumerators()) {
815 if (e->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900816 return resolved_ = e->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900817 }
818 }
819 } else {
820 for (const auto& c : defined_type->GetConstantDeclarations()) {
821 if (c->GetName() == field_name_) {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900822 return resolved_ = &c->GetValue();
Jooyung Han690f5842020-12-04 13:02:04 +0900823 }
824 }
825 }
Jooyung Hane9f5b272021-01-07 00:18:11 +0900826 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900827 return nullptr;
828}
829
830bool AidlConstantReference::CheckValid() const {
831 if (is_evaluated_) return is_valid_;
832 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
833 is_valid_ = resolved_->CheckValid();
834 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900835}
836
Jooyung Han74675c22020-12-15 08:39:57 +0900837bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900838 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900839 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
840 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900841
Jooyung Han74675c22020-12-15 08:39:57 +0900842 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900843 is_valid_ = resolved_->is_valid_;
844 final_type_ = resolved_->final_type_;
845 if (is_valid_) {
846 if (final_type_ == Type::STRING) {
847 final_string_value_ = resolved_->final_string_value_;
848 } else {
849 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900850 }
851 }
Jooyung Han29813842020-12-08 01:28:03 +0900852 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900853}
854
Will McVickerd7d18df2019-09-12 13:40:50 -0700855bool AidlUnaryConstExpression::CheckValid() const {
856 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000857 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700858
859 is_valid_ = unary_->CheckValid();
860 if (!is_valid_) {
861 final_type_ = Type::ERROR;
862 return false;
863 }
864
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800865 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700866}
867
Jooyung Han74675c22020-12-15 08:39:57 +0900868bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700869 if (is_evaluated_) {
870 return is_valid_;
871 }
872 is_evaluated_ = true;
873
874 // Recursively evaluate the expression tree
875 if (!unary_->is_evaluated_) {
876 // TODO(b/142722772) CheckValid() should be called before ValueString()
877 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900878 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700879 if (!success) {
880 is_valid_ = false;
881 return false;
882 }
883 }
Devin Moorec233fb82020-04-07 11:13:44 -0700884 if (!IsCompatibleType(unary_->final_type_, op_)) {
885 AIDL_ERROR(unary_) << "'" << op_ << "'"
886 << " is not compatible with " << ToString(unary_->final_type_)
887 << ": " + value_;
888 is_valid_ = false;
889 return false;
890 }
891 if (!unary_->is_valid_) {
892 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700893 is_valid_ = false;
894 return false;
895 }
896 final_type_ = unary_->final_type_;
897
898 if (final_type_ == Type::FLOATING) {
899 // don't do anything here. ValueString() will handle everything.
900 is_valid_ = true;
901 return true;
902 }
903
Steven Morelande1ff67e2020-07-16 23:22:36 +0000904#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800905 return is_valid_ = \
906 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700907
908 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
909 is_valid_ = false; return false;)
910}
911
Will McVickerd7d18df2019-09-12 13:40:50 -0700912bool AidlBinaryConstExpression::CheckValid() const {
913 bool success = false;
914 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000915 AIDL_FATAL_IF(left_val_ == nullptr, this);
916 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700917
918 success = left_val_->CheckValid();
919 if (!success) {
920 final_type_ = Type::ERROR;
921 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
922 }
923
924 success = right_val_->CheckValid();
925 if (!success) {
926 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
927 final_type_ = Type::ERROR;
928 }
929
930 if (final_type_ == Type::ERROR) {
931 is_valid_ = false;
932 return false;
933 }
934
935 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800936 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700937}
938
Jooyung Han74675c22020-12-15 08:39:57 +0900939bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700940 if (is_evaluated_) {
941 return is_valid_;
942 }
943 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900944 AIDL_FATAL_IF(left_val_ == nullptr, this);
945 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700946
947 // Recursively evaluate the binary expression tree
948 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
949 // TODO(b/142722772) CheckValid() should be called before ValueString()
950 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900951 success &= left_val_->evaluate();
952 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700953 if (!success) {
954 is_valid_ = false;
955 return false;
956 }
957 }
958 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
959 is_valid_ = false;
960 return false;
961 }
962 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
963 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000964 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
965 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
966 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700967 return false;
968 }
969
970 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
971
972 // Handle String case first
973 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000974 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700975 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000976 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700977 final_type_ = Type::ERROR;
978 is_valid_ = false;
979 return false;
980 }
981
982 // Remove trailing " from lhs
983 const string& lhs = left_val_->final_string_value_;
984 if (lhs.back() != '"') {
985 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
986 final_type_ = Type::ERROR;
987 is_valid_ = false;
988 return false;
989 }
990 const string& rhs = right_val_->final_string_value_;
991 // Remove starting " from rhs
992 if (rhs.front() != '"') {
993 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
994 final_type_ = Type::ERROR;
995 is_valid_ = false;
996 return false;
997 }
998
999 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
1000 final_type_ = Type::STRING;
1001 return true;
1002 }
1003
Will McVickerd7d18df2019-09-12 13:40:50 -07001004 // CASE: + - * / % | ^ & < > <= >= == !=
1005 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -07001006 // promoted kind for both operands.
1007 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1008 IntegralPromotion(right_val_->final_type_));
1009 // result kind.
1010 final_type_ = isArithmeticOrBitflip
1011 ? promoted // arithmetic or bitflip operators generates promoted type
1012 : Type::BOOLEAN; // comparison operators generates bool
1013
Devin Moore1f0360d2020-12-21 12:12:48 -08001014#define CASE_BINARY_COMMON(__type__) \
1015 return is_valid_ = \
1016 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1017 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001018
1019 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1020 is_valid_ = false; return false;)
1021 }
1022
1023 // CASE: << >>
1024 string newOp = op_;
1025 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001026 // promoted kind for both operands.
1027 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1028 IntegralPromotion(right_val_->final_type_));
1029 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001030 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001031 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001032 // is defined as shifting into the other direction.
1033 newOp = OPEQ("<<") ? ">>" : "<<";
1034 numBits = -numBits;
1035 }
1036
Devin Moore1f0360d2020-12-21 12:12:48 -08001037#define CASE_SHIFT(__type__) \
1038 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1039 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001040
1041 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1042 is_valid_ = false; return false;)
1043 }
1044
1045 // CASE: && ||
1046 if (OP_IS_BIN_LOGICAL) {
1047 final_type_ = Type::BOOLEAN;
1048 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001049 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1050 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001051 }
1052
1053 SHOULD_NOT_REACH();
1054 is_valid_ = false;
1055 return false;
1056}
1057
Will McVickerd7d18df2019-09-12 13:40:50 -07001058AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1059 int64_t parsed_value, const string& checked_value)
1060 : AidlNode(location),
1061 type_(parsed_type),
1062 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001063 final_type_(parsed_type),
1064 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001065 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1066 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001067}
Will McVickerefd970d2019-09-25 15:28:30 -07001068
1069AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001070 const string& checked_value)
1071 : AidlNode(location),
1072 type_(type),
1073 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001074 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001075 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001076 switch (type_) {
1077 case Type::INT8:
1078 case Type::INT32:
1079 case Type::INT64:
1080 case Type::ARRAY:
1081 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1082 break;
1083 default:
1084 break;
1085 }
1086}
1087
1088AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001089 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1090 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001091 : AidlNode(location),
1092 type_(type),
1093 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001094 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001095 is_valid_(false),
1096 is_evaluated_(false),
1097 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001098 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001099}
1100
1101AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1102 std::unique_ptr<AidlConstantValue> rval)
1103 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1104 unary_(std::move(rval)),
1105 op_(op) {
1106 final_type_ = Type::UNARY;
1107}
1108
1109AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1110 std::unique_ptr<AidlConstantValue> lval,
1111 const string& op,
1112 std::unique_ptr<AidlConstantValue> rval)
1113 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1114 left_val_(std::move(lval)),
1115 right_val_(std::move(rval)),
1116 op_(op) {
1117 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001118}