]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/traits/declare.rs
f9a2965284309d290f81549c6f558218a698d017
[rust.git] / src / librustc_codegen_ssa / traits / declare.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use super::BackendTypes;
12 use rustc::hir::def_id::DefId;
13 use rustc::mir::mono::{Linkage, Visibility};
14 use rustc::ty;
15 use rustc_mir::monomorphize::Instance;
16
17 pub trait DeclareMethods<'tcx>: BackendTypes {
18     /// Declare a global value.
19     ///
20     /// If there’s a value with the same name already declared, the function will
21     /// return its Value instead.
22     fn declare_global(&self, name: &str, ty: Self::Type) -> Self::Value;
23
24     /// Declare a C ABI function.
25     ///
26     /// Only use this for foreign function ABIs and glue. For Rust functions use
27     /// `declare_fn` instead.
28     ///
29     /// If there’s a value with the same name already declared, the function will
30     /// update the declaration and return existing Value instead.
31     fn declare_cfn(&self, name: &str, fn_type: Self::Type) -> Self::Value;
32
33     /// Declare a Rust function.
34     ///
35     /// If there’s a value with the same name already declared, the function will
36     /// update the declaration and return existing Value instead.
37     fn declare_fn(&self, name: &str, sig: ty::PolyFnSig<'tcx>) -> Self::Value;
38
39     /// Declare a global with an intention to define it.
40     ///
41     /// Use this function when you intend to define a global. This function will
42     /// return None if the name already has a definition associated with it. In that
43     /// case an error should be reported to the user, because it usually happens due
44     /// to user’s fault (e.g. misuse of #[no_mangle] or #[export_name] attributes).
45     fn define_global(&self, name: &str, ty: Self::Type) -> Option<Self::Value>;
46
47     /// Declare a private global
48     ///
49     /// Use this function when you intend to define a global without a name.
50     fn define_private_global(&self, ty: Self::Type) -> Self::Value;
51
52     /// Declare a Rust function with an intention to define it.
53     ///
54     /// Use this function when you intend to define a function. This function will
55     /// return panic if the name already has a definition associated with it. This
56     /// can happen with #[no_mangle] or #[export_name], for example.
57     fn define_fn(&self, name: &str, fn_sig: ty::PolyFnSig<'tcx>) -> Self::Value;
58
59     /// Declare a Rust function with an intention to define it.
60     ///
61     /// Use this function when you intend to define a function. This function will
62     /// return panic if the name already has a definition associated with it. This
63     /// can happen with #[no_mangle] or #[export_name], for example.
64     fn define_internal_fn(&self, name: &str, fn_sig: ty::PolyFnSig<'tcx>) -> Self::Value;
65
66     /// Get declared value by name.
67     fn get_declared_value(&self, name: &str) -> Option<Self::Value>;
68
69     /// Get defined or externally defined (AvailableExternally linkage) value by
70     /// name.
71     fn get_defined_value(&self, name: &str) -> Option<Self::Value>;
72 }
73
74 pub trait PreDefineMethods<'tcx>: BackendTypes {
75     fn predefine_static(
76         &self,
77         def_id: DefId,
78         linkage: Linkage,
79         visibility: Visibility,
80         symbol_name: &str,
81     );
82     fn predefine_fn(
83         &self,
84         instance: Instance<'tcx>,
85         linkage: Linkage,
86         visibility: Visibility,
87         symbol_name: &str,
88     );
89 }