[1931bb01] | 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 | // |
---|
[11df881] | 7 | // ReplaceTypedef.cpp -- Fill in all typedefs with the underlying type. |
---|
[1931bb01] | 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Tue Jun 29 14:59:00 2022 |
---|
[b1f2007] | 11 | // Last Modified By : Peter A. Buhr |
---|
[ca9d65e] | 12 | // Last Modified On : Thu Dec 14 16:11:51 2023 |
---|
| 13 | // Update Count : 4 |
---|
[1931bb01] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "ReplaceTypedef.hpp" |
---|
| 17 | |
---|
[bccd70a] | 18 | #include "AST/Copy.hpp" |
---|
[1931bb01] | 19 | #include "AST/Pass.hpp" |
---|
| 20 | #include "Common/ScopedMap.h" |
---|
| 21 | #include "Common/UniqueName.h" |
---|
[e563edf] | 22 | #include "ResolvExpr/Unify.h" |
---|
[1931bb01] | 23 | |
---|
| 24 | namespace Validate { |
---|
| 25 | |
---|
| 26 | namespace { |
---|
| 27 | |
---|
| 28 | struct ReplaceTypedefCore final : |
---|
[e9e9f56] | 29 | public ast::WithCodeLocation, |
---|
| 30 | public ast::WithDeclsToAdd<>, |
---|
[1931bb01] | 31 | public ast::WithGuards, |
---|
| 32 | public ast::WithShortCircuiting, |
---|
[e9e9f56] | 33 | public ast::WithVisitorRef<ReplaceTypedefCore> { |
---|
[1931bb01] | 34 | |
---|
| 35 | void previsit( ast::QualifiedType const * ); |
---|
| 36 | ast::Type const * postvisit( ast::QualifiedType const * ); |
---|
| 37 | ast::Type const * postvisit( ast::TypeInstType const * ); |
---|
| 38 | ast::Decl const * postvisit( ast::TypedefDecl const * ); |
---|
| 39 | void previsit( ast::TypeDecl const * ); |
---|
| 40 | void previsit( ast::FunctionDecl const * ); |
---|
| 41 | void previsit( ast::ObjectDecl const * ); |
---|
| 42 | ast::DeclWithType const * postvisit( ast::ObjectDecl const * ); |
---|
| 43 | |
---|
| 44 | void previsit( ast::CastExpr const * ); |
---|
| 45 | void previsit( ast::CompoundStmt const * ); |
---|
| 46 | void postvisit( ast::CompoundStmt const * ); |
---|
| 47 | |
---|
| 48 | ast::StructDecl const * previsit( ast::StructDecl const * ); |
---|
| 49 | ast::UnionDecl const * previsit( ast::UnionDecl const * ); |
---|
| 50 | void previsit( ast::EnumDecl const * ); |
---|
| 51 | void previsit( ast::TraitDecl const * ); |
---|
| 52 | |
---|
| 53 | template<typename AggrDecl> |
---|
| 54 | void addImplicitTypedef( AggrDecl * aggDecl ); |
---|
| 55 | template<typename AggrDecl> |
---|
| 56 | AggrDecl const * handleAggregate( AggrDecl const * aggDecl ); |
---|
| 57 | |
---|
| 58 | using TypedefDeclPtr = ast::ptr<ast::TypedefDecl>; |
---|
| 59 | using TypedefMap = ScopedMap<std::string, std::pair<TypedefDeclPtr, int>>; |
---|
| 60 | using TypeDeclMap = ScopedMap<std::string, ast::TypeDecl const *>; |
---|
| 61 | |
---|
| 62 | TypedefMap typedefNames; |
---|
| 63 | TypeDeclMap typedeclNames; |
---|
| 64 | int scopeLevel; |
---|
[af75a87] | 65 | bool isAtFunctionTop = false; |
---|
[1931bb01] | 66 | }; |
---|
| 67 | |
---|
| 68 | void ReplaceTypedefCore::previsit( ast::QualifiedType const * ) { |
---|
| 69 | visit_children = false; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | ast::Type const * ReplaceTypedefCore::postvisit( |
---|
| 73 | ast::QualifiedType const * type ) { |
---|
| 74 | // Replacing typedefs only makes sense for the 'oldest ancestor' |
---|
| 75 | // of the qualified type. |
---|
| 76 | return ast::mutate_field( type, &ast::QualifiedType::parent, |
---|
| 77 | type->parent->accept( *visitor ) ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | ast::Type const * ReplaceTypedefCore::postvisit( |
---|
| 81 | ast::TypeInstType const * type ) { |
---|
| 82 | // Instances of typedef types will come here. If it is an instance |
---|
| 83 | // of a typedef type, link the instance to its actual type. |
---|
| 84 | TypedefMap::const_iterator def = typedefNames.find( type->name ); |
---|
| 85 | if ( def != typedefNames.end() ) { |
---|
| 86 | ast::Type * ret = ast::deepCopy( def->second.first->base ); |
---|
| 87 | ret->qualifiers |= type->qualifiers; |
---|
[af75a87] | 88 | // We ignore certain attributes on function parameters if they arrive |
---|
| 89 | // by typedef. GCC appears to do the same thing. |
---|
| 90 | if ( isAtFunctionTop ) { |
---|
[b262cb3] | 91 | erase_if( ret->attributes, []( ast::Attribute const * attr ){ |
---|
| 92 | return !attr->isValidOnFuncParam(); |
---|
| 93 | } ); |
---|
[1931bb01] | 94 | } |
---|
| 95 | for ( const auto & attribute : type->attributes ) { |
---|
| 96 | ret->attributes.push_back( attribute ); |
---|
| 97 | } |
---|
| 98 | // Place instance parameters on the typedef'd type. |
---|
| 99 | if ( !type->params.empty() ) { |
---|
| 100 | auto rtt = dynamic_cast<ast::BaseInstType *>( ret ); |
---|
| 101 | if ( !rtt ) { |
---|
[e9e9f56] | 102 | assert( location ); |
---|
[b1f2007] | 103 | SemanticError( *location, "Cannot apply type parameters to base type of %s.", type->name.c_str() ); |
---|
[1931bb01] | 104 | } |
---|
| 105 | rtt->params.clear(); |
---|
| 106 | for ( auto it : type->params ) { |
---|
| 107 | rtt->params.push_back( ast::deepCopy( it ) ); |
---|
| 108 | } |
---|
| 109 | // Recursively fix typedefs on parameters. |
---|
| 110 | ast::mutate_each( rtt, &ast::BaseInstType::params, *visitor ); |
---|
| 111 | } |
---|
| 112 | return ret; |
---|
| 113 | } else { |
---|
| 114 | TypeDeclMap::const_iterator base = typedeclNames.find( type->name ); |
---|
| 115 | if ( base == typedeclNames.end() ) { |
---|
[e9e9f56] | 116 | assert( location ); |
---|
[b1f2007] | 117 | SemanticError( *location, "Use of undefined type %s.", type->name.c_str() ); |
---|
[1931bb01] | 118 | } |
---|
| 119 | return ast::mutate_field( type, &ast::TypeInstType::base, base->second ); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | struct VarLenChecker : public ast::WithShortCircuiting { |
---|
| 124 | bool result = false; |
---|
| 125 | void previsit( ast::FunctionType const * ) { visit_children = false; } |
---|
| 126 | void previsit( ast::ArrayType const * at ) { result |= at->isVarLen; } |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | ast::Decl const * ReplaceTypedefCore::postvisit( |
---|
| 130 | ast::TypedefDecl const * decl ) { |
---|
| 131 | if ( 1 == typedefNames.count( decl->name ) && |
---|
| 132 | typedefNames[ decl->name ].second == scopeLevel ) { |
---|
| 133 | ast::Type const * t0 = decl->base; |
---|
| 134 | ast::Type const * t1 = typedefNames[ decl->name ].first->base; |
---|
| 135 | // Cannot redefine VLA typedefs. Note: this is slightly incorrect, |
---|
| 136 | // because our notion of VLAs at this point in the translator is |
---|
| 137 | // imprecise. In particular, this will disallow redefining typedefs |
---|
| 138 | // with arrays whose dimension is an enumerator or a cast of a |
---|
| 139 | // constant/enumerator. The effort required to fix this corner case |
---|
| 140 | // likely outweighs the utility of allowing it. |
---|
[251ce80] | 141 | if ( !ResolvExpr::typesCompatible( t0, t1 ) |
---|
[1931bb01] | 142 | || ast::Pass<VarLenChecker>::read( t0 ) |
---|
| 143 | || ast::Pass<VarLenChecker>::read( t1 ) ) { |
---|
[ca9d65e] | 144 | SemanticError( decl->location, "Cannot redefine typedef %s", decl->name.c_str() ); |
---|
[1931bb01] | 145 | } |
---|
| 146 | } else { |
---|
| 147 | typedefNames[ decl->name ] = |
---|
| 148 | std::make_pair( TypedefDeclPtr( decl ), scopeLevel ); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // When a typedef is a forward declaration: |
---|
| 152 | // > typedef struct screen SCREEN; |
---|
| 153 | // the declaration portion must be retained: |
---|
| 154 | // > struct screen; |
---|
| 155 | // because the expansion of the typedef is: |
---|
| 156 | // > void func( SCREEN * p ) -> void func( struct screen * p ); |
---|
| 157 | // hence type name "screen" must be defined. |
---|
| 158 | // Note: qualifiers on the typedef are not used for the forward declaration. |
---|
| 159 | |
---|
| 160 | ast::Type const * designatorType = decl->base->stripDeclarator(); |
---|
| 161 | if ( auto structType = dynamic_cast<ast::StructInstType const *>( designatorType ) ) { |
---|
| 162 | declsToAddBefore.push_back( new ast::StructDecl( |
---|
| 163 | decl->location, structType->name, ast::AggregateDecl::Struct, {}, |
---|
| 164 | decl->linkage ) ); |
---|
| 165 | } else if ( auto unionType = dynamic_cast<ast::UnionInstType const *>( designatorType ) ) { |
---|
| 166 | declsToAddBefore.push_back( new ast::UnionDecl( |
---|
| 167 | decl->location, unionType->name, {}, decl->linkage ) ); |
---|
| 168 | } else if ( auto enumType = dynamic_cast<ast::EnumInstType const *>( designatorType ) ) { |
---|
| 169 | declsToAddBefore.push_back( new ast::EnumDecl( |
---|
[b0d9ff7] | 170 | decl->location, enumType->name, false, {}, decl->linkage, |
---|
[1931bb01] | 171 | ( (enumType->base) ? enumType->base->base : nullptr ) |
---|
| 172 | ) ); |
---|
| 173 | } |
---|
| 174 | return ast::deepCopy( decl ); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void ReplaceTypedefCore::previsit( ast::TypeDecl const * decl ) { |
---|
[21a2a7d] | 178 | typedefNames.erase( decl->name ); |
---|
[1931bb01] | 179 | typedeclNames.insert( decl->name, decl ); |
---|
| 180 | } |
---|
| 181 | |
---|
[0bd46fd] | 182 | void ReplaceTypedefCore::previsit( ast::FunctionDecl const * ) { |
---|
[1931bb01] | 183 | GuardScope( typedefNames ); |
---|
| 184 | GuardScope( typedeclNames ); |
---|
[af75a87] | 185 | GuardValue( isAtFunctionTop ) = true; |
---|
[1931bb01] | 186 | } |
---|
| 187 | |
---|
[0bd46fd] | 188 | void ReplaceTypedefCore::previsit( ast::ObjectDecl const * ) { |
---|
[1931bb01] | 189 | GuardScope( typedefNames ); |
---|
| 190 | GuardScope( typedeclNames ); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | ast::DeclWithType const * ReplaceTypedefCore::postvisit( |
---|
| 194 | ast::ObjectDecl const * decl ) { |
---|
| 195 | if ( ast::FunctionType const * type = decl->type.as<ast::FunctionType>() ) { |
---|
| 196 | using DWTVector = std::vector<ast::ptr<ast::DeclWithType>>; |
---|
| 197 | using DeclVector = std::vector<ast::ptr<ast::TypeDecl>>; |
---|
[e9e9f56] | 198 | CodeLocation const & declLocation = decl->location; |
---|
[1931bb01] | 199 | UniqueName paramNamer( decl->name + "Param" ); |
---|
| 200 | |
---|
| 201 | // Replace the current object declaration with a function declaration. |
---|
| 202 | ast::FunctionDecl const * newDecl = new ast::FunctionDecl( |
---|
[e9e9f56] | 203 | declLocation, |
---|
[1931bb01] | 204 | decl->name, |
---|
| 205 | map_range<DeclVector>( type->forall, []( const ast::TypeInstType * inst ) { |
---|
| 206 | return ast::deepCopy( inst->base ); |
---|
| 207 | } ), |
---|
| 208 | map_range<DWTVector>( type->assertions, []( const ast::VariableExpr * expr ) { |
---|
| 209 | return ast::deepCopy( expr->var ); |
---|
| 210 | } ), |
---|
[e9e9f56] | 211 | map_range<DWTVector>( type->params, [&declLocation, ¶mNamer]( const ast::Type * type ) { |
---|
[1931bb01] | 212 | assert( type ); |
---|
[e9e9f56] | 213 | return new ast::ObjectDecl( declLocation, paramNamer.newName(), ast::deepCopy( type ) ); |
---|
[1931bb01] | 214 | } ), |
---|
[e9e9f56] | 215 | map_range<DWTVector>( type->returns, [&declLocation, ¶mNamer]( const ast::Type * type ) { |
---|
[1931bb01] | 216 | assert( type ); |
---|
[e9e9f56] | 217 | return new ast::ObjectDecl( declLocation, paramNamer.newName(), ast::deepCopy( type ) ); |
---|
[1931bb01] | 218 | } ), |
---|
| 219 | nullptr, |
---|
| 220 | decl->storage, |
---|
| 221 | decl->linkage, |
---|
| 222 | {/* attributes */}, |
---|
| 223 | decl->funcSpec |
---|
| 224 | ); |
---|
| 225 | return newDecl; |
---|
| 226 | } |
---|
| 227 | return decl; |
---|
| 228 | } |
---|
| 229 | |
---|
[0bd46fd] | 230 | void ReplaceTypedefCore::previsit( ast::CastExpr const * ) { |
---|
[1931bb01] | 231 | GuardScope( typedefNames ); |
---|
| 232 | GuardScope( typedeclNames ); |
---|
| 233 | } |
---|
| 234 | |
---|
[0bd46fd] | 235 | void ReplaceTypedefCore::previsit( ast::CompoundStmt const * ) { |
---|
[1931bb01] | 236 | GuardScope( typedefNames ); |
---|
| 237 | GuardScope( typedeclNames ); |
---|
[af75a87] | 238 | GuardValue( isAtFunctionTop ) = false; |
---|
[1931bb01] | 239 | scopeLevel += 1; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | void ReplaceTypedefCore::postvisit( ast::CompoundStmt const * ) { |
---|
| 243 | scopeLevel -= 1; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | ast::StructDecl const * ReplaceTypedefCore::previsit( ast::StructDecl const * decl ) { |
---|
| 247 | visit_children = false; |
---|
| 248 | addImplicitTypedef( decl ); |
---|
| 249 | return handleAggregate( decl ); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | ast::UnionDecl const * ReplaceTypedefCore::previsit( ast::UnionDecl const * decl ) { |
---|
| 253 | visit_children = false; |
---|
| 254 | addImplicitTypedef( decl ); |
---|
| 255 | return handleAggregate( decl ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void ReplaceTypedefCore::previsit( ast::EnumDecl const * decl ) { |
---|
| 259 | addImplicitTypedef( decl ); |
---|
| 260 | } |
---|
| 261 | |
---|
[0bd46fd] | 262 | void ReplaceTypedefCore::previsit( ast::TraitDecl const * ) { |
---|
[1931bb01] | 263 | GuardScope( typedefNames ); |
---|
| 264 | GuardScope( typedeclNames ); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | template<typename AggrDecl> |
---|
| 268 | void ReplaceTypedefCore::addImplicitTypedef( AggrDecl * aggrDecl ) { |
---|
| 269 | if ( 0 != typedefNames.count( aggrDecl->name ) ) { |
---|
| 270 | return; |
---|
| 271 | } |
---|
| 272 | ast::Type * type = nullptr; |
---|
| 273 | if ( auto structDecl = dynamic_cast<const ast::StructDecl *>( aggrDecl ) ) { |
---|
| 274 | type = new ast::StructInstType( structDecl->name ); |
---|
| 275 | } else if ( auto unionDecl = dynamic_cast<const ast::UnionDecl *>( aggrDecl ) ) { |
---|
| 276 | type = new ast::UnionInstType( unionDecl->name ); |
---|
| 277 | } else if ( auto enumDecl = dynamic_cast<const ast::EnumDecl *>( aggrDecl ) ) { |
---|
| 278 | type = new ast::EnumInstType( enumDecl->name ); |
---|
| 279 | } |
---|
| 280 | assert( type ); |
---|
| 281 | |
---|
| 282 | TypedefDeclPtr typeDecl = new ast::TypedefDecl( aggrDecl->location, |
---|
| 283 | aggrDecl->name, ast::Storage::Classes(), type, aggrDecl->linkage ); |
---|
| 284 | // Add the implicit typedef to the AST. |
---|
[b7c53a9d] | 285 | declsToAddAfter.push_back( ast::deepCopy( typeDecl.get() ) ); |
---|
[1931bb01] | 286 | // Shore the name in the map of names. |
---|
| 287 | typedefNames[ aggrDecl->name ] = |
---|
| 288 | std::make_pair( std::move( typeDecl ), scopeLevel ); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | template<typename AggrDecl> |
---|
| 292 | AggrDecl const * ReplaceTypedefCore::handleAggregate( AggrDecl const * decl ) { |
---|
| 293 | SemanticErrorException errors; |
---|
| 294 | |
---|
| 295 | ValueGuard<decltype(declsToAddBefore)> oldBeforeDecls( declsToAddBefore ); |
---|
| 296 | ValueGuard<decltype(declsToAddAfter )> oldAfterDecls( declsToAddAfter ); |
---|
| 297 | declsToAddBefore.clear(); |
---|
| 298 | declsToAddAfter.clear(); |
---|
| 299 | |
---|
| 300 | GuardScope( typedefNames ); |
---|
| 301 | GuardScope( typedeclNames ); |
---|
| 302 | decl = mutate_each( decl, &ast::AggregateDecl::params, *visitor ); |
---|
| 303 | decl = mutate_each( decl, &ast::AggregateDecl::attributes, *visitor ); |
---|
| 304 | |
---|
| 305 | auto mut = ast::mutate( decl ); |
---|
| 306 | |
---|
[b7c53a9d] | 307 | std::list<ast::ptr<ast::Decl>> members; |
---|
[1931bb01] | 308 | // Unroll accept_all for decl->members so that implicit typedefs for |
---|
| 309 | // nested types are added to the aggregate body. |
---|
| 310 | for ( ast::ptr<ast::Decl> const & member : mut->members ) { |
---|
[b7c53a9d] | 311 | assert( declsToAddBefore.empty() ); |
---|
[1931bb01] | 312 | assert( declsToAddAfter.empty() ); |
---|
| 313 | ast::Decl const * newMember = nullptr; |
---|
| 314 | try { |
---|
| 315 | newMember = member->accept( *visitor ); |
---|
| 316 | } catch ( SemanticErrorException & e ) { |
---|
| 317 | errors.append( e ); |
---|
| 318 | } |
---|
| 319 | if ( !declsToAddBefore.empty() ) { |
---|
[b7c53a9d] | 320 | members.splice( members.end(), declsToAddBefore ); |
---|
[1931bb01] | 321 | } |
---|
| 322 | members.push_back( newMember ); |
---|
[b7c53a9d] | 323 | if ( !declsToAddAfter.empty() ) { |
---|
| 324 | members.splice( members.end(), declsToAddAfter ); |
---|
| 325 | } |
---|
[1931bb01] | 326 | } |
---|
[b7c53a9d] | 327 | assert( declsToAddBefore.empty() ); |
---|
[1931bb01] | 328 | assert( declsToAddAfter.empty() ); |
---|
| 329 | if ( !errors.isEmpty() ) { throw errors; } |
---|
| 330 | |
---|
| 331 | mut->members.clear(); |
---|
| 332 | for ( auto member : members ) { |
---|
| 333 | mut->members.push_back( member ); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | return mut; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | } // namespace |
---|
| 340 | |
---|
| 341 | void replaceTypedef( ast::TranslationUnit & translationUnit ) { |
---|
| 342 | ast::Pass<ReplaceTypedefCore> pass; |
---|
| 343 | ast::accept_all( translationUnit, pass ); |
---|
| 344 | if ( pass.core.typedefNames.count( "size_t" ) ) { |
---|
| 345 | translationUnit.global.sizeType = |
---|
| 346 | ast::deepCopy( pass.core.typedefNames["size_t"].first->base ); |
---|
| 347 | } else { |
---|
| 348 | // Missing the global definition, default to long unsigned int. |
---|
| 349 | // Perhaps this should be a warning instead. |
---|
| 350 | translationUnit.global.sizeType = |
---|
| 351 | new ast::BasicType( ast::BasicType::LongUnsignedInt ); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | } // namespace Validate |
---|
| 356 | |
---|
| 357 | // Local Variables: // |
---|
| 358 | // tab-width: 4 // |
---|
| 359 | // mode: c++ // |
---|
| 360 | // compile-command: "make install" // |
---|
| 361 | // End: // |
---|