/* * 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. */ #pragma once #include #include #include #include #include #include #include enum { PACKAGE_PRIVATE = 0x00000000, PUBLIC = 0x00000001, PRIVATE = 0x00000002, PROTECTED = 0x00000003, SCOPE_MASK = 0x00000003, STATIC = 0x00000010, FINAL = 0x00000020, ABSTRACT = 0x00000040, OVERRIDE = 0x00000100, ALL_MODIFIERS = 0xffffffff }; namespace android { namespace aidl { class CodeWriter; } // namespace aidl } // namespace android namespace android { namespace aidl { namespace java { // Write the modifiers that are set in both mod and mask void WriteModifiers(CodeWriter* to, int mod, int mask); struct AstNode { AstNode() = default; virtual ~AstNode() = default; virtual void Write(CodeWriter* to) const = 0; std::string ToString(); }; struct ClassElement : public AstNode { ClassElement() = default; virtual ~ClassElement() = default; }; struct Expression : public AstNode { virtual ~Expression() = default; }; struct LiteralExpression : public Expression { std::string value; explicit LiteralExpression(const std::string& value); virtual ~LiteralExpression() = default; void Write(CodeWriter* to) const override; }; struct StringLiteralExpression : public Expression { std::string value; explicit StringLiteralExpression(const std::string& value); virtual ~StringLiteralExpression() = default; void Write(CodeWriter* to) const override; }; struct Variable : public Expression { std::vector annotations; const std::string type; std::string name; Variable() = default; Variable(const std::string& type, const std::string& name); virtual ~Variable() = default; void WriteDeclaration(CodeWriter* to) const; void Write(CodeWriter* to) const; }; struct FieldVariable : public Expression { std::variant, std::string> receiver; std::string name; FieldVariable(std::shared_ptr object, const std::string& name); FieldVariable(const std::string& clazz, const std::string& name); virtual ~FieldVariable() = default; void Write(CodeWriter* to) const; }; struct Field : public ClassElement { std::string comment; std::vector annotations; int modifiers = 0; std::shared_ptr variable = nullptr; std::string value; Field() = default; Field(int modifiers, std::shared_ptr variable); virtual ~Field() = default; void Write(CodeWriter* to) const override; }; struct Statement : public AstNode { virtual ~Statement() = default; }; struct LiteralStatement : public Statement { public: LiteralStatement(const std::string& value); virtual ~LiteralStatement() = default; void Write(CodeWriter* to) const override; private: const std::string value_; }; struct StatementBlock : public Statement { std::vector> statements; StatementBlock() = default; virtual ~StatementBlock() = default; void Write(CodeWriter* to) const override; void Add(std::shared_ptr statement); void Add(std::shared_ptr expression); }; struct ExpressionStatement : public Statement { std::shared_ptr expression; explicit ExpressionStatement(std::shared_ptr expression); virtual ~ExpressionStatement() = default; void Write(CodeWriter* to) const override; }; struct Assignment : public Expression { std::shared_ptr lvalue; std::shared_ptr rvalue; std::optional cast = std::nullopt; Assignment(std::shared_ptr lvalue, std::shared_ptr rvalue); Assignment(std::shared_ptr lvalue, std::shared_ptr rvalue, std::string cast); virtual ~Assignment() = default; void Write(CodeWriter* to) const override; }; struct MethodCall : public Expression { std::variant, std::string> receiver; std::string name; std::vector> arguments; std::vector exceptions; explicit MethodCall(const std::string& name); MethodCall(const std::string& name, const std::vector>& args); MethodCall(std::shared_ptr obj, const std::string& name); MethodCall(const std::string& clazz, const std::string& name); MethodCall(std::shared_ptr obj, const std::string& name, const std::vector>& args); MethodCall(const std::string&, const std::string& name, const std::vector>& args); virtual ~MethodCall() = default; void Write(CodeWriter* to) const override; }; struct Comparison : public Expression { std::shared_ptr lvalue; std::string op; std::shared_ptr rvalue; Comparison(std::shared_ptr lvalue, const std::string& op, std::shared_ptr rvalue); virtual ~Comparison() = default; void Write(CodeWriter* to) const override; }; struct NewExpression : public Expression { const std::string instantiableName; std::vector> arguments; explicit NewExpression(const std::string& name); NewExpression(const std::string& name, const std::vector>& args); virtual ~NewExpression() = default; void Write(CodeWriter* to) const override; }; struct Cast : public Expression { const std::string type; std::shared_ptr expression = nullptr; Cast() = default; Cast(const std::string& type, std::shared_ptr expression); virtual ~Cast() = default; void Write(CodeWriter* to) const override; }; struct VariableDeclaration : public Statement { std::shared_ptr lvalue = nullptr; std::shared_ptr rvalue = nullptr; explicit VariableDeclaration(std::shared_ptr lvalue); VariableDeclaration(std::shared_ptr lvalue, std::shared_ptr rvalue); virtual ~VariableDeclaration() = default; void Write(CodeWriter* to) const override; }; struct IfStatement : public Statement { std::shared_ptr expression = nullptr; std::shared_ptr statements = std::make_shared(); std::shared_ptr elseif = nullptr; IfStatement() = default; virtual ~IfStatement() = default; void Write(CodeWriter* to) const override; }; struct ReturnStatement : public Statement { std::shared_ptr expression; explicit ReturnStatement(std::shared_ptr expression); virtual ~ReturnStatement() = default; void Write(CodeWriter* to) const override; }; struct BreakStatement : public Statement { BreakStatement() = default; virtual ~BreakStatement() = default; void Write(CodeWriter* to) const override; }; struct TryStatement : public Statement { std::shared_ptr statements = std::make_shared(); TryStatement() = default; virtual ~TryStatement() = default; void Write(CodeWriter* to) const override; }; struct FinallyStatement : public Statement { std::shared_ptr statements = std::make_shared(); FinallyStatement() = default; virtual ~FinallyStatement() = default; void Write(CodeWriter* to) const override; }; struct Case : public AstNode { std::vector cases; std::shared_ptr statements = std::make_shared(); Case() = default; explicit Case(const std::string& c); virtual ~Case() = default; void Write(CodeWriter* to) const override; }; struct SwitchStatement : public Statement { std::shared_ptr expression; std::vector> cases; explicit SwitchStatement(std::shared_ptr expression); virtual ~SwitchStatement() = default; void Write(CodeWriter* to) const override; }; struct Method : public ClassElement { std::string comment; std::vector annotations; int modifiers = 0; std::optional returnType = std::nullopt; // nullopt means constructor std::string name; std::vector> parameters; std::vector exceptions; std::shared_ptr statements = nullptr; Method() = default; virtual ~Method() = default; void Write(CodeWriter* to) const override; }; struct LiteralClassElement : public ClassElement { std::string element; LiteralClassElement(std::string e) : element(e) {} virtual ~LiteralClassElement() = default; void Write(CodeWriter* to) const override; }; struct Class : public ClassElement { enum { CLASS, INTERFACE }; std::string comment; std::vector annotations; int modifiers = 0; int what = CLASS; // CLASS or INTERFACE std::string type; std::optional extends = std::nullopt; std::vector interfaces; std::vector> elements; Class() = default; virtual ~Class() = default; void Write(CodeWriter* to) const override; }; extern std::shared_ptr NULL_VALUE; extern std::shared_ptr THIS_VALUE; extern std::shared_ptr SUPER_VALUE; extern std::shared_ptr TRUE_VALUE; extern std::shared_ptr FALSE_VALUE; } // namespace java } // namespace aidl } // namespace android