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 : Andrew Beach |
---|
12 | // Last Modified On : Fri Apr 22 11:36:00 2022 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Validate/FixQualifiedTypes.hpp" |
---|
17 | |
---|
18 | #include "AST/Pass.hpp" |
---|
19 | #include "AST/TranslationUnit.hpp" |
---|
20 | #include "Validate/NoIdSymbolTable.hpp" |
---|
21 | |
---|
22 | namespace Validate { |
---|
23 | |
---|
24 | namespace { |
---|
25 | |
---|
26 | struct FixQualifiedTypesCore : |
---|
27 | public WithNoIdSymbolTable, public ast::WithGuards { |
---|
28 | CodeLocation const * location = nullptr; |
---|
29 | |
---|
30 | void previsit( ast::ParseNode const * node ) { |
---|
31 | GuardValue( location ) = &node->location; |
---|
32 | } |
---|
33 | |
---|
34 | ast::Type const * postvisit( ast::QualifiedType const * type ) { |
---|
35 | assert( location ); |
---|
36 | |
---|
37 | ast::ptr<ast::Type> const & parent = type->parent; |
---|
38 | ast::ptr<ast::Type> const & child = type->child; |
---|
39 | if ( parent.as<ast::GlobalScopeType>() ) { |
---|
40 | // .T => lookup T at global scope. |
---|
41 | if ( auto inst = child.as<ast::TypeInstType>() ) { |
---|
42 | auto td = symtab.globalLookupType( inst->name ); |
---|
43 | if ( !td ) { |
---|
44 | SemanticError( *location, toString("Use of undefined global type ", inst->name) ); |
---|
45 | } |
---|
46 | auto base = td->base; |
---|
47 | assert( base ); |
---|
48 | ast::Type * ret = ast::deepCopy( base ); |
---|
49 | ret->qualifiers = type->qualifiers; |
---|
50 | return ret; |
---|
51 | } else { |
---|
52 | // .T => T is not a type name. |
---|
53 | assertf( false, "unhandled global qualified child type: %s", toCString(child) ); |
---|
54 | } |
---|
55 | } else { |
---|
56 | // S.T => S must be an aggregate type, find the declaration for T in S. |
---|
57 | ast::AggregateDecl const * aggr = nullptr; |
---|
58 | ast::BaseInstType const * instp = nullptr; |
---|
59 | if ( auto inst = parent.as<ast::StructInstType>() ) { |
---|
60 | aggr = inst->base; |
---|
61 | instp = inst; |
---|
62 | } else if ( auto inst = parent.as<ast::UnionInstType>() ) { |
---|
63 | aggr = inst->base; |
---|
64 | instp = inst; |
---|
65 | } else { |
---|
66 | SemanticError( *location, toString("Qualified type requires an aggregate on the left, but has: ", parent) ); |
---|
67 | } |
---|
68 | // TODO: Need to handle forward declarations. |
---|
69 | assert( aggr ); |
---|
70 | for ( ast::ptr<ast::Decl> const & member : aggr->members ) { |
---|
71 | if ( auto inst = child.as<ast::TypeInstType>() ) { |
---|
72 | if ( auto decl = member.as<ast::NamedTypeDecl>() ) { |
---|
73 | if ( decl->name == inst->name ) { |
---|
74 | assert( decl->base ); |
---|
75 | ast::Type * ret = ast::deepCopy( decl->base ); |
---|
76 | ret->qualifiers = type->qualifiers; |
---|
77 | ast::TypeSubstitution sub( aggr->params, instp->params ); |
---|
78 | auto result = sub.apply(ret); |
---|
79 | return result.node.release(); |
---|
80 | } |
---|
81 | } |
---|
82 | } else { |
---|
83 | // S.T - S is not an aggregate => error. |
---|
84 | assertf( false, "unhandled qualified child type: %s", toCString(type) ); |
---|
85 | } |
---|
86 | } |
---|
87 | // failed to find a satisfying definition of type |
---|
88 | SemanticError( *location, toString("Undefined type in qualified type: ", type) ); |
---|
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: // |
---|