mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user