source: src/Validate/FixQualifiedTypes.cpp@ 7ae39f0

ast-experimental
Last change on this file since 7ae39f0 was bccd70a, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Removed internal code from TypeSubstitution header. It caused a chain of include problems, which have been corrected.

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