Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | 6301884 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | c4173fa | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame^] | 24 | #include "clang/AST/ASTMutationListener.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 26 | #include "clang/Basic/IdentifierTable.h" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Specifiers.h" |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | ce20e8f | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 33 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 36 | static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) { |
| 37 | switch (A->getVisibility()) { |
| 38 | case VisibilityAttr::Default: |
| 39 | return DefaultVisibility; |
| 40 | case VisibilityAttr::Hidden: |
| 41 | return HiddenVisibility; |
| 42 | case VisibilityAttr::Protected: |
| 43 | return ProtectedVisibility; |
| 44 | } |
| 45 | return DefaultVisibility; |
| 46 | } |
| 47 | |
| 48 | typedef std::pair<Linkage,Visibility> LVPair; |
| 49 | static LVPair merge(LVPair L, LVPair R) { |
| 50 | return LVPair(minLinkage(L.first, R.first), |
| 51 | minVisibility(L.second, R.second)); |
| 52 | } |
| 53 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 54 | /// \brief Get the most restrictive linkage for the types in the given |
| 55 | /// template parameter list. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 56 | static LVPair |
| 57 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
| 58 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 59 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 60 | PEnd = Params->end(); |
| 61 | P != PEnd; ++P) { |
| 62 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 63 | if (!NTTP->getType()->isDependentType()) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 64 | LV = merge(LV, NTTP->getType()->getLinkageAndVisibility()); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (TemplateTemplateParmDecl *TTP |
| 69 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 70 | LV = |
| 71 | merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 75 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /// \brief Get the most restrictive linkage for the types and |
| 79 | /// declarations in the given template argument list. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 80 | static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args, |
| 81 | unsigned NumArgs) { |
| 82 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 83 | |
| 84 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 85 | switch (Args[I].getKind()) { |
| 86 | case TemplateArgument::Null: |
| 87 | case TemplateArgument::Integral: |
| 88 | case TemplateArgument::Expression: |
| 89 | break; |
| 90 | |
| 91 | case TemplateArgument::Type: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 92 | LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility()); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | case TemplateArgument::Declaration: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 96 | // The decl can validly be null as the representation of nullptr |
| 97 | // arguments, valid only in C++0x. |
| 98 | if (Decl *D = Args[I].getAsDecl()) { |
| 99 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 100 | LV = merge(LV, ND->getLinkageAndVisibility()); |
| 101 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 102 | LV = merge(LV, VD->getType()->getLinkageAndVisibility()); |
| 103 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | case TemplateArgument::Template: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 107 | if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl()) |
| 108 | LV = merge(LV, Template->getLinkageAndVisibility()); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 109 | break; |
| 110 | |
| 111 | case TemplateArgument::Pack: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 112 | LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(), |
| 113 | Args[I].pack_size())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 118 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 119 | } |
| 120 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 121 | static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) { |
| 122 | return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(), |
| 123 | TArgs.flat_size()); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 126 | static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 127 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 128 | "Not a name having namespace scope"); |
| 129 | ASTContext &Context = D->getASTContext(); |
| 130 | |
| 131 | // C++ [basic.link]p3: |
| 132 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 133 | // is the name of |
| 134 | // - an object, reference, function or function template that is |
| 135 | // explicitly declared static; or, |
| 136 | // (This bullet corresponds to C99 6.2.2p3.) |
| 137 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 138 | // Explicitly declared static. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 139 | if (Var->getStorageClass() == SC_Static) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 140 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 141 | |
| 142 | // - an object or reference that is explicitly declared const |
| 143 | // and neither explicitly declared extern nor previously |
| 144 | // declared to have external linkage; or |
| 145 | // (there is no equivalent in C99) |
| 146 | if (Context.getLangOptions().CPlusPlus && |
Eli Friedman | f873c2f | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 147 | Var->getType().isConstant(Context) && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 148 | Var->getStorageClass() != SC_Extern && |
| 149 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 150 | bool FoundExtern = false; |
| 151 | for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); |
| 152 | PrevVar && !FoundExtern; |
| 153 | PrevVar = PrevVar->getPreviousDeclaration()) |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 154 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 155 | FoundExtern = true; |
| 156 | |
| 157 | if (!FoundExtern) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 158 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 159 | } |
| 160 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 161 | // C++ [temp]p4: |
| 162 | // A non-member function template can have internal linkage; any |
| 163 | // other template name shall have external linkage. |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 164 | const FunctionDecl *Function = 0; |
| 165 | if (const FunctionTemplateDecl *FunTmpl |
| 166 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 167 | Function = FunTmpl->getTemplatedDecl(); |
| 168 | else |
| 169 | Function = cast<FunctionDecl>(D); |
| 170 | |
| 171 | // Explicitly declared static. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 172 | if (Function->getStorageClass() == SC_Static) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 173 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 174 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 175 | // - a data member of an anonymous union. |
| 176 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 177 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 180 | if (D->isInAnonymousNamespace()) |
| 181 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
| 182 | |
| 183 | // Set up the defaults. |
| 184 | |
| 185 | // C99 6.2.2p5: |
| 186 | // If the declaration of an identifier for an object has file |
| 187 | // scope and no storage-class specifier, its linkage is |
| 188 | // external. |
| 189 | LVPair LV(ExternalLinkage, DefaultVisibility); |
| 190 | |
| 191 | // We ignore -fvisibility on non-definitions and explicit |
| 192 | // instantiation declarations. |
| 193 | bool ConsiderDashFVisibility = true; |
| 194 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 195 | // C++ [basic.link]p4: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 197 | // A name having namespace scope has external linkage if it is the |
| 198 | // name of |
| 199 | // |
| 200 | // - an object or reference, unless it has internal linkage; or |
| 201 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 202 | // Modify the variable's LV by the LV of its type unless this is |
| 203 | // C or extern "C". This follows from [basic.link]p9: |
| 204 | // A type without linkage shall not be used as the type of a |
| 205 | // variable or function with external linkage unless |
| 206 | // - the entity has C language linkage, or |
| 207 | // - the entity is declared within an unnamed namespace, or |
| 208 | // - the entity is not used or is defined in the same |
| 209 | // translation unit. |
| 210 | // and [basic.link]p10: |
| 211 | // ...the types specified by all declarations referring to a |
| 212 | // given variable or function shall be identical... |
| 213 | // C does not have an equivalent rule. |
| 214 | // |
| 215 | // Note that we don't want to make the variable non-external |
| 216 | // because of this, but unique-external linkage suits us. |
| 217 | if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) { |
| 218 | LVPair TypeLV = Var->getType()->getLinkageAndVisibility(); |
| 219 | if (TypeLV.first != ExternalLinkage) |
| 220 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
| 221 | LV.second = minVisibility(LV.second, TypeLV.second); |
| 222 | } |
| 223 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 224 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 225 | (Var->getStorageClass() == SC_Extern || |
| 226 | Var->getStorageClass() == SC_PrivateExtern)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 227 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 228 | LV.second = HiddenVisibility; |
| 229 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 230 | // C99 6.2.2p4: |
| 231 | // For an identifier declared with the storage-class specifier |
| 232 | // extern in a scope in which a prior declaration of that |
| 233 | // identifier is visible, if the prior declaration specifies |
| 234 | // internal or external linkage, the linkage of the identifier |
| 235 | // at the later declaration is the same as the linkage |
| 236 | // specified at the prior declaration. If no prior declaration |
| 237 | // is visible, or if the prior declaration specifies no |
| 238 | // linkage, then the identifier has external linkage. |
| 239 | if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 240 | LVPair PrevLV = PrevVar->getLinkageAndVisibility(); |
| 241 | if (PrevLV.first) LV.first = PrevLV.first; |
| 242 | LV.second = minVisibility(LV.second, PrevLV.second); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 246 | // - a function, unless it has internal linkage; or |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 247 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 248 | // Modify the function's LV by the LV of its type unless this is |
| 249 | // C or extern "C". See the comment above about variables. |
| 250 | if (Context.getLangOptions().CPlusPlus && !Function->isExternC()) { |
| 251 | LVPair TypeLV = Function->getType()->getLinkageAndVisibility(); |
| 252 | if (TypeLV.first != ExternalLinkage) |
| 253 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
| 254 | LV.second = minVisibility(LV.second, TypeLV.second); |
| 255 | } |
| 256 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 257 | // C99 6.2.2p5: |
| 258 | // If the declaration of an identifier for a function has no |
| 259 | // storage-class specifier, its linkage is determined exactly |
| 260 | // as if it were declared with the storage-class specifier |
| 261 | // extern. |
| 262 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 263 | (Function->getStorageClass() == SC_Extern || |
| 264 | Function->getStorageClass() == SC_PrivateExtern || |
| 265 | Function->getStorageClass() == SC_None)) { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 266 | // C99 6.2.2p4: |
| 267 | // For an identifier declared with the storage-class specifier |
| 268 | // extern in a scope in which a prior declaration of that |
| 269 | // identifier is visible, if the prior declaration specifies |
| 270 | // internal or external linkage, the linkage of the identifier |
| 271 | // at the later declaration is the same as the linkage |
| 272 | // specified at the prior declaration. If no prior declaration |
| 273 | // is visible, or if the prior declaration specifies no |
| 274 | // linkage, then the identifier has external linkage. |
| 275 | if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 276 | LVPair PrevLV = PrevFunc->getLinkageAndVisibility(); |
| 277 | if (PrevLV.first) LV.first = PrevLV.first; |
| 278 | LV.second = minVisibility(LV.second, PrevLV.second); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 282 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 283 | = Function->getTemplateSpecializationInfo()) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 284 | LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility()); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 285 | const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 286 | LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs)); |
| 287 | |
| 288 | if (SpecInfo->getTemplateSpecializationKind() |
| 289 | == TSK_ExplicitInstantiationDeclaration) |
| 290 | ConsiderDashFVisibility = false; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 291 | } |
| 292 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 293 | if (ConsiderDashFVisibility) |
| 294 | ConsiderDashFVisibility = Function->hasBody(); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 295 | |
| 296 | // - a named class (Clause 9), or an unnamed class defined in a |
| 297 | // typedef declaration in which the class has the typedef name |
| 298 | // for linkage purposes (7.1.3); or |
| 299 | // - a named enumeration (7.2), or an unnamed enumeration |
| 300 | // defined in a typedef declaration in which the enumeration |
| 301 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 302 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 303 | // Unnamed tags have no linkage. |
| 304 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) |
| 305 | return LVPair(NoLinkage, DefaultVisibility); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 306 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 307 | // If this is a class template specialization, consider the |
| 308 | // linkage of the template and template arguments. |
| 309 | if (const ClassTemplateSpecializationDecl *Spec |
| 310 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
| 311 | // From the template. Note below the restrictions on how we |
| 312 | // compute template visibility. |
| 313 | LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility()); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 314 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 315 | // The arguments at which the template was instantiated. |
| 316 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 317 | LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs)); |
| 318 | |
| 319 | if (Spec->getTemplateSpecializationKind() |
| 320 | == TSK_ExplicitInstantiationDeclaration) |
| 321 | ConsiderDashFVisibility = false; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 322 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 323 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 324 | if (ConsiderDashFVisibility) |
| 325 | ConsiderDashFVisibility = Tag->isDefinition(); |
| 326 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 327 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 328 | } else if (isa<EnumConstantDecl>(D)) { |
| 329 | LVPair EnumLV = |
| 330 | cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility(); |
| 331 | if (!isExternalLinkage(EnumLV.first)) |
| 332 | return LVPair(NoLinkage, DefaultVisibility); |
| 333 | LV = merge(LV, EnumLV); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 334 | |
| 335 | // - a template, unless it is a function template that has |
| 336 | // internal linkage (Clause 14); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 337 | } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 338 | LV = merge(LV, getLVForTemplateParameterList( |
| 339 | Template->getTemplateParameters())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 340 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 341 | // We do not want to consider attributes or global settings when |
| 342 | // computing template visibility. |
| 343 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 344 | |
| 345 | // - a namespace (7.3), unless it is declared within an unnamed |
| 346 | // namespace. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 347 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 348 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 349 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 350 | // By extension, we assign external linkage to Objective-C |
| 351 | // interfaces. |
| 352 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 353 | // fallout |
| 354 | |
| 355 | // Everything not covered here has no linkage. |
| 356 | } else { |
| 357 | return LVPair(NoLinkage, DefaultVisibility); |
| 358 | } |
| 359 | |
| 360 | // If we ended up with non-external linkage, visibility should |
| 361 | // always be default. |
| 362 | if (LV.first != ExternalLinkage) |
| 363 | return LVPair(LV.first, DefaultVisibility); |
| 364 | |
| 365 | // If we didn't end up with hidden visibility, consider attributes |
| 366 | // and -fvisibility. |
| 367 | if (LV.second != HiddenVisibility) { |
| 368 | Visibility StandardV; |
| 369 | |
| 370 | // If we have an explicit visibility attribute, merge that in. |
| 371 | const VisibilityAttr *VA = D->getAttr<VisibilityAttr>(); |
| 372 | if (VA) |
| 373 | StandardV = GetVisibilityFromAttr(VA); |
| 374 | else if (ConsiderDashFVisibility) |
| 375 | StandardV = Context.getLangOptions().getVisibilityMode(); |
| 376 | else |
| 377 | StandardV = DefaultVisibility; // no-op |
| 378 | |
| 379 | LV.second = minVisibility(LV.second, StandardV); |
| 380 | } |
| 381 | |
| 382 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 383 | } |
| 384 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 385 | static LVPair getLVForClassMember(const NamedDecl *D) { |
| 386 | // Only certain class members have linkage. Note that fields don't |
| 387 | // really have linkage, but it's convenient to say they do for the |
| 388 | // purposes of calculating linkage of pointer-to-data-member |
| 389 | // template arguments. |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 390 | if (!(isa<CXXMethodDecl>(D) || |
| 391 | isa<VarDecl>(D) || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 392 | isa<FieldDecl>(D) || |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 393 | (isa<TagDecl>(D) && |
| 394 | (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl())))) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 395 | return LVPair(NoLinkage, DefaultVisibility); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 396 | |
| 397 | // Class members only have linkage if their class has external linkage. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 398 | LVPair ClassLV = |
| 399 | cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility(); |
| 400 | if (!isExternalLinkage(ClassLV.first)) |
| 401 | return LVPair(NoLinkage, DefaultVisibility); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 402 | |
| 403 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 404 | if (ClassLV.first == UniqueExternalLinkage) |
| 405 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 406 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 407 | // Start with the class's linkage and visibility. |
| 408 | LVPair LV = ClassLV; |
| 409 | |
| 410 | // If we have an explicit visibility attribute, merge that in. |
| 411 | const VisibilityAttr *VA = D->getAttr<VisibilityAttr>(); |
| 412 | if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA)); |
| 413 | |
| 414 | // If it's a value declaration, apply the LV from its type. |
| 415 | // See the comment about namespace-scope variable decls above. |
| 416 | if (isa<ValueDecl>(D)) { |
| 417 | LVPair TypeLV = cast<ValueDecl>(D)->getType()->getLinkageAndVisibility(); |
| 418 | if (TypeLV.first != ExternalLinkage) |
| 419 | LV.first = minLinkage(LV.first, UniqueExternalLinkage); |
| 420 | LV.second = minVisibility(LV.second, TypeLV.second); |
| 421 | } |
| 422 | |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 423 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 424 | // If this is a method template specialization, use the linkage for |
| 425 | // the template parameters and arguments. |
| 426 | if (FunctionTemplateSpecializationInfo *Spec |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 427 | = MD->getTemplateSpecializationInfo()) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 428 | LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments)); |
| 429 | LV = merge(LV, getLVForTemplateParameterList( |
| 430 | Spec->getTemplate()->getTemplateParameters())); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 431 | } |
| 432 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 433 | // If -fvisibility-inlines-hidden was provided, then inline C++ |
| 434 | // member functions get "hidden" visibility if they don't have an |
| 435 | // explicit visibility attribute. |
| 436 | if (!VA && MD->isInlined() && LV.second > HiddenVisibility && |
| 437 | D->getASTContext().getLangOptions().InlineVisibilityHidden) |
| 438 | LV.second = HiddenVisibility; |
| 439 | |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 440 | // Similarly for member class template specializations. |
| 441 | } else if (const ClassTemplateSpecializationDecl *Spec |
| 442 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 443 | LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs())); |
| 444 | LV = merge(LV, getLVForTemplateParameterList( |
| 445 | Spec->getSpecializedTemplate()->getTemplateParameters())); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 446 | } |
| 447 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 448 | return LV; |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 449 | } |
| 450 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 451 | LVPair NamedDecl::getLinkageAndVisibility() const { |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 452 | |
| 453 | // Objective-C: treat all Objective-C declarations as having external |
| 454 | // linkage. |
| 455 | switch (getKind()) { |
| 456 | default: |
| 457 | break; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 458 | case Decl::TemplateTemplateParm: // count these as external |
| 459 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 460 | case Decl::ObjCAtDefsField: |
| 461 | case Decl::ObjCCategory: |
| 462 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 463 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 464 | case Decl::ObjCForwardProtocol: |
| 465 | case Decl::ObjCImplementation: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 466 | case Decl::ObjCMethod: |
| 467 | case Decl::ObjCProperty: |
| 468 | case Decl::ObjCPropertyImpl: |
| 469 | case Decl::ObjCProtocol: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 470 | return LVPair(ExternalLinkage, DefaultVisibility); |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 473 | // Handle linkage for namespace-scope names. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 474 | if (getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 475 | return getLVForNamespaceScopeDecl(this); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 476 | |
| 477 | // C++ [basic.link]p5: |
| 478 | // In addition, a member function, static data member, a named |
| 479 | // class or enumeration of class scope, or an unnamed class or |
| 480 | // enumeration defined in a class-scope typedef declaration such |
| 481 | // that the class or enumeration has the typedef name for linkage |
| 482 | // purposes (7.1.3), has external linkage if the name of the class |
| 483 | // has external linkage. |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 484 | if (getDeclContext()->isRecord()) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 485 | return getLVForClassMember(this); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 486 | |
| 487 | // C++ [basic.link]p6: |
| 488 | // The name of a function declared in block scope and the name of |
| 489 | // an object declared by a block scope extern declaration have |
| 490 | // linkage. If there is a visible declaration of an entity with |
| 491 | // linkage having the same name and type, ignoring entities |
| 492 | // declared outside the innermost enclosing namespace scope, the |
| 493 | // block scope declaration declares that same entity and receives |
| 494 | // the linkage of the previous declaration. If there is more than |
| 495 | // one such matching entity, the program is ill-formed. Otherwise, |
| 496 | // if no matching entity is found, the block scope entity receives |
| 497 | // external linkage. |
| 498 | if (getLexicalDeclContext()->isFunctionOrMethod()) { |
| 499 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 500 | if (Function->isInAnonymousNamespace()) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 501 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 502 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 503 | LVPair LV(ExternalLinkage, DefaultVisibility); |
| 504 | if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>()) |
| 505 | LV.second = GetVisibilityFromAttr(VA); |
| 506 | |
| 507 | if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) { |
| 508 | LVPair PrevLV = Prev->getLinkageAndVisibility(); |
| 509 | if (PrevLV.first) LV.first = PrevLV.first; |
| 510 | LV.second = minVisibility(LV.second, PrevLV.second); |
| 511 | } |
| 512 | |
| 513 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 517 | if (Var->getStorageClass() == SC_Extern || |
| 518 | Var->getStorageClass() == SC_PrivateExtern) { |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 519 | if (Var->isInAnonymousNamespace()) |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 520 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 521 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 522 | LVPair LV(ExternalLinkage, DefaultVisibility); |
| 523 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 524 | LV.second = HiddenVisibility; |
| 525 | else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>()) |
| 526 | LV.second = GetVisibilityFromAttr(VA); |
| 527 | |
| 528 | if (const VarDecl *Prev = Var->getPreviousDeclaration()) { |
| 529 | LVPair PrevLV = Prev->getLinkageAndVisibility(); |
| 530 | if (PrevLV.first) LV.first = PrevLV.first; |
| 531 | LV.second = minVisibility(LV.second, PrevLV.second); |
| 532 | } |
| 533 | |
| 534 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | // C++ [basic.link]p6: |
| 539 | // Names not covered by these rules have no linkage. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 540 | return LVPair(NoLinkage, DefaultVisibility); |
| 541 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 542 | |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 543 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 544 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 545 | } |
| 546 | |
| 547 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 548 | const DeclContext *Ctx = getDeclContext(); |
| 549 | |
| 550 | if (Ctx->isFunctionOrMethod()) |
| 551 | return getNameAsString(); |
| 552 | |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 553 | typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy; |
| 554 | ContextsTy Contexts; |
| 555 | |
| 556 | // Collect contexts. |
| 557 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 558 | Contexts.push_back(Ctx); |
| 559 | Ctx = Ctx->getParent(); |
| 560 | }; |
| 561 | |
| 562 | std::string QualName; |
| 563 | llvm::raw_string_ostream OS(QualName); |
| 564 | |
| 565 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 566 | I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 568 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 569 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 570 | std::string TemplateArgsStr |
| 571 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 572 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 573 | TemplateArgs.flat_size(), |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 574 | P); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 575 | OS << Spec->getName() << TemplateArgsStr; |
| 576 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 577 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 578 | OS << "<anonymous namespace>"; |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 579 | else |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 580 | OS << ND; |
| 581 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 582 | if (!RD->getIdentifier()) |
| 583 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 584 | else |
| 585 | OS << RD; |
| 586 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 587 | const FunctionProtoType *FT = 0; |
| 588 | if (FD->hasWrittenPrototype()) |
| 589 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 590 | |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 591 | OS << FD << '('; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 592 | if (FT) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 593 | unsigned NumParams = FD->getNumParams(); |
| 594 | for (unsigned i = 0; i < NumParams; ++i) { |
| 595 | if (i) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 596 | OS << ", "; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 597 | std::string Param; |
| 598 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 599 | OS << Param; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (FT->isVariadic()) { |
| 603 | if (NumParams > 0) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 604 | OS << ", "; |
| 605 | OS << "..."; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 608 | OS << ')'; |
| 609 | } else { |
| 610 | OS << cast<NamedDecl>(*I); |
| 611 | } |
| 612 | OS << "::"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 613 | } |
| 614 | |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 615 | if (getDeclName()) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 616 | OS << this; |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 617 | else |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 618 | OS << "<anonymous>"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 619 | |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 620 | return OS.str(); |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 623 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 624 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 625 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 626 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 627 | // We want to keep it, unless it nominates same namespace. |
| 628 | if (getKind() == Decl::UsingDirective) { |
| 629 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 630 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 631 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 633 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 634 | // For function declarations, we keep track of redeclarations. |
| 635 | return FD->getPreviousDeclaration() == OldD; |
| 636 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 637 | // For function templates, the underlying function declarations are linked. |
| 638 | if (const FunctionTemplateDecl *FunctionTemplate |
| 639 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 640 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 641 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 642 | return FunctionTemplate->getTemplatedDecl() |
| 643 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Steve Naroff | c4173fa | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 645 | // For method declarations, we keep track of redeclarations. |
| 646 | if (isa<ObjCMethodDecl>(this)) |
| 647 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 649 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 650 | return true; |
| 651 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 652 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 653 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 654 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 655 | |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 656 | // For non-function declarations, if the declarations are of the |
| 657 | // same kind then this must be a redeclaration, or semantic analysis |
| 658 | // would not have given us the new declaration. |
| 659 | return this->getKind() == OldD->getKind(); |
| 660 | } |
| 661 | |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 662 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 663 | return getLinkage() != NoLinkage; |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 664 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 665 | |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 666 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 667 | NamedDecl *ND = this; |
| 668 | while (true) { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 669 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 670 | ND = UD->getTargetDecl(); |
| 671 | else if (ObjCCompatibleAliasDecl *AD |
| 672 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 673 | return AD->getClassInterface(); |
| 674 | else |
| 675 | return ND; |
| 676 | } |
| 677 | } |
| 678 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 679 | bool NamedDecl::isCXXInstanceMember() const { |
| 680 | assert(isCXXClassMember() && |
| 681 | "checking whether non-member is instance member"); |
| 682 | |
| 683 | const NamedDecl *D = this; |
| 684 | if (isa<UsingShadowDecl>(D)) |
| 685 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 686 | |
| 687 | if (isa<FieldDecl>(D)) |
| 688 | return true; |
| 689 | if (isa<CXXMethodDecl>(D)) |
| 690 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 691 | if (isa<FunctionTemplateDecl>(D)) |
| 692 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 693 | ->getTemplatedDecl())->isInstance(); |
| 694 | return false; |
| 695 | } |
| 696 | |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 697 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 698 | // DeclaratorDecl Implementation |
| 699 | //===----------------------------------------------------------------------===// |
| 700 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 701 | template <typename DeclT> |
| 702 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 703 | if (decl->getNumTemplateParameterLists() > 0) |
| 704 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 705 | else |
| 706 | return decl->getInnerLocStart(); |
| 707 | } |
| 708 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 709 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 710 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 711 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 712 | return SourceLocation(); |
| 713 | } |
| 714 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 715 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 716 | SourceRange QualifierRange) { |
| 717 | if (Qualifier) { |
| 718 | // Make sure the extended decl info is allocated. |
| 719 | if (!hasExtInfo()) { |
| 720 | // Save (non-extended) type source info pointer. |
| 721 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 722 | // Allocate external info struct. |
| 723 | DeclInfo = new (getASTContext()) ExtInfo; |
| 724 | // Restore savedTInfo into (extended) decl info. |
| 725 | getExtInfo()->TInfo = savedTInfo; |
| 726 | } |
| 727 | // Set qualifier info. |
| 728 | getExtInfo()->NNS = Qualifier; |
| 729 | getExtInfo()->NNSRange = QualifierRange; |
| 730 | } |
| 731 | else { |
| 732 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 733 | assert(QualifierRange.isInvalid()); |
| 734 | if (hasExtInfo()) { |
| 735 | // Save type source info pointer. |
| 736 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 737 | // Deallocate the extended decl info. |
| 738 | getASTContext().Deallocate(getExtInfo()); |
| 739 | // Restore savedTInfo into (non-extended) decl info. |
| 740 | DeclInfo = savedTInfo; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 745 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 746 | return getTemplateOrInnerLocStart(this); |
| 747 | } |
| 748 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 749 | void |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 750 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 751 | unsigned NumTPLists, |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 752 | TemplateParameterList **TPLists) { |
| 753 | assert((NumTPLists == 0 || TPLists != 0) && |
| 754 | "Empty array of template parameters with positive size!"); |
| 755 | assert((NumTPLists == 0 || NNS) && |
| 756 | "Nonempty array of template parameters with no qualifier!"); |
| 757 | |
| 758 | // Free previous template parameters (if any). |
| 759 | if (NumTemplParamLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 760 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 761 | TemplParamLists = 0; |
| 762 | NumTemplParamLists = 0; |
| 763 | } |
| 764 | // Set info on matched template parameter lists (if any). |
| 765 | if (NumTPLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 766 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 767 | NumTemplParamLists = NumTPLists; |
| 768 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 769 | TemplParamLists[i] = TPLists[i]; |
| 770 | } |
| 771 | } |
| 772 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 773 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 774 | // VarDecl Implementation |
| 775 | //===----------------------------------------------------------------------===// |
| 776 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 777 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 778 | switch (SC) { |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 779 | case SC_None: break; |
| 780 | case SC_Auto: return "auto"; break; |
| 781 | case SC_Extern: return "extern"; break; |
| 782 | case SC_PrivateExtern: return "__private_extern__"; break; |
| 783 | case SC_Register: return "register"; break; |
| 784 | case SC_Static: return "static"; break; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | assert(0 && "Invalid storage class"); |
| 788 | return 0; |
| 789 | } |
| 790 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 791 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 792 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 793 | StorageClass S, StorageClass SCAsWritten) { |
| 794 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 797 | SourceLocation VarDecl::getInnerLocStart() const { |
Douglas Gregor | 562c1f9 | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 798 | SourceLocation Start = getTypeSpecStartLoc(); |
| 799 | if (Start.isInvalid()) |
| 800 | Start = getLocation(); |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 801 | return Start; |
| 802 | } |
| 803 | |
| 804 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 805 | if (getInit()) |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 806 | return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); |
| 807 | return SourceRange(getOuterLocStart(), getLocation()); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 810 | bool VarDecl::isExternC() const { |
| 811 | ASTContext &Context = getASTContext(); |
| 812 | if (!Context.getLangOptions().CPlusPlus) |
| 813 | return (getDeclContext()->isTranslationUnit() && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 814 | getStorageClass() != SC_Static) || |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 815 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 816 | |
| 817 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 818 | DC = DC->getParent()) { |
| 819 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 820 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 821 | return getStorageClass() != SC_Static; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 822 | |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | if (DC->isFunctionOrMethod()) |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | VarDecl *VarDecl::getCanonicalDecl() { |
| 834 | return getFirstDeclaration(); |
| 835 | } |
| 836 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 837 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { |
| 838 | // C++ [basic.def]p2: |
| 839 | // A declaration is a definition unless [...] it contains the 'extern' |
| 840 | // specifier or a linkage-specification and neither an initializer [...], |
| 841 | // it declares a static data member in a class declaration [...]. |
| 842 | // C++ [temp.expl.spec]p15: |
| 843 | // An explicit specialization of a static data member of a template is a |
| 844 | // definition if the declaration includes an initializer; otherwise, it is |
| 845 | // a declaration. |
| 846 | if (isStaticDataMember()) { |
| 847 | if (isOutOfLine() && (hasInit() || |
| 848 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 849 | return Definition; |
| 850 | else |
| 851 | return DeclarationOnly; |
| 852 | } |
| 853 | // C99 6.7p5: |
| 854 | // A definition of an identifier is a declaration for that identifier that |
| 855 | // [...] causes storage to be reserved for that object. |
| 856 | // Note: that applies for all non-file-scope objects. |
| 857 | // C99 6.9.2p1: |
| 858 | // If the declaration of an identifier for an object has file scope and an |
| 859 | // initializer, the declaration is an external definition for the identifier |
| 860 | if (hasInit()) |
| 861 | return Definition; |
| 862 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 863 | if (hasExternalStorage()) |
| 864 | return DeclarationOnly; |
Fariborz Jahanian | cc99b3c | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 865 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 866 | if (getStorageClassAsWritten() == SC_Extern || |
| 867 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Fariborz Jahanian | cc99b3c | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 868 | for (const VarDecl *PrevVar = getPreviousDeclaration(); |
| 869 | PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) { |
| 870 | if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) |
| 871 | return DeclarationOnly; |
| 872 | } |
| 873 | } |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 874 | // C99 6.9.2p2: |
| 875 | // A declaration of an object that has file scope without an initializer, |
| 876 | // and without a storage class specifier or the scs 'static', constitutes |
| 877 | // a tentative definition. |
| 878 | // No such thing in C++. |
| 879 | if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) |
| 880 | return TentativeDefinition; |
| 881 | |
| 882 | // What's left is (in C, block-scope) declarations without initializers or |
| 883 | // external storage. These are definitions. |
| 884 | return Definition; |
| 885 | } |
| 886 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 887 | VarDecl *VarDecl::getActingDefinition() { |
| 888 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 889 | if (Kind != TentativeDefinition) |
| 890 | return 0; |
| 891 | |
Chris Lattner | 48eb14d | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 892 | VarDecl *LastTentative = 0; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 893 | VarDecl *First = getFirstDeclaration(); |
| 894 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 895 | I != E; ++I) { |
| 896 | Kind = (*I)->isThisDeclarationADefinition(); |
| 897 | if (Kind == Definition) |
| 898 | return 0; |
| 899 | else if (Kind == TentativeDefinition) |
| 900 | LastTentative = *I; |
| 901 | } |
| 902 | return LastTentative; |
| 903 | } |
| 904 | |
| 905 | bool VarDecl::isTentativeDefinitionNow() const { |
| 906 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 907 | if (Kind != TentativeDefinition) |
| 908 | return false; |
| 909 | |
| 910 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 911 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 912 | return false; |
| 913 | } |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 914 | return true; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 917 | VarDecl *VarDecl::getDefinition() { |
Sebastian Redl | ccdb5ff | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 918 | VarDecl *First = getFirstDeclaration(); |
| 919 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 920 | I != E; ++I) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 921 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 922 | return *I; |
| 923 | } |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 928 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 929 | while (I != E && !I->getInit()) |
| 930 | ++I; |
| 931 | |
| 932 | if (I != E) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 933 | D = *I; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 934 | return I->getInit(); |
| 935 | } |
| 936 | return 0; |
| 937 | } |
| 938 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 939 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 940 | if (Decl::isOutOfLine()) |
| 941 | return true; |
Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 942 | |
| 943 | if (!isStaticDataMember()) |
| 944 | return false; |
| 945 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 946 | // If this static data member was instantiated from a static data member of |
| 947 | // a class template, check whether that static data member was defined |
| 948 | // out-of-line. |
| 949 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 950 | return VD->isOutOfLine(); |
| 951 | |
| 952 | return false; |
| 953 | } |
| 954 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 955 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 956 | if (!isStaticDataMember()) |
| 957 | return 0; |
| 958 | |
| 959 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 960 | RD != RDEnd; ++RD) { |
| 961 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 962 | return *RD; |
| 963 | } |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 968 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 969 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 970 | Eval->~EvaluatedStmt(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 971 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | Init = I; |
| 975 | } |
| 976 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 977 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 978 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 979 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 984 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 985 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 986 | return MSI->getTemplateSpecializationKind(); |
| 987 | |
| 988 | return TSK_Undeclared; |
| 989 | } |
| 990 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 991 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 992 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 993 | } |
| 994 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 995 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 996 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 997 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 998 | assert(MSI && "Not an instantiated static data member?"); |
| 999 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1000 | if (TSK != TSK_ExplicitSpecialization && |
| 1001 | PointOfInstantiation.isValid() && |
| 1002 | MSI->getPointOfInstantiation().isInvalid()) |
| 1003 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1006 | //===----------------------------------------------------------------------===// |
| 1007 | // ParmVarDecl Implementation |
| 1008 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1009 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1010 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1011 | SourceLocation L, IdentifierInfo *Id, |
| 1012 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1013 | StorageClass S, StorageClass SCAsWritten, |
| 1014 | Expr *DefArg) { |
| 1015 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, |
| 1016 | S, SCAsWritten, DefArg); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1019 | Expr *ParmVarDecl::getDefaultArg() { |
| 1020 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1021 | assert(!hasUninstantiatedDefaultArg() && |
| 1022 | "Default argument is not yet instantiated!"); |
| 1023 | |
| 1024 | Expr *Arg = getInit(); |
| 1025 | if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg)) |
| 1026 | return E->getSubExpr(); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1027 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1028 | return Arg; |
| 1029 | } |
| 1030 | |
| 1031 | unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { |
| 1032 | if (const CXXExprWithTemporaries *E = |
| 1033 | dyn_cast<CXXExprWithTemporaries>(getInit())) |
| 1034 | return E->getNumTemporaries(); |
| 1035 | |
Argyrios Kyrtzidis | 1506d9b | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1036 | return 0; |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1039 | CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { |
| 1040 | assert(getNumDefaultArgTemporaries() && |
| 1041 | "Default arguments does not have any temporaries!"); |
| 1042 | |
| 1043 | CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit()); |
| 1044 | return E->getTemporary(i); |
| 1045 | } |
| 1046 | |
| 1047 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1048 | if (const Expr *E = getInit()) |
| 1049 | return E->getSourceRange(); |
| 1050 | |
| 1051 | if (hasUninstantiatedDefaultArg()) |
| 1052 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1053 | |
| 1054 | return SourceRange(); |
Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1057 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1058 | // FunctionDecl Implementation |
| 1059 | //===----------------------------------------------------------------------===// |
| 1060 | |
John McCall | e1f2ec2 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 1061 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1062 | const PrintingPolicy &Policy, |
| 1063 | bool Qualified) const { |
| 1064 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1065 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1066 | if (TemplateArgs) |
| 1067 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1068 | TemplateArgs->getFlatArgumentList(), |
| 1069 | TemplateArgs->flat_size(), |
| 1070 | Policy); |
| 1071 | |
| 1072 | } |
Ted Kremenek | ce20e8f | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 1073 | |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1074 | bool FunctionDecl::isVariadic() const { |
| 1075 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1076 | return FT->isVariadic(); |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1080 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1081 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1082 | if (I->Body) { |
| 1083 | Definition = *I; |
| 1084 | return true; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1091 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | 1506d9b | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1092 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1093 | if (I->Body) { |
| 1094 | Definition = *I; |
| 1095 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return 0; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1102 | void FunctionDecl::setBody(Stmt *B) { |
| 1103 | Body = B; |
Argyrios Kyrtzidis | 49abd4d | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 1104 | if (B) |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1105 | EndRangeLoc = B->getLocEnd(); |
| 1106 | } |
| 1107 | |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1108 | void FunctionDecl::setPure(bool P) { |
| 1109 | IsPure = P; |
| 1110 | if (P) |
| 1111 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1112 | Parent->markedVirtualFunctionPure(); |
| 1113 | } |
| 1114 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1115 | bool FunctionDecl::isMain() const { |
| 1116 | ASTContext &Context = getASTContext(); |
John McCall | deb8448 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 1117 | return !Context.getLangOptions().Freestanding && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1118 | getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1119 | getIdentifier() && getIdentifier()->isStr("main"); |
| 1120 | } |
| 1121 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1122 | bool FunctionDecl::isExternC() const { |
| 1123 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1124 | // In C, any non-static, non-overloadable function has external |
| 1125 | // linkage. |
| 1126 | if (!Context.getLangOptions().CPlusPlus) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1127 | return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1128 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1130 | DC = DC->getParent()) { |
| 1131 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 1132 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1133 | return getStorageClass() != SC_Static && |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1134 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1135 | |
| 1136 | break; |
| 1137 | } |
Douglas Gregor | 175ea04 | 2010-08-17 16:09:23 +0000 | [diff] [blame] | 1138 | |
| 1139 | if (DC->isRecord()) |
| 1140 | break; |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Douglas Gregor | bff6203 | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 1143 | return isMain(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1146 | bool FunctionDecl::isGlobal() const { |
| 1147 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1148 | return Method->isStatic(); |
| 1149 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1150 | if (getStorageClass() == SC_Static) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1151 | return false; |
| 1152 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1154 | DC->isNamespace(); |
| 1155 | DC = DC->getParent()) { |
| 1156 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1157 | if (!Namespace->getDeclName()) |
| 1158 | return false; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | return true; |
| 1164 | } |
| 1165 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1166 | void |
| 1167 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1168 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1169 | |
| 1170 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1171 | FunctionTemplateDecl *PrevFunTmpl |
| 1172 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1173 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1174 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1179 | return getFirstDeclaration(); |
| 1180 | } |
| 1181 | |
| 1182 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1183 | return getFirstDeclaration(); |
| 1184 | } |
| 1185 | |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1186 | /// \brief Returns a value indicating whether this function |
| 1187 | /// corresponds to a builtin function. |
| 1188 | /// |
| 1189 | /// The function corresponds to a built-in function if it is |
| 1190 | /// declared at translation scope or within an extern "C" block and |
| 1191 | /// its name matches with the name of a builtin. The returned value |
| 1192 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1194 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1195 | unsigned FunctionDecl::getBuiltinID() const { |
| 1196 | ASTContext &Context = getASTContext(); |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1197 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 1198 | return 0; |
| 1199 | |
| 1200 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 1201 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1202 | return BuiltinID; |
| 1203 | |
| 1204 | // This function has the name of a known C library |
| 1205 | // function. Determine whether it actually refers to the C library |
| 1206 | // function or whether it just has the same name. |
| 1207 | |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1208 | // If this is a static function, it's not a builtin. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1209 | if (getStorageClass() == SC_Static) |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1210 | return 0; |
| 1211 | |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1212 | // If this function is at translation-unit scope and we're not in |
| 1213 | // C++, it refers to the C library function. |
| 1214 | if (!Context.getLangOptions().CPlusPlus && |
| 1215 | getDeclContext()->isTranslationUnit()) |
| 1216 | return BuiltinID; |
| 1217 | |
| 1218 | // If the function is in an extern "C" linkage specification and is |
| 1219 | // not marked "overloadable", it's the real function. |
| 1220 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1222 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1223 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1224 | return BuiltinID; |
| 1225 | |
| 1226 | // Not a builtin |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1231 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1232 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1233 | /// after it has been created. |
| 1234 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1235 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1236 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1237 | return 0; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1238 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1242 | void FunctionDecl::setParams(ASTContext &C, |
| 1243 | ParmVarDecl **NewParamInfo, unsigned NumParams) { |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1244 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 9af40c1 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1245 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 1247 | // Zero params -> null pointer. |
| 1248 | if (NumParams) { |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1249 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | 4ba36fc | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1250 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1251 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1252 | |
Argyrios Kyrtzidis | 53aeec3 | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 1253 | // Update source range. The check below allows us to set EndRangeLoc before |
| 1254 | // setting the parameters. |
Argyrios Kyrtzidis | dfc5dca | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 1255 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1256 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 1257 | } |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1260 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1261 | /// needed to call this function. This may be fewer than the number of |
| 1262 | /// function parameters, if some of the parameters have default |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 1263 | /// arguments (in C++). |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1264 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 1265 | unsigned NumRequiredArgs = getNumParams(); |
| 1266 | while (NumRequiredArgs > 0 |
Anders Carlsson | 8544647 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1267 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1268 | --NumRequiredArgs; |
| 1269 | |
| 1270 | return NumRequiredArgs; |
| 1271 | } |
| 1272 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1273 | bool FunctionDecl::isInlined() const { |
Anders Carlsson | cfb65d7 | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1274 | // FIXME: This is not enough. Consider: |
| 1275 | // |
| 1276 | // inline void f(); |
| 1277 | // void f() { } |
| 1278 | // |
| 1279 | // f is inlined, but does not have inline specified. |
| 1280 | // To fix this we should add an 'inline' flag to FunctionDecl. |
| 1281 | if (isInlineSpecified()) |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1282 | return true; |
Anders Carlsson | cfb65d7 | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (isa<CXXMethodDecl>(this)) { |
| 1285 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1286 | return true; |
| 1287 | } |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1288 | |
| 1289 | switch (getTemplateSpecializationKind()) { |
| 1290 | case TSK_Undeclared: |
| 1291 | case TSK_ExplicitSpecialization: |
| 1292 | return false; |
| 1293 | |
| 1294 | case TSK_ImplicitInstantiation: |
| 1295 | case TSK_ExplicitInstantiationDeclaration: |
| 1296 | case TSK_ExplicitInstantiationDefinition: |
| 1297 | // Handle below. |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1302 | bool HasPattern = false; |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1303 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1304 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1305 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1306 | if (HasPattern && PatternDecl) |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1307 | return PatternDecl->isInlined(); |
| 1308 | |
| 1309 | return false; |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1312 | /// \brief For an inline function definition in C or C++, determine whether the |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1313 | /// definition will be externally visible. |
| 1314 | /// |
| 1315 | /// Inline function definitions are always available for inlining optimizations. |
| 1316 | /// However, depending on the language dialect, declaration specifiers, and |
| 1317 | /// attributes, the definition of an inline function may or may not be |
| 1318 | /// "externally" visible to other translation units in the program. |
| 1319 | /// |
| 1320 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 1321 | /// if even one of the global-scope declarations is marked "extern inline", the |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1322 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 1323 | /// |
| 1324 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 1325 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 1326 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 1327 | /// an externally visible symbol, but "extern inline" will not create an |
| 1328 | /// externally visible symbol. |
| 1329 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 1330 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1331 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1332 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1333 | |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1334 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1335 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 1336 | // following heuristic: if the "inline" keyword is present on a |
| 1337 | // declaration of the function but "extern" is not present on that |
| 1338 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 1339 | // "extern inline" semantics applies and the symbol is not externally |
| 1340 | // visible. |
| 1341 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1342 | Redecl != RedeclEnd; |
| 1343 | ++Redecl) { |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1344 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern) |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1345 | return true; |
| 1346 | } |
| 1347 | |
| 1348 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1349 | return false; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | // C99 6.7.4p6: |
| 1353 | // [...] If all of the file scope declarations for a function in a |
| 1354 | // translation unit include the inline function specifier without extern, |
| 1355 | // then the definition in that translation unit is an inline definition. |
| 1356 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1357 | Redecl != RedeclEnd; |
| 1358 | ++Redecl) { |
| 1359 | // Only consider file-scope declarations in this test. |
| 1360 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1361 | continue; |
| 1362 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1363 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1364 | return true; // Not an inline definition |
| 1365 | } |
| 1366 | |
| 1367 | // C99 6.7.4p6: |
| 1368 | // An inline definition does not provide an external definition for the |
| 1369 | // function, and does not forbid an external definition in another |
| 1370 | // translation unit. |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1371 | return false; |
| 1372 | } |
| 1373 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1374 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 1375 | /// function represents, if any. |
| 1376 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1377 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 1378 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1379 | else |
| 1380 | return OO_None; |
| 1381 | } |
| 1382 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 1383 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 1384 | /// represents, if any. |
| 1385 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 1386 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 1387 | return getDeclName().getCXXLiteralIdentifier(); |
| 1388 | else |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1392 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 1393 | if (TemplateOrSpecialization.isNull()) |
| 1394 | return TK_NonTemplate; |
| 1395 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 1396 | return TK_FunctionTemplate; |
| 1397 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 1398 | return TK_MemberSpecialization; |
| 1399 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 1400 | return TK_FunctionTemplateSpecialization; |
| 1401 | if (TemplateOrSpecialization.is |
| 1402 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 1403 | return TK_DependentFunctionTemplateSpecialization; |
| 1404 | |
| 1405 | assert(false && "Did we miss a TemplateOrSpecialization type?"); |
| 1406 | return TK_NonTemplate; |
| 1407 | } |
| 1408 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1409 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1410 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1411 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 1412 | |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1416 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 1417 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1418 | } |
| 1419 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1420 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1421 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 1422 | FunctionDecl *FD, |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1423 | TemplateSpecializationKind TSK) { |
| 1424 | assert(TemplateOrSpecialization.isNull() && |
| 1425 | "Member function is already a specialization"); |
| 1426 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1427 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1428 | TemplateOrSpecialization = Info; |
| 1429 | } |
| 1430 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1431 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1432 | // If the function is invalid, it can't be implicitly instantiated. |
| 1433 | if (isInvalidDecl()) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1434 | return false; |
| 1435 | |
| 1436 | switch (getTemplateSpecializationKind()) { |
| 1437 | case TSK_Undeclared: |
| 1438 | case TSK_ExplicitSpecialization: |
| 1439 | case TSK_ExplicitInstantiationDefinition: |
| 1440 | return false; |
| 1441 | |
| 1442 | case TSK_ImplicitInstantiation: |
| 1443 | return true; |
| 1444 | |
| 1445 | case TSK_ExplicitInstantiationDeclaration: |
| 1446 | // Handled below. |
| 1447 | break; |
| 1448 | } |
| 1449 | |
| 1450 | // Find the actual template from which we will instantiate. |
| 1451 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1452 | bool HasPattern = false; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1453 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1454 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1455 | |
| 1456 | // C++0x [temp.explicit]p9: |
| 1457 | // Except for inline functions, other explicit instantiation declarations |
| 1458 | // have the effect of suppressing the implicit instantiation of the entity |
| 1459 | // to which they refer. |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1460 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1461 | return true; |
| 1462 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1463 | return PatternDecl->isInlined(); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1467 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1468 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1469 | // If we have hit a point where the user provided a specialization of |
| 1470 | // this template, we're done looking. |
| 1471 | if (Primary->isMemberSpecialization()) |
| 1472 | break; |
| 1473 | |
| 1474 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1475 | } |
| 1476 | |
| 1477 | return Primary->getTemplatedDecl(); |
| 1478 | } |
| 1479 | |
| 1480 | return getInstantiatedFromMemberFunction(); |
| 1481 | } |
| 1482 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1483 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1485 | = TemplateOrSpecialization |
| 1486 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1487 | return Info->Template.getPointer(); |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1488 | } |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | const TemplateArgumentList * |
| 1493 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1495 | = TemplateOrSpecialization |
| 1496 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1497 | return Info->TemplateArguments; |
| 1498 | } |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1502 | const TemplateArgumentListInfo * |
| 1503 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 1504 | if (FunctionTemplateSpecializationInfo *Info |
| 1505 | = TemplateOrSpecialization |
| 1506 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 1507 | return Info->TemplateArgumentsAsWritten; |
| 1508 | } |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1513 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 1514 | FunctionTemplateDecl *Template, |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1515 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1516 | void *InsertPos, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1517 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 1518 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 1519 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1520 | assert(TSK != TSK_Undeclared && |
| 1521 | "Must specify the type of function template specialization"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1523 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1524 | if (!Info) |
Argyrios Kyrtzidis | e262a95 | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 1525 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 1526 | TemplateArgs, |
| 1527 | TemplateArgsAsWritten, |
| 1528 | PointOfInstantiation); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1529 | TemplateOrSpecialization = Info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1531 | // Insert this function template specialization into the set of known |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1532 | // function template specializations. |
| 1533 | if (InsertPos) |
| 1534 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1535 | else { |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1536 | // Try to insert the new node. If there is an existing node, leave it, the |
| 1537 | // set will contain the canonical decls while |
| 1538 | // FunctionTemplateDecl::findSpecialization will return |
| 1539 | // the most recent redeclarations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1540 | FunctionTemplateSpecializationInfo *Existing |
| 1541 | = Template->getSpecializations().GetOrInsertNode(Info); |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1542 | (void)Existing; |
| 1543 | assert((!Existing || Existing->Function->isCanonicalDecl()) && |
| 1544 | "Set is supposed to only contain canonical decls"); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1545 | } |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1548 | void |
| 1549 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 1550 | const UnresolvedSetImpl &Templates, |
| 1551 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1552 | assert(TemplateOrSpecialization.isNull()); |
| 1553 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 1554 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 900d980 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 1555 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1556 | void *Buffer = Context.Allocate(Size); |
| 1557 | DependentFunctionTemplateSpecializationInfo *Info = |
| 1558 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 1559 | TemplateArgs); |
| 1560 | TemplateOrSpecialization = Info; |
| 1561 | } |
| 1562 | |
| 1563 | DependentFunctionTemplateSpecializationInfo:: |
| 1564 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 1565 | const TemplateArgumentListInfo &TArgs) |
| 1566 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 1567 | |
| 1568 | d.NumTemplates = Ts.size(); |
| 1569 | d.NumArgs = TArgs.size(); |
| 1570 | |
| 1571 | FunctionTemplateDecl **TsArray = |
| 1572 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 1573 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 1574 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 1575 | |
| 1576 | TemplateArgumentLoc *ArgsArray = |
| 1577 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 1578 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 1579 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 1580 | } |
| 1581 | |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1582 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | // For a function template specialization, query the specialization |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1584 | // information object. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1585 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1586 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1587 | if (FTSInfo) |
| 1588 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1590 | MemberSpecializationInfo *MSInfo |
| 1591 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1592 | if (MSInfo) |
| 1593 | return MSInfo->getTemplateSpecializationKind(); |
| 1594 | |
| 1595 | return TSK_Undeclared; |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | void |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1599 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1600 | SourceLocation PointOfInstantiation) { |
| 1601 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1602 | = TemplateOrSpecialization.dyn_cast< |
| 1603 | FunctionTemplateSpecializationInfo*>()) { |
| 1604 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1605 | if (TSK != TSK_ExplicitSpecialization && |
| 1606 | PointOfInstantiation.isValid() && |
| 1607 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1608 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1609 | } else if (MemberSpecializationInfo *MSInfo |
| 1610 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1611 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1612 | if (TSK != TSK_ExplicitSpecialization && |
| 1613 | PointOfInstantiation.isValid() && |
| 1614 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1615 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1616 | } else |
| 1617 | assert(false && "Function cannot have a template specialization kind"); |
| 1618 | } |
| 1619 | |
| 1620 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1621 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1622 | = TemplateOrSpecialization.dyn_cast< |
| 1623 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1624 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1625 | else if (MemberSpecializationInfo *MSInfo |
| 1626 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1627 | return MSInfo->getPointOfInstantiation(); |
| 1628 | |
| 1629 | return SourceLocation(); |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1632 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1633 | if (Decl::isOutOfLine()) |
| 1634 | return true; |
| 1635 | |
| 1636 | // If this function was instantiated from a member function of a |
| 1637 | // class template, check whether that member function was defined out-of-line. |
| 1638 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1639 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1640 | if (FD->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1641 | return Definition->isOutOfLine(); |
| 1642 | } |
| 1643 | |
| 1644 | // If this function was instantiated from a function template, |
| 1645 | // check whether that function template was defined out-of-line. |
| 1646 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1647 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1648 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1649 | return Definition->isOutOfLine(); |
| 1650 | } |
| 1651 | |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1655 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1656 | // FieldDecl Implementation |
| 1657 | //===----------------------------------------------------------------------===// |
| 1658 | |
| 1659 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1660 | IdentifierInfo *Id, QualType T, |
| 1661 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 1662 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); |
| 1663 | } |
| 1664 | |
| 1665 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 1666 | if (!isImplicit() || getDeclName()) |
| 1667 | return false; |
| 1668 | |
| 1669 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 1670 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 1671 | |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
| 1675 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1676 | // TagDecl Implementation |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1677 | //===----------------------------------------------------------------------===// |
| 1678 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1679 | SourceLocation TagDecl::getOuterLocStart() const { |
| 1680 | return getTemplateOrInnerLocStart(this); |
| 1681 | } |
| 1682 | |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1683 | SourceRange TagDecl::getSourceRange() const { |
| 1684 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1685 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1688 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1689 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1692 | void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { |
| 1693 | TypedefDeclOrQualifier = TDD; |
| 1694 | if (TypeForDecl) |
| 1695 | TypeForDecl->ClearLinkageCache(); |
| 1696 | } |
| 1697 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1698 | void TagDecl::startDefinition() { |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1699 | IsBeingDefined = true; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (isa<CXXRecordDecl>(this)) { |
| 1702 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 1703 | struct CXXRecordDecl::DefinitionData *Data = |
| 1704 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 93cc732 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 1705 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 1706 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1707 | } |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | void TagDecl::completeDefinition() { |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 1711 | assert((!isa<CXXRecordDecl>(this) || |
| 1712 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 1713 | "definition completed but not started"); |
| 1714 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1715 | IsDefinition = true; |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1716 | IsBeingDefined = false; |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame^] | 1717 | |
| 1718 | if (ASTMutationListener *L = getASTMutationListener()) |
| 1719 | L->CompletedTagDefinition(this); |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1722 | TagDecl* TagDecl::getDefinition() const { |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1723 | if (isDefinition()) |
| 1724 | return const_cast<TagDecl *>(this); |
Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 1725 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 1726 | return CXXRD->getDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
| 1728 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1729 | R != REnd; ++R) |
| 1730 | if (R->isDefinition()) |
| 1731 | return *R; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1733 | return 0; |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1736 | void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 1737 | SourceRange QualifierRange) { |
| 1738 | if (Qualifier) { |
| 1739 | // Make sure the extended qualifier info is allocated. |
| 1740 | if (!hasExtInfo()) |
| 1741 | TypedefDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 1742 | // Set qualifier info. |
| 1743 | getExtInfo()->NNS = Qualifier; |
| 1744 | getExtInfo()->NNSRange = QualifierRange; |
| 1745 | } |
| 1746 | else { |
| 1747 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 1748 | assert(QualifierRange.isInvalid()); |
| 1749 | if (hasExtInfo()) { |
| 1750 | getASTContext().Deallocate(getExtInfo()); |
| 1751 | TypedefDeclOrQualifier = (TypedefDecl*) 0; |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1756 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1757 | // EnumDecl Implementation |
| 1758 | //===----------------------------------------------------------------------===// |
| 1759 | |
| 1760 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1761 | IdentifierInfo *Id, SourceLocation TKL, |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1762 | EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) { |
| 1763 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL, |
| 1764 | IsScoped, IsFixed); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1765 | C.getTypeDeclType(Enum, PrevDecl); |
| 1766 | return Enum; |
| 1767 | } |
| 1768 | |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1769 | EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1770 | return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(), |
| 1771 | false, false); |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1774 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1775 | QualType NewPromotionType, |
| 1776 | unsigned NumPositiveBits, |
| 1777 | unsigned NumNegativeBits) { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1778 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1779 | if (!IntegerType) |
| 1780 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1781 | PromotionType = NewPromotionType; |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1782 | setNumPositiveBits(NumPositiveBits); |
| 1783 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1784 | TagDecl::completeDefinition(); |
| 1785 | } |
| 1786 | |
| 1787 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1788 | // RecordDecl Implementation |
| 1789 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 1790 | |
Argyrios Kyrtzidis | 88e1b97 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1791 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1792 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1793 | SourceLocation TKL) |
| 1794 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1795 | HasFlexibleArrayMember = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1796 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1797 | HasObjectMember = false; |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1798 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1799 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1803 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 82fe3e3 | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1804 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1806 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1807 | C.getTypeDeclType(R, PrevDecl); |
| 1808 | return R; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1811 | RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) { |
| 1812 | return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0, |
| 1813 | SourceLocation()); |
| 1814 | } |
| 1815 | |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1816 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1818 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1819 | } |
| 1820 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1821 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 1822 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 1823 | LoadFieldsFromExternalStorage(); |
| 1824 | |
| 1825 | return field_iterator(decl_iterator(FirstDecl)); |
| 1826 | } |
| 1827 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1828 | /// completeDefinition - Notes that the definition of this type is now |
| 1829 | /// complete. |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1830 | void RecordDecl::completeDefinition() { |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 1831 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1832 | TagDecl::completeDefinition(); |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 1833 | } |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 1834 | |
John McCall | 61925b0 | 2010-05-21 01:17:40 +0000 | [diff] [blame] | 1835 | ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() { |
| 1836 | // Force the decl chain to come into existence properly. |
| 1837 | if (!getNextDeclInContext()) getParent()->decls_begin(); |
| 1838 | |
| 1839 | assert(isAnonymousStructOrUnion()); |
| 1840 | ValueDecl *D = cast<ValueDecl>(getNextDeclInContext()); |
| 1841 | assert(D->getType()->isRecordType()); |
| 1842 | assert(D->getType()->getAs<RecordType>()->getDecl() == this); |
| 1843 | return D; |
| 1844 | } |
| 1845 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1846 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 1847 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 1848 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 1849 | |
| 1850 | // Notify that we have a RecordDecl doing some initialization. |
| 1851 | ExternalASTSource::Deserializing TheFields(Source); |
| 1852 | |
| 1853 | llvm::SmallVector<Decl*, 64> Decls; |
| 1854 | if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) |
| 1855 | return; |
| 1856 | |
| 1857 | #ifndef NDEBUG |
| 1858 | // Check that all decls we got were FieldDecls. |
| 1859 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 1860 | assert(isa<FieldDecl>(Decls[i])); |
| 1861 | #endif |
| 1862 | |
| 1863 | LoadedFieldsFromExternalStorage = true; |
| 1864 | |
| 1865 | if (Decls.empty()) |
| 1866 | return; |
| 1867 | |
| 1868 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 1869 | } |
| 1870 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1871 | //===----------------------------------------------------------------------===// |
| 1872 | // BlockDecl Implementation |
| 1873 | //===----------------------------------------------------------------------===// |
| 1874 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1875 | void BlockDecl::setParams(ParmVarDecl **NewParamInfo, |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1876 | unsigned NParms) { |
| 1877 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1879 | // Zero params -> null pointer. |
| 1880 | if (NParms) { |
| 1881 | NumParams = NParms; |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1882 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1883 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1884 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | unsigned BlockDecl::getNumParams() const { |
| 1889 | return NumParams; |
| 1890 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1891 | |
| 1892 | |
| 1893 | //===----------------------------------------------------------------------===// |
| 1894 | // Other Decl Allocation/Deallocation Method Implementations |
| 1895 | //===----------------------------------------------------------------------===// |
| 1896 | |
| 1897 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 1898 | return new (C) TranslationUnitDecl(C); |
| 1899 | } |
| 1900 | |
| 1901 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 1902 | SourceLocation L, IdentifierInfo *Id) { |
| 1903 | return new (C) NamespaceDecl(DC, L, Id); |
| 1904 | } |
| 1905 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1906 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 1907 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
| 1908 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
| 1909 | } |
| 1910 | |
| 1911 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1912 | const DeclarationNameInfo &NameInfo, |
| 1913 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1914 | StorageClass S, StorageClass SCAsWritten, |
| 1915 | bool isInline, bool hasWrittenPrototype) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1916 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1917 | S, SCAsWritten, isInline); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1918 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 1919 | return New; |
| 1920 | } |
| 1921 | |
| 1922 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 1923 | return new (C) BlockDecl(DC, L); |
| 1924 | } |
| 1925 | |
| 1926 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 1927 | SourceLocation L, |
| 1928 | IdentifierInfo *Id, QualType T, |
| 1929 | Expr *E, const llvm::APSInt &V) { |
| 1930 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 1931 | } |
| 1932 | |
Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 1933 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 1934 | SourceLocation End = getLocation(); |
| 1935 | if (Init) |
| 1936 | End = Init->getLocEnd(); |
| 1937 | return SourceRange(getLocation(), End); |
| 1938 | } |
| 1939 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1940 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 1941 | SourceLocation L, IdentifierInfo *Id, |
| 1942 | TypeSourceInfo *TInfo) { |
| 1943 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
| 1944 | } |
| 1945 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1946 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 1947 | SourceLocation L, |
| 1948 | StringLiteral *Str) { |
| 1949 | return new (C) FileScopeAsmDecl(DC, L, Str); |
| 1950 | } |