blob: 8e9ebeae630224897d48f8db6164e11466eebc59 [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);
Jooyung Han29813842020-12-08 01:28:03 +0900443 std::vector<std::string> str_values;
444 for (const auto& v : *values) {
445 str_values.push_back(v->value_);
446 }
447 return new AidlConstantValue(location, Type::ARRAY, std::move(values), Join(str_values, ", "));
Will McVickerefd970d2019-09-25 15:28:30 -0700448}
449
Will McVickerd7d18df2019-09-12 13:40:50 -0700450AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700451 for (size_t i = 0; i < value.length(); ++i) {
452 if (!isValidLiteralChar(value[i])) {
453 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
454 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800455 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700456 }
457 }
458
459 return new AidlConstantValue(location, Type::STRING, value);
460}
461
Will McVickerd7d18df2019-09-12 13:40:50 -0700462string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
463 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700464 if (type.IsGeneric()) {
465 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
466 return "";
467 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 if (!is_evaluated_) {
469 // TODO(b/142722772) CheckValid() should be called before ValueString()
470 bool success = CheckValid();
471 success &= evaluate(type);
472 if (!success) {
473 // the detailed error message shall be printed in evaluate
474 return "";
475 }
Will McVickerefd970d2019-09-25 15:28:30 -0700476 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700477 if (!is_valid_) {
478 AIDL_ERROR(this) << "Invalid constant value: " + value_;
479 return "";
480 }
Jooyung Han690f5842020-12-04 13:02:04 +0900481
482 const AidlDefinedType* defined_type = type.GetDefinedType();
483 if (defined_type && !type.IsArray()) {
484 const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
485 if (!enum_type) {
486 AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
Jooyung Han29813842020-12-08 01:28:03 +0900487 << ") for a const value (" << value_ << ")";
Jooyung Han690f5842020-12-04 13:02:04 +0900488 return "";
489 }
490 if (type_ != Type::REF) {
491 AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum "
492 << enum_type->GetCanonicalName();
493 return "";
494 }
495 return decorator(type, value_);
496 }
497
Will McVickerd7d18df2019-09-12 13:40:50 -0700498 const string& type_string = type.GetName();
499 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700500
Will McVickerd7d18df2019-09-12 13:40:50 -0700501 switch (final_type_) {
502 case Type::CHARACTER:
503 if (type_string == "char") {
504 return decorator(type, final_string_value_);
505 }
506 err = -1;
507 break;
508 case Type::STRING:
509 if (type_string == "String") {
510 return decorator(type, final_string_value_);
511 }
512 err = -1;
513 break;
514 case Type::BOOLEAN: // fall-through
515 case Type::INT8: // fall-through
516 case Type::INT32: // fall-through
517 case Type::INT64:
518 if (type_string == "byte") {
519 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
520 err = -1;
521 break;
522 }
523 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
524 } else if (type_string == "int") {
525 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
526 err = -1;
527 break;
528 }
529 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
530 } else if (type_string == "long") {
531 return decorator(type, std::to_string(final_value_));
532 } else if (type_string == "boolean") {
533 return decorator(type, final_value_ ? "true" : "false");
534 }
535 err = -1;
536 break;
537 case Type::ARRAY: {
538 if (!type.IsArray()) {
539 err = -1;
540 break;
541 }
542 vector<string> value_strings;
543 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700544 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700545
Will McVickerefd970d2019-09-25 15:28:30 -0700546 for (const auto& value : values_) {
547 const AidlTypeSpecifier& array_base = type.ArrayBase();
Will McVickerd7d18df2019-09-12 13:40:50 -0700548 const string value_string = value->ValueString(array_base, decorator);
549 if (value_string.empty()) {
550 success = false;
551 break;
552 }
553 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700554 }
555 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700556 err = -1;
557 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700558 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700559
560 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700561 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700562 case Type::FLOATING: {
563 std::string_view raw_view(value_.c_str());
564 bool is_float_literal = ConsumeSuffix(&raw_view, "f");
565 std::string stripped_value = std::string(raw_view);
Will McVickerefd970d2019-09-25 15:28:30 -0700566
567 if (type_string == "double") {
568 double parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700569 if (!android::base::ParseDouble(stripped_value, &parsed_value)) {
570 AIDL_ERROR(this) << "Could not parse " << value_;
571 err = -1;
572 break;
573 }
Will McVickerefd970d2019-09-25 15:28:30 -0700574 return decorator(type, std::to_string(parsed_value));
575 }
576 if (is_float_literal && type_string == "float") {
577 float parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700578 if (!android::base::ParseFloat(stripped_value, &parsed_value)) {
579 AIDL_ERROR(this) << "Could not parse " << value_;
580 err = -1;
581 break;
582 }
Will McVickerefd970d2019-09-25 15:28:30 -0700583 return decorator(type, std::to_string(parsed_value) + "f");
584 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700585 err = -1;
586 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700587 }
Will McVickerefd970d2019-09-25 15:28:30 -0700588 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700589 err = -1;
590 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700591 }
592
Steven Moreland21780812020-09-11 01:29:45 +0000593 AIDL_FATAL_IF(err == 0, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700594 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string;
Will McVickerefd970d2019-09-25 15:28:30 -0700595 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700596}
597
598bool AidlConstantValue::CheckValid() const {
599 // Nothing needs to be checked here. The constant value will be validated in
600 // the constructor or in the evaluate() function.
601 if (is_evaluated_) return is_valid_;
602
603 switch (type_) {
604 case Type::BOOLEAN: // fall-through
605 case Type::INT8: // fall-through
606 case Type::INT32: // fall-through
607 case Type::INT64: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700608 case Type::CHARACTER: // fall-through
609 case Type::STRING: // fall-through
Jooyung Han690f5842020-12-04 13:02:04 +0900610 case Type::REF: // fall-through
Will McVickerd7d18df2019-09-12 13:40:50 -0700611 case Type::FLOATING: // fall-through
612 case Type::UNARY: // fall-through
613 case Type::BINARY:
614 is_valid_ = true;
615 break;
Jooyung Han29813842020-12-08 01:28:03 +0900616 case Type::ARRAY:
617 is_valid_ = true;
618 for (const auto& v : values_) is_valid_ &= v->CheckValid();
619 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800620 case Type::ERROR:
621 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700622 default:
623 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
624 return false;
625 }
626
627 return true;
628}
629
630bool AidlConstantValue::evaluate(const AidlTypeSpecifier& type) const {
631 if (is_evaluated_) {
632 return is_valid_;
633 }
634 int err = 0;
635 is_evaluated_ = true;
636
637 switch (type_) {
638 case Type::ARRAY: {
639 if (!type.IsArray()) {
640 AIDL_ERROR(this) << "Invalid constant array type: " << type.GetName();
641 err = -1;
642 break;
643 }
644 Type array_type = Type::ERROR;
645 bool success = true;
646 for (const auto& value : values_) {
647 success = value->CheckValid();
648 if (success) {
649 success = value->evaluate(type.ArrayBase());
650 if (!success) {
651 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
652 break;
653 }
654 if (array_type == Type::ERROR) {
655 array_type = value->final_type_;
656 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
657 value->final_type_)) {
658 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
659 << ". Expecting type compatible with " << ToString(array_type);
660 success = false;
661 break;
662 }
663 } else {
664 break;
665 }
666 }
667 if (!success) {
668 err = -1;
669 break;
670 }
671 final_type_ = type_;
672 break;
673 }
674 case Type::BOOLEAN:
675 if ((value_ != "true") && (value_ != "false")) {
676 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
677 err = -1;
678 break;
679 }
680 final_value_ = (value_ == "true") ? 1 : 0;
681 final_type_ = type_;
682 break;
683 case Type::INT8: // fall-through
684 case Type::INT32: // fall-through
685 case Type::INT64:
686 // Parsing happens in the constructor
687 final_type_ = type_;
688 break;
689 case Type::CHARACTER: // fall-through
690 case Type::STRING:
691 final_string_value_ = value_;
692 final_type_ = type_;
693 break;
694 case Type::FLOATING:
695 // Just parse on the fly in ValueString
696 final_type_ = type_;
697 break;
698 default:
699 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
700 err = -1;
701 }
702
703 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700704}
705
706string AidlConstantValue::ToString(Type type) {
707 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700708 case Type::BOOLEAN:
709 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700710 case Type::INT8:
711 return "an int8 literal";
712 case Type::INT32:
713 return "an int32 literal";
714 case Type::INT64:
715 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800716 case Type::ARRAY:
717 return "a literal array";
718 case Type::CHARACTER:
719 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700720 case Type::STRING:
721 return "a literal string";
Jooyung Han690f5842020-12-04 13:02:04 +0900722 case Type::REF:
723 return "a reference";
Steven Morelanda923a722019-11-26 20:08:30 -0800724 case Type::FLOATING:
725 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700726 case Type::UNARY:
727 return "a unary expression";
728 case Type::BINARY:
729 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800730 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000731 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800732 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700733 default:
Steven Moreland21780812020-09-11 01:29:45 +0000734 AIDL_FATAL(AIDL_LOCATION_HERE)
735 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700736 return ""; // not reached
737 }
738}
739
Jooyung Han690f5842020-12-04 13:02:04 +0900740AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value,
741 const std::string& comments)
742 : AidlConstantValue(location, Type::REF, value), comments_(comments) {
743 const auto pos = value.find_last_of('.');
744 if (pos == string::npos) {
745 field_name_ = value;
746 } else {
747 ref_type_ =
748 std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), false, nullptr, "");
749 field_name_ = value.substr(pos + 1);
750 }
751}
752
Jooyung Han29813842020-12-08 01:28:03 +0900753const AidlConstantValue* AidlConstantReference::Resolve() {
754 if (resolved_) return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900755 if (!GetRefType() || !GetRefType()->GetDefinedType()) {
756 // This can happen when "const reference" is used in an unsupported way,
757 // but missed in checks there. It works as a safety net.
758 AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
Jooyung Han29813842020-12-08 01:28:03 +0900759 return nullptr;
Jooyung Han690f5842020-12-04 13:02:04 +0900760 }
761
762 auto defined_type = GetRefType()->GetDefinedType();
763 if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
764 for (const auto& e : enum_decl->GetEnumerators()) {
765 if (e->GetName() == field_name_) {
Jooyung Han29813842020-12-08 01:28:03 +0900766 resolved_ = e->GetValue();
767 return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900768 }
769 }
770 } else {
771 for (const auto& c : defined_type->GetConstantDeclarations()) {
772 if (c->GetName() == field_name_) {
Jooyung Han29813842020-12-08 01:28:03 +0900773 resolved_ = &c->GetValue();
774 return resolved_;
Jooyung Han690f5842020-12-04 13:02:04 +0900775 }
776 }
777 }
778 AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << ref_type_->GetName();
Jooyung Han29813842020-12-08 01:28:03 +0900779 return nullptr;
780}
781
782bool AidlConstantReference::CheckValid() const {
783 if (is_evaluated_) return is_valid_;
784 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
785 is_valid_ = resolved_->CheckValid();
786 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900787}
788
789bool AidlConstantReference::evaluate(const AidlTypeSpecifier& type) const {
790 if (is_evaluated_) return is_valid_;
Jooyung Han29813842020-12-08 01:28:03 +0900791 AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_;
792 is_evaluated_ = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900793 const AidlDefinedType* view_type = type.GetDefinedType();
794 if (view_type) {
795 auto enum_decl = view_type->AsEnumDeclaration();
796 if (!enum_decl) {
797 AIDL_ERROR(type) << "Can't refer to a constant expression: " << value_;
798 return false;
799 }
800 }
801
Jooyung Han29813842020-12-08 01:28:03 +0900802 resolved_->evaluate(type);
803 is_valid_ = resolved_->is_valid_;
804 final_type_ = resolved_->final_type_;
805 if (is_valid_) {
806 if (final_type_ == Type::STRING) {
807 final_string_value_ = resolved_->final_string_value_;
808 } else {
809 final_value_ = resolved_->final_value_;
Jooyung Han690f5842020-12-04 13:02:04 +0900810 }
811 }
Jooyung Han29813842020-12-08 01:28:03 +0900812 return is_valid_;
Jooyung Han690f5842020-12-04 13:02:04 +0900813}
814
Will McVickerd7d18df2019-09-12 13:40:50 -0700815bool AidlUnaryConstExpression::CheckValid() const {
816 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000817 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700818
819 is_valid_ = unary_->CheckValid();
820 if (!is_valid_) {
821 final_type_ = Type::ERROR;
822 return false;
823 }
824
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800825 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700826}
827
828bool AidlUnaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
829 if (is_evaluated_) {
830 return is_valid_;
831 }
832 is_evaluated_ = true;
833
834 // Recursively evaluate the expression tree
835 if (!unary_->is_evaluated_) {
836 // TODO(b/142722772) CheckValid() should be called before ValueString()
837 bool success = CheckValid();
838 success &= unary_->evaluate(type);
839 if (!success) {
840 is_valid_ = false;
841 return false;
842 }
843 }
Devin Moorec233fb82020-04-07 11:13:44 -0700844 if (!IsCompatibleType(unary_->final_type_, op_)) {
845 AIDL_ERROR(unary_) << "'" << op_ << "'"
846 << " is not compatible with " << ToString(unary_->final_type_)
847 << ": " + value_;
848 is_valid_ = false;
849 return false;
850 }
851 if (!unary_->is_valid_) {
852 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700853 is_valid_ = false;
854 return false;
855 }
856 final_type_ = unary_->final_type_;
857
858 if (final_type_ == Type::FLOATING) {
859 // don't do anything here. ValueString() will handle everything.
860 is_valid_ = true;
861 return true;
862 }
863
Steven Morelande1ff67e2020-07-16 23:22:36 +0000864#define CASE_UNARY(__type__) \
865 return handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700866
867 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
868 is_valid_ = false; return false;)
869}
870
Will McVickerd7d18df2019-09-12 13:40:50 -0700871bool AidlBinaryConstExpression::CheckValid() const {
872 bool success = false;
873 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000874 AIDL_FATAL_IF(left_val_ == nullptr, this);
875 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700876
877 success = left_val_->CheckValid();
878 if (!success) {
879 final_type_ = Type::ERROR;
880 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
881 }
882
883 success = right_val_->CheckValid();
884 if (!success) {
885 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
886 final_type_ = Type::ERROR;
887 }
888
889 if (final_type_ == Type::ERROR) {
890 is_valid_ = false;
891 return false;
892 }
893
894 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800895 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700896}
897
898bool AidlBinaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
899 if (is_evaluated_) {
900 return is_valid_;
901 }
902 is_evaluated_ = true;
Steven Moreland21780812020-09-11 01:29:45 +0000903 AIDL_FATAL_IF(left_val_ == nullptr, type);
904 AIDL_FATAL_IF(right_val_ == nullptr, type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700905
906 // Recursively evaluate the binary expression tree
907 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
908 // TODO(b/142722772) CheckValid() should be called before ValueString()
909 bool success = CheckValid();
910 success &= left_val_->evaluate(type);
911 success &= right_val_->evaluate(type);
912 if (!success) {
913 is_valid_ = false;
914 return false;
915 }
916 }
917 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
918 is_valid_ = false;
919 return false;
920 }
921 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
922 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000923 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
924 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
925 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700926 return false;
927 }
928
929 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
930
931 // Handle String case first
932 if (left_val_->final_type_ == Type::STRING) {
Steven Moreland22e36112020-10-01 00:50:45 +0000933 AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700934 if (!OPEQ("+")) {
Steven Moreland22e36112020-10-01 00:50:45 +0000935 AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'.";
Will McVickerd7d18df2019-09-12 13:40:50 -0700936 final_type_ = Type::ERROR;
937 is_valid_ = false;
938 return false;
939 }
940
941 // Remove trailing " from lhs
942 const string& lhs = left_val_->final_string_value_;
943 if (lhs.back() != '"') {
944 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
945 final_type_ = Type::ERROR;
946 is_valid_ = false;
947 return false;
948 }
949 const string& rhs = right_val_->final_string_value_;
950 // Remove starting " from rhs
951 if (rhs.front() != '"') {
952 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
953 final_type_ = Type::ERROR;
954 is_valid_ = false;
955 return false;
956 }
957
958 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
959 final_type_ = Type::STRING;
960 return true;
961 }
962
Will McVickerd7d18df2019-09-12 13:40:50 -0700963 // CASE: + - * / % | ^ & < > <= >= == !=
964 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700965 // promoted kind for both operands.
966 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
967 IntegralPromotion(right_val_->final_type_));
968 // result kind.
969 final_type_ = isArithmeticOrBitflip
970 ? promoted // arithmetic or bitflip operators generates promoted type
971 : Type::BOOLEAN; // comparison operators generates bool
972
Steven Morelande1ff67e2020-07-16 23:22:36 +0000973#define CASE_BINARY_COMMON(__type__) \
974 return handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
975 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700976
977 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
978 is_valid_ = false; return false;)
979 }
980
981 // CASE: << >>
982 string newOp = op_;
983 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -0700984 // promoted kind for both operands.
985 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
986 IntegralPromotion(right_val_->final_type_));
987 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700988 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -0800989 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -0700990 // is defined as shifting into the other direction.
991 newOp = OPEQ("<<") ? ">>" : "<<";
992 numBits = -numBits;
993 }
994
Devin Moore04823022020-09-11 10:43:35 -0700995#define CASE_SHIFT(__type__) \
996 return handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
997 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700998
999 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
1000 is_valid_ = false; return false;)
1001 }
1002
1003 // CASE: && ||
1004 if (OP_IS_BIN_LOGICAL) {
1005 final_type_ = Type::BOOLEAN;
1006 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +00001007 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
1008 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -07001009 }
1010
1011 SHOULD_NOT_REACH();
1012 is_valid_ = false;
1013 return false;
1014}
1015
Will McVickerd7d18df2019-09-12 13:40:50 -07001016AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
1017 int64_t parsed_value, const string& checked_value)
1018 : AidlNode(location),
1019 type_(parsed_type),
1020 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001021 final_type_(parsed_type),
1022 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +00001023 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
1024 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001025}
Will McVickerefd970d2019-09-25 15:28:30 -07001026
1027AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -07001028 const string& checked_value)
1029 : AidlNode(location),
1030 type_(type),
1031 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001032 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001033 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001034 switch (type_) {
1035 case Type::INT8:
1036 case Type::INT32:
1037 case Type::INT64:
1038 case Type::ARRAY:
1039 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
1040 break;
1041 default:
1042 break;
1043 }
1044}
1045
1046AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +09001047 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
1048 const std::string& value)
Will McVickerd7d18df2019-09-12 13:40:50 -07001049 : AidlNode(location),
1050 type_(type),
1051 values_(std::move(*values)),
Jooyung Han29813842020-12-08 01:28:03 +09001052 value_(value),
Will McVickerd7d18df2019-09-12 13:40:50 -07001053 is_valid_(false),
1054 is_evaluated_(false),
1055 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +00001056 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -07001057}
1058
1059AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
1060 std::unique_ptr<AidlConstantValue> rval)
1061 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
1062 unary_(std::move(rval)),
1063 op_(op) {
1064 final_type_ = Type::UNARY;
1065}
1066
1067AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
1068 std::unique_ptr<AidlConstantValue> lval,
1069 const string& op,
1070 std::unique_ptr<AidlConstantValue> rval)
1071 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
1072 left_val_(std::move(lval)),
1073 right_val_(std::move(rval)),
1074 op_(op) {
1075 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -07001076}