@@ -322,7 +322,7 @@ bool ResolveFuncTypeWithEmptySignature(const Module& module,
322322Result ResolveRefTypes (const Module& module ,
323323 TypeVector& types,
324324 uint32_t base_index,
325- FuncSignature ::ReferenceVars& ref_vars,
325+ WastParserContext ::ReferenceVars& ref_vars,
326326 Errors* errors) {
327327 Result result = Result::Ok;
328328 size_t types_size = types.size ();
@@ -361,19 +361,6 @@ Result ResolveRefTypes(const Module& module,
361361 return Result::Ok;
362362}
363363
364- Result ResolveTypeNames (const Module& module ,
365- FuncSignature& signature,
366- Errors* errors) {
367- Result result = Result::Ok;
368-
369- result |= ResolveRefTypes (module , signature.param_types , 0 ,
370- signature.param_type_vars , errors);
371-
372- result |= ResolveRefTypes (module , signature.result_types , 0 ,
373- signature.result_type_vars , errors);
374- return result;
375- }
376-
377364void ResolveImplicitlyDefinedFunctionType (const Location& loc,
378365 Module* module ,
379366 const FuncDeclaration& decl) {
@@ -475,11 +462,11 @@ bool IsInlinableFuncSignature(const FuncSignature& sig) {
475462
476463class ResolveFuncTypesExprVisitorDelegate : public ExprVisitor ::DelegateNop {
477464 public:
478- explicit ResolveFuncTypesExprVisitorDelegate (Module* module , Errors* errors)
479- : module_(module ), errors_(errors) {}
465+ explicit ResolveFuncTypesExprVisitorDelegate (WastParserContext* parser_context, Module* module , Errors* errors)
466+ : parser_context_(parser_context), module_(module ), errors_(errors) {}
480467
481468 void ResolveBlockDeclaration (const Location& loc, BlockDeclaration* decl) {
482- ResolveTypeNames (*module_, decl->sig , errors_);
469+ parser_context_-> ResolveTypeNames (*module_, & decl->sig , errors_);
483470 ResolveFuncTypeWithEmptySignature (*module_, decl);
484471 if (!IsInlinableFuncSignature (decl->sig )) {
485472 ResolveImplicitlyDefinedFunctionType (loc, module_, *decl);
@@ -525,11 +512,12 @@ class ResolveFuncTypesExprVisitorDelegate : public ExprVisitor::DelegateNop {
525512 }
526513
527514 private:
515+ WastParserContext* parser_context_;
528516 Module* module_;
529517 Errors* errors_;
530518};
531519
532- Result ResolveFuncTypes (Module* module , Errors* errors) {
520+ Result ResolveFuncTypes (WastParserContext* parser_context, Module* module , Errors* errors) {
533521 Result result = Result::Ok;
534522 for (ModuleField& field : module ->fields ) {
535523 Func* func = nullptr ;
@@ -556,7 +544,7 @@ Result ResolveFuncTypes(Module* module, Errors* errors) {
556544
557545 if (entry->kind () == TypeEntryKind::Func) {
558546 FuncType* func_type = cast<FuncType>(entry);
559- result |= ResolveTypeNames (*module , func_type->sig , errors);
547+ result |= parser_context-> ResolveTypeNames (*module , & func_type->sig , errors);
560548 }
561549 continue ;
562550 } else {
@@ -566,7 +554,7 @@ Result ResolveFuncTypes(Module* module, Errors* errors) {
566554 bool has_func_type_and_empty_signature = false ;
567555
568556 if (decl) {
569- result |= ResolveTypeNames (*module , decl->sig , errors);
557+ result |= parser_context-> ResolveTypeNames (*module , & decl->sig , errors);
570558 has_func_type_and_empty_signature =
571559 ResolveFuncTypeWithEmptySignature (*module , decl);
572560 ResolveImplicitlyDefinedFunctionType (field.loc , module , *decl);
@@ -575,13 +563,16 @@ Result ResolveFuncTypes(Module* module, Errors* errors) {
575563 }
576564
577565 if (func) {
578- if (!func->local_type_list .empty ()) {
579- result |= ResolveRefTypes (*module , func->local_type_list ,
566+ auto it = parser_context->local_type_data .find (func);
567+ if (it != parser_context->local_type_data .end ()) {
568+ auto data = parser_context->func_sig_data [&decl->sig ];
569+ assert (!data.local_type_vars .empty ());
570+
571+ result |= ResolveRefTypes (*module , it->second ,
580572 func->GetNumParams (),
581- decl-> sig . param_type_vars , errors);
573+ data. local_type_vars , errors);
582574
583- func->local_types .Set (func->local_type_list );
584- func->local_type_list .clear ();
575+ func->local_types .Set (it->second );
585576 }
586577
587578 if (has_func_type_and_empty_signature) {
@@ -595,7 +586,7 @@ Result ResolveFuncTypes(Module* module, Errors* errors) {
595586 }
596587 }
597588
598- ResolveFuncTypesExprVisitorDelegate delegate (module , errors);
589+ ResolveFuncTypesExprVisitorDelegate delegate (parser_context, module , errors);
599590 ExprVisitor visitor (&delegate);
600591 result |= visitor.VisitFunc (func);
601592 }
@@ -618,6 +609,22 @@ void AppendInlineExportFields(Module* module,
618609
619610} // End of anonymous namespace
620611
612+ Result WastParserContext::ResolveTypeNames (const Module& module ,
613+ FuncSignature* signature,
614+ Errors* errors) {
615+ Result result = Result::Ok;
616+ auto it = func_sig_data.find (signature);
617+
618+ if (it != func_sig_data.end ()) {
619+ result |= ResolveRefTypes (module , signature->param_types , 0 ,
620+ it->second .param_type_vars , errors);
621+
622+ result |= ResolveRefTypes (module , signature->result_types , 0 ,
623+ it->second .result_type_vars , errors);
624+ }
625+ return result;
626+ }
627+
621628WastParser::WastParser (WastLexer* lexer,
622629 Errors* errors,
623630 WastParseOptions* options)
@@ -1000,7 +1007,7 @@ Result WastParser::ParseValueType(Var* out_type) {
10001007}
10011008
10021009Result WastParser::ParseValueTypeList (TypeVector* out_type_list,
1003- FuncSignature ::ReferenceVars* type_vars,
1010+ WastParserContext ::ReferenceVars* type_vars,
10041011 Index binding_index_offset) {
10051012 WABT_TRACE (ParseValueTypeList);
10061013 while (true ) {
@@ -1022,7 +1029,7 @@ Result WastParser::ParseValueTypeList(TypeVector* out_type_list,
10221029 assert (type.is_name ());
10231030 assert (options_->features .function_references_enabled ());
10241031 uint32_t index = binding_index_offset + out_type_list->size ();
1025- type_vars->push_back (FuncSignature ::ReferenceVar (index, type));
1032+ type_vars->push_back (WastParserContext ::ReferenceVar (index, type));
10261033 out_type_list->push_back (Type (Type::Reference, kInvalidIndex ));
10271034 }
10281035 }
@@ -1380,7 +1387,7 @@ Result WastParser::ParseModuleFieldList(Module* module) {
13801387 CHECK_RESULT (Synchronize (IsModuleField));
13811388 }
13821389 }
1383- CHECK_RESULT (ResolveFuncTypes (module , errors_));
1390+ CHECK_RESULT (ResolveFuncTypes (&context_, module , errors_));
13841391 CHECK_RESULT (ResolveNamesModule (module , errors_));
13851392 return Result::Ok;
13861393}
@@ -1585,15 +1592,19 @@ Result WastParser::ParseFuncModuleField(Module* module) {
15851592 CHECK_RESULT (ParseTypeUseOpt (&func.decl ));
15861593 CHECK_RESULT (ParseFuncSignature (&func.decl .sig , &func.bindings ));
15871594
1588- size_t names_size = func.decl .sig .param_type_vars .size ();
1595+ WastParserContext::ReferenceVars locals_references;
1596+ TypeVector types;
1597+
15891598 CHECK_RESULT (ParseBoundValueTypeList (
1590- TokenType::Local, &func. local_type_list , &func.bindings ,
1591- &func. decl . sig . param_type_vars , func.GetNumParams ()));
1599+ TokenType::Local, &types , &func.bindings ,
1600+ &locals_references , func.GetNumParams ()));
15921601
1593- if (names_size == func. decl . sig . param_type_vars . size ()) {
1602+ if (locals_references. empty ()) {
15941603 // No named references in the list, local types can be processed now.
1595- func.local_types .Set (func.local_type_list );
1596- func.local_type_list .clear ();
1604+ func.local_types .Set (types);
1605+ } else {
1606+ context_.local_type_data [&func] = std::move (types);
1607+ context_.func_sig_data [&func.decl .sig ].local_type_vars = std::move (locals_references);
15971608 }
15981609
15991610 CHECK_RESULT (ParseTerminatingInstrList (&func.exprs ));
@@ -2016,25 +2027,41 @@ Result WastParser::ParseTypeUseOpt(FuncDeclaration* decl) {
20162027Result WastParser::ParseFuncSignature (FuncSignature* sig,
20172028 BindingHash* param_bindings) {
20182029 WABT_TRACE (ParseFuncSignature);
2030+ assert (context_.func_sig_data .find (sig) == context_.func_sig_data .end ());
2031+
2032+ WastParserContext::FuncSignatureExtraData extra_data;
2033+
20192034 CHECK_RESULT (ParseBoundValueTypeList (TokenType::Param, &sig->param_types ,
2020- param_bindings, &sig->param_type_vars ));
2021- CHECK_RESULT (ParseResultList (&sig->result_types , &sig->result_type_vars ));
2035+ param_bindings, &extra_data.param_type_vars ));
2036+ CHECK_RESULT (ParseResultList (&sig->result_types , &extra_data.result_type_vars ));
2037+
2038+ if (!extra_data.param_type_vars .empty () || !extra_data.result_type_vars .empty ()) {
2039+ context_.func_sig_data [sig] = std::move (extra_data);
2040+ }
2041+
20222042 return Result::Ok;
20232043}
20242044
20252045Result WastParser::ParseUnboundFuncSignature (FuncSignature* sig) {
20262046 WABT_TRACE (ParseUnboundFuncSignature);
2047+ assert (context_.func_sig_data .find (sig) == context_.func_sig_data .end ());
2048+
2049+ WastParserContext::FuncSignatureExtraData extra_data;
20272050 CHECK_RESULT (ParseUnboundValueTypeList (TokenType::Param, &sig->param_types ,
2028- &sig->param_type_vars ));
2029- CHECK_RESULT (ParseResultList (&sig->result_types , &sig->result_type_vars ));
2051+ &extra_data.param_type_vars ));
2052+ CHECK_RESULT (ParseResultList (&sig->result_types , &extra_data.result_type_vars ));
2053+
2054+ if (!extra_data.param_type_vars .empty () || !extra_data.result_type_vars .empty ()) {
2055+ context_.func_sig_data [sig] = std::move (extra_data);
2056+ }
20302057 return Result::Ok;
20312058}
20322059
20332060Result WastParser::ParseBoundValueTypeList (
20342061 TokenType token,
20352062 TypeVector* types,
20362063 BindingHash* bindings,
2037- FuncSignature ::ReferenceVars* type_vars,
2064+ WastParserContext ::ReferenceVars* type_vars,
20382065 Index binding_index_offset) {
20392066 WABT_TRACE (ParseBoundValueTypeList);
20402067 while (MatchLpar (token)) {
@@ -2052,7 +2079,7 @@ Result WastParser::ParseBoundValueTypeList(
20522079 assert (type.is_name ());
20532080 assert (options_->features .function_references_enabled ());
20542081 uint32_t index = binding_index_offset + types->size ();
2055- type_vars->push_back (FuncSignature ::ReferenceVar (index, type));
2082+ type_vars->push_back (WastParserContext ::ReferenceVar (index, type));
20562083 types->push_back (Type (Type::Reference, kInvalidIndex ));
20572084 }
20582085 } else {
@@ -2066,7 +2093,7 @@ Result WastParser::ParseBoundValueTypeList(
20662093Result WastParser::ParseUnboundValueTypeList (
20672094 TokenType token,
20682095 TypeVector* types,
2069- FuncSignature ::ReferenceVars* type_vars) {
2096+ WastParserContext ::ReferenceVars* type_vars) {
20702097 WABT_TRACE (ParseUnboundValueTypeList);
20712098 while (MatchLpar (token)) {
20722099 CHECK_RESULT (ParseValueTypeList (types, type_vars, 0 ));
@@ -2076,7 +2103,7 @@ Result WastParser::ParseUnboundValueTypeList(
20762103}
20772104
20782105Result WastParser::ParseResultList (TypeVector* result_types,
2079- FuncSignature ::ReferenceVars* type_vars) {
2106+ WastParserContext ::ReferenceVars* type_vars) {
20802107 WABT_TRACE (ParseResultList);
20812108 return ParseUnboundValueTypeList (TokenType::Result, result_types, type_vars);
20822109}
@@ -3152,10 +3179,9 @@ Result WastParser::ParseBlockDeclaration(BlockDeclaration* decl) {
31523179 WABT_TRACE (ParseBlockDeclaration);
31533180 FuncDeclaration func_decl;
31543181 CHECK_RESULT (ParseTypeUseOpt (&func_decl));
3155- CHECK_RESULT (ParseUnboundFuncSignature (&func_decl. sig ));
3182+ CHECK_RESULT (ParseUnboundFuncSignature (&decl-> sig ));
31563183 decl->has_func_type = func_decl.has_func_type ;
31573184 decl->type_var = func_decl.type_var ;
3158- decl->sig = func_decl.sig ;
31593185 return Result::Ok;
31603186}
31613187
0 commit comments