forked from Rust-GCC/gccrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust-item.h
4204 lines (3344 loc) · 126 KB
/
rust-item.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef RUST_AST_ITEM_H
#define RUST_AST_ITEM_H
#include "rust-ast.h"
#include "rust-hir-map.h"
#include "rust-mapping-common.h"
#include "rust-path.h"
#include "rust-common.h"
#include "rust-expr.h"
namespace Rust {
namespace AST {
// forward decls
class TypePath;
// TODO: inline?
/*struct AbiName {
std::string abi_name;
// Technically is meant to be STRING_LITERAL
public:
// Returns whether abi name is empty, i.e. doesn't exist.
bool is_empty() const {
return abi_name.empty();
}
AbiName(std::string name) : abi_name(std::move(name)) {}
// Empty AbiName constructor
AbiName() {}
};*/
// A type generic parameter (as opposed to a lifetime generic parameter)
class TypeParam : public GenericParam
{
// bool has_outer_attribute;
// std::unique_ptr<Attribute> outer_attr;
Attribute outer_attr;
Identifier type_representation;
// bool has_type_param_bounds;
// TypeParamBounds type_param_bounds;
std::vector<std::unique_ptr<TypeParamBound>>
type_param_bounds; // inlined form
// bool has_type;
std::unique_ptr<Type> type;
location_t locus;
public:
Identifier get_type_representation () const { return type_representation; }
// Returns whether the type of the type param has been specified.
bool has_type () const { return type != nullptr; }
// Returns whether the type param has type param bounds.
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
// Returns whether the type param has an outer attribute.
bool has_outer_attribute () const { return !outer_attr.is_empty (); }
Attribute &get_outer_attribute () { return outer_attr; }
TypeParam (Identifier type_representation, location_t locus = UNDEF_LOCATION,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
= std::vector<std::unique_ptr<TypeParamBound>> (),
std::unique_ptr<Type> type = nullptr,
Attribute outer_attr = Attribute::create_empty ())
: GenericParam (Analysis::Mappings::get ()->get_next_node_id ()),
outer_attr (std::move (outer_attr)),
type_representation (std::move (type_representation)),
type_param_bounds (std::move (type_param_bounds)),
type (std::move (type)), locus (locus)
{}
// Copy constructor uses clone
TypeParam (TypeParam const &other)
: GenericParam (other.node_id), outer_attr (other.outer_attr),
type_representation (other.type_representation), locus (other.locus)
{
// guard to prevent null pointer dereference
if (other.type != nullptr)
type = other.type->clone_type ();
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
}
// Overloaded assignment operator to clone
TypeParam &operator= (TypeParam const &other)
{
type_representation = other.type_representation;
outer_attr = other.outer_attr;
locus = other.locus;
node_id = other.node_id;
// guard to prevent null pointer dereference
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
return *this;
}
// move constructors
TypeParam (TypeParam &&other) = default;
TypeParam &operator= (TypeParam &&other) = default;
std::string as_string () const override;
location_t get_locus () const override final { return locus; }
Kind get_kind () const override final { return Kind::Type; }
void accept_vis (ASTVisitor &vis) override;
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
// TODO: mutable getter seems kinda dodgy
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
protected:
// Clone function implementation as virtual method
TypeParam *clone_generic_param_impl () const override
{
return new TypeParam (*this);
}
};
/* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
* TypeBoundWhereClauseItem */
class WhereClauseItem
{
public:
virtual ~WhereClauseItem () {}
// Unique pointer custom clone function
std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
{
return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
}
virtual std::string as_string () const = 0;
virtual void accept_vis (ASTVisitor &vis) = 0;
virtual NodeId get_node_id () const = 0;
protected:
// Clone function implementation as pure virtual method
virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
};
// A lifetime where clause item
class LifetimeWhereClauseItem : public WhereClauseItem
{
Lifetime lifetime;
std::vector<Lifetime> lifetime_bounds;
location_t locus;
NodeId node_id;
public:
LifetimeWhereClauseItem (Lifetime lifetime,
std::vector<Lifetime> lifetime_bounds,
location_t locus)
: lifetime (std::move (lifetime)),
lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
NodeId get_node_id () const override final { return node_id; }
Lifetime &get_lifetime () { return lifetime; }
std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
location_t get_locus () const { return locus; }
protected:
// Clone function implementation as (not pure) virtual method
LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
{
return new LifetimeWhereClauseItem (*this);
}
};
// A type bound where clause item
class TypeBoundWhereClauseItem : public WhereClauseItem
{
std::vector<LifetimeParam> for_lifetimes;
std::unique_ptr<Type> bound_type;
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
NodeId node_id;
location_t locus;
public:
// Returns whether the item has ForLifetimes
bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
// Returns whether the item has type param bounds
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
TypeBoundWhereClauseItem (
std::vector<LifetimeParam> for_lifetimes, std::unique_ptr<Type> bound_type,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
location_t locus)
: for_lifetimes (std::move (for_lifetimes)),
bound_type (std::move (bound_type)),
type_param_bounds (std::move (type_param_bounds)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
{}
// Copy constructor requires clone
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other)
: for_lifetimes (other.for_lifetimes),
bound_type (other.bound_type->clone_type ())
{
node_id = other.node_id;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
}
// Overload assignment operator to clone
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other)
{
node_id = other.node_id;
for_lifetimes = other.for_lifetimes;
bound_type = other.bound_type->clone_type ();
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
return *this;
}
// move constructors
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
= default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
std::unique_ptr<Type> &get_type ()
{
rust_assert (bound_type != nullptr);
return bound_type;
}
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
NodeId get_node_id () const override final { return node_id; }
location_t get_locus () const { return locus; }
protected:
// Clone function implementation as (not pure) virtual method
TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
{
return new TypeBoundWhereClauseItem (*this);
}
};
// A where clause
class WhereClause
{
std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
NodeId node_id;
public:
WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
: where_clause_items (std::move (where_clause_items)),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
// copy constructor with vector clone
WhereClause (WhereClause const &other)
{
node_id = other.node_id;
where_clause_items.reserve (other.where_clause_items.size ());
for (const auto &e : other.where_clause_items)
where_clause_items.push_back (e->clone_where_clause_item ());
}
// overloaded assignment operator with vector clone
WhereClause &operator= (WhereClause const &other)
{
node_id = other.node_id;
where_clause_items.reserve (other.where_clause_items.size ());
for (const auto &e : other.where_clause_items)
where_clause_items.push_back (e->clone_where_clause_item ());
return *this;
}
// move constructors
WhereClause (WhereClause &&other) = default;
WhereClause &operator= (WhereClause &&other) = default;
// Creates a WhereClause with no items.
static WhereClause create_empty ()
{
return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
}
// Returns whether the WhereClause has no items.
bool is_empty () const { return where_clause_items.empty (); }
std::string as_string () const;
NodeId get_node_id () const { return node_id; }
// TODO: this mutable getter seems kinda dodgy
std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
{
return where_clause_items;
}
const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
{
return where_clause_items;
}
};
// Abstract class Param
class Param : public Visitable
{
public:
Param (std::vector<Attribute> outer_attrs, location_t locus)
: outer_attrs (std::move (outer_attrs)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
virtual ~Param () = default;
std::unique_ptr<Param> clone_param () const
{
return std::unique_ptr<Param> (clone_param_impl ());
}
virtual bool is_variadic () const { return false; }
virtual bool is_self () const { return false; }
NodeId get_node_id () const { return node_id; }
location_t get_locus () const { return locus; }
std::vector<Attribute> get_outer_attrs () const { return outer_attrs; }
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
virtual Param *clone_param_impl () const = 0;
virtual std::string as_string () const = 0;
protected:
std::vector<Attribute> outer_attrs;
location_t locus;
NodeId node_id;
};
// A self parameter in a method
class SelfParam : public Param
{
bool has_ref;
bool is_mut;
// bool has_lifetime; // only possible if also ref
Lifetime lifetime;
// bool has_type; // only possible if not ref
std::unique_ptr<Type> type;
// Unrestricted constructor used for error state
SelfParam (Lifetime lifetime, bool has_ref, bool is_mut, Type *type)
: Param ({}, UNDEF_LOCATION), has_ref (has_ref), is_mut (is_mut),
lifetime (std::move (lifetime)), type (type)
{}
// this is ok as no outside classes can ever call this
// TODO: self param can have outer attributes
public:
// Returns whether the self-param has a type field.
bool has_type () const { return type != nullptr; }
// Returns whether the self-param has a valid lifetime.
bool has_lifetime () const { return !lifetime.is_error (); }
// Returns whether the self-param is in an error state.
bool is_error () const
{
return (has_type () && has_lifetime ()) || (has_lifetime () && !has_ref);
// not having either is not an error
}
// Creates an error state self-param.
static SelfParam create_error ()
{
// cannot have no ref but have a lifetime at the same time
return SelfParam (Lifetime (Lifetime::STATIC), false, false, nullptr);
}
// Type-based self parameter (not ref, no lifetime)
SelfParam (std::unique_ptr<Type> type, bool is_mut, location_t locus)
: Param ({}, locus), has_ref (false), is_mut (is_mut),
lifetime (Lifetime::error ()), type (std::move (type))
{}
// Lifetime-based self parameter (is ref, no type)
SelfParam (Lifetime lifetime, bool is_mut, location_t locus)
: Param ({}, locus), has_ref (true), is_mut (is_mut),
lifetime (std::move (lifetime))
{}
// Copy constructor requires clone
SelfParam (SelfParam const &other)
: Param (other.get_outer_attrs (), other.get_locus ()),
has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime)
{
node_id = other.node_id;
if (other.type != nullptr)
type = other.type->clone_type ();
}
// Overload assignment operator to use clone
SelfParam &operator= (SelfParam const &other)
{
is_mut = other.is_mut;
has_ref = other.has_ref;
lifetime = other.lifetime;
locus = other.locus;
node_id = other.node_id;
outer_attrs = other.outer_attrs;
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
return *this;
}
// move constructors
SelfParam (SelfParam &&other) = default;
SelfParam &operator= (SelfParam &&other) = default;
std::string as_string () const override;
location_t get_locus () const { return locus; }
bool is_self () const override { return true; }
bool get_has_ref () const { return has_ref; };
bool get_is_mut () const { return is_mut; }
Lifetime get_lifetime () const { return lifetime; }
Lifetime &get_lifetime () { return lifetime; }
NodeId get_node_id () const { return node_id; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (has_type ());
return type;
}
void accept_vis (ASTVisitor &vis) override;
SelfParam *clone_param_impl () const override
{
return new SelfParam (*this);
}
};
// Qualifiers for function, i.e. const, unsafe, extern etc.
class FunctionQualifiers
{
AsyncConstStatus const_status;
bool has_unsafe;
bool has_extern;
std::string extern_abi;
location_t locus;
public:
FunctionQualifiers (location_t locus, AsyncConstStatus const_status,
bool has_unsafe, bool has_extern = false,
std::string extern_abi = std::string ())
: const_status (const_status), has_unsafe (has_unsafe),
has_extern (has_extern), extern_abi (std::move (extern_abi)),
locus (locus)
{
if (!this->extern_abi.empty ())
{
// having extern is required; not having it is an implementation error
rust_assert (has_extern);
}
}
std::string as_string () const;
AsyncConstStatus get_const_status () const { return const_status; }
bool is_unsafe () const { return has_unsafe; }
bool is_extern () const { return has_extern; }
std::string get_extern_abi () const { return extern_abi; }
bool has_abi () const { return !extern_abi.empty (); }
location_t get_locus () const { return locus; }
};
class VariadicParam : public Param
{
std::unique_ptr<Pattern> param_name;
public:
VariadicParam (std::unique_ptr<Pattern> param_name,
std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), std::move (locus)),
param_name (std::move (param_name))
{}
VariadicParam (std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), std::move (locus)), param_name (nullptr)
{}
VariadicParam (VariadicParam const &other)
: Param (other.get_outer_attrs (), other.locus)
{
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
}
VariadicParam &operator= (VariadicParam const &other)
{
outer_attrs = other.outer_attrs;
locus = other.locus;
node_id = other.node_id;
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
else
param_name = nullptr;
return *this;
}
bool is_variadic () const override { return true; }
VariadicParam *clone_param_impl () const override
{
return new VariadicParam (*this);
}
std::unique_ptr<Pattern> &get_pattern ()
{
rust_assert (param_name != nullptr);
return param_name;
}
const std::unique_ptr<Pattern> &get_pattern () const
{
rust_assert (param_name != nullptr);
return param_name;
}
bool has_pattern () const { return param_name != nullptr; }
void accept_vis (ASTVisitor &vis) override;
std::string as_string () const override;
};
// A function parameter
class FunctionParam : public Param
{
std::unique_ptr<Pattern> param_name;
std::unique_ptr<Type> type;
public:
FunctionParam (std::unique_ptr<Pattern> param_name,
std::unique_ptr<Type> param_type,
std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), locus),
param_name (std::move (param_name)), type (std::move (param_type))
{}
// Copy constructor uses clone
FunctionParam (FunctionParam const &other)
: Param (other.get_outer_attrs (), other.locus)
{
// guard to prevent nullptr dereference
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
if (other.type != nullptr)
type = other.type->clone_type ();
}
// Overload assignment operator to use clone
FunctionParam &operator= (FunctionParam const &other)
{
locus = other.locus;
node_id = other.node_id;
// guard to prevent nullptr dereference
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
else
param_name = nullptr;
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
return *this;
}
// move constructors
FunctionParam (FunctionParam &&other) = default;
FunctionParam &operator= (FunctionParam &&other) = default;
// Returns whether FunctionParam is in an invalid state.
bool is_error () const { return param_name == nullptr || type == nullptr; }
// Creates an error FunctionParam.
static FunctionParam create_error ()
{
return FunctionParam (nullptr, nullptr, {}, UNDEF_LOCATION);
}
std::string as_string () const override;
// TODO: seems kinda dodgy. Think of better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Pattern> &get_pattern ()
{
rust_assert (param_name != nullptr);
return param_name;
}
bool has_name () const { return param_name != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
FunctionParam *clone_param_impl () const override
{
return new FunctionParam (*this);
}
void accept_vis (ASTVisitor &vis) override;
};
// Rust module item - abstract base class
class Module : public VisItem
{
public:
// Type of the current module. A module can be either loaded or unloaded,
// meaning that the items of the module can already be present or not. For
// example, the following module would be loaded: `mod foo { fn bar() {} }`.
// However, the module would be unloaded if it refers to an external file (i.e
// `mod foo;`) and then become loaded upon expansion.
enum ModuleKind
{
LOADED,
UNLOADED,
};
Identifier get_name () const { return module_name; }
AST::Kind get_ast_kind () const override { return AST::Kind::MODULE; }
private:
Identifier module_name;
location_t locus;
ModuleKind kind;
// Name of the file including the module
std::string outer_filename;
// bool has_inner_attrs;
std::vector<Attribute> inner_attrs;
// bool has_items;
std::vector<std::unique_ptr<Item>> items;
// Names of including inline modules (immediate parent is last in the list)
std::vector<std::string> module_scope;
// Filename the module refers to. Empty string on LOADED modules or if an
// error occured when dealing with UNLOADED modules
std::string module_file;
void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
{
items.reserve (other_items.size ());
for (const auto &e : other_items)
items.push_back (e->clone_item ());
}
public:
// Returns whether the module has items in its body.
bool has_items () const { return !items.empty (); }
// Returns whether the module has any inner attributes.
bool has_inner_attrs () const { return !inner_attrs.empty (); }
// Unloaded module constructor
Module (Identifier module_name, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus,
std::string outer_filename, std::vector<std::string> module_scope)
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
outer_filename (outer_filename), inner_attrs (std::vector<Attribute> ()),
items (std::vector<std::unique_ptr<Item>> ()),
module_scope (std::move (module_scope))
{}
// Loaded module constructor, with items
Module (Identifier name, location_t locus,
std::vector<std::unique_ptr<Item>> items,
Visibility visibility = Visibility::create_error (),
std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (name), locus (locus), kind (ModuleKind::LOADED),
outer_filename (std::string ()), inner_attrs (std::move (inner_attrs)),
items (std::move (items))
{}
// Copy constructor with vector clone
Module (Module const &other)
: VisItem (other), module_name (other.module_name), locus (other.locus),
kind (other.kind), inner_attrs (other.inner_attrs),
module_scope (other.module_scope)
{
// We need to check whether we are copying a loaded module or an unloaded
// one. In the second case, clear the `items` vector.
if (other.kind == LOADED)
clone_items (other.items);
else
items.clear ();
}
// Overloaded assignment operator with vector clone
Module &operator= (Module const &other)
{
VisItem::operator= (other);
module_name = other.module_name;
locus = other.locus;
kind = other.kind;
inner_attrs = other.inner_attrs;
module_scope = other.module_scope;
// Likewise, we need to clear the `items` vector in case the other module is
// unloaded
if (kind == LOADED)
clone_items (other.items);
else
items.clear ();
return *this;
}
// Search for the filename associated with an external module, storing it in
// module_file
void process_file_path ();
// Load the items contained in an external module
void load_items ();
void accept_vis (ASTVisitor &vis) override;
/* Override that runs the function recursively on all items contained within
* the module. */
void add_crate_name (std::vector<std::string> &names) const override;
// Returns the kind of the module
enum ModuleKind get_kind () const { return kind; }
// TODO: think of better way to do this - mutable getter seems dodgy
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
const std::vector<std::unique_ptr<Item>> &get_items () const { return items; }
std::vector<std::unique_ptr<Item>> &get_items () { return items; }
std::vector<std::unique_ptr<AST::Item>> take_items ()
{
return std::move (items);
}
void set_items (std::vector<std::unique_ptr<AST::Item>> &&new_items)
{
items = std::move (new_items);
}
// move constructors
Module (Module &&other) = default;
Module &operator= (Module &&other) = default;
std::string as_string () const override;
location_t get_locus () const override final { return locus; }
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { module_name = {""}; }
bool is_marked_for_strip () const override { return module_name.empty (); }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Module *clone_item_impl () const override { return new Module (*this); }
};
// Rust extern crate declaration AST node
class ExternCrate : public VisItem
{
// this is either an identifier or "self", with self parsed to string
std::string referenced_crate;
// bool has_as_clause;
// AsClause as_clause;
// this is either an identifier or "_", with _ parsed to string
std::string as_clause_name;
location_t locus;
/* e.g.
"extern crate foo as _"
"extern crate foo"
"extern crate std as cool_std" */
public:
std::string as_string () const override;
// Returns whether extern crate declaration has an as clause.
bool has_as_clause () const { return !as_clause_name.empty (); }
/* Returns whether extern crate declaration references the current crate
* (i.e. self). */
bool references_self () const { return referenced_crate == "self"; }
// Constructor
ExternCrate (std::string referenced_crate, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus,
std::string as_clause_name = std::string ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
referenced_crate (std::move (referenced_crate)),
as_clause_name (std::move (as_clause_name)), locus (locus)
{}
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
const std::string &get_referenced_crate () const { return referenced_crate; }
const std::string &get_as_clause () const { return as_clause_name; }
// Override that adds extern crate name in decl to passed list of names.
void add_crate_name (std::vector<std::string> &names) const override
{
names.push_back (referenced_crate);
}
// Invalid if crate name is empty, so base stripping on that.
void mark_for_strip () override { referenced_crate = ""; }
bool is_marked_for_strip () const override
{
return referenced_crate.empty ();
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternCrate *clone_item_impl () const override
{
return new ExternCrate (*this);
}
};
// The path-ish thing referred to in a use declaration - abstract base class
class UseTree
{
location_t locus;
NodeId node_id;
public:
enum Kind
{
Glob,
Rebind,
List,
};
virtual ~UseTree () {}
// Overload assignment operator to clone
UseTree &operator= (UseTree const &other)
{
locus = other.locus;
return *this;
}
UseTree (const UseTree &other) = default;
// move constructors
UseTree (UseTree &&other) = default;
UseTree &operator= (UseTree &&other) = default;
// Unique pointer custom clone function
std::unique_ptr<UseTree> clone_use_tree () const
{
return std::unique_ptr<UseTree> (clone_use_tree_impl ());
}
virtual std::string as_string () const = 0;
virtual Kind get_kind () const = 0;
location_t get_locus () const { return locus; }
NodeId get_node_id () const { return node_id; }
virtual void accept_vis (ASTVisitor &vis) = 0;
protected:
// Clone function implementation as pure virtual method
virtual UseTree *clone_use_tree_impl () const = 0;
UseTree (location_t locus)
: locus (locus), node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
};
// Use tree with a glob (wildcard) operator
class UseTreeGlob : public UseTree
{
public:
enum PathType
{
NO_PATH,
GLOBAL,
PATH_PREFIXED