| 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 | // GenericSubstitution.cpp -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Aaron B. Moss | 
|---|
| 10 | // Created On       : Wed May 22 14:15:00 2019 | 
|---|
| 11 | // Last Modified By : Aaron B. Moss | 
|---|
| 12 | // Created On       : Wed May 22 14:15:00 2019 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "GenericSubstitution.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include <cassert> | 
|---|
| 19 | #include <utility>               // for move | 
|---|
| 20 |  | 
|---|
| 21 | #include "Decl.hpp" | 
|---|
| 22 | #include "Node.hpp"              // for maybe_accept | 
|---|
| 23 | #include "Pass.hpp" | 
|---|
| 24 | #include "Type.hpp" | 
|---|
| 25 | #include "TypeSubstitution.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | namespace ast { | 
|---|
| 28 |  | 
|---|
| 29 | namespace { | 
|---|
| 30 | struct GenericSubstitutionBuilder : public WithShortCircuiting { | 
|---|
| 31 | TypeSubstitution sub; | 
|---|
| 32 |  | 
|---|
| 33 | void previsit( const Type * ty ) { | 
|---|
| 34 | assertf( false, "Attempted generic substitution for non-aggregate type: %s", | 
|---|
| 35 | toString( ty ).c_str() ); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | void previsit( const ReferenceType * ) { | 
|---|
| 39 | // do nothing; allows substitution from base type | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | void previsit( const ReferenceToType * ty ) { | 
|---|
| 43 | visit_children = false; | 
|---|
| 44 | // build substitution from base parameters | 
|---|
| 45 | const AggregateDecl * aggr = ty->aggr(); | 
|---|
| 46 | sub = TypeSubstitution{ aggr->params.begin(), aggr->params.end(), ty->params.begin() }; | 
|---|
| 47 | } | 
|---|
| 48 | }; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | TypeSubstitution genericSubsitution( const Type * ty ) { | 
|---|
| 52 | Pass<GenericSubstitutionBuilder> builder; | 
|---|
| 53 | maybe_accept( ty, builder ); | 
|---|
| 54 | return std::move(builder.pass.sub); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | // Local Variables: // | 
|---|
| 60 | // tab-width: 4 // | 
|---|
| 61 | // mode: c++ // | 
|---|
| 62 | // compile-command: "make install" // | 
|---|
| 63 | // End: // | 
|---|