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" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/Attr.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.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" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/Stmt.h" |
| 25 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 27 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 28 | #include "clang/Basic/Module.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Specifiers.h" |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 37 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 40 | // Visibility rules aren't rigorously externally specified, but here |
| 41 | // are the basic principles behind what we implement: |
| 42 | // |
| 43 | // 1. An explicit visibility attribute is generally a direct expression |
| 44 | // of the user's intent and should be honored. Only the innermost |
| 45 | // visibility attribute applies. If no visibility attribute applies, |
| 46 | // global visibility settings are considered. |
| 47 | // |
| 48 | // 2. There is one caveat to the above: on or in a template pattern, |
| 49 | // an explicit visibility attribute is just a default rule, and |
| 50 | // visibility can be decreased by the visibility of template |
| 51 | // arguments. But this, too, has an exception: an attribute on an |
| 52 | // explicit specialization or instantiation causes all the visibility |
| 53 | // restrictions of the template arguments to be ignored. |
| 54 | // |
| 55 | // 3. A variable that does not otherwise have explicit visibility can |
| 56 | // be restricted by the visibility of its type. |
| 57 | // |
| 58 | // 4. A visibility restriction is explicit if it comes from an |
| 59 | // attribute (or something like it), not a global visibility setting. |
| 60 | // When emitting a reference to an external symbol, visibility |
| 61 | // restrictions are ignored unless they are explicit. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 62 | // |
| 63 | // 5. When computing the visibility of a non-type, including a |
| 64 | // non-type member of a class, only non-type visibility restrictions |
| 65 | // are considered: the 'visibility' attribute, global value-visibility |
| 66 | // settings, and a few special cases like __private_extern. |
| 67 | // |
| 68 | // 6. When computing the visibility of a type, including a type member |
| 69 | // of a class, only type visibility restrictions are considered: |
| 70 | // the 'type_visibility' attribute and global type-visibility settings. |
| 71 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 72 | // attribute on any declaration that only has the former. |
| 73 | // |
| 74 | // The visibility of a "secondary" entity, like a template argument, |
| 75 | // is computed using the kind of that entity, not the kind of the |
| 76 | // primary entity for which we are computing visibility. For example, |
| 77 | // the visibility of a specialization of either of these templates: |
| 78 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 79 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 80 | // is restricted according to the type visibility of the argument 'T', |
| 81 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 82 | // the argument function 'compare'. That 'has_match' is a value |
| 83 | // and 'matcher' is a type only matters when looking for attributes |
| 84 | // and settings from the immediate context. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 85 | |
| 86 | /// Kinds of LV computation. The linkage side of the computation is |
| 87 | /// always the same, but different things can change how visibility is |
| 88 | /// computed. |
| 89 | enum LVComputationKind { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 90 | /// Do a normal LV computation for, ultimately, a type. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 91 | LVForType = NamedDecl::VisibilityForType, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 92 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 93 | /// Do a normal LV computation for, ultimately, a non-type declaration. |
| 94 | LVForValue = NamedDecl::VisibilityForValue, |
| 95 | |
| 96 | /// Do a normal LV computation for, ultimately, a type that already |
| 97 | /// has some sort of explicit visibility. |
| 98 | LVForExplicitType, |
| 99 | |
| 100 | /// Do a normal LV computation for, ultimately, a non-type declaration |
| 101 | /// that already has some sort of explicit visibility. |
| 102 | LVForExplicitValue |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 105 | /// Does this computation kind permit us to consider additional |
| 106 | /// visibility settings from attributes and the like? |
| 107 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
| 108 | return ((unsigned(computation) & 2) != 0); |
| 109 | } |
| 110 | |
| 111 | /// Given an LVComputationKind, return one of the same type/value sort |
| 112 | /// that records that it already has explicit visibility. |
| 113 | static LVComputationKind |
| 114 | withExplicitVisibilityAlready(LVComputationKind oldKind) { |
| 115 | LVComputationKind newKind = |
| 116 | static_cast<LVComputationKind>(unsigned(oldKind) | 2); |
| 117 | assert(oldKind != LVForType || newKind == LVForExplicitType); |
| 118 | assert(oldKind != LVForValue || newKind == LVForExplicitValue); |
| 119 | assert(oldKind != LVForExplicitType || newKind == LVForExplicitType); |
| 120 | assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue); |
| 121 | return newKind; |
| 122 | } |
| 123 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 124 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 125 | LVComputationKind kind) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 126 | assert(!hasExplicitVisibilityAlready(kind) && |
| 127 | "asking for explicit visibility when we shouldn't be"); |
| 128 | return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind); |
| 129 | } |
| 130 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 131 | typedef NamedDecl::LinkageInfo LinkageInfo; |
| 132 | |
| 133 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 134 | /// visibility computation? |
| 135 | static bool usesTypeVisibility(const NamedDecl *D) { |
John McCall | a880b19 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 136 | return isa<TypeDecl>(D) || |
| 137 | isa<ClassTemplateDecl>(D) || |
| 138 | isa<ObjCInterfaceDecl>(D); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 139 | } |
| 140 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 141 | /// Given a visibility attribute, return the explicit visibility |
| 142 | /// associated with it. |
| 143 | template <class T> |
| 144 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 145 | switch (attr->getVisibility()) { |
| 146 | case T::Default: |
| 147 | return DefaultVisibility; |
| 148 | case T::Hidden: |
| 149 | return HiddenVisibility; |
| 150 | case T::Protected: |
| 151 | return ProtectedVisibility; |
| 152 | } |
| 153 | llvm_unreachable("bad visibility kind"); |
| 154 | } |
| 155 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 156 | /// Return the explicit visibility of the given declaration. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 157 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 158 | NamedDecl::ExplicitVisibilityKind kind) { |
| 159 | // If we're ultimately computing the visibility of a type, look for |
| 160 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 161 | if (kind == NamedDecl::VisibilityForType) { |
| 162 | if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) { |
| 163 | return getVisibilityFromAttr(A); |
| 164 | } |
| 165 | } |
| 166 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 167 | // If this declaration has an explicit visibility attribute, use it. |
| 168 | if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 169 | return getVisibilityFromAttr(A); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 171 | |
| 172 | // If we're on Mac OS X, an 'availability' for Mac OS X attribute |
| 173 | // implies visibility(default). |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 174 | if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 175 | for (specific_attr_iterator<AvailabilityAttr> |
| 176 | A = D->specific_attr_begin<AvailabilityAttr>(), |
| 177 | AEnd = D->specific_attr_end<AvailabilityAttr>(); |
| 178 | A != AEnd; ++A) |
| 179 | if ((*A)->getPlatform()->getName().equals("macosx")) |
| 180 | return DefaultVisibility; |
| 181 | } |
| 182 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 183 | return None; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 186 | static LinkageInfo getLVForType(QualType T) { |
| 187 | std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility(); |
| 188 | return LinkageInfo(P.first, P.second, T->isVisibilityExplicit()); |
| 189 | } |
| 190 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 191 | /// \brief Get the most restrictive linkage for the types in the given |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 192 | /// template parameter list. For visibility purposes, template |
| 193 | /// parameters are part of the signature of a template. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 194 | static LinkageInfo |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 195 | getLVForTemplateParameterList(const TemplateParameterList *params) { |
| 196 | LinkageInfo LV; |
| 197 | for (TemplateParameterList::const_iterator P = params->begin(), |
| 198 | PEnd = params->end(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 199 | P != PEnd; ++P) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 200 | |
| 201 | // Template type parameters are the most common and never |
| 202 | // contribute to visibility, pack or not. |
| 203 | if (isa<TemplateTypeParmDecl>(*P)) |
| 204 | continue; |
| 205 | |
| 206 | // Non-type template parameters can be restricted by the value type, e.g. |
| 207 | // template <enum X> class A { ... }; |
| 208 | // We have to be careful here, though, because we can be dealing with |
| 209 | // dependent types. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 210 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 211 | // Handle the non-pack case first. |
| 212 | if (!NTTP->isExpandedParameterPack()) { |
| 213 | if (!NTTP->getType()->isDependentType()) { |
| 214 | LV.merge(getLVForType(NTTP->getType())); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 215 | } |
| 216 | continue; |
| 217 | } |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 218 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 219 | // Look at all the types in an expanded pack. |
| 220 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 221 | QualType type = NTTP->getExpansionType(i); |
| 222 | if (!type->isDependentType()) |
| 223 | LV.merge(getLVForType(type)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 224 | } |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 225 | continue; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 226 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 227 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 228 | // Template template parameters can be restricted by their |
| 229 | // template parameters, recursively. |
| 230 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 231 | |
| 232 | // Handle the non-pack case first. |
| 233 | if (!TTP->isExpandedParameterPack()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 234 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters())); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 235 | continue; |
| 236 | } |
| 237 | |
| 238 | // Look at all expansions in an expanded pack. |
| 239 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 240 | i != n; ++i) { |
| 241 | LV.merge(getLVForTemplateParameterList( |
| 242 | TTP->getExpansionTemplateParameters(i))); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 246 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 249 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 250 | static LinkageInfo getLVForDecl(const NamedDecl *D, |
| 251 | LVComputationKind computation); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 253 | /// \brief Get the most restrictive linkage for the types and |
| 254 | /// declarations in the given template argument list. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 255 | /// |
| 256 | /// Note that we don't take an LVComputationKind because we always |
| 257 | /// want to honor the visibility of template arguments in the same way. |
| 258 | static LinkageInfo |
| 259 | getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) { |
| 260 | LinkageInfo LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 261 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 262 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
| 263 | const TemplateArgument &arg = args[i]; |
| 264 | switch (arg.getKind()) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 265 | case TemplateArgument::Null: |
| 266 | case TemplateArgument::Integral: |
| 267 | case TemplateArgument::Expression: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 268 | continue; |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 270 | case TemplateArgument::Type: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 271 | LV.merge(getLVForType(arg.getAsType())); |
| 272 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 273 | |
| 274 | case TemplateArgument::Declaration: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 275 | if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) { |
| 276 | assert(!usesTypeVisibility(ND)); |
| 277 | LV.merge(getLVForDecl(ND, LVForValue)); |
| 278 | } |
| 279 | continue; |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 280 | |
| 281 | case TemplateArgument::NullPtr: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 282 | LV.merge(getLVForType(arg.getNullPtrType())); |
| 283 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 284 | |
| 285 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 286 | case TemplateArgument::TemplateExpansion: |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 287 | if (TemplateDecl *Template |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 288 | = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
| 289 | LV.merge(getLVForDecl(Template, LVForValue)); |
| 290 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 291 | |
| 292 | case TemplateArgument::Pack: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 293 | LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray())); |
| 294 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 295 | } |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 296 | llvm_unreachable("bad template argument kind"); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 297 | } |
| 298 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 299 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 302 | static LinkageInfo |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 303 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) { |
| 304 | return getLVForTemplateArgumentList(TArgs.asArray()); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 305 | } |
| 306 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 307 | /// Merge in template-related linkage and visibility for the given |
| 308 | /// function template specialization. |
| 309 | /// |
| 310 | /// We don't need a computation kind here because we can assume |
| 311 | /// LVForValue. |
| 312 | static void mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn, |
| 313 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 314 | bool hasExplicitVisibility = fn->hasAttr<VisibilityAttr>(); |
| 315 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
| 316 | |
| 317 | // Include visibility from the template parameters and arguments |
| 318 | // only if this is not an explicit instantiation or specialization |
| 319 | // with direct explicit visibility. (Implicit instantiations won't |
| 320 | // have a direct attribute.) |
| 321 | bool considerVisibility = !hasExplicitVisibility; |
| 322 | |
| 323 | // Merge information from the template parameters. |
| 324 | LinkageInfo tempLV = |
| 325 | getLVForTemplateParameterList(temp->getTemplateParameters()); |
| 326 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 327 | |
| 328 | // Merge information from the template arguments. |
| 329 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| 330 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs); |
| 331 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 332 | } |
| 333 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 334 | /// Should we consider visibility associated with the template |
| 335 | /// arguments and parameters of the given class template specialization? |
| 336 | static bool shouldConsiderTemplateVisibility( |
| 337 | const ClassTemplateSpecializationDecl *spec, |
| 338 | LVComputationKind computation) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 339 | // Include visibility from the template parameters and arguments |
| 340 | // only if this is not an explicit instantiation or specialization |
| 341 | // with direct explicit visibility (and note that implicit |
| 342 | // instantiations won't have a direct attribute). |
| 343 | // |
| 344 | // Furthermore, we want to ignore template parameters and arguments |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 345 | // for an explicit specialization when computing the visibility of a |
| 346 | // member thereof with explicit visibility. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 347 | // |
| 348 | // This is a bit complex; let's unpack it. |
| 349 | // |
| 350 | // An explicit class specialization is an independent, top-level |
| 351 | // declaration. As such, if it or any of its members has an |
| 352 | // explicit visibility attribute, that must directly express the |
| 353 | // user's intent, and we should honor it. The same logic applies to |
| 354 | // an explicit instantiation of a member of such a thing. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 355 | |
| 356 | // Fast path: if this is not an explicit instantiation or |
| 357 | // specialization, we always want to consider template-related |
| 358 | // visibility restrictions. |
| 359 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 360 | return true; |
| 361 | |
| 362 | // This is the 'member thereof' check. |
| 363 | if (spec->isExplicitSpecialization() && |
| 364 | hasExplicitVisibilityAlready(computation)) |
| 365 | return false; |
| 366 | |
| 367 | // Otherwise, look to see if we have an attribute. |
| 368 | switch (computation) { |
| 369 | case LVForType: |
| 370 | case LVForExplicitType: |
| 371 | if (spec->hasAttr<TypeVisibilityAttr>()) |
| 372 | return false; |
| 373 | // fallthrough |
| 374 | case LVForValue: |
| 375 | case LVForExplicitValue: |
| 376 | if (spec->hasAttr<VisibilityAttr>()) |
| 377 | return false; |
| 378 | return true; |
| 379 | } |
| 380 | llvm_unreachable("bad visibility computation kind"); |
| 381 | } |
| 382 | |
| 383 | /// Merge in template-related linkage and visibility for the given |
| 384 | /// class template specialization. |
| 385 | static void mergeTemplateLV(LinkageInfo &LV, |
| 386 | const ClassTemplateSpecializationDecl *spec, |
| 387 | LVComputationKind computation) { |
| 388 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 389 | |
| 390 | // Merge information from the template parameters, but ignore |
| 391 | // visibility if we're only considering template arguments. |
| 392 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 393 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 394 | LinkageInfo tempLV = |
| 395 | getLVForTemplateParameterList(temp->getTemplateParameters()); |
| 396 | LV.mergeMaybeWithVisibility(tempLV, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 397 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 398 | |
| 399 | // Merge information from the template arguments. We ignore |
| 400 | // template-argument visibility if we've got an explicit |
| 401 | // instantiation with a visibility attribute. |
| 402 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 403 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs); |
| 404 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 407 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 408 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
Rafael Espindola | 0bab9da | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 409 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 410 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 411 | return false; |
| 412 | |
| 413 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 414 | if (!FD) |
| 415 | return false; |
| 416 | |
| 417 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 418 | if (FunctionTemplateSpecializationInfo *spec |
| 419 | = FD->getTemplateSpecializationInfo()) { |
| 420 | TSK = spec->getTemplateSpecializationKind(); |
| 421 | } else if (MemberSpecializationInfo *MSI = |
| 422 | FD->getMemberSpecializationInfo()) { |
| 423 | TSK = MSI->getTemplateSpecializationKind(); |
| 424 | } |
| 425 | |
| 426 | const FunctionDecl *Def = 0; |
| 427 | // InlineVisibilityHidden only applies to definitions, and |
| 428 | // isInlined() only gives meaningful answers on definitions |
| 429 | // anyway. |
| 430 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 431 | TSK != TSK_ExplicitInstantiationDefinition && |
Rafael Espindola | 0142f0c | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 432 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 435 | template <typename T> static bool isInExternCContext(T *D) { |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 436 | const T *First = D->getFirstDeclaration(); |
| 437 | return First->getDeclContext()->isExternCContext(); |
| 438 | } |
| 439 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 440 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 441 | LVComputationKind computation) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 442 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 443 | "Not a name having namespace scope"); |
| 444 | ASTContext &Context = D->getASTContext(); |
| 445 | |
| 446 | // C++ [basic.link]p3: |
| 447 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 448 | // is the name of |
| 449 | // - an object, reference, function or function template that is |
| 450 | // explicitly declared static; or, |
| 451 | // (This bullet corresponds to C99 6.2.2p3.) |
| 452 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 453 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 454 | if (Var->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 455 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 456 | |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 457 | // - a non-volatile object or reference that is explicitly declared const |
| 458 | // or constexpr and neither explicitly declared extern nor previously |
| 459 | // declared to have external linkage; or (there is no equivalent in C99) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 460 | if (Context.getLangOpts().CPlusPlus && |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 461 | Var->getType().isConstQualified() && |
| 462 | !Var->getType().isVolatileQualified() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 463 | Var->getStorageClass() != SC_Extern && |
| 464 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 465 | bool FoundExtern = false; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 466 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 467 | PrevVar && !FoundExtern; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 468 | PrevVar = PrevVar->getPreviousDecl()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 469 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 470 | FoundExtern = true; |
| 471 | |
| 472 | if (!FoundExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 473 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 474 | } |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 475 | if (Var->getStorageClass() == SC_None) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 476 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| 477 | for (; PrevVar; PrevVar = PrevVar->getPreviousDecl()) |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 478 | if (PrevVar->getStorageClass() == SC_PrivateExtern) |
| 479 | break; |
Eli Friedman | 8c7a185 | 2012-10-26 23:05:34 +0000 | [diff] [blame] | 480 | if (PrevVar) |
| 481 | return PrevVar->getLinkageAndVisibility(); |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 482 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 483 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 484 | // C++ [temp]p4: |
| 485 | // A non-member function template can have internal linkage; any |
| 486 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 487 | const FunctionDecl *Function = 0; |
| 488 | if (const FunctionTemplateDecl *FunTmpl |
| 489 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 490 | Function = FunTmpl->getTemplatedDecl(); |
| 491 | else |
| 492 | Function = cast<FunctionDecl>(D); |
| 493 | |
| 494 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 495 | if (Function->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 496 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 497 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 498 | // - a data member of an anonymous union. |
| 499 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 500 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 503 | if (D->isInAnonymousNamespace()) { |
| 504 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 505 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 506 | if ((!Var || !isInExternCContext(Var)) && |
| 507 | (!Func || !isInExternCContext(Func))) |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 508 | return LinkageInfo::uniqueExternal(); |
| 509 | } |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 510 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 511 | // Set up the defaults. |
| 512 | |
| 513 | // C99 6.2.2p5: |
| 514 | // If the declaration of an identifier for an object has file |
| 515 | // scope and no storage-class specifier, its linkage is |
| 516 | // external. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 517 | LinkageInfo LV; |
| 518 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 519 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 520 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 521 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 522 | } else { |
| 523 | // If we're declared in a namespace with a visibility attribute, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 524 | // use that namespace's visibility, and it still counts as explicit. |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 525 | for (const DeclContext *DC = D->getDeclContext(); |
| 526 | !isa<TranslationUnitDecl>(DC); |
| 527 | DC = DC->getParent()) { |
| 528 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); |
| 529 | if (!ND) continue; |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 530 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 531 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 536 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 537 | // Add in global settings if the above didn't give us direct visibility. |
| 538 | if (!LV.visibilityExplicit()) { |
John McCall | a880b19 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 539 | // Use global type/value visibility as appropriate. |
| 540 | Visibility globalVisibility; |
| 541 | if (computation == LVForValue) { |
| 542 | globalVisibility = Context.getLangOpts().getValueVisibilityMode(); |
| 543 | } else { |
| 544 | assert(computation == LVForType); |
| 545 | globalVisibility = Context.getLangOpts().getTypeVisibilityMode(); |
| 546 | } |
| 547 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 548 | |
| 549 | // If we're paying attention to global visibility, apply |
| 550 | // -finline-visibility-hidden if this is an inline method. |
| 551 | if (useInlineVisibilityHidden(D)) |
| 552 | LV.mergeVisibility(HiddenVisibility, true); |
| 553 | } |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 554 | } |
Rafael Espindola | ff25798 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 556 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 558 | // A name having namespace scope has external linkage if it is the |
| 559 | // name of |
| 560 | // |
| 561 | // - an object or reference, unless it has internal linkage; or |
| 562 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 563 | // GCC applies the following optimization to variables and static |
| 564 | // data members, but not to functions: |
| 565 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 566 | // Modify the variable's LV by the LV of its type unless this is |
| 567 | // C or extern "C". This follows from [basic.link]p9: |
| 568 | // A type without linkage shall not be used as the type of a |
| 569 | // variable or function with external linkage unless |
| 570 | // - the entity has C language linkage, or |
| 571 | // - the entity is declared within an unnamed namespace, or |
| 572 | // - the entity is not used or is defined in the same |
| 573 | // translation unit. |
| 574 | // and [basic.link]p10: |
| 575 | // ...the types specified by all declarations referring to a |
| 576 | // given variable or function shall be identical... |
| 577 | // C does not have an equivalent rule. |
| 578 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 579 | // Ignore this if we've got an explicit attribute; the user |
| 580 | // probably knows what they're doing. |
| 581 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 582 | // Note that we don't want to make the variable non-external |
| 583 | // because of this, but unique-external linkage suits us. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 584 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 585 | !Var->getDeclContext()->isExternCContext()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 586 | LinkageInfo TypeLV = getLVForType(Var->getType()); |
| 587 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 588 | return LinkageInfo::uniqueExternal(); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 589 | if (!LV.visibilityExplicit()) |
| 590 | LV.mergeVisibility(TypeLV); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 591 | } |
| 592 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 593 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 594 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 595 | |
Rafael Espindola | 538fb98 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 596 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 597 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 598 | // to do it here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 600 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 601 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 602 | // In theory, we can modify the function's LV by the LV of its |
| 603 | // type unless it has C linkage (see comment above about variables |
| 604 | // for justification). In practice, GCC doesn't do this, so it's |
| 605 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 606 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 607 | if (Function->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 608 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 609 | |
Rafael Espindola | 5175861 | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 610 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 611 | // merging storage classes and visibility attributes, so we don't have to |
| 612 | // look at previous decls in here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 613 | |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 614 | // In C++, then if the type of the function uses a type with |
| 615 | // unique-external linkage, it's not legally usable from outside |
| 616 | // this translation unit. However, we should use the C linkage |
| 617 | // rules instead for extern "C" declarations. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 618 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 619 | !Function->getDeclContext()->isExternCContext() && |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 620 | Function->getType()->getLinkage() == UniqueExternalLinkage) |
| 621 | return LinkageInfo::uniqueExternal(); |
| 622 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 623 | // Consider LV from the template and the template arguments unless |
| 624 | // this is an explicit specialization with a visibility attribute. |
| 625 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 626 | = Function->getTemplateSpecializationInfo()) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 627 | mergeTemplateLV(LV, Function, specInfo); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 630 | // - a named class (Clause 9), or an unnamed class defined in a |
| 631 | // typedef declaration in which the class has the typedef name |
| 632 | // for linkage purposes (7.1.3); or |
| 633 | // - a named enumeration (7.2), or an unnamed enumeration |
| 634 | // defined in a typedef declaration in which the enumeration |
| 635 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 636 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 637 | // Unnamed tags have no linkage. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 638 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 639 | return LinkageInfo::none(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 640 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 641 | // If this is a class template specialization, consider the |
| 642 | // linkage of the template and template arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 643 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 644 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 645 | mergeTemplateLV(LV, spec, computation); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 646 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 647 | |
| 648 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 649 | } else if (isa<EnumConstantDecl>(D)) { |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 650 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 651 | computation); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 652 | if (!isExternalLinkage(EnumLV.linkage())) |
| 653 | return LinkageInfo::none(); |
| 654 | LV.merge(EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 655 | |
| 656 | // - a template, unless it is a function template that has |
| 657 | // internal linkage (Clause 14); |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 658 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 659 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 660 | LinkageInfo tempLV = |
| 661 | getLVForTemplateParameterList(temp->getTemplateParameters()); |
| 662 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 663 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 664 | // - a namespace (7.3), unless it is declared within an unnamed |
| 665 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 666 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 667 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 668 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 669 | // By extension, we assign external linkage to Objective-C |
| 670 | // interfaces. |
| 671 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 672 | // fallout |
| 673 | |
| 674 | // Everything not covered here has no linkage. |
| 675 | } else { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 676 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | // If we ended up with non-external linkage, visibility should |
| 680 | // always be default. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 681 | if (LV.linkage() != ExternalLinkage) |
| 682 | return LinkageInfo(LV.linkage(), DefaultVisibility, false); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 683 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 684 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 687 | static LinkageInfo getLVForClassMember(const NamedDecl *D, |
| 688 | LVComputationKind computation) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 689 | // Only certain class members have linkage. Note that fields don't |
| 690 | // really have linkage, but it's convenient to say they do for the |
| 691 | // purposes of calculating linkage of pointer-to-data-member |
| 692 | // template arguments. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 693 | if (!(isa<CXXMethodDecl>(D) || |
| 694 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 695 | isa<FieldDecl>(D) || |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 696 | isa<TagDecl>(D))) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 697 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 698 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 699 | LinkageInfo LV; |
| 700 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 701 | // If we have an explicit visibility attribute, merge that in. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 702 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 703 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 704 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 705 | // If we're paying attention to global visibility, apply |
| 706 | // -finline-visibility-hidden if this is an inline method. |
| 707 | // |
| 708 | // Note that we do this before merging information about |
| 709 | // the class visibility. |
| 710 | if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D)) |
| 711 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 712 | } |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 713 | |
| 714 | // If this class member has an explicit visibility attribute, the only |
| 715 | // thing that can change its visibility is the template arguments, so |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 716 | // only look for them when processing the class. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 717 | LVComputationKind classComputation = computation; |
| 718 | if (LV.visibilityExplicit()) |
| 719 | classComputation = withExplicitVisibilityAlready(computation); |
Rafael Espindola | 0f90590 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 720 | |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 721 | // If this member has an visibility attribute, ClassF will exclude |
| 722 | // attributes on the class or command line options, keeping only information |
| 723 | // about the template instantiation. If the member has no visibility |
| 724 | // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin |
| 725 | // produces the desired result. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 726 | LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), |
| 727 | classComputation)); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 728 | if (!isExternalLinkage(LV.linkage())) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 729 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 730 | |
| 731 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 732 | if (LV.linkage() == UniqueExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 733 | return LinkageInfo::uniqueExternal(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 734 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 735 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 736 | // If the type of the function uses a type with unique-external |
| 737 | // linkage, it's not legally usable from outside this translation unit. |
| 738 | if (MD->getType()->getLinkage() == UniqueExternalLinkage) |
| 739 | return LinkageInfo::uniqueExternal(); |
| 740 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 741 | // If this is a method template specialization, use the linkage for |
| 742 | // the template parameters and arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 743 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 744 | = MD->getTemplateSpecializationInfo()) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 745 | mergeTemplateLV(LV, MD, spec); |
John McCall | 66cbcf3 | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 746 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 747 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 748 | // Note that in contrast to basically every other situation, we |
| 749 | // *do* apply -fvisibility to method declarations. |
| 750 | |
| 751 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 752 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 753 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 754 | mergeTemplateLV(LV, spec, computation); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 755 | } |
| 756 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 757 | // Static data members. |
| 758 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 759 | // Modify the variable's linkage by its type, but ignore the |
| 760 | // type's visibility unless it's a definition. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 761 | LinkageInfo typeLV = getLVForType(VD->getType()); |
| 762 | if (typeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 763 | LV.mergeLinkage(UniqueExternalLinkage); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 764 | if (!LV.visibilityExplicit()) |
| 765 | LV.mergeVisibility(typeLV); |
| 766 | |
| 767 | // Template members. |
| 768 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
| 769 | bool considerVisibility = |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 770 | (!LV.visibilityExplicit() && |
| 771 | !hasExplicitVisibilityAlready(computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 772 | LinkageInfo tempLV = |
| 773 | getLVForTemplateParameterList(temp->getTemplateParameters()); |
| 774 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 775 | } |
| 776 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 777 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 778 | } |
| 779 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 780 | static void clearLinkageForClass(const CXXRecordDecl *record) { |
| 781 | for (CXXRecordDecl::decl_iterator |
| 782 | i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { |
| 783 | Decl *child = *i; |
| 784 | if (isa<NamedDecl>(child)) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 785 | cast<NamedDecl>(child)->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 789 | void NamedDecl::anchor() { } |
| 790 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 791 | void NamedDecl::ClearLinkageCache() { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 792 | // Note that we can't skip clearing the linkage of children just |
| 793 | // because the parent doesn't have cached linkage: we don't cache |
| 794 | // when computing linkage for parent contexts. |
| 795 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 796 | HasCachedLinkage = 0; |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 797 | |
| 798 | // If we're changing the linkage of a class, we need to reset the |
| 799 | // linkage of child declarations, too. |
| 800 | if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) |
| 801 | clearLinkageForClass(record); |
| 802 | |
Dmitri Gribenko | f8c1214 | 2013-02-03 16:10:26 +0000 | [diff] [blame] | 803 | if (ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 804 | // Clear linkage for the template pattern. |
| 805 | CXXRecordDecl *record = temp->getTemplatedDecl(); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 806 | record->HasCachedLinkage = 0; |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 807 | clearLinkageForClass(record); |
| 808 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 809 | // We need to clear linkage for specializations, too. |
| 810 | for (ClassTemplateDecl::spec_iterator |
| 811 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 812 | i->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 813 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 814 | |
| 815 | // Clear cached linkage for function template decls, too. |
Dmitri Gribenko | f8c1214 | 2013-02-03 16:10:26 +0000 | [diff] [blame] | 816 | if (FunctionTemplateDecl *temp = dyn_cast<FunctionTemplateDecl>(this)) { |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 817 | temp->getTemplatedDecl()->ClearLinkageCache(); |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 818 | for (FunctionTemplateDecl::spec_iterator |
| 819 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 820 | i->ClearLinkageCache(); |
John McCall | 7895194 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 821 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 822 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 825 | Linkage NamedDecl::getLinkage() const { |
Richard Smith | ad0e27b | 2013-02-12 05:48:23 +0000 | [diff] [blame] | 826 | if (HasCachedLinkage) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 827 | return Linkage(CachedLinkage); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 828 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 829 | // We don't care about visibility here, so ask for the cheapest |
| 830 | // possible visibility analysis. |
| 831 | CachedLinkage = getLVForDecl(this, LVForExplicitValue).linkage(); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 832 | HasCachedLinkage = 1; |
| 833 | |
| 834 | #ifndef NDEBUG |
| 835 | verifyLinkage(); |
| 836 | #endif |
| 837 | |
| 838 | return Linkage(CachedLinkage); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 839 | } |
| 840 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 841 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 842 | LVComputationKind computation = |
| 843 | (usesTypeVisibility(this) ? LVForType : LVForValue); |
| 844 | LinkageInfo LI = getLVForDecl(this, computation); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 845 | if (HasCachedLinkage) { |
| 846 | assert(Linkage(CachedLinkage) == LI.linkage()); |
| 847 | return LI; |
Rafael Espindola | 140aadf | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 848 | } |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 849 | HasCachedLinkage = 1; |
| 850 | CachedLinkage = LI.linkage(); |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 851 | |
| 852 | #ifndef NDEBUG |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 853 | verifyLinkage(); |
| 854 | #endif |
| 855 | |
| 856 | return LI; |
| 857 | } |
| 858 | |
| 859 | void NamedDecl::verifyLinkage() const { |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 860 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 861 | // static can follow an extern, so we can have two decls with different |
| 862 | // linkages. |
| 863 | const LangOptions &Opts = getASTContext().getLangOpts(); |
| 864 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 865 | return; |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 866 | |
| 867 | // We have just computed the linkage for this decl. By induction we know |
| 868 | // that all other computed linkages match, check that the one we just computed |
| 869 | // also does. |
| 870 | NamedDecl *D = NULL; |
| 871 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 872 | NamedDecl *T = cast<NamedDecl>(*I); |
| 873 | if (T == this) |
| 874 | continue; |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 875 | if (T->HasCachedLinkage != 0) { |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 876 | D = T; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | assert(!D || D->CachedLinkage == CachedLinkage); |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 881 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 882 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 883 | Optional<Visibility> |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 884 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 885 | // Use the most recent declaration of a variable. |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 886 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 887 | if (Optional<Visibility> V = getVisibilityOf(Var, kind)) |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 888 | return V; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 889 | |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 890 | if (Var->isStaticDataMember()) { |
| 891 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 892 | if (InstantiatedFrom) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 893 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 894 | } |
| 895 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 896 | return None; |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 897 | } |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 898 | // Use the most recent declaration of a function, and also handle |
| 899 | // function template specializations. |
| 900 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 901 | if (Optional<Visibility> V = getVisibilityOf(fn, kind)) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 902 | return V; |
| 903 | |
| 904 | // If the function is a specialization of a template with an |
| 905 | // explicit visibility attribute, use that. |
| 906 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 907 | = fn->getTemplateSpecializationInfo()) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 908 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
| 909 | kind); |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 910 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 911 | // If the function is a member of a specialization of a class template |
| 912 | // and the corresponding decl has explicit visibility, use that. |
| 913 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 914 | if (InstantiatedFrom) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 915 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 916 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 917 | return None; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | // Otherwise, just check the declaration itself first. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 921 | if (Optional<Visibility> V = getVisibilityOf(this, kind)) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 922 | return V; |
| 923 | |
Rafael Espindola | 9849901 | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 924 | // The visibility of a template is stored in the templated decl. |
| 925 | if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this)) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 926 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
Rafael Espindola | 9849901 | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 927 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 928 | // If there wasn't explicit visibility there, and this is a |
| 929 | // specialization of a class template, check for visibility |
| 930 | // on the pattern. |
| 931 | if (const ClassTemplateSpecializationDecl *spec |
Rafael Espindola | d3d02dd | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 932 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 933 | return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(), |
| 934 | kind); |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 935 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 936 | // If this is a member class of a specialization of a class template |
| 937 | // and the corresponding decl has explicit visibility, use that. |
| 938 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) { |
| 939 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 940 | if (InstantiatedFrom) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 941 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 942 | } |
| 943 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 944 | return None; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 945 | } |
| 946 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 947 | static LinkageInfo getLVForLocalDecl(const NamedDecl *D, |
| 948 | LVComputationKind computation) { |
| 949 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 950 | if (Function->isInAnonymousNamespace() && |
| 951 | !Function->getDeclContext()->isExternCContext()) |
| 952 | return LinkageInfo::uniqueExternal(); |
| 953 | |
| 954 | // This is a "void f();" which got merged with a file static. |
| 955 | if (Function->getStorageClass() == SC_Static) |
| 956 | return LinkageInfo::internal(); |
| 957 | |
| 958 | LinkageInfo LV; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 959 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 960 | if (Optional<Visibility> Vis = |
| 961 | getExplicitVisibility(Function, computation)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 962 | LV.mergeVisibility(*Vis, true); |
| 963 | } |
| 964 | |
| 965 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 966 | // merging storage classes and visibility attributes, so we don't have to |
| 967 | // look at previous decls in here. |
| 968 | |
| 969 | return LV; |
| 970 | } |
| 971 | |
| 972 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 973 | if (Var->getStorageClassAsWritten() == SC_Extern || |
| 974 | Var->getStorageClassAsWritten() == SC_PrivateExtern) { |
| 975 | if (Var->isInAnonymousNamespace() && |
| 976 | !Var->getDeclContext()->isExternCContext()) |
| 977 | return LinkageInfo::uniqueExternal(); |
| 978 | |
| 979 | // This is an "extern int foo;" which got merged with a file static. |
| 980 | if (Var->getStorageClass() == SC_Static) |
| 981 | return LinkageInfo::internal(); |
| 982 | |
| 983 | LinkageInfo LV; |
| 984 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 985 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 986 | else if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 987 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 988 | LV.mergeVisibility(*Vis, true); |
| 989 | } |
| 990 | |
| 991 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 992 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't |
| 993 | // have to do it here. |
| 994 | return LV; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | return LinkageInfo::none(); |
| 999 | } |
| 1000 | |
| 1001 | static LinkageInfo getLVForDecl(const NamedDecl *D, |
| 1002 | LVComputationKind computation) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1003 | // Objective-C: treat all Objective-C declarations as having external |
| 1004 | // linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1005 | switch (D->getKind()) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1006 | default: |
| 1007 | break; |
Argyrios Kyrtzidis | f8d34ed | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1008 | case Decl::ParmVar: |
| 1009 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1010 | case Decl::TemplateTemplateParm: // count these as external |
| 1011 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1012 | case Decl::ObjCAtDefsField: |
| 1013 | case Decl::ObjCCategory: |
| 1014 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1015 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1016 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1017 | case Decl::ObjCMethod: |
| 1018 | case Decl::ObjCProperty: |
| 1019 | case Decl::ObjCPropertyImpl: |
| 1020 | case Decl::ObjCProtocol: |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1021 | return LinkageInfo::external(); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1022 | |
| 1023 | case Decl::CXXRecord: { |
| 1024 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); |
| 1025 | if (Record->isLambda()) { |
| 1026 | if (!Record->getLambdaManglingNumber()) { |
| 1027 | // This lambda has no mangling number, so it's internal. |
| 1028 | return LinkageInfo::internal(); |
| 1029 | } |
| 1030 | |
| 1031 | // This lambda has its linkage/visibility determined by its owner. |
| 1032 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 1033 | if (Decl *ContextDecl = Record->getLambdaContextDecl()) { |
| 1034 | if (isa<ParmVarDecl>(ContextDecl)) |
| 1035 | DC = ContextDecl->getDeclContext()->getRedeclContext(); |
| 1036 | else |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1037 | return getLVForDecl(cast<NamedDecl>(ContextDecl), computation); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1041 | return getLVForDecl(ND, computation); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1042 | |
| 1043 | return LinkageInfo::external(); |
| 1044 | } |
| 1045 | |
| 1046 | break; |
| 1047 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1050 | // Handle linkage for namespace-scope names. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1051 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1052 | return getLVForNamespaceScopeDecl(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1053 | |
| 1054 | // C++ [basic.link]p5: |
| 1055 | // In addition, a member function, static data member, a named |
| 1056 | // class or enumeration of class scope, or an unnamed class or |
| 1057 | // enumeration defined in a class-scope typedef declaration such |
| 1058 | // that the class or enumeration has the typedef name for linkage |
| 1059 | // purposes (7.1.3), has external linkage if the name of the class |
| 1060 | // has external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1061 | if (D->getDeclContext()->isRecord()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1062 | return getLVForClassMember(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1063 | |
| 1064 | // C++ [basic.link]p6: |
| 1065 | // The name of a function declared in block scope and the name of |
| 1066 | // an object declared by a block scope extern declaration have |
| 1067 | // linkage. If there is a visible declaration of an entity with |
| 1068 | // linkage having the same name and type, ignoring entities |
| 1069 | // declared outside the innermost enclosing namespace scope, the |
| 1070 | // block scope declaration declares that same entity and receives |
| 1071 | // the linkage of the previous declaration. If there is more than |
| 1072 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1073 | // if no matching entity is found, the block scope entity receives |
| 1074 | // external linkage. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1075 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1076 | return getLVForLocalDecl(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1077 | |
| 1078 | // C++ [basic.link]p6: |
| 1079 | // Names not covered by these rules have no linkage. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1080 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1081 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1083 | std::string NamedDecl::getQualifiedNameAsString() const { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1084 | return getQualifiedNameAsString(getASTContext().getPrintingPolicy()); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1088 | const DeclContext *Ctx = getDeclContext(); |
| 1089 | |
| 1090 | if (Ctx->isFunctionOrMethod()) |
| 1091 | return getNameAsString(); |
| 1092 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1093 | typedef SmallVector<const DeclContext *, 8> ContextsTy; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1094 | ContextsTy Contexts; |
| 1095 | |
| 1096 | // Collect contexts. |
| 1097 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 1098 | Contexts.push_back(Ctx); |
| 1099 | Ctx = Ctx->getParent(); |
| 1100 | }; |
| 1101 | |
| 1102 | std::string QualName; |
| 1103 | llvm::raw_string_ostream OS(QualName); |
| 1104 | |
| 1105 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 1106 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1108 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1109 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1110 | std::string TemplateArgsStr |
| 1111 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1112 | TemplateArgs.data(), |
| 1113 | TemplateArgs.size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1114 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1115 | OS << Spec->getName() << TemplateArgsStr; |
| 1116 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1117 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1118 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1119 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1120 | OS << *ND; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1121 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 1122 | if (!RD->getIdentifier()) |
| 1123 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 1124 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1125 | OS << *RD; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1126 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1127 | const FunctionProtoType *FT = 0; |
| 1128 | if (FD->hasWrittenPrototype()) |
Eli Friedman | 482466b | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1129 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1130 | |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1131 | OS << *FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1132 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1133 | unsigned NumParams = FD->getNumParams(); |
| 1134 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1135 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1136 | OS << ", "; |
Argyrios Kyrtzidis | 7ad5c99 | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1137 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | if (FT->isVariadic()) { |
| 1141 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1142 | OS << ", "; |
| 1143 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1146 | OS << ')'; |
| 1147 | } else { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1148 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1149 | } |
| 1150 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1153 | if (getDeclName()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1154 | OS << *this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1155 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1156 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1157 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1158 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1161 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1162 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1163 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1164 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 1165 | // We want to keep it, unless it nominates same namespace. |
| 1166 | if (getKind() == Decl::UsingDirective) { |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1167 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() |
| 1168 | ->getOriginalNamespace() == |
| 1169 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 1170 | ->getOriginalNamespace(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1173 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 1174 | // For function declarations, we keep track of redeclarations. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1175 | return FD->getPreviousDecl() == OldD; |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1176 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1177 | // For function templates, the underlying function declarations are linked. |
| 1178 | if (const FunctionTemplateDecl *FunctionTemplate |
| 1179 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 1180 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 1181 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 1182 | return FunctionTemplate->getTemplatedDecl() |
| 1183 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 1185 | // For method declarations, we keep track of redeclarations. |
| 1186 | if (isa<ObjCMethodDecl>(this)) |
| 1187 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1189 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 1190 | return true; |
| 1191 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1192 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 1193 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 1194 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 1195 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1196 | if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { |
| 1197 | ASTContext &Context = getASTContext(); |
| 1198 | return Context.getCanonicalNestedNameSpecifier( |
| 1199 | cast<UsingDecl>(this)->getQualifier()) == |
| 1200 | Context.getCanonicalNestedNameSpecifier( |
| 1201 | cast<UsingDecl>(OldD)->getQualifier()); |
| 1202 | } |
Argyrios Kyrtzidis | c80117e | 2010-11-04 08:48:52 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | 7a53740 | 2012-01-03 23:26:26 +0000 | [diff] [blame] | 1204 | // A typedef of an Objective-C class type can replace an Objective-C class |
| 1205 | // declaration or definition, and vice versa. |
| 1206 | if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) || |
| 1207 | (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD))) |
| 1208 | return true; |
| 1209 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1210 | // For non-function declarations, if the declarations are of the |
| 1211 | // same kind then this must be a redeclaration, or semantic analysis |
| 1212 | // would not have given us the new declaration. |
| 1213 | return this->getKind() == OldD->getKind(); |
| 1214 | } |
| 1215 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1216 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1217 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1218 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1219 | |
Daniel Dunbar | 6daffa5 | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1220 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1221 | NamedDecl *ND = this; |
Benjamin Kramer | 56757e9 | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1222 | while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
| 1223 | ND = UD->getTargetDecl(); |
| 1224 | |
| 1225 | if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 1226 | return AD->getClassInterface(); |
| 1227 | |
| 1228 | return ND; |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1231 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 5bc37f6 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1232 | if (!isCXXClassMember()) |
| 1233 | return false; |
| 1234 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1235 | const NamedDecl *D = this; |
| 1236 | if (isa<UsingShadowDecl>(D)) |
| 1237 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1238 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1239 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1240 | return true; |
| 1241 | if (isa<CXXMethodDecl>(D)) |
| 1242 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 1243 | if (isa<FunctionTemplateDecl>(D)) |
| 1244 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 1245 | ->getTemplatedDecl())->isInstance(); |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1249 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1250 | // DeclaratorDecl Implementation |
| 1251 | //===----------------------------------------------------------------------===// |
| 1252 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1253 | template <typename DeclT> |
| 1254 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1255 | if (decl->getNumTemplateParameterLists() > 0) |
| 1256 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1257 | else |
| 1258 | return decl->getInnerLocStart(); |
| 1259 | } |
| 1260 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1261 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1262 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1263 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1264 | return SourceLocation(); |
| 1265 | } |
| 1266 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1267 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1268 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1269 | // Make sure the extended decl info is allocated. |
| 1270 | if (!hasExtInfo()) { |
| 1271 | // Save (non-extended) type source info pointer. |
| 1272 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1273 | // Allocate external info struct. |
| 1274 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1275 | // Restore savedTInfo into (extended) decl info. |
| 1276 | getExtInfo()->TInfo = savedTInfo; |
| 1277 | } |
| 1278 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1279 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1280 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1281 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1282 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1283 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1284 | // Save type source info pointer. |
| 1285 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1286 | // Deallocate the extended decl info. |
| 1287 | getASTContext().Deallocate(getExtInfo()); |
| 1288 | // Restore savedTInfo into (non-extended) decl info. |
| 1289 | DeclInfo = savedTInfo; |
| 1290 | } |
| 1291 | else |
| 1292 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1297 | void |
| 1298 | DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 1299 | unsigned NumTPLists, |
| 1300 | TemplateParameterList **TPLists) { |
| 1301 | assert(NumTPLists > 0); |
| 1302 | // Make sure the extended decl info is allocated. |
| 1303 | if (!hasExtInfo()) { |
| 1304 | // Save (non-extended) type source info pointer. |
| 1305 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1306 | // Allocate external info struct. |
| 1307 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1308 | // Restore savedTInfo into (extended) decl info. |
| 1309 | getExtInfo()->TInfo = savedTInfo; |
| 1310 | } |
| 1311 | // Set the template parameter lists info. |
| 1312 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 1313 | } |
| 1314 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1315 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1316 | return getTemplateOrInnerLocStart(this); |
| 1317 | } |
| 1318 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1319 | namespace { |
| 1320 | |
| 1321 | // Helper function: returns true if QT is or contains a type |
| 1322 | // having a postfix component. |
| 1323 | bool typeIsPostfix(clang::QualType QT) { |
| 1324 | while (true) { |
| 1325 | const Type* T = QT.getTypePtr(); |
| 1326 | switch (T->getTypeClass()) { |
| 1327 | default: |
| 1328 | return false; |
| 1329 | case Type::Pointer: |
| 1330 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1331 | break; |
| 1332 | case Type::BlockPointer: |
| 1333 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1334 | break; |
| 1335 | case Type::MemberPointer: |
| 1336 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1337 | break; |
| 1338 | case Type::LValueReference: |
| 1339 | case Type::RValueReference: |
| 1340 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1341 | break; |
| 1342 | case Type::PackExpansion: |
| 1343 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1344 | break; |
| 1345 | case Type::Paren: |
| 1346 | case Type::ConstantArray: |
| 1347 | case Type::DependentSizedArray: |
| 1348 | case Type::IncompleteArray: |
| 1349 | case Type::VariableArray: |
| 1350 | case Type::FunctionProto: |
| 1351 | case Type::FunctionNoProto: |
| 1352 | return true; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | } // namespace |
| 1358 | |
| 1359 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1360 | SourceLocation RangeEnd = getLocation(); |
| 1361 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 1362 | if (typeIsPostfix(TInfo->getType())) |
| 1363 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1364 | } |
| 1365 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1366 | } |
| 1367 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1368 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1369 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 1370 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1371 | TemplateParameterList **TPLists) { |
| 1372 | assert((NumTPLists == 0 || TPLists != 0) && |
| 1373 | "Empty array of template parameters with positive size!"); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1374 | |
| 1375 | // Free previous template parameters (if any). |
| 1376 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1377 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1378 | TemplParamLists = 0; |
| 1379 | NumTemplParamLists = 0; |
| 1380 | } |
| 1381 | // Set info on matched template parameter lists (if any). |
| 1382 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1383 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1384 | NumTemplParamLists = NumTPLists; |
| 1385 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 1386 | TemplParamLists[i] = TPLists[i]; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1390 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1391 | // VarDecl Implementation |
| 1392 | //===----------------------------------------------------------------------===// |
| 1393 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1394 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1395 | switch (SC) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1396 | case SC_None: break; |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1397 | case SC_Auto: return "auto"; |
| 1398 | case SC_Extern: return "extern"; |
| 1399 | case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; |
| 1400 | case SC_PrivateExtern: return "__private_extern__"; |
| 1401 | case SC_Register: return "register"; |
| 1402 | case SC_Static: return "static"; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1405 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1408 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1409 | SourceLocation StartL, SourceLocation IdL, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1410 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1411 | StorageClass S, StorageClass SCAsWritten) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1412 | return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1415 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1416 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl)); |
| 1417 | return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0, |
| 1418 | QualType(), 0, SC_None, SC_None); |
| 1419 | } |
| 1420 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1421 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1422 | assert(isLegalForVariable(SC)); |
| 1423 | if (getStorageClass() != SC) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1424 | ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1425 | |
John McCall | f1e4fbf | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1426 | VarDeclBits.SClass = SC; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1429 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1430 | if (const Expr *Init = getInit()) { |
| 1431 | SourceLocation InitEnd = Init->getLocEnd(); |
Nico Weber | 3a344f9 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1432 | // If Init is implicit, ignore its source range and fallback on |
| 1433 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1434 | if (InitEnd.isValid() && InitEnd != getLocation()) |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1435 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1436 | } |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1437 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Rafael Espindola | 7ac928b | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1440 | template<typename T> |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1441 | static LanguageLinkage getLanguageLinkageTemplate(const T &D) { |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1442 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 1443 | // and variable names with external linkage have a language linkage. |
| 1444 | if (!isExternalLinkage(D.getLinkage())) |
| 1445 | return NoLanguageLinkage; |
| 1446 | |
| 1447 | // Language linkage is a C++ concept, but saying that everything else in C has |
Rafael Espindola | 2b721f5 | 2013-01-04 20:41:40 +0000 | [diff] [blame] | 1448 | // C language linkage fits the implementation nicely. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1449 | ASTContext &Context = D.getASTContext(); |
| 1450 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1451 | return CLanguageLinkage; |
| 1452 | |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1453 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 1454 | // language linkage of the names of class members and the function type of |
| 1455 | // class member functions. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1456 | const DeclContext *DC = D.getDeclContext(); |
| 1457 | if (DC->isRecord()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1458 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1459 | |
| 1460 | // If the first decl is in an extern "C" context, any other redeclaration |
| 1461 | // will have C language linkage. If the first one is not in an extern "C" |
| 1462 | // context, we would have reported an error for any other decl being in one. |
Rafael Espindola | 7ac928b | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1463 | const T *First = D.getFirstDeclaration(); |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1464 | if (First->getDeclContext()->isExternCContext()) |
| 1465 | return CLanguageLinkage; |
| 1466 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1469 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
| 1470 | return getLanguageLinkageTemplate(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1473 | VarDecl *VarDecl::getCanonicalDecl() { |
| 1474 | return getFirstDeclaration(); |
| 1475 | } |
| 1476 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1477 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition( |
| 1478 | ASTContext &C) const |
| 1479 | { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1480 | // C++ [basic.def]p2: |
| 1481 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1482 | // specifier or a linkage-specification and neither an initializer [...], |
| 1483 | // it declares a static data member in a class declaration [...]. |
| 1484 | // C++ [temp.expl.spec]p15: |
| 1485 | // An explicit specialization of a static data member of a template is a |
| 1486 | // definition if the declaration includes an initializer; otherwise, it is |
| 1487 | // a declaration. |
| 1488 | if (isStaticDataMember()) { |
| 1489 | if (isOutOfLine() && (hasInit() || |
| 1490 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 1491 | return Definition; |
| 1492 | else |
| 1493 | return DeclarationOnly; |
| 1494 | } |
| 1495 | // C99 6.7p5: |
| 1496 | // A definition of an identifier is a declaration for that identifier that |
| 1497 | // [...] causes storage to be reserved for that object. |
| 1498 | // Note: that applies for all non-file-scope objects. |
| 1499 | // C99 6.9.2p1: |
| 1500 | // If the declaration of an identifier for an object has file scope and an |
| 1501 | // initializer, the declaration is an external definition for the identifier |
| 1502 | if (hasInit()) |
| 1503 | return Definition; |
| 1504 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 1505 | if (hasExternalStorage()) |
| 1506 | return DeclarationOnly; |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1507 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1508 | if (getStorageClassAsWritten() == SC_Extern || |
| 1509 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1510 | for (const VarDecl *PrevVar = getPreviousDecl(); |
| 1511 | PrevVar; PrevVar = PrevVar->getPreviousDecl()) { |
Rafael Espindola | 372df45 | 2012-12-17 22:23:47 +0000 | [diff] [blame] | 1512 | if (PrevVar->getLinkage() == InternalLinkage) |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1513 | return DeclarationOnly; |
| 1514 | } |
| 1515 | } |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1516 | // C99 6.9.2p2: |
| 1517 | // A declaration of an object that has file scope without an initializer, |
| 1518 | // and without a storage class specifier or the scs 'static', constitutes |
| 1519 | // a tentative definition. |
| 1520 | // No such thing in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1521 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1522 | return TentativeDefinition; |
| 1523 | |
| 1524 | // What's left is (in C, block-scope) declarations without initializers or |
| 1525 | // external storage. These are definitions. |
| 1526 | return Definition; |
| 1527 | } |
| 1528 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1529 | VarDecl *VarDecl::getActingDefinition() { |
| 1530 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1531 | if (Kind != TentativeDefinition) |
| 1532 | return 0; |
| 1533 | |
Chris Lattner | f0ed9ef | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 1534 | VarDecl *LastTentative = 0; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1535 | VarDecl *First = getFirstDeclaration(); |
| 1536 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1537 | I != E; ++I) { |
| 1538 | Kind = (*I)->isThisDeclarationADefinition(); |
| 1539 | if (Kind == Definition) |
| 1540 | return 0; |
| 1541 | else if (Kind == TentativeDefinition) |
| 1542 | LastTentative = *I; |
| 1543 | } |
| 1544 | return LastTentative; |
| 1545 | } |
| 1546 | |
| 1547 | bool VarDecl::isTentativeDefinitionNow() const { |
| 1548 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1549 | if (Kind != TentativeDefinition) |
| 1550 | return false; |
| 1551 | |
| 1552 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1553 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 1554 | return false; |
| 1555 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1556 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1559 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 1560 | VarDecl *First = getFirstDeclaration(); |
| 1561 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1562 | I != E; ++I) { |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1563 | if ((*I)->isThisDeclarationADefinition(C) == Definition) |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1564 | return *I; |
| 1565 | } |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1569 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1570 | DefinitionKind Kind = DeclarationOnly; |
| 1571 | |
| 1572 | const VarDecl *First = getFirstDeclaration(); |
| 1573 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
Daniel Dunbar | 047da19 | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1574 | I != E; ++I) { |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1575 | Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 047da19 | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1576 | if (Kind == Definition) |
| 1577 | break; |
| 1578 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1579 | |
| 1580 | return Kind; |
| 1581 | } |
| 1582 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1583 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1584 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 1585 | while (I != E && !I->getInit()) |
| 1586 | ++I; |
| 1587 | |
| 1588 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1589 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1590 | return I->getInit(); |
| 1591 | } |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1595 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1596 | if (Decl::isOutOfLine()) |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1597 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (!isStaticDataMember()) |
| 1600 | return false; |
| 1601 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1602 | // If this static data member was instantiated from a static data member of |
| 1603 | // a class template, check whether that static data member was defined |
| 1604 | // out-of-line. |
| 1605 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 1606 | return VD->isOutOfLine(); |
| 1607 | |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1611 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 1612 | if (!isStaticDataMember()) |
| 1613 | return 0; |
| 1614 | |
| 1615 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 1616 | RD != RDEnd; ++RD) { |
| 1617 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 1618 | return *RD; |
| 1619 | } |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1624 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1625 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 1626 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1627 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | Init = I; |
| 1631 | } |
| 1632 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1633 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1634 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1635 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1636 | if (!Lang.CPlusPlus) |
| 1637 | return false; |
| 1638 | |
| 1639 | // In C++11, any variable of reference type can be used in a constant |
| 1640 | // expression if it is initialized by a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1641 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1642 | return true; |
| 1643 | |
| 1644 | // Only const objects can be used in constant expressions in C++. C++98 does |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1645 | // not require the variable to be non-volatile, but we consider this to be a |
| 1646 | // defect. |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1647 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1648 | return false; |
| 1649 | |
| 1650 | // In C++, const, non-volatile variables of integral or enumeration types |
| 1651 | // can be used in constant expressions. |
| 1652 | if (getType()->isIntegralOrEnumerationType()) |
| 1653 | return true; |
| 1654 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1655 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 1656 | // constant expressions. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1657 | return Lang.CPlusPlus11 && isConstexpr(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1660 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 1661 | /// form, which contains extra information on the evaluated value of the |
| 1662 | /// initializer. |
| 1663 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| 1664 | EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| 1665 | if (!Eval) { |
| 1666 | Stmt *S = Init.get<Stmt *>(); |
| 1667 | Eval = new (getASTContext()) EvaluatedStmt; |
| 1668 | Eval->Value = S; |
| 1669 | Init = Eval; |
| 1670 | } |
| 1671 | return Eval; |
| 1672 | } |
| 1673 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1674 | APValue *VarDecl::evaluateValue() const { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1675 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1676 | return evaluateValue(Notes); |
| 1677 | } |
| 1678 | |
| 1679 | APValue *VarDecl::evaluateValue( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1680 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1681 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1682 | |
| 1683 | // We only produce notes indicating why an initializer is non-constant the |
| 1684 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 1685 | // first time we try evaluation, so might not be produced at all. |
| 1686 | if (Eval->WasEvaluated) |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1687 | return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1688 | |
| 1689 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1690 | assert(!Init->isValueDependent()); |
| 1691 | |
| 1692 | if (Eval->IsEvaluating) { |
| 1693 | // FIXME: Produce a diagnostic for self-initialization. |
| 1694 | Eval->CheckedICE = true; |
| 1695 | Eval->IsICE = false; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1696 | return 0; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | Eval->IsEvaluating = true; |
| 1700 | |
| 1701 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 1702 | this, Notes); |
| 1703 | |
| 1704 | // Ensure the result is an uninitialized APValue if evaluation fails. |
| 1705 | if (!Result) |
| 1706 | Eval->Evaluated = APValue(); |
| 1707 | |
| 1708 | Eval->IsEvaluating = false; |
| 1709 | Eval->WasEvaluated = true; |
| 1710 | |
| 1711 | // In C++11, we have determined whether the initializer was a constant |
| 1712 | // expression as a side-effect. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1713 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1714 | Eval->CheckedICE = true; |
Eli Friedman | 210386e | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 1715 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1718 | return Result ? &Eval->Evaluated : 0; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | bool VarDecl::checkInitIsICE() const { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 1722 | // Initializers of weak variables are never ICEs. |
| 1723 | if (isWeak()) |
| 1724 | return false; |
| 1725 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1726 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1727 | if (Eval->CheckedICE) |
| 1728 | // We have already checked whether this subexpression is an |
| 1729 | // integral constant expression. |
| 1730 | return Eval->IsICE; |
| 1731 | |
| 1732 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1733 | assert(!Init->isValueDependent()); |
| 1734 | |
| 1735 | // In C++11, evaluate the initializer to check whether it's a constant |
| 1736 | // expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1737 | if (getASTContext().getLangOpts().CPlusPlus11) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1738 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1739 | evaluateValue(Notes); |
| 1740 | return Eval->IsICE; |
| 1741 | } |
| 1742 | |
| 1743 | // It's an ICE whether or not the definition we found is |
| 1744 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 1745 | // 6206 for details. |
| 1746 | |
| 1747 | if (Eval->CheckingICE) |
| 1748 | return false; |
| 1749 | Eval->CheckingICE = true; |
| 1750 | |
| 1751 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 1752 | Eval->CheckingICE = false; |
| 1753 | Eval->CheckedICE = true; |
| 1754 | return Eval->IsICE; |
| 1755 | } |
| 1756 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1757 | bool VarDecl::extendsLifetimeOfTemporary() const { |
Douglas Gregor | 0b58108 | 2011-06-21 18:20:46 +0000 | [diff] [blame] | 1758 | assert(getType()->isReferenceType() &&"Non-references never extend lifetime"); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1759 | |
| 1760 | const Expr *E = getInit(); |
| 1761 | if (!E) |
| 1762 | return false; |
| 1763 | |
| 1764 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E)) |
| 1765 | E = Cleanups->getSubExpr(); |
| 1766 | |
| 1767 | return isa<MaterializeTemporaryExpr>(E); |
| 1768 | } |
| 1769 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1770 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1771 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1772 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1777 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1778 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1779 | return MSI->getTemplateSpecializationKind(); |
| 1780 | |
| 1781 | return TSK_Undeclared; |
| 1782 | } |
| 1783 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1784 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1785 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 1786 | } |
| 1787 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1788 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1789 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1790 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1791 | assert(MSI && "Not an instantiated static data member?"); |
| 1792 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1793 | if (TSK != TSK_ExplicitSpecialization && |
| 1794 | PointOfInstantiation.isValid() && |
| 1795 | MSI->getPointOfInstantiation().isInvalid()) |
| 1796 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1799 | //===----------------------------------------------------------------------===// |
| 1800 | // ParmVarDecl Implementation |
| 1801 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1802 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1803 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1804 | SourceLocation StartLoc, |
| 1805 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1806 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1807 | StorageClass S, StorageClass SCAsWritten, |
| 1808 | Expr *DefArg) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1809 | return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1810 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1813 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1814 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl)); |
| 1815 | return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(), |
| 1816 | 0, QualType(), 0, SC_None, SC_None, 0); |
| 1817 | } |
| 1818 | |
Argyrios Kyrtzidis | 0bfe83b | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 1819 | SourceRange ParmVarDecl::getSourceRange() const { |
| 1820 | if (!hasInheritedDefaultArg()) { |
| 1821 | SourceRange ArgRange = getDefaultArgRange(); |
| 1822 | if (ArgRange.isValid()) |
| 1823 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 1824 | } |
| 1825 | |
| 1826 | return DeclaratorDecl::getSourceRange(); |
| 1827 | } |
| 1828 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1829 | Expr *ParmVarDecl::getDefaultArg() { |
| 1830 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1831 | assert(!hasUninstantiatedDefaultArg() && |
| 1832 | "Default argument is not yet instantiated!"); |
| 1833 | |
| 1834 | Expr *Arg = getInit(); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1835 | if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1836 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1837 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1838 | return Arg; |
| 1839 | } |
| 1840 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1841 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1842 | if (const Expr *E = getInit()) |
| 1843 | return E->getSourceRange(); |
| 1844 | |
| 1845 | if (hasUninstantiatedDefaultArg()) |
| 1846 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1847 | |
| 1848 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 1851 | bool ParmVarDecl::isParameterPack() const { |
| 1852 | return isa<PackExpansionType>(getType()); |
| 1853 | } |
| 1854 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1855 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 1856 | getASTContext().setParameterIndex(this, parameterIndex); |
| 1857 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 1858 | } |
| 1859 | |
| 1860 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 1861 | return getASTContext().getParameterIndex(this); |
| 1862 | } |
| 1863 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1864 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1865 | // FunctionDecl Implementation |
| 1866 | //===----------------------------------------------------------------------===// |
| 1867 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1868 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1869 | const PrintingPolicy &Policy, |
| 1870 | bool Qualified) const { |
| 1871 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1872 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1873 | if (TemplateArgs) |
| 1874 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1875 | TemplateArgs->data(), |
| 1876 | TemplateArgs->size(), |
| 1877 | Policy); |
| 1878 | |
| 1879 | } |
| 1880 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1881 | bool FunctionDecl::isVariadic() const { |
| 1882 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1883 | return FT->isVariadic(); |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1887 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1888 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1889 | if (I->Body || I->IsLateTemplateParsed) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1890 | Definition = *I; |
| 1891 | return true; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1898 | bool FunctionDecl::hasTrivialBody() const |
| 1899 | { |
| 1900 | Stmt *S = getBody(); |
| 1901 | if (!S) { |
| 1902 | // Since we don't have a body for this function, we don't know if it's |
| 1903 | // trivial or not. |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
| 1907 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 1908 | return true; |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1912 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
| 1913 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 1914 | if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1915 | Definition = I->IsDeleted ? I->getCanonicalDecl() : *I; |
| 1916 | return true; |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | return false; |
| 1921 | } |
| 1922 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1923 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1924 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1925 | if (I->Body) { |
| 1926 | Definition = *I; |
| 1927 | return I->Body.get(getASTContext().getExternalSource()); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1928 | } else if (I->IsLateTemplateParsed) { |
| 1929 | Definition = *I; |
| 1930 | return 0; |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1937 | void FunctionDecl::setBody(Stmt *B) { |
| 1938 | Body = B; |
Douglas Gregor | b5f35ba | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 1939 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1940 | EndRangeLoc = B->getLocEnd(); |
| 1941 | } |
| 1942 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1943 | void FunctionDecl::setPure(bool P) { |
| 1944 | IsPure = P; |
| 1945 | if (P) |
| 1946 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1947 | Parent->markedVirtualFunctionPure(); |
| 1948 | } |
| 1949 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1950 | bool FunctionDecl::isMain() const { |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1951 | const TranslationUnitDecl *tunit = |
| 1952 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 1953 | return tunit && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | !tunit->getASTContext().getLangOpts().Freestanding && |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1955 | getIdentifier() && |
| 1956 | getIdentifier()->isStr("main"); |
| 1957 | } |
| 1958 | |
| 1959 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 1960 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 1961 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 1962 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 1963 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 1964 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 1965 | |
| 1966 | if (isa<CXXRecordDecl>(getDeclContext())) return false; |
| 1967 | assert(getDeclContext()->getRedeclContext()->isTranslationUnit()); |
| 1968 | |
| 1969 | const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); |
| 1970 | if (proto->getNumArgs() != 2 || proto->isVariadic()) return false; |
| 1971 | |
| 1972 | ASTContext &Context = |
| 1973 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 1974 | ->getASTContext(); |
| 1975 | |
| 1976 | // The result type and first argument type are constant across all |
| 1977 | // these operators. The second argument must be exactly void*. |
| 1978 | return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1981 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
Rafael Espindola | 6dcea67 | 2013-01-12 15:27:44 +0000 | [diff] [blame] | 1982 | // Users expect to be able to write |
| 1983 | // extern "C" void *__builtin_alloca (size_t); |
| 1984 | // so consider builtins as having C language linkage. |
Rafael Espindola | 508276c | 2013-01-12 15:27:43 +0000 | [diff] [blame] | 1985 | if (getBuiltinID()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1986 | return CLanguageLinkage; |
Rafael Espindola | 508276c | 2013-01-12 15:27:43 +0000 | [diff] [blame] | 1987 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1988 | return getLanguageLinkageTemplate(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1991 | bool FunctionDecl::isGlobal() const { |
| 1992 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1993 | return Method->isStatic(); |
| 1994 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1995 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1996 | return false; |
| 1997 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1999 | DC->isNamespace(); |
| 2000 | DC = DC->getParent()) { |
| 2001 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 2002 | if (!Namespace->getDeclName()) |
| 2003 | return false; |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | return true; |
| 2009 | } |
| 2010 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2011 | bool FunctionDecl::isNoReturn() const { |
| 2012 | return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2013 | hasAttr<C11NoReturnAttr>() || |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2014 | getType()->getAs<FunctionType>()->getNoReturnAttr(); |
| 2015 | } |
| 2016 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2017 | void |
| 2018 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 2019 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 2020 | |
| 2021 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 2022 | FunctionTemplateDecl *PrevFunTmpl |
| 2023 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 2024 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 2025 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 2026 | } |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2027 | |
Axel Naumann | d9d137e | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 2028 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2029 | IsInline = true; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 2033 | return getFirstDeclaration(); |
| 2034 | } |
| 2035 | |
| 2036 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 2037 | return getFirstDeclaration(); |
| 2038 | } |
| 2039 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 2040 | void FunctionDecl::setStorageClass(StorageClass SC) { |
| 2041 | assert(isLegalForFunction(SC)); |
| 2042 | if (getStorageClass() != SC) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2043 | ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 2044 | |
| 2045 | SClass = SC; |
| 2046 | } |
| 2047 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2048 | /// \brief Returns a value indicating whether this function |
| 2049 | /// corresponds to a builtin function. |
| 2050 | /// |
| 2051 | /// The function corresponds to a built-in function if it is |
| 2052 | /// declared at translation scope or within an extern "C" block and |
| 2053 | /// its name matches with the name of a builtin. The returned value |
| 2054 | /// 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] | 2055 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2056 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2057 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2058 | if (!getIdentifier()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2059 | return 0; |
| 2060 | |
| 2061 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2062 | if (!BuiltinID) |
| 2063 | return 0; |
| 2064 | |
| 2065 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2066 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 2067 | return BuiltinID; |
| 2068 | |
| 2069 | // This function has the name of a known C library |
| 2070 | // function. Determine whether it actually refers to the C library |
| 2071 | // function or whether it just has the same name. |
| 2072 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2073 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2074 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2075 | return 0; |
| 2076 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2077 | // If this function is at translation-unit scope and we're not in |
| 2078 | // C++, it refers to the C library function. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2079 | if (!Context.getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2080 | getDeclContext()->isTranslationUnit()) |
| 2081 | return BuiltinID; |
| 2082 | |
| 2083 | // If the function is in an extern "C" linkage specification and is |
| 2084 | // not marked "overloadable", it's the real function. |
| 2085 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2087 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2088 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2089 | return BuiltinID; |
| 2090 | |
| 2091 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2096 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | 8dbfbf4 | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 2097 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2098 | /// after it has been created. |
| 2099 | unsigned FunctionDecl::getNumParams() const { |
Eli Friedman | 482466b | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 2100 | const FunctionType *FT = getType()->castAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2101 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 2102 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2103 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2107 | void FunctionDecl::setParams(ASTContext &C, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2108 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | assert(ParamInfo == 0 && "Already has param info!"); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2110 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2112 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2113 | if (!NewParamInfo.empty()) { |
| 2114 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 2115 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2119 | void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2120 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 2121 | |
| 2122 | if (!NewDecls.empty()) { |
| 2123 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 2124 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2125 | DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size()); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2129 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 2130 | /// needed to call this function. This may be fewer than the number of |
| 2131 | /// function parameters, if some of the parameters have default |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2132 | /// arguments (in C++) or the last parameter is a parameter pack. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2133 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2134 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 2135 | return getNumParams(); |
| 2136 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2137 | unsigned NumRequiredArgs = getNumParams(); |
| 2138 | |
| 2139 | // If the last parameter is a parameter pack, we don't need an argument for |
| 2140 | // it. |
| 2141 | if (NumRequiredArgs > 0 && |
| 2142 | getParamDecl(NumRequiredArgs - 1)->isParameterPack()) |
| 2143 | --NumRequiredArgs; |
| 2144 | |
| 2145 | // If this parameter has a default argument, we don't need an argument for |
| 2146 | // it. |
| 2147 | while (NumRequiredArgs > 0 && |
| 2148 | getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2149 | --NumRequiredArgs; |
| 2150 | |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 2151 | // We might have parameter packs before the end. These can't be deduced, |
| 2152 | // but they can still handle multiple arguments. |
| 2153 | unsigned ArgIdx = NumRequiredArgs; |
| 2154 | while (ArgIdx > 0) { |
| 2155 | if (getParamDecl(ArgIdx - 1)->isParameterPack()) |
| 2156 | NumRequiredArgs = ArgIdx; |
| 2157 | |
| 2158 | --ArgIdx; |
| 2159 | } |
| 2160 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2161 | return NumRequiredArgs; |
| 2162 | } |
| 2163 | |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2164 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 2165 | // Only consider file-scope declarations in this test. |
| 2166 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 2167 | return false; |
| 2168 | |
| 2169 | // Only consider explicit declarations; the presence of a builtin for a |
| 2170 | // libcall shouldn't affect whether a definition is externally visible. |
| 2171 | if (Redecl->isImplicit()) |
| 2172 | return false; |
| 2173 | |
| 2174 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 2175 | return true; // Not an inline definition |
| 2176 | |
| 2177 | return false; |
| 2178 | } |
| 2179 | |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2180 | /// \brief For a function declaration in C or C++, determine whether this |
| 2181 | /// declaration causes the definition to be externally visible. |
| 2182 | /// |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2183 | /// Specifically, this determines if adding the current declaration to the set |
| 2184 | /// of redeclarations of the given functions causes |
| 2185 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2186 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 2187 | assert(!doesThisDeclarationHaveABody() && |
| 2188 | "Must have a declaration without a body."); |
| 2189 | |
| 2190 | ASTContext &Context = getASTContext(); |
| 2191 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2192 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2193 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 2194 | // an externally visible definition. |
| 2195 | // |
| 2196 | // FIXME: What happens if gnu_inline gets added on after the first |
| 2197 | // declaration? |
| 2198 | if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern) |
| 2199 | return false; |
| 2200 | |
| 2201 | const FunctionDecl *Prev = this; |
| 2202 | bool FoundBody = false; |
| 2203 | while ((Prev = Prev->getPreviousDecl())) { |
| 2204 | FoundBody |= Prev->Body; |
| 2205 | |
| 2206 | if (Prev->Body) { |
| 2207 | // If it's not the case that both 'inline' and 'extern' are |
| 2208 | // specified on the definition, then it is always externally visible. |
| 2209 | if (!Prev->isInlineSpecified() || |
| 2210 | Prev->getStorageClassAsWritten() != SC_Extern) |
| 2211 | return false; |
| 2212 | } else if (Prev->isInlineSpecified() && |
| 2213 | Prev->getStorageClassAsWritten() != SC_Extern) { |
| 2214 | return false; |
| 2215 | } |
| 2216 | } |
| 2217 | return FoundBody; |
| 2218 | } |
| 2219 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2220 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2221 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2222 | |
| 2223 | // C99 6.7.4p6: |
| 2224 | // [...] If all of the file scope declarations for a function in a |
| 2225 | // translation unit include the inline function specifier without extern, |
| 2226 | // then the definition in that translation unit is an inline definition. |
| 2227 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2228 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2229 | const FunctionDecl *Prev = this; |
| 2230 | bool FoundBody = false; |
| 2231 | while ((Prev = Prev->getPreviousDecl())) { |
| 2232 | FoundBody |= Prev->Body; |
| 2233 | if (RedeclForcesDefC99(Prev)) |
| 2234 | return false; |
| 2235 | } |
| 2236 | return FoundBody; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2239 | /// \brief For an inline function definition in C, or for a gnu_inline function |
| 2240 | /// in C++, determine whether the definition will be externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2241 | /// |
| 2242 | /// Inline function definitions are always available for inlining optimizations. |
| 2243 | /// However, depending on the language dialect, declaration specifiers, and |
| 2244 | /// attributes, the definition of an inline function may or may not be |
| 2245 | /// "externally" visible to other translation units in the program. |
| 2246 | /// |
| 2247 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2248 | /// 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] | 2249 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2250 | /// |
| 2251 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2252 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2253 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2254 | /// an externally visible symbol, but "extern inline" will not create an |
| 2255 | /// externally visible symbol. |
| 2256 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2257 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2258 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2259 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2260 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2261 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2262 | // Note: If you change the logic here, please change |
| 2263 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2264 | // |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2265 | // If it's not the case that both 'inline' and 'extern' are |
| 2266 | // specified on the definition, then this inline definition is |
| 2267 | // externally visible. |
| 2268 | if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) |
| 2269 | return true; |
| 2270 | |
| 2271 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2272 | // is externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2273 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2274 | Redecl != RedeclEnd; |
| 2275 | ++Redecl) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2276 | if (Redecl->isInlineSpecified() && |
| 2277 | Redecl->getStorageClassAsWritten() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2278 | return true; |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2279 | } |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2281 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2282 | } |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2283 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2284 | // The rest of this function is C-only. |
| 2285 | assert(!Context.getLangOpts().CPlusPlus && |
| 2286 | "should not use C inline rules in C++"); |
| 2287 | |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2288 | // C99 6.7.4p6: |
| 2289 | // [...] If all of the file scope declarations for a function in a |
| 2290 | // translation unit include the inline function specifier without extern, |
| 2291 | // then the definition in that translation unit is an inline definition. |
| 2292 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2293 | Redecl != RedeclEnd; |
| 2294 | ++Redecl) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2295 | if (RedeclForcesDefC99(*Redecl)) |
| 2296 | return true; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | // C99 6.7.4p6: |
| 2300 | // An inline definition does not provide an external definition for the |
| 2301 | // function, and does not forbid an external definition in another |
| 2302 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2303 | return false; |
| 2304 | } |
| 2305 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2306 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2307 | /// function represents, if any. |
| 2308 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2309 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2310 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2311 | else |
| 2312 | return OO_None; |
| 2313 | } |
| 2314 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2315 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2316 | /// represents, if any. |
| 2317 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2318 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2319 | return getDeclName().getCXXLiteralIdentifier(); |
| 2320 | else |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2324 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2325 | if (TemplateOrSpecialization.isNull()) |
| 2326 | return TK_NonTemplate; |
| 2327 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2328 | return TK_FunctionTemplate; |
| 2329 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2330 | return TK_MemberSpecialization; |
| 2331 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2332 | return TK_FunctionTemplateSpecialization; |
| 2333 | if (TemplateOrSpecialization.is |
| 2334 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2335 | return TK_DependentFunctionTemplateSpecialization; |
| 2336 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2337 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2340 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2341 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2342 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 2343 | |
| 2344 | return 0; |
| 2345 | } |
| 2346 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2347 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 2348 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2349 | } |
| 2350 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2351 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2352 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2353 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2354 | TemplateSpecializationKind TSK) { |
| 2355 | assert(TemplateOrSpecialization.isNull() && |
| 2356 | "Member function is already a specialization"); |
| 2357 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2358 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2359 | TemplateOrSpecialization = Info; |
| 2360 | } |
| 2361 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2362 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2363 | // If the function is invalid, it can't be implicitly instantiated. |
| 2364 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2365 | return false; |
| 2366 | |
| 2367 | switch (getTemplateSpecializationKind()) { |
| 2368 | case TSK_Undeclared: |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2369 | case TSK_ExplicitInstantiationDefinition: |
| 2370 | return false; |
| 2371 | |
| 2372 | case TSK_ImplicitInstantiation: |
| 2373 | return true; |
| 2374 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2375 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2376 | // if the FunctionDecl has a class scope specialization pattern. |
| 2377 | case TSK_ExplicitSpecialization: |
| 2378 | return getClassScopeSpecializationPattern() != 0; |
| 2379 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2380 | case TSK_ExplicitInstantiationDeclaration: |
| 2381 | // Handled below. |
| 2382 | break; |
| 2383 | } |
| 2384 | |
| 2385 | // Find the actual template from which we will instantiate. |
| 2386 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2387 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2388 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2389 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2390 | |
| 2391 | // C++0x [temp.explicit]p9: |
| 2392 | // Except for inline functions, other explicit instantiation declarations |
| 2393 | // have the effect of suppressing the implicit instantiation of the entity |
| 2394 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2395 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2396 | return true; |
| 2397 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2398 | return PatternDecl->isInlined(); |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | bool FunctionDecl::isTemplateInstantiation() const { |
| 2402 | switch (getTemplateSpecializationKind()) { |
| 2403 | case TSK_Undeclared: |
| 2404 | case TSK_ExplicitSpecialization: |
| 2405 | return false; |
| 2406 | case TSK_ImplicitInstantiation: |
| 2407 | case TSK_ExplicitInstantiationDeclaration: |
| 2408 | case TSK_ExplicitInstantiationDefinition: |
| 2409 | return true; |
| 2410 | } |
| 2411 | llvm_unreachable("All TSK values handled."); |
| 2412 | } |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2413 | |
| 2414 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2415 | // Handle class scope explicit specialization special case. |
| 2416 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2417 | return getClassScopeSpecializationPattern(); |
| 2418 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2419 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 2420 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 2421 | // If we have hit a point where the user provided a specialization of |
| 2422 | // this template, we're done looking. |
| 2423 | if (Primary->isMemberSpecialization()) |
| 2424 | break; |
| 2425 | |
| 2426 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 2427 | } |
| 2428 | |
| 2429 | return Primary->getTemplatedDecl(); |
| 2430 | } |
| 2431 | |
| 2432 | return getInstantiatedFromMemberFunction(); |
| 2433 | } |
| 2434 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2435 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2437 | = TemplateOrSpecialization |
| 2438 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2439 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2440 | } |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2444 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 2445 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 2446 | } |
| 2447 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2448 | const TemplateArgumentList * |
| 2449 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2451 | = TemplateOrSpecialization |
| 2452 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2453 | return Info->TemplateArguments; |
| 2454 | } |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 2458 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2459 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 2460 | if (FunctionTemplateSpecializationInfo *Info |
| 2461 | = TemplateOrSpecialization |
| 2462 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 2463 | return Info->TemplateArgumentsAsWritten; |
| 2464 | } |
| 2465 | return 0; |
| 2466 | } |
| 2467 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2468 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2469 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 2470 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2471 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2472 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2473 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 2474 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 2475 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2476 | assert(TSK != TSK_Undeclared && |
| 2477 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2479 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2480 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 2481 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 2482 | TemplateArgs, |
| 2483 | TemplateArgsAsWritten, |
| 2484 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2485 | TemplateOrSpecialization = Info; |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2486 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2489 | void |
| 2490 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 2491 | const UnresolvedSetImpl &Templates, |
| 2492 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2493 | assert(TemplateOrSpecialization.isNull()); |
| 2494 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 2495 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 2496 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2497 | void *Buffer = Context.Allocate(Size); |
| 2498 | DependentFunctionTemplateSpecializationInfo *Info = |
| 2499 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 2500 | TemplateArgs); |
| 2501 | TemplateOrSpecialization = Info; |
| 2502 | } |
| 2503 | |
| 2504 | DependentFunctionTemplateSpecializationInfo:: |
| 2505 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 2506 | const TemplateArgumentListInfo &TArgs) |
| 2507 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 2508 | |
| 2509 | d.NumTemplates = Ts.size(); |
| 2510 | d.NumArgs = TArgs.size(); |
| 2511 | |
| 2512 | FunctionTemplateDecl **TsArray = |
| 2513 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 2514 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 2515 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 2516 | |
| 2517 | TemplateArgumentLoc *ArgsArray = |
| 2518 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 2519 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 2520 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 2521 | } |
| 2522 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2523 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2525 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2526 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2527 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2528 | if (FTSInfo) |
| 2529 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2531 | MemberSpecializationInfo *MSInfo |
| 2532 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2533 | if (MSInfo) |
| 2534 | return MSInfo->getTemplateSpecializationKind(); |
| 2535 | |
| 2536 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2540 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2541 | SourceLocation PointOfInstantiation) { |
| 2542 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2543 | = TemplateOrSpecialization.dyn_cast< |
| 2544 | FunctionTemplateSpecializationInfo*>()) { |
| 2545 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 2546 | if (TSK != TSK_ExplicitSpecialization && |
| 2547 | PointOfInstantiation.isValid() && |
| 2548 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 2549 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2550 | } else if (MemberSpecializationInfo *MSInfo |
| 2551 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 2552 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2553 | if (TSK != TSK_ExplicitSpecialization && |
| 2554 | PointOfInstantiation.isValid() && |
| 2555 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 2556 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2557 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2558 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2562 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2563 | = TemplateOrSpecialization.dyn_cast< |
| 2564 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2565 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2566 | else if (MemberSpecializationInfo *MSInfo |
| 2567 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2568 | return MSInfo->getPointOfInstantiation(); |
| 2569 | |
| 2570 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2573 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2574 | if (Decl::isOutOfLine()) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2575 | return true; |
| 2576 | |
| 2577 | // If this function was instantiated from a member function of a |
| 2578 | // class template, check whether that member function was defined out-of-line. |
| 2579 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 2580 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2581 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2582 | return Definition->isOutOfLine(); |
| 2583 | } |
| 2584 | |
| 2585 | // If this function was instantiated from a function template, |
| 2586 | // check whether that function template was defined out-of-line. |
| 2587 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 2588 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2589 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2590 | return Definition->isOutOfLine(); |
| 2591 | } |
| 2592 | |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2596 | SourceRange FunctionDecl::getSourceRange() const { |
| 2597 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 2598 | } |
| 2599 | |
Anna Zaks | 9392d4e | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 2600 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2601 | IdentifierInfo *FnInfo = getIdentifier(); |
| 2602 | |
| 2603 | if (!FnInfo) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2604 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Builtin handling. |
| 2607 | switch (getBuiltinID()) { |
| 2608 | case Builtin::BI__builtin_memset: |
| 2609 | case Builtin::BI__builtin___memset_chk: |
| 2610 | case Builtin::BImemset: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2611 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2612 | |
| 2613 | case Builtin::BI__builtin_memcpy: |
| 2614 | case Builtin::BI__builtin___memcpy_chk: |
| 2615 | case Builtin::BImemcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2616 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2617 | |
| 2618 | case Builtin::BI__builtin_memmove: |
| 2619 | case Builtin::BI__builtin___memmove_chk: |
| 2620 | case Builtin::BImemmove: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2621 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2622 | |
| 2623 | case Builtin::BIstrlcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2624 | return Builtin::BIstrlcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2625 | case Builtin::BIstrlcat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2626 | return Builtin::BIstrlcat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2627 | |
| 2628 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2629 | case Builtin::BImemcmp: |
| 2630 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2631 | |
| 2632 | case Builtin::BI__builtin_strncpy: |
| 2633 | case Builtin::BI__builtin___strncpy_chk: |
| 2634 | case Builtin::BIstrncpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2635 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2636 | |
| 2637 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2638 | case Builtin::BIstrncmp: |
| 2639 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2640 | |
| 2641 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2642 | case Builtin::BIstrncasecmp: |
| 2643 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2644 | |
| 2645 | case Builtin::BI__builtin_strncat: |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2646 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2647 | case Builtin::BIstrncat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2648 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2649 | |
| 2650 | case Builtin::BI__builtin_strndup: |
| 2651 | case Builtin::BIstrndup: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2652 | return Builtin::BIstrndup; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2653 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2654 | case Builtin::BI__builtin_strlen: |
| 2655 | case Builtin::BIstrlen: |
| 2656 | return Builtin::BIstrlen; |
| 2657 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2658 | default: |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2659 | if (isExternC()) { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2660 | if (FnInfo->isStr("memset")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2661 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2662 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2663 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2664 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2665 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2666 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2667 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2668 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2669 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2670 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2671 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2672 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2673 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2674 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2675 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2676 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2677 | return Builtin::BIstrndup; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2678 | else if (FnInfo->isStr("strlen")) |
| 2679 | return Builtin::BIstrlen; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2680 | } |
| 2681 | break; |
| 2682 | } |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2683 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2686 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2687 | // FieldDecl Implementation |
| 2688 | //===----------------------------------------------------------------------===// |
| 2689 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2690 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2691 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2692 | IdentifierInfo *Id, QualType T, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2693 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2694 | InClassInitStyle InitStyle) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2695 | return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2696 | BW, Mutable, InitStyle); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2699 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2700 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl)); |
| 2701 | return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2702 | 0, QualType(), 0, 0, false, ICIS_NoInit); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2705 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 2706 | if (!isImplicit() || getDeclName()) |
| 2707 | return false; |
| 2708 | |
| 2709 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 2710 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 2711 | |
| 2712 | return false; |
| 2713 | } |
| 2714 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2715 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 2716 | assert(isBitField() && "not a bitfield"); |
| 2717 | Expr *BitWidth = InitializerOrBitWidth.getPointer(); |
| 2718 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 2719 | } |
| 2720 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2721 | unsigned FieldDecl::getFieldIndex() const { |
| 2722 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 2723 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2724 | unsigned Index = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2725 | const RecordDecl *RD = getParent(); |
| 2726 | const FieldDecl *LastFD = 0; |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 2727 | bool IsMsStruct = RD->isMsStruct(getASTContext()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2728 | |
| 2729 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 2730 | I != E; ++I, ++Index) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2731 | I->CachedFieldIndex = Index + 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2732 | |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2733 | if (IsMsStruct) { |
| 2734 | // Zero-length bitfields following non-bitfield members are ignored. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2735 | if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2736 | --Index; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2737 | continue; |
| 2738 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2739 | LastFD = *I; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2740 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2743 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 2744 | return CachedFieldIndex - 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2747 | SourceRange FieldDecl::getSourceRange() const { |
Abramo Bagnara | d330e23 | 2011-08-05 08:02:55 +0000 | [diff] [blame] | 2748 | if (const Expr *E = InitializerOrBitWidth.getPointer()) |
| 2749 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2750 | return DeclaratorDecl::getSourceRange(); |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Abramo Bagnara | a533576 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 2753 | void FieldDecl::setBitWidth(Expr *Width) { |
| 2754 | assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() && |
| 2755 | "bit width or initializer already set"); |
| 2756 | InitializerOrBitWidth.setPointer(Width); |
| 2757 | } |
| 2758 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2759 | void FieldDecl::setInClassInitializer(Expr *Init) { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2760 | assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2761 | "bit width or initializer already set"); |
| 2762 | InitializerOrBitWidth.setPointer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2765 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2766 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2767 | //===----------------------------------------------------------------------===// |
| 2768 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2769 | SourceLocation TagDecl::getOuterLocStart() const { |
| 2770 | return getTemplateOrInnerLocStart(this); |
| 2771 | } |
| 2772 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2773 | SourceRange TagDecl::getSourceRange() const { |
| 2774 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2775 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2778 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2779 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2782 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| 2783 | TypedefNameDeclOrQualifier = TDD; |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2784 | if (TypeForDecl) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2785 | const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); |
| 2786 | ClearLinkageCache(); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2789 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2790 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2791 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 2792 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2793 | struct CXXRecordDecl::DefinitionData *Data = |
| 2794 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 2795 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 2796 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2797 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2801 | assert((!isa<CXXRecordDecl>(this) || |
| 2802 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 2803 | "definition completed but not started"); |
| 2804 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2805 | IsCompleteDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2806 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 2807 | |
| 2808 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2809 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2812 | TagDecl *TagDecl::getDefinition() const { |
| 2813 | if (isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2814 | return const_cast<TagDecl *>(this); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2815 | |
| 2816 | // If it's possible for us to have an out-of-date definition, check now. |
| 2817 | if (MayHaveOutOfDateDef) { |
| 2818 | if (IdentifierInfo *II = getIdentifier()) { |
| 2819 | if (II->isOutOfDate()) { |
| 2820 | updateOutOfDate(*II); |
| 2821 | } |
| 2822 | } |
| 2823 | } |
| 2824 | |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 2825 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 2826 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
| 2828 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2829 | R != REnd; ++R) |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2830 | if (R->isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2831 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2833 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2836 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 2837 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2838 | // Make sure the extended qualifier info is allocated. |
| 2839 | if (!hasExtInfo()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2840 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2841 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2842 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2843 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2844 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2845 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2846 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 2847 | getASTContext().Deallocate(getExtInfo()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2848 | TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2849 | } |
| 2850 | else |
| 2851 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | } |
| 2855 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2856 | void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 2857 | unsigned NumTPLists, |
| 2858 | TemplateParameterList **TPLists) { |
| 2859 | assert(NumTPLists > 0); |
| 2860 | // Make sure the extended decl info is allocated. |
| 2861 | if (!hasExtInfo()) |
| 2862 | // Allocate external info struct. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2863 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2864 | // Set the template parameter lists info. |
| 2865 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 2866 | } |
| 2867 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2868 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2869 | // EnumDecl Implementation |
| 2870 | //===----------------------------------------------------------------------===// |
| 2871 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2872 | void EnumDecl::anchor() { } |
| 2873 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2874 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 2875 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2876 | IdentifierInfo *Id, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2877 | EnumDecl *PrevDecl, bool IsScoped, |
| 2878 | bool IsScopedUsingClassTag, bool IsFixed) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2879 | EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2880 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2881 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2882 | C.getTypeDeclType(Enum, PrevDecl); |
| 2883 | return Enum; |
| 2884 | } |
| 2885 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2886 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2887 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl)); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2888 | EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), |
| 2889 | 0, 0, false, false, false); |
| 2890 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2891 | return Enum; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2894 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2895 | QualType NewPromotionType, |
| 2896 | unsigned NumPositiveBits, |
| 2897 | unsigned NumNegativeBits) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2898 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2899 | if (!IntegerType) |
| 2900 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2901 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2902 | setNumPositiveBits(NumPositiveBits); |
| 2903 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2904 | TagDecl::completeDefinition(); |
| 2905 | } |
| 2906 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2907 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 2908 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2909 | return MSI->getTemplateSpecializationKind(); |
| 2910 | |
| 2911 | return TSK_Undeclared; |
| 2912 | } |
| 2913 | |
| 2914 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2915 | SourceLocation PointOfInstantiation) { |
| 2916 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 2917 | assert(MSI && "Not an instantiated member enumeration?"); |
| 2918 | MSI->setTemplateSpecializationKind(TSK); |
| 2919 | if (TSK != TSK_ExplicitSpecialization && |
| 2920 | PointOfInstantiation.isValid() && |
| 2921 | MSI->getPointOfInstantiation().isInvalid()) |
| 2922 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 2923 | } |
| 2924 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2925 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 2926 | if (SpecializationInfo) |
| 2927 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
| 2932 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 2933 | TemplateSpecializationKind TSK) { |
| 2934 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 2935 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 2936 | } |
| 2937 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2938 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2939 | // RecordDecl Implementation |
| 2940 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2941 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2942 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, |
| 2943 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2944 | IdentifierInfo *Id, RecordDecl *PrevDecl) |
| 2945 | : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2946 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2947 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2948 | HasObjectMember = false; |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 2949 | HasVolatileMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2950 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2951 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2954 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2955 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2956 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| 2957 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, |
| 2958 | PrevDecl); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2959 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2960 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2961 | C.getTypeDeclType(R, PrevDecl); |
| 2962 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2965 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 2966 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl)); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2967 | RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), |
| 2968 | SourceLocation(), 0, 0); |
| 2969 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2970 | return R; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2973 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2975 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 2976 | } |
| 2977 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2978 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 2979 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 2980 | LoadFieldsFromExternalStorage(); |
| 2981 | |
| 2982 | return field_iterator(decl_iterator(FirstDecl)); |
| 2983 | } |
| 2984 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2985 | /// completeDefinition - Notes that the definition of this type is now |
| 2986 | /// complete. |
| 2987 | void RecordDecl::completeDefinition() { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2988 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2989 | TagDecl::completeDefinition(); |
| 2990 | } |
| 2991 | |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 2992 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 2993 | /// This which can be turned on with an attribute, pragma, or the |
| 2994 | /// -mms-bitfields command-line option. |
| 2995 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
| 2996 | return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1; |
| 2997 | } |
| 2998 | |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 2999 | static bool isFieldOrIndirectField(Decl::Kind K) { |
| 3000 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 3001 | } |
| 3002 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3003 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 3004 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 3005 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 3006 | |
| 3007 | // Notify that we have a RecordDecl doing some initialization. |
| 3008 | ExternalASTSource::Deserializing TheFields(Source); |
| 3009 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3010 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3011 | LoadedFieldsFromExternalStorage = true; |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3012 | switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField, |
| 3013 | Decls)) { |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3014 | case ELR_Success: |
| 3015 | break; |
| 3016 | |
| 3017 | case ELR_AlreadyLoaded: |
| 3018 | case ELR_Failure: |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3019 | return; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3020 | } |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3021 | |
| 3022 | #ifndef NDEBUG |
| 3023 | // Check that all decls we got were FieldDecls. |
| 3024 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3025 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3026 | #endif |
| 3027 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3028 | if (Decls.empty()) |
| 3029 | return; |
| 3030 | |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 3031 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| 3032 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 3035 | //===----------------------------------------------------------------------===// |
| 3036 | // BlockDecl Implementation |
| 3037 | //===----------------------------------------------------------------------===// |
| 3038 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3039 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3040 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3042 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3043 | if (!NewParamInfo.empty()) { |
| 3044 | NumParams = NewParamInfo.size(); |
| 3045 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 3046 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3050 | void BlockDecl::setCaptures(ASTContext &Context, |
| 3051 | const Capture *begin, |
| 3052 | const Capture *end, |
| 3053 | bool capturesCXXThis) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3054 | CapturesCXXThis = capturesCXXThis; |
| 3055 | |
| 3056 | if (begin == end) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3057 | NumCaptures = 0; |
| 3058 | Captures = 0; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3059 | return; |
| 3060 | } |
| 3061 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3062 | NumCaptures = end - begin; |
| 3063 | |
| 3064 | // Avoid new Capture[] because we don't want to provide a default |
| 3065 | // constructor. |
| 3066 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 3067 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 3068 | memcpy(buffer, begin, allocationSize); |
| 3069 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3070 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3071 | |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3072 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| 3073 | for (capture_const_iterator |
| 3074 | i = capture_begin(), e = capture_end(); i != e; ++i) |
| 3075 | // Only auto vars can be captured, so no redeclaration worries. |
| 3076 | if (i->getVariable() == variable) |
| 3077 | return true; |
| 3078 | |
| 3079 | return false; |
| 3080 | } |
| 3081 | |
Douglas Gregor | 2fcbcef | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 3082 | SourceRange BlockDecl::getSourceRange() const { |
| 3083 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 3084 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3085 | |
| 3086 | //===----------------------------------------------------------------------===// |
| 3087 | // Other Decl Allocation/Deallocation Method Implementations |
| 3088 | //===----------------------------------------------------------------------===// |
| 3089 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3090 | void TranslationUnitDecl::anchor() { } |
| 3091 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3092 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 3093 | return new (C) TranslationUnitDecl(C); |
| 3094 | } |
| 3095 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3096 | void LabelDecl::anchor() { } |
| 3097 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3098 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3099 | SourceLocation IdentL, IdentifierInfo *II) { |
| 3100 | return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); |
| 3101 | } |
| 3102 | |
| 3103 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 3104 | SourceLocation IdentL, IdentifierInfo *II, |
| 3105 | SourceLocation GnuLabelL) { |
| 3106 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| 3107 | return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3110 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3111 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl)); |
| 3112 | return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation()); |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3115 | void ValueDecl::anchor() { } |
| 3116 | |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 3117 | bool ValueDecl::isWeak() const { |
| 3118 | for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I) |
| 3119 | if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I)) |
| 3120 | return true; |
| 3121 | |
| 3122 | return isWeakImported(); |
| 3123 | } |
| 3124 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3125 | void ImplicitParamDecl::anchor() { } |
| 3126 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3127 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3128 | SourceLocation IdLoc, |
| 3129 | IdentifierInfo *Id, |
| 3130 | QualType Type) { |
| 3131 | return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3134 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| 3135 | unsigned ID) { |
| 3136 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl)); |
| 3137 | return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType()); |
| 3138 | } |
| 3139 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3140 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3141 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3142 | const DeclarationNameInfo &NameInfo, |
| 3143 | QualType T, TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3144 | StorageClass SC, StorageClass SCAsWritten, |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3145 | bool isInlineSpecified, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3146 | bool hasWrittenPrototype, |
| 3147 | bool isConstexprSpecified) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3148 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, |
| 3149 | T, TInfo, SC, SCAsWritten, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3150 | isInlineSpecified, |
| 3151 | isConstexprSpecified); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3152 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 3153 | return New; |
| 3154 | } |
| 3155 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3156 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3157 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl)); |
| 3158 | return new (Mem) FunctionDecl(Function, 0, SourceLocation(), |
| 3159 | DeclarationNameInfo(), QualType(), 0, |
| 3160 | SC_None, SC_None, false, false); |
| 3161 | } |
| 3162 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3163 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 3164 | return new (C) BlockDecl(DC, L); |
| 3165 | } |
| 3166 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3167 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3168 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl)); |
| 3169 | return new (Mem) BlockDecl(0, SourceLocation()); |
| 3170 | } |
| 3171 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3172 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 3173 | SourceLocation L, |
| 3174 | IdentifierInfo *Id, QualType T, |
| 3175 | Expr *E, const llvm::APSInt &V) { |
| 3176 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 3177 | } |
| 3178 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3179 | EnumConstantDecl * |
| 3180 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3181 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl)); |
| 3182 | return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0, |
| 3183 | llvm::APSInt()); |
| 3184 | } |
| 3185 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3186 | void IndirectFieldDecl::anchor() { } |
| 3187 | |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3188 | IndirectFieldDecl * |
| 3189 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 3190 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 3191 | unsigned CHS) { |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3192 | return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
| 3193 | } |
| 3194 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3195 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 3196 | unsigned ID) { |
| 3197 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl)); |
| 3198 | return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(), |
| 3199 | QualType(), 0, 0); |
| 3200 | } |
| 3201 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 3202 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 3203 | SourceLocation End = getLocation(); |
| 3204 | if (Init) |
| 3205 | End = Init->getLocEnd(); |
| 3206 | return SourceRange(getLocation(), End); |
| 3207 | } |
| 3208 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3209 | void TypeDecl::anchor() { } |
| 3210 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3211 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3212 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3213 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| 3214 | return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3217 | void TypedefNameDecl::anchor() { } |
| 3218 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3219 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3220 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl)); |
| 3221 | return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 3222 | } |
| 3223 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3224 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 3225 | SourceLocation StartLoc, |
| 3226 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3227 | TypeSourceInfo *TInfo) { |
| 3228 | return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); |
| 3229 | } |
| 3230 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3231 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3232 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl)); |
| 3233 | return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 3234 | } |
| 3235 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3236 | SourceRange TypedefDecl::getSourceRange() const { |
| 3237 | SourceLocation RangeEnd = getLocation(); |
| 3238 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 3239 | if (typeIsPostfix(TInfo->getType())) |
| 3240 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3241 | } |
| 3242 | return SourceRange(getLocStart(), RangeEnd); |
| 3243 | } |
| 3244 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3245 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 3246 | SourceLocation RangeEnd = getLocStart(); |
| 3247 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 3248 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3249 | return SourceRange(getLocStart(), RangeEnd); |
| 3250 | } |
| 3251 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3252 | void FileScopeAsmDecl::anchor() { } |
| 3253 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3254 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 3255 | StringLiteral *Str, |
| 3256 | SourceLocation AsmLoc, |
| 3257 | SourceLocation RParenLoc) { |
| 3258 | return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3259 | } |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3261 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| 3262 | unsigned ID) { |
| 3263 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl)); |
| 3264 | return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation()); |
| 3265 | } |
| 3266 | |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3267 | //===----------------------------------------------------------------------===// |
| 3268 | // ImportDecl Implementation |
| 3269 | //===----------------------------------------------------------------------===// |
| 3270 | |
| 3271 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 3272 | /// module. |
| 3273 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 3274 | unsigned Result = 1; |
| 3275 | while (Mod->Parent) { |
| 3276 | Mod = Mod->Parent; |
| 3277 | ++Result; |
| 3278 | } |
| 3279 | return Result; |
| 3280 | } |
| 3281 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3282 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3283 | Module *Imported, |
| 3284 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3285 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3286 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3287 | { |
| 3288 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 3289 | SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); |
| 3290 | memcpy(StoredLocs, IdentifierLocs.data(), |
| 3291 | IdentifierLocs.size() * sizeof(SourceLocation)); |
| 3292 | } |
| 3293 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3294 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3295 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3296 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3297 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3298 | { |
| 3299 | *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; |
| 3300 | } |
| 3301 | |
| 3302 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3303 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3304 | ArrayRef<SourceLocation> IdentifierLocs) { |
| 3305 | void *Mem = C.Allocate(sizeof(ImportDecl) + |
| 3306 | IdentifierLocs.size() * sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3307 | return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3311 | SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3312 | Module *Imported, |
| 3313 | SourceLocation EndLoc) { |
| 3314 | void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3315 | ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3316 | Import->setImplicit(); |
| 3317 | return Import; |
| 3318 | } |
| 3319 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3320 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 3321 | unsigned NumLocations) { |
| 3322 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 3323 | (sizeof(ImportDecl) + |
| 3324 | NumLocations * sizeof(SourceLocation))); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3325 | return new (Mem) ImportDecl(EmptyShell()); |
| 3326 | } |
| 3327 | |
| 3328 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 3329 | if (!ImportedAndComplete.getInt()) |
| 3330 | return ArrayRef<SourceLocation>(); |
| 3331 | |
| 3332 | const SourceLocation *StoredLocs |
| 3333 | = reinterpret_cast<const SourceLocation *>(this + 1); |
| 3334 | return ArrayRef<SourceLocation>(StoredLocs, |
| 3335 | getNumModuleIdentifiers(getImportedModule())); |
| 3336 | } |
| 3337 | |
| 3338 | SourceRange ImportDecl::getSourceRange() const { |
| 3339 | if (!ImportedAndComplete.getInt()) |
| 3340 | return SourceRange(getLocation(), |
| 3341 | *reinterpret_cast<const SourceLocation *>(this + 1)); |
| 3342 | |
| 3343 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 3344 | } |