| 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 | // FixQualifiedTypes.cpp -- Replace the qualified type with a direct type.
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Andrew Beach
|
|---|
| 10 | // Created On : Thr Apr 21 11:13:00 2022
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Wed Dec 13 09:00:25 2023
|
|---|
| 13 | // Update Count : 6
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Validate/FixQualifiedTypes.hpp"
|
|---|
| 17 |
|
|---|
| 18 | #include "AST/Copy.hpp"
|
|---|
| 19 | #include "AST/LinkageSpec.hpp" // for Linkage
|
|---|
| 20 | #include "AST/Pass.hpp"
|
|---|
| 21 | #include "AST/TranslationUnit.hpp"
|
|---|
| 22 | #include "Common/ToString.hpp" // for toString
|
|---|
| 23 | #include "SymTab/Mangler.h" // for Mangler
|
|---|
| 24 | #include "Validate/NoIdSymbolTable.hpp"
|
|---|
| 25 |
|
|---|
| 26 | namespace Validate {
|
|---|
| 27 |
|
|---|
| 28 | namespace {
|
|---|
| 29 |
|
|---|
| 30 | struct FixQualifiedTypesCore :
|
|---|
| 31 | public WithNoIdSymbolTable,
|
|---|
| 32 | public ast::WithCodeLocation {
|
|---|
| 33 | ast::Type const * postvisit( ast::QualifiedType const * type ) {
|
|---|
| 34 | assert( location );
|
|---|
| 35 |
|
|---|
| 36 | ast::ptr<ast::Type> const & parent = type->parent;
|
|---|
| 37 | ast::ptr<ast::Type> const & child = type->child;
|
|---|
| 38 | if ( parent.as<ast::GlobalScopeType>() ) {
|
|---|
| 39 | // .T => lookup T at global scope.
|
|---|
| 40 | if ( auto inst = child.as<ast::TypeInstType>() ) {
|
|---|
| 41 | auto td = symtab.globalLookupType( inst->name );
|
|---|
| 42 | if ( !td ) {
|
|---|
| 43 | SemanticError( *location, "Use of undefined global type %s.", inst->name.c_str() );
|
|---|
| 44 | }
|
|---|
| 45 | auto base = td->base;
|
|---|
| 46 | assert( base );
|
|---|
| 47 | ast::Type * ret = ast::deepCopy( base );
|
|---|
| 48 | ret->qualifiers = type->qualifiers;
|
|---|
| 49 | return ret;
|
|---|
| 50 | } else {
|
|---|
| 51 | // .T => T is not a type name.
|
|---|
| 52 | assertf( false, "unhandled global qualified child type: %s", toCString( child ) );
|
|---|
| 53 | }
|
|---|
| 54 | } else {
|
|---|
| 55 | // S.T => S must be an aggregate type, find the declaration for T in S.
|
|---|
| 56 | ast::AggregateDecl const * aggr = nullptr;
|
|---|
| 57 | ast::BaseInstType const * instp = nullptr;
|
|---|
| 58 | if ( auto inst = parent.as<ast::StructInstType>() ) {
|
|---|
| 59 | aggr = inst->base;
|
|---|
| 60 | instp = inst;
|
|---|
| 61 | } else if ( auto inst = parent.as<ast::UnionInstType>() ) {
|
|---|
| 62 | aggr = inst->base;
|
|---|
| 63 | instp = inst;
|
|---|
| 64 | } else {
|
|---|
| 65 | SemanticError( *location, "Qualified type requires an aggregate on the left, but has %s.", toCString( parent ) );
|
|---|
| 66 | }
|
|---|
| 67 | // TODO: Need to handle forward declarations.
|
|---|
| 68 | assert( aggr );
|
|---|
| 69 | for ( ast::ptr<ast::Decl> const & member : aggr->members ) {
|
|---|
| 70 | if ( auto inst = child.as<ast::TypeInstType>() ) {
|
|---|
| 71 | if ( auto decl = member.as<ast::NamedTypeDecl>() ) {
|
|---|
| 72 | if ( decl->name == inst->name ) {
|
|---|
| 73 | assert( decl->base );
|
|---|
| 74 | ast::Type * ret = ast::deepCopy( decl->base );
|
|---|
| 75 | ret->qualifiers = type->qualifiers;
|
|---|
| 76 | ast::TypeSubstitution sub( aggr->params, instp->params );
|
|---|
| 77 | auto result = sub.apply(ret);
|
|---|
| 78 | return result.node.release();
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | } else {
|
|---|
| 82 | // S.T - S is not an aggregate => error.
|
|---|
| 83 | assertf( false, "unhandled qualified child type %s.", toCString( type ) );
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | // failed to find a satisfying definition of type
|
|---|
| 87 | SemanticError( *location, "Undefined type in qualified type %s", toCString( type ) );
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | } // namespace
|
|---|
| 94 |
|
|---|
| 95 | void fixQualifiedTypes( ast::TranslationUnit & translationUnit ) {
|
|---|
| 96 | ast::Pass<FixQualifiedTypesCore>::run( translationUnit );
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | } // namespace Validate
|
|---|
| 100 |
|
|---|
| 101 | // Local Variables: //
|
|---|
| 102 | // tab-width: 4 //
|
|---|
| 103 | // mode: c++ //
|
|---|
| 104 | // compile-command: "make install" //
|
|---|
| 105 | // End: //
|
|---|