source: src/ResolvExpr/ResolveTypeof.cc@ 3bc69f2

ADT ast-experimental enum pthread-emulation qualifiedEnum
Last change on this file since 3bc69f2 was 39d8950, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Thread global information through resolution. Non-top-level calls to the resolver have a bit of a hack but improvements would require changes to the Pass helpers.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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// ResolveTypeof.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:12:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Mar 16 16:09:00 2022
13// Update Count : 4
14//
15
16#include "ResolveTypeof.h"
17#include "RenameVars.h"
18
19#include <cassert> // for assert
20
21#include "AST/CVQualifiers.hpp"
22#include "AST/Node.hpp"
23#include "AST/Pass.hpp"
24#include "AST/TranslationUnit.hpp"
25#include "AST/Type.hpp"
26#include "AST/TypeEnvironment.hpp"
27#include "Common/PassVisitor.h" // for PassVisitor
28#include "Common/utility.h" // for copy
29#include "Resolver.h" // for resolveInVoidContext
30#include "SynTree/Expression.h" // for Expression
31#include "SynTree/Mutator.h" // for Mutator
32#include "SynTree/Type.h" // for TypeofType, Type
33#include "SymTab/Mangler.h"
34#include "InitTweak/InitTweak.h" // for isConstExpr
35
36namespace SymTab {
37class Indexer;
38} // namespace SymTab
39
40namespace ResolvExpr {
41 namespace {
42#if 0
43 void
44 printAlts( const AltList &list, std::ostream &os, int indent = 0 )
45 {
46 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
47 i->print( os, indent );
48 os << std::endl;
49 }
50 }
51#endif
52 }
53
54 class ResolveTypeof_old : public WithShortCircuiting {
55 public:
56 ResolveTypeof_old( const SymTab::Indexer &indexer ) : indexer( indexer ) {}
57 void premutate( TypeofType *typeofType );
58 Type * postmutate( TypeofType *typeofType );
59
60 private:
61 const SymTab::Indexer &indexer;
62 };
63
64 Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) {
65 PassVisitor<ResolveTypeof_old> mutator( indexer );
66 return type->acceptMutator( mutator );
67 }
68
69 void ResolveTypeof_old::premutate( TypeofType * ) {
70 visit_children = false;
71 }
72
73 Type * ResolveTypeof_old::postmutate( TypeofType *typeofType ) {
74#if 0
75 std::cerr << "resolving typeof: ";
76 typeofType->print( std::cerr );
77 std::cerr << std::endl;
78#endif
79 // pass on null expression
80 if ( ! typeofType->expr ) return typeofType;
81
82 bool isBasetypeof = typeofType->is_basetypeof;
83 auto oldQuals = typeofType->get_qualifiers().val;
84
85 Type* newType;
86 if ( TypeExpr* tyExpr = dynamic_cast<TypeExpr*>(typeofType->expr) ) {
87 // typeof wrapping type
88 newType = tyExpr->type;
89 tyExpr->type = nullptr;
90 delete tyExpr;
91 } else {
92 // typeof wrapping expression
93 Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer );
94 assert( newExpr->result && ! newExpr->result->isVoid() );
95 newType = newExpr->result;
96 newExpr->result = nullptr;
97 delete typeofType;
98 delete newExpr;
99 }
100
101 // clear qualifiers for base, combine with typeoftype quals in any case
102 if ( isBasetypeof ) {
103 // replace basetypeof(<enum>) by int
104 if ( dynamic_cast<EnumInstType*>(newType) ) {
105 Type* newerType =
106 new BasicType{ newType->get_qualifiers(), BasicType::SignedInt,
107 newType->attributes };
108 delete newType;
109 newType = newerType;
110 }
111 newType->get_qualifiers().val
112 = ( newType->get_qualifiers().val & ~Type::Qualifiers::Mask ) | oldQuals;
113 } else {
114 newType->get_qualifiers().val |= oldQuals;
115 }
116
117 return newType;
118 }
119
120namespace {
121 struct ResolveTypeof_new : public ast::WithShortCircuiting {
122 const ResolveContext & context;
123
124 ResolveTypeof_new( const ResolveContext & context ) :
125 context( context ) {}
126
127 void previsit( const ast::TypeofType * ) { visit_children = false; }
128
129 const ast::Type * postvisit( const ast::TypeofType * typeofType ) {
130 // pass on null expression
131 if ( ! typeofType->expr ) return typeofType;
132
133 ast::ptr< ast::Type > newType;
134 if ( auto tyExpr = typeofType->expr.as< ast::TypeExpr >() ) {
135 // typeof wrapping type
136 newType = tyExpr->type;
137 } else {
138 // typeof wrapping expression
139 ast::TypeEnvironment dummy;
140 ast::ptr< ast::Expr > newExpr =
141 resolveInVoidContext( typeofType->expr, context, dummy );
142 assert( newExpr->result && ! newExpr->result->isVoid() );
143 newType = newExpr->result;
144 }
145
146 // clear qualifiers for base, combine with typeoftype quals regardless
147 if ( typeofType->kind == ast::TypeofType::Basetypeof ) {
148 // replace basetypeof(<enum>) by int
149 if ( newType.as< ast::EnumInstType >() ) {
150 newType = new ast::BasicType{
151 ast::BasicType::SignedInt, newType->qualifiers, copy(newType->attributes) };
152 }
153 reset_qualifiers(
154 newType,
155 ( newType->qualifiers & ~ast::CV::EquivQualifiers ) | typeofType->qualifiers );
156 } else {
157 add_qualifiers( newType, typeofType->qualifiers );
158 }
159
160 return newType.release();
161 }
162 };
163} // anonymous namespace
164
165const ast::Type * resolveTypeof( const ast::Type * type , const ResolveContext & context ) {
166 ast::Pass< ResolveTypeof_new > mutator( context );
167 return type->accept( mutator );
168}
169
170struct FixArrayDimension {
171 // should not require a mutable symbol table - prevent pass template instantiation
172 const ResolveContext & context;
173 FixArrayDimension(const ResolveContext & context) : context( context ) {}
174
175 const ast::ArrayType * previsit (const ast::ArrayType * arrayType) {
176 if (!arrayType->dimension) return arrayType;
177 auto mutType = mutate(arrayType);
178 auto globalSizeType = context.global.sizeType;
179 ast::ptr<ast::Type> sizetype = globalSizeType ? globalSizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt);
180 mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, context );
181
182 if (InitTweak::isConstExpr(mutType->dimension)) {
183 mutType->isVarLen = ast::LengthFlag::FixedLen;
184 }
185 else {
186 mutType->isVarLen = ast::LengthFlag::VariableLen;
187 }
188 return mutType;
189 }
190};
191
192const ast::Type * fixArrayType( const ast::Type * type, const ResolveContext & context ) {
193 ast::Pass<FixArrayDimension> visitor(context);
194 return type->accept(visitor);
195}
196
197const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & context ) {
198 if (!decl->isTypeFixed) {
199 auto mutDecl = mutate(decl);
200 auto resolvedType = resolveTypeof(decl->type, context);
201 resolvedType = fixArrayType(resolvedType, context);
202 mutDecl->type = resolvedType;
203
204 // check variable length if object is an array.
205 // xxx - should this be part of fixObjectType?
206
207 /*
208 if (auto arrayType = dynamic_cast<const ast::ArrayType *>(resolvedType)) {
209 auto dimExpr = findSingleExpression(arrayType->dimension, ast::sizeType, symtab);
210 if (auto varexpr = arrayType->dimension.as<ast::VariableExpr>()) {// hoisted previously
211 if (InitTweak::isConstExpr(varexpr->var.strict_as<ast::ObjectDecl>()->init)) {
212 auto mutType = mutate(arrayType);
213 mutType->isVarLen = ast::LengthFlag::VariableLen;
214 mutDecl->type = mutType;
215 }
216 }
217 }
218 */
219
220
221 if (!mutDecl->name.empty())
222 mutDecl->mangleName = Mangle::mangle(mutDecl); // do not mangle unnamed variables
223
224 mutDecl->type = renameTyVars(mutDecl->type, RenameMode::GEN_EXPR_ID);
225 mutDecl->isTypeFixed = true;
226 return mutDecl;
227 }
228 return decl;
229}
230
231} // namespace ResolvExpr
232
233// Local Variables: //
234// tab-width: 4 //
235// mode: c++ //
236// compile-command: "make install" //
237// End: //
Note: See TracBrowser for help on using the repository browser.