1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
* Copyright (C) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AIDL_AST_CPP_H_
#define AIDL_AST_CPP_H_
#include <memory>
#include <string>
#include <vector>
#include <android-base/macros.h>
namespace android {
namespace aidl {
class CodeWriter;
} // namespace aidl
} // namespace android
namespace android {
namespace aidl {
namespace cpp {
class AstNode {
public:
AstNode() = default;
virtual ~AstNode() = default;
virtual void Write(CodeWriter* to) const = 0;
}; // class AstNode
class Declaration : public AstNode {
public:
Declaration() = default;
virtual ~Declaration() = default;
private:
DISALLOW_COPY_AND_ASSIGN(Declaration);
}; // class Declaration
class ClassDecl : public Declaration {
public:
ClassDecl(const std::string& name,
const std::string& parent);
ClassDecl(const std::string& name,
const std::string& parent,
std::vector<std::unique_ptr<Declaration>> public_members,
std::vector<std::unique_ptr<Declaration>> private_members);
virtual ~ClassDecl() = default;
void Write(CodeWriter* to) const override;
void AddPublic(std::unique_ptr<Declaration> member);
void AddPrivate(std::unique_ptr<Declaration> member);
private:
std::string name_;
std::string parent_;
std::vector<std::unique_ptr<Declaration>> public_members_;
std::vector<std::unique_ptr<Declaration>> private_members_;
DISALLOW_COPY_AND_ASSIGN(ClassDecl);
}; // class ClassDecl
class Enum : public Declaration {
public:
Enum(const std::string& name, const std::string& base_type);
explicit Enum(const std::string& name);
virtual ~Enum() = default;
bool HasValues() const { return !fields_.empty(); }
void Write(CodeWriter* to) const override;
void AddValue(const std::string& key, const std::string& value);
private:
struct EnumField {
EnumField(const std::string& k, const std::string& v);
const std::string key;
const std::string value;
};
std::string enum_name_;
std::string underlying_type_;
std::vector<EnumField> fields_;
DISALLOW_COPY_AND_ASSIGN(Enum);
}; // class Enum
class ArgList : public AstNode {
public:
ArgList() = default;
explicit ArgList(const std::string& single_argument);
explicit ArgList(const std::vector<std::string>& arg_list);
explicit ArgList(std::vector<std::unique_ptr<AstNode>> arg_list);
ArgList(ArgList&& arg_list);
virtual ~ArgList() = default;
void Write(CodeWriter* to) const override;
private:
std::vector<std::unique_ptr<AstNode>> arguments_;
DISALLOW_COPY_AND_ASSIGN(ArgList);
}; // class ArgList
class ConstructorDecl : public Declaration {
public:
enum Modifiers {
IS_VIRTUAL = 1 << 0,
IS_DEFAULT = 1 << 1,
IS_EXPLICIT = 1 << 2,
};
ConstructorDecl(const std::string& name,
ArgList&& arg_list);
ConstructorDecl(const std::string& name,
ArgList&& arg_list,
uint32_t modifiers);
virtual ~ConstructorDecl() = default;
void Write(CodeWriter* to) const override;
private:
const std::string name_;
const ArgList arguments_;
const uint32_t modifiers_;
DISALLOW_COPY_AND_ASSIGN(ConstructorDecl);
}; // class ConstructorDecl
class MacroDecl : public Declaration {
public:
MacroDecl(const std::string& name, ArgList&& arg_list);
virtual ~MacroDecl() = default;
void Write(CodeWriter* to) const override;
private:
const std::string name_;
const ArgList arguments_;
DISALLOW_COPY_AND_ASSIGN(MacroDecl);
}; // class MacroDecl
class MethodDecl : public Declaration {
public:
enum Modifiers {
IS_CONST = 1 << 0,
IS_VIRTUAL = 1 << 1,
IS_OVERRIDE = 1 << 2,
IS_PURE_VIRTUAL = 1 << 3,
IS_STATIC = 1 << 4,
};
MethodDecl(const std::string& return_type,
const std::string& name,
ArgList&& arg_list);
MethodDecl(const std::string& return_type,
const std::string& name,
ArgList&& arg_list,
uint32_t modifiers);
virtual ~MethodDecl() = default;
void Write(CodeWriter* to) const override;
private:
const std::string return_type_;
const std::string name_;
const ArgList arguments_;
bool is_const_ = false;
bool is_virtual_ = false;
bool is_override_ = false;
bool is_pure_virtual_ = false;
bool is_static_ = true;
DISALLOW_COPY_AND_ASSIGN(MethodDecl);
}; // class MethodDecl
class StatementBlock : public Declaration {
public:
StatementBlock() = default;
virtual ~StatementBlock() = default;
void AddStatement(std::unique_ptr<AstNode> statement);
void AddStatement(AstNode* statement); // Takes ownership
void AddLiteral(const std::string& expression, bool add_semicolon = true);
bool Empty() const { return statements_.empty(); }
void Write(CodeWriter* to) const override;
private:
std::vector<std::unique_ptr<AstNode>> statements_;
DISALLOW_COPY_AND_ASSIGN(StatementBlock);
}; // class StatementBlock
class ConstructorImpl : public Declaration {
public:
ConstructorImpl(const std::string& class_name,
ArgList&& arg_list,
const std::vector<std::string>& initializer_list);
virtual ~ConstructorImpl() = default;
void Write(CodeWriter* to) const override;
private:
std::string class_name_;
ArgList arguments_;
std::vector<std::string> initializer_list_;
StatementBlock body_;
DISALLOW_COPY_AND_ASSIGN(ConstructorImpl);
}; // class ConstructorImpl
class MethodImpl : public Declaration {
public:
// Passing an empty class name causes the method to be declared as a normal
// function (ie. no ClassName:: qualifier).
MethodImpl(const std::string& return_type,
const std::string& class_name,
const std::string& method_name,
ArgList&& arg_list,
bool is_const_method = false);
virtual ~MethodImpl() = default;
// MethodImpl retains ownership of the statement block.
StatementBlock* GetStatementBlock();
void Write(CodeWriter* to) const override;
private:
std::string return_type_;
std::string method_name_;
const ArgList arguments_;
StatementBlock statements_;
bool is_const_method_ = false;
DISALLOW_COPY_AND_ASSIGN(MethodImpl);
}; // class MethodImpl
class SwitchStatement : public AstNode {
public:
explicit SwitchStatement(const std::string& expression);
virtual ~SwitchStatement() = default;
// Add a case statement and return a pointer code block corresponding
// to the case. The switch statement will add a break statement
// after the code block by default to prevent accidental fall-through.
// Returns nullptr on duplicate value expressions (by strcmp, not value
// equivalence).
StatementBlock* AddCase(const std::string& value_expression);
void Write(CodeWriter* to) const override;
private:
const std::string switch_expression_;
std::vector<std::string> case_values_;
std::vector<std::unique_ptr<StatementBlock>> case_logic_;
DISALLOW_COPY_AND_ASSIGN(SwitchStatement);
}; // class SwitchStatement
class Assignment : public AstNode {
public:
Assignment(const std::string& left, const std::string& right);
Assignment(const std::string& left, AstNode* right);
~Assignment() = default;
void Write(CodeWriter* to) const override;
private:
const std::string lhs_;
std::unique_ptr<AstNode> rhs_;
DISALLOW_COPY_AND_ASSIGN(Assignment);
}; // class Assignment
class MethodCall : public AstNode {
public:
MethodCall(const std::string& method_name,
const std::string& single_argument);
MethodCall(const std::string& method_name, ArgList&& arg_list);
~MethodCall() = default;
void Write(CodeWriter* to) const override;
private:
const std::string method_name_;
const ArgList arguments_;
DISALLOW_COPY_AND_ASSIGN(MethodCall);
}; // class MethodCall
class IfStatement : public AstNode {
public:
explicit IfStatement(AstNode* expression,
bool invert_expression = false);
virtual ~IfStatement() = default;
StatementBlock* OnTrue() { return &on_true_; }
StatementBlock* OnFalse() { return &on_false_; }
void Write(CodeWriter* to) const override;
private:
std::unique_ptr<AstNode> expression_;
bool invert_expression_ = false;
StatementBlock on_true_;
StatementBlock on_false_;
DISALLOW_COPY_AND_ASSIGN(IfStatement);
}; // class IfStatement
class Statement : public AstNode {
public:
explicit Statement(std::unique_ptr<AstNode> expression);
explicit Statement(AstNode* expression); // Takes possession.
explicit Statement(const std::string& expression);
~Statement() = default;
void Write(CodeWriter* to) const override;
private:
std::unique_ptr<AstNode> expression_;
DISALLOW_COPY_AND_ASSIGN(Statement);
}; // class Statement
class Comparison : public AstNode {
public:
Comparison(AstNode* lhs, const std::string& comparison, AstNode* rhs);
~Comparison() = default;
void Write(CodeWriter* to) const override;
private:
std::unique_ptr<AstNode> left_;
std::unique_ptr<AstNode> right_;
const std::string operator_;
DISALLOW_COPY_AND_ASSIGN(Comparison);
}; // class Comparison
class LiteralExpression : public AstNode {
public:
explicit LiteralExpression(const std::string& expression);
~LiteralExpression() = default;
void Write(CodeWriter* to) const override;
private:
const std::string expression_;
DISALLOW_COPY_AND_ASSIGN(LiteralExpression);
}; // class LiteralExpression
class CppNamespace : public Declaration {
public:
CppNamespace(const std::string& name,
std::vector<std::unique_ptr<Declaration>> declarations);
CppNamespace(const std::string& name,
std::unique_ptr<Declaration> declaration);
explicit CppNamespace(const std::string& name);
virtual ~CppNamespace() = default;
void Write(CodeWriter* to) const override;
private:
std::vector<std::unique_ptr<Declaration>> declarations_;
std::string name_;
DISALLOW_COPY_AND_ASSIGN(CppNamespace);
}; // class CppNamespace
class Document : public AstNode {
public:
Document(const std::vector<std::string>& include_list,
std::unique_ptr<CppNamespace> a_namespace);
void Write(CodeWriter* to) const override;
private:
std::vector<std::string> include_list_;
std::unique_ptr<CppNamespace> namespace_;
DISALLOW_COPY_AND_ASSIGN(Document);
}; // class Document
class CppHeader final : public Document {
public:
CppHeader(const std::string& include_guard,
const std::vector<std::string>& include_list,
std::unique_ptr<CppNamespace> a_namespace);
void Write(CodeWriter* to) const override;
private:
const std::string include_guard_;
DISALLOW_COPY_AND_ASSIGN(CppHeader);
}; // class CppHeader
class CppSource final : public Document {
public:
CppSource(const std::vector<std::string>& include_list,
std::unique_ptr<CppNamespace> a_namespace);
private:
DISALLOW_COPY_AND_ASSIGN(CppSource);
}; // class CppSource
} // namespace cpp
} // namespace aidl
} // namespace android
#endif // AIDL_AST_CPP_H_
|