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