blob: deedacef44f4965f54361188d60a1877debdddac [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>
40class OverflowGuard {
41 public:
42 OverflowGuard(T value) : mValue(value) {}
43 bool Overflowed() const { return mOverflowed; }
44
45 T operator+() { return +mValue; }
46 T operator-() {
47 if (isMin()) {
48 mOverflowed = true;
49 return 0;
50 }
51 return -mValue;
52 }
53 T operator!() { return !mValue; }
54 T operator~() { return ~mValue; }
55
56 T operator+(T o) {
57 T out;
58 mOverflowed = __builtin_add_overflow(mValue, o, &out);
59 return out;
60 }
61 T operator-(T o) {
62 T out;
63 mOverflowed = __builtin_sub_overflow(mValue, o, &out);
64 return out;
65 }
66 T operator*(T o) {
67 T out;
68#ifdef _WIN32
69 // ___mulodi4 not on windows https://bugs.llvm.org/show_bug.cgi?id=46669
70 // we should still get an error here from ubsan, but the nice error
71 // is needed on linux for aidl_parser_fuzzer, where we are more
72 // concerned about overflows elsewhere in the compiler in addition to
73 // those in interfaces.
74 out = mValue * o;
75#else
76 mOverflowed = __builtin_mul_overflow(mValue, o, &out);
77#endif
78 return out;
79 }
80 T operator/(T o) {
81 if (o == 0 || (isMin() && o == -1)) {
82 mOverflowed = true;
83 return 0;
84 }
85 return mValue / o;
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) { return mValue | o; }
95 T operator^(T o) { return mValue ^ o; }
96 T operator&(T o) { return mValue & o; }
97 T operator<(T o) { return mValue < o; }
98 T operator>(T o) { return mValue > o; }
99 T operator<=(T o) { return mValue <= o; }
100 T operator>=(T o) { return mValue >= o; }
101 T operator==(T o) { return mValue == o; }
102 T operator!=(T o) { return mValue != o; }
103 T operator>>(T o) {
Devin Moorebc373792020-09-17 09:45:46 -0700104 if (o < 0 || o > static_cast<T>(sizeof(T) * 8) || mValue < 0) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000105 mOverflowed = true;
106 return 0;
107 }
108 return mValue >> o;
109 }
110 T operator<<(T o) {
Devin Moorebc373792020-09-17 09:45:46 -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) { return mValue || o; }
118 T operator&&(T o) { return mValue && o; }
119
120 private:
121 bool isMin() { return mValue == std::numeric_limits<T>::min(); }
122
123 T mValue;
124 bool mOverflowed = false;
125};
126
127template <typename T>
128bool processGuard(const OverflowGuard<T>& guard, const AidlConstantValue& context) {
129 if (guard.Overflowed()) {
130 AIDL_ERROR(context) << "Constant expression computation overflows.";
131 return false;
132 }
133 return true;
134}
135
136// TODO: factor out all these macros
Steven Moreland21780812020-09-11 01:29:45 +0000137#define SHOULD_NOT_REACH() AIDL_FATAL(AIDL_LOCATION_HERE) << "Should not reach."
Will McVickerd7d18df2019-09-12 13:40:50 -0700138#define OPEQ(__y__) (string(op_) == string(__y__))
Steven Moreland0521bf32020-09-09 22:44:07 +0000139#define COMPUTE_UNARY(T, __op__) \
140 if (op == string(#__op__)) { \
141 OverflowGuard<T> guard(val); \
142 *out = __op__ guard; \
143 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000144 }
Steven Moreland0521bf32020-09-09 22:44:07 +0000145#define COMPUTE_BINARY(T, __op__) \
146 if (op == string(#__op__)) { \
147 OverflowGuard<T> guard(lval); \
148 *out = guard __op__ rval; \
149 return processGuard(guard, context); \
Steven Morelande1ff67e2020-07-16 23:22:36 +0000150 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700151#define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%"))
152#define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&"))
153#define OP_IS_BIN_COMP \
154 (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!="))
155#define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<"))
156#define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&"))
157
158// NOLINT to suppress missing parentheses warnings about __def__.
159#define SWITCH_KIND(__cond__, __action__, __def__) \
160 switch (__cond__) { \
161 case Type::BOOLEAN: \
162 __action__(bool); \
163 case Type::INT8: \
164 __action__(int8_t); \
165 case Type::INT32: \
166 __action__(int32_t); \
167 case Type::INT64: \
168 __action__(int64_t); \
169 default: \
170 __def__; /* NOLINT */ \
171 }
172
173template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000174bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000175 COMPUTE_UNARY(T, +)
176 COMPUTE_UNARY(T, -)
177 COMPUTE_UNARY(T, !)
178 COMPUTE_UNARY(T, ~)
Steven Moreland720a3cc2020-07-16 23:44:59 +0000179 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
180 return false;
181}
182template <>
183bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000184 COMPUTE_UNARY(bool, +)
185 COMPUTE_UNARY(bool, -)
186 COMPUTE_UNARY(bool, !)
Yifan Hongf17e3a72020-02-20 17:34:58 -0800187
Steven Moreland720a3cc2020-07-16 23:44:59 +0000188 if (op == "~") {
189 AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true.";
190 return false;
191 }
Steven Morelande1ff67e2020-07-16 23:22:36 +0000192 AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val;
193 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700194}
195
196template <class T>
Steven Morelande1ff67e2020-07-16 23:22:36 +0000197bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval,
198 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000199 COMPUTE_BINARY(T, +)
200 COMPUTE_BINARY(T, -)
201 COMPUTE_BINARY(T, *)
202 COMPUTE_BINARY(T, /)
203 COMPUTE_BINARY(T, %)
204 COMPUTE_BINARY(T, |)
205 COMPUTE_BINARY(T, ^)
206 COMPUTE_BINARY(T, &)
Will McVickerd7d18df2019-09-12 13:40:50 -0700207 // comparison operators: return 0 or 1 by nature.
Steven Moreland0521bf32020-09-09 22:44:07 +0000208 COMPUTE_BINARY(T, ==)
209 COMPUTE_BINARY(T, !=)
210 COMPUTE_BINARY(T, <)
211 COMPUTE_BINARY(T, >)
212 COMPUTE_BINARY(T, <=)
213 COMPUTE_BINARY(T, >=)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000214
215 AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval;
216 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700217}
218
219template <class T>
Devin Moore04823022020-09-11 10:43:35 -0700220bool handleShift(const AidlConstantValue& context, T lval, const string& op, T rval, int64_t* out) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700221 // just cast rval to int64_t and it should fit.
Steven Moreland0521bf32020-09-09 22:44:07 +0000222 COMPUTE_BINARY(T, >>)
223 COMPUTE_BINARY(T, <<)
Steven Morelande1ff67e2020-07-16 23:22:36 +0000224
225 AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval;
226 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700227}
228
Steven Morelande1ff67e2020-07-16 23:22:36 +0000229bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval,
230 int64_t* out) {
Steven Moreland0521bf32020-09-09 22:44:07 +0000231 COMPUTE_BINARY(bool, ||);
232 COMPUTE_BINARY(bool, &&);
Steven Morelande1ff67e2020-07-16 23:22:36 +0000233
234 AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval;
Will McVickerd7d18df2019-09-12 13:40:50 -0700235 return false;
236}
237
Will McVickerefd970d2019-09-25 15:28:30 -0700238static bool isValidLiteralChar(char c) {
239 return !(c <= 0x1f || // control characters are < 0x20
240 c >= 0x7f || // DEL is 0x7f
241 c == '\\'); // Disallow backslashes for future proofing.
242}
243
Will McVickerd7d18df2019-09-12 13:40:50 -0700244bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) {
245 // Verify the unary type here
246 switch (type) {
247 case Type::BOOLEAN: // fall-through
248 case Type::INT8: // fall-through
249 case Type::INT32: // fall-through
250 case Type::INT64:
251 return true;
252 case Type::FLOATING:
253 return (op == "+" || op == "-");
254 default:
255 return false;
256 }
257}
258
259bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) {
260 switch (t1) {
261 case Type::STRING:
262 if (t2 == Type::STRING) {
263 return true;
264 }
265 break;
266 case Type::BOOLEAN: // fall-through
267 case Type::INT8: // fall-through
268 case Type::INT32: // fall-through
269 case Type::INT64:
270 switch (t2) {
271 case Type::BOOLEAN: // fall-through
272 case Type::INT8: // fall-through
273 case Type::INT32: // fall-through
274 case Type::INT64:
275 return true;
276 break;
277 default:
278 break;
279 }
280 break;
281 default:
282 break;
283 }
284
285 return false;
286}
287
288// Returns the promoted kind for both operands
289AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left,
290 Type right) {
291 // These are handled as special cases
Steven Moreland21780812020-09-11 01:29:45 +0000292 AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE);
293 AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE);
Will McVickerd7d18df2019-09-12 13:40:50 -0700294
295 // Kinds in concern: bool, (u)int[8|32|64]
296 if (left == right) return left; // easy case
297 if (left == Type::BOOLEAN) return right;
298 if (right == Type::BOOLEAN) return left;
299
300 return left < right ? right : left;
301}
302
303// Returns the promoted integral type where INT32 is the smallest type
304AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) {
305 return (Type::INT32 < in) ? in : Type::INT32;
306}
307
308template <typename T>
309T AidlConstantValue::cast() const {
Steven Moreland21780812020-09-11 01:29:45 +0000310 AIDL_FATAL_IF(!is_evaluated_, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700311
312#define CASE_CAST_T(__type__) return static_cast<T>(static_cast<__type__>(final_value_));
313
314 SWITCH_KIND(final_type_, CASE_CAST_T, SHOULD_NOT_REACH(); return 0;);
315}
316
Steven Moreland541788d2020-05-21 22:05:52 +0000317AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) {
318 AidlLocation location = specifier.GetLocation();
319
320 // allocation of int[0] is a bit wasteful in Java
321 if (specifier.IsArray()) {
322 return nullptr;
323 }
324
325 const std::string name = specifier.GetName();
326 if (name == "boolean") {
327 return Boolean(location, false);
328 }
329 if (name == "byte" || name == "int" || name == "long") {
330 return Integral(location, "0");
331 }
332 if (name == "float") {
333 return Floating(location, "0.0f");
334 }
335 if (name == "double") {
336 return Floating(location, "0.0");
337 }
338 return nullptr;
339}
340
Will McVickerefd970d2019-09-25 15:28:30 -0700341AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
342 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
343}
344
345AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800346 const std::string explicit_value = string("'") + value + "'";
Will McVickerefd970d2019-09-25 15:28:30 -0700347 if (!isValidLiteralChar(value)) {
348 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800349 return new AidlConstantValue(location, Type::ERROR, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700350 }
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800351 return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
Will McVickerefd970d2019-09-25 15:28:30 -0700352}
353
354AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
355 const std::string& value) {
356 return new AidlConstantValue(location, Type::FLOATING, value);
357}
358
Will McVickerd7d18df2019-09-12 13:40:50 -0700359bool AidlConstantValue::IsHex(const string& value) {
Steven Morelandcef22662020-07-08 20:54:28 +0000360 return StartsWith(value, "0x") || StartsWith(value, "0X");
Will McVickerefd970d2019-09-25 15:28:30 -0700361}
362
Will McVickerd7d18df2019-09-12 13:40:50 -0700363bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value,
364 Type* parsed_type) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700365 if (parsed_value == nullptr || parsed_type == nullptr) {
366 return false;
367 }
368
Steven Morelandcef22662020-07-08 20:54:28 +0000369 const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L');
370 const std::string value_substr = isLong ? value.substr(0, value.size() - 1) : value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700371
Steven Morelandcef22662020-07-08 20:54:28 +0000372 if (IsHex(value)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700373 // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to
374 // handle that when computing constant expressions, then we need to
375 // represent 0xffffffff as a uint32_t. However, AIDL only has signed types;
376 // so we parse as an unsigned int when possible and then cast to a signed
377 // int. One example of this is in ICameraService.aidl where a constant int
378 // is used for bit manipulations which ideally should be handled with an
379 // unsigned int.
Steven Morelandcef22662020-07-08 20:54:28 +0000380 //
381 // Note, for historical consistency, we need to consider small hex values
382 // as an integral type. Recognizing them as INT8 could break some files,
383 // even though it would simplify this code.
384 if (uint32_t rawValue32;
385 !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700386 *parsed_value = static_cast<int32_t>(rawValue32);
387 *parsed_type = Type::INT32;
Steven Morelandcef22662020-07-08 20:54:28 +0000388 } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) {
389 *parsed_value = static_cast<int64_t>(rawValue64);
Will McVickerd7d18df2019-09-12 13:40:50 -0700390 *parsed_type = Type::INT64;
Steven Morelandcef22662020-07-08 20:54:28 +0000391 } else {
392 *parsed_value = 0;
393 *parsed_type = Type::ERROR;
394 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700395 }
396 return true;
397 }
398
Steven Morelandcef22662020-07-08 20:54:28 +0000399 if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) {
400 *parsed_value = 0;
Will McVickerd7d18df2019-09-12 13:40:50 -0700401 *parsed_type = Type::ERROR;
402 return false;
403 }
404
Steven Morelandcef22662020-07-08 20:54:28 +0000405 if (isLong) {
406 *parsed_type = Type::INT64;
407 } else {
Will McVickerd7d18df2019-09-12 13:40:50 -0700408 // guess literal type.
409 if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) {
410 *parsed_type = Type::INT8;
411 } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) {
412 *parsed_type = Type::INT32;
413 } else {
414 *parsed_type = Type::INT64;
415 }
416 }
417 return true;
418}
419
420AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) {
Steven Moreland21780812020-09-11 01:29:45 +0000421 AIDL_FATAL_IF(value.empty(), location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700422
423 Type parsed_type;
424 int64_t parsed_value = 0;
425 bool success = ParseIntegral(value, &parsed_value, &parsed_type);
426 if (!success) {
427 return nullptr;
428 }
429
430 return new AidlConstantValue(location, parsed_type, parsed_value, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700431}
432
433AidlConstantValue* AidlConstantValue::Array(
Will McVickerd7d18df2019-09-12 13:40:50 -0700434 const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
Steven Moreland21780812020-09-11 01:29:45 +0000435 AIDL_FATAL_IF(values == nullptr, location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700436 return new AidlConstantValue(location, Type::ARRAY, std::move(values));
Will McVickerefd970d2019-09-25 15:28:30 -0700437}
438
Will McVickerd7d18df2019-09-12 13:40:50 -0700439AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
Will McVickerefd970d2019-09-25 15:28:30 -0700440 for (size_t i = 0; i < value.length(); ++i) {
441 if (!isValidLiteralChar(value[i])) {
442 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
443 << value << "'";
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800444 return new AidlConstantValue(location, Type::ERROR, value);
Will McVickerefd970d2019-09-25 15:28:30 -0700445 }
446 }
447
448 return new AidlConstantValue(location, Type::STRING, value);
449}
450
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700451AidlConstantValue* AidlConstantValue::ShallowIntegralCopy(const AidlConstantValue& other) {
Daniel Norman3cce7cd2020-02-07 13:25:12 -0800452 // TODO(b/141313220) Perform a full copy instead of parsing+unparsing
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700453 AidlTypeSpecifier type = AidlTypeSpecifier(AIDL_LOCATION_HERE, "long", false, nullptr, "");
Steven Moreland65606612019-11-10 21:21:25 -0800454 // TODO(b/142722772) CheckValid() should be called before ValueString()
Steven Morelandcdedd9b2019-12-02 10:54:47 -0800455 if (!other.CheckValid() || !other.evaluate(type)) {
Steven Moreland59e53e42019-11-26 20:38:08 -0800456 AIDL_ERROR(other) << "Failed to parse expression as integer: " << other.value_;
457 return nullptr;
458 }
459 const std::string& value = other.ValueString(type, AidlConstantValueDecorator);
460 if (value.empty()) {
461 return nullptr; // error already logged
Daniel Normanb28684e2019-10-17 15:31:39 -0700462 }
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700463
Steven Moreland59e53e42019-11-26 20:38:08 -0800464 AidlConstantValue* result = Integral(AIDL_LOCATION_HERE, value);
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700465 if (result == nullptr) {
466 AIDL_FATAL(other) << "Unable to perform ShallowIntegralCopy.";
467 }
468 return result;
Daniel Normanb28684e2019-10-17 15:31:39 -0700469}
470
Will McVickerd7d18df2019-09-12 13:40:50 -0700471string AidlConstantValue::ValueString(const AidlTypeSpecifier& type,
472 const ConstantValueDecorator& decorator) const {
Will McVickerefd970d2019-09-25 15:28:30 -0700473 if (type.IsGeneric()) {
474 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
475 return "";
476 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700477 if (!is_evaluated_) {
478 // TODO(b/142722772) CheckValid() should be called before ValueString()
479 bool success = CheckValid();
480 success &= evaluate(type);
481 if (!success) {
482 // the detailed error message shall be printed in evaluate
483 return "";
484 }
Will McVickerefd970d2019-09-25 15:28:30 -0700485 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700486 if (!is_valid_) {
487 AIDL_ERROR(this) << "Invalid constant value: " + value_;
488 return "";
489 }
490 const string& type_string = type.GetName();
491 int err = 0;
Will McVickerefd970d2019-09-25 15:28:30 -0700492
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 switch (final_type_) {
494 case Type::CHARACTER:
495 if (type_string == "char") {
496 return decorator(type, final_string_value_);
497 }
498 err = -1;
499 break;
500 case Type::STRING:
501 if (type_string == "String") {
502 return decorator(type, final_string_value_);
503 }
504 err = -1;
505 break;
506 case Type::BOOLEAN: // fall-through
507 case Type::INT8: // fall-through
508 case Type::INT32: // fall-through
509 case Type::INT64:
510 if (type_string == "byte") {
511 if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) {
512 err = -1;
513 break;
514 }
515 return decorator(type, std::to_string(static_cast<int8_t>(final_value_)));
516 } else if (type_string == "int") {
517 if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) {
518 err = -1;
519 break;
520 }
521 return decorator(type, std::to_string(static_cast<int32_t>(final_value_)));
522 } else if (type_string == "long") {
523 return decorator(type, std::to_string(final_value_));
524 } else if (type_string == "boolean") {
525 return decorator(type, final_value_ ? "true" : "false");
526 }
527 err = -1;
528 break;
529 case Type::ARRAY: {
530 if (!type.IsArray()) {
531 err = -1;
532 break;
533 }
534 vector<string> value_strings;
535 value_strings.reserve(values_.size());
Will McVickerefd970d2019-09-25 15:28:30 -0700536 bool success = true;
Will McVickerd7d18df2019-09-12 13:40:50 -0700537
Will McVickerefd970d2019-09-25 15:28:30 -0700538 for (const auto& value : values_) {
539 const AidlTypeSpecifier& array_base = type.ArrayBase();
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 const string value_string = value->ValueString(array_base, decorator);
541 if (value_string.empty()) {
542 success = false;
543 break;
544 }
545 value_strings.push_back(value_string);
Will McVickerefd970d2019-09-25 15:28:30 -0700546 }
547 if (!success) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700548 err = -1;
549 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700550 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700551
552 return decorator(type, "{" + Join(value_strings, ", ") + "}");
Will McVickerefd970d2019-09-25 15:28:30 -0700553 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700554 case Type::FLOATING: {
555 std::string_view raw_view(value_.c_str());
556 bool is_float_literal = ConsumeSuffix(&raw_view, "f");
557 std::string stripped_value = std::string(raw_view);
Will McVickerefd970d2019-09-25 15:28:30 -0700558
559 if (type_string == "double") {
560 double parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700561 if (!android::base::ParseDouble(stripped_value, &parsed_value)) {
562 AIDL_ERROR(this) << "Could not parse " << value_;
563 err = -1;
564 break;
565 }
Will McVickerefd970d2019-09-25 15:28:30 -0700566 return decorator(type, std::to_string(parsed_value));
567 }
568 if (is_float_literal && type_string == "float") {
569 float parsed_value;
Will McVickerd7d18df2019-09-12 13:40:50 -0700570 if (!android::base::ParseFloat(stripped_value, &parsed_value)) {
571 AIDL_ERROR(this) << "Could not parse " << value_;
572 err = -1;
573 break;
574 }
Will McVickerefd970d2019-09-25 15:28:30 -0700575 return decorator(type, std::to_string(parsed_value) + "f");
576 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700577 err = -1;
578 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700579 }
Will McVickerefd970d2019-09-25 15:28:30 -0700580 default:
Will McVickerd7d18df2019-09-12 13:40:50 -0700581 err = -1;
582 break;
Will McVickerefd970d2019-09-25 15:28:30 -0700583 }
584
Steven Moreland21780812020-09-11 01:29:45 +0000585 AIDL_FATAL_IF(err == 0, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string;
Will McVickerefd970d2019-09-25 15:28:30 -0700587 return "";
Will McVickerd7d18df2019-09-12 13:40:50 -0700588}
589
590bool AidlConstantValue::CheckValid() const {
591 // Nothing needs to be checked here. The constant value will be validated in
592 // the constructor or in the evaluate() function.
593 if (is_evaluated_) return is_valid_;
594
595 switch (type_) {
596 case Type::BOOLEAN: // fall-through
597 case Type::INT8: // fall-through
598 case Type::INT32: // fall-through
599 case Type::INT64: // fall-through
600 case Type::ARRAY: // fall-through
601 case Type::CHARACTER: // fall-through
602 case Type::STRING: // fall-through
603 case Type::FLOATING: // fall-through
604 case Type::UNARY: // fall-through
605 case Type::BINARY:
606 is_valid_ = true;
607 break;
Steven Moreland4ff04aa2019-12-02 10:44:29 -0800608 case Type::ERROR:
609 return false;
Will McVickerd7d18df2019-09-12 13:40:50 -0700610 default:
611 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
612 return false;
613 }
614
615 return true;
616}
617
618bool AidlConstantValue::evaluate(const AidlTypeSpecifier& type) const {
619 if (is_evaluated_) {
620 return is_valid_;
621 }
622 int err = 0;
623 is_evaluated_ = true;
624
625 switch (type_) {
626 case Type::ARRAY: {
627 if (!type.IsArray()) {
628 AIDL_ERROR(this) << "Invalid constant array type: " << type.GetName();
629 err = -1;
630 break;
631 }
632 Type array_type = Type::ERROR;
633 bool success = true;
634 for (const auto& value : values_) {
635 success = value->CheckValid();
636 if (success) {
637 success = value->evaluate(type.ArrayBase());
638 if (!success) {
639 AIDL_ERROR(this) << "Invalid array element: " << value->value_;
640 break;
641 }
642 if (array_type == Type::ERROR) {
643 array_type = value->final_type_;
644 } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type,
645 value->final_type_)) {
646 AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_)
647 << ". Expecting type compatible with " << ToString(array_type);
648 success = false;
649 break;
650 }
651 } else {
652 break;
653 }
654 }
655 if (!success) {
656 err = -1;
657 break;
658 }
659 final_type_ = type_;
660 break;
661 }
662 case Type::BOOLEAN:
663 if ((value_ != "true") && (value_ != "false")) {
664 AIDL_ERROR(this) << "Invalid constant boolean value: " << value_;
665 err = -1;
666 break;
667 }
668 final_value_ = (value_ == "true") ? 1 : 0;
669 final_type_ = type_;
670 break;
671 case Type::INT8: // fall-through
672 case Type::INT32: // fall-through
673 case Type::INT64:
674 // Parsing happens in the constructor
675 final_type_ = type_;
676 break;
677 case Type::CHARACTER: // fall-through
678 case Type::STRING:
679 final_string_value_ = value_;
680 final_type_ = type_;
681 break;
682 case Type::FLOATING:
683 // Just parse on the fly in ValueString
684 final_type_ = type_;
685 break;
686 default:
687 AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_);
688 err = -1;
689 }
690
691 return (err == 0) ? true : false;
Will McVickerefd970d2019-09-25 15:28:30 -0700692}
693
694string AidlConstantValue::ToString(Type type) {
695 switch (type) {
Will McVickerefd970d2019-09-25 15:28:30 -0700696 case Type::BOOLEAN:
697 return "a literal boolean";
Will McVickerd7d18df2019-09-12 13:40:50 -0700698 case Type::INT8:
699 return "an int8 literal";
700 case Type::INT32:
701 return "an int32 literal";
702 case Type::INT64:
703 return "an int64 literal";
Steven Morelanda923a722019-11-26 20:08:30 -0800704 case Type::ARRAY:
705 return "a literal array";
706 case Type::CHARACTER:
707 return "a literal char";
Will McVickerefd970d2019-09-25 15:28:30 -0700708 case Type::STRING:
709 return "a literal string";
Steven Morelanda923a722019-11-26 20:08:30 -0800710 case Type::FLOATING:
711 return "a literal float";
Will McVickerd7d18df2019-09-12 13:40:50 -0700712 case Type::UNARY:
713 return "a unary expression";
714 case Type::BINARY:
715 return "a binary expression";
Steven Morelanda923a722019-11-26 20:08:30 -0800716 case Type::ERROR:
Steven Moreland21780812020-09-11 01:29:45 +0000717 AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program";
Steven Morelanda923a722019-11-26 20:08:30 -0800718 return "";
Will McVickerefd970d2019-09-25 15:28:30 -0700719 default:
Steven Moreland21780812020-09-11 01:29:45 +0000720 AIDL_FATAL(AIDL_LOCATION_HERE)
721 << "aidl internal error: unknown constant type: " << static_cast<int>(type);
Will McVickerefd970d2019-09-25 15:28:30 -0700722 return ""; // not reached
723 }
724}
725
Will McVickerd7d18df2019-09-12 13:40:50 -0700726bool AidlUnaryConstExpression::CheckValid() const {
727 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000728 AIDL_FATAL_IF(unary_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700729
730 is_valid_ = unary_->CheckValid();
731 if (!is_valid_) {
732 final_type_ = Type::ERROR;
733 return false;
734 }
735
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800736 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700737}
738
739bool AidlUnaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
740 if (is_evaluated_) {
741 return is_valid_;
742 }
743 is_evaluated_ = true;
744
745 // Recursively evaluate the expression tree
746 if (!unary_->is_evaluated_) {
747 // TODO(b/142722772) CheckValid() should be called before ValueString()
748 bool success = CheckValid();
749 success &= unary_->evaluate(type);
750 if (!success) {
751 is_valid_ = false;
752 return false;
753 }
754 }
Devin Moorec233fb82020-04-07 11:13:44 -0700755 if (!IsCompatibleType(unary_->final_type_, op_)) {
756 AIDL_ERROR(unary_) << "'" << op_ << "'"
757 << " is not compatible with " << ToString(unary_->final_type_)
758 << ": " + value_;
759 is_valid_ = false;
760 return false;
761 }
762 if (!unary_->is_valid_) {
763 AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700764 is_valid_ = false;
765 return false;
766 }
767 final_type_ = unary_->final_type_;
768
769 if (final_type_ == Type::FLOATING) {
770 // don't do anything here. ValueString() will handle everything.
771 is_valid_ = true;
772 return true;
773 }
774
Steven Morelande1ff67e2020-07-16 23:22:36 +0000775#define CASE_UNARY(__type__) \
776 return handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700777
778 SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
779 is_valid_ = false; return false;)
780}
781
Will McVickerd7d18df2019-09-12 13:40:50 -0700782bool AidlBinaryConstExpression::CheckValid() const {
783 bool success = false;
784 if (is_evaluated_) return is_valid_;
Steven Moreland21780812020-09-11 01:29:45 +0000785 AIDL_FATAL_IF(left_val_ == nullptr, this);
786 AIDL_FATAL_IF(right_val_ == nullptr, this);
Will McVickerd7d18df2019-09-12 13:40:50 -0700787
788 success = left_val_->CheckValid();
789 if (!success) {
790 final_type_ = Type::ERROR;
791 AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_;
792 }
793
794 success = right_val_->CheckValid();
795 if (!success) {
796 AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_;
797 final_type_ = Type::ERROR;
798 }
799
800 if (final_type_ == Type::ERROR) {
801 is_valid_ = false;
802 return false;
803 }
804
805 is_valid_ = true;
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800806 return AidlConstantValue::CheckValid();
Will McVickerd7d18df2019-09-12 13:40:50 -0700807}
808
809bool AidlBinaryConstExpression::evaluate(const AidlTypeSpecifier& type) const {
810 if (is_evaluated_) {
811 return is_valid_;
812 }
813 is_evaluated_ = true;
Steven Moreland21780812020-09-11 01:29:45 +0000814 AIDL_FATAL_IF(left_val_ == nullptr, type);
815 AIDL_FATAL_IF(right_val_ == nullptr, type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700816
817 // Recursively evaluate the binary expression tree
818 if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) {
819 // TODO(b/142722772) CheckValid() should be called before ValueString()
820 bool success = CheckValid();
821 success &= left_val_->evaluate(type);
822 success &= right_val_->evaluate(type);
823 if (!success) {
824 is_valid_ = false;
825 return false;
826 }
827 }
828 if (!left_val_->is_valid_ || !right_val_->is_valid_) {
829 is_valid_ = false;
830 return false;
831 }
832 is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_);
833 if (!is_valid_) {
Steven Moreland1f9f2212020-09-24 18:20:15 +0000834 AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on "
835 << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType())
836 << ".";
Will McVickerd7d18df2019-09-12 13:40:50 -0700837 return false;
838 }
839
840 bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP;
841
842 // Handle String case first
843 if (left_val_->final_type_ == Type::STRING) {
844 if (!OPEQ("+")) {
845 // invalid operation on strings
846 final_type_ = Type::ERROR;
847 is_valid_ = false;
848 return false;
849 }
850
851 // Remove trailing " from lhs
852 const string& lhs = left_val_->final_string_value_;
853 if (lhs.back() != '"') {
854 AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote.";
855 final_type_ = Type::ERROR;
856 is_valid_ = false;
857 return false;
858 }
859 const string& rhs = right_val_->final_string_value_;
860 // Remove starting " from rhs
861 if (rhs.front() != '"') {
862 AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote.";
863 final_type_ = Type::ERROR;
864 is_valid_ = false;
865 return false;
866 }
867
868 final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end());
869 final_type_ = Type::STRING;
870 return true;
871 }
872
Will McVickerd7d18df2019-09-12 13:40:50 -0700873 // CASE: + - * / % | ^ & < > <= >= == !=
874 if (isArithmeticOrBitflip || OP_IS_BIN_COMP) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700875 // promoted kind for both operands.
876 Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
877 IntegralPromotion(right_val_->final_type_));
878 // result kind.
879 final_type_ = isArithmeticOrBitflip
880 ? promoted // arithmetic or bitflip operators generates promoted type
881 : Type::BOOLEAN; // comparison operators generates bool
882
Steven Morelande1ff67e2020-07-16 23:22:36 +0000883#define CASE_BINARY_COMMON(__type__) \
884 return handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \
885 static_cast<__type__>(right_val_->final_value_), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700886
887 SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
888 is_valid_ = false; return false;)
889 }
890
891 // CASE: << >>
892 string newOp = op_;
893 if (OP_IS_BIN_SHIFT) {
Devin Moore04823022020-09-11 10:43:35 -0700894 // promoted kind for both operands.
895 final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_),
896 IntegralPromotion(right_val_->final_type_));
897 auto numBits = right_val_->final_value_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700898 if (numBits < 0) {
Steven Moreland74d3f552020-02-04 15:57:50 -0800899 // shifting with negative number of bits is undefined in C. In AIDL it
Will McVickerd7d18df2019-09-12 13:40:50 -0700900 // is defined as shifting into the other direction.
901 newOp = OPEQ("<<") ? ">>" : "<<";
902 numBits = -numBits;
903 }
904
Devin Moore04823022020-09-11 10:43:35 -0700905#define CASE_SHIFT(__type__) \
906 return handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \
907 static_cast<__type__>(numBits), &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700908
909 SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR;
910 is_valid_ = false; return false;)
911 }
912
913 // CASE: && ||
914 if (OP_IS_BIN_LOGICAL) {
915 final_type_ = Type::BOOLEAN;
916 // easy; everything is bool.
Steven Morelande1ff67e2020-07-16 23:22:36 +0000917 return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_,
918 &final_value_);
Will McVickerd7d18df2019-09-12 13:40:50 -0700919 }
920
921 SHOULD_NOT_REACH();
922 is_valid_ = false;
923 return false;
924}
925
Will McVickerd7d18df2019-09-12 13:40:50 -0700926AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
927 int64_t parsed_value, const string& checked_value)
928 : AidlNode(location),
929 type_(parsed_type),
930 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -0700931 final_type_(parsed_type),
932 final_value_(parsed_value) {
Steven Moreland21780812020-09-11 01:29:45 +0000933 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
934 AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700935}
Will McVickerefd970d2019-09-25 15:28:30 -0700936
937AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700938 const string& checked_value)
939 : AidlNode(location),
940 type_(type),
941 value_(checked_value),
Will McVickerd7d18df2019-09-12 13:40:50 -0700942 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +0000943 AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700944 switch (type_) {
945 case Type::INT8:
946 case Type::INT32:
947 case Type::INT64:
948 case Type::ARRAY:
949 AIDL_FATAL(this) << "Invalid type: " << ToString(type_);
950 break;
951 default:
952 break;
953 }
954}
955
956AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
957 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values)
958 : AidlNode(location),
959 type_(type),
960 values_(std::move(*values)),
961 is_valid_(false),
962 is_evaluated_(false),
963 final_type_(type) {
Steven Moreland21780812020-09-11 01:29:45 +0000964 AIDL_FATAL_IF(type_ != Type::ARRAY, location);
Will McVickerd7d18df2019-09-12 13:40:50 -0700965}
966
967AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op,
968 std::unique_ptr<AidlConstantValue> rval)
969 : AidlConstantValue(location, Type::UNARY, op + rval->value_),
970 unary_(std::move(rval)),
971 op_(op) {
972 final_type_ = Type::UNARY;
973}
974
975AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location,
976 std::unique_ptr<AidlConstantValue> lval,
977 const string& op,
978 std::unique_ptr<AidlConstantValue> rval)
979 : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_),
980 left_val_(std::move(lval)),
981 right_val_(std::move(rval)),
982 op_(op) {
983 final_type_ = Type::BINARY;
Will McVickerefd970d2019-09-25 15:28:30 -0700984}