blob: e2c2c1bf870c1d5175bef4cf49bb59e6517a6656 [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 Han71a1b582020-12-25 23:58:41 +0900251// helper to see if T is the same to one of Args types.
252template <typename T, typename... Args>
253struct is_one_of : std::false_type {};
254
255template <typename T, typename S, typename... Args>
256struct is_one_of<T, S, Args...> {
257 enum { value = std::is_same_v<T, S> || is_one_of<T, Args...>::value };
258};
259
260// helper to see if T is std::vector of something.
261template <typename>
262struct is_vector : std::false_type {};
263
264template <typename T>
265struct is_vector<std::vector<T>> : std::true_type {};
266
267template <typename T>
268static bool ParseFloating(std::string_view sv, T* parsed) {
269 static_assert(is_one_of<T, float, double>::value);
270 android::base::ConsumeSuffix(&sv, "f");
271 if constexpr (std::is_same_v<T, float>) {
272 return android::base::ParseFloat(sv.data(), parsed);
273 } else {
274 return android::base::ParseDouble(sv.data(), parsed);
275 }
276}
277
Will McVickerd7d18df2019-09-12 13:40:50 -0700278bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
279 // Verify the unary type here
280 switch (type) {
281 case Type::BOOLEAN: // fall-through
282 case Type::INT8: // fall-through
283 case Type::INT32: // fall-through
284 case Type::INT64:
285 return true;
286 case Type::FLOATING:
287 return (op == "+" || op == "-");
288 default:
289 return false;
290 }
291}
292
293bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
294 switch (t1) {
295 case Type::STRING:
296 if (t2 == Type::STRING) {
297 return true;
298 }
299 break;
300 case Type::BOOLEAN: // fall-through
301 case Type::INT8: // fall-through
302 case Type::INT32: // fall-through
303 case Type::INT64:
304 switch (t2) {
305 case Type::BOOLEAN: // fall-through
306 case Type::INT8: // fall-through
307 case Type::INT32: // fall-through
308 case Type::INT64:
309 return true;
310 break;
311 default:
312 break;
313 }
314 break;
315 default:
316 break;
317 }
318
319 return false;
320}
321
322// Returns the promoted kind for both operands
323AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
324 Type right) {
325 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000326 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
327 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700328
329 // Kinds in concern: bool, (u)int[8|32|64]
330 if (left == right) return left; // easy case
331 if (left == Type::BOOLEAN) return right;
332 if (right == Type::BOOLEAN) return left;
333
334 return left < right ? right : left;
335}
336
337// Returns the promoted integral type where INT32 is the smallest type
338AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
339 return (Type::INT32 < in) ? in : Type::INT32;
340}
341
342template <typename T>
Jooyung Han71a1b582020-12-25 23:58:41 +0900343T AidlConstantValue::Cast() const {
344 static_assert(
345 is_one_of<T, bool, char, int8_t, int32_t, int64_t, std::string, float, double>::value ||
346 is_vector<T>::value);
Will McVickerd7d18df2019-09-12 13:40:50 -0700347
Jooyung Han71a1b582020-12-25 23:58:41 +0900348 is_evaluated_ || (CheckValid() && evaluate());
349 AIDL_FATAL_IF(!is_valid_, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700350
Jooyung Han71a1b582020-12-25 23:58:41 +0900351 if constexpr (is_vector<T>::value) {
352 AIDL_FATAL_IF(final_type_ != Type::ARRAY, this);
353 T result;
354 for (const auto& v : values_) {
355 result.push_back(v->Cast<typename T::value_type>());
356 }
357 return result;
358
359 } else if constexpr (is_one_of<T, float, double>::value) {
360 AIDL_FATAL_IF(final_type_ != Type::FLOATING, this);
361 T result;
362 AIDL_FATAL_IF(!ParseFloating(value_, &result), this);
363 return result;
364 } else if constexpr (std::is_same<T, std::string>::value) {
365 AIDL_FATAL_IF(final_type_ != Type::STRING, this);
366 return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote "
367 } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
368 AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
369 return static_cast<T>(final_value_);
370 } else if constexpr (std::is_same<T, char>::value) {
371 AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
372 return final_string_value_.at(1); // unquote '
373 } else if constexpr (std::is_same<T, bool>::value) {
374 AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this);
375 return final_value_ != 0;
376 }
377 SHOULD_NOT_REACH();
Will McVickerd7d18df2019-09-12 13:40:50 -0700378}
379
Jooyung Han71a1b582020-12-25 23:58:41 +0900380#define INSTANTIATE_AidlConstantValue_Cast(T) \
381 template T AidlConstantValue::Cast<T>() const; \
382 template std::vector<T> AidlConstantValue::Cast<std::vector<T>>() const
383
384INSTANTIATE_AidlConstantValue_Cast(int8_t);
385INSTANTIATE_AidlConstantValue_Cast(int32_t);
386INSTANTIATE_AidlConstantValue_Cast(int64_t);
387INSTANTIATE_AidlConstantValue_Cast(float);
388INSTANTIATE_AidlConstantValue_Cast(double);
389INSTANTIATE_AidlConstantValue_Cast(char);
390INSTANTIATE_AidlConstantValue_Cast(bool);
391INSTANTIATE_AidlConstantValue_Cast(std::string);
392#undef INSTANTIATE_AidlConstantValue_Cast
393
Steven Moreland541788d2020-05-21 22:05:52 +0000394AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
395 AidlLocation location = specifier.GetLocation();
396
397 // allocation of int[0] is a bit wasteful in Java
398 if (specifier.IsArray()) {
399 return nullptr;
400 }
401
402 const std::string name = specifier.GetName();
403 if (name == "boolean") {
404 return Boolean(location, false);
405 }
406 if (name == "byte" || name == "int" || name == "long") {
407 return Integral(location, "0");
408 }
409 if (name == "float") {
410 return Floating(location, "0.0f");
411 }
412 if (name == "double") {
413 return Floating(location, "0.0");
414 }
415 return nullptr;
416}
417
Will McVickerefd970d2019-09-25 15:28:30 -0700418AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
419 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
420}
421
422AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800423 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700424 if (!isValidLiteralChar(value)) {
425 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800426 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700427 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800428 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700429}
430
431AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
432 const std::string& value) {
433 return new AidlConstantValue(location, Type::FLOATING, value);
434}
435
Will McVickerd7d18df2019-09-12 13:40:50 -0700436bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000437 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700438}
439
Will McVickerd7d18df2019-09-12 13:40:50 -0700440bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
441 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 if (parsed_value == nullptr || parsed_type == nullptr) {
443 return false;
444 }
445
Steven Morelandcef22662020-07-08 20:54:28 +0000446 const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L');
447 const std::string value_substr = isLong ? value.substr(0, value.size() - 1) : value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700448
Steven Morelandcef22662020-07-08 20:54:28 +0000449 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700450 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
451 // handle that when computing constant expressions, then we need to
452 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
453 // so we parse as an unsigned int when possible and then cast to a signed
454 // int. One example of this is in ICameraService.aidl where a constant int
455 // is used for bit manipulations which ideally should be handled with an
456 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000457 //
458 // Note, for historical consistency, we need to consider small hex values
459 // as an integral type. Recognizing them as INT8 could break some files,
460 // even though it would simplify this code.
461 if (uint32_t rawValue32;
462 !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700463 *parsed_value = static_cast<int32_t>(rawValue32);
464 *parsed_type = Type::INT32;
Steven Morelandcef22662020-07-08 20:54:28 +0000465 } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) {
466 *parsed_value = static_cast<int64_t>(rawValue64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700467 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000468 } else {
469 *parsed_value = 0;
470 *parsed_type = Type::ERROR;
471 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700472 }
473 return true;
474 }
475
Steven Morelandcef22662020-07-08 20:54:28 +0000476 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
477 *parsed_value = 0;
Will McVickerd7d18df2019-09-12 13:40:50 -0700478 *parsed_type = Type::ERROR;
479 return false;
480 }
481
Steven Morelandcef22662020-07-08 20:54:28 +0000482 if (isLong) {
483 *parsed_type = Type::INT64;
484 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700485 // guess literal type.
486 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
487 *parsed_type = Type::INT8;
488 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
489 *parsed_type = Type::INT32;
490 } else {
491 *parsed_type = Type::INT64;
492 }
493 }
494 return true;
495}
496
497AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000498 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700499
500 Type parsed_type;
501 int64_t parsed_value = 0;
502 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
503 if (!success) {
504 return nullptr;
505 }
506
507 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700508}
509
510AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700511 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000512 AIDL_FATAL_IF(values == nullptr, location);
Jooyung Han29813842020-12-08 01:28:03 +0900513 std::vector<std::string> str_values;
514 for (const auto& v : *values) {
515 str_values.push_back(v->value_);
516 }
517 return new AidlConstantValue(location, Type::ARRAY, std::move(values), Join(str_values, ", "));
Will McVickerefd970d2019-09-25 15:28:30 -0700518}
519
Will McVickerd7d18df2019-09-12 13:40:50 -0700520AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700521 for (size_t i = 0; i < value.length(); ++i) {
522 if (!isValidLiteralChar(value[i])) {
523 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
524 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800525 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700526 }
527 }
528
529 return new AidlConstantValue(location, Type::STRING, value);
530}
531
Will McVickerd7d18df2019-09-12 13:40:50 -0700532string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
533 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700534 if (type.IsGeneric()) {
535 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
536 return "";
537 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 if (!is_evaluated_) {
539 // TODO(b/142722772) CheckValid() should be called before ValueString()
540 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900541 success &= evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700542 if (!success) {
543 // the detailed error message shall be printed in evaluate
544 return "";
545 }
Will McVickerefd970d2019-09-25 15:28:30 -0700546 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700547 if (!is_valid_) {
548 AIDL_ERROR(this) << "Invalid constant value: " + value_;
549 return "";
550 }
Jooyung Han690f5842020-12-04 13:02:04 +0900551
552 const AidlDefinedType* defined_type = type.GetDefinedType();
553 if (defined_type && !type.IsArray()) {
554 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
555 if (!enum_type) {
556 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900557 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900558 return "";
559 }
560 if (type_ != Type::REF) {
561 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
562 << enum_type->GetCanonicalName();
563 return "";
564 }
565 return decorator(type, value_);
566 }
567
Will McVickerd7d18df2019-09-12 13:40:50 -0700568 const string& type_string = type.GetName();
569 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700570
Will McVickerd7d18df2019-09-12 13:40:50 -0700571 switch (final_type_) {
572 case Type::CHARACTER:
573 if (type_string == "char") {
574 return decorator(type, final_string_value_);
575 }
576 err = -1;
577 break;
578 case Type::STRING:
579 if (type_string == "String") {
580 return decorator(type, final_string_value_);
581 }
582 err = -1;
583 break;
584 case Type::BOOLEAN: // fall-through
585 case Type::INT8: // fall-through
586 case Type::INT32: // fall-through
587 case Type::INT64:
588 if (type_string == "byte") {
589 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
590 err = -1;
591 break;
592 }
593 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
594 } else if (type_string == "int") {
595 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
596 err = -1;
597 break;
598 }
599 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
600 } else if (type_string == "long") {
601 return decorator(type, std::to_string(final_value_));
602 } else if (type_string == "boolean") {
603 return decorator(type, final_value_ ? "true" : "false");
604 }
605 err = -1;
606 break;
607 case Type::ARRAY: {
608 if (!type.IsArray()) {
609 err = -1;
610 break;
611 }
612 vector<string> value_strings;
613 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700614 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700615
Will McVickerefd970d2019-09-25 15:28:30 -0700616 for (const auto& value : values_) {
617 const AidlTypeSpecifier& array_base = type.ArrayBase();
Will McVickerd7d18df2019-09-12 13:40:50 -0700618 const string value_string = value->ValueString(array_base, decorator);
619 if (value_string.empty()) {
620 success = false;
621 break;
622 }
623 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700624 }
625 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700626 err = -1;
627 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700628 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700629
630 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700631 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700632 case Type::FLOATING: {
633 std::string_view raw_view(value_.c_str());
634 bool is_float_literal = ConsumeSuffix(&raw_view, "f");
635 std::string stripped_value = std::string(raw_view);
Will McVickerefd970d2019-09-25 15:28:30 -0700636
637 if (type_string == "double") {
638 double parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700639 if (!android::base::ParseDouble(stripped_value, &parsed_value)) {
640 AIDL_ERROR(this) << "Could not parse " << value_;
641 err = -1;
642 break;
643 }
Will McVickerefd970d2019-09-25 15:28:30 -0700644 return decorator(type, std::to_string(parsed_value));
645 }
646 if (is_float_literal && type_string == "float") {
647 float parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700648 if (!android::base::ParseFloat(stripped_value, &parsed_value)) {
649 AIDL_ERROR(this) << "Could not parse " << value_;
650 err = -1;
651 break;
652 }
Will McVickerefd970d2019-09-25 15:28:30 -0700653 return decorator(type, std::to_string(parsed_value) + "f");
654 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700655 err = -1;
656 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700657 }
Will McVickerefd970d2019-09-25 15:28:30 -0700658 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700659 err = -1;
660 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700661 }
662
Steven Moreland21780812020-09-11 01:29:45 +0000663 AIDL_FATAL_IF(err == 0, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700664 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string;
Will McVickerefd970d2019-09-25 15:28:30 -0700665 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700666}
667
668bool AidlConstantValue::CheckValid() const {
669 // Nothing needs to be checked here. The constant value will be validated in
670 // the constructor or in the evaluate() function.
671 if (is_evaluated_) return is_valid_;
672
673 switch (type_) {
674 case Type::BOOLEAN: // fall-through
675 case Type::INT8: // fall-through
676 case Type::INT32: // fall-through
677 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700678 case Type::CHARACTER: // fall-through
679 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900680 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700681 case Type::FLOATING: // fall-through
682 case Type::UNARY: // fall-through
683 case Type::BINARY:
684 is_valid_ = true;
685 break;
Jooyung Han29813842020-12-08 01:28:03 +0900686 case Type::ARRAY:
687 is_valid_ = true;
688 for (const auto& v : values_) is_valid_ &= v->CheckValid();
689 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800690 case Type::ERROR:
691 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700692 default:
693 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
694 return false;
695 }
696
697 return true;
698}
699
Jooyung Han74675c22020-12-15 08:39:57 +0900700bool AidlConstantValue::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700701 if (is_evaluated_) {
702 return is_valid_;
703 }
704 int err = 0;
705 is_evaluated_ = true;
706
707 switch (type_) {
708 case Type::ARRAY: {
Will McVickerd7d18df2019-09-12 13:40:50 -0700709 Type array_type = Type::ERROR;
710 bool success = true;
711 for (const auto& value : values_) {
712 success = value->CheckValid();
713 if (success) {
Jooyung Han74675c22020-12-15 08:39:57 +0900714 success = value->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700715 if (!success) {
716 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
717 break;
718 }
719 if (array_type == Type::ERROR) {
720 array_type = value->final_type_;
721 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
722 value->final_type_)) {
723 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
724 << ". Expecting type compatible with " << ToString(array_type);
725 success = false;
726 break;
727 }
728 } else {
729 break;
730 }
731 }
732 if (!success) {
733 err = -1;
734 break;
735 }
736 final_type_ = type_;
737 break;
738 }
739 case Type::BOOLEAN:
740 if ((value_ != "true") && (value_ != "false")) {
741 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
742 err = -1;
743 break;
744 }
745 final_value_ = (value_ == "true") ? 1 : 0;
746 final_type_ = type_;
747 break;
748 case Type::INT8: // fall-through
749 case Type::INT32: // fall-through
750 case Type::INT64:
751 // Parsing happens in the constructor
752 final_type_ = type_;
753 break;
754 case Type::CHARACTER: // fall-through
755 case Type::STRING:
756 final_string_value_ = value_;
757 final_type_ = type_;
758 break;
759 case Type::FLOATING:
760 // Just parse on the fly in ValueString
761 final_type_ = type_;
762 break;
763 default:
764 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
765 err = -1;
766 }
767
768 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700769}
770
771string AidlConstantValue::ToString(Type type) {
772 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700773 case Type::BOOLEAN:
774 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700775 case Type::INT8:
776 return "an int8 literal";
777 case Type::INT32:
778 return "an int32 literal";
779 case Type::INT64:
780 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800781 case Type::ARRAY:
782 return "a literal array";
783 case Type::CHARACTER:
784 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700785 case Type::STRING:
786 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900787 case Type::REF:
788 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800789 case Type::FLOATING:
790 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700791 case Type::UNARY:
792 return "a unary expression";
793 case Type::BINARY:
794 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800795 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000796 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800797 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700798 default:
Steven Moreland21780812020-09-11 01:29:45 +0000799 AIDL_FATAL(AIDL_LOCATION_HERE)
800 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700801 return ""; // not reached
802 }
803}
804
Jooyung Han690f5842020-12-04 13:02:04 +0900805AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value,
806 const std::string& comments)
807 : AidlConstantValue(location, Type::REF, value), comments_(comments) {
808 const auto pos = value.find_last_of('.');
809 if (pos == string::npos) {
810 field_name_ = value;
811 } else {
812 ref_type_ =
813 std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), false, nullptr, "");
814 field_name_ = value.substr(pos + 1);
815 }
816}
817
Jooyung Han29813842020-12-08 01:28:03 +0900818const AidlConstantValue* AidlConstantReference::Resolve() {
819 if (resolved_) return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900820 if (!GetRefType() || !GetRefType()->GetDefinedType()) {
821 // This can happen when "const reference" is used in an unsupported way,
822 // but missed in checks there. It works as a safety net.
823 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900824 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900825 }
826
827 auto defined_type = GetRefType()->GetDefinedType();
828 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
829 for (const auto& e : enum_decl->GetEnumerators()) {
830 if (e->GetName() == field_name_) {
Jooyung Han29813842020-12-08 01:28:03 +0900831 resolved_ = e->GetValue();
832 return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900833 }
834 }
835 } else {
836 for (const auto& c : defined_type->GetConstantDeclarations()) {
837 if (c->GetName() == field_name_) {
Jooyung Han29813842020-12-08 01:28:03 +0900838 resolved_ = &c->GetValue();
839 return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900840 }
841 }
842 }
843 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << ref_type_->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900844 return nullptr;
845}
846
847bool AidlConstantReference::CheckValid() const {
848 if (is_evaluated_) return is_valid_;
849 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
850 is_valid_ = resolved_->CheckValid();
851 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900852}
853
Jooyung Han74675c22020-12-15 08:39:57 +0900854bool AidlConstantReference::evaluate() const {
Jooyung Han690f5842020-12-04 13:02:04 +0900855 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900856 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
857 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900858
Jooyung Han74675c22020-12-15 08:39:57 +0900859 resolved_->evaluate();
Jooyung Han29813842020-12-08 01:28:03 +0900860 is_valid_ = resolved_->is_valid_;
861 final_type_ = resolved_->final_type_;
862 if (is_valid_) {
863 if (final_type_ == Type::STRING) {
864 final_string_value_ = resolved_->final_string_value_;
865 } else {
866 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900867 }
868 }
Jooyung Han29813842020-12-08 01:28:03 +0900869 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900870}
871
Will McVickerd7d18df2019-09-12 13:40:50 -0700872bool AidlUnaryConstExpression::CheckValid() const {
873 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000874 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700875
876 is_valid_ = unary_->CheckValid();
877 if (!is_valid_) {
878 final_type_ = Type::ERROR;
879 return false;
880 }
881
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800882 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700883}
884
Jooyung Han74675c22020-12-15 08:39:57 +0900885bool AidlUnaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700886 if (is_evaluated_) {
887 return is_valid_;
888 }
889 is_evaluated_ = true;
890
891 // Recursively evaluate the expression tree
892 if (!unary_->is_evaluated_) {
893 // TODO(b/142722772) CheckValid() should be called before ValueString()
894 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900895 success &= unary_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700896 if (!success) {
897 is_valid_ = false;
898 return false;
899 }
900 }
Devin Moorec233fb82020-04-07 11:13:44 -0700901 if (!IsCompatibleType(unary_->final_type_, op_)) {
902 AIDL_ERROR(unary_) << "'" << op_ << "'"
903 << " is not compatible with " << ToString(unary_->final_type_)
904 << ": " + value_;
905 is_valid_ = false;
906 return false;
907 }
908 if (!unary_->is_valid_) {
909 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700910 is_valid_ = false;
911 return false;
912 }
913 final_type_ = unary_->final_type_;
914
915 if (final_type_ == Type::FLOATING) {
916 // don't do anything here. ValueString() will handle everything.
917 is_valid_ = true;
918 return true;
919 }
920
Steven Morelande1ff67e2020-07-16 23:22:36 +0000921#define CASE_UNARY(__type__) \
Devin Moore1f0360d2020-12-21 12:12:48 -0800922 return is_valid_ = \
923 handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700924
925 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
926 is_valid_ = false; return false;)
927}
928
Will McVickerd7d18df2019-09-12 13:40:50 -0700929bool AidlBinaryConstExpression::CheckValid() const {
930 bool success = false;
931 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000932 AIDL_FATAL_IF(left_val_ == nullptr, this);
933 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700934
935 success = left_val_->CheckValid();
936 if (!success) {
937 final_type_ = Type::ERROR;
938 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
939 }
940
941 success = right_val_->CheckValid();
942 if (!success) {
943 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
944 final_type_ = Type::ERROR;
945 }
946
947 if (final_type_ == Type::ERROR) {
948 is_valid_ = false;
949 return false;
950 }
951
952 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800953 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700954}
955
Jooyung Han74675c22020-12-15 08:39:57 +0900956bool AidlBinaryConstExpression::evaluate() const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700957 if (is_evaluated_) {
958 return is_valid_;
959 }
960 is_evaluated_ = true;
Jooyung Han74675c22020-12-15 08:39:57 +0900961 AIDL_FATAL_IF(left_val_ == nullptr, this);
962 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700963
964 // Recursively evaluate the binary expression tree
965 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
966 // TODO(b/142722772) CheckValid() should be called before ValueString()
967 bool success = CheckValid();
Jooyung Han74675c22020-12-15 08:39:57 +0900968 success &= left_val_->evaluate();
969 success &= right_val_->evaluate();
Will McVickerd7d18df2019-09-12 13:40:50 -0700970 if (!success) {
971 is_valid_ = false;
972 return false;
973 }
974 }
975 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
976 is_valid_ = false;
977 return false;
978 }
979 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
980 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000981 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
982 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
983 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700984 return false;
985 }
986
987 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
988
989 // Handle String case first
990 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000991 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700992 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000993 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700994 final_type_ = Type::ERROR;
995 is_valid_ = false;
996 return false;
997 }
998
999 // Remove trailing " from lhs
1000 const string& lhs = left_val_->final_string_value_;
1001 if (lhs.back() != '"') {
1002 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
1003 final_type_ = Type::ERROR;
1004 is_valid_ = false;
1005 return false;
1006 }
1007 const string& rhs = right_val_->final_string_value_;
1008 // Remove starting " from rhs
1009 if (rhs.front() != '"') {
1010 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
1011 final_type_ = Type::ERROR;
1012 is_valid_ = false;
1013 return false;
1014 }
1015
1016 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
1017 final_type_ = Type::STRING;
1018 return true;
1019 }
1020
Will McVickerd7d18df2019-09-12 13:40:50 -07001021 // CASE: + - * / % | ^ & < > <= >= == !=
1022 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -07001023 // promoted kind for both operands.
1024 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1025 IntegralPromotion(right_val_->final_type_));
1026 // result kind.
1027 final_type_ = isArithmeticOrBitflip
1028 ? promoted // arithmetic or bitflip operators generates promoted type
1029 : Type::BOOLEAN; // comparison operators generates bool
1030
Devin Moore1f0360d2020-12-21 12:12:48 -08001031#define CASE_BINARY_COMMON(__type__) \
1032 return is_valid_ = \
1033 handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1034 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001035
1036 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1037 is_valid_ = false; return false;)
1038 }
1039
1040 // CASE: << >>
1041 string newOp = op_;
1042 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001043 // promoted kind for both operands.
1044 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1045 IntegralPromotion(right_val_->final_type_));
1046 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001047 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001048 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001049 // is defined as shifting into the other direction.
1050 newOp = OPEQ("<<") ? ">>" : "<<";
1051 numBits = -numBits;
1052 }
1053
Devin Moore1f0360d2020-12-21 12:12:48 -08001054#define CASE_SHIFT(__type__) \
1055 return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1056 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001057
1058 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1059 is_valid_ = false; return false;)
1060 }
1061
1062 // CASE: && ||
1063 if (OP_IS_BIN_LOGICAL) {
1064 final_type_ = Type::BOOLEAN;
1065 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001066 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1067 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001068 }
1069
1070 SHOULD_NOT_REACH();
1071 is_valid_ = false;
1072 return false;
1073}
1074
Will McVickerd7d18df2019-09-12 13:40:50 -07001075AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1076 int64_t parsed_value, const string& checked_value)
1077 : AidlNode(location),
1078 type_(parsed_type),
1079 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001080 final_type_(parsed_type),
1081 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001082 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1083 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001084}
Will McVickerefd970d2019-09-25 15:28:30 -07001085
1086AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001087 const string& checked_value)
1088 : AidlNode(location),
1089 type_(type),
1090 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001091 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001092 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001093 switch (type_) {
1094 case Type::INT8:
1095 case Type::INT32:
1096 case Type::INT64:
1097 case Type::ARRAY:
1098 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1099 break;
1100 default:
1101 break;
1102 }
1103}
1104
1105AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001106 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1107 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001108 : AidlNode(location),
1109 type_(type),
1110 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001111 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001112 is_valid_(false),
1113 is_evaluated_(false),
1114 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001115 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001116}
1117
1118AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1119 std::unique_ptr<AidlConstantValue> rval)
1120 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1121 unary_(std::move(rval)),
1122 op_(op) {
1123 final_type_ = Type::UNARY;
1124}
1125
1126AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1127 std::unique_ptr<AidlConstantValue> lval,
1128 const string& op,
1129 std::unique_ptr<AidlConstantValue> rval)
1130 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1131 left_val_(std::move(lval)),
1132 right_val_(std::move(rval)),
1133 op_(op) {
1134 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001135}