Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : : #include "rust-diagnostics.h"
26 : : #include "rust-hir-full-decls.h"
27 : :
28 : : namespace Rust {
29 : : namespace Compile {
30 : :
31 : 2068 : class CompileExternItem : public HIRCompileBase,
32 : : public HIR::HIRExternalItemVisitor
33 : : {
34 : : public:
35 : 2068 : static tree compile (HIR::ExternalItem *item, Context *ctx,
36 : : TyTy::BaseType *concrete = nullptr,
37 : : location_t ref_locus = UNDEF_LOCATION)
38 : : {
39 : 2068 : CompileExternItem compiler (ctx, concrete, ref_locus);
40 : 2068 : item->accept_vis (compiler);
41 : 2068 : return compiler.reference;
42 : 2068 : }
43 : :
44 : 0 : void visit (HIR::ExternalStaticItem &item) override
45 : : {
46 : : // check if its already been compiled
47 : 0 : Bvariable *lookup = Bvariable::error_variable ();
48 : 0 : if (ctx->lookup_var_decl (item.get_mappings ().get_hirid (), &lookup))
49 : : {
50 : 0 : reference = Backend::var_expression (lookup, ref_locus);
51 : 0 : return;
52 : : }
53 : :
54 : 0 : TyTy::BaseType *resolved_type = nullptr;
55 : 0 : bool ok = ctx->get_tyctx ()->lookup_type (item.get_mappings ().get_hirid (),
56 : : &resolved_type);
57 : 0 : rust_assert (ok);
58 : :
59 : 0 : std::string name = item.get_item_name ().as_string ();
60 : : // FIXME this is assuming C ABI
61 : 0 : std::string asm_name = name;
62 : :
63 : 0 : tree type = TyTyResolveCompile::compile (ctx, resolved_type);
64 : 0 : bool is_external = true;
65 : 0 : bool is_hidden = false;
66 : 0 : bool in_unique_section = false;
67 : :
68 : 0 : Bvariable *static_global
69 : 0 : = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
70 : : in_unique_section, item.get_locus ());
71 : 0 : ctx->insert_var_decl (item.get_mappings ().get_hirid (), static_global);
72 : 0 : ctx->push_var (static_global);
73 : :
74 : 0 : reference = Backend::var_expression (static_global, ref_locus);
75 : 0 : }
76 : :
77 : 2068 : void visit (HIR::ExternalFunctionItem &function) override
78 : : {
79 : 2068 : TyTy::BaseType *fntype_tyty;
80 : 2068 : if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
81 : : &fntype_tyty))
82 : : {
83 : 0 : rust_fatal_error (function.get_locus (),
84 : : "failed to lookup function type");
85 : 1155 : return;
86 : : }
87 : :
88 : 2068 : rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
89 : 2068 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
90 : 2068 : if (fntype->has_substitutions_defined ())
91 : : {
92 : : // we cant do anything for this only when it is used and a concrete type
93 : : // is given
94 : 786 : if (concrete == nullptr)
95 : : return;
96 : : else
97 : : {
98 : 0 : rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
99 : 0 : fntype = static_cast<TyTy::FnType *> (concrete);
100 : : }
101 : : }
102 : :
103 : : // items can be forward compiled which means we may not need to invoke this
104 : : // code. We might also have already compiled this generic function as well.
105 : 1282 : tree lookup = NULL_TREE;
106 : 1282 : if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
107 : : fntype->get_id (), fntype))
108 : : {
109 : 4 : reference = address_expression (lookup, ref_locus);
110 : 4 : return;
111 : : }
112 : :
113 : 1278 : if (fntype->has_substitutions_defined ())
114 : : // override the HIR lookups for the substitutions in this context
115 : 0 : fntype->override_context ();
116 : :
117 : 1278 : if (fntype->get_abi () == ABI::INTRINSIC)
118 : : {
119 : 365 : Intrinsics compile (ctx);
120 : 365 : tree fndecl = compile.compile (fntype);
121 : 365 : ctx->insert_function_decl (fntype, fndecl);
122 : 365 : return;
123 : : }
124 : :
125 : 913 : tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
126 : 1826 : std::string ir_symbol_name = function.get_item_name ().as_string ();
127 : 1826 : std::string asm_name = function.get_item_name ().as_string ();
128 : 913 : if (fntype->get_abi () == ABI::RUST)
129 : : {
130 : : // then we need to get the canonical path of it and mangle it
131 : 0 : auto canonical_path = ctx->get_mappings ().lookup_canonical_path (
132 : 0 : function.get_mappings ().get_nodeid ());
133 : :
134 : 0 : ir_symbol_name = canonical_path->get () + fntype->subst_as_string ();
135 : 0 : asm_name = ctx->mangle_item (fntype, *canonical_path);
136 : : }
137 : :
138 : 913 : const unsigned int flags = Backend::function_is_declaration;
139 : 913 : tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
140 : : flags, function.get_locus ());
141 : 913 : TREE_PUBLIC (fndecl) = 1;
142 : 913 : setup_abi_options (fndecl, fntype->get_abi ());
143 : :
144 : 913 : ctx->insert_function_decl (fntype, fndecl);
145 : :
146 : 913 : reference = address_expression (fndecl, ref_locus);
147 : 913 : }
148 : :
149 : 0 : void visit (HIR::ExternalTypeItem &type) override
150 : : {
151 : 0 : rust_sorry_at (type.get_locus (), "extern types are not supported yet");
152 : 0 : }
153 : :
154 : : private:
155 : 2068 : CompileExternItem (Context *ctx, TyTy::BaseType *concrete,
156 : : location_t ref_locus)
157 : 2068 : : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
158 : 2068 : ref_locus (ref_locus)
159 : : {}
160 : :
161 : : TyTy::BaseType *concrete;
162 : : tree reference;
163 : : location_t ref_locus;
164 : : };
165 : :
166 : : } // namespace Compile
167 : : } // namespace Rust
168 : :
169 : : #endif // RUST_COMPILE_EXTERN_ITEM
|