OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / winnt-cxx.c
1 /* Target support for C++ classes on Windows.
2    Contributed by Danny Smith (dannysmith@users.sourceforge.net)
3    Copyright (C) 2005, 2007, 2009  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 "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "output.h"
29 #include "tree.h"
30 #include "cp/cp-tree.h" /* This is why we're a separate module.  */
31 #include "flags.h"
32 #include "tm_p.h"
33 #include "toplev.h"
34 #include "hashtab.h"
35
36 bool
37 i386_pe_type_dllimport_p (tree decl)
38 {
39   gcc_assert (TREE_CODE (decl) == VAR_DECL 
40               || TREE_CODE (decl) == FUNCTION_DECL);
41
42   if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
43     return false;
44
45   /* We ignore the dllimport attribute for inline member functions.
46      This differs from MSVC behavior which treats it like GNUC
47      'extern inline' extension.  Also ignore for template
48      instantiations with linkonce semantics and artificial methods.  */
49   if (TREE_CODE (decl) ==  FUNCTION_DECL
50       && (DECL_DECLARED_INLINE_P (decl)
51           || DECL_TEMPLATE_INSTANTIATION (decl)
52           || DECL_ARTIFICIAL (decl)))
53     return false;
54   
55   /* Overrides of the class dllimport decls by out-of-class definitions are 
56      handled by tree.c:merge_dllimport_decl_attributes.   */
57   return true;
58 }
59
60 bool
61 i386_pe_type_dllexport_p (tree decl)
62 {
63   gcc_assert (TREE_CODE (decl) == VAR_DECL 
64               || TREE_CODE (decl) == FUNCTION_DECL);
65
66   /* Avoid exporting compiler-generated default dtors and copy ctors.
67      The only artificial methods that need to be exported are virtual
68      and non-virtual thunks.  */
69   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
70       && DECL_ARTIFICIAL (decl) && !DECL_THUNK_P (decl))
71     return false;
72   return true;
73 }
74
75 static inline void maybe_add_dllimport (tree decl) 
76 {
77   if (i386_pe_type_dllimport_p (decl))
78     DECL_DLLIMPORT_P (decl) = 1;
79 }
80
81 static inline void maybe_add_dllexport (tree decl) 
82 {
83   if (i386_pe_type_dllexport_p (decl))
84     {   
85       tree decl_attrs = DECL_ATTRIBUTES (decl);
86       if (lookup_attribute ("dllexport", decl_attrs) != NULL_TREE)
87         /* Already done.  */
88         return;
89       DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("dllexport"),
90                                           NULL_TREE, decl_attrs);
91     }
92 }
93
94 void
95 i386_pe_adjust_class_at_definition (tree t)
96 {
97   tree member;
98
99   gcc_assert (CLASS_TYPE_P (t));
100  
101  
102   if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (t)) != NULL_TREE)
103     {
104       /* Check static VAR_DECL's.  */
105       for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
106         if (TREE_CODE (member) == VAR_DECL)     
107           maybe_add_dllexport (member);
108     
109       /* Check FUNCTION_DECL's.  */
110       for (member = TYPE_METHODS (t); member;  member = TREE_CHAIN (member))
111         if (TREE_CODE (member) == FUNCTION_DECL)
112           {
113             tree thunk;
114             maybe_add_dllexport (member);
115           
116             /* Also add the attribute to its thunks.  */
117             for (thunk = DECL_THUNKS (member); thunk;
118                  thunk = TREE_CHAIN (thunk))
119               maybe_add_dllexport (thunk);
120         }
121       /* Check vtables  */
122       for (member = CLASSTYPE_VTABLES (t); member;  member = TREE_CHAIN (member))
123         if (TREE_CODE (member) == VAR_DECL) 
124           maybe_add_dllexport (member);
125     }
126
127   else if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (t)) != NULL_TREE)
128     {
129       /* We don't actually add the attribute to the decl, just set the flag
130          that signals that the address of this symbol is not a compile-time
131          constant.   Any subsequent out-of-class declaration of members wil
132          cause the DECL_DLLIMPORT_P flag to be unset.
133          (See  tree.c: merge_dllimport_decl_attributes).
134          That is just right since out-of class declarations can only be a
135          definition.   */
136
137       /* Check static VAR_DECL's.  */
138       for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
139         if (TREE_CODE (member) == VAR_DECL)     
140           maybe_add_dllimport (member);
141     
142       /* Check FUNCTION_DECL's.  */
143       for (member = TYPE_METHODS (t); member;  member = TREE_CHAIN (member))
144         if (TREE_CODE (member) == FUNCTION_DECL)
145           {
146             tree thunk;
147             maybe_add_dllimport (member);
148           
149             /* Also add the attribute to its thunks.  */
150             for (thunk = DECL_THUNKS (member); thunk;
151                  thunk = TREE_CHAIN (thunk))
152               maybe_add_dllimport (thunk);
153          }
154  
155       /* Check vtables  */
156       for (member = CLASSTYPE_VTABLES (t); member;  member = TREE_CHAIN (member))
157         if (TREE_CODE (member) == VAR_DECL) 
158           maybe_add_dllimport (member);
159
160       /* We leave typeinfo tables alone.  We can't mark TI objects as
161         dllimport, since the address of a secondary VTT may be needed
162         for static initialization of a primary VTT.  VTT's  of
163         dllimport'd classes should always be link-once COMDAT.  */ 
164     }
165 }