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