| 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 | // Resolver.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Aaron B. Moss
 | 
|---|
| 10 | // Created On       : Sun May 17 12:17:01 2015
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Wed Apr 20 10:41:00 2022
 | 
|---|
| 13 | // Update Count     : 248
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <cassert>                       // for strict_dynamic_cast, assert
 | 
|---|
| 17 | #include <memory>                        // for allocator, allocator_traits<...
 | 
|---|
| 18 | #include <tuple>                         // for get
 | 
|---|
| 19 | #include <vector>                        // for vector
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Candidate.hpp"
 | 
|---|
| 22 | #include "CandidateFinder.hpp"
 | 
|---|
| 23 | #include "CurrentObject.h"               // for CurrentObject
 | 
|---|
| 24 | #include "RenameVars.h"                  // for RenameVars, global_renamer
 | 
|---|
| 25 | #include "Resolver.h"
 | 
|---|
| 26 | #include "ResolveTypeof.h"
 | 
|---|
| 27 | #include "ResolveMode.hpp"               // for ResolveMode
 | 
|---|
| 28 | #include "typeops.h"                     // for extractResultType
 | 
|---|
| 29 | #include "Unify.h"                       // for unify
 | 
|---|
| 30 | #include "CompilationState.h"
 | 
|---|
| 31 | #include "AST/Decl.hpp"
 | 
|---|
| 32 | #include "AST/Init.hpp"
 | 
|---|
| 33 | #include "AST/Pass.hpp"
 | 
|---|
| 34 | #include "AST/Print.hpp"
 | 
|---|
| 35 | #include "AST/SymbolTable.hpp"
 | 
|---|
| 36 | #include "AST/Type.hpp"
 | 
|---|
| 37 | #include "Common/Eval.h"                 // for eval
 | 
|---|
| 38 | #include "Common/Iterate.hpp"            // for group_iterate
 | 
|---|
| 39 | #include "Common/SemanticError.h"        // for SemanticError
 | 
|---|
| 40 | #include "Common/Stats/ResolveTime.h"    // for ResolveTime::start(), ResolveTime::stop()
 | 
|---|
| 41 | #include "Common/ToString.hpp"           // for toCString
 | 
|---|
| 42 | #include "Common/UniqueName.h"           // for UniqueName
 | 
|---|
| 43 | #include "InitTweak/GenInit.h"
 | 
|---|
| 44 | #include "InitTweak/InitTweak.h"         // for isIntrinsicSingleArgCallStmt
 | 
|---|
| 45 | #include "SymTab/Mangler.h"              // for Mangler
 | 
|---|
| 46 | #include "Tuples/Tuples.h"
 | 
|---|
| 47 | #include "Validate/FindSpecialDecls.h"   // for SizeType
 | 
|---|
| 48 | 
 | 
|---|
| 49 | using namespace std;
 | 
|---|
| 50 | 
 | 
|---|
| 51 | namespace ResolvExpr {
 | 
|---|
| 52 |         template< typename iterator_t >
 | 
|---|
| 53 |         inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) {
 | 
|---|
| 54 |                 while( it != end && !(*it)->get_type()->get_mutex() ) {
 | 
|---|
| 55 |                         it++;
 | 
|---|
| 56 |                 }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |                 return it != end;
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         namespace {
 | 
|---|
| 62 |                 /// Finds deleted expressions in an expression tree
 | 
|---|
| 63 |                 struct DeleteFinder final : public ast::WithShortCircuiting, public ast::WithVisitorRef<DeleteFinder> {
 | 
|---|
| 64 |                         const ast::DeletedExpr * result = nullptr;
 | 
|---|
| 65 | 
 | 
|---|
| 66 |                         void previsit( const ast::DeletedExpr * expr ) {
 | 
|---|
| 67 |                                 if ( result ) { visit_children = false; }
 | 
|---|
| 68 |                                 else { result = expr; }
 | 
|---|
| 69 |                         }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                         void previsit( const ast::Expr * expr ) {
 | 
|---|
| 72 |                                 if ( result ) { visit_children = false; }
 | 
|---|
| 73 |                                 if (expr->inferred.hasParams()) {
 | 
|---|
| 74 |                                         for (auto & imp : expr->inferred.inferParams() ) {
 | 
|---|
| 75 |                                                 imp.second.expr->accept(*visitor);
 | 
|---|
| 76 |                                         }
 | 
|---|
| 77 |                                 }
 | 
|---|
| 78 |                         }
 | 
|---|
| 79 |                 };
 | 
|---|
| 80 | 
 | 
|---|
| 81 |                 struct ResolveDesignators final : public ast::WithShortCircuiting {
 | 
|---|
| 82 |                         ResolveContext& context;
 | 
|---|
| 83 |                         bool result = false;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                         ResolveDesignators( ResolveContext& _context ): context{_context} {};
 | 
|---|
| 86 | 
 | 
|---|
| 87 |                         void previsit( const ast::Node * ) {
 | 
|---|
| 88 |                                 // short circuit if we already know there are designations
 | 
|---|
| 89 |                                 if ( result ) visit_children = false;
 | 
|---|
| 90 |                         }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |                         void previsit( const ast::Designation * des ) {
 | 
|---|
| 93 |                                 if ( result ) visit_children = false;
 | 
|---|
| 94 |                                 else if ( ! des->designators.empty() ) {
 | 
|---|
| 95 |                                         if ( (des->designators.size() == 1) ) {
 | 
|---|
| 96 |                                                 const ast::Expr * designator = des->designators.at(0);
 | 
|---|
| 97 |                                                 if ( const ast::NameExpr * designatorName = dynamic_cast<const ast::NameExpr *>(designator) ) {
 | 
|---|
| 98 |                                                         auto candidates = context.symtab.lookupId(designatorName->name);
 | 
|---|
| 99 |                                                         for ( auto candidate : candidates ) {
 | 
|---|
| 100 |                                                                 if ( dynamic_cast<const ast::EnumInstType *>(candidate.id->get_type()) ) {
 | 
|---|
| 101 |                                                                         result = true;
 | 
|---|
| 102 |                                                                         break;
 | 
|---|
| 103 |                                                                 }
 | 
|---|
| 104 |                                                         }
 | 
|---|
| 105 |                                                 }
 | 
|---|
| 106 |                                         }
 | 
|---|
| 107 |                                         visit_children = false;
 | 
|---|
| 108 |                                 }
 | 
|---|
| 109 |                         }
 | 
|---|
| 110 |                 };
 | 
|---|
| 111 |         } // anonymous namespace
 | 
|---|
| 112 |         /// Check if this expression is or includes a deleted expression
 | 
|---|
| 113 |         const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) {
 | 
|---|
| 114 |                 return ast::Pass<DeleteFinder>::read( expr );
 | 
|---|
| 115 |         }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         namespace {
 | 
|---|
| 118 |                 /// always-accept candidate filter
 | 
|---|
| 119 |                 bool anyCandidate( const Candidate & ) { return true; }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |                 /// Calls the CandidateFinder and finds the single best candidate
 | 
|---|
| 122 |                 CandidateRef findUnfinishedKindExpression(
 | 
|---|
| 123 |                         const ast::Expr * untyped, const ResolveContext & context, const std::string & kind,
 | 
|---|
| 124 |                         std::function<bool(const Candidate &)> pred = anyCandidate, ResolveMode mode = {}
 | 
|---|
| 125 |                 ) {
 | 
|---|
| 126 |                         if ( ! untyped ) return nullptr;
 | 
|---|
| 127 | 
 | 
|---|
| 128 |                         // xxx - this isn't thread-safe, but should work until we parallelize the resolver
 | 
|---|
| 129 |                         static unsigned recursion_level = 0;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |                         ++recursion_level;
 | 
|---|
| 132 |                         ast::TypeEnvironment env;
 | 
|---|
| 133 |                         CandidateFinder finder( context, env );
 | 
|---|
| 134 |                         finder.allowVoid = true;
 | 
|---|
| 135 |                         finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode );
 | 
|---|
| 136 |                         --recursion_level;
 | 
|---|
| 137 | 
 | 
|---|
| 138 |                         // produce a filtered list of candidates
 | 
|---|
| 139 |                         CandidateList candidates;
 | 
|---|
| 140 |                         for ( auto & cand : finder.candidates ) {
 | 
|---|
| 141 |                                 if ( pred( *cand ) ) { candidates.emplace_back( cand ); }
 | 
|---|
| 142 |                         }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |                         // produce invalid error if no candidates
 | 
|---|
| 145 |                         if ( candidates.empty() ) {
 | 
|---|
| 146 |                                 SemanticError( untyped,
 | 
|---|
| 147 |                                         toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""),
 | 
|---|
| 148 |                                         "expression: ") );
 | 
|---|
| 149 |                         }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |                         // search for cheapest candidate
 | 
|---|
| 152 |                         CandidateList winners;
 | 
|---|
| 153 |                         bool seen_undeleted = false;
 | 
|---|
| 154 |                         for ( CandidateRef & cand : candidates ) {
 | 
|---|
| 155 |                                 int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost );
 | 
|---|
| 156 | 
 | 
|---|
| 157 |                                 if ( c > 0 ) continue;  // skip more expensive than winner
 | 
|---|
| 158 | 
 | 
|---|
| 159 |                                 if ( c < 0 ) {
 | 
|---|
| 160 |                                         // reset on new cheapest
 | 
|---|
| 161 |                                         seen_undeleted = ! findDeletedExpr( cand->expr );
 | 
|---|
| 162 |                                         winners.clear();
 | 
|---|
| 163 |                                 } else /* if ( c == 0 ) */ {
 | 
|---|
| 164 |                                         if ( findDeletedExpr( cand->expr ) ) {
 | 
|---|
| 165 |                                                 // skip deleted expression if already seen one equivalent-cost not
 | 
|---|
| 166 |                                                 if ( seen_undeleted ) continue;
 | 
|---|
| 167 |                                         } else if ( ! seen_undeleted ) {
 | 
|---|
| 168 |                                                 // replace list of equivalent-cost deleted expressions with one non-deleted
 | 
|---|
| 169 |                                                 winners.clear();
 | 
|---|
| 170 |                                                 seen_undeleted = true;
 | 
|---|
| 171 |                                         }
 | 
|---|
| 172 |                                 }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |                                 winners.emplace_back( std::move( cand ) );
 | 
|---|
| 175 |                         }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |                         // promote candidate.cvtCost to .cost
 | 
|---|
| 178 |                         // promoteCvtCost( winners );
 | 
|---|
| 179 | 
 | 
|---|
| 180 |                         // produce ambiguous errors, if applicable
 | 
