source: src/Common/CodeLocationTools.cpp @ 1784e9e

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 1784e9e was f6e6a55, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Added ast::WaitForClause? and changed ast::WaitForStmt? to use it. This simplified a lot of the internal code.

  • Property mode set to 100644
File size: 8.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// CodeLocationTools.cpp -- Additional tools for code locations.
8//
9// Author           : Andrew Beach
10// Created On       : Fri Dec  4 15:42:00 2020
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Mar 14 15:14:00 2022
13// Update Count     : 4
14//
15
16#include "CodeLocationTools.hpp"
17
18#include <type_traits>
19
20#include "AST/Pass.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "Common/CodeLocation.h"
23
24namespace {
25
26// There are a lot of helpers in this file that could be used much more
27// generally if anyone has another use for them.
28
29// Check if a node type has a code location.
30template<typename node_t>
31struct has_code_location : public std::is_base_of<ast::ParseNode, node_t> {};
32
33template<typename node_t, bool has_location>
34struct __GetCL;
35
36template<typename node_t>
37struct __GetCL<node_t, true> {
38        static inline CodeLocation const * get( node_t const * node ) {
39                return &node->location;
40        }
41
42        static inline CodeLocation * get( node_t * node ) {
43                return &node->location;
44        }
45};
46
47template<typename node_t>
48struct __GetCL<node_t, false> {
49        static inline CodeLocation * get( node_t const * ) {
50                return nullptr;
51        }
52};
53
54template<typename node_t>
55CodeLocation const * get_code_location( node_t const * node ) {
56        return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
57}
58
59template<typename node_t>
60CodeLocation * get_code_location( node_t * node ) {
61        return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
62}
63
64// Fill every location with a nearby (parent) location.
65class FillCore : public ast::WithGuards {
66        CodeLocation const * parent;
67public:
68        FillCore() : parent( nullptr ) {}
69
70        template<typename node_t>
71        node_t const * previsit( node_t const * node ) {
72                GuardValue( parent );
73                CodeLocation const * location = get_code_location( node );
74                if ( location && location->isUnset() ) {
75                        assert( parent );
76                        node_t * newNode = ast::mutate( node );
77                        CodeLocation * newLocation = get_code_location( newNode );
78                        assert( newLocation );
79                        *newLocation = *parent;
80                        parent = newLocation;
81                        return newNode;
82                } else if ( location ) {
83                        parent = location;
84                }
85                return node;
86        }
87};
88
89// ALL_VISITS(macro)
90// Expands `macro(node_type, return_type)` for every visit method of the
91// ast::Visitor class where node_type is the name of the parameter and
92// return_type is the name of the return type; not including the namespace,
93// pointer or const qualifiers.
94#define ALL_VISITS(macro) \
95    macro(ObjectDecl, DeclWithType) \
96    macro(FunctionDecl, DeclWithType) \
97    macro(StructDecl, Decl) \
98    macro(UnionDecl, Decl) \
99    macro(EnumDecl, Decl) \
100    macro(TraitDecl, Decl) \
101    macro(TypeDecl, Decl) \
102    macro(TypedefDecl, Decl) \
103    macro(AsmDecl, AsmDecl) \
104    macro(DirectiveDecl, DirectiveDecl) \
105    macro(StaticAssertDecl, StaticAssertDecl) \
106    macro(CompoundStmt, CompoundStmt) \
107    macro(ExprStmt, Stmt) \
108    macro(AsmStmt, Stmt) \
109    macro(DirectiveStmt, Stmt) \
110    macro(IfStmt, Stmt) \
111    macro(WhileDoStmt, Stmt) \
112    macro(ForStmt, Stmt) \
113    macro(SwitchStmt, Stmt) \
114    macro(CaseClause, CaseClause) \
115    macro(BranchStmt, Stmt) \
116    macro(ReturnStmt, Stmt) \
117    macro(ThrowStmt, Stmt) \
118    macro(TryStmt, Stmt) \
119    macro(CatchClause, CatchClause) \
120    macro(FinallyClause, FinallyClause) \
121    macro(SuspendStmt, Stmt) \
122    macro(WaitForStmt, Stmt) \
123    macro(WaitForClause, WaitForClause) \
124    macro(WithStmt, Decl) \
125    macro(NullStmt, NullStmt) \
126    macro(DeclStmt, Stmt) \
127    macro(ImplicitCtorDtorStmt, Stmt) \
128    macro(MutexStmt, Stmt) \
129    macro(ApplicationExpr, Expr) \
130    macro(UntypedExpr, Expr) \
131    macro(NameExpr, Expr) \
132    macro(AddressExpr, Expr) \
133    macro(LabelAddressExpr, Expr) \
134    macro(CastExpr, Expr) \
135    macro(KeywordCastExpr, Expr) \
136    macro(VirtualCastExpr, Expr) \
137    macro(UntypedMemberExpr, Expr) \
138    macro(MemberExpr, Expr) \
139    macro(VariableExpr, Expr) \
140    macro(ConstantExpr, Expr) \
141    macro(SizeofExpr, Expr) \
142    macro(AlignofExpr, Expr) \
143    macro(UntypedOffsetofExpr, Expr) \
144    macro(OffsetofExpr, Expr) \
145    macro(OffsetPackExpr, Expr) \
146    macro(LogicalExpr, Expr) \
147    macro(ConditionalExpr, Expr) \
148    macro(CommaExpr, Expr) \
149    macro(TypeExpr, Expr) \
150    macro(DimensionExpr, Expr) \
151    macro(AsmExpr, Expr) \
152    macro(ImplicitCopyCtorExpr, Expr) \
153    macro(ConstructorExpr, Expr) \
154    macro(CompoundLiteralExpr, Expr) \
155    macro(RangeExpr, Expr) \
156    macro(UntypedTupleExpr, Expr) \
157    macro(TupleExpr, Expr) \
158    macro(TupleIndexExpr, Expr) \
159    macro(TupleAssignExpr, Expr) \
160    macro(StmtExpr, Expr) \
161    macro(UniqueExpr, Expr) \
162    macro(UntypedInitExpr, Expr) \
163    macro(InitExpr, Expr) \
164    macro(DeletedExpr, Expr) \
165    macro(DefaultArgExpr, Expr) \
166    macro(GenericExpr, Expr) \
167    macro(VoidType, Type) \
168    macro(BasicType, Type) \
169    macro(PointerType, Type) \
170    macro(ArrayType, Type) \
171    macro(ReferenceType, Type) \
172    macro(QualifiedType, Type) \
173    macro(FunctionType, Type) \
174    macro(StructInstType, Type) \
175    macro(UnionInstType, Type) \
176    macro(EnumInstType, Type) \
177    macro(TraitInstType, Type) \
178    macro(TypeInstType, Type) \
179    macro(TupleType, Type) \
180    macro(TypeofType, Type) \
181    macro(VTableType, Type) \
182    macro(VarArgsType, Type) \
183    macro(ZeroType, Type) \
184    macro(OneType, Type) \
185    macro(GlobalScopeType, Type) \
186    macro(Designation, Designation) \
187    macro(SingleInit, Init) \
188    macro(ListInit, Init) \
189    macro(ConstructorInit, Init) \
190    macro(Attribute, Attribute) \
191    macro(TypeSubstitution, TypeSubstitution)
192
193// These could even go into the ast namespace.
194enum class LeafKind {
195#define VISIT(node_type, return_type) node_type,
196        ALL_VISITS(VISIT)
197#undef VISIT
198};
199
200struct LeafKindVisitor : public ast::Visitor {
201        LeafKind kind;
202
203#define VISIT(node_type, return_type) \
204        const ast::return_type * visit( const ast::node_type * ) final { \
205                kind = LeafKind::node_type; \
206                return nullptr; \
207        }
208        ALL_VISITS(VISIT)
209#undef VISIT
210};
211
212constexpr size_t leaf_kind_count = (1 + (size_t)LeafKind::TypeSubstitution);
213
214LeafKind get_leaf_kind( ast::Node const * node ) {
215        LeafKindVisitor visitor;
216        node->accept( visitor );
217        return visitor.kind;
218}
219
220const char * leaf_kind_names[leaf_kind_count] = {
221#define VISIT(node_type, return_type) #node_type,
222        ALL_VISITS(VISIT)
223#undef VISIT
224};
225
226// Collect pointers to all the nodes with unset code locations.
227class CollectCore {
228        std::list< ast::ptr< ast::Node > > & unset;
229public:
230        CollectCore( std::list< ast::ptr< ast::Node > > & unset ) :
231                unset( unset )
232        {}
233
234        template<typename node_t>
235        void previsit( node_t const * node ) {
236                CodeLocation const * location = get_code_location( node );
237                if ( location && location->isUnset() ) {
238                        unset.push_back( node );
239                }
240        }
241};
242
243class LocalFillCore : public ast::WithGuards {
244        CodeLocation const * parent;
245public:
246        LocalFillCore( CodeLocation const & location ) : parent( &location ) {
247                assert( location.isSet() );
248        }
249
250        template<typename node_t>
251        auto previsit( node_t const * node )
252                        -> typename std::enable_if<has_code_location<node_t>::value, node_t const *>::type {
253                if ( node->location.isSet() ) {
254                        GuardValue( parent ) = &node->location;
255                        return node;
256                } else {
257                        node_t * mut = ast::mutate( node );
258                        mut->location = *parent;
259                        return mut;
260                }
261        }
262};
263
264} // namespace
265
266void checkAllCodeLocations( ast::TranslationUnit const & unit ) {
267        checkAllCodeLocations( "unknown location", unit );
268}
269
270void checkAllCodeLocations( char const * label, ast::TranslationUnit const & unit ) {
271        std::list< ast::ptr< ast::Node > > unsetNodes;
272        {
273                ast::Pass<CollectCore> collector( unsetNodes );
274                for ( auto node : unit.decls ) {
275                        node->accept( collector );
276                }
277        }
278        if ( unsetNodes.empty() ) {
279                return;
280        }
281
282        std::cerr << "Code Location check at " << label << " failed." << std::endl;
283        std::cerr << "Total nodes without a set code location: "
284                << unsetNodes.size() << std::endl;
285
286        size_t node_counts[leaf_kind_count] = {0};
287        for ( auto unset : unsetNodes ) {
288                node_counts[(size_t)get_leaf_kind(unset)] += 1;
289        }
290        for ( size_t i = 0 ; i < leaf_kind_count ; ++i ) {
291                if ( node_counts[i] ) {
292                        std::cerr << '\t' << node_counts[i]
293                                << " of type " << leaf_kind_names[i] << std::endl;
294                }
295        }
296
297        assert( unsetNodes.empty() );
298}
299
300void forceFillCodeLocations( ast::TranslationUnit & unit ) {
301        ast::Pass<FillCore>::run( unit );
302}
303
304ast::Node const * localFillCodeLocations(
305                CodeLocation const & location , ast::Node const * node ) {
306        ast::Pass<LocalFillCore> visitor( location );
307        return node->accept( visitor );
308}
Note: See TracBrowser for help on using the repository browser.