| 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 | // HoistTypeDecls.cpp -- Hoists declarations of implicitly declared types.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Mon Jul  4  9:52:00 2022
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Mon Jul 12 14:07:00 2022
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "HoistTypeDecls.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Pass.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | namespace Validate {
 | 
|---|
| 21 | 
 | 
|---|
| 22 | namespace {
 | 
|---|
| 23 | 
 | 
|---|
| 24 | struct HoistTypeDecls final : public ast::WithDeclsToAdd<> {
 | 
|---|
| 25 |         void previsit( ast::SizeofExpr const * );
 | 
|---|
| 26 |         void previsit( ast::AlignofExpr const * );
 | 
|---|
| 27 |         void previsit( ast::UntypedOffsetofExpr const * );
 | 
|---|
| 28 |         void previsit( ast::CompoundLiteralExpr const * );
 | 
|---|
| 29 |         void handleType( ast::Type const * );
 | 
|---|
| 30 | };
 | 
|---|
| 31 | 
 | 
|---|
| 32 | void HoistTypeDecls::previsit( ast::SizeofExpr const * expr ) {
 | 
|---|
| 33 |         handleType( expr->type );
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | void HoistTypeDecls::previsit( ast::AlignofExpr const * expr ) {
 | 
|---|
| 37 |         handleType( expr->type );
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | void HoistTypeDecls::previsit( ast::UntypedOffsetofExpr const * expr ) {
 | 
|---|
| 41 |         handleType( expr->type );
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | void HoistTypeDecls::previsit( ast::CompoundLiteralExpr const * expr ) {
 | 
|---|
| 45 |         handleType( expr->result );
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | void HoistTypeDecls::handleType( ast::Type const * type ) {
 | 
|---|
| 49 |         // Some type declarations are buried in expressions and not easy to
 | 
|---|
| 50 |         // hoist during parsing; so they are hoisted here instead.
 | 
|---|
| 51 |         // This also where some missing code locations come in.
 | 
|---|
| 52 |         ast::AggregateDecl const * aggr = nullptr;
 | 
|---|
| 53 |         if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) {
 | 
|---|
| 54 |                 aggr = inst->base;
 | 
|---|
| 55 |         } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) {
 | 
|---|
| 56 |                 aggr = inst->base;
 | 
|---|
| 57 |         } else if ( auto inst = dynamic_cast<ast::EnumInstType const *>( type ) ) {
 | 
|---|
| 58 |                 aggr = inst->base;
 | 
|---|
| 59 |         }
 | 
|---|
| 60 |         if ( aggr && aggr->body ) {
 | 
|---|
| 61 |                 declsToAddBefore.push_front( aggr );
 | 
|---|
| 62 |         }
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | } // namespace
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void hoistTypeDecls( ast::TranslationUnit & translationUnit ) {
 | 
|---|
| 68 |         ast::Pass<HoistTypeDecls>::run( translationUnit );
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | } // namespace Validate
 | 
|---|
| 72 | 
 | 
|---|
| 73 | // Local Variables: //
 | 
|---|
| 74 | // tab-width: 4 //
 | 
|---|
| 75 | // mode: c++ //
 | 
|---|
| 76 | // compile-command: "make install" //
 | 
|---|
| 77 | // End: //
 | 
|---|