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 | // GcTracer.h -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Thu Mar 15 14:47:00 2018 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Thu Mar 15 14:47:00 2018 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <list> |
---|
19 | |
---|
20 | #include "BaseSyntaxNode.h" |
---|
21 | #include "Expression.h" |
---|
22 | #include "Type.h" |
---|
23 | |
---|
24 | #include "Common/GC.h" |
---|
25 | #include "Common/PassVisitor.h" |
---|
26 | |
---|
27 | class Expression; |
---|
28 | |
---|
29 | /// Implements `trace` method for syntax nodes |
---|
30 | class GcTracer final : public WithShortCircuiting, public WithVisitorRef<GcTracer> { |
---|
31 | const GC& gc; |
---|
32 | |
---|
33 | public: |
---|
34 | GcTracer( const GC& gc ) : gc(gc) {} |
---|
35 | |
---|
36 | // mark node and children |
---|
37 | |
---|
38 | void previsit( BaseSyntaxNode * node ) { |
---|
39 | // skip tree if already seen |
---|
40 | if ( node->mark == gc.mark ) { |
---|
41 | visit_children = false; |
---|
42 | return; |
---|
43 | } |
---|
44 | |
---|
45 | // mark node |
---|
46 | node->mark = gc.mark; |
---|
47 | } |
---|
48 | |
---|
49 | // add visits left out by PassVisitor |
---|
50 | |
---|
51 | void postvisit( Constant* con ) { |
---|
52 | maybeAccept( con->get_type(), *visitor ); |
---|
53 | } |
---|
54 | |
---|
55 | void postvisit( Expression* expr ) { |
---|
56 | maybeAccept( expr->env, *visitor ); |
---|
57 | } |
---|
58 | |
---|
59 | void postvisit( UntypedExpr* expr ) { |
---|
60 | postvisit( static_cast<Expression*>(expr) ); |
---|
61 | maybeAccept( expr->function, *visitor ); |
---|
62 | } |
---|
63 | |
---|
64 | void postvisit( VariableExpr* expr ) { |
---|
65 | postvisit( static_cast<Expression*>(expr) ); |
---|
66 | maybeAccept( expr->var, *visitor ); // not in PassVisitor because it causes cycle |
---|
67 | } |
---|
68 | |
---|
69 | void postvisit( Type* type ) { |
---|
70 | acceptAll( type->attributes, *visitor ); |
---|
71 | } |
---|
72 | |
---|
73 | void postvisit( PointerType* type ) { |
---|
74 | postvisit( static_cast<Type*>(type) ); |
---|
75 | maybeAccept( type->dimension, *visitor ); |
---|
76 | } |
---|
77 | }; |
---|
78 | |
---|
79 | /// Traces entire translation unit recursively |
---|
80 | static inline const GC& operator<< ( const GC& gc, const std::list<Declaration*>& translationUnit ) { |
---|
81 | PassVisitor<GcTracer> tracer{ gc }; |
---|
82 | acceptAll( const_cast<std::list<Declaration*>&>( translationUnit ), tracer ); |
---|
83 | return gc; |
---|
84 | } |
---|
85 | |
---|
86 | // Local Variables: // |
---|
87 | // tab-width: 4 // |
---|
88 | // mode: c++ // |
---|
89 | // compile-command: "make install" // |
---|
90 | // End: // |
---|