]>
Commit | Line | Data |
---|---|---|
43d9ad1d DS |
1 | /* Target support for C++ classes on Windows. |
2 | Contributed by Danny Smith (dannysmith@users.sourceforge.net) | |
5624e564 | 3 | Copyright (C) 2005-2015 Free Software Foundation, Inc. |
43d9ad1d DS |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
2f83c7d6 | 9 | Software Foundation; either version 3, or (at your option) any later |
43d9ad1d DS |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
2f83c7d6 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
40dcd88b | 20 | |
43d9ad1d DS |
21 | #include "config.h" |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
40e23961 | 25 | #include "alias.h" |
43d9ad1d | 26 | #include "tree.h" |
d8a2d370 DN |
27 | #include "stringpool.h" |
28 | #include "attribs.h" | |
09a6b8a4 | 29 | #include "cp/cp-tree.h" /* This is why we're a separate module. */ |
43d9ad1d DS |
30 | #include "flags.h" |
31 | #include "tm_p.h" | |
718f9c0f | 32 | #include "diagnostic-core.h" |
43d9ad1d DS |
33 | |
34 | bool | |
35 | i386_pe_type_dllimport_p (tree decl) | |
36 | { | |
6acd8838 DS |
37 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
38 | || TREE_CODE (decl) == FUNCTION_DECL); | |
39 | ||
40 | if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL) | |
41 | return false; | |
42 | ||
43 | /* We ignore the dllimport attribute for inline member functions. | |
44 | This differs from MSVC behavior which treats it like GNUC | |
45 | 'extern inline' extension. Also ignore for template | |
46 | instantiations with linkonce semantics and artificial methods. */ | |
47 | if (TREE_CODE (decl) == FUNCTION_DECL | |
48 | && (DECL_DECLARED_INLINE_P (decl) | |
49 | || DECL_TEMPLATE_INSTANTIATION (decl) | |
50 | || DECL_ARTIFICIAL (decl))) | |
51 | return false; | |
09a6b8a4 DS |
52 | |
53 | /* Overrides of the class dllimport decls by out-of-class definitions are | |
54 | handled by tree.c:merge_dllimport_decl_attributes. */ | |
6acd8838 | 55 | return true; |
43d9ad1d DS |
56 | } |
57 | ||
43d9ad1d DS |
58 | bool |
59 | i386_pe_type_dllexport_p (tree decl) | |
60 | { | |
09a6b8a4 DS |
61 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
62 | || TREE_CODE (decl) == FUNCTION_DECL); | |
63 | ||
64 | /* Avoid exporting compiler-generated default dtors and copy ctors. | |
65 | The only artificial methods that need to be exported are virtual | |
66 | and non-virtual thunks. */ | |
67 | if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE | |
68 | && DECL_ARTIFICIAL (decl) && !DECL_THUNK_P (decl)) | |
69 | return false; | |
34f56efc KT |
70 | if (TREE_CODE (decl) == FUNCTION_DECL |
71 | && DECL_DECLARED_INLINE_P (decl)) | |
72 | { | |
73 | if (DECL_REALLY_EXTERN (decl) | |
74 | || !flag_keep_inline_dllexport) | |
75 | return false; | |
76 | } | |
09a6b8a4 | 77 | return true; |
43d9ad1d DS |
78 | } |
79 | ||
80 | static inline void maybe_add_dllimport (tree decl) | |
81 | { | |
82 | if (i386_pe_type_dllimport_p (decl)) | |
09a6b8a4 DS |
83 | DECL_DLLIMPORT_P (decl) = 1; |
84 | } | |
85 | ||
86 | static inline void maybe_add_dllexport (tree decl) | |
87 | { | |
88 | if (i386_pe_type_dllexport_p (decl)) | |
89 | { | |
90 | tree decl_attrs = DECL_ATTRIBUTES (decl); | |
91 | if (lookup_attribute ("dllexport", decl_attrs) != NULL_TREE) | |
92 | /* Already done. */ | |
93 | return; | |
94 | DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("dllexport"), | |
95 | NULL_TREE, decl_attrs); | |
96 | } | |
43d9ad1d DS |
97 | } |
98 | ||
99 | void | |
100 | i386_pe_adjust_class_at_definition (tree t) | |
101 | { | |
102 | tree member; | |
103 | ||
104 | gcc_assert (CLASS_TYPE_P (t)); | |
09a6b8a4 DS |
105 | |
106 | ||
107 | if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (t)) != NULL_TREE) | |
108 | { | |
26cfb9ab KT |
109 | tree tmv = TYPE_MAIN_VARIANT (t); |
110 | ||
111 | /* Make sure that we set dllexport attribute to typeinfo's | |
112 | base declaration, as otherwise it would fail to be exported as | |
113 | it isn't a class-member. */ | |
114 | if (tmv != NULL_TREE | |
115 | && CLASSTYPE_TYPEINFO_VAR (tmv) != NULL_TREE) | |
116 | { | |
117 | tree na, ti_decl = CLASSTYPE_TYPEINFO_VAR (tmv); | |
118 | na = tree_cons (get_identifier ("dllexport"), NULL_TREE, | |
119 | NULL_TREE); | |
120 | decl_attributes (&ti_decl, na, 0); | |
121 | } | |
122 | ||
09a6b8a4 | 123 | /* Check static VAR_DECL's. */ |
910ad8de | 124 | for (member = TYPE_FIELDS (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
125 | if (TREE_CODE (member) == VAR_DECL) |
126 | maybe_add_dllexport (member); | |
127 | ||
128 | /* Check FUNCTION_DECL's. */ | |
910ad8de | 129 | for (member = TYPE_METHODS (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
130 | if (TREE_CODE (member) == FUNCTION_DECL) |
131 | { | |
132 | tree thunk; | |
133 | maybe_add_dllexport (member); | |
134 | ||
135 | /* Also add the attribute to its thunks. */ | |
136 | for (thunk = DECL_THUNKS (member); thunk; | |
137 | thunk = TREE_CHAIN (thunk)) | |
138 | maybe_add_dllexport (thunk); | |
139 | } | |
140 | /* Check vtables */ | |
910ad8de | 141 | for (member = CLASSTYPE_VTABLES (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
142 | if (TREE_CODE (member) == VAR_DECL) |
143 | maybe_add_dllexport (member); | |
144 | } | |
43d9ad1d | 145 | |
09a6b8a4 DS |
146 | else if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (t)) != NULL_TREE) |
147 | { | |
148 | /* We don't actually add the attribute to the decl, just set the flag | |
149 | that signals that the address of this symbol is not a compile-time | |
150 | constant. Any subsequent out-of-class declaration of members wil | |
151 | cause the DECL_DLLIMPORT_P flag to be unset. | |
152 | (See tree.c: merge_dllimport_decl_attributes). | |
153 | That is just right since out-of class declarations can only be a | |
154 | definition. */ | |
155 | ||
156 | /* Check static VAR_DECL's. */ | |
910ad8de | 157 | for (member = TYPE_FIELDS (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
158 | if (TREE_CODE (member) == VAR_DECL) |
159 | maybe_add_dllimport (member); | |
43d9ad1d | 160 | |
09a6b8a4 | 161 | /* Check FUNCTION_DECL's. */ |
910ad8de | 162 | for (member = TYPE_METHODS (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
163 | if (TREE_CODE (member) == FUNCTION_DECL) |
164 | { | |
165 | tree thunk; | |
166 | maybe_add_dllimport (member); | |
167 | ||
168 | /* Also add the attribute to its thunks. */ | |
169 | for (thunk = DECL_THUNKS (member); thunk; | |
910ad8de | 170 | thunk = DECL_CHAIN (thunk)) |
09a6b8a4 DS |
171 | maybe_add_dllimport (thunk); |
172 | } | |
43d9ad1d | 173 | |
09a6b8a4 | 174 | /* Check vtables */ |
910ad8de | 175 | for (member = CLASSTYPE_VTABLES (t); member; member = DECL_CHAIN (member)) |
09a6b8a4 DS |
176 | if (TREE_CODE (member) == VAR_DECL) |
177 | maybe_add_dllimport (member); | |
178 | ||
179 | /* We leave typeinfo tables alone. We can't mark TI objects as | |
180 | dllimport, since the address of a secondary VTT may be needed | |
181 | for static initialization of a primary VTT. VTT's of | |
182 | dllimport'd classes should always be link-once COMDAT. */ | |
183 | } | |
43d9ad1d | 184 | } |