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