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