[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 |
---|
[39d8950] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Mar 16 16:09:00 2022 |
---|
| 13 | // Update Count : 4 |
---|
[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" |
---|
[39d8950] | 24 | #include "AST/TranslationUnit.hpp" |
---|
[0f6a7752] | 25 | #include "AST/Type.hpp" |
---|
| 26 | #include "AST/TypeEnvironment.hpp" |
---|
[9d79f93] | 27 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[0f6a7752] | 28 | #include "Common/utility.h" // for copy |
---|
[ea6332d] | 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 |
---|
[16ba4a6] | 33 | #include "SymTab/Mangler.h" |
---|
| 34 | #include "InitTweak/InitTweak.h" // for isConstExpr |
---|
[ea6332d] | 35 | |
---|
| 36 | namespace SymTab { |
---|
| 37 | class Indexer; |
---|
| 38 | } // namespace SymTab |
---|
[51b7345] | 39 | |
---|
| 40 | namespace ResolvExpr { |
---|
[a08ba92] | 41 | namespace { |
---|
[51b7345] | 42 | #if 0 |
---|
[a32b204] | 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 | } |
---|
[51b7345] | 51 | #endif |
---|
[a08ba92] | 52 | } |
---|
[51b7345] | 53 | |
---|
[0f6a7752] | 54 | class ResolveTypeof_old : public WithShortCircuiting { |
---|
[a08ba92] | 55 | public: |
---|
[0f6a7752] | 56 | ResolveTypeof_old( const SymTab::Indexer &indexer ) : indexer( indexer ) {} |
---|
[9d79f93] | 57 | void premutate( TypeofType *typeofType ); |
---|
| 58 | Type * postmutate( TypeofType *typeofType ); |
---|
[51b7345] | 59 | |
---|
[a08ba92] | 60 | private: |
---|
[a32b204] | 61 | const SymTab::Indexer &indexer; |
---|
[a08ba92] | 62 | }; |
---|
[51b7345] | 63 | |
---|
[9d79f93] | 64 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) { |
---|
[0f6a7752] | 65 | PassVisitor<ResolveTypeof_old> mutator( indexer ); |
---|
[a32b204] | 66 | return type->acceptMutator( mutator ); |
---|
[a08ba92] | 67 | } |
---|
[51b7345] | 68 | |
---|
[0f6a7752] | 69 | void ResolveTypeof_old::premutate( TypeofType * ) { |
---|
[9d79f93] | 70 | visit_children = false; |
---|
| 71 | } |
---|
| 72 | |
---|
[0f6a7752] | 73 | Type * ResolveTypeof_old::postmutate( TypeofType *typeofType ) { |
---|
[d9a0e76] | 74 | #if 0 |
---|
[9d79f93] | 75 | std::cerr << "resolving typeof: "; |
---|
| 76 | typeofType->print( std::cerr ); |
---|
| 77 | std::cerr << std::endl; |
---|
[d9a0e76] | 78 | #endif |
---|
[f441c88] | 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 |
---|
[9d79f93] | 93 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer ); |
---|
| 94 | assert( newExpr->result && ! newExpr->result->isVoid() ); |
---|
[f441c88] | 95 | newType = newExpr->result; |
---|
[9d79f93] | 96 | newExpr->result = nullptr; |
---|
[a32b204] | 97 | delete typeofType; |
---|
[c93bc28] | 98 | delete newExpr; |
---|
[f441c88] | 99 | } |
---|
| 100 | |
---|
| 101 | // clear qualifiers for base, combine with typeoftype quals in any case |
---|
| 102 | if ( isBasetypeof ) { |
---|
[8e04794] | 103 | // replace basetypeof(<enum>) by int |
---|
| 104 | if ( dynamic_cast<EnumInstType*>(newType) ) { |
---|
[a6f26ca] | 105 | Type* newerType = |
---|
| 106 | new BasicType{ newType->get_qualifiers(), BasicType::SignedInt, |
---|
[8e04794] | 107 | newType->attributes }; |
---|
| 108 | delete newType; |
---|
| 109 | newType = newerType; |
---|
| 110 | } |
---|
[a6f26ca] | 111 | newType->get_qualifiers().val |
---|
[f441c88] | 112 | = ( newType->get_qualifiers().val & ~Type::Qualifiers::Mask ) | oldQuals; |
---|
| 113 | } else { |
---|
| 114 | newType->get_qualifiers().val |= oldQuals; |
---|
| 115 | } |
---|
[a6f26ca] | 116 | |
---|
[f441c88] | 117 | return newType; |
---|
[a08ba92] | 118 | } |
---|
[c8e4d2f8] | 119 | |
---|
[0f6a7752] | 120 | namespace { |
---|
| 121 | struct ResolveTypeof_new : public ast::WithShortCircuiting { |
---|
[39d8950] | 122 | const ResolveContext & context; |
---|
[0f6a7752] | 123 | |
---|
[39d8950] | 124 | ResolveTypeof_new( const ResolveContext & context ) : |
---|
| 125 | context( context ) {} |
---|
[0f6a7752] | 126 | |
---|
[a6f26ca] | 127 | void previsit( const ast::TypeofType * ) { visit_children = false; } |
---|
[0f6a7752] | 128 | |
---|
[a6f26ca] | 129 | const ast::Type * postvisit( const ast::TypeofType * typeofType ) { |
---|
[0f6a7752] | 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; |
---|
[a6f26ca] | 140 | ast::ptr< ast::Expr > newExpr = |
---|
[39d8950] | 141 | resolveInVoidContext( typeofType->expr, context, dummy ); |
---|
[0f6a7752] | 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 >() ) { |
---|
[a6f26ca] | 150 | newType = new ast::BasicType{ |
---|
[0f6a7752] | 151 | ast::BasicType::SignedInt, newType->qualifiers, copy(newType->attributes) }; |
---|
| 152 | } |
---|
[a6f26ca] | 153 | reset_qualifiers( |
---|
| 154 | newType, |
---|
[0f6a7752] | 155 | ( newType->qualifiers & ~ast::CV::EquivQualifiers ) | typeofType->qualifiers ); |
---|
| 156 | } else { |
---|
| 157 | add_qualifiers( newType, typeofType->qualifiers ); |
---|
| 158 | } |
---|
| 159 | |
---|
[417117e] | 160 | return newType.release(); |
---|
[0f6a7752] | 161 | } |
---|
| 162 | }; |
---|
| 163 | } // anonymous namespace |
---|
| 164 | |
---|
[39d8950] | 165 | const ast::Type * resolveTypeof( const ast::Type * type , const ResolveContext & context ) { |
---|
| 166 | ast::Pass< ResolveTypeof_new > mutator( context ); |
---|
[0f6a7752] | 167 | return type->accept( mutator ); |
---|
| 168 | } |
---|
| 169 | |
---|
[0dd9a5e] | 170 | struct FixArrayDimension { |
---|
[39d8950] | 171 | const ResolveContext & context; |
---|
| 172 | FixArrayDimension(const ResolveContext & context) : context( context ) {} |
---|
[0dd9a5e] | 173 | |
---|
| 174 | const ast::ArrayType * previsit (const ast::ArrayType * arrayType) { |
---|
| 175 | if (!arrayType->dimension) return arrayType; |
---|
| 176 | auto mutType = mutate(arrayType); |
---|
[39d8950] | 177 | auto globalSizeType = context.global.sizeType; |
---|
| 178 | ast::ptr<ast::Type> sizetype = globalSizeType ? globalSizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt); |
---|
| 179 | mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, context ); |
---|
[0dd9a5e] | 180 | |
---|
| 181 | if (InitTweak::isConstExpr(mutType->dimension)) { |
---|
| 182 | mutType->isVarLen = ast::LengthFlag::FixedLen; |
---|
| 183 | } |
---|
| 184 | else { |
---|
| 185 | mutType->isVarLen = ast::LengthFlag::VariableLen; |
---|
| 186 | } |
---|
| 187 | return mutType; |
---|
| 188 | } |
---|
| 189 | }; |
---|
| 190 | |
---|
[39d8950] | 191 | const ast::Type * fixArrayType( const ast::Type * type, const ResolveContext & context ) { |
---|
| 192 | ast::Pass<FixArrayDimension> visitor(context); |
---|
[0dd9a5e] | 193 | return type->accept(visitor); |
---|
| 194 | } |
---|
| 195 | |
---|
[39d8950] | 196 | const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & context ) { |
---|
[94fa946] | 197 | if (decl->isTypeFixed) { |
---|
| 198 | return decl; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | auto mutDecl = mutate(decl); |
---|
| 202 | { |
---|
[39d8950] | 203 | auto resolvedType = resolveTypeof(decl->type, context); |
---|
| 204 | resolvedType = fixArrayType(resolvedType, context); |
---|
[16ba4a6] | 205 | mutDecl->type = resolvedType; |
---|
[94fa946] | 206 | } |
---|
[16ba4a6] | 207 | |
---|
[94fa946] | 208 | // Do not mangle unnamed variables. |
---|
| 209 | if (!mutDecl->name.empty()) { |
---|
| 210 | mutDecl->mangleName = Mangle::mangle(mutDecl); |
---|
[16ba4a6] | 211 | } |
---|
[94fa946] | 212 | |
---|
| 213 | mutDecl->type = renameTyVars(mutDecl->type, RenameMode::GEN_EXPR_ID); |
---|
| 214 | mutDecl->isTypeFixed = true; |
---|
| 215 | return mutDecl; |
---|
[16ba4a6] | 216 | } |
---|
| 217 | |
---|
[51b7345] | 218 | } // namespace ResolvExpr |
---|
[a32b204] | 219 | |
---|
| 220 | // Local Variables: // |
---|
| 221 | // tab-width: 4 // |
---|
| 222 | // mode: c++ // |
---|
| 223 | // compile-command: "make install" // |
---|
| 224 | // End: // |
---|