|---|
| 181 |                         if ( winners.size() != 1 ) {
 | 
|---|
| 182 |                                 std::ostringstream stream;
 | 
|---|
| 183 |                                 stream << "Cannot choose between " << winners.size() << " alternatives for "
 | 
|---|
| 184 |                                         << kind << (kind != "" ? " " : "") << "expression\n";
 | 
|---|
| 185 |                                 ast::print( stream, untyped );
 | 
|---|
| 186 |                                 stream << " Alternatives are:\n";
 | 
|---|
| 187 |                                 print( stream, winners, 1 );
 | 
|---|
| 188 |                                 SemanticError( untyped->location, stream.str() );
 | 
|---|
| 189 |                         }
 | 
|---|
| 190 | 
 | 
|---|
| 191 |                         // single selected choice
 | 
|---|
| 192 |                         CandidateRef & choice = winners.front();
 | 
|---|
| 193 | 
 | 
|---|
| 194 |                         // fail on only expression deleted
 | 
|---|
| 195 |                         if ( ! seen_undeleted ) {
 | 
|---|
| 196 |                                 SemanticError( untyped->location, choice->expr.get(), "Unique best alternative "
 | 
|---|
| 197 |                                 "includes deleted identifier in " );
 | 
|---|
| 198 |                         }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |                         return std::move( choice );
 | 
|---|
| 201 |                 }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |                 /// Strips extraneous casts out of an expression
 | 
|---|
| 204 |                 struct StripCasts final {
 | 
|---|
| 205 |                         const ast::Expr * postvisit( const ast::CastExpr * castExpr ) {
 | 
|---|
| 206 |                                 if (
 | 
|---|
| 207 |                                         castExpr->isGenerated == ast::GeneratedCast
 | 
|---|
| 208 |                                         && typesCompatible( castExpr->arg->result, castExpr->result )
 | 
|---|
| 209 |                                 ) {
 | 
|---|
| 210 |                                         // generated cast is the same type as its argument, remove it after keeping env
 | 
|---|
| 211 |                                         return ast::mutate_field(
 | 
|---|
| 212 |                                                 castExpr->arg.get(), &ast::Expr::env, castExpr->env );
 | 
|---|
| 213 |                                 }
 | 
|---|
| 214 |                                 return castExpr;
 | 
|---|
| 215 |                         }
 | 
|---|
| 216 | 
 | 
|---|
| 217 |                         static void strip( ast::ptr< ast::Expr > & expr ) {
 | 
|---|
| 218 |                                 ast::Pass< StripCasts > stripper;
 | 
|---|
| 219 |                                 expr = expr->accept( stripper );
 | 
|---|
| 220 |                         }
 | 
|---|
| 221 |                 };
 | 
|---|
| 222 | 
 | 
|---|
| 223 |                 /// Swaps argument into expression pointer, saving original environment
 | 
|---|
| 224 |                 void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) {
 | 
|---|
| 225 |                         ast::ptr< ast::TypeSubstitution > env = expr->env;
 | 
|---|
| 226 |                         expr.set_and_mutate( newExpr )->env = env;
 | 
|---|
| 227 |                 }
 | 
|---|
| 228 | 
 | 
|---|
| 229 |                 /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts)
 | 
|---|
| 230 |                 void removeExtraneousCast( ast::ptr<ast::Expr> & expr ) {
 | 
|---|
| 231 |                         if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) {
 | 
|---|
| 232 |                                 if ( typesCompatible( castExpr->arg->result, castExpr->result ) ) {
 | 
|---|
| 233 |                                         // cast is to the same type as its argument, remove it
 | 
|---|
| 234 |                                         swap_and_save_env( expr, castExpr->arg );
 | 
|---|
| 235 |                                 }
 | 
|---|
| 236 |                         }
 | 
|---|
| 237 |                 }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | 
 | 
|---|
| 240 |         } // anonymous namespace
 | 
|---|
| 241 | /// Establish post-resolver invariants for expressions
 | 
|---|
| 242 |                 void finishExpr(
 | 
|---|
| 243 |                         ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env,
 | 
|---|
| 244 |                         const ast::TypeSubstitution * oldenv = nullptr
 | 
|---|
| 245 |                 ) {
 | 
|---|
| 246 |                         // set up new type substitution for expression
 | 
|---|
| 247 |                         ast::ptr< ast::TypeSubstitution > newenv =
 | 
|---|
| 248 |                                  oldenv ? oldenv : new ast::TypeSubstitution{};
 | 
|---|
| 249 |                         env.writeToSubstitution( *newenv.get_and_mutate() );
 | 
|---|
| 250 |                         expr.get_and_mutate()->env = std::move( newenv );
 | 
|---|
| 251 |                         // remove unncecessary casts
 | 
|---|
| 252 |                         StripCasts::strip( expr );
 | 
|---|
| 253 |                 }
 | 
|---|
| 254 | 
 | 
|---|
| 255 |         ast::ptr< ast::Expr > resolveInVoidContext(
 | 
|---|
| 256 |                 const ast::Expr * expr, const ResolveContext & context,
 | 
|---|
| 257 |                 ast::TypeEnvironment & env
 | 
|---|
| 258 |         ) {
 | 
|---|
| 259 |                 assertf( expr, "expected a non-null expression" );
 | 
|---|
| 260 | 
 | 
|---|
| 261 |                 // set up and resolve expression cast to void
 | 
|---|
| 262 |                 ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr };
 | 
|---|
| 263 |                 CandidateRef choice = findUnfinishedKindExpression(
 | 
|---|
| 264 |                         untyped, context, "", anyCandidate, ResolveMode::withAdjustment() );
 | 
|---|
| 265 | 
 | 
|---|
| 266 |                 // a cast expression has either 0 or 1 interpretations (by language rules);
 | 
|---|
| 267 |                 // if 0, an exception has already been thrown, and this code will not run
 | 
|---|
| 268 |                 const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >();
 | 
|---|
| 269 |                 env = std::move( choice->env );
 | 
|---|
| 270 | 
 | 
|---|
| 271 |                 return castExpr->arg;
 | 
|---|
| 272 |         }
 | 
|---|
| 273 | 
 | 
|---|
| 274 |         /// Resolve `untyped` to the expression whose candidate is the best match for a `void`
 | 
|---|
| 275 |                 /// context.
 | 
|---|
| 276 |                 ast::ptr< ast::Expr > findVoidExpression(
 | 
|---|
| 277 |                         const ast::Expr * untyped, const ResolveContext & context
 | 
|---|
| 278 |                 ) {
 | 
|---|
| 279 |                         ast::TypeEnvironment env;
 | 
|---|
| 280 |                         ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env );
 | 
|---|
| 281 |                         finishExpr( newExpr, env, untyped->env );
 | 
|---|
| 282 |                         return newExpr;
 | 
|---|
| 283 |                 }
 | 
|---|
| 284 | 
 | 
|---|
| 285 |         namespace {
 | 
|---|
| 286 | 
 | 
|---|
| 287 | 
 | 
|---|
| 288 |                 /// resolve `untyped` to the expression whose candidate satisfies `pred` with the
 | 
|---|
| 289 |                 /// lowest cost, returning the resolved version
 | 
|---|
| 290 |                 ast::ptr< ast::Expr > findKindExpression(
 | 
|---|
| 291 |                         const ast::Expr * untyped, const ResolveContext & context,
 | 
|---|
| 292 |                         std::function<bool(const Candidate &)> pred = anyCandidate,
 | 
|---|
| 293 |                         const std::string & kind = "", ResolveMode mode = {}
 | 
|---|
| 294 |                 ) {
 | 
|---|
| 295 |                         if ( ! untyped ) return {};
 | 
|---|
| 296 |                         CandidateRef choice =
 | 
|---|
| 297 |                                 findUnfinishedKindExpression( untyped, context, kind, pred, mode );
 | 
|---|
| 298 |                         ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env );
 | 
|---|
| 299 |                         return std::move( choice->expr );
 | 
|---|
| 300 |                 }
 | 
|---|
| 301 | 
 | 
|---|
| 302 |                 /// Resolve `untyped` to the single expression whose candidate is the best match
 | 
|---|
| 303 |                 ast::ptr< ast::Expr > findSingleExpression(
 | 
|---|
| 304 |                         const ast::Expr * untyped, const ResolveContext & context
 | 
|---|
| 305 |                 ) {
 | 
|---|
| 306 |                         Stats::ResolveTime::start( untyped );
 | 
|---|
| 307 |                         auto res = findKindExpression( untyped, context );
 | 
|---|
| 308 |                         Stats::ResolveTime::stop();
 | 
|---|
| 309 |                         return res;
 | 
|---|
| 310 |                 }
 | 
|---|
| 311 |         } // anonymous namespace
 | 
|---|
| 312 | 
 | 
|---|
| 313 |         ast::ptr< ast::Expr > findSingleExpression(
 | 
|---|
| 314 |                 const ast::Expr * untyped, const ast::Type * type,
 | 
|---|
| 315 |                 const ResolveContext & context
 | 
|---|
| 316 |         ) {
 | 
|---|
| 317 |                 assert( untyped && type );
 | 
|---|
| 318 |                 ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type };
 | 
|---|
| 319 |                 ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, context );
 | 
|---|
| 320 |                 removeExtraneousCast( newExpr );
 | 
|---|
| 321 |                 return newExpr;
 | 
|---|
| 322 |         }
 | 
|---|
| 323 | 
 | 
|---|
| 324 |         namespace {
 | 
|---|
| 325 |                 bool structOrUnion( const Candidate & i ) {
 | 
|---|
| 326 |                         const ast::Type * t = i.expr->result->stripReferences();
 | 
|---|
| 327 |                         return dynamic_cast< const ast::StructInstType * >( t ) || dynamic_cast< const ast::UnionInstType * >( t );
 | 
|---|
| 328 |                 }
 | 
|---|
| 329 |                 /// Predicate for "Candidate has integral type"
 | 
|---|
| 330 |                 bool hasIntegralType( const Candidate & i ) {
 | 
|---|
| 331 |                         const ast::Type * type = i.expr->result;
 | 
|---|
| 332 | 
 | 
|---|
| 333 |                         if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) {
 | 
|---|
| 334 |                                 return bt->isInteger();
 | 
|---|
| 335 |                         } else if (
 | 
|---|
| 336 |                                 dynamic_cast< const ast::EnumInstType * >( type )
 | 
|---|
| 337 |                                 || dynamic_cast< const ast::ZeroType * >( type )
 | 
|---|
| 338 |                                 || dynamic_cast< const ast::OneType * >( type )
 | 
|---|
| 339 |                         ) {
 | 
|---|
| 340 |                                 return true;
 | 
|---|
| 341 |                         } else return false;
 | 
|---|
| 342 |                 }
 | 
|---|
| 343 | 
 | 
|---|
| 344 |                 /// Resolve `untyped` as an integral expression, returning the resolved version
 | 
|---|
| 345 |                 ast::ptr< ast::Expr > findIntegralExpression(
 | 
|---|
| 346 |                         const ast::Expr * untyped, const ResolveContext & context
 | 
|---|
| 347 |                 ) {
 | 
|---|
| 348 |                         return findKindExpression( untyped, context, hasIntegralType, "condition" );
 | 
|---|
| 349 |                 }
 | 
|---|
| 350 | 
 | 
