|
28 | 28 | namespace Rust {
|
29 | 29 | namespace Resolver {
|
30 | 30 |
|
| 31 | +class TypeCheckTopLevelExternItem : public TypeCheckBase |
| 32 | +{ |
| 33 | + using Rust::Resolver::TypeCheckBase::visit; |
| 34 | + |
| 35 | +public: |
| 36 | + static void Resolve (HIR::ExternalItem *item) |
| 37 | + { |
| 38 | + TypeCheckTopLevelExternItem resolver; |
| 39 | + item->accept_vis (resolver); |
| 40 | + } |
| 41 | + |
| 42 | + void visit (HIR::ExternalStaticItem &item) override |
| 43 | + { |
| 44 | + TyTy::BaseType *actual_type |
| 45 | + = TypeCheckType::Resolve (item.get_item_type ().get ()); |
| 46 | + |
| 47 | + context->insert_type (item.get_mappings (), actual_type); |
| 48 | + } |
| 49 | + |
| 50 | + void visit (HIR::ExternalFunctionItem &function) override |
| 51 | + { |
| 52 | + std::vector<TyTy::SubstitutionParamMapping> substitutions; |
| 53 | + if (function.has_generics ()) |
| 54 | + { |
| 55 | + for (auto &generic_param : function.get_generic_params ()) |
| 56 | + { |
| 57 | + switch (generic_param.get ()->get_kind ()) |
| 58 | + { |
| 59 | + case HIR::GenericParam::GenericKind::LIFETIME: |
| 60 | + // Skipping Lifetime completely until better handling. |
| 61 | + break; |
| 62 | + |
| 63 | + case HIR::GenericParam::GenericKind::TYPE: { |
| 64 | + auto param_type |
| 65 | + = TypeResolveGenericParam::Resolve (generic_param.get ()); |
| 66 | + context->insert_type (generic_param->get_mappings (), |
| 67 | + param_type); |
| 68 | + |
| 69 | + substitutions.push_back (TyTy::SubstitutionParamMapping ( |
| 70 | + static_cast<HIR::TypeParam &> (*generic_param), |
| 71 | + param_type)); |
| 72 | + } |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + TyTy::BaseType *ret_type = nullptr; |
| 79 | + if (!function.has_return_type ()) |
| 80 | + ret_type = new TyTy::TupleType (function.get_mappings ().get_hirid ()); |
| 81 | + else |
| 82 | + { |
| 83 | + auto resolved |
| 84 | + = TypeCheckType::Resolve (function.get_return_type ().get ()); |
| 85 | + if (resolved == nullptr) |
| 86 | + { |
| 87 | + rust_error_at (function.get_locus (), |
| 88 | + "failed to resolve return type"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + ret_type = resolved->clone (); |
| 93 | + ret_type->set_ref ( |
| 94 | + function.get_return_type ()->get_mappings ().get_hirid ()); |
| 95 | + } |
| 96 | + |
| 97 | + std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params; |
| 98 | + for (auto ¶m : function.get_function_params ()) |
| 99 | + { |
| 100 | + // get the name as well required for later on |
| 101 | + auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); |
| 102 | + |
| 103 | + HIR::IdentifierPattern *param_pattern = new HIR::IdentifierPattern ( |
| 104 | + param.get_param_name (), Location (), false, false, |
| 105 | + std::unique_ptr<HIR::Pattern> (nullptr)); |
| 106 | + |
| 107 | + params.push_back ( |
| 108 | + std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern, |
| 109 | + param_tyty)); |
| 110 | + |
| 111 | + context->insert_type (param.get_mappings (), param_tyty); |
| 112 | + } |
| 113 | + |
| 114 | + auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (), |
| 115 | + function.get_mappings ().get_defid (), |
| 116 | + function.get_item_name (), |
| 117 | + FNTYPE_IS_EXTERN_FLAG, std::move (params), |
| 118 | + ret_type, std::move (substitutions)); |
| 119 | + context->insert_type (function.get_mappings (), fnType); |
| 120 | + } |
| 121 | + |
| 122 | +private: |
| 123 | + TypeCheckTopLevelExternItem () : TypeCheckBase () {} |
| 124 | +}; |
| 125 | + |
31 | 126 | class TypeCheckTopLevelImplItem : public TypeCheckBase
|
32 | 127 | {
|
33 | 128 | using Rust::Resolver::TypeCheckBase::visit;
|
@@ -134,11 +229,11 @@ class TypeCheckTopLevelImplItem : public TypeCheckBase
|
134 | 229 | context->insert_type (param.get_mappings (), param_tyty);
|
135 | 230 | }
|
136 | 231 |
|
137 |
| - auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (), |
138 |
| - function.get_mappings ().get_defid (), |
139 |
| - function.get_function_name (), |
140 |
| - function.is_method (), std::move (params), |
141 |
| - ret_type, std::move (substitutions)); |
| 232 | + auto fnType = new TyTy::FnType ( |
| 233 | + function.get_mappings ().get_hirid (), |
| 234 | + function.get_mappings ().get_defid (), function.get_function_name (), |
| 235 | + function.is_method () ? FNTYPE_IS_METHOD_FLAG : FNTYPE_DEFAULT_FLAGS, |
| 236 | + std::move (params), ret_type, std::move (substitutions)); |
142 | 237 | context->insert_type (function.get_mappings (), fnType);
|
143 | 238 | }
|
144 | 239 |
|
|
0 commit comments