@@ -24,26 +24,30 @@ use rustc::hir;
24
24
use rustc:: hir:: def:: Def ;
25
25
use rustc:: hir:: def_id:: { DefId , LOCAL_CRATE } ;
26
26
use rustc:: mir:: mono:: { Linkage , Visibility } ;
27
- use rustc:: ty:: TypeFoldable ;
28
- use rustc:: ty:: layout:: LayoutOf ;
27
+ use rustc:: ty:: { TypeFoldable , Ty } ;
28
+ use rustc:: ty:: layout:: { LayoutOf , HasTyCtxt , TyLayout } ;
29
29
use std:: fmt;
30
30
use value:: Value ;
31
+ use builder:: Builder ;
31
32
use interfaces:: * ;
32
33
33
34
pub use rustc:: mir:: mono:: MonoItem ;
34
35
35
36
pub use rustc_mir:: monomorphize:: item:: MonoItemExt as BaseMonoItemExt ;
36
37
37
- pub trait MonoItemExt < ' a , ' tcx > : fmt:: Debug + BaseMonoItemExt < ' a , ' tcx > {
38
- fn define ( & self , cx : & CodegenCx < ' a , ' tcx , & ' a Value > ) {
38
+ pub trait MonoItemExt < ' a , ' ll : ' a , ' tcx : ' ll , Bx : BuilderMethods < ' a , ' ll , ' tcx > > :
39
+ fmt:: Debug + BaseMonoItemExt < ' ll , ' tcx > where
40
+ & ' a Bx :: CodegenCx : LayoutOf < Ty = Ty < ' tcx > , TyLayout =TyLayout < ' tcx > > + HasTyCtxt < ' tcx >
41
+ {
42
+ fn define ( & self , cx : & ' a Bx :: CodegenCx ) {
39
43
debug ! ( "BEGIN IMPLEMENTING '{} ({})' in cgu {}" ,
40
- self . to_string( cx. tcx) ,
44
+ self . to_string( * cx. tcx( ) ) ,
41
45
self . to_raw_string( ) ,
42
- cx. codegen_unit. name( ) ) ;
46
+ cx. codegen_unit( ) . name( ) ) ;
43
47
44
48
match * self . as_mono_item ( ) {
45
49
MonoItem :: Static ( def_id) => {
46
- let tcx = cx. tcx ;
50
+ let tcx = * cx. tcx ( ) ;
47
51
let is_mutable = match tcx. describe_def ( def_id) {
48
52
Some ( Def :: Static ( _, is_mutable) ) => is_mutable,
49
53
Some ( other) => {
@@ -56,51 +60,51 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
56
60
cx. codegen_static ( def_id, is_mutable) ;
57
61
}
58
62
MonoItem :: GlobalAsm ( node_id) => {
59
- let item = cx. tcx . hir . expect_item ( node_id) ;
63
+ let item = cx. tcx ( ) . hir . expect_item ( node_id) ;
60
64
if let hir:: ItemKind :: GlobalAsm ( ref ga) = item. node {
61
65
cx. codegen_global_asm ( ga) ;
62
66
} else {
63
67
span_bug ! ( item. span, "Mismatch between hir::Item type and MonoItem type" )
64
68
}
65
69
}
66
70
MonoItem :: Fn ( instance) => {
67
- base:: codegen_instance ( & cx, instance) ;
71
+ base:: codegen_instance :: < ' a , ' ll , ' tcx , Bx > ( & cx, instance) ;
68
72
}
69
73
}
70
74
71
75
debug ! ( "END IMPLEMENTING '{} ({})' in cgu {}" ,
72
- self . to_string( cx. tcx) ,
76
+ self . to_string( * cx. tcx( ) ) ,
73
77
self . to_raw_string( ) ,
74
- cx. codegen_unit. name( ) ) ;
78
+ cx. codegen_unit( ) . name( ) ) ;
75
79
}
76
80
77
81
fn predefine ( & self ,
78
- cx : & CodegenCx < ' a , ' tcx , & ' a Value > ,
82
+ cx : & ' a Bx :: CodegenCx ,
79
83
linkage : Linkage ,
80
84
visibility : Visibility ) {
81
85
debug ! ( "BEGIN PREDEFINING '{} ({})' in cgu {}" ,
82
- self . to_string( cx. tcx) ,
86
+ self . to_string( * cx. tcx( ) ) ,
83
87
self . to_raw_string( ) ,
84
- cx. codegen_unit. name( ) ) ;
88
+ cx. codegen_unit( ) . name( ) ) ;
85
89
86
- let symbol_name = self . symbol_name ( cx. tcx ) . as_str ( ) ;
90
+ let symbol_name = self . symbol_name ( * cx. tcx ( ) ) . as_str ( ) ;
87
91
88
92
debug ! ( "symbol {}" , & symbol_name) ;
89
93
90
94
match * self . as_mono_item ( ) {
91
95
MonoItem :: Static ( def_id) => {
92
- predefine_static ( cx , def_id, linkage, visibility, & symbol_name) ;
96
+ cx . predefine_static ( def_id, linkage, visibility, & symbol_name) ;
93
97
}
94
98
MonoItem :: Fn ( instance) => {
95
- predefine_fn ( cx , instance, linkage, visibility, & symbol_name) ;
99
+ cx . predefine_fn ( instance, linkage, visibility, & symbol_name) ;
96
100
}
97
101
MonoItem :: GlobalAsm ( ..) => { }
98
102
}
99
103
100
104
debug ! ( "END PREDEFINING '{} ({})' in cgu {}" ,
101
- self . to_string( cx. tcx) ,
105
+ self . to_string( * cx. tcx( ) ) ,
102
106
self . to_raw_string( ) ,
103
- cx. codegen_unit. name( ) ) ;
107
+ cx. codegen_unit( ) . name( ) ) ;
104
108
}
105
109
106
110
fn to_raw_string ( & self ) -> String {
@@ -120,68 +124,71 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
120
124
}
121
125
}
122
126
123
- impl < ' a , ' tcx > MonoItemExt < ' a , ' tcx > for MonoItem < ' tcx > { }
127
+ impl < ' a , ' ll : ' a , ' tcx : ' ll > MonoItemExt < ' a , ' ll , ' tcx , Builder < ' a , ' ll , ' tcx , & ' ll Value > >
128
+ for MonoItem < ' tcx > { }
124
129
125
- fn predefine_static < ' a , ' tcx > ( cx : & CodegenCx < ' a , ' tcx , & ' a Value > ,
126
- def_id : DefId ,
127
- linkage : Linkage ,
128
- visibility : Visibility ,
129
- symbol_name : & str ) {
130
- let instance = Instance :: mono ( cx. tcx , def_id) ;
131
- let ty = instance. ty ( cx. tcx ) ;
132
- let llty = cx. layout_of ( ty) . llvm_type ( cx) ;
133
-
134
- let g = cx. define_global ( symbol_name, llty) . unwrap_or_else ( || {
135
- cx. sess ( ) . span_fatal ( cx. tcx . def_span ( def_id) ,
136
- & format ! ( "symbol `{}` is already defined" , symbol_name) )
137
- } ) ;
138
-
139
- unsafe {
140
- llvm:: LLVMRustSetLinkage ( g, base:: linkage_to_llvm ( linkage) ) ;
141
- llvm:: LLVMRustSetVisibility ( g, base:: visibility_to_llvm ( visibility) ) ;
142
- }
130
+ impl < ' ll , ' tcx : ' ll > PreDefineMethods < ' ll , ' tcx > for CodegenCx < ' ll , ' tcx , & ' ll Value > {
131
+ fn predefine_static ( & self ,
132
+ def_id : DefId ,
133
+ linkage : Linkage ,
134
+ visibility : Visibility ,
135
+ symbol_name : & str ) {
136
+ let instance = Instance :: mono ( self . tcx , def_id) ;
137
+ let ty = instance. ty ( self . tcx ) ;
138
+ let llty = self . layout_of ( ty) . llvm_type ( self ) ;
143
139
144
- cx. instances . borrow_mut ( ) . insert ( instance, g) ;
145
- }
140
+ let g = self . define_global ( symbol_name, llty) . unwrap_or_else ( || {
141
+ self . sess ( ) . span_fatal ( self . tcx . def_span ( def_id) ,
142
+ & format ! ( "symbol `{}` is already defined" , symbol_name) )
143
+ } ) ;
146
144
147
- fn predefine_fn < ' a , ' tcx > ( cx : & CodegenCx < ' a , ' tcx , & ' a Value > ,
148
- instance : Instance < ' tcx > ,
149
- linkage : Linkage ,
150
- visibility : Visibility ,
151
- symbol_name : & str ) {
152
- assert ! ( !instance. substs. needs_infer( ) &&
153
- !instance. substs. has_param_types( ) ) ;
154
-
155
- let mono_ty = instance. ty ( cx. tcx ) ;
156
- let attrs = cx. tcx . codegen_fn_attrs ( instance. def_id ( ) ) ;
157
- let lldecl = cx. declare_fn ( symbol_name, mono_ty) ;
158
- unsafe { llvm:: LLVMRustSetLinkage ( lldecl, base:: linkage_to_llvm ( linkage) ) } ;
159
- base:: set_link_section ( lldecl, & attrs) ;
160
- if linkage == Linkage :: LinkOnceODR ||
161
- linkage == Linkage :: WeakODR {
162
- llvm:: SetUniqueComdat ( cx. llmod , lldecl) ;
145
+ unsafe {
146
+ llvm:: LLVMRustSetLinkage ( g, base:: linkage_to_llvm ( linkage) ) ;
147
+ llvm:: LLVMRustSetVisibility ( g, base:: visibility_to_llvm ( visibility) ) ;
148
+ }
149
+
150
+ self . instances . borrow_mut ( ) . insert ( instance, g) ;
163
151
}
164
152
165
- // If we're compiling the compiler-builtins crate, e.g. the equivalent of
166
- // compiler-rt, then we want to implicitly compile everything with hidden
167
- // visibility as we're going to link this object all over the place but
168
- // don't want the symbols to get exported.
169
- if linkage != Linkage :: Internal && linkage != Linkage :: Private &&
170
- cx. tcx . is_compiler_builtins ( LOCAL_CRATE ) {
171
- unsafe {
172
- llvm:: LLVMRustSetVisibility ( lldecl, llvm:: Visibility :: Hidden ) ;
153
+ fn predefine_fn ( & self ,
154
+ instance : Instance < ' tcx > ,
155
+ linkage : Linkage ,
156
+ visibility : Visibility ,
157
+ symbol_name : & str ) {
158
+ assert ! ( !instance. substs. needs_infer( ) &&
159
+ !instance. substs. has_param_types( ) ) ;
160
+
161
+ let mono_ty = instance. ty ( self . tcx ) ;
162
+ let attrs = self . tcx . codegen_fn_attrs ( instance. def_id ( ) ) ;
163
+ let lldecl = self . declare_fn ( symbol_name, mono_ty) ;
164
+ unsafe { llvm:: LLVMRustSetLinkage ( lldecl, base:: linkage_to_llvm ( linkage) ) } ;
165
+ base:: set_link_section ( lldecl, & attrs) ;
166
+ if linkage == Linkage :: LinkOnceODR ||
167
+ linkage == Linkage :: WeakODR {
168
+ llvm:: SetUniqueComdat ( self . llmod , lldecl) ;
173
169
}
174
- } else {
175
- unsafe {
176
- llvm:: LLVMRustSetVisibility ( lldecl, base:: visibility_to_llvm ( visibility) ) ;
170
+
171
+ // If we're compiling the compiler-builtins crate, e.g. the equivalent of
172
+ // compiler-rt, then we want to implicitly compile everything with hidden
173
+ // visibility as we're going to link this object all over the place but
174
+ // don't want the symbols to get exported.
175
+ if linkage != Linkage :: Internal && linkage != Linkage :: Private &&
176
+ self . tcx . is_compiler_builtins ( LOCAL_CRATE ) {
177
+ unsafe {
178
+ llvm:: LLVMRustSetVisibility ( lldecl, llvm:: Visibility :: Hidden ) ;
179
+ }
180
+ } else {
181
+ unsafe {
182
+ llvm:: LLVMRustSetVisibility ( lldecl, base:: visibility_to_llvm ( visibility) ) ;
183
+ }
177
184
}
178
- }
179
185
180
- debug ! ( "predefine_fn: mono_ty = {:?} instance = {:?}" , mono_ty, instance) ;
181
- if instance. def . is_inline ( cx . tcx ) {
182
- attributes:: inline ( cx , lldecl, attributes:: InlineAttr :: Hint ) ;
183
- }
184
- attributes:: from_fn_attrs ( cx , lldecl, Some ( instance. def . def_id ( ) ) ) ;
186
+ debug ! ( "predefine_fn: mono_ty = {:?} instance = {:?}" , mono_ty, instance) ;
187
+ if instance. def . is_inline ( self . tcx ) {
188
+ attributes:: inline ( self , lldecl, attributes:: InlineAttr :: Hint ) ;
189
+ }
190
+ attributes:: from_fn_attrs ( self , lldecl, Some ( instance. def . def_id ( ) ) ) ;
185
191
186
- cx. instances . borrow_mut ( ) . insert ( instance, lldecl) ;
192
+ self . instances . borrow_mut ( ) . insert ( instance, lldecl) ;
193
+ }
187
194
}
0 commit comments