blob: 423bfca00e7d73822ccf444779c0146ae1651d92 [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
Will McVickerd7d18df2019-09-12 13:40:50 -0700251bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
252 // Verify the unary type here
253 switch (type) {
254 case Type::BOOLEAN: // fall-through
255 case Type::INT8: // fall-through
256 case Type::INT32: // fall-through
257 case Type::INT64:
258 return true;
259 case Type::FLOATING:
260 return (op == "+" || op == "-");
261 default:
262 return false;
263 }
264}
265
266bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
267 switch (t1) {
268 case Type::STRING:
269 if (t2 == Type::STRING) {
270 return true;
271 }
272 break;
273 case Type::BOOLEAN: // fall-through
274 case Type::INT8: // fall-through
275 case Type::INT32: // fall-through
276 case Type::INT64:
277 switch (t2) {
278 case Type::BOOLEAN: // fall-through
279 case Type::INT8: // fall-through
280 case Type::INT32: // fall-through
281 case Type::INT64:
282 return true;
283 break;
284 default:
285 break;
286 }
287 break;
288 default:
289 break;
290 }
291
292 return false;
293}
294
295// Returns the promoted kind for both operands
296AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
297 Type right) {
298 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000299 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
300 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700301
302 // Kinds in concern: bool, (u)int[8|32|64]
303 if (left == right) return left; // easy case
304 if (left == Type::BOOLEAN) return right;
305 if (right == Type::BOOLEAN) return left;
306
307 return left < right ? right : left;
308}
309
310// Returns the promoted integral type where INT32 is the smallest type
311AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
312 return (Type::INT32 < in) ? in : Type::INT32;
313}
314
315template <typename T>
316T AidlConstantValue::cast() const {
Steven Moreland21780812020-09-11 01:29:45 +0000317 AIDL_FATAL_IF(!is_evaluated_, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700318
319#define CASE_CAST_T(__type__) return static_cast<T>(static_cast<__type__>(final_value_));
320
321 SWITCH_KIND(final_type_, CASE_CAST_T, SHOULD_NOT_REACH(); return 0;);
322}
323
Steven Moreland541788d2020-05-21 22:05:52 +0000324AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
325 AidlLocation location = specifier.GetLocation();
326
327 // allocation of int[0] is a bit wasteful in Java
328 if (specifier.IsArray()) {
329 return nullptr;
330 }
331
332 const std::string name = specifier.GetName();
333 if (name == "boolean") {
334 return Boolean(location, false);
335 }
336 if (name == "byte" || name == "int" || name == "long") {
337 return Integral(location, "0");
338 }
339 if (name == "float") {
340 return Floating(location, "0.0f");
341 }
342 if (name == "double") {
343 return Floating(location, "0.0");
344 }
345 return nullptr;
346}
347
Will McVickerefd970d2019-09-25 15:28:30 -0700348AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
349 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
350}
351
352AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800353 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700354 if (!isValidLiteralChar(value)) {
355 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800356 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700357 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800358 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700359}
360
361AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
362 const std::string& value) {
363 return new AidlConstantValue(location, Type::FLOATING, value);
364}
365
Will McVickerd7d18df2019-09-12 13:40:50 -0700366bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000367 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700368}
369
Will McVickerd7d18df2019-09-12 13:40:50 -0700370bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
371 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700372 if (parsed_value == nullptr || parsed_type == nullptr) {
373 return false;
374 }
375
Steven Morelandcef22662020-07-08 20:54:28 +0000376 const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L');
377 const std::string value_substr = isLong ? value.substr(0, value.size() - 1) : value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700378
Steven Morelandcef22662020-07-08 20:54:28 +0000379 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700380 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
381 // handle that when computing constant expressions, then we need to
382 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
383 // so we parse as an unsigned int when possible and then cast to a signed
384 // int. One example of this is in ICameraService.aidl where a constant int
385 // is used for bit manipulations which ideally should be handled with an
386 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000387 //
388 // Note, for historical consistency, we need to consider small hex values
389 // as an integral type. Recognizing them as INT8 could break some files,
390 // even though it would simplify this code.
391 if (uint32_t rawValue32;
392 !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700393 *parsed_value = static_cast<int32_t>(rawValue32);
394 *parsed_type = Type::INT32;
Steven Morelandcef22662020-07-08 20:54:28 +0000395 } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) {
396 *parsed_value = static_cast<int64_t>(rawValue64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700397 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000398 } else {
399 *parsed_value = 0;
400 *parsed_type = Type::ERROR;
401 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700402 }
403 return true;
404 }
405
Steven Morelandcef22662020-07-08 20:54:28 +0000406 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
407 *parsed_value = 0;
Will McVickerd7d18df2019-09-12 13:40:50 -0700408 *parsed_type = Type::ERROR;
409 return false;
410 }
411
Steven Morelandcef22662020-07-08 20:54:28 +0000412 if (isLong) {
413 *parsed_type = Type::INT64;
414 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700415 // guess literal type.
416 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
417 *parsed_type = Type::INT8;
418 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
419 *parsed_type = Type::INT32;
420 } else {
421 *parsed_type = Type::INT64;
422 }
423 }
424 return true;
425}
426
427AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000428 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700429
430 Type parsed_type;
431 int64_t parsed_value = 0;
432 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
433 if (!success) {
434 return nullptr;
435 }
436
437 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700438}
439
440AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700441 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000442 AIDL_FATAL_IF(values == nullptr, location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700443 return new AidlConstantValue(location, Type::ARRAY, std::move(values));
Will McVickerefd970d2019-09-25 15:28:30 -0700444}
445
Will McVickerd7d18df2019-09-12 13:40:50 -0700446AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700447 for (size_t i = 0; i < value.length(); ++i) {
448 if (!isValidLiteralChar(value[i])) {
449 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
450 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800451 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700452 }
453 }
454
455 return new AidlConstantValue(location, Type::STRING, value);
456}
457
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700458AidlConstantValue* AidlConstantValue::ShallowIntegralCopy(const AidlConstantValue& other) {
Daniel Norman3cce7cd2020-02-07 13:25:12 -0800459 // TODO(b/141313220) Perform a full copy instead of parsing+unparsing
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700460 AidlTypeSpecifier type = AidlTypeSpecifier(AIDL_LOCATION_HERE, "long", false, nullptr, "");
Steven Moreland65606612019-11-10 21:21:25 -0800461 // TODO(b/142722772) CheckValid() should be called before ValueString()
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800462 if (!other.CheckValid() || !other.evaluate(type)) {
Steven Moreland59e53e42019-11-26 20:38:08 -0800463 AIDL_ERROR(other) << "Failed to parse expression as integer: " << other.value_;
464 return nullptr;
465 }
466 const std::string& value = other.ValueString(type, AidlConstantValueDecorator);
467 if (value.empty()) {
468 return nullptr; // error already logged
Daniel Normanb28684e2019-10-17 15:31:39 -0700469 }
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700470
Steven Moreland59e53e42019-11-26 20:38:08 -0800471 AidlConstantValue* result = Integral(AIDL_LOCATION_HERE, value);
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700472 if (result == nullptr) {
473 AIDL_FATAL(other) << "Unable to perform ShallowIntegralCopy.";
474 }
475 return result;
Daniel Normanb28684e2019-10-17 15:31:39 -0700476}
477
Will McVickerd7d18df2019-09-12 13:40:50 -0700478string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
479 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700480 if (type.IsGeneric()) {
481 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
482 return "";
483 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700484 if (!is_evaluated_) {
485 // TODO(b/142722772) CheckValid() should be called before ValueString()
486 bool success = CheckValid();
487 success &= evaluate(type);
488 if (!success) {
489 // the detailed error message shall be printed in evaluate
490 return "";
491 }
Will McVickerefd970d2019-09-25 15:28:30 -0700492 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 if (!is_valid_) {
494 AIDL_ERROR(this) << "Invalid constant value: " + value_;
495 return "";
496 }
Jooyung Han690f5842020-12-04 13:02:04 +0900497
498 const AidlDefinedType* defined_type = type.GetDefinedType();
499 if (defined_type && !type.IsArray()) {
500 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
501 if (!enum_type) {
502 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
503 << ") for a const value(" << value_ << ")";
504 return "";
505 }
506 if (type_ != Type::REF) {
507 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
508 << enum_type->GetCanonicalName();
509 return "";
510 }
511 return decorator(type, value_);
512 }
513
Will McVickerd7d18df2019-09-12 13:40:50 -0700514 const string& type_string = type.GetName();
515 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700516
Will McVickerd7d18df2019-09-12 13:40:50 -0700517 switch (final_type_) {
518 case Type::CHARACTER:
519 if (type_string == "char") {
520 return decorator(type, final_string_value_);
521 }
522 err = -1;
523 break;
524 case Type::STRING:
525 if (type_string == "String") {
526 return decorator(type, final_string_value_);
527 }
528 err = -1;
529 break;
530 case Type::BOOLEAN: // fall-through
531 case Type::INT8: // fall-through
532 case Type::INT32: // fall-through
533 case Type::INT64:
534 if (type_string == "byte") {
535 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
536 err = -1;
537 break;
538 }
539 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
540 } else if (type_string == "int") {
541 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
542 err = -1;
543 break;
544 }
545 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
546 } else if (type_string == "long") {
547 return decorator(type, std::to_string(final_value_));
548 } else if (type_string == "boolean") {
549 return decorator(type, final_value_ ? "true" : "false");
550 }
551 err = -1;
552 break;
553 case Type::ARRAY: {
554 if (!type.IsArray()) {
555 err = -1;
556 break;
557 }
558 vector<string> value_strings;
559 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700560 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700561
Will McVickerefd970d2019-09-25 15:28:30 -0700562 for (const auto& value : values_) {
563 const AidlTypeSpecifier& array_base = type.ArrayBase();
Will McVickerd7d18df2019-09-12 13:40:50 -0700564 const string value_string = value->ValueString(array_base, decorator);
565 if (value_string.empty()) {
566 success = false;
567 break;
568 }
569 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700570 }
571 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700572 err = -1;
573 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700574 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700575
576 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700577 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700578 case Type::FLOATING: {
579 std::string_view raw_view(value_.c_str());
580 bool is_float_literal = ConsumeSuffix(&raw_view, "f");
581 std::string stripped_value = std::string(raw_view);
Will McVickerefd970d2019-09-25 15:28:30 -0700582
583 if (type_string == "double") {
584 double parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700585 if (!android::base::ParseDouble(stripped_value, &parsed_value)) {
586 AIDL_ERROR(this) << "Could not parse " << value_;
587 err = -1;
588 break;
589 }
Will McVickerefd970d2019-09-25 15:28:30 -0700590 return decorator(type, std::to_string(parsed_value));
591 }
592 if (is_float_literal && type_string == "float") {
593 float parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700594 if (!android::base::ParseFloat(stripped_value, &parsed_value)) {
595 AIDL_ERROR(this) << "Could not parse " << value_;
596 err = -1;
597 break;
598 }
Will McVickerefd970d2019-09-25 15:28:30 -0700599 return decorator(type, std::to_string(parsed_value) + "f");
600 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700601 err = -1;
602 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700603 }
Will McVickerefd970d2019-09-25 15:28:30 -0700604 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700605 err = -1;
606 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700607 }
608
Steven Moreland21780812020-09-11 01:29:45 +0000609 AIDL_FATAL_IF(err == 0, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700610 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string;
Will McVickerefd970d2019-09-25 15:28:30 -0700611 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700612}
613
614bool AidlConstantValue::CheckValid() const {
615 // Nothing needs to be checked here. The constant value will be validated in
616 // the constructor or in the evaluate() function.
617 if (is_evaluated_) return is_valid_;
618
619 switch (type_) {
620 case Type::BOOLEAN: // fall-through
621 case Type::INT8: // fall-through
622 case Type::INT32: // fall-through
623 case Type::INT64: // fall-through
624 case Type::ARRAY: // fall-through
625 case Type::CHARACTER: // fall-through
626 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900627 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700628 case Type::FLOATING: // fall-through
629 case Type::UNARY: // fall-through
630 case Type::BINARY:
631 is_valid_ = true;
632 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800633 case Type::ERROR:
634 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700635 default:
636 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
637 return false;
638 }
639
640 return true;
641}
642
643bool AidlConstantValue::evaluate(const AidlTypeSpecifier& type) const {
644 if (is_evaluated_) {
645 return is_valid_;
646 }
647 int err = 0;
648 is_evaluated_ = true;
649
650 switch (type_) {
651 case Type::ARRAY: {
652 if (!type.IsArray()) {
653 AIDL_ERROR(this) << "Invalid constant array type: " << type.GetName();
654 err = -1;
655 break;
656 }
657 Type array_type = Type::ERROR;
658 bool success = true;
659 for (const auto& value : values_) {
660 success = value->CheckValid();
661 if (success) {
662 success = value->evaluate(type.ArrayBase());
663 if (!success) {
664 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
665 break;
666 }
667 if (array_type == Type::ERROR) {
668 array_type = value->final_type_;
669 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
670 value->final_type_)) {
671 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
672 << ". Expecting type compatible with " << ToString(array_type);
673 success = false;
674 break;
675 }
676 } else {
677 break;
678 }
679 }
680 if (!success) {
681 err = -1;
682 break;
683 }
684 final_type_ = type_;
685 break;
686 }
687 case Type::BOOLEAN:
688 if ((value_ != "true") && (value_ != "false")) {
689 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
690 err = -1;
691 break;
692 }
693 final_value_ = (value_ == "true") ? 1 : 0;
694 final_type_ = type_;
695 break;
696 case Type::INT8: // fall-through
697 case Type::INT32: // fall-through
698 case Type::INT64:
699 // Parsing happens in the constructor
700 final_type_ = type_;
701 break;
702 case Type::CHARACTER: // fall-through
703 case Type::STRING:
704 final_string_value_ = value_;
705 final_type_ = type_;
706 break;
707 case Type::FLOATING:
708 // Just parse on the fly in ValueString
709 final_type_ = type_;
710 break;
711 default:
712 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
713 err = -1;
714 }
715
716 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700717}
718
719string AidlConstantValue::ToString(Type type) {
720 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700721 case Type::BOOLEAN:
722 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700723 case Type::INT8:
724 return "an int8 literal";
725 case Type::INT32:
726 return "an int32 literal";
727 case Type::INT64:
728 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800729 case Type::ARRAY:
730 return "a literal array";
731 case Type::CHARACTER:
732 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700733 case Type::STRING:
734 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900735 case Type::REF:
736 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800737 case Type::FLOATING:
738 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700739 case Type::UNARY:
740 return "a unary expression";
741 case Type::BINARY:
742 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800743 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000744 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800745 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700746 default:
Steven Moreland21780812020-09-11 01:29:45 +0000747 AIDL_FATAL(AIDL_LOCATION_HERE)
748 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700749 return ""; // not reached
750 }
751}
752
Jooyung Han690f5842020-12-04 13:02:04 +0900753AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value,
754 const std::string& comments)
755 : AidlConstantValue(location, Type::REF, value), comments_(comments) {
756 const auto pos = value.find_last_of('.');
757 if (pos == string::npos) {
758 field_name_ = value;
759 } else {
760 ref_type_ =
761 std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), false, nullptr, "");
762 field_name_ = value.substr(pos + 1);
763 }
764}
765
766bool AidlConstantReference::CheckValid() const {
767 if (is_evaluated_) return is_valid_;
768
769 if (!GetRefType() || !GetRefType()->GetDefinedType()) {
770 // This can happen when "const reference" is used in an unsupported way,
771 // but missed in checks there. It works as a safety net.
772 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
773 is_valid_ = false;
774 return false;
775 }
776
777 auto defined_type = GetRefType()->GetDefinedType();
778 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
779 for (const auto& e : enum_decl->GetEnumerators()) {
780 if (e->GetName() == field_name_) {
781 is_valid_ = !e->GetValue() || e->GetValue()->CheckValid();
782 return is_valid_;
783 }
784 }
785 } else {
786 for (const auto& c : defined_type->GetConstantDeclarations()) {
787 if (c->GetName() == field_name_) {
788 is_valid_ = c->GetValue().CheckValid();
789 return is_valid_;
790 }
791 }
792 }
793 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << ref_type_->GetName();
794 is_valid_ = false;
795 return false;
796}
797
798bool AidlConstantReference::evaluate(const AidlTypeSpecifier& type) const {
799 if (is_evaluated_) return is_valid_;
800 is_evaluated_ = true;
801
802 const AidlDefinedType* view_type = type.GetDefinedType();
803 if (view_type) {
804 auto enum_decl = view_type->AsEnumDeclaration();
805 if (!enum_decl) {
806 AIDL_ERROR(type) << "Can't refer to a constant expression: " << value_;
807 return false;
808 }
809 }
810
811 auto defined_type = GetRefType()->GetDefinedType();
812 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
813 for (const auto& e : enum_decl->GetEnumerators()) {
814 if (e->GetName() == field_name_) {
815 if (e->GetValue()->evaluate(type)) {
816 is_valid_ = e->GetValue()->is_valid_;
817 if (is_valid_) {
818 final_type_ = e->GetValue()->final_type_;
819 if (final_type_ == Type::STRING) {
820 final_string_value_ = e->GetValue()->final_string_value_;
821 } else {
822 final_value_ = e->GetValue()->final_value_;
823 }
824 return true;
825 }
826 }
827 break;
828 }
829 }
830 } else {
831 for (const auto& c : defined_type->GetConstantDeclarations()) {
832 if (c->GetName() == field_name_) {
833 if (c->GetValue().evaluate(type)) {
834 is_valid_ = c->GetValue().is_valid_;
835 if (is_valid_) {
836 final_type_ = c->GetValue().final_type_;
837 if (final_type_ == Type::STRING) {
838 final_string_value_ = c->GetValue().final_string_value_;
839 } else {
840 final_value_ = c->GetValue().final_value_;
841 }
842 return true;
843 }
844 }
845 }
846 }
847 }
848
849 is_valid_ = false;
850 return false;
851}
852
Will McVickerd7d18df2019-09-12 13:40:50 -0700853bool AidlUnaryConstExpression::CheckValid() const {
854 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000855 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700856
857 is_valid_ = unary_->CheckValid();
858 if (!is_valid_) {
859 final_type_ = Type::ERROR;
860 return false;
861 }
862
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800863 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700864}
865
866bool AidlUnaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
867 if (is_evaluated_) {
868 return is_valid_;
869 }
870 is_evaluated_ = true;
871
872 // Recursively evaluate the expression tree
873 if (!unary_->is_evaluated_) {
874 // TODO(b/142722772) CheckValid() should be called before ValueString()
875 bool success = CheckValid();
876 success &= unary_->evaluate(type);
877 if (!success) {
878 is_valid_ = false;
879 return false;
880 }
881 }
Devin Moorec233fb82020-04-07 11:13:44 -0700882 if (!IsCompatibleType(unary_->final_type_, op_)) {
883 AIDL_ERROR(unary_) << "'" << op_ << "'"
884 << " is not compatible with " << ToString(unary_->final_type_)
885 << ": " + value_;
886 is_valid_ = false;
887 return false;
888 }
889 if (!unary_->is_valid_) {
890 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700891 is_valid_ = false;
892 return false;
893 }
894 final_type_ = unary_->final_type_;
895
896 if (final_type_ == Type::FLOATING) {
897 // don't do anything here. ValueString() will handle everything.
898 is_valid_ = true;
899 return true;
900 }
901
Steven Morelande1ff67e2020-07-16 23:22:36 +0000902#define CASE_UNARY(__type__) \
903 return handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700904
905 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
906 is_valid_ = false; return false;)
907}
908
Will McVickerd7d18df2019-09-12 13:40:50 -0700909bool AidlBinaryConstExpression::CheckValid() const {
910 bool success = false;
911 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000912 AIDL_FATAL_IF(left_val_ == nullptr, this);
913 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700914
915 success = left_val_->CheckValid();
916 if (!success) {
917 final_type_ = Type::ERROR;
918 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
919 }
920
921 success = right_val_->CheckValid();
922 if (!success) {
923 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
924 final_type_ = Type::ERROR;
925 }
926
927 if (final_type_ == Type::ERROR) {
928 is_valid_ = false;
929 return false;
930 }
931
932 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800933 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700934}
935
936bool AidlBinaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
937 if (is_evaluated_) {
938 return is_valid_;
939 }
940 is_evaluated_ = true;
Steven Moreland21780812020-09-11 01:29:45 +0000941 AIDL_FATAL_IF(left_val_ == nullptr, type);
942 AIDL_FATAL_IF(right_val_ == nullptr, type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700943
944 // Recursively evaluate the binary expression tree
945 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
946 // TODO(b/142722772) CheckValid() should be called before ValueString()
947 bool success = CheckValid();
948 success &= left_val_->evaluate(type);
949 success &= right_val_->evaluate(type);
950 if (!success) {
951 is_valid_ = false;
952 return false;
953 }
954 }
955 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
956 is_valid_ = false;
957 return false;
958 }
959 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
960 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000961 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
962 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
963 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700964 return false;
965 }
966
967 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
968
969 // Handle String case first
970 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000971 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700972 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000973 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700974 final_type_ = Type::ERROR;
975 is_valid_ = false;
976 return false;
977 }
978
979 // Remove trailing " from lhs
980 const string& lhs = left_val_->final_string_value_;
981 if (lhs.back() != '"') {
982 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
983 final_type_ = Type::ERROR;
984 is_valid_ = false;
985 return false;
986 }
987 const string& rhs = right_val_->final_string_value_;
988 // Remove starting " from rhs
989 if (rhs.front() != '"') {
990 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
991 final_type_ = Type::ERROR;
992 is_valid_ = false;
993 return false;
994 }
995
996 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
997 final_type_ = Type::STRING;
998 return true;
999 }
1000
Will McVickerd7d18df2019-09-12 13:40:50 -07001001 // CASE: + - * / % | ^ & < > <= >= == !=
1002 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -07001003 // promoted kind for both operands.
1004 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1005 IntegralPromotion(right_val_->final_type_));
1006 // result kind.
1007 final_type_ = isArithmeticOrBitflip
1008 ? promoted // arithmetic or bitflip operators generates promoted type
1009 : Type::BOOLEAN; // comparison operators generates bool
1010
Steven Morelande1ff67e2020-07-16 23:22:36 +00001011#define CASE_BINARY_COMMON(__type__) \
1012 return handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
1013 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001014
1015 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1016 is_valid_ = false; return false;)
1017 }
1018
1019 // CASE: << >>
1020 string newOp = op_;
1021 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -07001022 // promoted kind for both operands.
1023 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
1024 IntegralPromotion(right_val_->final_type_));
1025 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001026 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -08001027 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -07001028 // is defined as shifting into the other direction.
1029 newOp = OPEQ("<<") ? ">>" : "<<";
1030 numBits = -numBits;
1031 }
1032
Devin Moore04823022020-09-11 10:43:35 -07001033#define CASE_SHIFT(__type__) \
1034 return handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
1035 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001036
1037 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1038 is_valid_ = false; return false;)
1039 }
1040
1041 // CASE: && ||
1042 if (OP_IS_BIN_LOGICAL) {
1043 final_type_ = Type::BOOLEAN;
1044 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001045 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1046 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001047 }
1048
1049 SHOULD_NOT_REACH();
1050 is_valid_ = false;
1051 return false;
1052}
1053
Will McVickerd7d18df2019-09-12 13:40:50 -07001054AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1055 int64_t parsed_value, const string& checked_value)
1056 : AidlNode(location),
1057 type_(parsed_type),
1058 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001059 final_type_(parsed_type),
1060 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001061 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1062 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001063}
Will McVickerefd970d2019-09-25 15:28:30 -07001064
1065AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001066 const string& checked_value)
1067 : AidlNode(location),
1068 type_(type),
1069 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001070 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001071 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001072 switch (type_) {
1073 case Type::INT8:
1074 case Type::INT32:
1075 case Type::INT64:
1076 case Type::ARRAY:
1077 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1078 break;
1079 default:
1080 break;
1081 }
1082}
1083
1084AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
1085 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values)
1086 : AidlNode(location),
1087 type_(type),
1088 values_(std::move(*values)),
1089 is_valid_(false),
1090 is_evaluated_(false),
1091 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001092 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001093}
1094
1095AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1096 std::unique_ptr<AidlConstantValue> rval)
1097 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1098 unary_(std::move(rval)),
1099 op_(op) {
1100 final_type_ = Type::UNARY;
1101}
1102
1103AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1104 std::unique_ptr<AidlConstantValue> lval,
1105 const string& op,
1106 std::unique_ptr<AidlConstantValue> rval)
1107 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1108 left_val_(std::move(lval)),
1109 right_val_(std::move(rval)),
1110 op_(op) {
1111 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001112}