1 /* Target support for C++ classes on Windows.
2 Contributed by Danny Smith (dannysmith@users.sourceforge.net)
3 Copyright (C) 2005, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "hard-reg-set.h"
31 #include "cp/cp-tree.h" /* this is why we're a separate module */
38 i386_pe_type_dllimport_p (tree decl
)
40 gcc_assert (TREE_CODE (decl
) == VAR_DECL
41 || TREE_CODE (decl
) == FUNCTION_DECL
);
43 if (TARGET_NOP_FUN_DLLIMPORT
&& TREE_CODE (decl
) == FUNCTION_DECL
)
46 /* We ignore the dllimport attribute for inline member functions.
47 This differs from MSVC behavior which treats it like GNUC
48 'extern inline' extension. Also ignore for template
49 instantiations with linkonce semantics and artificial methods. */
50 if (TREE_CODE (decl
) == FUNCTION_DECL
51 && (DECL_DECLARED_INLINE_P (decl
)
52 || DECL_TEMPLATE_INSTANTIATION (decl
)
53 || DECL_ARTIFICIAL (decl
)))
57 /* Don't mark defined functions as dllimport. This code will only be
58 reached if we see a non-inline function defined out-of-class. */
59 else if (TREE_CODE (decl
) == FUNCTION_DECL
60 && (DECL_INITIAL (decl
)))
63 /* Don't allow definitions of static data members in dllimport class,
64 If vtable data is marked as DECL_EXTERNAL, import it; otherwise just
65 ignore the class attribute. */
66 else if (TREE_CODE (decl
) == VAR_DECL
67 && TREE_STATIC (decl
) && TREE_PUBLIC (decl
)
68 && !DECL_EXTERNAL (decl
))
70 if (!DECL_VIRTUAL_P (decl
))
71 error ("definition of static data member %q+D of "
72 "dllimport'd class", decl
);
81 i386_pe_type_dllexport_p (tree decl
)
83 gcc_assert (TREE_CODE (decl
) == VAR_DECL
84 || TREE_CODE (decl
) == FUNCTION_DECL
);
85 /* Avoid exporting compiler-generated default dtors and copy ctors.
86 The only artificial methods that need to be exported are virtual
87 and non-virtual thunks. */
88 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
89 && DECL_ARTIFICIAL (decl
) && !DECL_THUNK_P (decl
))
94 static inline void maybe_add_dllimport (tree decl
)
96 if (i386_pe_type_dllimport_p (decl
))
97 DECL_DLLIMPORT_P (decl
) = 1;
101 i386_pe_adjust_class_at_definition (tree t
)
105 gcc_assert (CLASS_TYPE_P (t
));
107 /* We only look at dllimport. The only thing that dllexport does is
108 add stuff to a '.drectiv' section at end-of-file, so no need to do
109 anything for dllexport'd classes until we generate RTL. */
110 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (t
)) == NULL_TREE
)
113 /* We don't actually add the attribute to the decl, just set the flag
114 that signals that the address of this symbol is not a compile-time
115 constant. Any subsequent out-of-class declaration of members wil
116 cause the DECL_DLLIMPORT_P flag to be unset.
117 (See tree.c: merge_dllimport_decl_attributes).
118 That is just right since out-of class declarations can only be a
119 definition. We recheck the class members at RTL generation to
120 emit warnings if this has happened. Definition of static data member
121 of dllimport'd class always causes an error (as per MS compiler).
124 /* Check static VAR_DECL's. */
125 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
126 if (TREE_CODE (member
) == VAR_DECL
)
127 maybe_add_dllimport (member
);
129 /* Check FUNCTION_DECL's. */
130 for (member
= TYPE_METHODS (t
); member
; member
= TREE_CHAIN (member
))
131 if (TREE_CODE (member
) == FUNCTION_DECL
)
132 maybe_add_dllimport (member
);
135 for (member
= CLASSTYPE_VTABLES (t
); member
; member
= TREE_CHAIN (member
))
136 if (TREE_CODE (member
) == VAR_DECL
)
137 maybe_add_dllimport (member
);
139 /* We leave typeinfo tables alone. We can't mark TI objects as
140 dllimport, since the address of a secondary VTT may be needed
141 for static initialization of a primary VTT. VTT's of
142 dllimport'd classes should always be link-once COMDAT. */