Skip to content

Commit fbe22e8

Browse files
Merge #940
940: Add more location info to AST structures r=CohenArthur a=CohenArthur Two classes still remain locus-less: `TupleStructItems` and `TuplePatternItems` as I do not believe they are constructed at the moment. Co-authored-by: Arthur Cohen <[email protected]>
2 parents 9fb06d6 + 0e15b89 commit fbe22e8

File tree

7 files changed

+78
-62
lines changed

7 files changed

+78
-62
lines changed

gcc/rust/ast/rust-expr.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -953,12 +953,11 @@ class ArrayElems
953953
class ArrayElemsValues : public ArrayElems
954954
{
955955
std::vector<std::unique_ptr<Expr> > values;
956-
957-
// TODO: should this store location data?
956+
Location locus;
958957

959958
public:
960-
ArrayElemsValues (std::vector<std::unique_ptr<Expr> > elems)
961-
: ArrayElems (), values (std::move (elems))
959+
ArrayElemsValues (std::vector<std::unique_ptr<Expr> > elems, Location locus)
960+
: ArrayElems (), values (std::move (elems)), locus (locus)
962961
{}
963962

964963
// copy constructor with vector clone
@@ -1008,15 +1007,14 @@ class ArrayElemsCopied : public ArrayElems
10081007
{
10091008
std::unique_ptr<Expr> elem_to_copy;
10101009
std::unique_ptr<Expr> num_copies;
1011-
1012-
// TODO: should this store location data?
1010+
Location locus;
10131011

10141012
public:
10151013
// Constructor requires pointers for polymorphism
10161014
ArrayElemsCopied (std::unique_ptr<Expr> copied_elem,
1017-
std::unique_ptr<Expr> copy_amount)
1015+
std::unique_ptr<Expr> copy_amount, Location locus)
10181016
: ArrayElems (), elem_to_copy (std::move (copied_elem)),
1019-
num_copies (std::move (copy_amount))
1017+
num_copies (std::move (copy_amount)), locus (locus)
10201018
{}
10211019

10221020
// Copy constructor required due to unique_ptr - uses custom clone
@@ -1516,11 +1514,11 @@ struct StructBase
15161514
{
15171515
private:
15181516
std::unique_ptr<Expr> base_struct;
1517+
Location locus;
15191518

15201519
public:
1521-
// TODO: should this store location data?
1522-
StructBase (std::unique_ptr<Expr> base_struct_ptr)
1523-
: base_struct (std::move (base_struct_ptr))
1520+
StructBase (std::unique_ptr<Expr> base_struct_ptr, Location locus)
1521+
: base_struct (std::move (base_struct_ptr)), locus (locus)
15241522
{}
15251523

15261524
// Copy constructor requires clone
@@ -1552,7 +1550,7 @@ struct StructBase
15521550
StructBase &operator= (StructBase &&other) = default;
15531551

15541552
// Returns a null expr-ed StructBase - error state
1555-
static StructBase error () { return StructBase (nullptr); }
1553+
static StructBase error () { return StructBase (nullptr, Location ()); }
15561554

15571555
// Returns whether StructBase is in error state
15581556
bool is_invalid () const { return base_struct == nullptr; }
@@ -2136,8 +2134,7 @@ struct ClosureParam
21362134

21372135
// bool has_type_given;
21382136
std::unique_ptr<Type> type;
2139-
2140-
// TODO: should this store location data?
2137+
Location locus;
21412138

21422139
public:
21432140
// Returns whether the type of the parameter has been given.
@@ -2146,11 +2143,12 @@ struct ClosureParam
21462143
bool has_outer_attrs () const { return !outer_attrs.empty (); }
21472144

21482145
// Constructor for closure parameter
2149-
ClosureParam (std::unique_ptr<Pattern> param_pattern,
2146+
ClosureParam (std::unique_ptr<Pattern> param_pattern, Location locus,
21502147
std::unique_ptr<Type> param_type = nullptr,
21512148
std::vector<Attribute> outer_attrs = {})
21522149
: outer_attrs (std::move (outer_attrs)),
2153-
pattern (std::move (param_pattern)), type (std::move (param_type))
2150+
pattern (std::move (param_pattern)), type (std::move (param_type)),
2151+
locus (locus)
21542152
{}
21552153

21562154
// Copy constructor required due to cloning as a result of unique_ptrs
@@ -2191,7 +2189,10 @@ struct ClosureParam
21912189
bool is_error () const { return pattern == nullptr; }
21922190

21932191
// Creates an error state closure parameter.
2194-
static ClosureParam create_error () { return ClosureParam (nullptr); }
2192+
static ClosureParam create_error ()
2193+
{
2194+
return ClosureParam (nullptr, Location ());
2195+
}
21952196

21962197
std::string as_string () const;
21972198

gcc/rust/ast/rust-item.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,12 +3941,11 @@ struct NamedFunctionParam
39413941

39423942
std::unique_ptr<Type> param_type;
39433943

3944-
// TODO: should this store location data?
3945-
39463944
// seemingly new since writing this node
39473945
std::vector<Attribute> outer_attrs;
39483946

39493947
NodeId node_id;
3948+
Location locus;
39503949

39513950
public:
39523951
/* Returns whether the named function parameter has a name (i.e. name is not
@@ -3967,14 +3966,14 @@ struct NamedFunctionParam
39673966
// Creates an error state named function parameter.
39683967
static NamedFunctionParam create_error ()
39693968
{
3970-
return NamedFunctionParam ("", nullptr, {});
3969+
return NamedFunctionParam ("", nullptr, {}, Location ());
39713970
}
39723971

39733972
NamedFunctionParam (std::string name, std::unique_ptr<Type> param_type,
3974-
std::vector<Attribute> outer_attrs)
3973+
std::vector<Attribute> outer_attrs, Location locus)
39753974
: name (std::move (name)), param_type (std::move (param_type)),
39763975
outer_attrs (std::move (outer_attrs)),
3977-
node_id (Analysis::Mappings::get ()->get_next_node_id ())
3976+
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
39783977
{}
39793978

39803979
// Copy constructor

gcc/rust/ast/rust-path.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ namespace AST {
3333
class PathIdentSegment
3434
{
3535
std::string segment_name;
36-
37-
// TODO: should this have location info stored?
36+
Location locus;
3837

3938
// only allow identifiers, "super", "self", "Self", "crate", or "$crate"
4039
public:
41-
PathIdentSegment (std::string segment_name)
42-
: segment_name (std::move (segment_name))
40+
PathIdentSegment (std::string segment_name, Location locus)
41+
: segment_name (std::move (segment_name)), locus (locus)
4342
{}
4443

4544
/* TODO: insert check in constructor for this? Or is this a semantic error
@@ -49,7 +48,10 @@ class PathIdentSegment
4948
* not entirely sure */
5049

5150
// Creates an error PathIdentSegment.
52-
static PathIdentSegment create_error () { return PathIdentSegment (""); }
51+
static PathIdentSegment create_error ()
52+
{
53+
return PathIdentSegment ("", Location ());
54+
}
5355

5456
// Returns whether PathIdentSegment is in an error state.
5557
bool is_error () const { return segment_name.empty (); }
@@ -221,7 +223,7 @@ class PathExprSegment
221223
bool has_generic_args () const { return generic_args.has_generic_args (); }
222224

223225
// Constructor for segment (from IdentSegment and GenericArgs)
224-
PathExprSegment (PathIdentSegment segment_name, Location locus = Location (),
226+
PathExprSegment (PathIdentSegment segment_name, Location locus,
225227
GenericArgs generic_args = GenericArgs::create_empty ())
226228
: segment_name (std::move (segment_name)),
227229
generic_args (std::move (generic_args)), locus (locus),
@@ -237,7 +239,7 @@ class PathExprSegment
237239
= std::vector<std::unique_ptr<Type> > (),
238240
std::vector<GenericArgsBinding> binding_args
239241
= std::vector<GenericArgsBinding> ())
240-
: segment_name (PathIdentSegment (std::move (segment_name))),
242+
: segment_name (PathIdentSegment (std::move (segment_name), locus)),
241243
generic_args (GenericArgs (std::move (lifetime_args),
242244
std::move (type_args),
243245
std::move (binding_args))),
@@ -250,7 +252,7 @@ class PathExprSegment
250252
// Creates an error-state path expression segment.
251253
static PathExprSegment create_error ()
252254
{
253-
return PathExprSegment (PathIdentSegment::create_error ());
255+
return PathExprSegment (PathIdentSegment::create_error (), Location ());
254256
}
255257

256258
std::string as_string () const;
@@ -440,7 +442,7 @@ class TypePathSegment
440442

441443
TypePathSegment (std::string segment_name,
442444
bool has_separating_scope_resolution, Location locus)
443-
: ident_segment (PathIdentSegment (std::move (segment_name))),
445+
: ident_segment (PathIdentSegment (std::move (segment_name), locus)),
444446
locus (locus),
445447
has_separating_scope_resolution (has_separating_scope_resolution),
446448
node_id (Analysis::Mappings::get ()->get_next_node_id ())
@@ -539,11 +541,13 @@ struct TypePathFunction
539541
// FIXME: think of better way to mark as invalid than taking up storage
540542
bool is_invalid;
541543

542-
// TODO: should this have location info?
544+
Location locus;
543545

544546
protected:
545547
// Constructor only used to create invalid type path functions.
546-
TypePathFunction (bool is_invalid) : is_invalid (is_invalid) {}
548+
TypePathFunction (bool is_invalid, Location locus)
549+
: is_invalid (is_invalid), locus (locus)
550+
{}
547551

548552
public:
549553
// Returns whether the return type of the function has been specified.
@@ -556,13 +560,16 @@ struct TypePathFunction
556560
bool is_error () const { return is_invalid; }
557561

558562
// Creates an error state function.
559-
static TypePathFunction create_error () { return TypePathFunction (true); }
563+
static TypePathFunction create_error ()
564+
{
565+
return TypePathFunction (true, Location ());
566+
}
560567

561568
// Constructor
562-
TypePathFunction (std::vector<std::unique_ptr<Type> > inputs,
569+
TypePathFunction (std::vector<std::unique_ptr<Type> > inputs, Location locus,
563570
std::unique_ptr<Type> type = nullptr)
564571
: inputs (std::move (inputs)), return_type (std::move (type)),
565-
is_invalid (false)
572+
is_invalid (false), locus (locus)
566573
{}
567574

568575
// Copy constructor with clone

gcc/rust/ast/rust-pattern.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,18 @@ class StructPattern : public Pattern
848848
// bool has_struct_pattern_elements;
849849
StructPatternElements elems;
850850

851-
// TODO: should this store location data? Accessor uses path location data.
852851
NodeId node_id;
852+
Location locus;
853853

854854
public:
855855
std::string as_string () const override;
856856

857857
// Constructs a struct pattern from specified StructPatternElements
858-
StructPattern (PathInExpression struct_path,
858+
StructPattern (PathInExpression struct_path, Location locus,
859859
StructPatternElements elems
860860
= StructPatternElements::create_empty ())
861861
: path (std::move (struct_path)), elems (std::move (elems)),
862-
node_id (Analysis::Mappings::get ()->get_next_node_id ())
862+
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
863863
{}
864864

865865
/* TODO: constructor to construct via elements included in

0 commit comments

Comments
 (0)