|---|
| 351 |                 /// check if a type is a character type
 | 
|---|
| 352 |                 bool isCharType( const ast::Type * t ) {
 | 
|---|
| 353 |                         if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) {
 | 
|---|
| 354 |                                 return bt->kind == ast::BasicType::Char
 | 
|---|
| 355 |                                         || bt->kind == ast::BasicType::SignedChar
 | 
|---|
| 356 |                                         || bt->kind == ast::BasicType::UnsignedChar;
 | 
|---|
| 357 |                         }
 | 
|---|
| 358 |                         return false;
 | 
|---|
| 359 |                 }
 | 
|---|
| 360 | 
 | 
|---|
| 361 |                 /// Advance a type itertor to the next mutex parameter
 | 
|---|
| 362 |                 template<typename Iter>
 | 
|---|
| 363 |                 inline bool nextMutex( Iter & it, const Iter & end ) {
 | 
|---|
| 364 |                         while ( it != end && ! (*it)->is_mutex() ) { ++it; }
 | 
|---|
| 365 |                         return it != end;
 | 
|---|
| 366 |                 }
 | 
|---|
| 367 |         }
 | 
|---|
| 368 | 
 | 
|---|
| 369 |         class Resolver final
 | 
|---|
| 370 |         : public ast::WithSymbolTable, public ast::WithGuards,
 | 
|---|
| 371 |           public ast::WithVisitorRef<Resolver>, public ast::WithShortCircuiting,
 | 
|---|
| 372 |           public ast::WithStmtsToAdd<> {
 | 
|---|
| 373 | 
 | 
|---|
| 374 |                 ast::ptr< ast::Type > functionReturn = nullptr;
 | 
|---|
| 375 |                 ast::CurrentObject currentObject;
 | 
|---|
| 376 |                 // for work previously in GenInit
 | 
|---|
| 377 |                 static InitTweak::ManagedTypes managedTypes;
 | 
|---|
| 378 |                 ResolveContext context;
 | 
|---|
| 379 | 
 | 
|---|
| 380 |                 bool inEnumDecl = false;
 | 
|---|
| 381 | 
 | 
|---|
| 382 |         public:
 | 
|---|
| 383 |                 static size_t traceId;
 | 
|---|
| 384 |                 Resolver( const ast::TranslationGlobal & global ) :
 | 
|---|
| 385 |                         ast::WithSymbolTable(ast::SymbolTable::ErrorDetection::ValidateOnAdd),
 | 
|---|
| 386 |                         context{ symtab, global } {}
 | 
|---|
| 387 |                 Resolver( const ResolveContext & context ) :
 | 
|---|
| 388 |                         ast::WithSymbolTable{ context.symtab },
 | 
|---|
| 389 |                         context{ symtab, context.global } {}
 | 
|---|
| 390 | 
 | 
|---|
| 391 |                 const ast::FunctionDecl * previsit( const ast::FunctionDecl * );
 | 
|---|
| 392 |                 const ast::FunctionDecl * postvisit( const ast::FunctionDecl * );
 | 
|---|
| 393 |                 const ast::ObjectDecl * previsit( const ast::ObjectDecl * );
 | 
|---|
| 394 |                 void previsit( const ast::AggregateDecl * );
 | 
|---|
| 395 |                 void previsit( const ast::StructDecl * );
 | 
|---|
| 396 |                 void previsit( const ast::EnumDecl * );
 | 
|---|
| 397 |                 const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * );
 | 
|---|
| 398 | 
 | 
|---|
| 399 |                 const ast::ArrayType * previsit( const ast::ArrayType * );
 | 
|---|
| 400 |                 const ast::PointerType * previsit( const ast::PointerType * );
 | 
|---|
| 401 | 
 | 
|---|
| 402 |                 const ast::ExprStmt *        previsit( const ast::ExprStmt * );
 | 
|---|
| 403 |                 const ast::AsmExpr *         previsit( const ast::AsmExpr * );
 | 
|---|
| 404 |                 const ast::AsmStmt *         previsit( const ast::AsmStmt * );
 | 
|---|
| 405 |                 const ast::IfStmt *          previsit( const ast::IfStmt * );
 | 
|---|
| 406 |                 const ast::WhileDoStmt *     previsit( const ast::WhileDoStmt * );
 | 
|---|
| 407 |                 const ast::ForStmt *         previsit( const ast::ForStmt * );
 | 
|---|
| 408 |                 const ast::SwitchStmt *      previsit( const ast::SwitchStmt * );
 | 
|---|
| 409 |                 const ast::CaseClause *      previsit( const ast::CaseClause * );
 | 
|---|
| 410 |                 const ast::BranchStmt *      previsit( const ast::BranchStmt * );
 | 
|---|
| 411 |                 const ast::ReturnStmt *      previsit( const ast::ReturnStmt * );
 | 
|---|
| 412 |                 const ast::ThrowStmt *       previsit( const ast::ThrowStmt * );
 | 
|---|
| 413 |                 const ast::CatchClause *     previsit( const ast::CatchClause * );
 | 
|---|
| 414 |                 const ast::CatchClause *     postvisit( const ast::CatchClause * );
 | 
|---|
| 415 |                 const ast::WaitForStmt *     previsit( const ast::WaitForStmt * );
 | 
|---|
| 416 |                 const ast::WithStmt *        previsit( const ast::WithStmt * );
 | 
|---|
| 417 | 
 | 
|---|
| 418 |                 const ast::SingleInit *      previsit( const ast::SingleInit * );
 | 
|---|
| 419 |                 const ast::ListInit *        previsit( const ast::ListInit * );
 | 
|---|
| 420 |                 const ast::ConstructorInit * previsit( const ast::ConstructorInit * );
 | 
|---|
| 421 | 
 | 
|---|
| 422 |                 void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd);
 | 
|---|
| 423 | 
 | 
|---|
| 424 |                 void beginScope() { managedTypes.beginScope(); }
 | 
|---|
| 425 |                 void endScope() { managedTypes.endScope(); }
 | 
|---|
| 426 |                 bool on_error(ast::ptr<ast::Decl> & decl);
 | 
|---|
| 427 |         };
 | 
|---|
| 428 |         // size_t Resolver::traceId = Stats::Heap::new_stacktrace_id("Resolver");
 | 
|---|
| 429 | 
 | 
|---|
| 430 |         InitTweak::ManagedTypes Resolver::managedTypes;
 | 
|---|
| 431 | 
 | 
|---|
| 432 |         void resolve( ast::TranslationUnit& translationUnit ) {
 | 
|---|
| 433 |                 ast::Pass< Resolver >::run( translationUnit, translationUnit.global );
 | 
|---|
| 434 |         }
 | 
|---|
| 435 | 
 | 
|---|
| 436 |         ast::ptr< ast::Init > resolveCtorInit(
 | 
|---|
| 437 |                 const ast::ConstructorInit * ctorInit, const ResolveContext & context
 | 
|---|
| 438 |         ) {
 | 
|---|
| 439 |                 assert( ctorInit );
 | 
|---|
| 440 |                 ast::Pass< Resolver > resolver( context );
 | 
|---|
| 441 |                 return ctorInit->accept( resolver );
 | 
|---|
| 442 |         }
 | 
|---|
| 443 | 
 | 
|---|
| 444 |         const ast::Expr * resolveStmtExpr(
 | 
|---|
| 445 |                 const ast::StmtExpr * stmtExpr, const ResolveContext & context
 | 
|---|
| 446 |         ) {
 | 
|---|
| 447 |                 assert( stmtExpr );
 | 
|---|
| 448 |                 ast::Pass< Resolver > resolver( context );
 | 
|---|
| 449 |                 auto ret = mutate(stmtExpr->accept(resolver));
 | 
|---|
| 450 |                 strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult();
 | 
|---|
| 451 |                 return ret;
 | 
|---|
| 452 |         }
 | 
|---|
| 453 | 
 | 
|---|
| 454 |         namespace {
 | 
|---|
| 455 |                 const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) {
 | 
|---|
| 456 |                         std::string name = attr->normalizedName();
 | 
|---|
| 457 |                         if (name == "constructor" || name == "destructor") {
 | 
|---|
| 458 |                                 if (attr->params.size() == 1) {
 | 
|---|
| 459 |                                         auto arg = attr->params.front();
 | 
|---|
| 460 |                                         auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), context );
 | 
|---|
| 461 |                                         auto result = eval(arg);
 | 
|---|
| 462 | 
 | 
|---|
| 463 |                                         auto mutAttr = mutate(attr);
 | 
|---|
| 464 |                                         mutAttr->params.front() = resolved;
 | 
|---|
| 465 |                                         if (! result.hasKnownValue) {
 | 
|---|
| 466 |                                                 SemanticWarning(loc, Warning::GccAttributes,
 | 
|---|
| 467 |                                                         toCString( name, " priorities must be integers from 0 to 65535 inclusive: ", arg ) );
 | 
|---|
| 468 |                                         }
 | 
|---|
| 469 |                                         else {
 | 
|---|
| 470 |                                                 auto priority = result.knownValue;
 | 
|---|
| 471 |                                                 if (priority < 101) {
 | 
|---|
| 472 |                                                         SemanticWarning(loc, Warning::GccAttributes,
 | 
|---|
| 473 |                                                                 toCString( name, " priorities from 0 to 100 are reserved for the implementation" ) );
 | 
|---|
| 474 |                                                 } else if (priority < 201 && ! buildingLibrary()) {
 | 
|---|
| 475 |                                                         SemanticWarning(loc, Warning::GccAttributes,
 | 
|---|
| 476 |                                                                 toCString( name, " priorities from 101 to 200 are reserved for the implementation" ) );
 | 
|---|
| 477 |                                                 }
 | 
|---|
| 478 |                                         }
 | 
|---|
| 479 |                                         return mutAttr;
 | 
|---|
| 480 |                                 } else if (attr->params.size() > 1) {
 | 
|---|
| 481 |                                         SemanticWarning(loc, Warning::GccAttributes, toCString( "too many arguments to ", name, " attribute" ) );
 | 
|---|
| 482 |                                 } else {
 | 
|---|
| 483 |                                         SemanticWarning(loc, Warning::GccAttributes, toCString( "too few arguments to ", name, " attribute" ) );
 | 
|---|
| 484 |                                 }
 | 
|---|
| 485 |                         }
 | 
|---|
| 486 |                         return attr;
 | 
|---|
| 487 |                 }
 | 
|---|
| 488 |         }
 | 
|---|
| 489 | 
 | 
