[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" |
---|
[ea6332d] | 17 | |
---|
[92355883] | 18 | #include <cassert> // for assert |
---|
[ea6332d] | 19 | |
---|
[0f6a7752] | 20 | #include "AST/CVQualifiers.hpp" |
---|
| 21 | #include "AST/Node.hpp" |
---|
| 22 | #include "AST/Pass.hpp" |
---|
[39d8950] | 23 | #include "AST/TranslationUnit.hpp" |
---|
[0f6a7752] | 24 | #include "AST/Type.hpp" |
---|
| 25 | #include "AST/TypeEnvironment.hpp" |
---|
[62c6cfa] | 26 | #include "Common/utility.h" // for copy |
---|
| 27 | #include "InitTweak/InitTweak.h" // for isConstExpr |
---|
[92355883] | 28 | #include "RenameVars.h" |
---|
| 29 | #include "Resolver.h" // for resolveInVoidContext |
---|
| 30 | #include "SymTab/Mangler.h" |
---|
[51b7345] | 31 | |
---|
| 32 | namespace ResolvExpr { |
---|
[c8e4d2f8] | 33 | |
---|
[0f6a7752] | 34 | namespace { |
---|
[2908f08] | 35 | |
---|
[0bd3faf] | 36 | struct ResolveTypeof : public ast::WithShortCircuiting { |
---|
[f431ab26] | 37 | const ResolveContext & context; |
---|
| 38 | |
---|
| 39 | ResolveTypeof( const ResolveContext & context ) : context( context ) {} |
---|
| 40 | |
---|
| 41 | void previsit( const ast::TypeofType * ) { visit_children = false; } |
---|
| 42 | |
---|
| 43 | const ast::Type * postvisit( const ast::TypeofType * typeofType ) { |
---|
| 44 | // pass on null expression |
---|
| 45 | if ( ! typeofType->expr ) return typeofType; |
---|
| 46 | |
---|
| 47 | ast::ptr< ast::Type > newType; |
---|
| 48 | if ( auto tyExpr = typeofType->expr.as< ast::TypeExpr >() ) { |
---|
| 49 | // typeof wrapping type |
---|
| 50 | newType = tyExpr->type; |
---|
| 51 | } else { |
---|
| 52 | // typeof wrapping expression |
---|
| 53 | ast::TypeEnvironment dummy; |
---|
| 54 | ast::ptr< ast::Expr > newExpr = |
---|
| 55 | resolveInVoidContext( typeofType->expr, context, dummy ); |
---|
| 56 | assert( newExpr->result && ! newExpr->result->isVoid() ); |
---|
| 57 | newType = newExpr->result; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // clear qualifiers for base, combine with typeoftype quals regardless |
---|
| 61 | if ( typeofType->kind == ast::TypeofType::Basetypeof ) { |
---|
| 62 | // replace basetypeof(<enum>) by int |
---|
| 63 | if ( newType.as< ast::EnumInstType >() ) { |
---|
| 64 | newType = new ast::BasicType( |
---|
| 65 | ast::BasicType::SignedInt, newType->qualifiers, copy(newType->attributes) ); |
---|
| 66 | } |
---|
| 67 | reset_qualifiers( |
---|
| 68 | newType, |
---|
| 69 | ( newType->qualifiers & ~ast::CV::EquivQualifiers ) | typeofType->qualifiers ); |
---|
| 70 | } else { |
---|
| 71 | add_qualifiers( newType, typeofType->qualifiers ); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | return newType.release(); |
---|
| 75 | } |
---|
[92355883] | 76 | }; |
---|
[2908f08] | 77 | |
---|
[62c6cfa] | 78 | } // anonymous namespace |
---|
[0f6a7752] | 79 | |
---|
[62c6cfa] | 80 | const ast::Type * resolveTypeof( const ast::Type * type , const ResolveContext & context ) { |
---|
[0bd3faf] | 81 | ast::Pass< ResolveTypeof > mutator( context ); |
---|
[62c6cfa] | 82 | return type->accept( mutator ); |
---|
[0f6a7752] | 83 | } |
---|
| 84 | |
---|
[0dd9a5e] | 85 | struct FixArrayDimension { |
---|
[62c6cfa] | 86 | const ResolveContext & context; |
---|
| 87 | FixArrayDimension(const ResolveContext & context) : context( context ) {} |
---|
| 88 | |
---|
| 89 | const ast::ArrayType * previsit (const ast::ArrayType * arrayType) { |
---|
| 90 | if (!arrayType->dimension) return arrayType; |
---|
| 91 | auto mutType = mutate(arrayType); |
---|
| 92 | auto globalSizeType = context.global.sizeType; |
---|
| 93 | ast::ptr<ast::Type> sizetype = globalSizeType ? globalSizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt); |
---|
| 94 | mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, context ); |
---|
| 95 | |
---|
| 96 | if (InitTweak::isConstExpr(mutType->dimension)) { |
---|
| 97 | mutType->isVarLen = ast::LengthFlag::FixedLen; |
---|
| 98 | } |
---|
| 99 | else { |
---|
| 100 | mutType->isVarLen = ast::LengthFlag::VariableLen; |
---|
| 101 | } |
---|
| 102 | return mutType; |
---|
| 103 | } |
---|
[c1e66d9] | 104 | }; |
---|
| 105 | |
---|
[62c6cfa] | 106 | const ast::Type * fixArrayType( const ast::Type * type, const ResolveContext & context ) { |
---|
| 107 | ast::Pass<FixArrayDimension> visitor(context); |
---|
| 108 | return type->accept(visitor); |
---|
[0dd9a5e] | 109 | } |
---|
| 110 | |
---|
[62c6cfa] | 111 | const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & context ) { |
---|
[f431ab26] | 112 | if ( decl->isTypeFixed ) { |
---|
| 113 | return decl; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | auto mutDecl = mutate(decl); |
---|
| 117 | fixObjectInit(decl, context); |
---|
| 118 | { |
---|
| 119 | auto resolvedType = resolveTypeof(decl->type, context); |
---|
| 120 | resolvedType = fixArrayType(resolvedType, context); |
---|
| 121 | mutDecl->type = resolvedType; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // Do not mangle unnamed variables. |
---|
| 125 | if ( !mutDecl->name.empty() ) { |
---|
| 126 | mutDecl->mangleName = Mangle::mangle(mutDecl); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | mutDecl->type = renameTyVars(mutDecl->type, RenameMode::GEN_EXPR_ID); |
---|
| 130 | mutDecl->isTypeFixed = true; |
---|
| 131 | return mutDecl; |
---|
[92355883] | 132 | } |
---|
[94fa946] | 133 | |
---|
[f431ab26] | 134 | const ast::ObjectDecl *fixObjectInit( |
---|
| 135 | const ast::ObjectDecl *decl, const ResolveContext &context) { |
---|
| 136 | if ( decl->isTypeFixed ) { |
---|
| 137 | return decl; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if ( auto listInit = decl->init.as<ast::ListInit>() ) { |
---|
| 141 | for ( size_t k = 0; k < listInit->designations.size(); k++ ) { |
---|
| 142 | const ast::Designation *des = listInit->designations[k].get(); |
---|
| 143 | // Desination here |
---|
| 144 | ast::Designation * newDesignation = new ast::Designation(des->location); |
---|
| 145 | std::deque<ast::ptr<ast::Expr>> newDesignators; |
---|
| 146 | |
---|
| 147 | for ( ast::ptr<ast::Expr> designator : des->designators ) { |
---|
| 148 | // Stupid flag variable for development, to be removed |
---|
| 149 | if ( const ast::NameExpr * designatorName = designator.as<ast::NameExpr>() ) { |
---|
| 150 | auto candidates = context.symtab.lookupId(designatorName->name); |
---|
| 151 | // Does not work for the overloading case currently |
---|
| 152 | // assert( candidates.size() == 1 ); |
---|
| 153 | if ( candidates.size() != 1 ) return decl; |
---|
| 154 | auto candidate = candidates.at(0); |
---|
| 155 | if ( const ast::EnumInstType * enumInst = dynamic_cast<const ast::EnumInstType *>(candidate.id->get_type())) { |
---|
| 156 | // determine that is an enumInst, swap it with its const value |
---|
| 157 | assert( candidates.size() == 1 ); |
---|
| 158 | const ast::EnumDecl * baseEnum = enumInst->base; |
---|
| 159 | // Need to iterate over all enum value to find the initializer to swap |
---|
| 160 | for ( size_t m = 0; m < baseEnum->members.size(); ++m ) { |
---|
| 161 | const ast::ObjectDecl * mem = baseEnum->members.at(m).as<const ast::ObjectDecl>(); |
---|
| 162 | if ( baseEnum->members.at(m)->name == designatorName->name ) { |
---|
| 163 | assert( mem ); |
---|
| 164 | newDesignators.push_back( ast::ConstantExpr::from_int(designator->location, m) ); |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } else { |
---|
| 169 | newDesignators.push_back( des->designators.at(0) ); |
---|
| 170 | } |
---|
| 171 | } else { |
---|
| 172 | newDesignators.push_back( des->designators.at(0) ); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | newDesignation->designators = newDesignators; |
---|
| 176 | listInit = ast::mutate_field_index(listInit, &ast::ListInit::designations, k, newDesignation); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | return decl; |
---|
[16ba4a6f] | 180 | } |
---|
| 181 | |
---|
[f431ab26] | 182 | } // namespace ResolvExpr |
---|
[a32b204] | 183 | |
---|
| 184 | // Local Variables: // |
---|
| 185 | // tab-width: 4 // |
---|
| 186 | // mode: c++ // |
---|
| 187 | // compile-command: "make install" // |
---|
| 188 | // End: // |
---|