1 // Copyright (C) 2020-2023 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_BASE
20 #define RUST_COMPILE_BASE
21
22 #include "rust-compile-context.h"
23 #include "rust-compile-type.h"
24 #include "rust-hir-visitor.h"
25 #include "rust-hir-full.h"
26
27 namespace Rust {
28 namespace Compile {
29
30 class HIRCompileBase
31 {
32 public:
33 virtual ~HIRCompileBase () {}
34
35 protected:
36 HIRCompileBase (Context *ctx) : ctx (ctx) {}
37
38 Context *ctx;
39
40 protected:
41 Context *get_context () { return ctx; }
42
43 tree coercion_site (HirId id, tree rvalue, const TyTy::BaseType *actual,
44 const TyTy::BaseType *expected, Location lvalue_locus,
45 Location rvalue_locus);
46 tree coercion_site1 (tree rvalue, const TyTy::BaseType *actual,
47 const TyTy::BaseType *expected, Location lvalue_locus,
48 Location rvalue_locus);
49
50 tree coerce_to_dyn_object (tree compiled_ref, const TyTy::BaseType *actual,
51 const TyTy::DynamicObjectType *ty, Location locus);
52
53 tree compute_address_for_trait_item (
54 const Resolver::TraitItemReference *ref,
55 const TyTy::TypeBoundPredicate *predicate,
56 std::vector<std::pair<Resolver::TraitReference *, HIR::ImplBlock *>>
57 &receiver_bounds,
58 const TyTy::BaseType *receiver, const TyTy::BaseType *root, Location locus);
59
60 bool verify_array_capacities (tree ltype, tree rtype, Location ltype_locus,
61 Location rtype_locus);
62
63 tree query_compile (HirId ref, TyTy::BaseType *lookup,
64 const HIR::PathIdentSegment &final_segment,
65 const Analysis::NodeMapping &mappings,
66 Location expr_locus, bool is_qualified_path);
67
68 tree resolve_adjustements (std::vector<Resolver::Adjustment> &adjustments,
69 tree expression, Location locus);
70
71 tree resolve_deref_adjustment (Resolver::Adjustment &adjustment,
72 tree expression, Location locus);
73
74 tree resolve_indirection_adjustment (Resolver::Adjustment &adjustment,
75 tree expression, Location locus);
76
77 tree resolve_unsized_adjustment (Resolver::Adjustment &adjustment,
78 tree expression, Location locus);
79
80 tree resolve_unsized_slice_adjustment (Resolver::Adjustment &adjustment,
81 tree expression, Location locus);
82
83 tree resolve_unsized_dyn_adjustment (Resolver::Adjustment &adjustment,
84 tree expression, Location locus);
85
86 static void setup_fndecl (tree fndecl, bool is_main_entry_point,
87 bool is_generic_fn, HIR::Visibility &visibility,
88 const HIR::FunctionQualifiers &qualifiers,
89 const AST::AttrVec &attrs);
90
91 static void handle_inline_attribute_on_fndecl (tree fndecl,
92 const AST::Attribute &attr);
93
94 static void handle_cold_attribute_on_fndecl (tree fndecl,
95 const AST::Attribute &attr);
96
97 static void handle_must_use_attribute_on_fndecl (tree fndecl,
98 const AST::Attribute &attr);
99
100 static void
101 handle_link_section_attribute_on_fndecl (tree fndecl,
102 const AST::Attribute &attr);
103 static void
104 handle_deprecated_attribute_on_fndecl (tree fndecl,
105 const AST::Attribute &attr);
106
107 static void handle_no_mangle_attribute_on_fndecl (tree fndecl,
108 const AST::Attribute &attr);
109
110 static void setup_abi_options (tree fndecl, ABI abi);
111
112 static tree address_expression (tree expr, Location locus);
113
114 static tree indirect_expression (tree expr, Location locus);
115
116 static bool mark_addressable (tree, Location);
117
118 static std::vector<Bvariable *>
119 compile_locals_for_block (Context *ctx, Resolver::Rib &rib, tree fndecl);
120
121 static void compile_function_body (Context *ctx, tree fndecl,
122 HIR::BlockExpr &function_body,
123 bool has_return_type);
124
125 static tree compile_function (
126 Context *ctx, const std::string &fn_name, HIR::SelfParam &self_param,
127 std::vector<HIR::FunctionParam> &function_params,
128 const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
129 AST::AttrVec &outer_attrs, Location locus, HIR::BlockExpr *function_body,
130 const Resolver::CanonicalPath *canonical_path, TyTy::FnType *fntype,
131 bool function_has_return);
132
133 static tree
134 compile_constant_item (Context *ctx, TyTy::BaseType *resolved_type,
135 const Resolver::CanonicalPath *canonical_path,
136 HIR::Expr *const_value_expr, Location locus);
137
138 static tree named_constant_expression (tree type_tree,
139 const std::string &name,
140 tree const_val, Location location);
141 };
142
143 } // namespace Compile
144 } // namespace Rust
145
146 #endif // RUST_COMPILE_BASE