|---|
| 490 |         const ast::FunctionDecl * Resolver::previsit( const ast::FunctionDecl * functionDecl ) {
 | 
|---|
| 491 |                 GuardValue( functionReturn );
 | 
|---|
| 492 | 
 | 
|---|
| 493 |                 assert (functionDecl->unique());
 | 
|---|
| 494 |                 if (!functionDecl->has_body() && !functionDecl->withExprs.empty()) {
 | 
|---|
| 495 |                         SemanticError(functionDecl->location, functionDecl, "Function without body has with declarations");
 | 
|---|
| 496 |                 }
 | 
|---|
| 497 | 
 | 
|---|
| 498 |                 if (!functionDecl->isTypeFixed) {
 | 
|---|
| 499 |                         auto mutDecl = mutate(functionDecl);
 | 
|---|
| 500 |                         auto mutType = mutDecl->type.get_and_mutate();
 | 
|---|
| 501 | 
 | 
|---|
| 502 |                         for (auto & attr: mutDecl->attributes) {
 | 
|---|
| 503 |                                 attr = handleAttribute(mutDecl->location, attr, context );
 | 
|---|
| 504 |                         }
 | 
|---|
| 505 | 
 | 
|---|
| 506 |                         // handle assertions
 | 
|---|
| 507 | 
 | 
|---|
| 508 |                         symtab.enterScope();
 | 
|---|
| 509 |                         mutType->forall.clear();
 | 
|---|
| 510 |                         mutType->assertions.clear();
 | 
|---|
| 511 |                         for (auto & typeParam : mutDecl->type_params) {
 | 
|---|
| 512 |                                 symtab.addType(typeParam);
 | 
|---|
| 513 |                                 mutType->forall.emplace_back(new ast::TypeInstType(typeParam));
 | 
|---|
| 514 |                         }
 | 
|---|
| 515 |                         for (auto & asst : mutDecl->assertions) {
 | 
|---|
| 516 |                                 asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), context);
 | 
|---|
| 517 |                                 symtab.addId(asst);
 | 
|---|
| 518 |                                 mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst));
 | 
|---|
| 519 |                         }
 | 
|---|
| 520 | 
 | 
|---|
| 521 |                         // temporarily adds params to symbol table.
 | 
|---|
| 522 |                         // actual scoping rules for params and withexprs differ - see Pass::visit(FunctionDecl)
 | 
|---|
| 523 | 
 | 
|---|
| 524 |                         std::vector<ast::ptr<ast::Type>> paramTypes;
 | 
|---|
| 525 |                         std::vector<ast::ptr<ast::Type>> returnTypes;
 | 
|---|
| 526 | 
 | 
|---|
| 527 |                         for (auto & param : mutDecl->params) {
 | 
|---|
| 528 |                                 param = fixObjectType(param.strict_as<ast::ObjectDecl>(), context);
 | 
|---|
| 529 |                                 symtab.addId(param);
 | 
|---|
| 530 |                                 paramTypes.emplace_back(param->get_type());
 | 
|---|
| 531 |                         }
 | 
|---|
| 532 |                         for (auto & ret : mutDecl->returns) {
 | 
|---|
| 533 |                                 ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), context);
 | 
|---|
| 534 |                                 returnTypes.emplace_back(ret->get_type());
 | 
|---|
| 535 |                         }
 | 
|---|
| 536 |                         // since function type in decl is just a view of param types, need to update that as well
 | 
|---|
| 537 |                         mutType->params = std::move(paramTypes);
 | 
|---|
| 538 |                         mutType->returns = std::move(returnTypes);
 | 
|---|
| 539 | 
 | 
|---|
| 540 |                         auto renamedType = strict_dynamic_cast<const ast::FunctionType *>(renameTyVars(mutType, RenameMode::GEN_EXPR_ID));
 | 
|---|
| 541 | 
 | 
|---|
| 542 |                         std::list<ast::ptr<ast::Stmt>> newStmts;
 | 
|---|
| 543 |                         resolveWithExprs (mutDecl->withExprs, newStmts);
 | 
|---|
| 544 | 
 | 
|---|
| 545 |                         if (mutDecl->stmts) {
 | 
|---|
| 546 |                                 auto mutStmt = mutDecl->stmts.get_and_mutate();
 | 
|---|
| 547 |                                 mutStmt->kids.splice(mutStmt->kids.begin(), std::move(newStmts));
 | 
|---|
| 548 |                                 mutDecl->stmts = mutStmt;
 | 
|---|
| 549 |                         }
 | 
|---|
| 550 | 
 | 
|---|
| 551 |                         symtab.leaveScope();
 | 
|---|
| 552 | 
 | 
|---|
| 553 |                         mutDecl->type = renamedType;
 | 
|---|
| 554 |                         mutDecl->mangleName = Mangle::mangle(mutDecl);
 | 
|---|
| 555 |                         mutDecl->isTypeFixed = true;
 | 
|---|
| 556 |                         functionDecl = mutDecl;
 | 
|---|
| 557 |                 }
 | 
|---|
| 558 |                 managedTypes.handleDWT(functionDecl);
 | 
|---|
| 559 | 
 | 
|---|
| 560 |                 functionReturn = extractResultType( functionDecl->type );
 | 
|---|
| 561 |                 return functionDecl;
 | 
|---|
| 562 |         }
 | 
|---|
| 563 | 
 | 
|---|
| 564 |         const ast::FunctionDecl * Resolver::postvisit( const ast::FunctionDecl * functionDecl ) {
 | 
|---|
| 565 |                 // default value expressions have an environment which shouldn't be there and trips up
 | 
|---|
| 566 |                 // later passes.
 | 
|---|
| 567 |                 assert( functionDecl->unique() );
 | 
|---|
| 568 |                 ast::FunctionType * mutType = mutate( functionDecl->type.get() );
 | 
|---|
| 569 | 
 | 
|---|
| 570 |                 for ( unsigned i = 0 ; i < mutType->params.size() ; ++i ) {
 | 
|---|
| 571 |                         if ( const ast::ObjectDecl * obj = mutType->params[i].as< ast::ObjectDecl >() ) {
 | 
|---|
| 572 |                                 if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) {
 | 
|---|
| 573 |                                         if ( init->value->env == nullptr ) continue;
 | 
|---|
| 574 |                                         // clone initializer minus the initializer environment
 | 
|---|
| 575 |                                         auto mutParam = mutate( mutType->params[i].strict_as< ast::ObjectDecl >() );
 | 
|---|
| 576 |                                         auto mutInit = mutate( mutParam->init.strict_as< ast::SingleInit >() );
 | 
|---|
| 577 |                                         auto mutValue = mutate( mutInit->value.get() );
 | 
|---|
| 578 | 
 | 
|---|
| 579 |                                         mutValue->env = nullptr;
 | 
|---|
| 580 |                                         mutInit->value = mutValue;
 | 
|---|
| 581 |                                         mutParam->init = mutInit;
 | 
|---|
| 582 |                                         mutType->params[i] = mutParam;
 | 
|---|
| 583 | 
 | 
|---|
| 584 |                                         assert( ! mutType->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env);
 | 
|---|
| 585 |                                 }
 | 
|---|
| 586 |                         }
 | 
|---|
| 587 |                 }
 | 
|---|
| 588 |                 mutate_field(functionDecl, &ast::FunctionDecl::type, mutType);
 | 
|---|
| 589 |                 return functionDecl;
 | 
|---|
| 590 |         }
 | 
|---|
| 591 | 
 | 
|---|
| 592 |         const ast::ObjectDecl * Resolver::previsit( const ast::ObjectDecl * objectDecl ) {
 | 
|---|
| 593 |                 // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()],
 | 
|---|
| 594 |                 // class-variable `initContext` is changed multiple times because the LHS is analyzed
 | 
|---|
| 595 |                 // twice. The second analysis changes `initContext` because a function type can contain
 | 
|---|
| 596 |                 // object declarations in the return and parameter types. Therefore each value of
 | 
|---|
| 597 |                 // `initContext` is retained so the type on the first analysis is preserved and used for
 | 
|---|
| 598 |                 // selecting the RHS.
 | 
|---|
| 599 |                 GuardValue( currentObject );
 | 
|---|
| 600 | 
 | 
|---|
| 601 |                 if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) {
 | 
|---|
| 602 |                         // enumerator initializers should not use the enum type to initialize, since the
 | 
|---|
| 603 |                         // enum type is still incomplete at this point. Use `int` instead.
 | 
|---|
| 604 | 
 | 
|---|
| 605 |                         if ( auto enumBase = dynamic_cast< const ast::EnumInstType * >
 | 
|---|
| 606 |                                 ( objectDecl->get_type() )->base->base ) {
 | 
|---|
| 607 |                                 objectDecl = fixObjectType( objectDecl, context );
 | 
|---|
| 608 |                                 currentObject = ast::CurrentObject{
 | 
|---|
| 609 |                                         objectDecl->location,
 | 
|---|
| 610 |                                         enumBase
 | 
|---|
| 611 |                                 };
 | 
|---|
| 612 |                         } else {
 | 
|---|
| 613 |                                 objectDecl = fixObjectType( objectDecl, context );
 | 
|---|
| 614 |                                 currentObject = ast::CurrentObject{
 | 
|---|
| 615 |                                         objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } };
 | 
|---|
| 616 |                         }
 | 
|---|
| 617 | 
 | 
|---|
| 618 |                 }
 | 
|---|
| 619 |                 else {
 | 
|---|
| 620 |                         if ( !objectDecl->isTypeFixed ) {
 | 
|---|
| 621 |                                 auto newDecl = fixObjectType(objectDecl, context);
 | 
|---|
| 622 |                                 auto mutDecl = mutate(newDecl);
 | 
|---|
| 623 | 
 | 
|---|
| 624 |                                 // generate CtorInit wrapper when necessary.
 | 
|---|
| 625 |                                 // in certain cases, fixObjectType is called before reaching
 | 
|---|
| 626 |                                 // this object in visitor pass, thus disabling CtorInit codegen.
 | 
|---|
| 627 |                                 // this happens on aggregate members and function parameters.
 | 
|---|
| 628 |                                 if ( InitTweak::tryConstruct( mutDecl ) && ( managedTypes.isManaged( mutDecl ) || ((! isInFunction() || mutDecl->storage.is_static ) && ! InitTweak::isConstExpr( mutDecl->init ) ) ) ) {
 | 
|---|
| 629 |                                         // constructed objects cannot be designated
 | 
|---|
| 630 |                                         if ( InitTweak::isDesignated( mutDecl->init ) ) {
 | 
|---|
| 631 |                                                 ast::Pass<ResolveDesignators> res( context );
 | 
|---|
| 632 |                                                 maybe_accept( mutDecl->init.get(), res );
 | 
|---|
| 633 |                                                 if ( !res.core.result ) {
 | 
|---|
| 634 |                                                         SemanticError( mutDecl, "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.\n" );
 | 
|---|
| 635 |                                                 }
 | 
|---|
| 636 |                                         }
 | 
|---|
| 637 |                                         // constructed objects should not have initializers nested too deeply
 | 
|---|
| 638 |                                         if ( ! InitTweak::checkInitDepth( mutDecl ) ) SemanticError( mutDecl, "Managed object's initializer is too deep " );
 | 
|---|
| 639 | 
 | 
|---|
| 640 |                                         mutDecl->init = InitTweak::genCtorInit( mutDecl->location, mutDecl );
 | 
|---|
| 641 |                                 }
 | 
|---|
| 642 | 
 | 
|---|
| 643 |                                 objectDecl = mutDecl;
 | 
|---|
| 644 |                         }
 | 
|---|
| 645 |                         currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() };
 | 
