OSDN Git Service

* config/i386/i386.md (absneg): New code iterator.
[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
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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
11 version.
12
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.
17
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/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "output.h"
30 #include "tree.h"
31 #include "cp/cp-tree.h" /* this is why we're a separate module */
32 #include "flags.h"
33 #include "tm_p.h"
34 #include "toplev.h"
35 #include "hashtab.h"
36
37 bool
38 i386_pe_type_dllimport_p (tree decl)
39 {
40   gcc_assert (TREE_CODE (decl) == VAR_DECL 
41               || TREE_CODE (decl) == FUNCTION_DECL);
42
43   if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
44     return false;
45
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)))
54     return false;
55
56
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)))
61     return false;
62
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))
69     {
70       if (!DECL_VIRTUAL_P (decl))
71           error ("definition of static data member %q+D of "
72                  "dllimport'd class", decl);
73       return false;
74     }
75
76   return true;
77 }
78
79
80 bool
81 i386_pe_type_dllexport_p (tree decl)
82 {
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))
90      return false;
91    return true;
92 }
93
94 static inline void maybe_add_dllimport (tree decl) 
95 {
96   if (i386_pe_type_dllimport_p (decl))
97     DECL_DLLIMPORT_P (decl) = 1;   
98 }
99
100 void
101 i386_pe_adjust_class_at_definition (tree t)
102 {
103   tree member;
104
105   gcc_assert (CLASS_TYPE_P (t));
106
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)
111     return;
112
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).
122   */
123
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);
128     
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);
133  
134   /* Check vtables  */
135   for (member = CLASSTYPE_VTABLES (t); member;  member = TREE_CHAIN (member))
136     if (TREE_CODE (member) == VAR_DECL) 
137       maybe_add_dllimport (member);
138
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.  */ 
143 }