| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- #ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
- #define EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
- #include <optional>
- #include <string>
- #include <variant>
- #include <vector>
- #include "common/ostream.h"
- #include "executable_semantics/ast/paren_contents.h"
- #include "executable_semantics/ast/source_location.h"
- #include "executable_semantics/common/arena.h"
- #include "llvm/ADT/ArrayRef.h"
- #include "llvm/Support/Compiler.h"
- namespace Carbon {
- class Expression {
- public:
- enum class Kind {
- BoolTypeLiteral,
- BoolLiteral,
- CallExpression,
- FunctionTypeLiteral,
- FieldAccessExpression,
- IndexExpression,
- IntTypeLiteral,
- ContinuationTypeLiteral, // The type of a continuation value.
- IntLiteral,
- PrimitiveOperatorExpression,
- StringLiteral,
- StringTypeLiteral,
- TupleLiteral,
- StructLiteral,
- StructTypeLiteral,
- TypeTypeLiteral,
- IdentifierExpression,
- IntrinsicExpression,
- };
- void Print(llvm::raw_ostream& out) const;
- LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
- // Returns the enumerator corresponding to the most-derived type of this
- // object.
- auto kind() const -> Kind { return kind_; }
- auto source_loc() const -> SourceLocation { return source_loc_; }
- protected:
- // Constructs an Expression representing syntax at the given line number.
- // `kind` must be the enumerator corresponding to the most-derived type being
- // constructed.
- Expression(Kind kind, SourceLocation source_loc)
- : kind_(kind), source_loc_(source_loc) {}
- private:
- const Kind kind_;
- SourceLocation source_loc_;
- };
- // Converts paren_contents to an Expression, interpreting the parentheses as
- // grouping if their contents permit that interpretation, or as forming a
- // tuple otherwise.
- auto ExpressionFromParenContents(
- Nonnull<Arena*> arena, SourceLocation source_loc,
- const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
- // Converts paren_contents to an Expression, interpreting the parentheses as
- // forming a tuple.
- auto TupleExpressionFromParenContents(
- Nonnull<Arena*> arena, SourceLocation source_loc,
- const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
- // A FieldInitializer represents the initialization of a single tuple field.
- struct FieldInitializer {
- FieldInitializer(std::string name, Nonnull<Expression*> expression)
- : name(std::move(name)), expression(expression) {}
- // The field name. Cannot be empty.
- std::string name;
- // The expression that initializes the field.
- Nonnull<Expression*> expression;
- };
- enum class Operator {
- Add,
- And,
- Deref,
- Eq,
- Mul,
- Neg,
- Not,
- Or,
- Sub,
- Ptr,
- };
- class IdentifierExpression : public Expression {
- public:
- explicit IdentifierExpression(SourceLocation source_loc, std::string name)
- : Expression(Kind::IdentifierExpression, source_loc),
- name(std::move(name)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::IdentifierExpression;
- }
- auto Name() const -> const std::string& { return name; }
- private:
- std::string name;
- };
- class FieldAccessExpression : public Expression {
- public:
- explicit FieldAccessExpression(SourceLocation source_loc,
- Nonnull<Expression*> aggregate,
- std::string field)
- : Expression(Kind::FieldAccessExpression, source_loc),
- aggregate(aggregate),
- field(std::move(field)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::FieldAccessExpression;
- }
- auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
- auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
- auto Field() const -> const std::string& { return field; }
- private:
- Nonnull<Expression*> aggregate;
- std::string field;
- };
- class IndexExpression : public Expression {
- public:
- explicit IndexExpression(SourceLocation source_loc,
- Nonnull<Expression*> aggregate,
- Nonnull<Expression*> offset)
- : Expression(Kind::IndexExpression, source_loc),
- aggregate(aggregate),
- offset(offset) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::IndexExpression;
- }
- auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
- auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
- auto Offset() const -> Nonnull<const Expression*> { return offset; }
- auto Offset() -> Nonnull<Expression*> { return offset; }
- private:
- Nonnull<Expression*> aggregate;
- Nonnull<Expression*> offset;
- };
- class IntLiteral : public Expression {
- public:
- explicit IntLiteral(SourceLocation source_loc, int val)
- : Expression(Kind::IntLiteral, source_loc), val(val) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::IntLiteral;
- }
- auto Val() const -> int { return val; }
- private:
- int val;
- };
- class BoolLiteral : public Expression {
- public:
- explicit BoolLiteral(SourceLocation source_loc, bool val)
- : Expression(Kind::BoolLiteral, source_loc), val(val) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::BoolLiteral;
- }
- auto Val() const -> bool { return val; }
- private:
- bool val;
- };
- class StringLiteral : public Expression {
- public:
- explicit StringLiteral(SourceLocation source_loc, std::string val)
- : Expression(Kind::StringLiteral, source_loc), val(std::move(val)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::StringLiteral;
- }
- auto Val() const -> const std::string& { return val; }
- private:
- std::string val;
- };
- class StringTypeLiteral : public Expression {
- public:
- explicit StringTypeLiteral(SourceLocation source_loc)
- : Expression(Kind::StringTypeLiteral, source_loc) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::StringTypeLiteral;
- }
- };
- class TupleLiteral : public Expression {
- public:
- explicit TupleLiteral(SourceLocation source_loc)
- : TupleLiteral(source_loc, {}) {}
- explicit TupleLiteral(SourceLocation source_loc,
- std::vector<FieldInitializer> fields)
- : Expression(Kind::TupleLiteral, source_loc), fields(std::move(fields)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::TupleLiteral;
- }
- auto Fields() const -> const std::vector<FieldInitializer>& { return fields; }
- private:
- std::vector<FieldInitializer> fields;
- };
- // A non-empty literal value of a struct type.
- //
- // It can't be empty because the syntax `{}` is a struct type literal as well
- // as a literal value of that type, so for consistency we always represent it
- // as a StructTypeLiteral rather than let it oscillate unpredictably between
- // the two.
- class StructLiteral : public Expression {
- public:
- explicit StructLiteral(SourceLocation loc,
- std::vector<FieldInitializer> fields)
- : Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
- CHECK(!fields_.empty())
- << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
- }
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::StructLiteral;
- }
- auto fields() const -> const std::vector<FieldInitializer>& {
- return fields_;
- }
- private:
- std::vector<FieldInitializer> fields_;
- };
- // A literal representing a struct type.
- //
- // Code that handles this type may sometimes need to have special-case handling
- // for `{}`, which is a struct value in addition to being a struct type.
- class StructTypeLiteral : public Expression {
- public:
- explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
- explicit StructTypeLiteral(SourceLocation loc,
- std::vector<FieldInitializer> fields)
- : Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::StructTypeLiteral;
- }
- auto fields() const -> const std::vector<FieldInitializer>& {
- return fields_;
- }
- private:
- std::vector<FieldInitializer> fields_;
- };
- class PrimitiveOperatorExpression : public Expression {
- public:
- explicit PrimitiveOperatorExpression(
- SourceLocation source_loc, Operator op,
- std::vector<Nonnull<Expression*>> arguments)
- : Expression(Kind::PrimitiveOperatorExpression, source_loc),
- op(op),
- arguments(std::move(arguments)) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::PrimitiveOperatorExpression;
- }
- auto Op() const -> Operator { return op; }
- auto Arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
- return arguments;
- }
- auto Arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
- return arguments;
- }
- private:
- Operator op;
- std::vector<Nonnull<Expression*>> arguments;
- };
- class CallExpression : public Expression {
- public:
- explicit CallExpression(SourceLocation source_loc,
- Nonnull<Expression*> function,
- Nonnull<Expression*> argument)
- : Expression(Kind::CallExpression, source_loc),
- function(function),
- argument(argument) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::CallExpression;
- }
- auto Function() const -> Nonnull<const Expression*> { return function; }
- auto Function() -> Nonnull<Expression*> { return function; }
- auto Argument() const -> Nonnull<const Expression*> { return argument; }
- auto Argument() -> Nonnull<Expression*> { return argument; }
- private:
- Nonnull<Expression*> function;
- Nonnull<Expression*> argument;
- };
- class FunctionTypeLiteral : public Expression {
- public:
- explicit FunctionTypeLiteral(SourceLocation source_loc,
- Nonnull<Expression*> parameter,
- Nonnull<Expression*> return_type,
- bool is_omitted_return_type)
- : Expression(Kind::FunctionTypeLiteral, source_loc),
- parameter(parameter),
- return_type(return_type),
- is_omitted_return_type(is_omitted_return_type) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::FunctionTypeLiteral;
- }
- auto Parameter() const -> Nonnull<const Expression*> { return parameter; }
- auto Parameter() -> Nonnull<Expression*> { return parameter; }
- auto ReturnType() const -> Nonnull<const Expression*> { return return_type; }
- auto ReturnType() -> Nonnull<Expression*> { return return_type; }
- auto IsOmittedReturnType() const -> bool { return is_omitted_return_type; }
- private:
- Nonnull<Expression*> parameter;
- Nonnull<Expression*> return_type;
- bool is_omitted_return_type;
- };
- class BoolTypeLiteral : public Expression {
- public:
- explicit BoolTypeLiteral(SourceLocation source_loc)
- : Expression(Kind::BoolTypeLiteral, source_loc) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::BoolTypeLiteral;
- }
- };
- class IntTypeLiteral : public Expression {
- public:
- explicit IntTypeLiteral(SourceLocation source_loc)
- : Expression(Kind::IntTypeLiteral, source_loc) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::IntTypeLiteral;
- }
- };
- class ContinuationTypeLiteral : public Expression {
- public:
- explicit ContinuationTypeLiteral(SourceLocation source_loc)
- : Expression(Kind::ContinuationTypeLiteral, source_loc) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::ContinuationTypeLiteral;
- }
- };
- class TypeTypeLiteral : public Expression {
- public:
- explicit TypeTypeLiteral(SourceLocation source_loc)
- : Expression(Kind::TypeTypeLiteral, source_loc) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::TypeTypeLiteral;
- }
- };
- class IntrinsicExpression : public Expression {
- public:
- enum class IntrinsicKind {
- Print,
- };
- explicit IntrinsicExpression(IntrinsicKind intrinsic)
- : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
- intrinsic(intrinsic) {}
- static auto classof(const Expression* exp) -> bool {
- return exp->kind() == Kind::IntrinsicExpression;
- }
- auto Intrinsic() const -> IntrinsicKind { return intrinsic; }
- private:
- IntrinsicKind intrinsic;
- };
- } // namespace Carbon
- #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|