Skip to content

Commit 6920940

Browse files
committed
Enable building aidl
1 parent d10f029 commit 6920940

7 files changed

+408
-6
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ find_package(BISON REQUIRED)
3030
find_package(EXPAT REQUIRED)
3131
find_package(FLEX REQUIRED)
3232
find_package(fmt REQUIRED)
33+
find_package(GTest REQUIRED)
3334
find_package(Protobuf CONFIG)
3435
find_package(Protobuf REQUIRED)
3536
include_directories(${Protobuf_INCLUDE_DIRS})

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ cmake and system provided toolchain.
3535

3636
## Supported Tools
3737

38+
* aidl - Android Interface Definition Language (AIDL) Compiler
39+
3840
* aapt - Android Asset Packaging Tool
3941

4042
* aapt2 - Android Asset Packaging Tool, version 2
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
From 5229cb1d6df508c965c918fb782493fa32d574e9 Mon Sep 17 00:00:00 2001
2+
From: Biswapriyo Nath <[email protected]>
3+
Date: Wed, 1 Jan 2025 00:00:00 +0000
4+
Subject: [PATCH] aidl: Move AidlConstantValue class definition
5+
6+
This fixes the following compiler error.
7+
8+
aidl_language.h:403:3: note: in instantiation of member function
9+
'std::vector<std::unique_ptr<AidlConstantValue>>::~vector' requested here
10+
403 | FixedSizeArray(std::unique_ptr<AidlConstantValue> dim) { dimensions.push_back(std::move(dim)); }
11+
| ^
12+
aidl_language.h:95:7: note: forward declaration of 'AidlConstantValue'
13+
95 | class AidlConstantValue;
14+
| ^
15+
---
16+
aidl_language.h | 262 ++++++++++++++++++++++++------------------------
17+
1 file changed, 131 insertions(+), 131 deletions(-)
18+
19+
diff --git a/aidl_language.h b/aidl_language.h
20+
index 8d25e619..5591c7ef 100644
21+
--- a/aidl_language.h
22+
+++ b/aidl_language.h
23+
@@ -396,6 +396,137 @@ class AidlAnnotatable : public AidlCommentable {
24+
vector<std::unique_ptr<AidlAnnotation>> annotations_;
25+
};
26+
27+
+class AidlUnaryConstExpression;
28+
+class AidlBinaryConstExpression;
29+
+class AidlConstantReference;
30+
+
31+
+class AidlConstantValue : public AidlNode {
32+
+public:
33+
+ enum class Type {
34+
+ // WARNING: Don't change this order! The order is used to determine type
35+
+ // promotion during a binary expression.
36+
+ BOOLEAN,
37+
+ INT8,
38+
+ INT32,
39+
+ INT64,
40+
+ ARRAY,
41+
+ CHARACTER,
42+
+ STRING,
43+
+ REF,
44+
+ FLOATING,
45+
+ UNARY,
46+
+ BINARY,
47+
+ ERROR,
48+
+ };
49+
+
50+
+ // Returns the evaluated value. T> should match to the actual type.
51+
+ template <typename T>
52+
+ T EvaluatedValue() const {
53+
+ is_evaluated_ || (CheckValid() && evaluate());
54+
+ AIDL_FATAL_IF(!is_valid_, this);
55+
+
56+
+ if constexpr (is_vector<T>::value) {
57+
+ AIDL_FATAL_IF(final_type_ != Type::ARRAY, this);
58+
+ T result;
59+
+ for (const auto& v : values_) {
60+
+ result.push_back(v->EvaluatedValue<typename T::value_type>());
61+
+ }
62+
+ return result;
63+
+ } else if constexpr (is_one_of<T, float, double>::value) {
64+
+ AIDL_FATAL_IF(final_type_ != Type::FLOATING, this);
65+
+ T result;
66+
+ AIDL_FATAL_IF(!ParseFloating(value_, &result), this);
67+
+ return result;
68+
+ } else if constexpr (std::is_same<T, std::string>::value) {
69+
+ AIDL_FATAL_IF(final_type_ != Type::STRING, this);
70+
+ return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote "
71+
+ } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
72+
+ AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
73+
+ return static_cast<T>(final_value_);
74+
+ } else if constexpr (std::is_same<T, char16_t>::value) {
75+
+ AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
76+
+ return final_string_value_.at(1); // unquote '
77+
+ } else if constexpr (std::is_same<T, bool>::value) {
78+
+ static_assert(std::is_same<T, bool>::value, "..");
79+
+ AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this);
80+
+ return final_value_ != 0;
81+
+ } else {
82+
+ static_assert(unsupported_type<T>::value);
83+
+ }
84+
+ }
85+
+
86+
+ virtual ~AidlConstantValue() = default;
87+
+
88+
+ // non-copyable, non-movable
89+
+ AidlConstantValue(const AidlConstantValue&) = delete;
90+
+ AidlConstantValue(AidlConstantValue&&) = delete;
91+
+ AidlConstantValue& operator=(const AidlConstantValue&) = delete;
92+
+ AidlConstantValue& operator=(AidlConstantValue&&) = delete;
93+
+
94+
+ // creates default value, when one isn't specified
95+
+ // nullptr if no default available
96+
+ static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
97+
+
98+
+ static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
99+
+ static AidlConstantValue* Character(const AidlLocation& location, const std::string& value);
100+
+ // example: 123, -5498, maybe any size
101+
+ static AidlConstantValue* Integral(const AidlLocation& location, const std::string& value);
102+
+ static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
103+
+ static AidlConstantValue* Array(const AidlLocation& location,
104+
+ std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
105+
+ // example: "\"asdf\""
106+
+ static AidlConstantValue* String(const AidlLocation& location, const string& value);
107+
+
108+
+ Type GetType() const { return final_type_; }
109+
+ const std::string& Literal() const { return value_; }
110+
+
111+
+ bool Evaluate() const;
112+
+ virtual bool CheckValid() const;
113+
+
114+
+ // Raw value of type (currently valid in C++ and Java). Empty string on error.
115+
+ string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
116+
+
117+
+ void TraverseChildren(std::function<void(const AidlNode&)> traverse) const override {
118+
+ if (type_ == Type::ARRAY) {
119+
+ for (const auto& v : values_) {
120+
+ traverse(*v);
121+
+ }
122+
+ }
123+
+ }
124+
+ void DispatchVisit(AidlVisitor& visitor) const override { visitor.Visit(*this); }
125+
+ size_t Size() const { return values_.size(); }
126+
+ const AidlConstantValue& ValueAt(size_t index) const { return *values_.at(index); }
127+
+ static string ToString(Type type);
128+
+
129+
+private:
130+
+ AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
131+
+ const string& checked_value);
132+
+ AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
133+
+ AidlConstantValue(const AidlLocation& location, Type type,
134+
+ std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
135+
+ const std::string& value);
136+
+ static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
137+
+ static bool IsHex(const string& value);
138+
+
139+
+ virtual bool evaluate() const;
140+
+ bool IsLiteral() const;
141+
+
142+
+ const Type type_ = Type::ERROR;
143+
+ const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
144+
+ const string value_; // otherwise
145+
+
146+
+ // State for tracking evaluation of expressions
147+
+ mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
148+
+ mutable bool is_evaluated_ = false; // whether evaluate has been called
149+
+ mutable Type final_type_;
150+
+ mutable int64_t final_value_;
151+
+ mutable string final_string_value_ = "";
152+
+
153+
+ friend AidlUnaryConstExpression;
154+
+ friend AidlBinaryConstExpression;
155+
+ friend AidlConstantReference;
156+
+};
157+
+
158+
// Represents `[]`
159+
struct DynamicArray {};
160+
// Represents `[N][M]..`
161+
@@ -613,137 +744,6 @@ struct ArgumentAspect {
162+
std::set<AidlArgument::Direction> possible_directions;
163+
};
164+
165+
-class AidlUnaryConstExpression;
166+
-class AidlBinaryConstExpression;
167+
-class AidlConstantReference;
168+
-
169+
-class AidlConstantValue : public AidlNode {
170+
- public:
171+
- enum class Type {
172+
- // WARNING: Don't change this order! The order is used to determine type
173+
- // promotion during a binary expression.
174+
- BOOLEAN,
175+
- INT8,
176+
- INT32,
177+
- INT64,
178+
- ARRAY,
179+
- CHARACTER,
180+
- STRING,
181+
- REF,
182+
- FLOATING,
183+
- UNARY,
184+
- BINARY,
185+
- ERROR,
186+
- };
187+
-
188+
- // Returns the evaluated value. T> should match to the actual type.
189+
- template <typename T>
190+
- T EvaluatedValue() const {
191+
- is_evaluated_ || (CheckValid() && evaluate());
192+
- AIDL_FATAL_IF(!is_valid_, this);
193+
-
194+
- if constexpr (is_vector<T>::value) {
195+
- AIDL_FATAL_IF(final_type_ != Type::ARRAY, this);
196+
- T result;
197+
- for (const auto& v : values_) {
198+
- result.push_back(v->EvaluatedValue<typename T::value_type>());
199+
- }
200+
- return result;
201+
- } else if constexpr (is_one_of<T, float, double>::value) {
202+
- AIDL_FATAL_IF(final_type_ != Type::FLOATING, this);
203+
- T result;
204+
- AIDL_FATAL_IF(!ParseFloating(value_, &result), this);
205+
- return result;
206+
- } else if constexpr (std::is_same<T, std::string>::value) {
207+
- AIDL_FATAL_IF(final_type_ != Type::STRING, this);
208+
- return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote "
209+
- } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
210+
- AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
211+
- return static_cast<T>(final_value_);
212+
- } else if constexpr (std::is_same<T, char16_t>::value) {
213+
- AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
214+
- return final_string_value_.at(1); // unquote '
215+
- } else if constexpr (std::is_same<T, bool>::value) {
216+
- static_assert(std::is_same<T, bool>::value, "..");
217+
- AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this);
218+
- return final_value_ != 0;
219+
- } else {
220+
- static_assert(unsupported_type<T>::value);
221+
- }
222+
- }
223+
-
224+
- virtual ~AidlConstantValue() = default;
225+
-
226+
- // non-copyable, non-movable
227+
- AidlConstantValue(const AidlConstantValue&) = delete;
228+
- AidlConstantValue(AidlConstantValue&&) = delete;
229+
- AidlConstantValue& operator=(const AidlConstantValue&) = delete;
230+
- AidlConstantValue& operator=(AidlConstantValue&&) = delete;
231+
-
232+
- // creates default value, when one isn't specified
233+
- // nullptr if no default available
234+
- static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
235+
-
236+
- static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
237+
- static AidlConstantValue* Character(const AidlLocation& location, const std::string& value);
238+
- // example: 123, -5498, maybe any size
239+
- static AidlConstantValue* Integral(const AidlLocation& location, const std::string& value);
240+
- static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
241+
- static AidlConstantValue* Array(const AidlLocation& location,
242+
- std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
243+
- // example: "\"asdf\""
244+
- static AidlConstantValue* String(const AidlLocation& location, const string& value);
245+
-
246+
- Type GetType() const { return final_type_; }
247+
- const std::string& Literal() const { return value_; }
248+
-
249+
- bool Evaluate() const;
250+
- virtual bool CheckValid() const;
251+
-
252+
- // Raw value of type (currently valid in C++ and Java). Empty string on error.
253+
- string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
254+
-
255+
- void TraverseChildren(std::function<void(const AidlNode&)> traverse) const override {
256+
- if (type_ == Type::ARRAY) {
257+
- for (const auto& v : values_) {
258+
- traverse(*v);
259+
- }
260+
- }
261+
- }
262+
- void DispatchVisit(AidlVisitor& visitor) const override { visitor.Visit(*this); }
263+
- size_t Size() const { return values_.size(); }
264+
- const AidlConstantValue& ValueAt(size_t index) const { return *values_.at(index); }
265+
- static string ToString(Type type);
266+
-
267+
- private:
268+
- AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
269+
- const string& checked_value);
270+
- AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
271+
- AidlConstantValue(const AidlLocation& location, Type type,
272+
- std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
273+
- const std::string& value);
274+
- static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
275+
- static bool IsHex(const string& value);
276+
-
277+
- virtual bool evaluate() const;
278+
- bool IsLiteral() const;
279+
-
280+
- const Type type_ = Type::ERROR;
281+
- const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
282+
- const string value_; // otherwise
283+
-
284+
- // State for tracking evaluation of expressions
285+
- mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
286+
- mutable bool is_evaluated_ = false; // whether evaluate has been called
287+
- mutable Type final_type_;
288+
- mutable int64_t final_value_;
289+
- mutable string final_string_value_ = "";
290+
-
291+
- friend AidlUnaryConstExpression;
292+
- friend AidlBinaryConstExpression;
293+
- friend AidlConstantReference;
294+
-};
295+
-
296+
// Represents "<type>.<field>" which resolves to a constant which is one of
297+
// - constant declaration
298+
// - enumerator
299+
--
300+
2.48.1
301+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
From 5b28dfc5fbe4d7b458585d31aca7f93715fdbf2d Mon Sep 17 00:00:00 2001
2+
From: Biswapriyo Nath <[email protected]>
3+
Date: Wed, 1 Jan 2025 00:00:00 +0000
4+
Subject: [PATCH] aidl: Move attribute before function definition
5+
6+
This fixes the following compiler error.
7+
8+
aidl/code_writer.h:47:48: error: attributes are not allowed on a function-definition
9+
47 | bool Write(const char* format, Args... args) __attribute__((format(printf, 2, 0))) {
10+
| ^~~~~~~~~~~~~
11+
---
12+
code_writer.h | 2 +-
13+
1 file changed, 1 insertion(+), 1 deletion(-)
14+
15+
diff --git a/code_writer.h b/code_writer.h
16+
index e6590c3f..593f98f0 100644
17+
--- a/code_writer.h
18+
+++ b/code_writer.h
19+
@@ -44,7 +44,7 @@ class CodeWriter {
20+
// Write a formatted string to this writer in the usual printf sense.
21+
// Returns false on error.
22+
template <typename... Args>
23+
- bool Write(const char* format, Args... args) __attribute__((format(printf, 2, 0))) {
24+
+ bool __attribute__((format(printf, 2, 0))) Write(const char* format, Args... args) {
25+
std::string formatted;
26+
android::base::StringAppendF(&formatted, format, args...);
27+
28+
--
29+
2.48.1
30+

0 commit comments

Comments
 (0)