|---|
| 646 |                 }
 | 
|---|
| 647 | 
 | 
|---|
| 648 |                 return objectDecl;
 | 
|---|
| 649 |         }
 | 
|---|
| 650 | 
 | 
|---|
| 651 |         void Resolver::previsit( const ast::AggregateDecl * _aggDecl ) {
 | 
|---|
| 652 |                 auto aggDecl = mutate(_aggDecl);
 | 
|---|
| 653 |                 assertf(aggDecl == _aggDecl, "type declarations must be unique");
 | 
|---|
| 654 | 
 | 
|---|
| 655 |                 for (auto & member: aggDecl->members) {
 | 
|---|
| 656 |                         // nested type decls are hoisted already. no need to do anything
 | 
|---|
| 657 |                         if (auto obj = member.as<ast::ObjectDecl>()) {
 | 
|---|
| 658 |                                 member = fixObjectType(obj, context);
 | 
|---|
| 659 |                         }
 | 
|---|
| 660 |                 }
 | 
|---|
| 661 |         }
 | 
|---|
| 662 | 
 | 
|---|
| 663 |         void Resolver::previsit( const ast::StructDecl * structDecl ) {
 | 
|---|
| 664 |                 previsit(static_cast<const ast::AggregateDecl *>(structDecl));
 | 
|---|
| 665 |                 managedTypes.handleStruct(structDecl);
 | 
|---|
| 666 |         }
 | 
|---|
| 667 | 
 | 
|---|
| 668 |         void Resolver::previsit( const ast::EnumDecl * ) {
 | 
|---|
| 669 |                 // in case we decide to allow nested enums
 | 
|---|
| 670 |                 GuardValue( inEnumDecl );
 | 
|---|
| 671 |                 inEnumDecl = true;
 | 
|---|
| 672 |                 // don't need to fix types for enum fields
 | 
|---|
| 673 |         }
 | 
|---|
| 674 | 
 | 
|---|
| 675 |         const ast::StaticAssertDecl * Resolver::previsit(
 | 
|---|
| 676 |                 const ast::StaticAssertDecl * assertDecl
 | 
|---|
| 677 |         ) {
 | 
|---|
| 678 |                 return ast::mutate_field(
 | 
|---|
| 679 |                         assertDecl, &ast::StaticAssertDecl::cond,
 | 
|---|
| 680 |                         findIntegralExpression( assertDecl->cond, context ) );
 | 
|---|
| 681 |         }
 | 
|---|
| 682 | 
 | 
|---|
| 683 |         template< typename PtrType >
 | 
|---|
| 684 |         const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) {
 | 
|---|
| 685 |                 if ( type->dimension ) {
 | 
|---|
| 686 |                         const ast::Type * sizeType = context.global.sizeType.get();
 | 
|---|
| 687 |                         ast::ptr< ast::Expr > dimension = findSingleExpression( type->dimension, sizeType, context );
 | 
|---|
| 688 |                         assertf(dimension->env->empty(), "array dimension expr has nonempty env");
 | 
|---|
| 689 |                         dimension.get_and_mutate()->env = nullptr;
 | 
|---|
| 690 |                         ast::mutate_field( type, &PtrType::dimension, dimension );
 | 
|---|
| 691 |                 }
 | 
|---|
| 692 |                 return type;
 | 
|---|
| 693 |         }
 | 
|---|
| 694 | 
 | 
|---|
| 695 |         const ast::ArrayType * Resolver::previsit( const ast::ArrayType * at ) {
 | 
|---|
| 696 |                 return handlePtrType( at, context );
 | 
|---|
| 697 |         }
 | 
|---|
| 698 | 
 | 
|---|
| 699 |         const ast::PointerType * Resolver::previsit( const ast::PointerType * pt ) {
 | 
|---|
| 700 |                 return handlePtrType( pt, context );
 | 
|---|
| 701 |         }
 | 
|---|
| 702 | 
 | 
|---|
| 703 |         const ast::ExprStmt * Resolver::previsit( const ast::ExprStmt * exprStmt ) {
 | 
|---|
| 704 |                 visit_children = false;
 | 
|---|
| 705 |                 assertf( exprStmt->expr, "ExprStmt has null expression in resolver" );
 | 
|---|
| 706 | 
 | 
|---|
| 707 |                 return ast::mutate_field(
 | 
|---|
| 708 |                         exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, context ) );
 | 
|---|
| 709 |         }
 | 
|---|
| 710 | 
 | 
|---|
| 711 |         const ast::AsmExpr * Resolver::previsit( const ast::AsmExpr * asmExpr ) {
 | 
|---|
| 712 |                 visit_children = false;
 | 
|---|
| 713 | 
 | 
|---|
| 714 |                 asmExpr = ast::mutate_field(
 | 
|---|
| 715 |                         asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, context ) );
 | 
|---|
| 716 | 
 | 
|---|
| 717 |                 return asmExpr;
 | 
|---|
| 718 |         }
 | 
|---|
| 719 | 
 | 
|---|
| 720 |         const ast::AsmStmt * Resolver::previsit( const ast::AsmStmt * asmStmt ) {
 | 
|---|
| 721 |                 visitor->maybe_accept( asmStmt, &ast::AsmStmt::input );
 | 
|---|
| 722 |                 visitor->maybe_accept( asmStmt, &ast::AsmStmt::output );
 | 
|---|
| 723 |                 visit_children = false;
 | 
|---|
| 724 |                 return asmStmt;
 | 
|---|
| 725 |         }
 | 
|---|
| 726 | 
 | 
|---|
| 727 |         const ast::IfStmt * Resolver::previsit( const ast::IfStmt * ifStmt ) {
 | 
|---|
| 728 |                 return ast::mutate_field(
 | 
|---|
| 729 |                         ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, context ) );
 | 
|---|
| 730 |         }
 | 
|---|
| 731 | 
 | 
|---|
| 732 |         const ast::WhileDoStmt * Resolver::previsit( const ast::WhileDoStmt * whileDoStmt ) {
 | 
|---|
| 733 |                 return ast::mutate_field(
 | 
|---|
| 734 |                         whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, context ) );
 | 
|---|
| 735 |         }
 | 
|---|
| 736 | 
 | 
|---|
| 737 |         const ast::ForStmt * Resolver::previsit( const ast::ForStmt * forStmt ) {
 | 
|---|
| 738 |                 if ( forStmt->cond ) {
 | 
|---|
| 739 |                         forStmt = ast::mutate_field(
 | 
|---|
| 740 |                                 forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, context ) );
 | 
|---|
| 741 |                 }
 | 
|---|
| 742 | 
 | 
|---|
| 743 |                 if ( forStmt->inc ) {
 | 
|---|
| 744 |                         forStmt = ast::mutate_field(
 | 
|---|
| 745 |                                 forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, context ) );
 | 
|---|
| 746 |                 }
 | 
|---|
| 747 | 
 | 
|---|
| 748 |                 return forStmt;
 | 
|---|
| 749 |         }
 | 
|---|
| 750 | 
 | 
|---|
| 751 |         const ast::SwitchStmt * Resolver::previsit( const ast::SwitchStmt * switchStmt ) {
 | 
|---|
| 752 |                 GuardValue( currentObject );
 | 
|---|
| 753 |                 switchStmt = ast::mutate_field(
 | 
|---|
| 754 |                         switchStmt, &ast::SwitchStmt::cond,
 | 
|---|
| 755 |                         findIntegralExpression( switchStmt->cond, context ) );
 | 
|---|
| 756 |                 currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result };
 | 
|---|
| 757 |                 return switchStmt;
 | 
|---|
| 758 |         }
 | 
|---|
| 759 | 
 | 
|---|
| 760 |         const ast::CaseClause * Resolver::previsit( const ast::CaseClause * caseStmt ) {
 | 
|---|
| 761 |                 if ( caseStmt->cond ) {
 | 
|---|
| 762 |                         std::deque< ast::InitAlternative > initAlts = currentObject.getOptions();
 | 
|---|
| 763 |                         assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral "
 | 
|---|
| 764 |                                 "expression." );
 | 
|---|
| 765 | 
 | 
|---|
| 766 |                         ast::ptr< ast::Expr > untyped =
 | 
|---|
| 767 |                                 new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type };
 | 
|---|
| 768 |                         ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, context );
 | 
|---|
| 769 | 
 | 
|---|
| 770 |                         // case condition cannot have a cast in C, so it must be removed here, regardless of
 | 
|---|
| 771 |                         // whether it would perform a conversion.
 | 
|---|
| 772 |                         if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) {
 | 
|---|
| 773 |                                 swap_and_save_env( newExpr, castExpr->arg );
 | 
|---|
| 774 |                         }
 | 
|---|
| 775 | 
 | 
|---|
| 776 |                         caseStmt = ast::mutate_field( caseStmt, &ast::CaseClause::cond, newExpr );
 | 
|---|
| 777 |                 }
 | 
|---|
| 778 |                 return caseStmt;
 | 
|---|
| 779 |         }
 | 
|---|
| 780 | 
 | 
|---|
| 781 |         const ast::BranchStmt * Resolver::previsit( const ast::BranchStmt * branchStmt ) {
 | 
|---|
| 782 |                 visit_children = false;
 | 
|---|
| 783 |                 // must resolve the argument of a computed goto
 | 
|---|
| 784 |                 if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) {
 | 
|---|
| 785 |                         // computed goto argument is void*
 | 
|---|
| 786 |                         ast::ptr< ast::Type > target = new ast::PointerType{ new ast::VoidType{} };
 | 
|---|
| 787 |                         branchStmt = ast::mutate_field(
 | 
|---|
| 788 |                                 branchStmt, &ast::BranchStmt::computedTarget,
 | 
|---|
| 789 |                                 findSingleExpression( branchStmt->computedTarget, target, context ) );
 | 
|---|
| 790 |                 }
 | 
|---|
| 791 |                 return branchStmt;
 | 
|---|
| 792 |         }
 | 
|---|
| 793 | 
 | 
|---|
| 794 |         const ast::ReturnStmt * Resolver::previsit( const ast::ReturnStmt * returnStmt ) {
 | 
|---|
| 795 |                 visit_children = false;
 | 
|---|
| 796 |                 if ( returnStmt->expr ) {
 | 
|---|
| 797 |                         returnStmt = ast::mutate_field(
 | 
|---|
| 798 |                                 returnStmt, &ast::ReturnStmt::expr,
 | 
|---|
| 799 |                                 findSingleExpression( returnStmt->expr, functionReturn, context ) );
 | 
|---|
| 800 |                 }
 | 
|---|
| 801 |                 return returnStmt;
 | 
|---|
| 802 |         }
 | 
