2007-05-30 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / config / i386 / winnt-cxx.c
bloba6a8510720bb5a7f517ce0f756d36e5a2e95b079
1 /* Target support for C++ classes on Windows.
2 Contributed by Danny Smith (dannysmith@users.sourceforge.net)
3 Copyright (C) 2005
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 2, or (at your option) any later
11 version.
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
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "output.h"
31 #include "tree.h"
32 #include "cp/cp-tree.h" /* this is why we're a separate module */
33 #include "flags.h"
34 #include "tm_p.h"
35 #include "toplev.h"
36 #include "hashtab.h"
38 bool
39 i386_pe_type_dllimport_p (tree decl)
41 gcc_assert (TREE_CODE (decl) == VAR_DECL
42 || TREE_CODE (decl) == FUNCTION_DECL);
44 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
45 return false;
47 /* We ignore the dllimport attribute for inline member functions.
48 This differs from MSVC behavior which treats it like GNUC
49 'extern inline' extension. Also ignore for template
50 instantiations with linkonce semantics and artificial methods. */
51 if (TREE_CODE (decl) == FUNCTION_DECL
52 && (DECL_DECLARED_INLINE_P (decl)
53 || DECL_TEMPLATE_INSTANTIATION (decl)
54 || DECL_ARTIFICIAL (decl)))
55 return false;
58 /* Don't mark defined functions as dllimport. This code will only be
59 reached if we see a non-inline function defined out-of-class. */
60 else if (TREE_CODE (decl) == FUNCTION_DECL
61 && (DECL_INITIAL (decl)))
62 return false;
64 /* Don't allow definitions of static data members in dllimport class,
65 If vtable data is marked as DECL_EXTERNAL, import it; otherwise just
66 ignore the class attribute. */
67 else if (TREE_CODE (decl) == VAR_DECL
68 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
69 && !DECL_EXTERNAL (decl))
71 if (!DECL_VIRTUAL_P (decl))
72 error ("definition of static data member %q+D of "
73 "dllimport'd class", decl);
74 return false;
77 return true;
81 bool
82 i386_pe_type_dllexport_p (tree decl)
84 gcc_assert (TREE_CODE (decl) == VAR_DECL
85 || TREE_CODE (decl) == FUNCTION_DECL);
86 /* Avoid exporting compiler-generated default dtors and copy ctors.
87 The only artificial methods that need to be exported are virtual
88 and non-virtual thunks. */
89 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
90 && DECL_ARTIFICIAL (decl) && !DECL_THUNK_P (decl))
91 return false;
92 return true;
95 static inline void maybe_add_dllimport (tree decl)
97 if (i386_pe_type_dllimport_p (decl))
98 DECL_DLLIMPORT_P (decl) = 1;
101 void
102 i386_pe_adjust_class_at_definition (tree t)
104 tree member;
106 gcc_assert (CLASS_TYPE_P (t));
108 /* We only look at dllimport. The only thing that dllexport does is
109 add stuff to a '.drectiv' section at end-of-file, so no need to do
110 anything for dllexport'd classes until we generate RTL. */
111 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (t)) == NULL_TREE)
112 return;
114 /* We don't actually add the attribute to the decl, just set the flag
115 that signals that the address of this symbol is not a compile-time
116 constant. Any subsequent out-of-class declaration of members wil
117 cause the DECL_DLLIMPORT_P flag to be unset.
118 (See tree.c: merge_dllimport_decl_attributes).
119 That is just right since out-of class declarations can only be a
120 definition. We recheck the class members at RTL generation to
121 emit warnings if this has happened. Definition of static data member
122 of dllimport'd class always causes an error (as per MS compiler).
125 /* Check static VAR_DECL's. */
126 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
127 if (TREE_CODE (member) == VAR_DECL)
128 maybe_add_dllimport (member);
130 /* Check FUNCTION_DECL's. */
131 for (member = TYPE_METHODS (t); member; member = TREE_CHAIN (member))
132 if (TREE_CODE (member) == FUNCTION_DECL)
133 maybe_add_dllimport (member);
135 /* Check vtables */
136 for (member = CLASSTYPE_VTABLES (t); member; member = TREE_CHAIN (member))
137 if (TREE_CODE (member) == VAR_DECL)
138 maybe_add_dllimport (member);
140 /* We leave typeinfo tables alone. We can't mark TI objects as
141 dllimport, since the address of a secondary VTT may be needed
142 for static initialization of a primary VTT. VTT's of
143 dllimport'd classes should always be link-once COMDAT. */