source: src/ResolvExpr/ResolveTypeof.cc @ 2dda05d

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2dda05d was 0dd9a5e, checked in by Fangren Yu <f37yu@…>, 4 years ago

delay autogen resolve

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