|---|
| 803 | 
 | 
|---|
| 804 |         const ast::ThrowStmt * Resolver::previsit( const ast::ThrowStmt * throwStmt ) {
 | 
|---|
| 805 |                 visit_children = false;
 | 
|---|
| 806 |                 if ( throwStmt->expr ) {
 | 
|---|
| 807 |                         const ast::StructDecl * exceptionDecl =
 | 
|---|
| 808 |                                 symtab.lookupStruct( "__cfaehm_base_exception_t" );
 | 
|---|
| 809 |                         assert( exceptionDecl );
 | 
|---|
| 810 |                         ast::ptr< ast::Type > exceptType =
 | 
|---|
| 811 |                                 new ast::PointerType{ new ast::StructInstType{ exceptionDecl } };
 | 
|---|
| 812 |                         throwStmt = ast::mutate_field(
 | 
|---|
| 813 |                                 throwStmt, &ast::ThrowStmt::expr,
 | 
|---|
| 814 |                                 findSingleExpression( throwStmt->expr, exceptType, context ) );
 | 
|---|
| 815 |                 }
 | 
|---|
| 816 |                 return throwStmt;
 | 
|---|
| 817 |         }
 | 
|---|
| 818 | 
 | 
|---|
| 819 |         const ast::CatchClause * Resolver::previsit( const ast::CatchClause * catchClause ) {
 | 
|---|
| 820 |                 // Until we are very sure this invarent (ifs that move between passes have then)
 | 
|---|
| 821 |                 // holds, check it. This allows a check for when to decode the mangling.
 | 
|---|
| 822 |                 if ( auto ifStmt = catchClause->body.as<ast::IfStmt>() ) {
 | 
|---|
| 823 |                         assert( ifStmt->then );
 | 
|---|
| 824 |                 }
 | 
|---|
| 825 |                 // Encode the catchStmt so the condition can see the declaration.
 | 
|---|
| 826 |                 if ( catchClause->cond ) {
 | 
|---|
| 827 |                         ast::CatchClause * clause = mutate( catchClause );
 | 
|---|
| 828 |                         clause->body = new ast::IfStmt( clause->location, clause->cond, nullptr, clause->body );
 | 
|---|
| 829 |                         clause->cond = nullptr;
 | 
|---|
| 830 |                         return clause;
 | 
|---|
| 831 |                 }
 | 
|---|
| 832 |                 return catchClause;
 | 
|---|
| 833 |         }
 | 
|---|
| 834 | 
 | 
|---|
| 835 |         const ast::CatchClause * Resolver::postvisit( const ast::CatchClause * catchClause ) {
 | 
|---|
| 836 |                 // Decode the catchStmt so everything is stored properly.
 | 
|---|
| 837 |                 const ast::IfStmt * ifStmt = catchClause->body.as<ast::IfStmt>();
 | 
|---|
| 838 |                 if ( nullptr != ifStmt && nullptr == ifStmt->then ) {
 | 
|---|
| 839 |                         assert( ifStmt->cond );
 | 
|---|
| 840 |                         assert( ifStmt->else_ );
 | 
|---|
| 841 |                         ast::CatchClause * clause = ast::mutate( catchClause );
 | 
|---|
| 842 |                         clause->cond = ifStmt->cond;
 | 
|---|
| 843 |                         clause->body = ifStmt->else_;
 | 
|---|
| 844 |                         // ifStmt should be implicately deleted here.
 | 
|---|
| 845 |                         return clause;
 | 
|---|
| 846 |                 }
 | 
|---|
| 847 |                 return catchClause;
 | 
|---|
| 848 |         }
 | 
|---|
| 849 | 
 | 
