[68f9c43] | 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" |
---|
[2efe4b8] | 22 | #include "Label.h" |
---|
[bd06384] | 23 | #include "Type.h" |
---|
[68f9c43] | 24 | |
---|
| 25 | #include "Common/GC.h" |
---|
| 26 | #include "Common/PassVisitor.h" |
---|
| 27 | |
---|
| 28 | class Expression; |
---|
| 29 | |
---|
| 30 | /// Implements `trace` method for syntax nodes |
---|
| 31 | class GcTracer final : public WithShortCircuiting, public WithVisitorRef<GcTracer> { |
---|
| 32 | const GC& gc; |
---|
| 33 | |
---|
| 34 | public: |
---|
| 35 | GcTracer( const GC& gc ) : gc(gc) {} |
---|
| 36 | |
---|
[34dcc474] | 37 | // mark node and children |
---|
| 38 | |
---|
[68f9c43] | 39 | void previsit( BaseSyntaxNode * node ) { |
---|
| 40 | // skip tree if already seen |
---|
[24de7b1] | 41 | if ( node->mark == gc.mark ) { |
---|
| 42 | visit_children = false; |
---|
| 43 | return; |
---|
| 44 | } |
---|
[68f9c43] | 45 | |
---|
| 46 | // mark node |
---|
| 47 | node->mark = gc.mark; |
---|
| 48 | } |
---|
| 49 | |
---|
[34dcc474] | 50 | // add visits left out by PassVisitor |
---|
| 51 | |
---|
[24de7b1] | 52 | void postvisit( Constant* con ) { |
---|
| 53 | maybeAccept( con->get_type(), *visitor ); |
---|
| 54 | } |
---|
| 55 | |
---|
[2efe4b8] | 56 | void postvisit( AggregateDecl* decl ) { |
---|
| 57 | acceptAll( decl->attributes, *visitor ); |
---|
| 58 | } |
---|
| 59 | |
---|
[5af7306] | 60 | void postvisit( DeclarationWithType* decl ) { |
---|
| 61 | maybeAccept( decl->asmName, *visitor ); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | private: |
---|
| 65 | void visit( InferredParams& inferParams ) { |
---|
| 66 | for ( auto& entry : inferParams ) { |
---|
| 67 | maybeAccept( entry.second.actualType, *visitor ); |
---|
| 68 | maybeAccept( entry.second.formalType, *visitor ); |
---|
| 69 | maybeAccept( entry.second.expr, *visitor ); |
---|
| 70 | visit( *entry.second.inferParams ); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public: |
---|
[68f9c43] | 75 | void postvisit( Expression* expr ) { |
---|
| 76 | maybeAccept( expr->env, *visitor ); |
---|
[5af7306] | 77 | visit( expr->inferParams ); |
---|
[68f9c43] | 78 | } |
---|
[bd06384] | 79 | |
---|
[42107b4] | 80 | void postvisit( OffsetofExpr* expr ) { |
---|
| 81 | postvisit( static_cast<Expression*>(expr) ); |
---|
| 82 | maybeAccept( expr->member, *visitor ); |
---|
| 83 | } |
---|
| 84 | |
---|
[2efe4b8] | 85 | void postvisit( UniqueExpr* expr ) { |
---|
| 86 | postvisit( static_cast<Expression*>(expr) ); |
---|
| 87 | maybeAccept( expr->object, *visitor ); |
---|
| 88 | maybeAccept( expr->var, *visitor ); |
---|
| 89 | } |
---|
| 90 | |
---|
[34dcc474] | 91 | void postvisit( UntypedExpr* expr ) { |
---|
| 92 | postvisit( static_cast<Expression*>(expr) ); |
---|
| 93 | maybeAccept( expr->function, *visitor ); |
---|
| 94 | } |
---|
| 95 | |
---|
[24de7b1] | 96 | void postvisit( VariableExpr* expr ) { |
---|
| 97 | postvisit( static_cast<Expression*>(expr) ); |
---|
| 98 | maybeAccept( expr->var, *visitor ); // not in PassVisitor because it causes cycle |
---|
| 99 | } |
---|
| 100 | |
---|
[2efe4b8] | 101 | private: |
---|
| 102 | void visit( Label& lbl ) { |
---|
| 103 | acceptAll( lbl.get_attributes(), *visitor ); |
---|
[f6f0cca3] | 104 | // maybeAccept( lbl.get_statement(), *visitor ); // introduces infinite loop in tracer |
---|
[2efe4b8] | 105 | } |
---|
| 106 | |
---|
| 107 | public: |
---|
| 108 | void postvisit( Statement* stmt ) { |
---|
| 109 | for ( Label& l : stmt->labels ) { |
---|
| 110 | visit( l ); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
[3ef35bd] | 114 | void postvisit( BranchStmt* stmt ) { |
---|
| 115 | postvisit( static_cast<Statement*>(stmt) ); |
---|
| 116 | visit( stmt->target ); |
---|
| 117 | maybeAccept( stmt->computedTarget, *visitor ); |
---|
| 118 | } |
---|
| 119 | |
---|
[b5aa3d8] | 120 | void postvisit( Type* type ) { |
---|
| 121 | acceptAll( type->attributes, *visitor ); |
---|
| 122 | } |
---|
| 123 | |
---|
[824a2dc] | 124 | void postvisit( EnumInstType* type ) { |
---|
| 125 | postvisit( static_cast<Type*>(type) ); |
---|
| 126 | maybeAccept( type->baseEnum, *visitor ); |
---|
| 127 | } |
---|
| 128 | |
---|
[b5aa3d8] | 129 | void postvisit( PointerType* type ) { |
---|
| 130 | postvisit( static_cast<Type*>(type) ); |
---|
| 131 | maybeAccept( type->dimension, *visitor ); |
---|
[bd06384] | 132 | } |
---|
[824a2dc] | 133 | |
---|
| 134 | void postvisit( StructInstType* type ) { |
---|
| 135 | postvisit( static_cast<Type*>(type) ); |
---|
| 136 | maybeAccept( type->baseStruct, *visitor ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void postvisit( TraitInstType* type ) { |
---|
| 140 | postvisit( static_cast<Type*>(type) ); |
---|
| 141 | maybeAccept( type->baseTrait, *visitor ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void postvisit( TypeInstType* type ) { |
---|
| 145 | postvisit( static_cast<Type*>(type) ); |
---|
| 146 | maybeAccept( type->baseType, *visitor ); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void postvisit( UnionInstType* type ) { |
---|
| 150 | postvisit( static_cast<Type*>(type) ); |
---|
| 151 | maybeAccept( type->baseUnion, *visitor ); |
---|
| 152 | } |
---|
[68f9c43] | 153 | }; |
---|
| 154 | |
---|
| 155 | /// Traces entire translation unit recursively |
---|
| 156 | static inline const GC& operator<< ( const GC& gc, const std::list<Declaration*>& translationUnit ) { |
---|
| 157 | PassVisitor<GcTracer> tracer{ gc }; |
---|
[8d7bef2] | 158 | acceptAll( const_cast<std::list<Declaration*>&>( translationUnit ), tracer ); |
---|
[68f9c43] | 159 | return gc; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // Local Variables: // |
---|
| 163 | // tab-width: 4 // |
---|
| 164 | // mode: c++ // |
---|
| 165 | // compile-command: "make install" // |
---|
| 166 | // End: // |
---|