mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 21:51:12 +01:00
Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
This commit is contained in:
@@ -4,6 +4,17 @@
|
||||
|
||||
package(default_visibility = ["//executable_semantics:__subpackages__"])
|
||||
|
||||
cc_library(
|
||||
name = "class_definition",
|
||||
hdrs = ["class_definition.h"],
|
||||
deps = [
|
||||
":member",
|
||||
":source_location",
|
||||
"//common:ostream",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "declaration",
|
||||
srcs = ["declaration.cpp"],
|
||||
@@ -16,6 +27,7 @@ cc_library(
|
||||
":function_definition",
|
||||
":member",
|
||||
":pattern",
|
||||
":source_location",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:ptr",
|
||||
"@llvm-project//llvm:Support",
|
||||
@@ -53,6 +65,7 @@ cc_library(
|
||||
hdrs = ["function_definition.h"],
|
||||
deps = [
|
||||
":expression",
|
||||
":source_location",
|
||||
":statement",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
@@ -64,6 +77,7 @@ cc_library(
|
||||
hdrs = ["member.h"],
|
||||
deps = [
|
||||
":pattern",
|
||||
":source_location",
|
||||
"//common:ostream",
|
||||
],
|
||||
)
|
||||
@@ -71,6 +85,10 @@ cc_library(
|
||||
cc_library(
|
||||
name = "paren_contents",
|
||||
hdrs = ["paren_contents.h"],
|
||||
deps = [
|
||||
":source_location",
|
||||
"//executable_semantics/common:error",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
@@ -79,6 +97,7 @@ cc_library(
|
||||
hdrs = ["pattern.h"],
|
||||
deps = [
|
||||
":expression",
|
||||
":source_location",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:error",
|
||||
@@ -98,6 +117,12 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "source_location",
|
||||
hdrs = ["source_location.h"],
|
||||
deps = ["//common:ostream"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "statement",
|
||||
srcs = ["statement.cpp"],
|
||||
@@ -105,19 +130,10 @@ cc_library(
|
||||
deps = [
|
||||
":expression",
|
||||
":pattern",
|
||||
":source_location",
|
||||
"//common:check",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:arena",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "class_definition",
|
||||
hdrs = ["class_definition.h"],
|
||||
deps = [
|
||||
":member",
|
||||
"//common:ostream",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -2,22 +2,23 @@
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include "executable_semantics/ast/member.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct ClassDefinition {
|
||||
int line_num;
|
||||
SourceLocation loc;
|
||||
std::string name;
|
||||
std::list<Member*> members;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/member.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
@@ -42,7 +43,7 @@ class Declaration {
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
|
||||
auto LineNumber() const -> int { return line_num; }
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
|
||||
@@ -50,17 +51,17 @@ class Declaration {
|
||||
// Constructs a Declaration representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Declaration(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
|
||||
Declaration(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
int line_num;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
class FunctionDeclaration : public Declaration {
|
||||
public:
|
||||
FunctionDeclaration(Ptr<const FunctionDefinition> definition)
|
||||
: Declaration(Kind::FunctionDeclaration, definition->line_num),
|
||||
: Declaration(Kind::FunctionDeclaration, definition->source_location),
|
||||
definition(definition) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
@@ -75,9 +76,10 @@ class FunctionDeclaration : public Declaration {
|
||||
|
||||
class ClassDeclaration : public Declaration {
|
||||
public:
|
||||
ClassDeclaration(int line_num, std::string name, std::list<Member*> members)
|
||||
: Declaration(Kind::ClassDeclaration, line_num),
|
||||
definition({.line_num = line_num,
|
||||
ClassDeclaration(SourceLocation loc, std::string name,
|
||||
std::list<Member*> members)
|
||||
: Declaration(Kind::ClassDeclaration, loc),
|
||||
definition({.loc = loc,
|
||||
.name = std::move(name),
|
||||
.members = std::move(members)}) {}
|
||||
|
||||
@@ -94,9 +96,9 @@ class ClassDeclaration : public Declaration {
|
||||
class ChoiceDeclaration : public Declaration {
|
||||
public:
|
||||
ChoiceDeclaration(
|
||||
int line_num, std::string name,
|
||||
SourceLocation loc, std::string name,
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives)
|
||||
: Declaration(Kind::ChoiceDeclaration, line_num),
|
||||
: Declaration(Kind::ChoiceDeclaration, loc),
|
||||
name(std::move(name)),
|
||||
alternatives(std::move(alternatives)) {}
|
||||
|
||||
@@ -118,9 +120,9 @@ class ChoiceDeclaration : public Declaration {
|
||||
// Global variable definition implements the Declaration concept.
|
||||
class VariableDeclaration : public Declaration {
|
||||
public:
|
||||
VariableDeclaration(int line_num, const BindingPattern* binding,
|
||||
VariableDeclaration(SourceLocation loc, const BindingPattern* binding,
|
||||
const Expression* initializer)
|
||||
: Declaration(Kind::VariableDeclaration, line_num),
|
||||
: Declaration(Kind::VariableDeclaration, loc),
|
||||
binding(binding),
|
||||
initializer(initializer) {}
|
||||
|
||||
|
||||
@@ -17,21 +17,21 @@ namespace Carbon {
|
||||
using llvm::cast;
|
||||
|
||||
auto ExpressionFromParenContents(
|
||||
int line_num, const ParenContents<Expression>& paren_contents)
|
||||
SourceLocation loc, const ParenContents<Expression>& paren_contents)
|
||||
-> const Expression* {
|
||||
std::optional<const Expression*> single_term = paren_contents.SingleTerm();
|
||||
if (single_term.has_value()) {
|
||||
return *single_term;
|
||||
} else {
|
||||
return TupleExpressionFromParenContents(line_num, paren_contents);
|
||||
return TupleExpressionFromParenContents(loc, paren_contents);
|
||||
}
|
||||
}
|
||||
|
||||
auto TupleExpressionFromParenContents(
|
||||
int line_num, const ParenContents<Expression>& paren_contents)
|
||||
SourceLocation loc, const ParenContents<Expression>& paren_contents)
|
||||
-> const Expression* {
|
||||
return global_arena->RawNew<TupleLiteral>(
|
||||
line_num, paren_contents.TupleElements<FieldInitializer>(line_num));
|
||||
loc, paren_contents.TupleElements<FieldInitializer>(loc));
|
||||
}
|
||||
|
||||
static void PrintOp(llvm::raw_ostream& out, Operator op) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/paren_contents.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -41,7 +42,7 @@ class Expression {
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
|
||||
auto LineNumber() const -> int { return line_num; }
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
@@ -50,24 +51,24 @@ class Expression {
|
||||
// Constructs an Expression representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Expression(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
|
||||
Expression(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
int line_num;
|
||||
SourceLocation 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(
|
||||
int line_num, const ParenContents<Expression>& paren_contents)
|
||||
SourceLocation loc, const ParenContents<Expression>& paren_contents)
|
||||
-> const Expression*;
|
||||
|
||||
// Converts paren_contents to an Expression, interpreting the parentheses as
|
||||
// forming a tuple.
|
||||
auto TupleExpressionFromParenContents(
|
||||
int line_num, const ParenContents<Expression>& paren_contents)
|
||||
SourceLocation loc, const ParenContents<Expression>& paren_contents)
|
||||
-> const Expression*;
|
||||
|
||||
// A FieldInitializer represents the initialization of a single tuple field.
|
||||
@@ -97,9 +98,8 @@ enum class Operator {
|
||||
|
||||
class IdentifierExpression : public Expression {
|
||||
public:
|
||||
explicit IdentifierExpression(int line_num, std::string name)
|
||||
: Expression(Kind::IdentifierExpression, line_num),
|
||||
name(std::move(name)) {}
|
||||
explicit IdentifierExpression(SourceLocation loc, std::string name)
|
||||
: Expression(Kind::IdentifierExpression, loc), name(std::move(name)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::IdentifierExpression;
|
||||
@@ -113,9 +113,9 @@ class IdentifierExpression : public Expression {
|
||||
|
||||
class FieldAccessExpression : public Expression {
|
||||
public:
|
||||
explicit FieldAccessExpression(int line_num, const Expression* aggregate,
|
||||
std::string field)
|
||||
: Expression(Kind::FieldAccessExpression, line_num),
|
||||
explicit FieldAccessExpression(SourceLocation loc,
|
||||
const Expression* aggregate, std::string field)
|
||||
: Expression(Kind::FieldAccessExpression, loc),
|
||||
aggregate(aggregate),
|
||||
field(std::move(field)) {}
|
||||
|
||||
@@ -133,9 +133,9 @@ class FieldAccessExpression : public Expression {
|
||||
|
||||
class IndexExpression : public Expression {
|
||||
public:
|
||||
explicit IndexExpression(int line_num, const Expression* aggregate,
|
||||
explicit IndexExpression(SourceLocation loc, const Expression* aggregate,
|
||||
const Expression* offset)
|
||||
: Expression(Kind::IndexExpression, line_num),
|
||||
: Expression(Kind::IndexExpression, loc),
|
||||
aggregate(aggregate),
|
||||
offset(offset) {}
|
||||
|
||||
@@ -153,8 +153,8 @@ class IndexExpression : public Expression {
|
||||
|
||||
class IntLiteral : public Expression {
|
||||
public:
|
||||
explicit IntLiteral(int line_num, int val)
|
||||
: Expression(Kind::IntLiteral, line_num), val(val) {}
|
||||
explicit IntLiteral(SourceLocation loc, int val)
|
||||
: Expression(Kind::IntLiteral, loc), val(val) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::IntLiteral;
|
||||
@@ -168,8 +168,8 @@ class IntLiteral : public Expression {
|
||||
|
||||
class BoolLiteral : public Expression {
|
||||
public:
|
||||
explicit BoolLiteral(int line_num, bool val)
|
||||
: Expression(Kind::BoolLiteral, line_num), val(val) {}
|
||||
explicit BoolLiteral(SourceLocation loc, bool val)
|
||||
: Expression(Kind::BoolLiteral, loc), val(val) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::BoolLiteral;
|
||||
@@ -183,8 +183,8 @@ class BoolLiteral : public Expression {
|
||||
|
||||
class StringLiteral : public Expression {
|
||||
public:
|
||||
explicit StringLiteral(int line_num, std::string val)
|
||||
: Expression(Kind::StringLiteral, line_num), val(std::move(val)) {}
|
||||
explicit StringLiteral(SourceLocation loc, std::string val)
|
||||
: Expression(Kind::StringLiteral, loc), val(std::move(val)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::StringLiteral;
|
||||
@@ -198,8 +198,8 @@ class StringLiteral : public Expression {
|
||||
|
||||
class StringTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit StringTypeLiteral(int line_num)
|
||||
: Expression(Kind::StringTypeLiteral, line_num) {}
|
||||
explicit StringTypeLiteral(SourceLocation loc)
|
||||
: Expression(Kind::StringTypeLiteral, loc) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::StringTypeLiteral;
|
||||
@@ -208,10 +208,11 @@ class StringTypeLiteral : public Expression {
|
||||
|
||||
class TupleLiteral : public Expression {
|
||||
public:
|
||||
explicit TupleLiteral(int line_num) : TupleLiteral(line_num, {}) {}
|
||||
explicit TupleLiteral(SourceLocation loc) : TupleLiteral(loc, {}) {}
|
||||
|
||||
explicit TupleLiteral(int line_num, std::vector<FieldInitializer> fields)
|
||||
: Expression(Kind::TupleLiteral, line_num), fields(std::move(fields)) {}
|
||||
explicit TupleLiteral(SourceLocation loc,
|
||||
std::vector<FieldInitializer> fields)
|
||||
: Expression(Kind::TupleLiteral, loc), fields(std::move(fields)) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::TupleLiteral;
|
||||
@@ -225,9 +226,9 @@ class TupleLiteral : public Expression {
|
||||
|
||||
class PrimitiveOperatorExpression : public Expression {
|
||||
public:
|
||||
explicit PrimitiveOperatorExpression(int line_num, Operator op,
|
||||
explicit PrimitiveOperatorExpression(SourceLocation loc, Operator op,
|
||||
std::vector<const Expression*> arguments)
|
||||
: Expression(Kind::PrimitiveOperatorExpression, line_num),
|
||||
: Expression(Kind::PrimitiveOperatorExpression, loc),
|
||||
op(op),
|
||||
arguments(std::move(arguments)) {}
|
||||
|
||||
@@ -247,9 +248,9 @@ class PrimitiveOperatorExpression : public Expression {
|
||||
|
||||
class CallExpression : public Expression {
|
||||
public:
|
||||
explicit CallExpression(int line_num, const Expression* function,
|
||||
explicit CallExpression(SourceLocation loc, const Expression* function,
|
||||
const Expression* argument)
|
||||
: Expression(Kind::CallExpression, line_num),
|
||||
: Expression(Kind::CallExpression, loc),
|
||||
function(function),
|
||||
argument(argument) {}
|
||||
|
||||
@@ -267,10 +268,10 @@ class CallExpression : public Expression {
|
||||
|
||||
class FunctionTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit FunctionTypeLiteral(int line_num, const Expression* parameter,
|
||||
explicit FunctionTypeLiteral(SourceLocation loc, const Expression* parameter,
|
||||
const Expression* return_type,
|
||||
bool is_omitted_return_type)
|
||||
: Expression(Kind::FunctionTypeLiteral, line_num),
|
||||
: Expression(Kind::FunctionTypeLiteral, loc),
|
||||
parameter(parameter),
|
||||
return_type(return_type),
|
||||
is_omitted_return_type(is_omitted_return_type) {}
|
||||
@@ -291,8 +292,8 @@ class FunctionTypeLiteral : public Expression {
|
||||
|
||||
class BoolTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit BoolTypeLiteral(int line_num)
|
||||
: Expression(Kind::BoolTypeLiteral, line_num) {}
|
||||
explicit BoolTypeLiteral(SourceLocation loc)
|
||||
: Expression(Kind::BoolTypeLiteral, loc) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::BoolTypeLiteral;
|
||||
@@ -301,8 +302,8 @@ class BoolTypeLiteral : public Expression {
|
||||
|
||||
class IntTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit IntTypeLiteral(int line_num)
|
||||
: Expression(Kind::IntTypeLiteral, line_num) {}
|
||||
explicit IntTypeLiteral(SourceLocation loc)
|
||||
: Expression(Kind::IntTypeLiteral, loc) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::IntTypeLiteral;
|
||||
@@ -311,8 +312,8 @@ class IntTypeLiteral : public Expression {
|
||||
|
||||
class ContinuationTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit ContinuationTypeLiteral(int line_num)
|
||||
: Expression(Kind::ContinuationTypeLiteral, line_num) {}
|
||||
explicit ContinuationTypeLiteral(SourceLocation loc)
|
||||
: Expression(Kind::ContinuationTypeLiteral, loc) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::ContinuationTypeLiteral;
|
||||
@@ -321,8 +322,8 @@ class ContinuationTypeLiteral : public Expression {
|
||||
|
||||
class TypeTypeLiteral : public Expression {
|
||||
public:
|
||||
explicit TypeTypeLiteral(int line_num)
|
||||
: Expression(Kind::TypeTypeLiteral, line_num) {}
|
||||
explicit TypeTypeLiteral(SourceLocation loc)
|
||||
: Expression(Kind::TypeTypeLiteral, loc) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::TypeTypeLiteral;
|
||||
@@ -336,7 +337,8 @@ class IntrinsicExpression : public Expression {
|
||||
};
|
||||
|
||||
explicit IntrinsicExpression(IntrinsicKind intrinsic)
|
||||
: Expression(Kind::IntrinsicExpression, -1), intrinsic(intrinsic) {}
|
||||
: Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
|
||||
intrinsic(intrinsic) {}
|
||||
|
||||
static auto classof(const Expression* exp) -> bool {
|
||||
return exp->Tag() == Kind::IntrinsicExpression;
|
||||
|
||||
@@ -26,12 +26,16 @@ MATCHER_P(IntFieldNamed, name, "") {
|
||||
arg.expression->Tag() == Expression::Kind::IntLiteral;
|
||||
}
|
||||
|
||||
static auto FakeSourceLoc(int line_num) -> SourceLocation {
|
||||
return SourceLocation("<test>", line_num);
|
||||
}
|
||||
|
||||
TEST(ExpressionTest, EmptyAsExpression) {
|
||||
ParenContents<Expression> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const Expression* expression =
|
||||
ExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(expression->LineNumber(), 1);
|
||||
ExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(), IsEmpty());
|
||||
}
|
||||
@@ -40,8 +44,8 @@ TEST(ExpressionTest, EmptyAsTuple) {
|
||||
ParenContents<Expression> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const Expression* tuple =
|
||||
TupleExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(), IsEmpty());
|
||||
}
|
||||
@@ -55,26 +59,26 @@ TEST(ExpressionTest, UnaryNoCommaAsExpression) {
|
||||
// ```
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(2),
|
||||
42)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Expression* expression =
|
||||
ExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(expression->LineNumber(), 2);
|
||||
ExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(2));
|
||||
ASSERT_EQ(expression->Tag(), Expression::Kind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ExpressionTest, UnaryNoCommaAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(2),
|
||||
42)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Expression* tuple =
|
||||
TupleExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
|
||||
ElementsAre(IntFieldNamed("0")));
|
||||
@@ -83,13 +87,13 @@ TEST(ExpressionTest, UnaryNoCommaAsTuple) {
|
||||
TEST(ExpressionTest, UnaryWithCommaAsExpression) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(2),
|
||||
42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* expression =
|
||||
ExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(expression->LineNumber(), 1);
|
||||
ExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
|
||||
ElementsAre(IntFieldNamed("0")));
|
||||
@@ -98,13 +102,13 @@ TEST(ExpressionTest, UnaryWithCommaAsExpression) {
|
||||
TEST(ExpressionTest, UnaryWithCommaAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(2),
|
||||
42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* tuple =
|
||||
TupleExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
|
||||
ElementsAre(IntFieldNamed("0")));
|
||||
@@ -114,15 +118,15 @@ TEST(ExpressionTest, BinaryAsExpression) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)},
|
||||
global_arena->RawNew<IntLiteral>(FakeSourceLoc(2), 42)},
|
||||
{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(3),
|
||||
42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* expression =
|
||||
ExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(expression->LineNumber(), 1);
|
||||
ExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
|
||||
ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
|
||||
@@ -132,15 +136,15 @@ TEST(ExpressionTest, BinaryAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)},
|
||||
global_arena->RawNew<IntLiteral>(FakeSourceLoc(2), 42)},
|
||||
{.name = std::nullopt,
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.term = global_arena->RawNew<IntLiteral>(FakeSourceLoc(3),
|
||||
42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* tuple =
|
||||
TupleExpressionFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
|
||||
EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
|
||||
ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
@@ -21,12 +22,12 @@ struct GenericBinding {
|
||||
};
|
||||
|
||||
struct FunctionDefinition {
|
||||
FunctionDefinition(int line_num, std::string name,
|
||||
FunctionDefinition(SourceLocation source_location, std::string name,
|
||||
std::vector<GenericBinding> deduced_params,
|
||||
const TuplePattern* param_pattern,
|
||||
const Pattern* return_type, bool is_omitted_return_type,
|
||||
const Statement* body)
|
||||
: line_num(line_num),
|
||||
: source_location(source_location),
|
||||
name(std::move(name)),
|
||||
deduced_parameters(deduced_params),
|
||||
param_pattern(param_pattern),
|
||||
@@ -38,7 +39,7 @@ struct FunctionDefinition {
|
||||
void PrintDepth(int depth, llvm::raw_ostream& out) const;
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
|
||||
int line_num;
|
||||
SourceLocation source_location;
|
||||
std::string name;
|
||||
std::vector<GenericBinding> deduced_parameters;
|
||||
const TuplePattern* param_pattern;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -33,7 +34,7 @@ class Member {
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
|
||||
auto LineNumber() const -> int { return line_num; }
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
|
||||
@@ -41,17 +42,17 @@ class Member {
|
||||
// Constructs a Member representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Member(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
|
||||
Member(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
int line_num;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
class FieldMember : public Member {
|
||||
public:
|
||||
FieldMember(int line_num, const BindingPattern* binding)
|
||||
: Member(Kind::FieldMember, line_num), binding(binding) {}
|
||||
FieldMember(SourceLocation loc, const BindingPattern* binding)
|
||||
: Member(Kind::FieldMember, loc), binding(binding) {}
|
||||
|
||||
static auto classof(const Member* member) -> bool {
|
||||
return member->Tag() == Kind::FieldMember;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -40,7 +41,7 @@ struct ParenContents {
|
||||
//
|
||||
// TODO: Find a way to deduce TupleElement from Term.
|
||||
template <typename TupleElement>
|
||||
auto TupleElements(int line_num) const -> std::vector<TupleElement>;
|
||||
auto TupleElements(SourceLocation loc) const -> std::vector<TupleElement>;
|
||||
|
||||
std::vector<Element> elements;
|
||||
bool has_trailing_comma;
|
||||
@@ -60,7 +61,7 @@ auto ParenContents<Term>::SingleTerm() const -> std::optional<const Term*> {
|
||||
|
||||
template <typename Term>
|
||||
template <typename TupleElement>
|
||||
auto ParenContents<Term>::TupleElements(int line_num) const
|
||||
auto ParenContents<Term>::TupleElements(SourceLocation loc) const
|
||||
-> std::vector<TupleElement> {
|
||||
std::vector<TupleElement> result;
|
||||
int i = 0;
|
||||
@@ -71,7 +72,7 @@ auto ParenContents<Term>::TupleElements(int line_num) const
|
||||
result.push_back(TupleElement(*element.name, element.term));
|
||||
} else {
|
||||
if (seen_named_member) {
|
||||
FATAL_PROGRAM_ERROR(line_num)
|
||||
FATAL_PROGRAM_ERROR(loc)
|
||||
<< "positional members must come before named members";
|
||||
}
|
||||
result.push_back(TupleElement(std::to_string(i), element.term));
|
||||
|
||||
@@ -55,7 +55,7 @@ void Pattern::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
|
||||
TuplePattern::TuplePattern(const Expression* tuple_literal)
|
||||
: Pattern(Kind::TuplePattern, tuple_literal->LineNumber()) {
|
||||
: Pattern(Kind::TuplePattern, tuple_literal->SourceLoc()) {
|
||||
const auto& tuple = cast<TupleLiteral>(*tuple_literal);
|
||||
for (const FieldInitializer& init : tuple.Fields()) {
|
||||
fields.push_back(Field(
|
||||
@@ -63,30 +63,30 @@ TuplePattern::TuplePattern(const Expression* tuple_literal)
|
||||
}
|
||||
}
|
||||
|
||||
auto PatternFromParenContents(int line_num,
|
||||
auto PatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const Pattern* {
|
||||
std::optional<const Pattern*> single_term = paren_contents.SingleTerm();
|
||||
if (single_term.has_value()) {
|
||||
return *single_term;
|
||||
} else {
|
||||
return TuplePatternFromParenContents(line_num, paren_contents);
|
||||
return TuplePatternFromParenContents(loc, paren_contents);
|
||||
}
|
||||
}
|
||||
|
||||
auto TuplePatternFromParenContents(int line_num,
|
||||
auto TuplePatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const TuplePattern* {
|
||||
return global_arena->RawNew<TuplePattern>(
|
||||
line_num, paren_contents.TupleElements<TuplePattern::Field>(line_num));
|
||||
loc, paren_contents.TupleElements<TuplePattern::Field>(loc));
|
||||
}
|
||||
|
||||
AlternativePattern::AlternativePattern(int line_num,
|
||||
AlternativePattern::AlternativePattern(SourceLocation loc,
|
||||
const Expression* alternative,
|
||||
const TuplePattern* arguments)
|
||||
: Pattern(Kind::AlternativePattern, line_num), arguments(arguments) {
|
||||
: Pattern(Kind::AlternativePattern, loc), arguments(arguments) {
|
||||
if (alternative->Tag() != Expression::Kind::FieldAccessExpression) {
|
||||
FATAL_PROGRAM_ERROR(alternative->LineNumber())
|
||||
FATAL_PROGRAM_ERROR(alternative->SourceLoc())
|
||||
<< "Alternative pattern must have the form of a field access.";
|
||||
}
|
||||
const auto& field_access = cast<FieldAccessExpression>(*alternative);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -39,7 +40,7 @@ class Pattern {
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
|
||||
auto LineNumber() const -> int { return line_num; }
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
@@ -48,17 +49,17 @@ class Pattern {
|
||||
// Constructs a Pattern representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Pattern(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
|
||||
Pattern(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
int line_num;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
// A pattern consisting of the `auto` keyword.
|
||||
class AutoPattern : public Pattern {
|
||||
public:
|
||||
explicit AutoPattern(int line_num) : Pattern(Kind::AutoPattern, line_num) {}
|
||||
explicit AutoPattern(SourceLocation loc) : Pattern(Kind::AutoPattern, loc) {}
|
||||
|
||||
static auto classof(const Pattern* pattern) -> bool {
|
||||
return pattern->Tag() == Kind::AutoPattern;
|
||||
@@ -69,11 +70,9 @@ class AutoPattern : public Pattern {
|
||||
// a name to it.
|
||||
class BindingPattern : public Pattern {
|
||||
public:
|
||||
BindingPattern(int line_num, std::optional<std::string> name,
|
||||
BindingPattern(SourceLocation loc, std::optional<std::string> name,
|
||||
const Pattern* type)
|
||||
: Pattern(Kind::BindingPattern, line_num),
|
||||
name(std::move(name)),
|
||||
type(type) {}
|
||||
: Pattern(Kind::BindingPattern, loc), name(std::move(name)), type(type) {}
|
||||
|
||||
static auto classof(const Pattern* pattern) -> bool {
|
||||
return pattern->Tag() == Kind::BindingPattern;
|
||||
@@ -105,8 +104,8 @@ class TuplePattern : public Pattern {
|
||||
const Pattern* pattern;
|
||||
};
|
||||
|
||||
TuplePattern(int line_num, std::vector<Field> fields)
|
||||
: Pattern(Kind::TuplePattern, line_num), fields(std::move(fields)) {}
|
||||
TuplePattern(SourceLocation loc, std::vector<Field> fields)
|
||||
: Pattern(Kind::TuplePattern, loc), fields(std::move(fields)) {}
|
||||
|
||||
// Converts tuple_literal to a TuplePattern, by wrapping each field in an
|
||||
// ExpressionPattern.
|
||||
@@ -127,13 +126,13 @@ class TuplePattern : public Pattern {
|
||||
// Converts paren_contents to a Pattern, interpreting the parentheses as
|
||||
// grouping if their contents permit that interpretation, or as forming a
|
||||
// tuple otherwise.
|
||||
auto PatternFromParenContents(int line_num,
|
||||
auto PatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const Pattern*;
|
||||
|
||||
// Converts paren_contents to a TuplePattern, interpreting the parentheses as
|
||||
// forming a tuple.
|
||||
auto TuplePatternFromParenContents(int line_num,
|
||||
auto TuplePatternFromParenContents(SourceLocation loc,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const TuplePattern*;
|
||||
|
||||
@@ -148,17 +147,17 @@ class AlternativePattern : public Pattern {
|
||||
// Constructs an AlternativePattern that matches a value of the type
|
||||
// specified by choice_type if it represents an alternative named
|
||||
// alternative_name, and its arguments match `arguments`.
|
||||
AlternativePattern(int line_num, const Expression* choice_type,
|
||||
AlternativePattern(SourceLocation loc, const Expression* choice_type,
|
||||
std::string alternative_name,
|
||||
const TuplePattern* arguments)
|
||||
: Pattern(Kind::AlternativePattern, line_num),
|
||||
: Pattern(Kind::AlternativePattern, loc),
|
||||
choice_type(choice_type),
|
||||
alternative_name(std::move(alternative_name)),
|
||||
arguments(arguments) {}
|
||||
|
||||
// Constructs an AlternativePattern that matches the alternative specified
|
||||
// by `alternative`, if its arguments match `arguments`.
|
||||
AlternativePattern(int line_num, const Expression* alternative,
|
||||
AlternativePattern(SourceLocation loc, const Expression* alternative,
|
||||
const TuplePattern* arguments);
|
||||
|
||||
static auto classof(const Pattern* pattern) -> bool {
|
||||
@@ -182,7 +181,7 @@ class AlternativePattern : public Pattern {
|
||||
class ExpressionPattern : public Pattern {
|
||||
public:
|
||||
ExpressionPattern(const Expression* expression)
|
||||
: Pattern(Kind::ExpressionPattern, expression->LineNumber()),
|
||||
: Pattern(Kind::ExpressionPattern, expression->SourceLoc()),
|
||||
expression(expression) {}
|
||||
|
||||
static auto classof(const Pattern* pattern) -> bool {
|
||||
|
||||
@@ -25,11 +25,15 @@ MATCHER_P(AutoFieldNamed, name, "") {
|
||||
return arg.name == std::string(name) && isa<AutoPattern>(arg.pattern);
|
||||
}
|
||||
|
||||
static auto FakeSourceLoc(int line_num) -> SourceLocation {
|
||||
return SourceLocation("<test>", line_num);
|
||||
}
|
||||
|
||||
TEST(PatternTest, EmptyAsPattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(), IsEmpty());
|
||||
}
|
||||
@@ -38,8 +42,8 @@ TEST(PatternTest, EmptyAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), IsEmpty());
|
||||
}
|
||||
|
||||
@@ -52,34 +56,37 @@ TEST(PatternTest, UnaryNoCommaAsPattern) {
|
||||
// ```
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 2);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(2));
|
||||
ASSERT_TRUE(isa<AutoPattern>(pattern));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryNoCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0")));
|
||||
@@ -88,25 +95,28 @@ TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
TEST(PatternTest, UnaryWithCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, BinaryAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
@@ -115,14 +125,16 @@ TEST(PatternTest, BinaryAsPattern) {
|
||||
TEST(PatternTest, BinaryAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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_SOURCE_LOCATION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_SOURCE_LOCATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class SourceLocation {
|
||||
public:
|
||||
// The filename should be eternal or arena-allocated to eliminate copies.
|
||||
SourceLocation(const char* filename, int line_num)
|
||||
: filename(filename), line_num(line_num) {}
|
||||
SourceLocation(Ptr<const std::string> filename, int line_num)
|
||||
: filename(filename->c_str()), line_num(line_num) {}
|
||||
|
||||
SourceLocation(const SourceLocation&) = default;
|
||||
SourceLocation(SourceLocation&&) = default;
|
||||
auto operator=(const SourceLocation&) -> SourceLocation& = default;
|
||||
auto operator=(SourceLocation&&) -> SourceLocation& = default;
|
||||
|
||||
bool operator==(SourceLocation other) const {
|
||||
return filename == other.filename && line_num == other.line_num;
|
||||
}
|
||||
|
||||
void Print(llvm::raw_ostream& out) const {
|
||||
out << filename << ":" << line_num;
|
||||
}
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
|
||||
private:
|
||||
std::string_view filename;
|
||||
int line_num;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_SOURCE_LOCATION_H_
|
||||
@@ -130,9 +130,9 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
Return::Return(int line_num, const Expression* exp, bool is_omitted_exp)
|
||||
: Statement(Kind::Return, line_num),
|
||||
exp(exp != nullptr ? exp : global_arena->RawNew<TupleLiteral>(line_num)),
|
||||
Return::Return(SourceLocation loc, const Expression* exp, bool is_omitted_exp)
|
||||
: Statement(Kind::Return, loc),
|
||||
exp(exp != nullptr ? exp : global_arena->RawNew<TupleLiteral>(loc)),
|
||||
is_omitted_exp(is_omitted_exp) {
|
||||
CHECK(exp != nullptr || is_omitted_exp);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -37,7 +38,7 @@ class Statement {
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
|
||||
auto LineNumber() const -> int { return line_num; }
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
|
||||
void PrintDepth(int depth, llvm::raw_ostream& out) const;
|
||||
@@ -47,17 +48,17 @@ class Statement {
|
||||
// Constructs an Statement representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Statement(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
|
||||
Statement(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
int line_num;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
class ExpressionStatement : public Statement {
|
||||
public:
|
||||
ExpressionStatement(int line_num, const Expression* exp)
|
||||
: Statement(Kind::ExpressionStatement, line_num), exp(exp) {}
|
||||
ExpressionStatement(SourceLocation loc, const Expression* exp)
|
||||
: Statement(Kind::ExpressionStatement, loc), exp(exp) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::ExpressionStatement;
|
||||
@@ -71,8 +72,8 @@ class ExpressionStatement : public Statement {
|
||||
|
||||
class Assign : public Statement {
|
||||
public:
|
||||
Assign(int line_num, const Expression* lhs, const Expression* rhs)
|
||||
: Statement(Kind::Assign, line_num), lhs(lhs), rhs(rhs) {}
|
||||
Assign(SourceLocation loc, const Expression* lhs, const Expression* rhs)
|
||||
: Statement(Kind::Assign, loc), lhs(lhs), rhs(rhs) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Assign;
|
||||
@@ -88,8 +89,9 @@ class Assign : public Statement {
|
||||
|
||||
class VariableDefinition : public Statement {
|
||||
public:
|
||||
VariableDefinition(int line_num, const Pattern* pat, const Expression* init)
|
||||
: Statement(Kind::VariableDefinition, line_num), pat(pat), init(init) {}
|
||||
VariableDefinition(SourceLocation loc, const Pattern* pat,
|
||||
const Expression* init)
|
||||
: Statement(Kind::VariableDefinition, loc), pat(pat), init(init) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::VariableDefinition;
|
||||
@@ -105,9 +107,9 @@ class VariableDefinition : public Statement {
|
||||
|
||||
class If : public Statement {
|
||||
public:
|
||||
If(int line_num, const Expression* cond, const Statement* then_stmt,
|
||||
If(SourceLocation loc, const Expression* cond, const Statement* then_stmt,
|
||||
const Statement* else_stmt)
|
||||
: Statement(Kind::If, line_num),
|
||||
: Statement(Kind::If, loc),
|
||||
cond(cond),
|
||||
then_stmt(then_stmt),
|
||||
else_stmt(else_stmt) {}
|
||||
@@ -128,7 +130,7 @@ class If : public Statement {
|
||||
|
||||
class Return : public Statement {
|
||||
public:
|
||||
Return(int line_num, const Expression* exp, bool is_omitted_exp);
|
||||
Return(SourceLocation loc, const Expression* exp, bool is_omitted_exp);
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Return;
|
||||
@@ -144,8 +146,8 @@ class Return : public Statement {
|
||||
|
||||
class Sequence : public Statement {
|
||||
public:
|
||||
Sequence(int line_num, const Statement* stmt, const Statement* next)
|
||||
: Statement(Kind::Sequence, line_num), stmt(stmt), next(next) {}
|
||||
Sequence(SourceLocation loc, const Statement* stmt, const Statement* next)
|
||||
: Statement(Kind::Sequence, loc), stmt(stmt), next(next) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Sequence;
|
||||
@@ -161,8 +163,8 @@ class Sequence : public Statement {
|
||||
|
||||
class Block : public Statement {
|
||||
public:
|
||||
Block(int line_num, const Statement* stmt)
|
||||
: Statement(Kind::Block, line_num), stmt(stmt) {}
|
||||
Block(SourceLocation loc, const Statement* stmt)
|
||||
: Statement(Kind::Block, loc), stmt(stmt) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Block;
|
||||
@@ -176,8 +178,8 @@ class Block : public Statement {
|
||||
|
||||
class While : public Statement {
|
||||
public:
|
||||
While(int line_num, const Expression* cond, const Statement* body)
|
||||
: Statement(Kind::While, line_num), cond(cond), body(body) {}
|
||||
While(SourceLocation loc, const Expression* cond, const Statement* body)
|
||||
: Statement(Kind::While, loc), cond(cond), body(body) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::While;
|
||||
@@ -193,7 +195,7 @@ class While : public Statement {
|
||||
|
||||
class Break : public Statement {
|
||||
public:
|
||||
explicit Break(int line_num) : Statement(Kind::Break, line_num) {}
|
||||
explicit Break(SourceLocation loc) : Statement(Kind::Break, loc) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Break;
|
||||
@@ -202,7 +204,7 @@ class Break : public Statement {
|
||||
|
||||
class Continue : public Statement {
|
||||
public:
|
||||
explicit Continue(int line_num) : Statement(Kind::Continue, line_num) {}
|
||||
explicit Continue(SourceLocation loc) : Statement(Kind::Continue, loc) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Continue;
|
||||
@@ -211,9 +213,9 @@ class Continue : public Statement {
|
||||
|
||||
class Match : public Statement {
|
||||
public:
|
||||
Match(int line_num, const Expression* exp,
|
||||
Match(SourceLocation loc, const Expression* exp,
|
||||
std::list<std::pair<const Pattern*, const Statement*>>* clauses)
|
||||
: Statement(Kind::Match, line_num), exp(exp), clauses(clauses) {}
|
||||
: Statement(Kind::Match, loc), exp(exp), clauses(clauses) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Match;
|
||||
@@ -237,9 +239,9 @@ class Match : public Statement {
|
||||
// }
|
||||
class Continuation : public Statement {
|
||||
public:
|
||||
Continuation(int line_num, std::string continuation_variable,
|
||||
Continuation(SourceLocation loc, std::string continuation_variable,
|
||||
const Statement* body)
|
||||
: Statement(Kind::Continuation, line_num),
|
||||
: Statement(Kind::Continuation, loc),
|
||||
continuation_variable(std::move(continuation_variable)),
|
||||
body(body) {}
|
||||
|
||||
@@ -262,8 +264,8 @@ class Continuation : public Statement {
|
||||
// __run <argument>;
|
||||
class Run : public Statement {
|
||||
public:
|
||||
Run(int line_num, const Expression* argument)
|
||||
: Statement(Kind::Run, line_num), argument(argument) {}
|
||||
Run(SourceLocation loc, const Expression* argument)
|
||||
: Statement(Kind::Run, loc), argument(argument) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Run;
|
||||
@@ -280,7 +282,7 @@ class Run : public Statement {
|
||||
// __await;
|
||||
class Await : public Statement {
|
||||
public:
|
||||
explicit Await(int line_num) : Statement(Kind::Await, line_num) {}
|
||||
explicit Await(SourceLocation loc) : Statement(Kind::Await, loc) {}
|
||||
|
||||
static auto classof(const Statement* stmt) -> bool {
|
||||
return stmt->Tag() == Kind::Await;
|
||||
|
||||
Reference in New Issue
Block a user