|---|
| 850 |         const ast::WaitForStmt * Resolver::previsit( const ast::WaitForStmt * stmt ) {
 | 
|---|
| 851 |                 visit_children = false;
 | 
|---|
| 852 | 
 | 
|---|
| 853 |                 // Resolve all clauses first
 | 
|---|
| 854 |                 for ( unsigned i = 0; i < stmt->clauses.size(); ++i ) {
 | 
|---|
| 855 |                         const ast::WaitForClause & clause = *stmt->clauses[i];
 | 
|---|
| 856 | 
 | 
|---|
| 857 |                         ast::TypeEnvironment env;
 | 
|---|
| 858 |                         CandidateFinder funcFinder( context, env );
 | 
|---|
| 859 | 
 | 
|---|
| 860 |                         // Find all candidates for a function in canonical form
 | 
|---|
| 861 |                         funcFinder.find( clause.target, ResolveMode::withAdjustment() );
 | 
|---|
| 862 | 
 | 
|---|
| 863 |                         if ( funcFinder.candidates.empty() ) {
 | 
|---|
| 864 |                                 stringstream ss;
 | 
|---|
| 865 |                                 ss << "Use of undeclared indentifier '";
 | 
|---|
| 866 |                                 ss << clause.target.strict_as< ast::NameExpr >()->name;
 | 
|---|
| 867 |                                 ss << "' in call to waitfor";
 | 
|---|
| 868 |                                 SemanticError( stmt->location, ss.str() );
 | 
|---|
| 869 |                         }
 | 
|---|
| 870 | 
 | 
|---|
| 871 |                         if ( clause.target_args.empty() ) {
 | 
|---|
| 872 |                                 SemanticError( stmt->location,
 | 
|---|
| 873 |                                         "Waitfor clause must have at least one mutex parameter");
 | 
|---|
| 874 |                         }
 | 
|---|
| 875 | 
 | 
|---|
| 876 |                         // Find all alternatives for all arguments in canonical form
 | 
|---|
| 877 |                         std::vector< CandidateFinder > argFinders =
 | 
|---|
| 878 |                                 funcFinder.findSubExprs( clause.target_args );
 | 
|---|
| 879 | 
 | 
|---|
| 880 |                         // List all combinations of arguments
 | 
|---|
| 881 |                         std::vector< CandidateList > possibilities;
 | 
|---|
| 882 |                         combos( argFinders.begin(), argFinders.end(), back_inserter( possibilities ) );
 | 
|---|
| 883 | 
 | 
|---|
| 884 |                         // For every possible function:
 | 
|---|
| 885 |                         // * try matching the arguments to the parameters, not the other way around because
 | 
|---|
| 886 |                         //   more arguments than parameters
 | 
|---|
| 887 |                         CandidateList funcCandidates;
 | 
|---|
| 888 |                         std::vector< CandidateList > argsCandidates;
 | 
|---|
| 889 |                         SemanticErrorException errors;
 | 
|---|
| 890 |                         for ( CandidateRef & func : funcFinder.candidates ) {
 | 
|---|
| 891 |                                 try {
 | 
|---|
| 892 |                                         auto pointerType = dynamic_cast< const ast::PointerType * >(
 | 
|---|
| 893 |                                                 func->expr->result->stripReferences() );
 | 
|---|
| 894 |                                         if ( ! pointerType ) {
 | 
|---|
| 895 |                                                 SemanticError( stmt->location, func->expr->result.get(),
 | 
|---|
| 896 |                                                         "candidate not viable: not a pointer type\n" );
 | 
|---|
| 897 |                                         }
 | 
|---|
| 898 | 
 | 
|---|
| 899 |                                         auto funcType = pointerType->base.as< ast::FunctionType >();
 | 
|---|
| 900 |                                         if ( ! funcType ) {
 | 
|---|
| 901 |                                                 SemanticError( stmt->location, func->expr->result.get(),
 | 
|---|
| 902 |                                                         "candidate not viable: not a function type\n" );
 | 
|---|
| 903 |                                         }
 | 
|---|
| 904 | 
 | 
|---|
| 905 |                                         {
 | 
|---|
| 906 |                                                 auto param    = funcType->params.begin();
 | 
|---|
| 907 |                                                 auto paramEnd = funcType->params.end();
 | 
|---|
| 908 | 
 | 
|---|
| 909 |                                                 if( ! nextMutex( param, paramEnd ) ) {
 | 
|---|
| 910 |                                                         SemanticError( stmt->location, funcType,
 | 
|---|
| 911 |                                                                 "candidate function not viable: no mutex parameters\n");
 | 
|---|
| 912 |                                                 }
 | 
|---|
| 913 |                                         }
 | 
|---|
| 914 | 
 | 
|---|
| 915 |                                         CandidateRef func2{ new Candidate{ *func } };
 | 
|---|
| 916 |                                         // strip reference from function
 | 
|---|
| 917 |                                         func2->expr = referenceToRvalueConversion( func->expr, func2->cost );
 | 
|---|
| 918 | 
 | 
|---|
| 919 |                                         // Each argument must be matched with a parameter of the current candidate
 | 
|---|
| 920 |                                         for ( auto & argsList : possibilities ) {
 | 
|---|
| 921 |                                                 try {
 | 
|---|
| 922 |                                                         // Declare data structures needed for resolution
 | 
|---|
| 923 |                                                         ast::OpenVarSet open;
 | 
|---|
| 924 |                                                         ast::AssertionSet need, have;
 | 
|---|
| 925 |                                                         ast::TypeEnvironment resultEnv{ func->env };
 | 
|---|
| 926 |                                                         // Add all type variables as open so that those not used in the
 | 
|---|
| 927 |                                                         // parameter list are still considered open
 | 
|---|
| 928 |                                                         resultEnv.add( funcType->forall );
 | 
|---|
| 929 | 
 | 
|---|
| 930 |                                                         // load type variables from arguments into one shared space
 | 
|---|
| 931 |                                                         for ( auto & arg : argsList ) {
 | 
|---|
| 932 |                                                                 resultEnv.simpleCombine( arg->env );
 | 
|---|
| 933 |                                                         }
 | 
|---|
| 934 | 
 | 
|---|
| 935 |                                                         // Make sure we don't widen any existing bindings
 | 
|---|
| 936 |                                                         resultEnv.forbidWidening();
 | 
|---|
| 937 | 
 | 
|---|
| 938 |                                                         // Find any unbound type variables
 | 
|---|
| 939 |                                                         resultEnv.extractOpenVars( open );
 | 
|---|
| 940 | 
 | 
|---|
| 941 |                                                         auto param = funcType->params.begin();
 | 
|---|
| 942 |                                                         auto paramEnd = funcType->params.end();
 | 
|---|
| 943 | 
 | 
|---|
| 944 |                                                         unsigned n_mutex_param = 0;
 | 
|---|
| 945 | 
 | 
|---|
| 946 |                                                         // For every argument of its set, check if it matches one of the
 | 
|---|
| 947 |                                                         // parameters. The order is important
 | 
|---|
| 948 |                                                         for ( auto & arg : argsList ) {
 | 
|---|
| 949 |                                                                 // Ignore non-mutex arguments
 | 
|---|
| 950 |                                                                 if ( ! nextMutex( param, paramEnd ) ) {
 | 
|---|
| 951 |                                                                         // We ran out of parameters but still have arguments.
 | 
|---|
| 952 |                                                                         // This function doesn't match
 | 
|---|
| 953 |                                                                         SemanticError( stmt->location, funcType,
 | 
|---|
| 954 |                                                                                 toString("candidate function not viable: too many mutex "
 | 
|---|
| 955 |                                                                                 "arguments, expected ", n_mutex_param, "\n" ) );
 | 
|---|
| 956 |                                                                 }
 | 
|---|
| 957 | 
 | 
|---|
| 958 |                                                                 ++n_mutex_param;
 | 
|---|
| 959 | 
 | 
|---|
| 960 |                                                                 // Check if the argument matches the parameter type in the current
 | 
|---|
| 961 |                                                                 // scope
 | 
|---|
| 962 |                                                                 // ast::ptr< ast::Type > paramType = (*param)->get_type();
 | 
|---|
| 963 |                                                                 if (
 | 
|---|
| 964 |                                                                         ! unify(
 | 
|---|
| 965 |                                                                                 arg->expr->result, *param, resultEnv, need, have, open )
 | 
|---|
| 966 |                                                                 ) {
 | 
|---|
| 967 |                                                                         // Type doesn't match
 | 
|---|
| 968 |                                                                         stringstream ss;
 | 
|---|
| 969 |                                                                         ss << "candidate function not viable: no known conversion "
 | 
|---|
| 970 |                                                                                 "from '";
 | 
|---|
| 971 |                                                                         ast::print( ss, *param );
 | 
|---|
| 972 |                                                                         ss << "' to '";
 | 
|---|
| 973 |                                                                         ast::print( ss, arg->expr->result );
 | 
|---|
| 974 |                                                                         ss << "' with env '";
 | 
|---|
| 975 |                                                                         ast::print( ss, resultEnv );
 | 
|---|
| 976 |                                                                         ss << "'\n";
 | 
|---|
| 977 |                                                                         SemanticError( stmt->location, funcType, ss.str() );
 | 
|---|
| 978 |                                                                 }
 | 
|---|
| 979 | 
 | 
|---|
| 980 |                                                                 ++param;
 | 
|---|
| 981 |                                                         }
 | 
|---|
| 982 | 
 | 
|---|
| 983 |                                                         // All arguments match!
 | 
|---|
| 984 | 
 | 
|---|
| 985 |                                                         // Check if parameters are missing
 | 
|---|
| 986 |                                                         if ( nextMutex( param, paramEnd ) ) {
 | 
|---|
| 987 |                                                                 do {
 | 
|---|
| 988 |                                                                         ++n_mutex_param;
 | 
|---|
| 989 |                                                                         ++param;
 | 
|---|
| 990 |                                                                 } while ( nextMutex( param, paramEnd ) );
 | 
|---|
| 991 | 
 | 
|---|
| 992 |                                                                 // We ran out of arguments but still have parameters left; this
 | 
|---|
| 993 |                                                                 // function doesn't match
 | 
|---|
| 994 |                                                                 SemanticError( stmt->location, funcType,
 | 
|---|
| 995 |                                                                         toString( "candidate function not viable: too few mutex "
 | 
|---|
| 996 |                                                                         "arguments, expected ", n_mutex_param, "\n" ) );
 | 
|---|
| 997 |                                                         }
 | 
|---|
| 998 | 
 | 
|---|
| 999 |                                                         // All parameters match!
 | 
|---|
| 1000 | 
 | 
|---|
| 1001 |                                                         // Finish the expressions to tie in proper environments
 | 
|---|
| 1002 |                                                         finishExpr( func2->expr, resultEnv );
 | 
|---|
| 1003 |                                                         for ( CandidateRef & arg : argsList ) {
 | 
|---|
| 1004 |                                                                 finishExpr( arg->expr, resultEnv );
 | 
|---|
| 1005 |                                                         }
 | 
|---|
| 1006 | 
 | 
|---|
| 1007 |                                                         // This is a match, store it and save it for later
 | 
|---|
| 1008 |                                                         funcCandidates.emplace_back( std::move( func2 ) );
 | 
|---|
| 1009 |                                                         argsCandidates.emplace_back( std::move( argsList ) );
 | 
|---|
| 1010 | 
 | 
|---|
| 1011 |                                                 } catch ( SemanticErrorException & e ) {
 | 
|---|
| 1012 |                                                         errors.append( e );
 | 
|---|
| 1013 |                                                 }
 | 
|---|
| 1014 |                                         }
 | 
|---|
| 1015 |                                 } catch ( SemanticErrorException & e ) {
 | 
|---|
| 1016 |                                         errors.append( e );
 | 
|---|
| 1017 |                                 }
 | 
|---|
| 1018 |                         }
 | 
|---|
| 1019 | 
 | 
|---|
| 1020 |                         // Make sure correct number of arguments
 | 
|---|
| 1021 |                         if( funcCandidates.empty() ) {
 | 
|---|
| 1022 |                                 SemanticErrorException top( stmt->location,
 | 
|---|
| 1023 |                                         "No alternatives for function in call to waitfor" );
 | 
|---|
| 1024 |                                 top.append( errors );
 | 
|---|
| 1025 |                                 throw top;
 | 
|---|
| 1026 |                         }
 | 
|---|
| 1027 | 
 | 
|---|
| 1028 |                         if( argsCandidates.empty() ) {
 | 
|---|
| 1029 |                                 SemanticErrorException top( stmt->location,
 | 
|---|
| 1030 |                                         "No alternatives for arguments in call to waitfor" );
 | 
|---|
| 1031 |                                 top.append( errors );
 | 
|---|
| 1032 |                                 throw top;
 | 
|---|
| 1033 |                         }
 | 
|---|
| 1034 | 
 | 
|---|
| 1035 |                         if( funcCandidates.size() > 1 ) {
 | 
|---|
| 1036 |                                 SemanticErrorException top( stmt->location,
 | 
|---|
| 1037 |                                         "Ambiguous function in call to waitfor" );
 | 
|---|
| 1038 |                                 top.append( errors );
 | 
|---|
| 1039 |                                 throw top;
 | 
|---|
| 1040 |                         }
 | 
|---|
| 1041 |                         if( argsCandidates.size() > 1 ) {
 | 
|---|
| 1042 |                                 SemanticErrorException top( stmt->location,
 | 
|---|
| 1043 |                                         "Ambiguous arguments in call to waitfor" );
 | 
|---|
| 1044 |                                 top.append( errors );
 | 
|---|
| 1045 |                                 throw top;
 | 
|---|
| 1046 |                         }
 | 
|---|
| 1047 |                         // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used.
 | 
|---|
| 1048 | 
 | 
|---|
| 1049 |                         // build new clause
 | 
|---|
| 1050 |                         auto clause2 = new ast::WaitForClause( clause.location );
 | 
|---|
| 1051 | 
 | 
|---|
| 1052 |                         clause2->target = funcCandidates.front()->expr;
 | 
|---|
| 1053 | 
 | 
|---|
| 1054 |                         clause2->target_args.reserve( clause.target_args.size() );
 | 
|---|
| 1055 |                         const ast::StructDecl * decl_monitor = symtab.lookupStruct( "monitor$" );
 | 
|---|
| 1056 |                         for ( auto arg : argsCandidates.front() ) {
 | 
|---|
| 1057 |                                 const auto & loc = stmt->location;
 | 
|---|
| 1058 | 
 | 
|---|
| 1059 |                                 ast::Expr * init = new ast::CastExpr( loc,
 | 
|---|
| 1060 |                                         new ast::UntypedExpr( loc,
 | 
|---|
| 1061 |                                                 new ast::NameExpr( loc, "get_monitor" ),
 | 
|---|
| 1062 |                                                 { arg->expr }
 | 
|---|
| 1063 |                                         ),
 | 
|---|
| 1064 |                                         new ast::PointerType(
 | 
|---|
| 1065 |                                                 new ast::StructInstType(
 | 
|---|
| 1066 |                                                         decl_monitor
 | 
|---|
| 1067 |                                                 )
 | 
|---|
| 1068 |                                         )
 | 
|---|
| 1069 |                                 );
 | 
|---|
| 1070 | 
 | 
|---|
| 1071 |                                 clause2->target_args.emplace_back( findSingleExpression( init, context ) );
 | 
|---|
| 1072 |                         }
 | 
|---|
| 1073 | 
 | 
|---|
| 1074 |                         // Resolve the conditions as if it were an IfStmt, statements normally
 | 
|---|
| 1075 |                         clause2->when_cond = findSingleExpression( clause.when_cond, context );
 | 
|---|
| 1076 |                         clause2->stmt = clause.stmt->accept( *visitor );
 | 
|---|
| 1077 | 
 | 
|---|
| 1078 |                         // set results into stmt
 | 
|---|
| 1079 |                         auto n = mutate( stmt );
 | 
|---|
| 1080 |                         n->clauses[i] = clause2;
 | 
|---|
| 1081 |                         stmt = n;
 | 
|---|
| 1082 |                 }
 | 
|---|
| 1083 | 
 | 
|---|
| 1084 |                 if ( stmt->timeout_stmt ) {
 | 
|---|
| 1085 |                         // resolve the timeout as a size_t, the conditions like IfStmt, and stmts normally
 | 
|---|
| 1086 |                         ast::ptr< ast::Type > target =
 | 
|---|
| 1087 |                                 new ast::BasicType{ ast::BasicType::LongLongUnsignedInt };
 | 
|---|
| 1088 |                         auto timeout_time = findSingleExpression( stmt->timeout_time, target, context );
 | 
|---|
| 1089 |                         auto timeout_cond = findSingleExpression( stmt->timeout_cond, context );
 | 
|---|
| 1090 |                         auto timeout_stmt = stmt->timeout_stmt->accept( *visitor );
 | 
|---|
| 1091 | 
 | 
|---|
| 1092 |                         // set results into stmt
 | 
|---|
| 1093 |                         auto n = mutate( stmt );
 | 
|---|
| 1094 |                         n->timeout_time = std::move( timeout_time );
 | 
|---|
| 1095 |                         n->timeout_cond = std::move( timeout_cond );
 | 
|---|
| 1096 |                         n->timeout_stmt = std::move( timeout_stmt );
 | 
|---|
| 1097 |                         stmt = n;
 | 
|---|
| 1098 |                 }
 | 
|---|
| 1099 | 
 | 
|---|
| 1100 |                 if ( stmt->else_stmt ) {
 | 
|---|
| 1101 |                         // resolve the condition like IfStmt, stmts normally
 | 
|---|
| 1102 |                         auto else_cond = findSingleExpression( stmt->else_cond, context );
 | 
|---|
| 1103 |                         auto else_stmt = stmt->else_stmt->accept( *visitor );
 | 
|---|
| 1104 | 
 | 
|---|
| 1105 |                         // set results into stmt
 | 
|---|
| 1106 |                         auto n = mutate( stmt );
 | 
|---|
| 1107 |                         n->else_cond = std::move( else_cond );
 | 
|---|
| 1108 |                         n->else_stmt = std::move( else_stmt );
 | 
|---|
| 1109 |                         stmt = n;
 | 
|---|
| 1110 |                 }
 | 
|---|
| 1111 | 
 | 
|---|
| 1112 |                 return stmt;
 | 
|---|
| 1113 |         }
 | 
