forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.h
2700 lines (2319 loc) · 87.9 KB
/
AST.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
/*
This file is part of solidity.
solidity 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 of the License, or
(at your option) any later version.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @author Christian <[email protected]>
* @date 2014
* Solidity abstract syntax tree.
*/
#pragma once
#include <libsolidity/ast/ASTForward.h>
#include <libsolidity/ast/Types.h>
#include <libsolidity/ast/ASTAnnotations.h>
#include <libsolidity/ast/ASTEnums.h>
#include <libsolidity/parsing/Token.h>
#include <liblangutil/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/FixedHash.h>
#include <libsolutil/LazyInit.h>
#include <libsolutil/Visitor.h>
#include <libsolutil/JSON.h>
#include <range/v3/view/subrange.hpp>
#include <range/v3/view/map.hpp>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
namespace solidity::yul
{
// Forward-declaration to <yul/AST.h>
class AST;
class Dialect;
}
namespace solidity::frontend
{
class ASTVisitor;
class ASTConstVisitor;
/**
* The root (abstract) class of the AST inheritance tree.
* It is possible to traverse all direct and indirect children of an AST node by calling
* accept, providing an ASTVisitor.
*/
class ASTNode
{
public:
/// Noncopyable.
ASTNode(ASTNode const&) = delete;
ASTNode& operator=(ASTNode const&) = delete;
using CompareByID = frontend::ASTCompareByID<ASTNode>;
using SourceLocation = langutil::SourceLocation;
explicit ASTNode(int64_t _id, SourceLocation _location);
virtual ~ASTNode() {}
/// @returns an identifier of this AST node that is unique for a single compilation run.
int64_t id() const { return int64_t(m_id); }
virtual void accept(ASTVisitor& _visitor) = 0;
virtual void accept(ASTConstVisitor& _visitor) const = 0;
template <class T>
static void listAccept(std::vector<T> const& _list, ASTVisitor& _visitor)
{
for (T const& element: _list)
{
solAssert(element);
element->accept(_visitor);
}
}
template <class T>
static void listAccept(std::vector<T> const& _list, ASTConstVisitor& _visitor)
{
for (T const& element: _list)
{
solAssert(element);
element->accept(_visitor);
}
}
/// @returns a copy of the vector containing only the nodes which derive from T.
template <class T>
static std::vector<T const*> filteredNodes(std::vector<ASTPointer<ASTNode>> const& _nodes);
/// Extracts the referenced declaration from all nodes whose annotations support
/// `referencedDeclaration`.
static Declaration const* referencedDeclaration(Expression const& _expression);
/// Performs potential super or virtual lookup for a function call based on the most derived contract.
static FunctionDefinition const* resolveFunctionCall(FunctionCall const& _functionCall, ContractDefinition const* _mostDerivedContract);
/// Returns the source code location of this node.
SourceLocation const& location() const { return m_location; }
///@todo make this const-safe by providing a different way to access the annotation
virtual ASTAnnotation& annotation() const;
///@{
///@name equality operators
/// Equality relies on the fact that nodes cannot be copied.
bool operator==(ASTNode const& _other) const { return this == &_other; }
bool operator!=(ASTNode const& _other) const { return !operator==(_other); }
///@}
virtual bool experimentalSolidityOnly() const { return false; }
protected:
size_t const m_id = 0;
template <class T>
T& initAnnotation() const
{
if (!m_annotation)
m_annotation = std::make_unique<T>();
return dynamic_cast<T&>(*m_annotation);
}
private:
/// Annotation - is specialised in derived classes, is created upon request (because of polymorphism).
mutable std::unique_ptr<ASTAnnotation> m_annotation;
SourceLocation m_location;
};
template <class T>
std::vector<T const*> ASTNode::filteredNodes(std::vector<ASTPointer<ASTNode>> const& _nodes)
{
std::vector<T const*> ret;
for (auto const& n: _nodes)
if (auto const* nt = dynamic_cast<T const*>(n.get()))
ret.push_back(nt);
return ret;
}
/**
* Abstract marker class that specifies that this AST node opens a scope.
*/
class ScopeOpener
{
public:
virtual ~ScopeOpener() = default;
};
/**
* Source unit containing import directives and contract definitions.
*/
class SourceUnit: public ASTNode, public ScopeOpener
{
public:
SourceUnit(
int64_t _id,
SourceLocation const& _location,
std::optional<std::string> _licenseString,
std::vector<ASTPointer<ASTNode>> _nodes,
bool _experimentalSolidity
):
ASTNode(_id, _location),
m_licenseString(std::move(_licenseString)),
m_nodes(std::move(_nodes)),
m_experimentalSolidity(_experimentalSolidity)
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
SourceUnitAnnotation& annotation() const override;
std::optional<std::string> const& licenseString() const { return m_licenseString; }
std::vector<ASTPointer<ASTNode>> nodes() const { return m_nodes; }
/// @returns a set of referenced SourceUnits. Recursively if @a _recurse is true.
std::set<SourceUnit const*> referencedSourceUnits(bool _recurse = false, std::set<SourceUnit const*> _skipList = std::set<SourceUnit const*>()) const;
bool experimentalSolidity() const { return m_experimentalSolidity; }
private:
void referencedSourceUnits(
std::set<SourceUnit const*>& _referencedSourceUnits,
bool _recurse,
std::set<SourceUnit const*>& _skipList
) const;
std::optional<std::string> m_licenseString;
std::vector<ASTPointer<ASTNode>> m_nodes;
bool m_experimentalSolidity = false;
};
/**
* Abstract class that is added to each AST node that is stored inside a scope
* (including scopes).
*/
class Scopable
{
public:
virtual ~Scopable() = default;
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
/// Available only after name and type resolution step.
ASTNode const* scope() const { return annotation().scope; }
/// @returns the source unit this scopable is present in.
SourceUnit const& sourceUnit() const;
/// @returns the function or modifier definition this scopable is present in or nullptr.
CallableDeclaration const* functionOrModifierDefinition() const;
/// @returns the source name this scopable is present in.
/// Can be combined with annotation().canonicalName (if present) to form a globally unique name.
std::string sourceUnitName() const;
virtual ScopableAnnotation& annotation() const = 0;
};
/**
* Abstract AST class for a declaration (contract, function, struct, variable, import directive).
*/
class Declaration: public ASTNode, public Scopable
{
public:
static std::string visibilityToString(Visibility _visibility)
{
switch (_visibility)
{
case Visibility::Public:
return "public";
case Visibility::Internal:
return "internal";
case Visibility::Private:
return "private";
case Visibility::External:
return "external";
default:
solAssert(false, "Invalid visibility specifier.");
}
return std::string();
}
Declaration(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> _name,
SourceLocation _nameLocation,
Visibility _visibility = Visibility::Default
):
ASTNode(_id, _location), m_name(std::move(_name)), m_nameLocation(std::move(_nameLocation)), m_visibility(_visibility) {}
/// @returns the declared name.
ASTString const& name() const { return *m_name; }
/// @returns the location of the declared name itself or empty location if not available or unknown.
SourceLocation const& nameLocation() const noexcept { return m_nameLocation; }
bool noVisibilitySpecified() const { return m_visibility == Visibility::Default; }
Visibility visibility() const { return m_visibility == Visibility::Default ? defaultVisibility() : m_visibility; }
bool isPublic() const { return visibility() >= Visibility::Public; }
virtual bool isVisibleInContract() const { return visibility() != Visibility::External; }
virtual bool isVisibleInDerivedContracts() const { return isVisibleInContract() && visibility() >= Visibility::Internal; }
bool isVisibleAsLibraryMember() const { return visibility() >= Visibility::Internal; }
virtual bool isVisibleViaContractTypeAccess() const { return false; }
virtual bool isLValue() const { return false; }
virtual bool isPartOfExternalInterface() const { return false; }
/// @returns true if this is a declaration of an enum member.
bool isEnumValue() const;
/// @returns true if this is a declaration of a struct member.
bool isStructMember() const;
/// @returns true if this is a declaration of a parameter of an event.
bool isEventOrErrorParameter() const;
/// @returns false if the declaration can never be referenced without being qualified with a scope.
/// Usually the name alone can be used to refer to the corresponding entity.
/// But, for example, struct member names or enum member names always require a prefix.
/// Another example is event parameter names, which do not participate in any proper scope.
bool isVisibleAsUnqualifiedName() const;
/// @returns the type of expressions referencing this declaration.
/// This can only be called once types of variable declarations have already been resolved.
virtual Type const* type() const = 0;
/// @returns the type for members of the containing contract type that refer to this declaration.
/// This can only be called once types of variable declarations have already been resolved.
virtual Type const* typeViaContractName() const { return type(); }
/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.
/// @returns null when it is not accessible as a function.
virtual FunctionTypePointer functionType(bool /*_internal*/) const { return {}; }
DeclarationAnnotation& annotation() const override;
protected:
virtual Visibility defaultVisibility() const { return Visibility::Public; }
private:
ASTPointer<ASTString> m_name;
SourceLocation m_nameLocation;
Visibility m_visibility;
};
/**
* Pragma directive, only version requirements in the form `pragma solidity "^0.4.0";` are
* supported for now.
*/
class PragmaDirective: public ASTNode
{
public:
PragmaDirective(
int64_t _id,
SourceLocation const& _location,
std::vector<Token> _tokens,
std::vector<ASTString> _literals
): ASTNode(_id, _location), m_tokens(std::move(_tokens)), m_literals(std::move(_literals))
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
std::vector<Token> const& tokens() const { return m_tokens; }
std::vector<ASTString> const& literals() const { return m_literals; }
private:
/// Sequence of tokens following the "pragma" keyword.
std::vector<Token> m_tokens;
/// Sequence of literals following the "pragma" keyword.
std::vector<ASTString> m_literals;
};
/**
* Import directive for referencing other files / source objects.
* Example: import "abc.sol" // imports all symbols of "abc.sol" into current scope
* Source objects are identified by a string which can be a file name but does not have to be.
* Other ways to use it:
* import "abc" as x; // creates symbol "x" that contains all symbols in "abc"
* import * as x from "abc"; // same as above
* import {a as b, c} from "abc"; // creates new symbols "b" and "c" referencing "a" and "c" in "abc", respectively.
*/
class ImportDirective: public Declaration
{
public:
struct SymbolAlias
{
ASTPointer<Identifier> symbol;
ASTPointer<ASTString> alias;
SourceLocation location;
};
using SymbolAliasList = std::vector<SymbolAlias>;
ImportDirective(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> _path,
ASTPointer<ASTString> const& _unitAlias,
SourceLocation _unitAliasLocation,
SymbolAliasList _symbolAliases
):
Declaration(_id, _location, _unitAlias, std::move(_unitAliasLocation)),
m_path(std::move(_path)),
m_symbolAliases(std::move(_symbolAliases))
{ }
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
ASTString const& path() const { return *m_path; }
SymbolAliasList const& symbolAliases() const
{
return m_symbolAliases;
}
ImportAnnotation& annotation() const override;
Type const* type() const override;
private:
ASTPointer<ASTString> m_path;
/// The aliases for the specific symbols to import. If non-empty import the specific symbols.
/// If the `alias` component is empty, import the identifier unchanged.
/// If both m_unitAlias and m_symbolAlias are empty, import all symbols into the current scope.
SymbolAliasList m_symbolAliases;
};
/**
* Abstract class that is added to each AST node that can store local variables.
* Local variables in functions are always added to functions, even though they are not
* in scope for the whole function.
*/
class VariableScope
{
public:
virtual ~VariableScope() = default;
void addLocalVariable(VariableDeclaration const& _localVariable) { m_localVariables.push_back(&_localVariable); }
std::vector<VariableDeclaration const*> const& localVariables() const { return m_localVariables; }
private:
std::vector<VariableDeclaration const*> m_localVariables;
};
/**
* The doxygen-style, structured documentation class that represents an AST node.
*/
class StructuredDocumentation: public ASTNode
{
public:
StructuredDocumentation(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> _text
): ASTNode(_id, _location), m_text(std::move(_text))
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
/// @return A shared pointer of an ASTString.
/// Contains doxygen-style, structured documentation that is parsed later on.
ASTPointer<ASTString> const& text() const { return m_text; }
private:
ASTPointer<ASTString> m_text;
};
/**
* Abstract class that is added to each AST node that can receive documentation.
*/
class Documented
{
public:
virtual ~Documented() = default;
explicit Documented(ASTPointer<ASTString> _documentation): m_documentation(std::move(_documentation)) {}
/// @return A shared pointer of an ASTString.
/// Can contain a nullptr in which case indicates absence of documentation
ASTPointer<ASTString> const& documentation() const { return m_documentation; }
protected:
ASTPointer<ASTString> m_documentation;
};
/**
* Abstract class that is added to each AST node that can receive a structured documentation.
*/
class StructurallyDocumented
{
public:
virtual ~StructurallyDocumented() = default;
explicit StructurallyDocumented(ASTPointer<StructuredDocumentation> _documentation): m_documentation(std::move(_documentation)) {}
/// @return A shared pointer of a FormalDocumentation.
/// Can contain a nullptr in which case indicates absence of documentation
ASTPointer<StructuredDocumentation> const& documentation() const { return m_documentation; }
protected:
ASTPointer<StructuredDocumentation> m_documentation;
};
/**
* Abstract class that is added to AST nodes that can be marked as not being fully implemented
*/
class ImplementationOptional
{
public:
virtual ~ImplementationOptional() = default;
explicit ImplementationOptional(bool _implemented): m_implemented(_implemented) {}
/// @return whether this node is fully implemented or not
bool isImplemented() const { return m_implemented; }
protected:
bool m_implemented;
};
/// @}
/**
* Definition of a contract or library. This is the only AST nodes where child nodes are not visited in
* document order. It first visits all struct declarations, then all variable declarations and
* finally all function declarations.
*/
class ContractDefinition: public Declaration, public StructurallyDocumented, public ScopeOpener
{
public:
ContractDefinition(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
SourceLocation _nameLocation,
ASTPointer<StructuredDocumentation> const& _documentation,
std::vector<ASTPointer<InheritanceSpecifier>> _baseContracts,
std::vector<ASTPointer<ASTNode>> _subNodes,
ContractKind _contractKind = ContractKind::Contract,
bool _abstract = false,
ASTPointer<StorageLayoutSpecifier> _storageLayoutSpecifier = nullptr
):
Declaration(_id, _location, _name, std::move(_nameLocation)),
StructurallyDocumented(_documentation),
m_baseContracts(std::move(_baseContracts)),
m_subNodes(std::move(_subNodes)),
m_contractKind(_contractKind),
m_abstract(_abstract),
m_storageLayoutSpecifier(_storageLayoutSpecifier)
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
std::vector<ASTPointer<InheritanceSpecifier>> const& baseContracts() const { return m_baseContracts; }
std::vector<ASTPointer<ASTNode>> const& subNodes() const { return m_subNodes; }
std::vector<UsingForDirective const*> usingForDirectives() const { return filteredNodes<UsingForDirective>(m_subNodes); }
std::vector<StructDefinition const*> definedStructs() const { return filteredNodes<StructDefinition>(m_subNodes); }
std::vector<EnumDefinition const*> definedEnums() const { return filteredNodes<EnumDefinition>(m_subNodes); }
std::vector<VariableDeclaration const*> stateVariables() const { return filteredNodes<VariableDeclaration>(m_subNodes); }
std::vector<ModifierDefinition const*> functionModifiers() const { return filteredNodes<ModifierDefinition>(m_subNodes); }
std::vector<FunctionDefinition const*> definedFunctions() const { return filteredNodes<FunctionDefinition>(m_subNodes); }
/// @returns a view<FunctionDefinition const*> of all functions
/// defined in this contract of the given name (excluding inherited functions).
auto definedFunctions(std::string const& _name) const
{
auto&& [b, e] = definedFunctionsByName().equal_range(_name);
return ranges::subrange<decltype(b)>(b, e) | ranges::views::values;
}
std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); }
std::vector<EventDefinition const*> const& definedInterfaceEvents() const;
std::vector<EventDefinition const*> const usedInterfaceEvents() const;
/// @return all events defined in this contract and its base contracts and all events
/// that are emitted during the execution of the contract.
/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.
std::vector<EventDefinition const*> interfaceEvents(bool _requireCallGraph = true) const;
/// @returns all errors defined in this contract or any base contract
/// and all errors referenced during execution.
/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.
std::vector<ErrorDefinition const*> interfaceErrors(bool _requireCallGraph = true) const;
bool isInterface() const { return m_contractKind == ContractKind::Interface; }
bool isLibrary() const { return m_contractKind == ContractKind::Library; }
/// @returns true, if the contract derives from @arg _base.
bool derivesFrom(ContractDefinition const& _base) const;
/// @returns a map of canonical function signatures to FunctionDefinitions
/// as intended for use by the ABI.
std::map<util::FixedHash<4>, FunctionTypePointer> interfaceFunctions(bool _includeInheritedFunctions = true) const;
std::vector<std::pair<util::FixedHash<4>, FunctionTypePointer>> const& interfaceFunctionList(bool _includeInheritedFunctions = true) const;
/// @returns the EIP-165 compatible interface identifier. This will exclude inherited functions.
uint32_t interfaceId() const;
/// @returns a list of all declarations in this contract
std::vector<Declaration const*> declarations() const { return filteredNodes<Declaration>(m_subNodes); }
/// Returns the constructor or nullptr if no constructor was specified.
FunctionDefinition const* constructor() const;
/// @returns true iff the contract can be deployed, i.e. is not abstract and has a
/// public constructor.
/// Should only be called after the type checker has run.
bool canBeDeployed() const;
/// Returns the fallback function or nullptr if no fallback function was specified.
FunctionDefinition const* fallbackFunction() const;
/// Returns the ether receiver function or nullptr if no receive function was specified.
FunctionDefinition const* receiveFunction() const;
std::string fullyQualifiedName() const { return sourceUnitName() + ":" + name(); }
Type const* type() const override;
ContractDefinitionAnnotation& annotation() const override;
ContractKind contractKind() const { return m_contractKind; }
bool abstract() const { return m_abstract; }
StorageLayoutSpecifier const* storageLayoutSpecifier() const { return m_storageLayoutSpecifier.get(); }
StorageLayoutSpecifier* storageLayoutSpecifier() { return m_storageLayoutSpecifier.get(); }
ContractDefinition const* superContract(ContractDefinition const& _mostDerivedContract) const;
/// @returns the next constructor in the inheritance hierarchy.
FunctionDefinition const* nextConstructor(ContractDefinition const& _mostDerivedContract) const;
private:
std::multimap<std::string, FunctionDefinition const*> const& definedFunctionsByName() const;
std::vector<ASTPointer<InheritanceSpecifier>> m_baseContracts;
std::vector<ASTPointer<ASTNode>> m_subNodes;
ContractKind m_contractKind;
bool m_abstract{false};
ASTPointer<StorageLayoutSpecifier> m_storageLayoutSpecifier;
util::LazyInit<std::vector<std::pair<util::FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList[2];
util::LazyInit<std::vector<EventDefinition const*>> m_interfaceEvents;
util::LazyInit<std::multimap<std::string, FunctionDefinition const*>> m_definedFunctionsByName;
};
class StorageLayoutSpecifier : public ASTNode
{
public:
StorageLayoutSpecifier(
int64_t _id,
SourceLocation const& _location,
ASTPointer<Expression> _baseSlotExpression
);
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
Expression const& baseSlotExpression() const { solAssert(m_baseSlotExpression); return *m_baseSlotExpression; }
StorageLayoutSpecifierAnnotation& annotation() const override;
private:
ASTPointer<Expression> m_baseSlotExpression;
};
/**
* A sequence of identifiers separated by dots used outside the expression context. Inside the expression context, this is a sequence of Identifier and MemberAccess.
*/
class IdentifierPath: public ASTNode
{
public:
IdentifierPath(
int64_t _id,
SourceLocation const& _location,
std::vector<ASTString> _path,
std::vector<SourceLocation> _pathLocations
):
ASTNode(_id, _location), m_path(std::move(_path)), m_pathLocations(std::move(_pathLocations))
{
solAssert(m_pathLocations.size() == m_path.size());
}
std::vector<ASTString> const& path() const { return m_path; }
std::vector<SourceLocation > const& pathLocations() const { return m_pathLocations; }
IdentifierPathAnnotation& annotation() const override
{
return initAnnotation<IdentifierPathAnnotation>();
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
private:
std::vector<ASTString> m_path;
// Corresponding locations for m_path. Array has same length and indices as m_path.
std::vector<SourceLocation> m_pathLocations;
};
class InheritanceSpecifier: public ASTNode
{
public:
InheritanceSpecifier(
int64_t _id,
SourceLocation const& _location,
ASTPointer<IdentifierPath> _baseName,
std::unique_ptr<std::vector<ASTPointer<Expression>>> _arguments
):
ASTNode(_id, _location), m_baseName(std::move(_baseName)), m_arguments(std::move(_arguments))
{
solAssert(m_baseName != nullptr, "Name cannot be null.");
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
IdentifierPath const& name() const { return *m_baseName; }
// Returns nullptr if no argument list was given (``C``).
// If an argument list is given (``C(...)``), the arguments are returned
// as a vector of expressions. Note that this vector can be empty (``C()``).
std::vector<ASTPointer<Expression>> const* arguments() const { return m_arguments.get(); }
private:
ASTPointer<IdentifierPath> m_baseName;
std::unique_ptr<std::vector<ASTPointer<Expression>>> m_arguments;
};
/**
* Using for directive:
*
* 1. `using LibraryName for T` attaches all functions from the library `LibraryName` to the type `T`.
* 2. `using LibraryName for *` attaches to all types.
* 3. `using {f1, f2, ..., fn} for T` attaches the functions `f1`, `f2`, ..., `fn`, respectively to `T`.
* 4. `using {f1 as op1, f2 as op2, ..., fn as opn} for T` implements operator `opn` for type `T` with function `fn`.
*
* For version 3, T has to be implicitly convertible to the first parameter type of
* all functions, and this is checked at the point of the using statement. For versions 1 and
* 2, this check is only done when a function is called.
*
* For version 4, T has to be user-defined value type and the function must be pure.
* All parameters and return value of all the functions have to be of type T.
* This version can be combined with version 3 - a single directive may attach functions to the
* type and define operators on it at the same time.
*
* Finally, `using {f1, f2, ..., fn} for T global` is also valid at file level, as long as T is
* a user-defined type defined in the same file at file level. In this case, the methods are
* attached to all objects of that type regardless of scope.
*/
class UsingForDirective: public ASTNode
{
public:
UsingForDirective(
int64_t _id,
SourceLocation const& _location,
std::vector<ASTPointer<IdentifierPath>> _functionsOrLibrary,
std::vector<std::optional<Token>> _operators,
bool _usesBraces,
ASTPointer<TypeName> _typeName,
bool _global
):
ASTNode(_id, _location),
m_functionsOrLibrary(std::move(_functionsOrLibrary)),
m_operators(std::move(_operators)),
m_usesBraces(_usesBraces),
m_typeName(std::move(_typeName)),
m_global{_global}
{
solAssert(m_functionsOrLibrary.size() == m_operators.size());
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
/// @returns the type name the library is attached to, null for `*`.
TypeName const* typeName() const { return m_typeName.get(); }
/// @returns a list of functions or the single library.
std::vector<ASTPointer<IdentifierPath>> const& functionsOrLibrary() const { return m_functionsOrLibrary; }
std::vector<std::pair<ASTPointer<IdentifierPath>, std::optional<Token>>> functionsAndOperators() const;
bool usesBraces() const { return m_usesBraces; }
bool global() const { return m_global; }
private:
/// Either the single library or a list of functions.
std::vector<ASTPointer<IdentifierPath>> m_functionsOrLibrary;
/// Operators, the functions from @a m_functionsOrLibrary implement.
/// A token if the corresponding element in m_functionsOrLibrary
/// defines an operator, nullptr otherwise.
/// Note that this vector size must be equal to m_functionsOrLibrary size.
std::vector<std::optional<Token>> m_operators;
bool m_usesBraces;
ASTPointer<TypeName> m_typeName;
bool m_global = false;
};
class StructDefinition: public Declaration, public StructurallyDocumented, public ScopeOpener
{
public:
StructDefinition(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
SourceLocation _nameLocation,
std::vector<ASTPointer<VariableDeclaration>> _members,
ASTPointer<StructuredDocumentation> _documentation
):
Declaration(_id, _location, _name, std::move(_nameLocation)),
StructurallyDocumented(std::move(_documentation)),
m_members(std::move(_members))
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
std::vector<ASTPointer<VariableDeclaration>> const& members() const { return m_members; }
Type const* type() const override;
bool isVisibleInDerivedContracts() const override { return true; }
bool isVisibleViaContractTypeAccess() const override { return true; }
StructDeclarationAnnotation& annotation() const override;
private:
std::vector<ASTPointer<VariableDeclaration>> m_members;
};
class EnumDefinition: public Declaration, public StructurallyDocumented, public ScopeOpener
{
public:
EnumDefinition(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
SourceLocation _nameLocation,
std::vector<ASTPointer<EnumValue>> _members,
ASTPointer<StructuredDocumentation> _documentation
):
Declaration(_id, _location, _name, std::move(_nameLocation)),
StructurallyDocumented(std::move(_documentation)),
m_members(std::move(_members))
{}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
bool isVisibleInDerivedContracts() const override { return true; }
bool isVisibleViaContractTypeAccess() const override { return true; }
std::vector<ASTPointer<EnumValue>> const& members() const { return m_members; }
Type const* type() const override;
TypeDeclarationAnnotation& annotation() const override;
private:
std::vector<ASTPointer<EnumValue>> m_members;
};
/**
* Declaration of an Enum Value
*/
class EnumValue: public Declaration, public StructurallyDocumented
{
public:
EnumValue(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
ASTPointer<StructuredDocumentation> _documentation
):
Declaration(_id, _location, _name, _location),
StructurallyDocumented(std::move(_documentation))
{
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
Type const* type() const override;
};
/**
* User defined value types, i.e., custom types, for example, `type MyInt is int`. Allows creating a
* zero cost abstraction over value type with stricter type requirements.
*/
class UserDefinedValueTypeDefinition: public Declaration
{
public:
UserDefinedValueTypeDefinition(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> _name,
SourceLocation _nameLocation,
ASTPointer<TypeName> _underlyingType
):
Declaration(_id, _location, _name, std::move(_nameLocation), Visibility::Default),
m_underlyingType(std::move(_underlyingType))
{
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
Type const* type() const override;
TypeDeclarationAnnotation& annotation() const override;
TypeName const* underlyingType() const { return m_underlyingType.get(); }
bool isVisibleViaContractTypeAccess() const override { return true; }
private:
/// The name of the underlying type
ASTPointer<TypeName> m_underlyingType;
};
/**
* Parameter list, used as function parameter list, return list and for try and catch.
* None of the parameters is allowed to contain mappings (not even recursively
* inside structs).
*/
class ParameterList: public ASTNode
{
public:
ParameterList(
int64_t _id,
SourceLocation const& _location,
std::vector<ASTPointer<VariableDeclaration>> _parameters
):
ASTNode(_id, _location), m_parameters(std::move(_parameters)) {}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
std::vector<ASTPointer<VariableDeclaration>> const& parameters() const { return m_parameters; }
private:
std::vector<ASTPointer<VariableDeclaration>> m_parameters;
};
/**
* Base class for all nodes that define function-like objects, i.e. FunctionDefinition,
* EventDefinition, ErrorDefinition and ModifierDefinition.
*/
class CallableDeclaration: public Declaration, public VariableScope
{
public:
CallableDeclaration(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
SourceLocation _nameLocation,
Visibility _visibility,
ASTPointer<ParameterList> _parameters,
bool _isVirtual = false,
ASTPointer<OverrideSpecifier> _overrides = nullptr,
ASTPointer<ParameterList> _returnParameters = ASTPointer<ParameterList>()
):
Declaration(_id, _location, _name, std::move(_nameLocation), _visibility),
m_parameters(std::move(_parameters)),
m_overrides(std::move(_overrides)),
m_returnParameters(std::move(_returnParameters)),
m_isVirtual(_isVirtual)
{
}
std::vector<ASTPointer<VariableDeclaration>> const& parameters() const { return m_parameters->parameters(); }
ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; }
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
ParameterList const& parameterList() const { return *m_parameters; }
ASTPointer<ParameterList> const& returnParameterList() const { return m_returnParameters; }
bool markedVirtual() const { return m_isVirtual; }
virtual bool virtualSemantics() const { return markedVirtual(); }
CallableDeclarationAnnotation& annotation() const override = 0;
/// Performs virtual or super function/modifier lookup:
/// If @a _searchStart is nullptr, performs virtual function lookup, i.e.
/// searches the inheritance hierarchy of @a _mostDerivedContract towards the base
/// and returns the first function/modifier definition that
/// is overwritten by this callable.
/// If @a _searchStart is non-null, starts searching only from that contract, but
/// still in the hierarchy of @a _mostDerivedContract.
virtual CallableDeclaration const& resolveVirtual(
ContractDefinition const& _mostDerivedContract,
ContractDefinition const* _searchStart = nullptr
) const = 0;
protected:
ASTPointer<ParameterList> m_parameters;
ASTPointer<OverrideSpecifier> m_overrides;
ASTPointer<ParameterList> m_returnParameters;
bool m_isVirtual = false;
};
/**
* Function override specifier. Consists of a single override keyword
* potentially followed by a parenthesized list of base contract names.
*/
class OverrideSpecifier: public ASTNode
{
public:
OverrideSpecifier(
int64_t _id,
SourceLocation const& _location,
std::vector<ASTPointer<IdentifierPath>> _overrides
):
ASTNode(_id, _location),
m_overrides(std::move(_overrides))
{
}
void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
/// @returns the list of specific overrides, if any
std::vector<ASTPointer<IdentifierPath>> const& overrides() const { return m_overrides; }
protected:
std::vector<ASTPointer<IdentifierPath>> m_overrides;
};
class FunctionDefinition: public CallableDeclaration, public StructurallyDocumented, public ImplementationOptional, public ScopeOpener
{
public:
FunctionDefinition(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
SourceLocation const& _nameLocation,
Visibility _visibility,
StateMutability _stateMutability,
bool _free,
Token _kind,
bool _isVirtual,
ASTPointer<OverrideSpecifier> const& _overrides,
ASTPointer<StructuredDocumentation> const& _documentation,
ASTPointer<ParameterList> const& _parameters,