| 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 | //
|
|---|
| 7 | // ResolveTypeof.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Sun May 17 12:12:20 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Tue May 19 16:49:04 2015
|
|---|
| 13 | // Update Count : 3
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "ResolveTypeof.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert> // for assert
|
|---|
| 19 |
|
|---|
| 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"
|
|---|
| 25 | #include "Common/PassVisitor.h" // for PassVisitor
|
|---|
| 26 | #include "Common/utility.h" // for copy
|
|---|
| 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
|
|---|
| 35 |
|
|---|
| 36 | namespace ResolvExpr {
|
|---|
| 37 | namespace {
|
|---|
| 38 | #if 0
|
|---|
| 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 | }
|
|---|
| 47 | #endif
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | class ResolveTypeof_old : public WithShortCircuiting {
|
|---|
| 51 | public:
|
|---|
| 52 | ResolveTypeof_old( const SymTab::Indexer &indexer ) : indexer( indexer ) {}
|
|---|
| 53 | void premutate( TypeofType *typeofType );
|
|---|
| 54 | Type * postmutate( TypeofType *typeofType );
|
|---|
| 55 |
|
|---|
| 56 | private:
|
|---|
| 57 | const SymTab::Indexer &indexer;
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) {
|
|---|
| 61 | PassVisitor<ResolveTypeof_old> mutator( indexer );
|
|---|
| 62 | return type->acceptMutator( mutator );
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void ResolveTypeof_old::premutate( TypeofType * ) {
|
|---|
| 66 | visit_children = false;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | Type * ResolveTypeof_old::postmutate( TypeofType *typeofType ) {
|
|---|
| 70 | #if 0
|
|---|
| 71 | std::cerr << "resolving typeof: ";
|
|---|
| 72 | typeofType->print( std::cerr );
|
|---|
| 73 | std::cerr << std::endl;
|
|---|
| 74 | #endif
|
|---|
| 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
|
|---|
| 89 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer );
|
|---|
| 90 | assert( newExpr->result && ! newExpr->result->isVoid() );
|
|---|
| 91 | newType = newExpr->result;
|
|---|
| 92 | newExpr->result = nullptr;
|
|---|
| 93 | delete typeofType;
|
|---|
| 94 | delete newExpr;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | // clear qualifiers for base, combine with typeoftype quals in any case
|
|---|
| 98 | if ( isBasetypeof ) {
|
|---|
| 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 | }
|
|---|
| 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;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 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 previsit( const ast::TypeofType * ) { visit_children = false; }
|
|---|
| 123 |
|
|---|
| 124 | const ast::Type * postvisit( 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.release();
|
|---|
| 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 |
|
|---|
| 165 | } // namespace ResolvExpr
|
|---|
| 166 |
|
|---|
| 167 | // Local Variables: //
|
|---|
| 168 | // tab-width: 4 //
|
|---|
| 169 | // mode: c++ //
|
|---|
| 170 | // compile-command: "make install" //
|
|---|
| 171 | // End: //
|
|---|