Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #ifndef RUST_COMPILE_EXTERN_ITEM
20 : : #define RUST_COMPILE_EXTERN_ITEM
21 : :
22 : : #include "rust-compile-base.h"
23 : : #include "rust-compile-intrinsic.h"
24 : : #include "rust-compile-type.h"
25 : :
26 : : namespace Rust {
27 : : namespace Compile {
28 : :
29 : 1574 : class CompileExternItem : public HIRCompileBase,
30 : : public HIR::HIRExternalItemVisitor
31 : : {
32 : : public:
33 : 1574 : static tree compile (HIR::ExternalItem *item, Context *ctx,
34 : : TyTy::BaseType *concrete = nullptr,
35 : : bool is_query_mode = false,
36 : : location_t ref_locus = UNDEF_LOCATION)
37 : : {
38 : 1574 : CompileExternItem compiler (ctx, concrete, ref_locus);
39 : 1574 : item->accept_vis (compiler);
40 : :
41 : 1574 : if (is_query_mode && compiler.reference == error_mark_node)
42 : 0 : rust_internal_error_at (ref_locus, "failed to compile extern item: %s",
43 : 0 : item->as_string ().c_str ());
44 : :
45 : 1574 : return compiler.reference;
46 : 1574 : }
47 : :
48 : 0 : void visit (HIR::ExternalStaticItem &item) override
49 : : {
50 : : // check if its already been compiled
51 : 0 : Bvariable *lookup = Bvariable::error_variable ();
52 : 0 : if (ctx->lookup_var_decl (item.get_mappings ().get_hirid (), &lookup))
53 : : {
54 : 0 : reference = Backend::var_expression (lookup, ref_locus);
55 : 0 : return;
56 : : }
57 : :
58 : 0 : TyTy::BaseType *resolved_type = nullptr;
59 : 0 : bool ok = ctx->get_tyctx ()->lookup_type (item.get_mappings ().get_hirid (),
60 : : &resolved_type);
61 : 0 : rust_assert (ok);
62 : :
63 : 0 : std::string name = item.get_item_name ().as_string ();
64 : : // FIXME this is assuming C ABI
65 : 0 : std::string asm_name = name;
66 : :
67 : 0 : tree type = TyTyResolveCompile::compile (ctx, resolved_type);
68 : 0 : bool is_external = true;
69 : 0 : bool is_hidden = false;
70 : 0 : bool in_unique_section = false;
71 : :
72 : 0 : Bvariable *static_global
73 : 0 : = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
74 : : in_unique_section, item.get_locus ());
75 : 0 : ctx->insert_var_decl (item.get_mappings ().get_hirid (), static_global);
76 : 0 : ctx->push_var (static_global);
77 : :
78 : 0 : reference = Backend::var_expression (static_global, ref_locus);
79 : 0 : }
80 : :
81 : 1574 : void visit (HIR::ExternalFunctionItem &function) override
82 : : {
83 : 1574 : TyTy::BaseType *fntype_tyty;
84 : 1574 : if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
85 : : &fntype_tyty))
86 : : {
87 : 0 : rust_fatal_error (function.get_locus (),
88 : : "failed to lookup function type");
89 : 819 : return;
90 : : }
91 : :
92 : 1574 : rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
93 : 1574 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
94 : 1574 : if (fntype->has_substitutions_defined ())
95 : : {
96 : : // we cant do anything for this only when it is used and a concrete type
97 : : // is given
98 : 501 : if (concrete == nullptr)
99 : : return;
100 : : else
101 : : {
102 : 0 : rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
103 : 0 : fntype = static_cast<TyTy::FnType *> (concrete);
104 : : }
105 : : }
106 : :
107 : : // items can be forward compiled which means we may not need to invoke this
108 : : // code. We might also have already compiled this generic function as well.
109 : 1073 : tree lookup = NULL_TREE;
110 : 1073 : if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
111 : : fntype->get_id (), fntype))
112 : : {
113 : 2 : reference = address_expression (lookup, ref_locus);
114 : 2 : return;
115 : : }
116 : :
117 : 1071 : if (fntype->has_substitutions_defined ())
118 : : // override the HIR lookups for the substitutions in this context
119 : 0 : fntype->override_context ();
120 : :
121 : 1071 : if (fntype->get_abi () == ABI::INTRINSIC)
122 : : {
123 : 316 : Intrinsics compile (ctx);
124 : 316 : tree fndecl = compile.compile (fntype);
125 : 316 : ctx->insert_function_decl (fntype, fndecl);
126 : 316 : return;
127 : : }
128 : :
129 : 755 : tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
130 : 755 : std::string ir_symbol_name = function.get_item_name ().as_string ();
131 : 755 : std::string asm_name = function.get_item_name ().as_string ();
132 : 755 : if (fntype->get_abi () == ABI::RUST)
133 : : {
134 : : // then we need to get the canonical path of it and mangle it
135 : 0 : const Resolver::CanonicalPath *canonical_path = nullptr;
136 : 0 : bool ok = ctx->get_mappings ()->lookup_canonical_path (
137 : 0 : function.get_mappings ().get_nodeid (), &canonical_path);
138 : 0 : rust_assert (ok);
139 : :
140 : 0 : ir_symbol_name = canonical_path->get () + fntype->subst_as_string ();
141 : 0 : asm_name = ctx->mangle_item (fntype, *canonical_path);
142 : : }
143 : :
144 : 755 : const unsigned int flags = Backend::function_is_declaration;
145 : 755 : tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
146 : : flags, function.get_locus ());
147 : 755 : TREE_PUBLIC (fndecl) = 1;
148 : 755 : setup_abi_options (fndecl, fntype->get_abi ());
149 : :
150 : 755 : ctx->insert_function_decl (fntype, fndecl);
151 : :
152 : 755 : reference = address_expression (fndecl, ref_locus);
153 : 755 : }
154 : :
155 : : private:
156 : 1574 : CompileExternItem (Context *ctx, TyTy::BaseType *concrete,
157 : : location_t ref_locus)
158 : 1574 : : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
159 : 1574 : ref_locus (ref_locus)
160 : : {}
161 : :
162 : : TyTy::BaseType *concrete;
163 : : tree reference;
164 : : location_t ref_locus;
165 : : };
166 : :
167 : : } // namespace Compile
168 : : } // namespace Rust
169 : :
170 : : #endif // RUST_COMPILE_EXTERN_ITEM
|