[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 |
---|
| 31 | |
---|
| 32 | namespace SymTab { |
---|
| 33 | class Indexer; |
---|
| 34 | } // namespace SymTab |
---|
[51b7345] | 35 | |
---|
| 36 | namespace ResolvExpr { |
---|
[a08ba92] | 37 | namespace { |
---|
[51b7345] | 38 | #if 0 |
---|
[a32b204] | 39 | void |
---|
| 40 | printAlts( const AltList &list, std::ostream &os, int indent = 0 ) |
---|
| 41 | { |
---|
| 42 | for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { |
---|
| 43 | i->print( os, indent ); |
---|
| 44 | os << std::endl; |
---|
| 45 | } |
---|
| 46 | } |
---|
[51b7345] | 47 | #endif |
---|
[a08ba92] | 48 | } |
---|
[51b7345] | 49 | |
---|
[0f6a7752] | 50 | class ResolveTypeof_old : public WithShortCircuiting { |
---|
[a08ba92] | 51 | public: |
---|
[0f6a7752] | 52 | ResolveTypeof_old( const SymTab::Indexer &indexer ) : indexer( indexer ) {} |
---|
[9d79f93] | 53 | void premutate( TypeofType *typeofType ); |
---|
| 54 | Type * postmutate( TypeofType *typeofType ); |
---|
[51b7345] | 55 | |
---|
[a08ba92] | 56 | private: |
---|
[a32b204] | 57 | const SymTab::Indexer &indexer; |
---|
[a08ba92] | 58 | }; |
---|
[51b7345] | 59 | |
---|
[9d79f93] | 60 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) { |
---|
[0f6a7752] | 61 | PassVisitor<ResolveTypeof_old> mutator( indexer ); |
---|
[a32b204] | 62 | return type->acceptMutator( mutator ); |
---|
[a08ba92] | 63 | } |
---|
[51b7345] | 64 | |
---|
[0f6a7752] | 65 | void ResolveTypeof_old::premutate( TypeofType * ) { |
---|
[9d79f93] | 66 | visit_children = false; |
---|
| 67 | } |
---|
| 68 | |
---|
[0f6a7752] | 69 | Type * ResolveTypeof_old::postmutate( TypeofType *typeofType ) { |
---|
[d9a0e76] | 70 | #if 0 |
---|
[9d79f93] | 71 | std::cerr << "resolving typeof: "; |
---|
| 72 | typeofType->print( std::cerr ); |
---|
| 73 | std::cerr << std::endl; |
---|
[d9a0e76] | 74 | #endif |
---|
[f441c88] | 75 | // pass on null expression |
---|
| 76 | if ( ! typeofType->expr ) return typeofType; |
---|
| 77 | |
---|
| 78 | bool isBasetypeof = typeofType->is_basetypeof; |
---|
| 79 | auto oldQuals = typeofType->get_qualifiers().val; |
---|
| 80 | |
---|
| 81 | Type* newType; |
---|
| 82 | if ( TypeExpr* tyExpr = dynamic_cast<TypeExpr*>(typeofType->expr) ) { |
---|
| 83 | // typeof wrapping type |
---|
| 84 | newType = tyExpr->type; |
---|
| 85 | tyExpr->type = nullptr; |
---|
| 86 | delete tyExpr; |
---|
| 87 | } else { |
---|
| 88 | // typeof wrapping expression |
---|
[9d79f93] | 89 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer ); |
---|
| 90 | assert( newExpr->result && ! newExpr->result->isVoid() ); |
---|
[f441c88] | 91 | newType = newExpr->result; |
---|
[9d79f93] | 92 | newExpr->result = nullptr; |
---|
[a32b204] | 93 | delete typeofType; |
---|
[c93bc28] | 94 | delete newExpr; |
---|
[f441c88] | 95 | } |
---|
| 96 | |
---|
| 97 | // clear qualifiers for base, combine with typeoftype quals in any case |
---|
| 98 | if ( isBasetypeof ) { |
---|
[8e04794] | 99 | // replace basetypeof(<enum>) by int |
---|
| 100 | if ( dynamic_cast<EnumInstType*>(newType) ) { |
---|
| 101 | Type* newerType = |
---|
| 102 | new BasicType{ newType->get_qualifiers(), BasicType::SignedInt, |
---|
| 103 | newType->attributes }; |
---|
| 104 | delete newType; |
---|
| 105 | newType = newerType; |
---|
| 106 | } |
---|
[f441c88] | 107 | newType->get_qualifiers().val |
---|
| 108 | = ( newType->get_qualifiers().val & ~Type::Qualifiers::Mask ) | oldQuals; |
---|
| 109 | } else { |
---|
| 110 | newType->get_qualifiers().val |= oldQuals; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | return newType; |
---|
[a08ba92] | 114 | } |
---|
[c8e4d2f8] | 115 | |
---|
[0f6a7752] | 116 | namespace { |
---|
| 117 | struct ResolveTypeof_new : public ast::WithShortCircuiting { |
---|
| 118 | const ast::SymbolTable & localSymtab; |
---|
| 119 | |
---|
| 120 | ResolveTypeof_new( const ast::SymbolTable & syms ) : localSymtab( syms ) {} |
---|
| 121 | |
---|
| 122 | void premutate( const ast::TypeofType * ) { visit_children = false; } |
---|
| 123 | |
---|
| 124 | const ast::Type * postmutate( const ast::TypeofType * typeofType ) { |
---|
| 125 | // pass on null expression |
---|
| 126 | if ( ! typeofType->expr ) return typeofType; |
---|
| 127 | |
---|
| 128 | ast::ptr< ast::Type > newType; |
---|
| 129 | if ( auto tyExpr = typeofType->expr.as< ast::TypeExpr >() ) { |
---|
| 130 | // typeof wrapping type |
---|
| 131 | newType = tyExpr->type; |
---|
| 132 | } else { |
---|
| 133 | // typeof wrapping expression |
---|
| 134 | ast::TypeEnvironment dummy; |
---|
| 135 | ast::ptr< ast::Expr > newExpr = |
---|
| 136 | resolveInVoidContext( typeofType->expr, localSymtab, dummy ); |
---|
| 137 | assert( newExpr->result && ! newExpr->result->isVoid() ); |
---|
| 138 | newType = newExpr->result; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | // clear qualifiers for base, combine with typeoftype quals regardless |
---|
| 142 | if ( typeofType->kind == ast::TypeofType::Basetypeof ) { |
---|
| 143 | // replace basetypeof(<enum>) by int |
---|
| 144 | if ( newType.as< ast::EnumInstType >() ) { |
---|
| 145 | newType = new ast::BasicType{ |
---|
| 146 | ast::BasicType::SignedInt, newType->qualifiers, copy(newType->attributes) }; |
---|
| 147 | } |
---|
| 148 | reset_qualifiers( |
---|
| 149 | newType, |
---|
| 150 | ( newType->qualifiers & ~ast::CV::EquivQualifiers ) | typeofType->qualifiers ); |
---|
| 151 | } else { |
---|
| 152 | add_qualifiers( newType, typeofType->qualifiers ); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return newType; |
---|
| 156 | } |
---|
| 157 | }; |
---|
| 158 | } // anonymous namespace |
---|
| 159 | |
---|
| 160 | const ast::Type * resolveTypeof( const ast::Type * type , const ast::SymbolTable & symtab ) { |
---|
| 161 | ast::Pass< ResolveTypeof_new > mutator{ symtab }; |
---|
| 162 | return type->accept( mutator ); |
---|
| 163 | } |
---|
| 164 | |
---|
[51b7345] | 165 | } // namespace ResolvExpr |
---|
[a32b204] | 166 | |
---|
| 167 | // Local Variables: // |
---|
| 168 | // tab-width: 4 // |
---|
| 169 | // mode: c++ // |
---|
| 170 | // compile-command: "make install" // |
---|
| 171 | // End: // |
---|