[298fe57] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // HoistStruct.cpp -- Flattens nested type declarations. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Thr Apr 21 10:34:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thr Apr 21 10:34:00 2022 |
---|
| 13 | // Update Count : 0 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include "Validate/HoistStruct.hpp" |
---|
| 17 | |
---|
[9feb34b] | 18 | #include <sstream> |
---|
| 19 | |
---|
[0658672] | 20 | #include "AST/DeclReplacer.hpp" |
---|
[298fe57] | 21 | #include "AST/Pass.hpp" |
---|
| 22 | #include "AST/TranslationUnit.hpp" |
---|
[0658672] | 23 | #include "AST/Vector.hpp" |
---|
[298fe57] | 24 | |
---|
| 25 | namespace Validate { |
---|
| 26 | |
---|
| 27 | namespace { |
---|
| 28 | |
---|
[0153dbd] | 29 | /// Is this a declaration can appear in a struct/union and should be hoisted? |
---|
[298fe57] | 30 | bool shouldHoist( ast::Decl const * decl ) { |
---|
| 31 | return dynamic_cast< ast::StructDecl const * >( decl ) |
---|
| 32 | || dynamic_cast< ast::UnionDecl const * >( decl ) |
---|
[8b4faf6] | 33 | || dynamic_cast< ast::EnumDecl const * >( decl ) |
---|
[298fe57] | 34 | || dynamic_cast< ast::StaticAssertDecl const * >( decl ); |
---|
| 35 | } |
---|
| 36 | |
---|
[0153dbd] | 37 | /// Helper that updates an InstType if the base name could be updated. |
---|
| 38 | template<typename InstType> |
---|
| 39 | InstType const * preInstType( InstType const * type ) { |
---|
| 40 | assert( type->base ); |
---|
| 41 | if ( nullptr == type->base->parent ) return type; |
---|
| 42 | auto mut = ast::mutate( type ); |
---|
| 43 | mut->name = mut->base->name; |
---|
| 44 | return mut; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /// Update StructInstType and UnionInstType names. |
---|
| 48 | struct NameUpdater { |
---|
| 49 | ast::StructInstType const * previsit( ast::StructInstType const * type ) { |
---|
| 50 | return preInstType( type ); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | ast::UnionInstType const * previsit( ast::UnionInstType const * type ) { |
---|
| 54 | return preInstType( type ); |
---|
| 55 | } |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | ast::Decl const * updateNames( ast::Decl const * decl ) { |
---|
| 59 | ast::Pass<NameUpdater> visitor; |
---|
| 60 | return decl->accept( visitor ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /* This pass hoists from structs/unions. Hoisted declarations should always |
---|
[298fe57] | 64 | * appear before the declaration they are hoisted out of and if two types are |
---|
| 65 | * nested in the same declaration their order should not change. |
---|
[0153dbd] | 66 | * It also sets up parent relationships, does name mangling of hoisted types |
---|
| 67 | * and updates instance types of the hoisted types. |
---|
[298fe57] | 68 | */ |
---|
| 69 | struct HoistStructCore final : |
---|
| 70 | public ast::WithDeclsToAdd<>, public ast::WithGuards { |
---|
| 71 | ast::StructDecl const * previsit( ast::StructDecl const * decl ); |
---|
| 72 | ast::StructDecl const * postvisit( ast::StructDecl const * decl ); |
---|
| 73 | ast::UnionDecl const * previsit( ast::UnionDecl const * decl ); |
---|
| 74 | ast::UnionDecl const * postvisit( ast::UnionDecl const * decl ); |
---|
| 75 | ast::StructInstType const * previsit( ast::StructInstType const * type ); |
---|
| 76 | ast::UnionInstType const * previsit( ast::UnionInstType const * type ); |
---|
| 77 | ast::EnumInstType const * previsit( ast::EnumInstType const * type ); |
---|
| 78 | |
---|
| 79 | private: |
---|
| 80 | template<typename AggrDecl> |
---|
| 81 | AggrDecl const * preAggregate( AggrDecl const * ); |
---|
| 82 | template<typename AggrDecl> |
---|
| 83 | AggrDecl const * postAggregate( AggrDecl const * ); |
---|
[0658672] | 84 | template<typename InstType> |
---|
| 85 | InstType const * preCollectionInstType( InstType const * type ); |
---|
[298fe57] | 86 | |
---|
| 87 | ast::AggregateDecl const * parent = nullptr; |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | void qualifiedName( ast::AggregateDecl const * decl, std::ostringstream & ss ) { |
---|
| 91 | if ( decl->parent ) { |
---|
| 92 | qualifiedName( decl->parent, ss ); |
---|
| 93 | } |
---|
| 94 | ss << "__" << decl->name; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | std::string qualifiedName( ast::AggregateDecl const * decl ) { |
---|
| 98 | std::ostringstream ss; |
---|
| 99 | qualifiedName( decl, ss ); |
---|
| 100 | return ss.str(); |
---|
| 101 | } |
---|
| 102 | |
---|
[0658672] | 103 | void extendParams( ast::vector<ast::TypeDecl> & dstParams, |
---|
| 104 | ast::vector<ast::TypeDecl> const & srcParams ) { |
---|
| 105 | if ( srcParams.empty() ) return; |
---|
| 106 | |
---|
| 107 | ast::DeclReplacer::TypeMap newToOld; |
---|
| 108 | ast::vector<ast::TypeDecl> params; |
---|
| 109 | for ( ast::ptr<ast::TypeDecl> const & srcParam : srcParams ) { |
---|
| 110 | ast::TypeDecl * dstParam = ast::deepCopy( srcParam.get() ); |
---|
| 111 | dstParam->init = nullptr; |
---|
| 112 | newToOld.emplace( srcParam, dstParam ); |
---|
| 113 | for ( auto assertion : dstParam->assertions ) { |
---|
| 114 | assertion = ast::DeclReplacer::replace( assertion, newToOld ); |
---|
| 115 | } |
---|
| 116 | params.emplace_back( dstParam ); |
---|
| 117 | } |
---|
| 118 | spliceBegin( dstParams, params ); |
---|
| 119 | } |
---|
| 120 | |
---|
[298fe57] | 121 | template<typename AggrDecl> |
---|
| 122 | AggrDecl const * HoistStructCore::preAggregate( AggrDecl const * decl ) { |
---|
| 123 | if ( parent ) { |
---|
| 124 | auto mut = ast::mutate( decl ); |
---|
| 125 | mut->parent = parent; |
---|
[0658672] | 126 | extendParams( mut->params, parent->params ); |
---|
| 127 | decl = mut; |
---|
[298fe57] | 128 | } |
---|
[0658672] | 129 | GuardValue( parent ) = decl; |
---|
| 130 | return decl; |
---|
[298fe57] | 131 | } |
---|
| 132 | |
---|
| 133 | template<typename AggrDecl> |
---|
| 134 | AggrDecl const * HoistStructCore::postAggregate( AggrDecl const * decl ) { |
---|
| 135 | auto mut = ast::mutate( decl ); |
---|
| 136 | for ( auto it = mut->members.begin() ; it != mut->members.end() ; ) { |
---|
| 137 | if ( shouldHoist( *it ) ) { |
---|
| 138 | // This is the place where the actual hoisting happens. |
---|
| 139 | declsToAddBefore.push_back( it->get() ); |
---|
| 140 | it = mut->members.erase( it ); |
---|
| 141 | } else { |
---|
| 142 | ++it; |
---|
| 143 | } |
---|
| 144 | } |
---|
[0153dbd] | 145 | // Is this a nested type? Then update the name, after the parent's name |
---|
| 146 | // has been updated (hence the post visit). |
---|
| 147 | if ( mut->parent ) { |
---|
| 148 | mut->name = qualifiedName( mut ); |
---|
| 149 | // Top level type that has hoisted? Then do a second pass subpass to make |
---|
| 150 | // sure we update instance type names after the declaration is renamed. |
---|
| 151 | } else if ( !declsToAddBefore.empty() ) { |
---|
| 152 | for ( ast::ptr<ast::Decl> & member : mut->members ) { |
---|
| 153 | member = updateNames( member.get() ); |
---|
| 154 | } |
---|
| 155 | for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) { |
---|
| 156 | declToAdd = updateNames( declToAdd ); |
---|
| 157 | } |
---|
| 158 | } |
---|
[298fe57] | 159 | return mut; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | ast::StructDecl const * HoistStructCore::previsit( ast::StructDecl const * decl ) { |
---|
| 163 | return preAggregate( decl ); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | ast::StructDecl const * HoistStructCore::postvisit( ast::StructDecl const * decl ) { |
---|
| 167 | return postAggregate( decl ); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | ast::UnionDecl const * HoistStructCore::previsit( ast::UnionDecl const * decl ) { |
---|
| 171 | return preAggregate( decl ); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | ast::UnionDecl const * HoistStructCore::postvisit( ast::UnionDecl const * decl ) { |
---|
| 175 | return postAggregate( decl ); |
---|
| 176 | } |
---|
| 177 | |
---|
[0658672] | 178 | ast::AggregateDecl const * commonParent( |
---|
| 179 | ast::AggregateDecl const * lhs, ast::AggregateDecl const * rhs ) { |
---|
| 180 | for ( auto outer = lhs ; outer ; outer = outer->parent ) { |
---|
| 181 | for ( auto inner = rhs ; inner ; inner = inner->parent ) { |
---|
| 182 | if ( outer == inner ) { |
---|
| 183 | return outer; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | return nullptr; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | template<typename InstType> |
---|
| 191 | InstType const * HoistStructCore::preCollectionInstType( InstType const * type ) { |
---|
[fc1a3e2] | 192 | if ( !type->base->parent ) return type; |
---|
| 193 | if ( type->base->params.empty() ) return type; |
---|
| 194 | |
---|
| 195 | InstType * mut = ast::mutate( type ); |
---|
| 196 | ast::AggregateDecl const * parent = |
---|
| 197 | commonParent( this->parent, mut->base->parent ); |
---|
| 198 | assert( parent ); |
---|
| 199 | |
---|
| 200 | std::vector<ast::ptr<ast::Expr>> args; |
---|
| 201 | for ( const ast::ptr<ast::TypeDecl> & param : parent->params ) { |
---|
| 202 | args.emplace_back( new ast::TypeExpr( param->location, |
---|
| 203 | new ast::TypeInstType( param ) |
---|
| 204 | ) ); |
---|
| 205 | } |
---|
| 206 | spliceBegin( mut->params, args ); |
---|
| 207 | return mut; |
---|
[0658672] | 208 | } |
---|
| 209 | |
---|
[298fe57] | 210 | ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) { |
---|
[0658672] | 211 | return preInstType( preCollectionInstType( type ) ); |
---|
[298fe57] | 212 | } |
---|
| 213 | |
---|
| 214 | ast::UnionInstType const * HoistStructCore::previsit( ast::UnionInstType const * type ) { |
---|
[0658672] | 215 | return preInstType( preCollectionInstType( type ) ); |
---|
[298fe57] | 216 | } |
---|
| 217 | |
---|
| 218 | ast::EnumInstType const * HoistStructCore::previsit( ast::EnumInstType const * type ) { |
---|
| 219 | return preInstType( type ); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | } // namespace |
---|
| 223 | |
---|
| 224 | void hoistStruct( ast::TranslationUnit & translationUnit ) { |
---|
| 225 | ast::Pass<HoistStructCore>::run( translationUnit ); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | } // namespace Validate |
---|
| 229 | |
---|
| 230 | // Local Variables: // |
---|
| 231 | // tab-width: 4 // |
---|
| 232 | // mode: c++ // |
---|
| 233 | // compile-command: "make install" // |
---|
| 234 | // End: // |
---|