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