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 -- |
---|
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 | // = parent->genericSubstitution(); |
---|
79 | auto result = sub.apply(ret); |
---|
80 | return result.node.release(); |
---|
81 | } |
---|
82 | } |
---|
83 | } else { |
---|
84 | // S.T - S is not an aggregate => error. |
---|
85 | assertf( false, "unhandled qualified child type: %s", toCString(type) ); |
---|
86 | } |
---|
87 | } |
---|
88 | // failed to find a satisfying definition of type |
---|
89 | SemanticError( *location, toString("Undefined type in qualified type: ", type) ); |
---|
90 | } |
---|
91 | } |
---|
92 | }; |
---|
93 | |
---|
94 | } // namespace |
---|
95 | |
---|
96 | void fixQualifiedTypes( ast::TranslationUnit & translationUnit ) { |
---|
97 | ast::Pass<FixQualifiedTypesCore>::run( translationUnit ); |
---|
98 | } |
---|
99 | |
---|
100 | } // namespace Validate |
---|
101 | |
---|
102 | // Local Variables: // |
---|
103 | // tab-width: 4 // |
---|
104 | // mode: c++ // |
---|
105 | // compile-command: "make install" // |
---|
106 | // End: // |
---|