source: src/Validate/FixQualifiedTypes.cpp@ 4520b77e

ADT ast-experimental pthread-emulation
Last change on this file since 4520b77e was b0d9ff7, checked in by JiadaL <j82liang@…>, 3 years ago

Fix up the QualifiedNameExpr. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

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