source: src/Validate/FixQualifiedTypes.cpp @ fa2c005

ADT
Last change on this file since fa2c005 was fa2c005, checked in by JiadaL <j82liang@…>, 12 months ago

Finish Adt POC

  • Property mode set to 100644
File size: 4.2 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 if ( auto inst = parent.as<ast::AdtInstType>() ) {
64                                aggr = inst->base;
65                                instp = inst;
66                        } else {
67                                SemanticError( *location, toString("Qualified type requires an aggregate on the left, but has: ", parent) );
68                        }
69                        // TODO: Need to handle forward declarations.
70                        assert( aggr );
71                        for ( ast::ptr<ast::Decl> const & member : aggr->members ) {
72                                if ( auto inst = child.as<ast::TypeInstType>() ) {
73                                        if ( auto decl = member.as<ast::NamedTypeDecl>() ) {
74                                                if ( decl->name == inst->name ) {
75                                                        assert( decl->base );
76                                                        ast::Type * ret = ast::deepCopy( decl->base );
77                                                        ret->qualifiers = type->qualifiers;
78                                                        ast::TypeSubstitution sub( aggr->params, instp->params );
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        ast::Expr const * postvisit( ast::QualifiedNameExpr const * t) {
94                assert( location );
95                if ( t->type_decl ) {
96                auto enumName = t->type_decl->name;
97                const ast::EnumDecl * enumDecl = symtab.lookupEnum( enumName );
98                        for ( ast::ptr<ast::Decl> const & member : enumDecl->members ) {
99                                if ( auto memberAsObj = member.as<ast::ObjectDecl>() ) {
100                                        if ( memberAsObj->name == t->name ) {
101                                                return new ast::VariableExpr( t->location, memberAsObj );
102                                        }
103                                } else {
104                                        assertf( false, "unhandled qualified child type");
105                                }
106                        }
107
108                auto var = new ast::ObjectDecl( t->location, t->name,
109                        new ast::EnumInstType(enumDecl, ast::CV::Const), nullptr, {}, ast::Linkage::Cforall );
110                        var->mangleName = Mangle::mangle( var );
111                        return new ast::VariableExpr( t->location, var );
112        }
113
114                return t;
115        }
116
117};
118
119} // namespace
120
121void fixQualifiedTypes( ast::TranslationUnit & translationUnit ) {
122        ast::Pass<FixQualifiedTypesCore>::run( translationUnit );
123}
124
125} // namespace Validate
126
127// Local Variables: //
128// tab-width: 4 //
129// mode: c++ //
130// compile-command: "make install" //
131// End: //
Note: See TracBrowser for help on using the repository browser.