|---|
| 1114 | 
 | 
|---|
| 1115 |         const ast::WithStmt * Resolver::previsit( const ast::WithStmt * withStmt ) {
 | 
|---|
| 1116 |                 auto mutStmt = mutate(withStmt);
 | 
|---|
| 1117 |                 resolveWithExprs(mutStmt->exprs, stmtsToAddBefore);
 | 
|---|
| 1118 |                 return mutStmt;
 | 
|---|
| 1119 |         }
 | 
|---|
| 1120 | 
 | 
|---|
| 1121 |         void Resolver::resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd) {
 | 
|---|
| 1122 |                 for (auto & expr : exprs) {
 | 
|---|
| 1123 |                         // only struct- and union-typed expressions are viable candidates
 | 
|---|
| 1124 |                         expr = findKindExpression( expr, context, structOrUnion, "with expression" );
 | 
|---|
| 1125 | 
 | 
|---|
| 1126 |                         // if with expression might be impure, create a temporary so that it is evaluated once
 | 
|---|
| 1127 |                         if ( Tuples::maybeImpure( expr ) ) {
 | 
|---|
| 1128 |                                 static UniqueName tmpNamer( "_with_tmp_" );
 | 
|---|
| 1129 |                                 const CodeLocation loc = expr->location;
 | 
|---|
| 1130 |                                 auto tmp = new ast::ObjectDecl(loc, tmpNamer.newName(), expr->result, new ast::SingleInit(loc, expr ) );
 | 
|---|
| 1131 |                                 expr = new ast::VariableExpr( loc, tmp );
 | 
|---|
| 1132 |                                 stmtsToAdd.push_back( new ast::DeclStmt(loc, tmp ) );
 | 
|---|
| 1133 |                                 if ( InitTweak::isConstructable( tmp->type ) ) {
 | 
|---|
| 1134 |                                         // generate ctor/dtor and resolve them
 | 
|---|
| 1135 |                                         tmp->init = InitTweak::genCtorInit( loc, tmp );
 | 
|---|
| 1136 |                                 }
 | 
|---|
| 1137 |                                 // since tmp is freshly created, this should modify tmp in-place
 | 
|---|
| 1138 |                                 tmp->accept( *visitor );
 | 
|---|
| 1139 |                         }
 | 
|---|
| 1140 |                         else if (expr->env && expr->env->empty()) {
 | 
|---|
| 1141 |                                 expr = ast::mutate_field(expr.get(), &ast::Expr::env, nullptr);
 | 
|---|
| 1142 |                         }
 | 
|---|
| 1143 |                 }
 | 
|---|
| 1144 |         }
 | 
|---|
| 1145 | 
 | 
|---|
| 1146 | 
 | 
|---|
| 1147 |         const ast::SingleInit * Resolver::previsit( const ast::SingleInit * singleInit ) {
 | 
|---|
| 1148 |                 visit_children = false;
 | 
|---|
| 1149 |                 // resolve initialization using the possibilities as determined by the `currentObject`
 | 
|---|
| 1150 |                 // cursor.
 | 
|---|
| 1151 |                 ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{
 | 
|---|
| 1152 |                         singleInit->location, singleInit->value, currentObject.getOptions() };
 | 
|---|
| 1153 |                 ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, context );
 | 
|---|
| 1154 |                 const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >();
 | 
|---|
| 1155 | 
 | 
|---|
| 1156 |                 // move cursor to the object that is actually initialized
 | 
|---|
| 1157 |                 currentObject.setNext( initExpr->designation );
 | 
|---|
| 1158 | 
 | 
|---|
| 1159 |                 // discard InitExpr wrapper and retain relevant pieces.
 | 
|---|
| 1160 |                 // `initExpr` may have inferred params in the case where the expression specialized a
 | 
|---|
| 1161 |                 // function pointer, and newExpr may already have inferParams of its own, so a simple
 | 
|---|
| 1162 |                 // swap is not sufficient
 | 
|---|
| 1163 |                 ast::Expr::InferUnion inferred = initExpr->inferred;
 | 
|---|
| 1164 |                 swap_and_save_env( newExpr, initExpr->expr );
 | 
|---|
| 1165 |                 newExpr.get_and_mutate()->inferred.splice( std::move(inferred) );
 | 
|---|
| 1166 | 
 | 
|---|
| 1167 |                 // get the actual object's type (may not exactly match what comes back from the resolver
 | 
|---|
| 1168 |                 // due to conversions)
 | 
|---|
| 1169 |                 const ast::Type * initContext = currentObject.getCurrentType();
 | 
|---|
| 1170 | 
 | 
|---|
| 1171 |                 removeExtraneousCast( newExpr );
 | 
|---|
| 1172 | 
 | 
|---|
| 1173 |                 // check if actual object's type is char[]
 | 
|---|
| 1174 |                 if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) {
 | 
|---|
| 1175 |                         if ( isCharType( at->base ) ) {
 | 
|---|
| 1176 |                                 // check if the resolved type is char*
 | 
|---|
| 1177 |                                 if ( auto pt = newExpr->result.as< ast::PointerType >() ) {
 | 
|---|
| 1178 |                                         if ( isCharType( pt->base ) ) {
 | 
|---|
| 1179 |                                                 // strip cast if we're initializing a char[] with a char*
 | 
|---|
| 1180 |                                                 // e.g. char x[] = "hello"
 | 
|---|
| 1181 |                                                 if ( auto ce = newExpr.as< ast::CastExpr >() ) {
 | 
|---|
| 1182 |                                                         swap_and_save_env( newExpr, ce->arg );
 | 
|---|
| 1183 |                                                 }
 | 
|---|
| 1184 |                                         }
 | 
|---|
| 1185 |                                 }
 | 
|---|
| 1186 |                         }
 | 
|---|
| 1187 |                 }
 | 
|---|
| 1188 | 
 | 
|---|
| 1189 |                 // move cursor to next object in preparation for next initializer
 | 
|---|
| 1190 |                 currentObject.increment();
 | 
|---|
| 1191 | 
 | 
|---|
| 1192 |                 // set initializer expression to resolved expression
 | 
|---|
| 1193 |                 return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) );
 | 
|---|
| 1194 |         }
 | 
|---|
| 1195 | 
 | 
|---|
| 1196 |         const ast::ListInit * Resolver::previsit( const ast::ListInit * listInit ) {
 | 
|---|
| 1197 |                 // move cursor into brace-enclosed initializer-list
 | 
|---|
| 1198 |                 currentObject.enterListInit( listInit->location );
 | 
|---|
| 1199 | 
 | 
|---|
| 1200 |                 assert( listInit->designations.size() == listInit->initializers.size() );
 | 
|---|
| 1201 |                 for ( unsigned i = 0; i < listInit->designations.size(); ++i ) {
 | 
|---|
| 1202 |                         // iterate designations and initializers in pairs, moving the cursor to the current
 | 
|---|
| 1203 |                         // designated object and resolving the initializer against that object
 | 
|---|
| 1204 |                         listInit = ast::mutate_field_index(
 | 
|---|
| 1205 |                                 listInit, &ast::ListInit::designations, i,
 | 
|---|
| 1206 |                                 currentObject.findNext( listInit->designations[i] ) );
 | 
|---|
| 1207 |                         listInit = ast::mutate_field_index(
 | 
|---|
| 1208 |                                 listInit, &ast::ListInit::initializers, i,
 | 
|---|
| 1209 |                                 listInit->initializers[i]->accept( *visitor ) );
 | 
|---|
| 1210 |                 }
 | 
|---|
| 1211 | 
 | 
|---|
| 1212 |                 // move cursor out of brace-enclosed initializer-list
 | 
|---|
| 1213 |                 currentObject.exitListInit();
 | 
|---|
| 1214 | 
 | 
|---|
| 1215 |                 visit_children = false;
 | 
|---|
| 1216 |                 return listInit;
 | 
|---|
| 1217 |         }
 | 
|---|
| 1218 | 
 | 
|---|
| 1219 |         const ast::ConstructorInit * Resolver::previsit( const ast::ConstructorInit * ctorInit ) {
 | 
|---|
| 1220 |                 visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor );
 | 
|---|
| 1221 |                 visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor );
 | 
|---|
| 1222 | 
 | 
|---|
| 1223 |                 // found a constructor - can get rid of C-style initializer
 | 
|---|
| 1224 |                 // xxx - Rob suggests this field is dead code
 | 
|---|
| 1225 |                 ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr );
 | 
|---|
| 1226 | 
 | 
|---|
| 1227 |                 // intrinsic single-parameter constructors and destructors do nothing. Since this was
 | 
|---|
| 1228 |                 // implicitly generated, there's no way for it to have side effects, so get rid of it to
 | 
|---|
| 1229 |                 // clean up generated code
 | 
|---|
| 1230 |                 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) {
 | 
|---|
| 1231 |                         ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr );
 | 
|---|
| 1232 |                 }
 | 
|---|
| 1233 |                 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) {
 | 
|---|
| 1234 |                         ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr );
 | 
|---|
| 1235 |                 }
 | 
|---|
| 1236 | 
 | 
|---|
| 1237 |                 return ctorInit;
 | 
|---|
| 1238 |         }
 | 
|---|
| 1239 | 
 | 
|---|
| 1240 |         // suppress error on autogen functions and mark invalid autogen as deleted.
 | 
|---|
| 1241 |         bool Resolver::on_error(ast::ptr<ast::Decl> & decl) {
 | 
|---|
| 1242 |                 if (auto functionDecl = decl.as<ast::FunctionDecl>()) {
 | 
|---|
| 1243 |                         // xxx - can intrinsic gen ever fail?
 | 
|---|
| 1244 |                         if (functionDecl->linkage == ast::Linkage::AutoGen) {
 | 
|---|
| 1245 |                                 auto mutDecl = mutate(functionDecl);
 | 
|---|
| 1246 |                                 mutDecl->isDeleted = true;
 | 
|---|
| 1247 |                                 mutDecl->stmts = nullptr;
 | 
|---|
| 1248 |                                 decl = mutDecl;
 | 
|---|
| 1249 |                                 return false;
 | 
|---|
| 1250 |                         }
 | 
|---|
| 1251 |                 }
 | 
|---|
| 1252 |                 return true;
 | 
|---|
| 1253 |         }
 | 
|---|
| 1254 | 
 | 
|---|
| 1255 | } // namespace ResolvExpr
 | 
|---|
| 1256 | 
 | 
|---|
| 1257 | // Local Variables: //
 | 
|---|
| 1258 | // tab-width: 4 //
 | 
|---|
| 1259 | // mode: c++ //
 | 
|---|
| 1260 | // compile-command: "make install" //
 | 
|---|
| 1261 | // End: //
 | 
|---|