source: src/Validate/FixQualifiedTypes.cpp @ bd72c28

ADTast-experimental
Last change on this file since bd72c28 was 9feb34b, checked in by Andrew Beach <ajbeach@…>, 15 months ago

Moved toString and toCString to a new header. Updated includes. cassert was somehow getting instances of toString before but that stopped working so I embedded the new smaller include.

  • Property mode set to 100644
File size: 4.1 KB
Line 
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 : Tue Sep 20 16:15:00 2022
13// Update Count     : 1
14//
15
16#include "Validate/FixQualifiedTypes.hpp"
17
18#include "AST/LinkageSpec.hpp"             // for Linkage
19#include "AST/Pass.hpp"
20#include "AST/TranslationUnit.hpp"
21#include "Common/ToString.hpp"             // for toString
22#include "SymTab/Mangler.h"                // for Mangler
23#include "Validate/NoIdSymbolTable.hpp"
24
25namespace Validate {
26
27namespace {
28
29struct FixQualifiedTypesCore :
30                public WithNoIdSymbolTable,
31                public ast::WithCodeLocation {
32        ast::Type const * postvisit( ast::QualifiedType const * type ) {
33                assert( location );
34
35                ast::ptr<ast::Type> const & parent = type->parent;
36                ast::ptr<ast::Type> const & child = type->child;
37                if ( parent.as<ast::GlobalScopeType>() ) {
38                        // .T => lookup T at global scope.
39                        if ( auto inst = child.as<ast::TypeInstType>() ) {
40                                auto td = symtab.globalLookupType( inst->name );
41                                if ( !td ) {
42                                        SemanticError( *location, toString("Use of undefined global type ", inst->name) );
43                                }
44                                auto base = td->base;
45                                assert( base );
46                                ast::Type * ret = ast::deepCopy( base );
47                                ret->qualifiers = type->qualifiers;
48                                return ret;
49                        } else {
50                                // .T => T is not a type name.
51                                assertf( false, "unhandled global qualified child type: %s", toCString(child) );
52                        }
53                } else {
54                        // S.T => S must be an aggregate type, find the declaration for T in S.
55                        ast::AggregateDecl const * aggr = nullptr;
56                        ast::BaseInstType const * instp = nullptr;
57                        if ( auto inst = parent.as<ast::StructInstType>() ) {
58                                aggr = inst->base;
59                                instp = inst;
60                        } else if ( auto inst = parent.as<ast::UnionInstType>() ) {
61                                aggr = inst->base;
62                                instp = inst;
63                        } else {
64                                SemanticError( *location, toString("Qualified type requires an aggregate on the left, but has: ", parent) );
65                        }
66                        // TODO: Need to handle forward declarations.
67                        assert( aggr );
68                        for ( ast::ptr<ast::Decl> const & member : aggr->members ) {
69                                if ( auto inst = child.as<ast::TypeInstType>() ) {
70                                        if ( auto decl = member.as<ast::NamedTypeDecl>() ) {
71                                                if ( decl->name == inst->name ) {
72                                                        assert( decl->base );
73                                                        ast::Type * ret = ast::deepCopy( decl->base );
74                                                        ret->qualifiers = type->qualifiers;
75                                                        ast::TypeSubstitution sub( aggr->params, instp->params );
76                                                        auto result = sub.apply(ret);
77                                                        return result.node.release();
78                                                }
79                                        }
80                                } else {
81                                        // S.T - S is not an aggregate => error.
82                                        assertf( false, "unhandled qualified child type: %s", toCString(type) );
83                                }
84                        }
85                        // failed to find a satisfying definition of type
86                        SemanticError( *location, toString("Undefined type in qualified type: ", type) );
87                }
88        }
89
90        ast::Expr const * postvisit( ast::QualifiedNameExpr const * t) {
91                assert( location );
92                if ( t->type_decl ) {
93                auto enumName = t->type_decl->name;
94                const ast::EnumDecl * enumDecl = symtab.lookupEnum( enumName );
95                        for ( ast::ptr<ast::Decl> const & member : enumDecl->members ) {
96                                if ( auto memberAsObj = member.as<ast::ObjectDecl>() ) {
97                                        if ( memberAsObj->name == t->name ) {
98                                                return new ast::VariableExpr( t->location, memberAsObj );
99                                        }
100                                } else {
101                                        assertf( false, "unhandled qualified child type");
102                                }
103                        }
104
105                auto var = new ast::ObjectDecl( t->location, t->name,
106                        new ast::EnumInstType(enumDecl, ast::CV::Const), nullptr, {}, ast::Linkage::Cforall );
107                        var->mangleName = Mangle::mangle( var );
108                        return new ast::VariableExpr( t->location, var );
109        }
110
111                return t;
112        }
113
114};
115
116} // namespace
117
118void fixQualifiedTypes( ast::TranslationUnit & translationUnit ) {
119        ast::Pass<FixQualifiedTypesCore>::run( translationUnit );
120}
121
122} // namespace Validate
123
124// Local Variables: //
125// tab-width: 4 //
126// mode: c++ //
127// compile-command: "make install" //
128// End: //
Note: See TracBrowser for help on using the repository browser.