Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 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 | // |
| 10 | // This file implements type-related semantic analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLocVisitor.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 32 | /// \brief Perform adjustment on the parameter type of a function. |
| 33 | /// |
| 34 | /// This routine adjusts the given parameter type @p T to the actual |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 36 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 37 | QualType Sema::adjustParameterType(QualType T) { |
| 38 | // C99 6.7.5.3p7: |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 39 | // A declaration of a parameter as "array of type" shall be |
| 40 | // adjusted to "qualified pointer to type", where the type |
| 41 | // qualifiers (if any) are those specified within the [ and ] of |
| 42 | // the array type derivation. |
| 43 | if (T->isArrayType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 44 | return Context.getArrayDecayedType(T); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 45 | |
| 46 | // C99 6.7.5.3p8: |
| 47 | // A declaration of a parameter as "function returning type" |
| 48 | // shall be adjusted to "pointer to function returning type", as |
| 49 | // in 6.3.2.1. |
| 50 | if (T->isFunctionType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 51 | return Context.getPointerType(T); |
| 52 | |
| 53 | return T; |
| 54 | } |
| 55 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
| 59 | /// return type because this is a omitted return type on a block literal. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 60 | static bool isOmittedBlockReturnType(const Declarator &D) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 61 | if (D.getContext() != Declarator::BlockLiteralContext || |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 62 | D.getDeclSpec().hasTypeSpecifier()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 63 | return false; |
| 64 | |
| 65 | if (D.getNumTypeObjects() == 0) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 66 | return true; // ^{ ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 67 | |
| 68 | if (D.getNumTypeObjects() == 1 && |
| 69 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 70 | return true; // ^(int X, float Y) { ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 75 | /// diagnoseBadTypeAttribute - Diagnoses a type attribute which |
| 76 | /// doesn't apply to the given type. |
| 77 | static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr, |
| 78 | QualType type) { |
| 79 | bool useInstantiationLoc = false; |
| 80 | |
| 81 | unsigned diagID = 0; |
| 82 | switch (attr.getKind()) { |
| 83 | case AttributeList::AT_objc_gc: |
| 84 | diagID = diag::warn_pointer_attribute_wrong_type; |
| 85 | useInstantiationLoc = true; |
| 86 | break; |
| 87 | |
| 88 | default: |
| 89 | // Assume everything else was a function attribute. |
| 90 | diagID = diag::warn_function_attribute_wrong_type; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | SourceLocation loc = attr.getLoc(); |
| 95 | llvm::StringRef name = attr.getName()->getName(); |
| 96 | |
| 97 | // The GC attributes are usually written with macros; special-case them. |
| 98 | if (useInstantiationLoc && loc.isMacroID() && attr.getParameterName()) { |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 99 | if (attr.getParameterName()->isStr("strong")) { |
| 100 | if (S.findMacroSpelling(loc, "__strong")) name = "__strong"; |
| 101 | } else if (attr.getParameterName()->isStr("weak")) { |
| 102 | if (S.findMacroSpelling(loc, "__weak")) name = "__weak"; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | S.Diag(loc, diagID) << name << type; |
| 107 | } |
| 108 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 109 | // objc_gc applies to Objective-C pointers or, otherwise, to the |
| 110 | // smallest available pointer type (i.e. 'void*' in 'void**'). |
| 111 | #define OBJC_POINTER_TYPE_ATTRS_CASELIST \ |
| 112 | case AttributeList::AT_objc_gc |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 113 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 114 | // Function type attributes. |
| 115 | #define FUNCTION_TYPE_ATTRS_CASELIST \ |
| 116 | case AttributeList::AT_noreturn: \ |
| 117 | case AttributeList::AT_cdecl: \ |
| 118 | case AttributeList::AT_fastcall: \ |
| 119 | case AttributeList::AT_stdcall: \ |
| 120 | case AttributeList::AT_thiscall: \ |
| 121 | case AttributeList::AT_pascal: \ |
| 122 | case AttributeList::AT_regparm |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 123 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 124 | namespace { |
| 125 | /// An object which stores processing state for the entire |
| 126 | /// GetTypeForDeclarator process. |
| 127 | class TypeProcessingState { |
| 128 | Sema &sema; |
| 129 | |
| 130 | /// The declarator being processed. |
| 131 | Declarator &declarator; |
| 132 | |
| 133 | /// The index of the declarator chunk we're currently processing. |
| 134 | /// May be the total number of valid chunks, indicating the |
| 135 | /// DeclSpec. |
| 136 | unsigned chunkIndex; |
| 137 | |
| 138 | /// Whether there are non-trivial modifications to the decl spec. |
| 139 | bool trivial; |
| 140 | |
| 141 | /// The original set of attributes on the DeclSpec. |
| 142 | llvm::SmallVector<AttributeList*, 2> savedAttrs; |
| 143 | |
| 144 | /// A list of attributes to diagnose the uselessness of when the |
| 145 | /// processing is complete. |
| 146 | llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs; |
| 147 | |
| 148 | public: |
| 149 | TypeProcessingState(Sema &sema, Declarator &declarator) |
| 150 | : sema(sema), declarator(declarator), |
| 151 | chunkIndex(declarator.getNumTypeObjects()), |
| 152 | trivial(true) {} |
| 153 | |
| 154 | Sema &getSema() const { |
| 155 | return sema; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 156 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 157 | |
| 158 | Declarator &getDeclarator() const { |
| 159 | return declarator; |
| 160 | } |
| 161 | |
| 162 | unsigned getCurrentChunkIndex() const { |
| 163 | return chunkIndex; |
| 164 | } |
| 165 | |
| 166 | void setCurrentChunkIndex(unsigned idx) { |
| 167 | assert(idx <= declarator.getNumTypeObjects()); |
| 168 | chunkIndex = idx; |
| 169 | } |
| 170 | |
| 171 | AttributeList *&getCurrentAttrListRef() const { |
| 172 | assert(chunkIndex <= declarator.getNumTypeObjects()); |
| 173 | if (chunkIndex == declarator.getNumTypeObjects()) |
| 174 | return getMutableDeclSpec().getAttributes().getListRef(); |
| 175 | return declarator.getTypeObject(chunkIndex).getAttrListRef(); |
| 176 | } |
| 177 | |
| 178 | /// Save the current set of attributes on the DeclSpec. |
| 179 | void saveDeclSpecAttrs() { |
| 180 | // Don't try to save them multiple times. |
| 181 | if (!savedAttrs.empty()) return; |
| 182 | |
| 183 | DeclSpec &spec = getMutableDeclSpec(); |
| 184 | for (AttributeList *attr = spec.getAttributes().getList(); attr; |
| 185 | attr = attr->getNext()) |
| 186 | savedAttrs.push_back(attr); |
| 187 | trivial &= savedAttrs.empty(); |
| 188 | } |
| 189 | |
| 190 | /// Record that we had nowhere to put the given type attribute. |
| 191 | /// We will diagnose such attributes later. |
| 192 | void addIgnoredTypeAttr(AttributeList &attr) { |
| 193 | ignoredTypeAttrs.push_back(&attr); |
| 194 | } |
| 195 | |
| 196 | /// Diagnose all the ignored type attributes, given that the |
| 197 | /// declarator worked out to the given type. |
| 198 | void diagnoseIgnoredTypeAttrs(QualType type) const { |
| 199 | for (llvm::SmallVectorImpl<AttributeList*>::const_iterator |
| 200 | i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end(); |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 201 | i != e; ++i) |
| 202 | diagnoseBadTypeAttribute(getSema(), **i, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | ~TypeProcessingState() { |
| 206 | if (trivial) return; |
| 207 | |
| 208 | restoreDeclSpecAttrs(); |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | DeclSpec &getMutableDeclSpec() const { |
| 213 | return const_cast<DeclSpec&>(declarator.getDeclSpec()); |
| 214 | } |
| 215 | |
| 216 | void restoreDeclSpecAttrs() { |
| 217 | assert(!savedAttrs.empty()); |
| 218 | getMutableDeclSpec().getAttributes().set(savedAttrs[0]); |
| 219 | for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i) |
| 220 | savedAttrs[i]->setNext(savedAttrs[i+1]); |
| 221 | savedAttrs.back()->setNext(0); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | /// Basically std::pair except that we really want to avoid an |
| 226 | /// implicit operator= for safety concerns. It's also a minor |
| 227 | /// link-time optimization for this to be a private type. |
| 228 | struct AttrAndList { |
| 229 | /// The attribute. |
| 230 | AttributeList &first; |
| 231 | |
| 232 | /// The head of the list the attribute is currently in. |
| 233 | AttributeList *&second; |
| 234 | |
| 235 | AttrAndList(AttributeList &attr, AttributeList *&head) |
| 236 | : first(attr), second(head) {} |
| 237 | }; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 238 | } |
| 239 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 240 | namespace llvm { |
| 241 | template <> struct isPodLike<AttrAndList> { |
| 242 | static const bool value = true; |
| 243 | }; |
| 244 | } |
| 245 | |
| 246 | static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) { |
| 247 | attr.setNext(head); |
| 248 | head = &attr; |
| 249 | } |
| 250 | |
| 251 | static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) { |
| 252 | if (head == &attr) { |
| 253 | head = attr.getNext(); |
| 254 | return; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 255 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 256 | |
| 257 | AttributeList *cur = head; |
| 258 | while (true) { |
| 259 | assert(cur && cur->getNext() && "ran out of attrs?"); |
| 260 | if (cur->getNext() == &attr) { |
| 261 | cur->setNext(attr.getNext()); |
| 262 | return; |
| 263 | } |
| 264 | cur = cur->getNext(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void moveAttrFromListToList(AttributeList &attr, |
| 269 | AttributeList *&fromList, |
| 270 | AttributeList *&toList) { |
| 271 | spliceAttrOutOfList(attr, fromList); |
| 272 | spliceAttrIntoList(attr, toList); |
| 273 | } |
| 274 | |
| 275 | static void processTypeAttrs(TypeProcessingState &state, |
| 276 | QualType &type, bool isDeclSpec, |
| 277 | AttributeList *attrs); |
| 278 | |
| 279 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 280 | AttributeList &attr, |
| 281 | QualType &type); |
| 282 | |
| 283 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 284 | AttributeList &attr, QualType &type); |
| 285 | |
| 286 | static bool handleObjCPointerTypeAttr(TypeProcessingState &state, |
| 287 | AttributeList &attr, QualType &type) { |
| 288 | // Right now, we have exactly one of these attributes: objc_gc. |
| 289 | assert(attr.getKind() == AttributeList::AT_objc_gc); |
| 290 | return handleObjCGCTypeAttr(state, attr, type); |
| 291 | } |
| 292 | |
| 293 | /// Given that an objc_gc attribute was written somewhere on a |
| 294 | /// declaration *other* than on the declarator itself (for which, use |
| 295 | /// distributeObjCPointerTypeAttrFromDeclarator), and given that it |
| 296 | /// didn't apply in whatever position it was written in, try to move |
| 297 | /// it to a more appropriate position. |
| 298 | static void distributeObjCPointerTypeAttr(TypeProcessingState &state, |
| 299 | AttributeList &attr, |
| 300 | QualType type) { |
| 301 | Declarator &declarator = state.getDeclarator(); |
| 302 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 303 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 304 | switch (chunk.Kind) { |
| 305 | case DeclaratorChunk::Pointer: |
| 306 | case DeclaratorChunk::BlockPointer: |
| 307 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 308 | chunk.getAttrListRef()); |
| 309 | return; |
| 310 | |
| 311 | case DeclaratorChunk::Paren: |
| 312 | case DeclaratorChunk::Array: |
| 313 | continue; |
| 314 | |
| 315 | // Don't walk through these. |
| 316 | case DeclaratorChunk::Reference: |
| 317 | case DeclaratorChunk::Function: |
| 318 | case DeclaratorChunk::MemberPointer: |
| 319 | goto error; |
| 320 | } |
| 321 | } |
| 322 | error: |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 323 | |
| 324 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /// Distribute an objc_gc type attribute that was written on the |
| 328 | /// declarator. |
| 329 | static void |
| 330 | distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state, |
| 331 | AttributeList &attr, |
| 332 | QualType &declSpecType) { |
| 333 | Declarator &declarator = state.getDeclarator(); |
| 334 | |
| 335 | // objc_gc goes on the innermost pointer to something that's not a |
| 336 | // pointer. |
| 337 | unsigned innermost = -1U; |
| 338 | bool considerDeclSpec = true; |
| 339 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 340 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 341 | switch (chunk.Kind) { |
| 342 | case DeclaratorChunk::Pointer: |
| 343 | case DeclaratorChunk::BlockPointer: |
| 344 | innermost = i; |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 345 | continue; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 346 | |
| 347 | case DeclaratorChunk::Reference: |
| 348 | case DeclaratorChunk::MemberPointer: |
| 349 | case DeclaratorChunk::Paren: |
| 350 | case DeclaratorChunk::Array: |
| 351 | continue; |
| 352 | |
| 353 | case DeclaratorChunk::Function: |
| 354 | considerDeclSpec = false; |
| 355 | goto done; |
| 356 | } |
| 357 | } |
| 358 | done: |
| 359 | |
| 360 | // That might actually be the decl spec if we weren't blocked by |
| 361 | // anything in the declarator. |
| 362 | if (considerDeclSpec) { |
| 363 | if (handleObjCPointerTypeAttr(state, attr, declSpecType)) |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | // Otherwise, if we found an appropriate chunk, splice the attribute |
| 368 | // into it. |
| 369 | if (innermost != -1U) { |
| 370 | moveAttrFromListToList(attr, declarator.getAttrListRef(), |
| 371 | declarator.getTypeObject(innermost).getAttrListRef()); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Otherwise, diagnose when we're done building the type. |
| 376 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 377 | state.addIgnoredTypeAttr(attr); |
| 378 | } |
| 379 | |
| 380 | /// A function type attribute was written somewhere in a declaration |
| 381 | /// *other* than on the declarator itself or in the decl spec. Given |
| 382 | /// that it didn't apply in whatever position it was written in, try |
| 383 | /// to move it to a more appropriate position. |
| 384 | static void distributeFunctionTypeAttr(TypeProcessingState &state, |
| 385 | AttributeList &attr, |
| 386 | QualType type) { |
| 387 | Declarator &declarator = state.getDeclarator(); |
| 388 | |
| 389 | // Try to push the attribute from the return type of a function to |
| 390 | // the function itself. |
| 391 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 392 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 393 | switch (chunk.Kind) { |
| 394 | case DeclaratorChunk::Function: |
| 395 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 396 | chunk.getAttrListRef()); |
| 397 | return; |
| 398 | |
| 399 | case DeclaratorChunk::Paren: |
| 400 | case DeclaratorChunk::Pointer: |
| 401 | case DeclaratorChunk::BlockPointer: |
| 402 | case DeclaratorChunk::Array: |
| 403 | case DeclaratorChunk::Reference: |
| 404 | case DeclaratorChunk::MemberPointer: |
| 405 | continue; |
| 406 | } |
| 407 | } |
| 408 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 409 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /// Try to distribute a function type attribute to the innermost |
| 413 | /// function chunk or type. Returns true if the attribute was |
| 414 | /// distributed, false if no location was found. |
| 415 | static bool |
| 416 | distributeFunctionTypeAttrToInnermost(TypeProcessingState &state, |
| 417 | AttributeList &attr, |
| 418 | AttributeList *&attrList, |
| 419 | QualType &declSpecType) { |
| 420 | Declarator &declarator = state.getDeclarator(); |
| 421 | |
| 422 | // Put it on the innermost function chunk, if there is one. |
| 423 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 424 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 425 | if (chunk.Kind != DeclaratorChunk::Function) continue; |
| 426 | |
| 427 | moveAttrFromListToList(attr, attrList, chunk.getAttrListRef()); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | return handleFunctionTypeAttr(state, attr, declSpecType); |
| 432 | } |
| 433 | |
| 434 | /// A function type attribute was written in the decl spec. Try to |
| 435 | /// apply it somewhere. |
| 436 | static void |
| 437 | distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state, |
| 438 | AttributeList &attr, |
| 439 | QualType &declSpecType) { |
| 440 | state.saveDeclSpecAttrs(); |
| 441 | |
| 442 | // Try to distribute to the innermost. |
| 443 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 444 | state.getCurrentAttrListRef(), |
| 445 | declSpecType)) |
| 446 | return; |
| 447 | |
| 448 | // If that failed, diagnose the bad attribute when the declarator is |
| 449 | // fully built. |
| 450 | state.addIgnoredTypeAttr(attr); |
| 451 | } |
| 452 | |
| 453 | /// A function type attribute was written on the declarator. Try to |
| 454 | /// apply it somewhere. |
| 455 | static void |
| 456 | distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state, |
| 457 | AttributeList &attr, |
| 458 | QualType &declSpecType) { |
| 459 | Declarator &declarator = state.getDeclarator(); |
| 460 | |
| 461 | // Try to distribute to the innermost. |
| 462 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 463 | declarator.getAttrListRef(), |
| 464 | declSpecType)) |
| 465 | return; |
| 466 | |
| 467 | // If that failed, diagnose the bad attribute when the declarator is |
| 468 | // fully built. |
| 469 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 470 | state.addIgnoredTypeAttr(attr); |
| 471 | } |
| 472 | |
| 473 | /// \brief Given that there are attributes written on the declarator |
| 474 | /// itself, try to distribute any type attributes to the appropriate |
| 475 | /// declarator chunk. |
| 476 | /// |
| 477 | /// These are attributes like the following: |
| 478 | /// int f ATTR; |
| 479 | /// int (f ATTR)(); |
| 480 | /// but not necessarily this: |
| 481 | /// int f() ATTR; |
| 482 | static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state, |
| 483 | QualType &declSpecType) { |
| 484 | // Collect all the type attributes from the declarator itself. |
| 485 | assert(state.getDeclarator().getAttributes() && "declarator has no attrs!"); |
| 486 | AttributeList *attr = state.getDeclarator().getAttributes(); |
| 487 | AttributeList *next; |
| 488 | do { |
| 489 | next = attr->getNext(); |
| 490 | |
| 491 | switch (attr->getKind()) { |
| 492 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 493 | distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 494 | break; |
| 495 | |
| 496 | FUNCTION_TYPE_ATTRS_CASELIST: |
| 497 | distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 498 | break; |
| 499 | |
| 500 | default: |
| 501 | break; |
| 502 | } |
| 503 | } while ((attr = next)); |
| 504 | } |
| 505 | |
| 506 | /// Add a synthetic '()' to a block-literal declarator if it is |
| 507 | /// required, given the return type. |
| 508 | static void maybeSynthesizeBlockSignature(TypeProcessingState &state, |
| 509 | QualType declSpecType) { |
| 510 | Declarator &declarator = state.getDeclarator(); |
| 511 | |
| 512 | // First, check whether the declarator would produce a function, |
| 513 | // i.e. whether the innermost semantic chunk is a function. |
| 514 | if (declarator.isFunctionDeclarator()) { |
| 515 | // If so, make that declarator a prototyped declarator. |
| 516 | declarator.getFunctionTypeInfo().hasPrototype = true; |
| 517 | return; |
| 518 | } |
| 519 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 520 | // If there are any type objects, the type as written won't name a |
| 521 | // function, regardless of the decl spec type. This is because a |
| 522 | // block signature declarator is always an abstract-declarator, and |
| 523 | // abstract-declarators can't just be parentheses chunks. Therefore |
| 524 | // we need to build a function chunk unless there are no type |
| 525 | // objects and the decl spec type is a function. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 526 | if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType()) |
| 527 | return; |
| 528 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 529 | // Note that there *are* cases with invalid declarators where |
| 530 | // declarators consist solely of parentheses. In general, these |
| 531 | // occur only in failed efforts to make function declarators, so |
| 532 | // faking up the function chunk is still the right thing to do. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 533 | |
| 534 | // Otherwise, we need to fake up a function declarator. |
| 535 | SourceLocation loc = declarator.getSourceRange().getBegin(); |
| 536 | |
| 537 | // ...and *prepend* it to the declarator. |
| 538 | declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction( |
| 539 | ParsedAttributes(), |
| 540 | /*proto*/ true, |
| 541 | /*variadic*/ false, SourceLocation(), |
| 542 | /*args*/ 0, 0, |
| 543 | /*type quals*/ 0, |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 544 | /*ref-qualifier*/true, SourceLocation(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 545 | /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0, |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 546 | /*parens*/ loc, loc, |
| 547 | declarator)); |
| 548 | |
| 549 | // For consistency, make sure the state still has us as processing |
| 550 | // the decl spec. |
| 551 | assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1); |
| 552 | state.setCurrentChunkIndex(declarator.getNumTypeObjects()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 555 | /// \brief Convert the specified declspec to the appropriate type |
| 556 | /// object. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 557 | /// \param D the declarator containing the declaration specifier. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 558 | /// \returns The type described by the declaration specifiers. This function |
| 559 | /// never returns null. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 560 | static QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 562 | // checking. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 563 | |
| 564 | Declarator &declarator = state.getDeclarator(); |
| 565 | const DeclSpec &DS = declarator.getDeclSpec(); |
| 566 | SourceLocation DeclLoc = declarator.getIdentifierLoc(); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 567 | if (DeclLoc.isInvalid()) |
| 568 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 569 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 570 | ASTContext &Context = S.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 572 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 574 | case DeclSpec::TST_void: |
| 575 | Result = Context.VoidTy; |
| 576 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | case DeclSpec::TST_char: |
| 578 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 579 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 581 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | else { |
| 583 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 584 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 585 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 587 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 588 | case DeclSpec::TST_wchar: |
| 589 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 590 | Result = Context.WCharTy; |
| 591 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 592 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 593 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 594 | Result = Context.getSignedWCharType(); |
| 595 | } else { |
| 596 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 597 | "Unknown TSS value"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 598 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 599 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 600 | Result = Context.getUnsignedWCharType(); |
| 601 | } |
| 602 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 603 | case DeclSpec::TST_char16: |
| 604 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 605 | "Unknown TSS value"); |
| 606 | Result = Context.Char16Ty; |
| 607 | break; |
| 608 | case DeclSpec::TST_char32: |
| 609 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 610 | "Unknown TSS value"); |
| 611 | Result = Context.Char32Ty; |
| 612 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 613 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 614 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 615 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 616 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 617 | (ObjCProtocolDecl**)PQ, |
| 618 | DS.getNumProtocolQualifiers()); |
| 619 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 622 | |
| 623 | // If this is a missing declspec in a block literal return context, then it |
| 624 | // is inferred from the return statements inside the block. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 625 | if (isOmittedBlockReturnType(declarator)) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 626 | Result = Context.DependentTy; |
| 627 | break; |
| 628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 630 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 631 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 632 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 633 | // Note that the one exception to this is function definitions, which are |
| 634 | // allowed to be completely missing a declspec. This is handled in the |
| 635 | // parser already though by it pretending to have seen an 'int' in this |
| 636 | // case. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 637 | if (S.getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 638 | // In C89 mode, we only warn if there is a completely missing declspec |
| 639 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 640 | if (DS.isEmpty()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 641 | S.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 642 | << DS.getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 643 | << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 644 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 645 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 646 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 647 | // "At least one type specifier shall be given in the declaration |
| 648 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 649 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 650 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 651 | if (S.getLangOptions().CPlusPlus && |
| 652 | !S.getLangOptions().Microsoft) { |
| 653 | S.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 654 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 656 | // When this occurs in C++ code, often something is very broken with the |
| 657 | // value being declared, poison it as invalid so we don't get chains of |
| 658 | // errors. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 659 | declarator.setInvalidType(true); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 660 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 661 | S.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 662 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 663 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
| 666 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 667 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 669 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 670 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 671 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 672 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 673 | case DeclSpec::TSW_longlong: |
| 674 | Result = Context.LongLongTy; |
| 675 | |
| 676 | // long long is a C99 feature. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 677 | if (!S.getLangOptions().C99 && |
| 678 | !S.getLangOptions().CPlusPlus0x) |
| 679 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 680 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 681 | } |
| 682 | } else { |
| 683 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 684 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 685 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 686 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 687 | case DeclSpec::TSW_longlong: |
| 688 | Result = Context.UnsignedLongLongTy; |
| 689 | |
| 690 | // long long is a C99 feature. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 691 | if (!S.getLangOptions().C99 && |
| 692 | !S.getLangOptions().CPlusPlus0x) |
| 693 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 694 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 697 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 698 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 699 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 700 | case DeclSpec::TST_double: |
| 701 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 702 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 703 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 704 | Result = Context.DoubleTy; |
Peter Collingbourne | 39d3e7a | 2011-02-15 19:46:23 +0000 | [diff] [blame] | 705 | |
| 706 | if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) { |
| 707 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64); |
| 708 | declarator.setInvalidType(true); |
| 709 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 710 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 711 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 713 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 714 | case DeclSpec::TST_decimal128: // _Decimal128 |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 715 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 716 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 717 | declarator.setInvalidType(true); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 718 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 719 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | case DeclSpec::TST_enum: |
| 721 | case DeclSpec::TST_union: |
| 722 | case DeclSpec::TST_struct: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 723 | TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl()); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 724 | if (!D) { |
| 725 | // This can happen in C++ with ambiguous lookups. |
| 726 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 727 | declarator.setInvalidType(true); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 731 | // If the type is deprecated or unavailable, diagnose it. |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 732 | S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc()); |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 734 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 735 | DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); |
| 736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | // TypeQuals handled by caller. |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 738 | Result = Context.getTypeDeclType(D); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 739 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 740 | // In both C and C++, make an ElaboratedType. |
| 741 | ElaboratedTypeKeyword Keyword |
| 742 | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 743 | Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result); |
| 744 | |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 745 | if (D->isInvalidDecl()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 746 | declarator.setInvalidType(true); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 747 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 749 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 751 | DS.getTypeSpecSign() == 0 && |
| 752 | "Can't handle qualifiers on typedef names yet!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 753 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 754 | if (Result.isNull()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 755 | declarator.setInvalidType(true); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 756 | else if (DeclSpec::ProtocolQualifierListTy PQ |
| 757 | = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 758 | if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) { |
| 759 | // Silently drop any existing protocol qualifiers. |
| 760 | // TODO: determine whether that's the right thing to do. |
| 761 | if (ObjT->getNumProtocols()) |
| 762 | Result = ObjT->getBaseType(); |
| 763 | |
| 764 | if (DS.getNumProtocolQualifiers()) |
| 765 | Result = Context.getObjCObjectType(Result, |
| 766 | (ObjCProtocolDecl**) PQ, |
| 767 | DS.getNumProtocolQualifiers()); |
| 768 | } else if (Result->isObjCIdType()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 769 | // id<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 770 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 771 | (ObjCProtocolDecl**) PQ, |
| 772 | DS.getNumProtocolQualifiers()); |
| 773 | Result = Context.getObjCObjectPointerType(Result); |
| 774 | } else if (Result->isObjCClassType()) { |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 775 | // Class<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 776 | Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, |
| 777 | (ObjCProtocolDecl**) PQ, |
| 778 | DS.getNumProtocolQualifiers()); |
| 779 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 780 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 781 | S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 782 | << DS.getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 783 | declarator.setInvalidType(true); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 784 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 787 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 788 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 790 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 791 | // FIXME: Preserve type source info. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 792 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 793 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Fariborz Jahanian | 730e175 | 2010-10-06 17:00:02 +0000 | [diff] [blame] | 794 | if (!Result->isDependentType()) |
| 795 | if (const TagType *TT = Result->getAs<TagType>()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 796 | S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc()); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 797 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 798 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 799 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 800 | case DeclSpec::TST_typeofExpr: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 801 | Expr *E = DS.getRepAsExpr(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 802 | assert(E && "Didn't get an expression for typeof?"); |
| 803 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 804 | Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 805 | if (Result.isNull()) { |
| 806 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 807 | declarator.setInvalidType(true); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 808 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 809 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 810 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 811 | case DeclSpec::TST_decltype: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 812 | Expr *E = DS.getRepAsExpr(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 813 | assert(E && "Didn't get an expression for decltype?"); |
| 814 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 815 | Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc()); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 816 | if (Result.isNull()) { |
| 817 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 818 | declarator.setInvalidType(true); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 819 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 820 | break; |
| 821 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 822 | case DeclSpec::TST_auto: { |
| 823 | // TypeQuals handled by caller. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 824 | Result = Context.getAutoType(QualType()); |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 825 | break; |
| 826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 828 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 829 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 830 | declarator.setInvalidType(true); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 831 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 834 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 835 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 836 | if (S.getLangOptions().Freestanding) |
| 837 | S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 838 | Result = Context.getComplexType(Result); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 839 | } else if (DS.isTypeAltiVecVector()) { |
| 840 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
| 841 | assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 842 | VectorType::VectorKind VecKind = VectorType::AltiVecVector; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 843 | if (DS.isTypeAltiVecPixel()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 844 | VecKind = VectorType::AltiVecPixel; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 845 | else if (DS.isTypeAltiVecBool()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 846 | VecKind = VectorType::AltiVecBool; |
| 847 | Result = Context.getVectorType(Result, 128/typeSize, VecKind); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 848 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Argyrios Kyrtzidis | 47423bd | 2010-09-23 09:40:31 +0000 | [diff] [blame] | 850 | // FIXME: Imaginary. |
| 851 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 852 | S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 854 | // Before we process any type attributes, synthesize a block literal |
| 855 | // function declarator if necessary. |
| 856 | if (declarator.getContext() == Declarator::BlockLiteralContext) |
| 857 | maybeSynthesizeBlockSignature(state, Result); |
| 858 | |
| 859 | // Apply any type attributes from the decl spec. This may cause the |
| 860 | // list of type attributes to be temporarily saved while the type |
| 861 | // attributes are pushed around. |
| 862 | if (AttributeList *attrs = DS.getAttributes().getList()) |
| 863 | processTypeAttrs(state, Result, true, attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 865 | // Apply const/volatile/restrict qualifiers to T. |
| 866 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 867 | |
| 868 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 869 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 870 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 871 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Fariborz Jahanian | 2b5ff1a | 2009-12-07 18:08:58 +0000 | [diff] [blame] | 872 | if (Result->isAnyPointerType() || Result->isReferenceType()) { |
| 873 | QualType EltTy; |
| 874 | if (Result->isObjCObjectPointerType()) |
| 875 | EltTy = Result; |
| 876 | else |
| 877 | EltTy = Result->isPointerType() ? |
| 878 | Result->getAs<PointerType>()->getPointeeType() : |
| 879 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 881 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 882 | // incomplete type. |
| 883 | if (!EltTy->isIncompleteOrObjectType()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 884 | S.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 885 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 886 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 887 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 888 | } |
| 889 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 890 | S.Diag(DS.getRestrictSpecLoc(), |
| 891 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 892 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 893 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 894 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 897 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 898 | // of a function type includes any type qualifiers, the behavior is |
| 899 | // undefined." |
| 900 | if (Result->isFunctionType() && TypeQuals) { |
| 901 | // Get some location to point at, either the C or V location. |
| 902 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 903 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 904 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 905 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 906 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 907 | else { |
| 908 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 909 | "Has CVR quals but not C, V, or R?"); |
| 910 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 911 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 912 | S.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 913 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 916 | // C++ [dcl.ref]p1: |
| 917 | // Cv-qualified references are ill-formed except when the |
| 918 | // cv-qualifiers are introduced through the use of a typedef |
| 919 | // (7.1.3) or of a template type argument (14.3), in which |
| 920 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 921 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 922 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 923 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 924 | TypeQuals &= ~DeclSpec::TQ_const; |
| 925 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | } |
| 927 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 928 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 929 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 930 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 931 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 932 | return Result; |
| 933 | } |
| 934 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 935 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 936 | if (Entity) |
| 937 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 939 | return "type name"; |
| 940 | } |
| 941 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 942 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
| 943 | Qualifiers Qs) { |
| 944 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 945 | // object or incomplete types shall not be restrict-qualified." |
| 946 | if (Qs.hasRestrict()) { |
| 947 | unsigned DiagID = 0; |
| 948 | QualType ProblemTy; |
| 949 | |
| 950 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
| 951 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) { |
| 952 | if (!RTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 953 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 954 | ProblemTy = T->getAs<ReferenceType>()->getPointeeType(); |
| 955 | } |
| 956 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 957 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 958 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 959 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 960 | } |
| 961 | } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) { |
| 962 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 963 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 964 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 965 | } |
| 966 | } else if (!Ty->isDependentType()) { |
| 967 | // FIXME: this deserves a proper diagnostic |
| 968 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 969 | ProblemTy = T; |
| 970 | } |
| 971 | |
| 972 | if (DiagID) { |
| 973 | Diag(Loc, DiagID) << ProblemTy; |
| 974 | Qs.removeRestrict(); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | return Context.getQualifiedType(T, Qs); |
| 979 | } |
| 980 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 981 | /// \brief Build a paren type including \p T. |
| 982 | QualType Sema::BuildParenType(QualType T) { |
| 983 | return Context.getParenType(T); |
| 984 | } |
| 985 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 986 | /// \brief Build a pointer type. |
| 987 | /// |
| 988 | /// \param T The type to which we'll be building a pointer. |
| 989 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 990 | /// \param Loc The location of the entity whose type involves this |
| 991 | /// pointer type or, if there is no such entity, the location of the |
| 992 | /// type that will have pointer type. |
| 993 | /// |
| 994 | /// \param Entity The name of the entity that involves the pointer |
| 995 | /// type, if known. |
| 996 | /// |
| 997 | /// \returns A suitable pointer type, if there are no |
| 998 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 999 | QualType Sema::BuildPointerType(QualType T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1000 | SourceLocation Loc, DeclarationName Entity) { |
| 1001 | if (T->isReferenceType()) { |
| 1002 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 1003 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1004 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1005 | return QualType(); |
| 1006 | } |
| 1007 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1008 | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1010 | // Build the pointer type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1011 | return Context.getPointerType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /// \brief Build a reference type. |
| 1015 | /// |
| 1016 | /// \param T The type to which we'll be building a reference. |
| 1017 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1018 | /// \param Loc The location of the entity whose type involves this |
| 1019 | /// reference type or, if there is no such entity, the location of the |
| 1020 | /// type that will have reference type. |
| 1021 | /// |
| 1022 | /// \param Entity The name of the entity that involves the reference |
| 1023 | /// type, if known. |
| 1024 | /// |
| 1025 | /// \returns A suitable reference type, if there are no |
| 1026 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1027 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1028 | SourceLocation Loc, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1029 | DeclarationName Entity) { |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1030 | // C++0x [dcl.ref]p6: |
| 1031 | // If a typedef (7.1.3), a type template-parameter (14.3.1), or a |
| 1032 | // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a |
| 1033 | // type T, an attempt to create the type "lvalue reference to cv TR" creates |
| 1034 | // the type "lvalue reference to T", while an attempt to create the type |
| 1035 | // "rvalue reference to cv TR" creates the type TR. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1036 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 1037 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1038 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1039 | // |
| 1040 | // According to C++ DR 106, references to references are only |
| 1041 | // diagnosed when they are written directly (e.g., "int & &"), |
| 1042 | // but not when they happen via a typedef: |
| 1043 | // |
| 1044 | // typedef int& intref; |
| 1045 | // typedef intref& intref2; |
| 1046 | // |
| 1047 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 1048 | // references are written directly; here, we handle the |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1049 | // collapsing of references-to-references as described in C++0x. |
| 1050 | // DR 106 and 540 introduce reference-collapsing into C++98/03. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1051 | |
| 1052 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1053 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1054 | // is ill-formed. |
| 1055 | if (T->isVoidType()) { |
| 1056 | Diag(Loc, diag::err_reference_to_void); |
| 1057 | return QualType(); |
| 1058 | } |
| 1059 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1060 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1061 | if (LValueRef) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1062 | return Context.getLValueReferenceType(T, SpelledAsLValue); |
| 1063 | return Context.getRValueReferenceType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /// \brief Build an array type. |
| 1067 | /// |
| 1068 | /// \param T The type of each element in the array. |
| 1069 | /// |
| 1070 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | /// |
| 1072 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1073 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1074 | /// \param Loc The location of the entity whose type involves this |
| 1075 | /// array type or, if there is no such entity, the location of the |
| 1076 | /// type that will have array type. |
| 1077 | /// |
| 1078 | /// \param Entity The name of the entity that involves the array |
| 1079 | /// type, if known. |
| 1080 | /// |
| 1081 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 1082 | /// returns a NULL type. |
| 1083 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 1084 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1085 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1087 | SourceLocation Loc = Brackets.getBegin(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1088 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1089 | // C++ [dcl.array]p1: |
| 1090 | // T is called the array element type; this type shall not be a reference |
| 1091 | // type, the (possibly cv-qualified) type void, a function type or an |
| 1092 | // abstract class type. |
| 1093 | // |
| 1094 | // Note: function types are handled in the common path with C. |
| 1095 | if (T->isReferenceType()) { |
| 1096 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 1097 | << getPrintableNameForEntity(Entity) << T; |
| 1098 | return QualType(); |
| 1099 | } |
| 1100 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1101 | if (T->isVoidType()) { |
| 1102 | Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; |
| 1103 | return QualType(); |
| 1104 | } |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (RequireNonAbstractType(Brackets.getBegin(), T, |
| 1107 | diag::err_array_of_abstract_type)) |
| 1108 | return QualType(); |
| 1109 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1110 | } else { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1111 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 1112 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1113 | if (RequireCompleteType(Loc, T, |
| 1114 | diag::err_illegal_decl_array_incomplete_type)) |
| 1115 | return QualType(); |
| 1116 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (T->isFunctionType()) { |
| 1119 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1120 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1121 | return QualType(); |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1124 | if (T->getContainedAutoType()) { |
| 1125 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
| 1126 | << getPrintableNameForEntity(Entity) << T; |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 1127 | return QualType(); |
| 1128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1130 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1131 | // If the element type is a struct or union that contains a variadic |
| 1132 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 1133 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 1134 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1135 | } else if (T->isObjCObjectType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 1136 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 1137 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1140 | // Do lvalue-to-rvalue conversions on the array size expression. |
| 1141 | if (ArraySize && !ArraySize->isRValue()) |
| 1142 | DefaultLvalueConversion(ArraySize); |
| 1143 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1144 | // C99 6.7.5.2p1: The size expression shall have integer type. |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1145 | // TODO: in theory, if we were insane, we could allow contextual |
| 1146 | // conversions to integer type here. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1147 | if (ArraySize && !ArraySize->isTypeDependent() && |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1148 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1149 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 1150 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1151 | return QualType(); |
| 1152 | } |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1153 | llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1154 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1155 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1156 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1157 | else |
| 1158 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1159 | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1160 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1161 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1162 | (!T->isDependentType() && !T->isIncompleteType() && |
| 1163 | !T->isConstantSizeType())) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1164 | // Per C99, a variable array is an array with either a non-constant |
| 1165 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1166 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1167 | } else { |
| 1168 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 1169 | // have a value greater than zero. |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1170 | if (ConstVal.isSigned() && ConstVal.isNegative()) { |
Chandler Carruth | b2b5cc0 | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 1171 | if (Entity) |
| 1172 | Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size) |
| 1173 | << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange(); |
| 1174 | else |
| 1175 | Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size) |
| 1176 | << ArraySize->getSourceRange(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1177 | return QualType(); |
| 1178 | } |
| 1179 | if (ConstVal == 0) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1180 | // GCC accepts zero sized static arrays. We allow them when |
| 1181 | // we're not in a SFINAE context. |
| 1182 | Diag(ArraySize->getLocStart(), |
| 1183 | isSFINAEContext()? diag::err_typecheck_zero_array_size |
| 1184 | : diag::ext_typecheck_zero_array_size) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1185 | << ArraySize->getSourceRange(); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1186 | } else if (!T->isDependentType() && !T->isVariablyModifiedType() && |
| 1187 | !T->isIncompleteType()) { |
| 1188 | // Is the array too large? |
| 1189 | unsigned ActiveSizeBits |
| 1190 | = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal); |
| 1191 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1192 | Diag(ArraySize->getLocStart(), diag::err_array_too_large) |
| 1193 | << ConstVal.toString(10) |
| 1194 | << ArraySize->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | } |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1196 | |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 1197 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1198 | } |
David Chisnall | af40776 | 2010-01-11 23:08:08 +0000 | [diff] [blame] | 1199 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 1200 | if (!getLangOptions().C99) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1201 | if (T->isVariableArrayType()) { |
| 1202 | // Prohibit the use of non-POD types in VLAs. |
Douglas Gregor | 204ce17 | 2010-05-24 20:42:30 +0000 | [diff] [blame] | 1203 | if (!T->isDependentType() && |
| 1204 | !Context.getBaseElementType(T)->isPODType()) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1205 | Diag(Loc, diag::err_vla_non_pod) |
| 1206 | << Context.getBaseElementType(T); |
| 1207 | return QualType(); |
| 1208 | } |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 1209 | // Prohibit the use of VLAs during template argument deduction. |
| 1210 | else if (isSFINAEContext()) { |
| 1211 | Diag(Loc, diag::err_vla_in_sfinae); |
| 1212 | return QualType(); |
| 1213 | } |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1214 | // Just extwarn about VLAs. |
| 1215 | else |
| 1216 | Diag(Loc, diag::ext_vla); |
| 1217 | } else if (ASM != ArrayType::Normal || Quals != 0) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1218 | Diag(Loc, |
| 1219 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
| 1220 | : diag::ext_c99_array_usage); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | return T; |
| 1224 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1225 | |
| 1226 | /// \brief Build an ext-vector type. |
| 1227 | /// |
| 1228 | /// Run the required checks for the extended vector type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1229 | QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1230 | SourceLocation AttrLoc) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1231 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1232 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1234 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 1235 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 1236 | return QualType(); |
| 1237 | } |
| 1238 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1239 | if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1240 | llvm::APSInt vecSize(32); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1241 | if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1242 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1243 | << "ext_vector_type" << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1244 | return QualType(); |
| 1245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
| 1247 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1248 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 1250 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1251 | if (vectorSize == 0) { |
| 1252 | Diag(AttrLoc, diag::err_attribute_zero_size) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1253 | << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1254 | return QualType(); |
| 1255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1257 | if (!T->isDependentType()) |
| 1258 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1261 | return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1264 | /// \brief Build a function type. |
| 1265 | /// |
| 1266 | /// This routine checks the function type according to C++ rules and |
| 1267 | /// under the assumption that the result type and parameter types have |
| 1268 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1269 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1270 | /// simpler form that is only suitable for this narrow use case. |
| 1271 | /// |
| 1272 | /// \param T The return type of the function. |
| 1273 | /// |
| 1274 | /// \param ParamTypes The parameter types of the function. This array |
| 1275 | /// will be modified to account for adjustments to the types of the |
| 1276 | /// function parameters. |
| 1277 | /// |
| 1278 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 1279 | /// |
| 1280 | /// \param Variadic Whether this is a variadic function type. |
| 1281 | /// |
| 1282 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 1283 | /// |
| 1284 | /// \param Loc The location of the entity whose type involves this |
| 1285 | /// function type or, if there is no such entity, the location of the |
| 1286 | /// type that will have function type. |
| 1287 | /// |
| 1288 | /// \param Entity The name of the entity that involves the function |
| 1289 | /// type, if known. |
| 1290 | /// |
| 1291 | /// \returns A suitable function type, if there are no |
| 1292 | /// errors. Otherwise, returns a NULL type. |
| 1293 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1295 | unsigned NumParamTypes, |
| 1296 | bool Variadic, unsigned Quals, |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1297 | RefQualifierKind RefQualifier, |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 1298 | SourceLocation Loc, DeclarationName Entity, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1299 | FunctionType::ExtInfo Info) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1300 | if (T->isArrayType() || T->isFunctionType()) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1301 | Diag(Loc, diag::err_func_returning_array_function) |
| 1302 | << T->isFunctionType() << T; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1303 | return QualType(); |
| 1304 | } |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1306 | bool Invalid = false; |
| 1307 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1308 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 1309 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1310 | Diag(Loc, diag::err_param_with_void_type); |
| 1311 | Invalid = true; |
| 1312 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1313 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1314 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (Invalid) |
| 1318 | return QualType(); |
| 1319 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1320 | FunctionProtoType::ExtProtoInfo EPI; |
| 1321 | EPI.Variadic = Variadic; |
| 1322 | EPI.TypeQuals = Quals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1323 | EPI.RefQualifier = RefQualifier; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1324 | EPI.ExtInfo = Info; |
| 1325 | |
| 1326 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1329 | /// \brief Build a member pointer type \c T Class::*. |
| 1330 | /// |
| 1331 | /// \param T the type to which the member pointer refers. |
| 1332 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1333 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1334 | /// \param Loc the location where this type begins |
| 1335 | /// \param Entity the name of the entity that will have this member pointer type |
| 1336 | /// |
| 1337 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 1338 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1340 | SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1341 | DeclarationName Entity) { |
| 1342 | // Verify that we're not building a pointer to pointer to function with |
| 1343 | // exception specification. |
| 1344 | if (CheckDistantExceptionSpec(T)) { |
| 1345 | Diag(Loc, diag::err_distant_exception_spec); |
| 1346 | |
| 1347 | // FIXME: If we're doing this as part of template instantiation, |
| 1348 | // we should return immediately. |
| 1349 | |
| 1350 | // Build the type anyway, but use the canonical type so that the |
| 1351 | // exception specifiers are stripped off. |
| 1352 | T = Context.getCanonicalType(T); |
| 1353 | } |
| 1354 | |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 1355 | // C++ 8.3.3p3: A pointer to member shall not point to ... a member |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1356 | // with reference type, or "cv void." |
| 1357 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 1358 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1359 | << (Entity? Entity.getAsString() : "type name") << T; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1360 | return QualType(); |
| 1361 | } |
| 1362 | |
| 1363 | if (T->isVoidType()) { |
| 1364 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 1365 | << (Entity? Entity.getAsString() : "type name"); |
| 1366 | return QualType(); |
| 1367 | } |
| 1368 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1369 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 1370 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 1371 | return QualType(); |
| 1372 | } |
| 1373 | |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 1374 | // In the Microsoft ABI, the class is allowed to be an incomplete |
| 1375 | // type. In such cases, the compiler makes a worst-case assumption. |
| 1376 | // We make no such assumption right now, so emit an error if the |
| 1377 | // class isn't a complete type. |
Charles Davis | 20cf717 | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 1378 | if (Context.Target.getCXXABI() == CXXABI_Microsoft && |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 1379 | RequireCompleteType(Loc, Class, diag::err_incomplete_type)) |
| 1380 | return QualType(); |
| 1381 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1382 | return Context.getMemberPointerType(T, Class.getTypePtr()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1385 | /// \brief Build a block pointer type. |
| 1386 | /// |
| 1387 | /// \param T The type to which we'll be building a block pointer. |
| 1388 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1389 | /// \param CVR The cvr-qualifiers to be applied to the block pointer type. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1390 | /// |
| 1391 | /// \param Loc The location of the entity whose type involves this |
| 1392 | /// block pointer type or, if there is no such entity, the location of the |
| 1393 | /// type that will have block pointer type. |
| 1394 | /// |
| 1395 | /// \param Entity The name of the entity that involves the block pointer |
| 1396 | /// type, if known. |
| 1397 | /// |
| 1398 | /// \returns A suitable block pointer type, if there are no |
| 1399 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1400 | QualType Sema::BuildBlockPointerType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1402 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1403 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1404 | Diag(Loc, diag::err_nonfunction_block_type); |
| 1405 | return QualType(); |
| 1406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1408 | return Context.getBlockPointerType(T); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1411 | QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { |
| 1412 | QualType QT = Ty.get(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1413 | if (QT.isNull()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1414 | if (TInfo) *TInfo = 0; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1415 | return QualType(); |
| 1416 | } |
| 1417 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1418 | TypeSourceInfo *DI = 0; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1419 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1420 | QT = LIT->getType(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1421 | DI = LIT->getTypeSourceInfo(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1424 | if (TInfo) *TInfo = DI; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1425 | return QT; |
| 1426 | } |
| 1427 | |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1428 | static void DiagnoseIgnoredQualifiers(unsigned Quals, |
| 1429 | SourceLocation ConstQualLoc, |
| 1430 | SourceLocation VolatileQualLoc, |
| 1431 | SourceLocation RestrictQualLoc, |
| 1432 | Sema& S) { |
| 1433 | std::string QualStr; |
| 1434 | unsigned NumQuals = 0; |
| 1435 | SourceLocation Loc; |
| 1436 | |
| 1437 | FixItHint ConstFixIt; |
| 1438 | FixItHint VolatileFixIt; |
| 1439 | FixItHint RestrictFixIt; |
| 1440 | |
| 1441 | // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to |
| 1442 | // find a range and grow it to encompass all the qualifiers, regardless of |
| 1443 | // the order in which they textually appear. |
| 1444 | if (Quals & Qualifiers::Const) { |
| 1445 | ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc); |
| 1446 | Loc = ConstQualLoc; |
| 1447 | ++NumQuals; |
| 1448 | QualStr = "const"; |
| 1449 | } |
| 1450 | if (Quals & Qualifiers::Volatile) { |
| 1451 | VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc); |
| 1452 | if (NumQuals == 0) { |
| 1453 | Loc = VolatileQualLoc; |
| 1454 | QualStr = "volatile"; |
| 1455 | } else { |
| 1456 | QualStr += " volatile"; |
| 1457 | } |
| 1458 | ++NumQuals; |
| 1459 | } |
| 1460 | if (Quals & Qualifiers::Restrict) { |
| 1461 | RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc); |
| 1462 | if (NumQuals == 0) { |
| 1463 | Loc = RestrictQualLoc; |
| 1464 | QualStr = "restrict"; |
| 1465 | } else { |
| 1466 | QualStr += " restrict"; |
| 1467 | } |
| 1468 | ++NumQuals; |
| 1469 | } |
| 1470 | |
| 1471 | assert(NumQuals > 0 && "No known qualifiers?"); |
| 1472 | |
| 1473 | S.Diag(Loc, diag::warn_qual_return_type) |
| 1474 | << QualStr << NumQuals |
| 1475 | << ConstFixIt << VolatileFixIt << RestrictFixIt; |
| 1476 | } |
| 1477 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1478 | /// GetTypeForDeclarator - Convert the type for the specified |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1479 | /// declarator to Type instances. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1480 | /// |
| 1481 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 1482 | /// owns the declaration of a type (e.g., the definition of a struct |
| 1483 | /// type), then *OwnedDecl will receive the owned declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1484 | /// |
| 1485 | /// The result of this call will never be null, but the associated |
| 1486 | /// type may be a null type if there's an unrecoverable error. |
| 1487 | TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1488 | TagDecl **OwnedDecl, |
| 1489 | bool AutoAllowedInTypeName) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1490 | // Determine the type of the declarator. Not all forms of declarator |
| 1491 | // have a type. |
| 1492 | QualType T; |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 1493 | TypeSourceInfo *ReturnTypeInfo = 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1494 | |
| 1495 | TypeProcessingState state(*this, D); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1496 | |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1497 | // In C++0x, deallocation functions (normal and array operator delete) |
| 1498 | // are implicitly noexcept. |
| 1499 | bool ImplicitlyNoexcept = false; |
| 1500 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1501 | switch (D.getName().getKind()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1502 | case UnqualifiedId::IK_OperatorFunctionId: |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1503 | if (getLangOptions().CPlusPlus0x) { |
| 1504 | OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator; |
| 1505 | if (OO == OO_Delete || OO == OO_Array_Delete) |
| 1506 | ImplicitlyNoexcept = true; |
| 1507 | } |
| 1508 | // Intentional fall-through. |
| 1509 | case UnqualifiedId::IK_Identifier: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1510 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1511 | case UnqualifiedId::IK_TemplateId: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1512 | T = ConvertDeclSpecToType(*this, state); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 1513 | |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1514 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1515 | TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
Abramo Bagnara | 1598797 | 2011-03-08 22:33:38 +0000 | [diff] [blame] | 1516 | // Owned declaration is embedded in declarator. |
| 1517 | Owned->setEmbeddedInDeclarator(true); |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1518 | if (OwnedDecl) *OwnedDecl = Owned; |
| 1519 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1520 | break; |
| 1521 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1522 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1523 | case UnqualifiedId::IK_ConstructorTemplateId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1524 | case UnqualifiedId::IK_DestructorName: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1525 | // Constructors and destructors don't have return types. Use |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1526 | // "void" instead. |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1527 | T = Context.VoidTy; |
| 1528 | break; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1529 | |
| 1530 | case UnqualifiedId::IK_ConversionFunctionId: |
| 1531 | // The result type of a conversion function is the type that it |
| 1532 | // converts to. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 1533 | T = GetTypeFromParser(D.getName().ConversionFunctionId, |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1534 | &ReturnTypeInfo); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1535 | break; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1536 | } |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 1537 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1538 | if (D.getAttributes()) |
| 1539 | distributeTypeAttrsFromDeclarator(state, T); |
| 1540 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1541 | // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context. |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1542 | // In C++0x, a function declarator using 'auto' must have a trailing return |
| 1543 | // type (this is checked later) and we can skip this. In other languages |
| 1544 | // using auto, we need to check regardless. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1545 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1546 | (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) { |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1547 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1549 | switch (D.getContext()) { |
| 1550 | case Declarator::KNRTypeListContext: |
| 1551 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 1552 | break; |
John McCall | c05a94b | 2011-03-23 23:43:04 +0000 | [diff] [blame^] | 1553 | case Declarator::ObjCPrototypeContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1554 | case Declarator::PrototypeContext: |
| 1555 | Error = 0; // Function prototype |
| 1556 | break; |
| 1557 | case Declarator::MemberContext: |
| 1558 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1559 | case TTK_Enum: assert(0 && "unhandled tag kind"); break; |
| 1560 | case TTK_Struct: Error = 1; /* Struct member */ break; |
| 1561 | case TTK_Union: Error = 2; /* Union member */ break; |
| 1562 | case TTK_Class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1564 | break; |
| 1565 | case Declarator::CXXCatchContext: |
| 1566 | Error = 4; // Exception declaration |
| 1567 | break; |
| 1568 | case Declarator::TemplateParamContext: |
| 1569 | Error = 5; // Template parameter |
| 1570 | break; |
| 1571 | case Declarator::BlockLiteralContext: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1572 | Error = 6; // Block literal |
| 1573 | break; |
| 1574 | case Declarator::TemplateTypeArgContext: |
| 1575 | Error = 7; // Template type argument |
| 1576 | break; |
| 1577 | case Declarator::TypeNameContext: |
| 1578 | if (!AutoAllowedInTypeName) |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1579 | Error = 10; // Generic |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | case Declarator::FileContext: |
| 1582 | case Declarator::BlockContext: |
| 1583 | case Declarator::ForContext: |
| 1584 | case Declarator::ConditionContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | } |
| 1587 | |
Richard Smith | ddc83f9 | 2011-02-21 23:18:00 +0000 | [diff] [blame] | 1588 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1589 | Error = 8; |
| 1590 | |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1591 | // In Objective-C it is an error to use 'auto' on a function declarator. |
| 1592 | if (D.isFunctionDeclarator()) |
| 1593 | Error = 9; |
| 1594 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1595 | // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator |
| 1596 | // contains a trailing return type. That is only legal at the outermost |
| 1597 | // level. Check all declarator chunks (outermost first) anyway, to give |
| 1598 | // better diagnostics. |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1599 | if (getLangOptions().CPlusPlus0x && Error != -1) { |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1600 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 1601 | unsigned chunkIndex = e - i - 1; |
| 1602 | state.setCurrentChunkIndex(chunkIndex); |
| 1603 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
| 1604 | if (DeclType.Kind == DeclaratorChunk::Function) { |
| 1605 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 1606 | if (FTI.TrailingReturnType) { |
| 1607 | Error = -1; |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1614 | if (Error != -1) { |
| 1615 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 1616 | << Error; |
| 1617 | T = Context.IntTy; |
| 1618 | D.setInvalidType(true); |
| 1619 | } |
| 1620 | } |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1621 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1622 | if (T.isNull()) |
| 1623 | return Context.getNullTypeSourceInfo(); |
| 1624 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1625 | // The name we're declaring, if any. |
| 1626 | DeclarationName Name; |
| 1627 | if (D.getIdentifier()) |
| 1628 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1630 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 1631 | // DeclTypeInfos are ordered from the identifier out, which is |
| 1632 | // opposite of what we want :). |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1633 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1634 | unsigned chunkIndex = e - i - 1; |
| 1635 | state.setCurrentChunkIndex(chunkIndex); |
| 1636 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | switch (DeclType.Kind) { |
| 1638 | default: assert(0 && "Unknown decltype!"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1639 | case DeclaratorChunk::Paren: |
| 1640 | T = BuildParenType(T); |
| 1641 | break; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1642 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1643 | // If blocks are disabled, emit an error. |
| 1644 | if (!LangOpts.Blocks) |
| 1645 | Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1647 | T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name); |
| 1648 | if (DeclType.Cls.TypeQuals) |
| 1649 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1650 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1651 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1652 | // Verify that we're not building a pointer to pointer to function with |
| 1653 | // exception specification. |
| 1654 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1655 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1656 | D.setInvalidType(true); |
| 1657 | // Build the type anyway. |
| 1658 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1659 | if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) { |
| 1660 | T = Context.getObjCObjectPointerType(T); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1661 | if (DeclType.Ptr.TypeQuals) |
| 1662 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1665 | T = BuildPointerType(T, DeclType.Loc, Name); |
| 1666 | if (DeclType.Ptr.TypeQuals) |
| 1667 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1668 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1670 | case DeclaratorChunk::Reference: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1671 | // Verify that we're not building a reference to pointer to function with |
| 1672 | // exception specification. |
| 1673 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1674 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1675 | D.setInvalidType(true); |
| 1676 | // Build the type anyway. |
| 1677 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1678 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); |
| 1679 | |
| 1680 | Qualifiers Quals; |
| 1681 | if (DeclType.Ref.HasRestrict) |
| 1682 | T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1683 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1684 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1685 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1686 | // Verify that we're not building an array of pointers to function with |
| 1687 | // exception specification. |
| 1688 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1689 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1690 | D.setInvalidType(true); |
| 1691 | // Build the type anyway. |
| 1692 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1693 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 1694 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1695 | ArrayType::ArraySizeModifier ASM; |
| 1696 | if (ATI.isStar) |
| 1697 | ASM = ArrayType::Star; |
| 1698 | else if (ATI.hasStatic) |
| 1699 | ASM = ArrayType::Static; |
| 1700 | else |
| 1701 | ASM = ArrayType::Normal; |
John McCall | c05a94b | 2011-03-23 23:43:04 +0000 | [diff] [blame^] | 1702 | if (ASM == ArrayType::Star && !D.isPrototypeContext()) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1703 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 1704 | // for function definitions, and disallows some edge cases detailed |
| 1705 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 1706 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 1707 | ASM = ArrayType::Normal; |
| 1708 | D.setInvalidType(true); |
| 1709 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1710 | T = BuildArrayType(T, ASM, ArraySize, |
| 1711 | Qualifiers::fromCVRMask(ATI.TypeQuals), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1712 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1715 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | // If the function declarator has a prototype (i.e. it is not () and |
| 1717 | // does not have a K&R-style identifier list), then the arguments are part |
| 1718 | // of the type, otherwise the argument list is (). |
| 1719 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1720 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1721 | // Check for auto functions and trailing return type and adjust the |
| 1722 | // return type accordingly. |
| 1723 | if (!D.isInvalidType()) { |
| 1724 | // trailing-return-type is only required if we're declaring a function, |
| 1725 | // and not, for instance, a pointer to a function. |
| 1726 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
| 1727 | !FTI.TrailingReturnType && chunkIndex == 0) { |
| 1728 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 1729 | diag::err_auto_missing_trailing_return); |
| 1730 | T = Context.IntTy; |
| 1731 | D.setInvalidType(true); |
| 1732 | } else if (FTI.TrailingReturnType) { |
| 1733 | // T must be exactly 'auto' at this point. See CWG issue 681. |
| 1734 | if (isa<ParenType>(T)) { |
| 1735 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 1736 | diag::err_trailing_return_in_parens) |
| 1737 | << T << D.getDeclSpec().getSourceRange(); |
| 1738 | D.setInvalidType(true); |
| 1739 | } else if (T.hasQualifiers() || !isa<AutoType>(T)) { |
| 1740 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 1741 | diag::err_trailing_return_without_auto) |
| 1742 | << T << D.getDeclSpec().getSourceRange(); |
| 1743 | D.setInvalidType(true); |
| 1744 | } |
| 1745 | |
| 1746 | T = GetTypeFromParser( |
| 1747 | ParsedType::getFromOpaquePtr(FTI.TrailingReturnType), |
| 1748 | &ReturnTypeInfo); |
| 1749 | } |
| 1750 | } |
| 1751 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1752 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1753 | // For conversion functions, we'll diagnose this particular error later. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1754 | if ((T->isArrayType() || T->isFunctionType()) && |
| 1755 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 1756 | unsigned diagID = diag::err_func_returning_array_function; |
Argyrios Kyrtzidis | a4356ad | 2011-01-26 01:26:44 +0000 | [diff] [blame] | 1757 | // Last processing chunk in block context means this function chunk |
| 1758 | // represents the block. |
| 1759 | if (chunkIndex == 0 && |
| 1760 | D.getContext() == Declarator::BlockLiteralContext) |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 1761 | diagID = diag::err_block_returning_array_function; |
| 1762 | Diag(DeclType.Loc, diagID) << T->isFunctionType() << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1763 | T = Context.IntTy; |
| 1764 | D.setInvalidType(true); |
| 1765 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1766 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1767 | // cv-qualifiers on return types are pointless except when the type is a |
| 1768 | // class type in C++. |
Douglas Gregor | fff9513 | 2011-03-01 17:04:42 +0000 | [diff] [blame] | 1769 | if (isa<PointerType>(T) && T.getLocalCVRQualifiers() && |
Rafael Espindola | 1e15394 | 2011-03-11 04:56:58 +0000 | [diff] [blame] | 1770 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) && |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1771 | (!getLangOptions().CPlusPlus || !T->isDependentType())) { |
| 1772 | assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?"); |
| 1773 | DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); |
| 1774 | assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer); |
| 1775 | |
| 1776 | DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr; |
| 1777 | |
| 1778 | DiagnoseIgnoredQualifiers(PTI.TypeQuals, |
| 1779 | SourceLocation::getFromRawEncoding(PTI.ConstQualLoc), |
| 1780 | SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc), |
| 1781 | SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc), |
| 1782 | *this); |
| 1783 | |
| 1784 | } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1785 | (!getLangOptions().CPlusPlus || |
| 1786 | (!T->isDependentType() && !T->isRecordType()))) { |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1787 | |
| 1788 | DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(), |
| 1789 | D.getDeclSpec().getConstSpecLoc(), |
| 1790 | D.getDeclSpec().getVolatileSpecLoc(), |
| 1791 | D.getDeclSpec().getRestrictSpecLoc(), |
| 1792 | *this); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1793 | } |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1795 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 1796 | // C++ [dcl.fct]p6: |
| 1797 | // Types shall not be defined in return or parameter types. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1798 | TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1799 | if (Tag->isDefinition()) |
| 1800 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 1801 | << Context.getTypeDeclType(Tag); |
| 1802 | } |
| 1803 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1804 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 1805 | // anyway. |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 1806 | if (FTI.getExceptionSpecType() != EST_None && |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1807 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 1808 | Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef); |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1809 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1810 | if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) { |
| 1811 | // Simple void foo(), where the incoming T is the result type. |
| 1812 | T = Context.getFunctionNoProtoType(T); |
| 1813 | } else { |
| 1814 | // We allow a zero-parameter variadic function in C if the |
| 1815 | // function is marked with the "overloadable" attribute. Scan |
| 1816 | // for this attribute now. |
| 1817 | if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1818 | bool Overloadable = false; |
| 1819 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1820 | Attrs; Attrs = Attrs->getNext()) { |
| 1821 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1822 | Overloadable = true; |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | if (!Overloadable) |
| 1828 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1829 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1832 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function |
| 1833 | // definition. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1834 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 1835 | D.setInvalidType(true); |
| 1836 | break; |
| 1837 | } |
| 1838 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1839 | FunctionProtoType::ExtProtoInfo EPI; |
| 1840 | EPI.Variadic = FTI.isVariadic; |
| 1841 | EPI.TypeQuals = FTI.TypeQuals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1842 | EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None |
| 1843 | : FTI.RefQualifierIsLValueRef? RQ_LValue |
| 1844 | : RQ_RValue; |
| 1845 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1846 | // Otherwise, we have a function with an argument list that is |
| 1847 | // potentially variadic. |
| 1848 | llvm::SmallVector<QualType, 16> ArgTys; |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1849 | ArgTys.reserve(FTI.NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1851 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1852 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1853 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1854 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1855 | |
| 1856 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1857 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1858 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1859 | // Look for 'void'. void is allowed only as a single argument to a |
| 1860 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1861 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1862 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1864 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1865 | // have arguments of incomplete type. |
| 1866 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1867 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1868 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1869 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1870 | } else if (FTI.ArgInfo[i].Ident) { |
| 1871 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1872 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1873 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1874 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1875 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1876 | } else { |
| 1877 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1878 | if (ArgTy.hasQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1879 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1881 | // Do not add 'void' to the ArgTys list. |
| 1882 | break; |
| 1883 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1884 | } else if (!FTI.hasPrototype) { |
| 1885 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 1886 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 1887 | Param->setKNRPromoted(true); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1888 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 1889 | if (BTy->getKind() == BuiltinType::Float) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1890 | ArgTy = Context.DoubleTy; |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 1891 | Param->setKNRPromoted(true); |
| 1892 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1893 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1894 | } |
Fariborz Jahanian | 56a965c | 2010-09-08 21:36:35 +0000 | [diff] [blame] | 1895 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1896 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1897 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1898 | |
| 1899 | llvm::SmallVector<QualType, 4> Exceptions; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 1900 | EPI.ExceptionSpecType = FTI.getExceptionSpecType(); |
| 1901 | if (FTI.getExceptionSpecType() == EST_Dynamic) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1902 | Exceptions.reserve(FTI.NumExceptions); |
| 1903 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
| 1904 | // FIXME: Preserve type source info. |
| 1905 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
| 1906 | // Check that the type is valid for an exception spec, and |
| 1907 | // drop it if not. |
| 1908 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1909 | Exceptions.push_back(ET); |
| 1910 | } |
John McCall | 373920b | 2010-12-14 16:45:57 +0000 | [diff] [blame] | 1911 | EPI.NumExceptions = Exceptions.size(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1912 | EPI.Exceptions = Exceptions.data(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 1913 | } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1914 | // If an error occurred, there's no expression here. |
| 1915 | if (Expr *NoexceptExpr = FTI.NoexceptExpr) { |
| 1916 | assert((NoexceptExpr->isTypeDependent() || |
| 1917 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 1918 | Context.BoolTy) && |
| 1919 | "Parser should have made sure that the expression is boolean"); |
| 1920 | SourceLocation ErrLoc; |
| 1921 | llvm::APSInt Dummy; |
| 1922 | if (!NoexceptExpr->isValueDependent() && |
| 1923 | !NoexceptExpr->isIntegerConstantExpr(Dummy, Context, &ErrLoc, |
| 1924 | /*evaluated*/false)) |
| 1925 | Diag(ErrLoc, diag::err_noexcept_needs_constant_expression) |
| 1926 | << NoexceptExpr->getSourceRange(); |
| 1927 | else |
| 1928 | EPI.NoexceptExpr = NoexceptExpr; |
| 1929 | } |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1930 | } else if (FTI.getExceptionSpecType() == EST_None && |
| 1931 | ImplicitlyNoexcept && chunkIndex == 0) { |
| 1932 | // Only the outermost chunk is marked noexcept, of course. |
| 1933 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1934 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1935 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1936 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1937 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1941 | case DeclaratorChunk::MemberPointer: |
| 1942 | // The scope spec must refer to a class, or be dependent. |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1943 | CXXScopeSpec &SS = DeclType.Mem.Scope(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1944 | QualType ClsType; |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1945 | if (SS.isInvalid()) { |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 1946 | // Avoid emitting extra errors if we already errored on the scope. |
| 1947 | D.setInvalidType(true); |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1948 | } else if (isDependentScopeSpecifier(SS) || |
| 1949 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | NestedNameSpecifier *NNS |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1951 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1952 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); |
| 1953 | switch (NNS->getKind()) { |
| 1954 | case NestedNameSpecifier::Identifier: |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1955 | ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1956 | NNS->getAsIdentifier()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | |
| 1959 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1960 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1961 | case NestedNameSpecifier::Global: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1962 | llvm_unreachable("Nested-name-specifier must name a type"); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1963 | break; |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1965 | case NestedNameSpecifier::TypeSpec: |
| 1966 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1967 | ClsType = QualType(NNS->getAsType(), 0); |
Abramo Bagnara | 91ce2c4 | 2011-03-10 10:18:27 +0000 | [diff] [blame] | 1968 | // Note: if the NNS has a prefix and ClsType is a nondependent |
| 1969 | // TemplateSpecializationType, then the NNS prefix is NOT included |
| 1970 | // in ClsType; hence we wrap ClsType into an ElaboratedType. |
| 1971 | // NOTE: in particular, no wrap occurs if ClsType already is an |
| 1972 | // Elaborated, DependentName, or DependentTemplateSpecialization. |
| 1973 | if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType())) |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 1974 | ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1975 | break; |
| 1976 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1977 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1978 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1979 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1980 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1981 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1982 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1985 | if (!ClsType.isNull()) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1986 | T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1987 | if (T.isNull()) { |
| 1988 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1989 | D.setInvalidType(true); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1990 | } else if (DeclType.Mem.TypeQuals) { |
| 1991 | T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1992 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1993 | break; |
| 1994 | } |
| 1995 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1996 | if (T.isNull()) { |
| 1997 | D.setInvalidType(true); |
| 1998 | T = Context.IntTy; |
| 1999 | } |
| 2000 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2001 | // See if there are any attributes on this declarator chunk. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2002 | if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs())) |
| 2003 | processTypeAttrs(state, T, false, attrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2004 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2005 | |
| 2006 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2007 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 2008 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 708f3b8 | 2010-10-14 22:03:51 +0000 | [diff] [blame] | 2010 | // C++ 8.3.5p4: |
| 2011 | // A cv-qualifier-seq shall only be part of the function type |
| 2012 | // for a nonstatic member function, the function type to which a pointer |
| 2013 | // to member refers, or the top-level function type of a function typedef |
| 2014 | // declaration. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2015 | // |
| 2016 | // Core issue 547 also allows cv-qualifiers on function types that are |
| 2017 | // top-level template type arguments. |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2018 | bool FreeFunction; |
| 2019 | if (!D.getCXXScopeSpec().isSet()) { |
| 2020 | FreeFunction = (D.getContext() != Declarator::MemberContext || |
| 2021 | D.getDeclSpec().isFriendSpecified()); |
| 2022 | } else { |
| 2023 | DeclContext *DC = computeDeclContext(D.getCXXScopeSpec()); |
| 2024 | FreeFunction = (DC && !DC->isRecord()); |
| 2025 | } |
| 2026 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2027 | // C++0x [dcl.fct]p6: |
| 2028 | // A ref-qualifier shall only be part of the function type for a |
| 2029 | // non-static member function, the function type to which a pointer to |
| 2030 | // member refers, or the top-level function type of a function typedef |
| 2031 | // declaration. |
| 2032 | if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) && |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2033 | !(D.getContext() == Declarator::TemplateTypeArgContext && |
| 2034 | !D.isFunctionDeclarator()) && |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2035 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Sebastian Redl | c61bb20 | 2010-07-09 21:26:08 +0000 | [diff] [blame] | 2036 | (FreeFunction || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2037 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2038 | if (D.getContext() == Declarator::TemplateTypeArgContext) { |
| 2039 | // Accept qualified function types as template type arguments as a GNU |
| 2040 | // extension. This is also the subject of C++ core issue 547. |
| 2041 | std::string Quals; |
| 2042 | if (FnTy->getTypeQuals() != 0) |
| 2043 | Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString(); |
| 2044 | |
| 2045 | switch (FnTy->getRefQualifier()) { |
| 2046 | case RQ_None: |
| 2047 | break; |
| 2048 | |
| 2049 | case RQ_LValue: |
| 2050 | if (!Quals.empty()) |
| 2051 | Quals += ' '; |
| 2052 | Quals += '&'; |
| 2053 | break; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2054 | |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2055 | case RQ_RValue: |
| 2056 | if (!Quals.empty()) |
| 2057 | Quals += ' '; |
| 2058 | Quals += "&&"; |
| 2059 | break; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2060 | } |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2061 | |
| 2062 | Diag(D.getIdentifierLoc(), |
| 2063 | diag::ext_qualified_function_type_template_arg) |
| 2064 | << Quals; |
| 2065 | } else { |
| 2066 | if (FnTy->getTypeQuals() != 0) { |
| 2067 | if (D.isFunctionDeclarator()) |
| 2068 | Diag(D.getIdentifierLoc(), |
| 2069 | diag::err_invalid_qualified_function_type); |
| 2070 | else |
| 2071 | Diag(D.getIdentifierLoc(), |
| 2072 | diag::err_invalid_qualified_typedef_function_type_use) |
| 2073 | << FreeFunction; |
| 2074 | } |
| 2075 | |
| 2076 | if (FnTy->getRefQualifier()) { |
| 2077 | if (D.isFunctionDeclarator()) { |
| 2078 | SourceLocation Loc = D.getIdentifierLoc(); |
| 2079 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 2080 | const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1); |
| 2081 | if (Chunk.Kind == DeclaratorChunk::Function && |
| 2082 | Chunk.Fun.hasRefQualifier()) { |
| 2083 | Loc = Chunk.Fun.getRefQualifierLoc(); |
| 2084 | break; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | Diag(Loc, diag::err_invalid_ref_qualifier_function_type) |
| 2089 | << (FnTy->getRefQualifier() == RQ_LValue) |
| 2090 | << FixItHint::CreateRemoval(Loc); |
| 2091 | } else { |
| 2092 | Diag(D.getIdentifierLoc(), |
| 2093 | diag::err_invalid_ref_qualifier_typedef_function_type_use) |
| 2094 | << FreeFunction |
| 2095 | << (FnTy->getRefQualifier() == RQ_LValue); |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | // Strip the cv-qualifiers and ref-qualifiers from the type. |
| 2100 | FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo(); |
| 2101 | EPI.TypeQuals = 0; |
| 2102 | EPI.RefQualifier = RQ_None; |
| 2103 | |
| 2104 | T = Context.getFunctionType(FnTy->getResultType(), |
| 2105 | FnTy->arg_type_begin(), |
| 2106 | FnTy->getNumArgs(), EPI); |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2107 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2111 | // Apply any undistributed attributes from the declarator. |
| 2112 | if (!T.isNull()) |
| 2113 | if (AttributeList *attrs = D.getAttributes()) |
| 2114 | processTypeAttrs(state, T, false, attrs); |
| 2115 | |
| 2116 | // Diagnose any ignored type attributes. |
| 2117 | if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T); |
| 2118 | |
Peter Collingbourne | 148f1f7 | 2011-03-20 08:06:45 +0000 | [diff] [blame] | 2119 | // C++0x [dcl.constexpr]p9: |
| 2120 | // A constexpr specifier used in an object declaration declares the object |
| 2121 | // as const. |
| 2122 | if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) { |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 2123 | T.addConst(); |
| 2124 | } |
| 2125 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2126 | // If there was an ellipsis in the declarator, the declaration declares a |
| 2127 | // parameter pack whose type may be a pack expansion type. |
| 2128 | if (D.hasEllipsis() && !T.isNull()) { |
| 2129 | // C++0x [dcl.fct]p13: |
| 2130 | // A declarator-id or abstract-declarator containing an ellipsis shall |
| 2131 | // only be used in a parameter-declaration. Such a parameter-declaration |
| 2132 | // is a parameter pack (14.5.3). [...] |
| 2133 | switch (D.getContext()) { |
| 2134 | case Declarator::PrototypeContext: |
| 2135 | // C++0x [dcl.fct]p13: |
| 2136 | // [...] When it is part of a parameter-declaration-clause, the |
| 2137 | // parameter pack is a function parameter pack (14.5.3). The type T |
| 2138 | // of the declarator-id of the function parameter pack shall contain |
| 2139 | // a template parameter pack; each template parameter pack in T is |
| 2140 | // expanded by the function parameter pack. |
| 2141 | // |
| 2142 | // We represent function parameter packs as function parameters whose |
| 2143 | // type is a pack expansion. |
| 2144 | if (!T->containsUnexpandedParameterPack()) { |
| 2145 | Diag(D.getEllipsisLoc(), |
| 2146 | diag::err_function_parameter_pack_without_parameter_packs) |
| 2147 | << T << D.getSourceRange(); |
| 2148 | D.setEllipsisLoc(SourceLocation()); |
| 2149 | } else { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2150 | T = Context.getPackExpansionType(T, llvm::Optional<unsigned>()); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2151 | } |
| 2152 | break; |
| 2153 | |
| 2154 | case Declarator::TemplateParamContext: |
| 2155 | // C++0x [temp.param]p15: |
| 2156 | // If a template-parameter is a [...] is a parameter-declaration that |
| 2157 | // declares a parameter pack (8.3.5), then the template-parameter is a |
| 2158 | // template parameter pack (14.5.3). |
| 2159 | // |
| 2160 | // Note: core issue 778 clarifies that, if there are any unexpanded |
| 2161 | // parameter packs in the type of the non-type template parameter, then |
| 2162 | // it expands those parameter packs. |
| 2163 | if (T->containsUnexpandedParameterPack()) |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2164 | T = Context.getPackExpansionType(T, llvm::Optional<unsigned>()); |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 2165 | else if (!getLangOptions().CPlusPlus0x) |
Douglas Gregor | 5ce5f52 | 2011-01-19 21:59:15 +0000 | [diff] [blame] | 2166 | Diag(D.getEllipsisLoc(), diag::ext_variadic_templates); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | |
| 2169 | case Declarator::FileContext: |
| 2170 | case Declarator::KNRTypeListContext: |
John McCall | c05a94b | 2011-03-23 23:43:04 +0000 | [diff] [blame^] | 2171 | case Declarator::ObjCPrototypeContext: // FIXME: special diagnostic here? |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2172 | case Declarator::TypeNameContext: |
| 2173 | case Declarator::MemberContext: |
| 2174 | case Declarator::BlockContext: |
| 2175 | case Declarator::ForContext: |
| 2176 | case Declarator::ConditionContext: |
| 2177 | case Declarator::CXXCatchContext: |
| 2178 | case Declarator::BlockLiteralContext: |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2179 | case Declarator::TemplateTypeArgContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2180 | // FIXME: We may want to allow parameter packs in block-literal contexts |
| 2181 | // in the future. |
| 2182 | Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter); |
| 2183 | D.setEllipsisLoc(SourceLocation()); |
| 2184 | break; |
| 2185 | } |
| 2186 | } |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2187 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 2188 | if (T.isNull()) |
| 2189 | return Context.getNullTypeSourceInfo(); |
| 2190 | else if (D.isInvalidType()) |
| 2191 | return Context.getTrivialTypeSourceInfo(T); |
| 2192 | return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2195 | /// Map an AttributedType::Kind to an AttributeList::Kind. |
| 2196 | static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) { |
| 2197 | switch (kind) { |
| 2198 | case AttributedType::attr_address_space: |
| 2199 | return AttributeList::AT_address_space; |
| 2200 | case AttributedType::attr_regparm: |
| 2201 | return AttributeList::AT_regparm; |
| 2202 | case AttributedType::attr_vector_size: |
| 2203 | return AttributeList::AT_vector_size; |
| 2204 | case AttributedType::attr_neon_vector_type: |
| 2205 | return AttributeList::AT_neon_vector_type; |
| 2206 | case AttributedType::attr_neon_polyvector_type: |
| 2207 | return AttributeList::AT_neon_polyvector_type; |
| 2208 | case AttributedType::attr_objc_gc: |
| 2209 | return AttributeList::AT_objc_gc; |
| 2210 | case AttributedType::attr_noreturn: |
| 2211 | return AttributeList::AT_noreturn; |
| 2212 | case AttributedType::attr_cdecl: |
| 2213 | return AttributeList::AT_cdecl; |
| 2214 | case AttributedType::attr_fastcall: |
| 2215 | return AttributeList::AT_fastcall; |
| 2216 | case AttributedType::attr_stdcall: |
| 2217 | return AttributeList::AT_stdcall; |
| 2218 | case AttributedType::attr_thiscall: |
| 2219 | return AttributeList::AT_thiscall; |
| 2220 | case AttributedType::attr_pascal: |
| 2221 | return AttributeList::AT_pascal; |
| 2222 | } |
| 2223 | llvm_unreachable("unexpected attribute kind!"); |
| 2224 | return AttributeList::Kind(); |
| 2225 | } |
| 2226 | |
| 2227 | static void fillAttributedTypeLoc(AttributedTypeLoc TL, |
| 2228 | const AttributeList *attrs) { |
| 2229 | AttributedType::Kind kind = TL.getAttrKind(); |
| 2230 | |
| 2231 | assert(attrs && "no type attributes in the expected location!"); |
| 2232 | AttributeList::Kind parsedKind = getAttrListKind(kind); |
| 2233 | while (attrs->getKind() != parsedKind) { |
| 2234 | attrs = attrs->getNext(); |
| 2235 | assert(attrs && "no matching attribute in expected location!"); |
| 2236 | } |
| 2237 | |
| 2238 | TL.setAttrNameLoc(attrs->getLoc()); |
| 2239 | if (TL.hasAttrExprOperand()) |
| 2240 | TL.setAttrExprOperand(attrs->getArg(0)); |
| 2241 | else if (TL.hasAttrEnumOperand()) |
| 2242 | TL.setAttrEnumOperandLoc(attrs->getParameterLoc()); |
| 2243 | |
| 2244 | // FIXME: preserve this information to here. |
| 2245 | if (TL.hasAttrOperand()) |
| 2246 | TL.setAttrOperandParensRange(SourceRange()); |
| 2247 | } |
| 2248 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2249 | namespace { |
| 2250 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2251 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2252 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2253 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2254 | public: |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2255 | TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS) |
| 2256 | : Context(Context), DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2257 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2258 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 2259 | fillAttributedTypeLoc(TL, DS.getAttributes().getList()); |
| 2260 | Visit(TL.getModifiedLoc()); |
| 2261 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2262 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 2263 | Visit(TL.getUnqualifiedLoc()); |
| 2264 | } |
| 2265 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2266 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 2267 | } |
| 2268 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2269 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2270 | } |
| 2271 | void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 2272 | // Handle the base type, which might not have been written explicitly. |
| 2273 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 2274 | TL.setHasBaseTypeAsWritten(false); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2275 | TL.getBaseLoc().initialize(Context, SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2276 | } else { |
| 2277 | TL.setHasBaseTypeAsWritten(true); |
| 2278 | Visit(TL.getBaseLoc()); |
| 2279 | } |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 2280 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2281 | // Protocol qualifiers. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2282 | if (DS.getProtocolQualifiers()) { |
| 2283 | assert(TL.getNumProtocols() > 0); |
| 2284 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 2285 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 2286 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 2287 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 2288 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 2289 | } else { |
| 2290 | assert(TL.getNumProtocols() == 0); |
| 2291 | TL.setLAngleLoc(SourceLocation()); |
| 2292 | TL.setRAngleLoc(SourceLocation()); |
| 2293 | } |
| 2294 | } |
| 2295 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2296 | TL.setStarLoc(SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2297 | Visit(TL.getPointeeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2298 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2299 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2300 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2301 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2302 | |
| 2303 | // If we got no declarator info from previous Sema routines, |
| 2304 | // just fill with the typespec loc. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2305 | if (!TInfo) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2306 | TL.initialize(Context, DS.getTypeSpecTypeNameLoc()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
| 2309 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2310 | TypeLoc OldTL = TInfo->getTypeLoc(); |
| 2311 | if (TInfo->getType()->getAs<ElaboratedType>()) { |
| 2312 | ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL); |
| 2313 | TemplateSpecializationTypeLoc NamedTL = |
| 2314 | cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc()); |
| 2315 | TL.copy(NamedTL); |
| 2316 | } |
| 2317 | else |
| 2318 | TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL)); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2319 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2320 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 2321 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); |
| 2322 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 2323 | TL.setParensRange(DS.getTypeofParensRange()); |
| 2324 | } |
| 2325 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 2326 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); |
| 2327 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 2328 | TL.setParensRange(DS.getTypeofParensRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2329 | assert(DS.getRepAsType()); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2330 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2331 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2332 | TL.setUnderlyingTInfo(TInfo); |
| 2333 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 2334 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 2335 | // By default, use the source location of the type specifier. |
| 2336 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); |
| 2337 | if (TL.needsExtraLocalData()) { |
| 2338 | // Set info for the written builtin specifiers. |
| 2339 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); |
| 2340 | // Try to have a meaningful source location. |
| 2341 | if (TL.getWrittenSignSpec() != TSS_unspecified) |
| 2342 | // Sign spec loc overrides the others (e.g., 'unsigned long'). |
| 2343 | TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); |
| 2344 | else if (TL.getWrittenWidthSpec() != TSW_unspecified) |
| 2345 | // Width spec loc overrides type spec loc (e.g., 'short int'). |
| 2346 | TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); |
| 2347 | } |
| 2348 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2349 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 2350 | ElaboratedTypeKeyword Keyword |
| 2351 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2352 | if (DS.getTypeSpecType() == TST_typename) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2353 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2354 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2355 | if (TInfo) { |
| 2356 | TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc())); |
| 2357 | return; |
| 2358 | } |
| 2359 | } |
| 2360 | TL.setKeywordLoc(Keyword != ETK_None |
| 2361 | ? DS.getTypeSpecTypeLoc() |
| 2362 | : SourceLocation()); |
| 2363 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2364 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2365 | Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); |
| 2366 | } |
| 2367 | void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 2368 | ElaboratedTypeKeyword Keyword |
| 2369 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2370 | if (DS.getTypeSpecType() == TST_typename) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2371 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2372 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2373 | if (TInfo) { |
| 2374 | TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc())); |
| 2375 | return; |
| 2376 | } |
| 2377 | } |
| 2378 | TL.setKeywordLoc(Keyword != ETK_None |
| 2379 | ? DS.getTypeSpecTypeLoc() |
| 2380 | : SourceLocation()); |
| 2381 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 2382 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2383 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2384 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2385 | void VisitDependentTemplateSpecializationTypeLoc( |
| 2386 | DependentTemplateSpecializationTypeLoc TL) { |
| 2387 | ElaboratedTypeKeyword Keyword |
| 2388 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 2389 | if (Keyword == ETK_Typename) { |
| 2390 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2391 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2392 | if (TInfo) { |
| 2393 | TL.copy(cast<DependentTemplateSpecializationTypeLoc>( |
| 2394 | TInfo->getTypeLoc())); |
| 2395 | return; |
| 2396 | } |
| 2397 | } |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2398 | TL.initializeLocal(Context, SourceLocation()); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2399 | TL.setKeywordLoc(Keyword != ETK_None |
| 2400 | ? DS.getTypeSpecTypeLoc() |
| 2401 | : SourceLocation()); |
| 2402 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2403 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2404 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
| 2405 | } |
| 2406 | void VisitTagTypeLoc(TagTypeLoc TL) { |
| 2407 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2408 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2409 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2410 | void VisitTypeLoc(TypeLoc TL) { |
| 2411 | // FIXME: add other typespec types and change this to an assert. |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2412 | TL.initialize(Context, DS.getTypeSpecTypeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2413 | } |
| 2414 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2415 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2416 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2417 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2418 | const DeclaratorChunk &Chunk; |
| 2419 | |
| 2420 | public: |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2421 | DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk) |
| 2422 | : Context(Context), Chunk(Chunk) {} |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2423 | |
| 2424 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2425 | llvm_unreachable("qualified type locs not expected here!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 2429 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 2430 | TL.setCaretLoc(Chunk.Loc); |
| 2431 | } |
| 2432 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 2433 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 2434 | TL.setStarLoc(Chunk.Loc); |
| 2435 | } |
| 2436 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 2437 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 2438 | TL.setStarLoc(Chunk.Loc); |
| 2439 | } |
| 2440 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 2441 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2442 | const CXXScopeSpec& SS = Chunk.Mem.Scope(); |
| 2443 | NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context); |
| 2444 | |
| 2445 | const Type* ClsTy = TL.getClass(); |
| 2446 | QualType ClsQT = QualType(ClsTy, 0); |
| 2447 | TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0); |
| 2448 | // Now copy source location info into the type loc component. |
| 2449 | TypeLoc ClsTL = ClsTInfo->getTypeLoc(); |
| 2450 | switch (NNSLoc.getNestedNameSpecifier()->getKind()) { |
| 2451 | case NestedNameSpecifier::Identifier: |
| 2452 | assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc"); |
| 2453 | { |
Abramo Bagnara | fd9c42e | 2011-03-06 22:48:24 +0000 | [diff] [blame] | 2454 | DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2455 | DNTLoc.setKeywordLoc(SourceLocation()); |
| 2456 | DNTLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 2457 | DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc()); |
| 2458 | } |
| 2459 | break; |
| 2460 | |
| 2461 | case NestedNameSpecifier::TypeSpec: |
| 2462 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2463 | if (isa<ElaboratedType>(ClsTy)) { |
| 2464 | ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL); |
| 2465 | ETLoc.setKeywordLoc(SourceLocation()); |
| 2466 | ETLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 2467 | TypeLoc NamedTL = ETLoc.getNamedTypeLoc(); |
| 2468 | NamedTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 2469 | } else { |
| 2470 | ClsTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 2471 | } |
| 2472 | break; |
| 2473 | |
| 2474 | case NestedNameSpecifier::Namespace: |
| 2475 | case NestedNameSpecifier::NamespaceAlias: |
| 2476 | case NestedNameSpecifier::Global: |
| 2477 | llvm_unreachable("Nested-name-specifier must name a type"); |
| 2478 | break; |
| 2479 | } |
| 2480 | |
| 2481 | // Finally fill in MemberPointerLocInfo fields. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2482 | TL.setStarLoc(Chunk.Loc); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2483 | TL.setClassTInfo(ClsTInfo); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2484 | } |
| 2485 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 2486 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2487 | // 'Amp' is misleading: this might have been originally |
| 2488 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2489 | TL.setAmpLoc(Chunk.Loc); |
| 2490 | } |
| 2491 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 2492 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 2493 | assert(!Chunk.Ref.LValueRef); |
| 2494 | TL.setAmpAmpLoc(Chunk.Loc); |
| 2495 | } |
| 2496 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 2497 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 2498 | TL.setLBracketLoc(Chunk.Loc); |
| 2499 | TL.setRBracketLoc(Chunk.EndLoc); |
| 2500 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 2501 | } |
| 2502 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 2503 | assert(Chunk.Kind == DeclaratorChunk::Function); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 2504 | TL.setLocalRangeBegin(Chunk.Loc); |
| 2505 | TL.setLocalRangeEnd(Chunk.EndLoc); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2506 | TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2507 | |
| 2508 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2509 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2510 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2511 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2512 | } |
| 2513 | // FIXME: exception specs |
| 2514 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2515 | void VisitParenTypeLoc(ParenTypeLoc TL) { |
| 2516 | assert(Chunk.Kind == DeclaratorChunk::Paren); |
| 2517 | TL.setLParenLoc(Chunk.Loc); |
| 2518 | TL.setRParenLoc(Chunk.EndLoc); |
| 2519 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2520 | |
| 2521 | void VisitTypeLoc(TypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2522 | llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2523 | } |
| 2524 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2527 | /// \brief Create and instantiate a TypeSourceInfo with type source information. |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 2528 | /// |
| 2529 | /// \param T QualType referring to the type as written in source code. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 2530 | /// |
| 2531 | /// \param ReturnTypeInfo For declarators whose return type does not show |
| 2532 | /// up in the normal place in the declaration specifiers (such as a C++ |
| 2533 | /// conversion function), this pointer will refer to a type source information |
| 2534 | /// for that return type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2535 | TypeSourceInfo * |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 2536 | Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, |
| 2537 | TypeSourceInfo *ReturnTypeInfo) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2538 | TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); |
| 2539 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2541 | // Handle parameter packs whose type is a pack expansion. |
| 2542 | if (isa<PackExpansionType>(T)) { |
| 2543 | cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc()); |
| 2544 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
| 2545 | } |
| 2546 | |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 2547 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2548 | while (isa<AttributedTypeLoc>(CurrTL)) { |
| 2549 | AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL); |
| 2550 | fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs()); |
| 2551 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); |
| 2552 | } |
| 2553 | |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2554 | DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2555 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 2556 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2557 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2558 | // If we have different source information for the return type, use |
| 2559 | // that. This really only applies to C++ conversion functions. |
| 2560 | if (ReturnTypeInfo) { |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 2561 | TypeLoc TL = ReturnTypeInfo->getTypeLoc(); |
| 2562 | assert(TL.getFullDataSize() == CurrTL.getFullDataSize()); |
| 2563 | memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2564 | } else { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2565 | TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL); |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2568 | return TInfo; |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2571 | /// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2572 | ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) { |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 2573 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 2574 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 2575 | // it's appropriate, instead of allocating them and keeping them around. |
Douglas Gregor | eb0eb49 | 2010-12-10 08:12:03 +0000 | [diff] [blame] | 2576 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), |
| 2577 | TypeAlignment); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2578 | new (LocT) LocInfoType(T, TInfo); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 2579 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 2580 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2581 | return ParsedType::make(QualType(LocT, 0)); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 2585 | const PrintingPolicy &Policy) const { |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 2586 | assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" |
| 2587 | " was used directly instead of getting the QualType through" |
| 2588 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2591 | TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2592 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 2593 | // the parser. |
| 2594 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2596 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 2597 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 2598 | QualType T = TInfo->getType(); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 2599 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2600 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2602 | if (getLangOptions().CPlusPlus) { |
| 2603 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 2604 | CheckExtraCXXDefaultArguments(D); |
| 2605 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2606 | // C++0x [dcl.type]p3: |
| 2607 | // A type-specifier-seq shall not define a class or enumeration |
| 2608 | // unless it appears in the type-id of an alias-declaration |
| 2609 | // (7.1.3). |
| 2610 | if (OwnedTag && OwnedTag->isDefinition()) |
| 2611 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 2612 | << Context.getTypeDeclType(OwnedTag); |
| 2613 | } |
| 2614 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2615 | return CreateParsedType(T, TInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2618 | |
| 2619 | |
| 2620 | //===----------------------------------------------------------------------===// |
| 2621 | // Type Attribute Processing |
| 2622 | //===----------------------------------------------------------------------===// |
| 2623 | |
| 2624 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 2625 | /// specified type. The attribute contains 1 argument, the id of the address |
| 2626 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2628 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2630 | // If this type is already address space qualified, reject it. |
| 2631 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 2632 | // for two or more different address spaces." |
| 2633 | if (Type.getAddressSpace()) { |
| 2634 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2635 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2636 | return; |
| 2637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2639 | // Check the attribute arguments. |
| 2640 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 2641 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2642 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2643 | return; |
| 2644 | } |
| 2645 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 2646 | llvm::APSInt addrSpace(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2647 | if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || |
| 2648 | !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2649 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 2650 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2651 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2652 | return; |
| 2653 | } |
| 2654 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 2655 | // Bounds checking. |
| 2656 | if (addrSpace.isSigned()) { |
| 2657 | if (addrSpace.isNegative()) { |
| 2658 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 2659 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2660 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | } |
| 2663 | addrSpace.setIsSigned(false); |
| 2664 | } |
| 2665 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2666 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 2667 | if (addrSpace > max) { |
| 2668 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2669 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2670 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 2671 | return; |
| 2672 | } |
| 2673 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2675 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2678 | /// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type |
| 2679 | /// attribute on the specified type. Returns true to indicate that |
| 2680 | /// the attribute was handled, false to indicate that the type does |
| 2681 | /// not permit the attribute. |
| 2682 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 2683 | AttributeList &attr, |
| 2684 | QualType &type) { |
| 2685 | Sema &S = state.getSema(); |
| 2686 | |
| 2687 | // Delay if this isn't some kind of pointer. |
| 2688 | if (!type->isPointerType() && |
| 2689 | !type->isObjCObjectPointerType() && |
| 2690 | !type->isBlockPointerType()) |
| 2691 | return false; |
| 2692 | |
| 2693 | if (type.getObjCGCAttr() != Qualifiers::GCNone) { |
| 2694 | S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
| 2695 | attr.setInvalid(); |
| 2696 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2699 | // Check the attribute arguments. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2700 | if (!attr.getParameterName()) { |
| 2701 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2702 | << "objc_gc" << 1; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2703 | attr.setInvalid(); |
| 2704 | return true; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2705 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2706 | Qualifiers::GC GCAttr; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2707 | if (attr.getNumArgs() != 0) { |
| 2708 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2709 | attr.setInvalid(); |
| 2710 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2711 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2712 | if (attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2713 | GCAttr = Qualifiers::Weak; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2714 | else if (attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2715 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2716 | else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2717 | S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2718 | << "objc_gc" << attr.getParameterName(); |
| 2719 | attr.setInvalid(); |
| 2720 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2723 | QualType origType = type; |
| 2724 | type = S.Context.getObjCGCQualType(origType, GCAttr); |
| 2725 | |
| 2726 | // Make an attributed type to preserve the source information. |
| 2727 | if (attr.getLoc().isValid()) |
| 2728 | type = S.Context.getAttributedType(AttributedType::attr_objc_gc, |
| 2729 | origType, type); |
| 2730 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2731 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2734 | namespace { |
| 2735 | /// A helper class to unwrap a type down to a function for the |
| 2736 | /// purposes of applying attributes there. |
| 2737 | /// |
| 2738 | /// Use: |
| 2739 | /// FunctionTypeUnwrapper unwrapped(SemaRef, T); |
| 2740 | /// if (unwrapped.isFunctionType()) { |
| 2741 | /// const FunctionType *fn = unwrapped.get(); |
| 2742 | /// // change fn somehow |
| 2743 | /// T = unwrapped.wrap(fn); |
| 2744 | /// } |
| 2745 | struct FunctionTypeUnwrapper { |
| 2746 | enum WrapKind { |
| 2747 | Desugar, |
| 2748 | Parens, |
| 2749 | Pointer, |
| 2750 | BlockPointer, |
| 2751 | Reference, |
| 2752 | MemberPointer |
| 2753 | }; |
| 2754 | |
| 2755 | QualType Original; |
| 2756 | const FunctionType *Fn; |
| 2757 | llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack; |
| 2758 | |
| 2759 | FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) { |
| 2760 | while (true) { |
| 2761 | const Type *Ty = T.getTypePtr(); |
| 2762 | if (isa<FunctionType>(Ty)) { |
| 2763 | Fn = cast<FunctionType>(Ty); |
| 2764 | return; |
| 2765 | } else if (isa<ParenType>(Ty)) { |
| 2766 | T = cast<ParenType>(Ty)->getInnerType(); |
| 2767 | Stack.push_back(Parens); |
| 2768 | } else if (isa<PointerType>(Ty)) { |
| 2769 | T = cast<PointerType>(Ty)->getPointeeType(); |
| 2770 | Stack.push_back(Pointer); |
| 2771 | } else if (isa<BlockPointerType>(Ty)) { |
| 2772 | T = cast<BlockPointerType>(Ty)->getPointeeType(); |
| 2773 | Stack.push_back(BlockPointer); |
| 2774 | } else if (isa<MemberPointerType>(Ty)) { |
| 2775 | T = cast<MemberPointerType>(Ty)->getPointeeType(); |
| 2776 | Stack.push_back(MemberPointer); |
| 2777 | } else if (isa<ReferenceType>(Ty)) { |
| 2778 | T = cast<ReferenceType>(Ty)->getPointeeType(); |
| 2779 | Stack.push_back(Reference); |
| 2780 | } else { |
| 2781 | const Type *DTy = Ty->getUnqualifiedDesugaredType(); |
| 2782 | if (Ty == DTy) { |
| 2783 | Fn = 0; |
| 2784 | return; |
| 2785 | } |
| 2786 | |
| 2787 | T = QualType(DTy, 0); |
| 2788 | Stack.push_back(Desugar); |
| 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | bool isFunctionType() const { return (Fn != 0); } |
| 2794 | const FunctionType *get() const { return Fn; } |
| 2795 | |
| 2796 | QualType wrap(Sema &S, const FunctionType *New) { |
| 2797 | // If T wasn't modified from the unwrapped type, do nothing. |
| 2798 | if (New == get()) return Original; |
| 2799 | |
| 2800 | Fn = New; |
| 2801 | return wrap(S.Context, Original, 0); |
| 2802 | } |
| 2803 | |
| 2804 | private: |
| 2805 | QualType wrap(ASTContext &C, QualType Old, unsigned I) { |
| 2806 | if (I == Stack.size()) |
| 2807 | return C.getQualifiedType(Fn, Old.getQualifiers()); |
| 2808 | |
| 2809 | // Build up the inner type, applying the qualifiers from the old |
| 2810 | // type to the new type. |
| 2811 | SplitQualType SplitOld = Old.split(); |
| 2812 | |
| 2813 | // As a special case, tail-recurse if there are no qualifiers. |
| 2814 | if (SplitOld.second.empty()) |
| 2815 | return wrap(C, SplitOld.first, I); |
| 2816 | return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second); |
| 2817 | } |
| 2818 | |
| 2819 | QualType wrap(ASTContext &C, const Type *Old, unsigned I) { |
| 2820 | if (I == Stack.size()) return QualType(Fn, 0); |
| 2821 | |
| 2822 | switch (static_cast<WrapKind>(Stack[I++])) { |
| 2823 | case Desugar: |
| 2824 | // This is the point at which we potentially lose source |
| 2825 | // information. |
| 2826 | return wrap(C, Old->getUnqualifiedDesugaredType(), I); |
| 2827 | |
| 2828 | case Parens: { |
| 2829 | QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I); |
| 2830 | return C.getParenType(New); |
| 2831 | } |
| 2832 | |
| 2833 | case Pointer: { |
| 2834 | QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I); |
| 2835 | return C.getPointerType(New); |
| 2836 | } |
| 2837 | |
| 2838 | case BlockPointer: { |
| 2839 | QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I); |
| 2840 | return C.getBlockPointerType(New); |
| 2841 | } |
| 2842 | |
| 2843 | case MemberPointer: { |
| 2844 | const MemberPointerType *OldMPT = cast<MemberPointerType>(Old); |
| 2845 | QualType New = wrap(C, OldMPT->getPointeeType(), I); |
| 2846 | return C.getMemberPointerType(New, OldMPT->getClass()); |
| 2847 | } |
| 2848 | |
| 2849 | case Reference: { |
| 2850 | const ReferenceType *OldRef = cast<ReferenceType>(Old); |
| 2851 | QualType New = wrap(C, OldRef->getPointeeType(), I); |
| 2852 | if (isa<LValueReferenceType>(OldRef)) |
| 2853 | return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue()); |
| 2854 | else |
| 2855 | return C.getRValueReferenceType(New); |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | llvm_unreachable("unknown wrapping kind"); |
| 2860 | return QualType(); |
| 2861 | } |
| 2862 | }; |
| 2863 | } |
| 2864 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2865 | /// Process an individual function attribute. Returns true to |
| 2866 | /// indicate that the attribute was handled, false if it wasn't. |
| 2867 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 2868 | AttributeList &attr, |
| 2869 | QualType &type) { |
| 2870 | Sema &S = state.getSema(); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2871 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2872 | FunctionTypeUnwrapper unwrapped(S, type); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2873 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2874 | if (attr.getKind() == AttributeList::AT_noreturn) { |
| 2875 | if (S.CheckNoReturnAttr(attr)) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2876 | return true; |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2877 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2878 | // Delay if this is not a function type. |
| 2879 | if (!unwrapped.isFunctionType()) |
| 2880 | return false; |
| 2881 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2882 | // Otherwise we can process right away. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2883 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true); |
| 2884 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 2885 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2886 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2887 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2888 | if (attr.getKind() == AttributeList::AT_regparm) { |
| 2889 | unsigned value; |
| 2890 | if (S.CheckRegparmAttr(attr, value)) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2891 | return true; |
| 2892 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2893 | // Delay if this is not a function type. |
| 2894 | if (!unwrapped.isFunctionType()) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2895 | return false; |
| 2896 | |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 2897 | // Diagnose regparm with fastcall. |
| 2898 | const FunctionType *fn = unwrapped.get(); |
| 2899 | CallingConv CC = fn->getCallConv(); |
| 2900 | if (CC == CC_X86FastCall) { |
| 2901 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 2902 | << FunctionType::getNameForCallConv(CC) |
| 2903 | << "regparm"; |
| 2904 | attr.setInvalid(); |
| 2905 | return true; |
| 2906 | } |
| 2907 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2908 | FunctionType::ExtInfo EI = |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2909 | unwrapped.get()->getExtInfo().withRegParm(value); |
| 2910 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 2911 | return true; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2914 | // Otherwise, a calling convention. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2915 | CallingConv CC; |
| 2916 | if (S.CheckCallingConvAttr(attr, CC)) |
| 2917 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2918 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2919 | // Delay if the type didn't work out to a function. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2920 | if (!unwrapped.isFunctionType()) return false; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2921 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2922 | const FunctionType *fn = unwrapped.get(); |
| 2923 | CallingConv CCOld = fn->getCallConv(); |
Charles Davis | 064f7db | 2010-02-23 06:13:55 +0000 | [diff] [blame] | 2924 | if (S.Context.getCanonicalCallConv(CC) == |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2925 | S.Context.getCanonicalCallConv(CCOld)) { |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 2926 | FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 2927 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2928 | return true; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2929 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2930 | |
| 2931 | if (CCOld != CC_Default) { |
| 2932 | // Should we diagnose reapplications of the same convention? |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2933 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2934 | << FunctionType::getNameForCallConv(CC) |
| 2935 | << FunctionType::getNameForCallConv(CCOld); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2936 | attr.setInvalid(); |
| 2937 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | // Diagnose the use of X86 fastcall on varargs or unprototyped functions. |
| 2941 | if (CC == CC_X86FastCall) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2942 | if (isa<FunctionNoProtoType>(fn)) { |
| 2943 | S.Diag(attr.getLoc(), diag::err_cconv_knr) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2944 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2945 | attr.setInvalid(); |
| 2946 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2949 | const FunctionProtoType *FnP = cast<FunctionProtoType>(fn); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2950 | if (FnP->isVariadic()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2951 | S.Diag(attr.getLoc(), diag::err_cconv_varargs) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2952 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2953 | attr.setInvalid(); |
| 2954 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2955 | } |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 2956 | |
| 2957 | // Also diagnose fastcall with regparm. |
| 2958 | if (fn->getRegParmType()) { |
| 2959 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 2960 | << "regparm" |
| 2961 | << FunctionType::getNameForCallConv(CC); |
| 2962 | attr.setInvalid(); |
| 2963 | return true; |
| 2964 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2967 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 2968 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 2969 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2972 | /// Handle OpenCL image access qualifiers: read_only, write_only, read_write |
| 2973 | static void HandleOpenCLImageAccessAttribute(QualType& CurType, |
| 2974 | const AttributeList &Attr, |
| 2975 | Sema &S) { |
| 2976 | // Check the attribute arguments. |
| 2977 | if (Attr.getNumArgs() != 1) { |
| 2978 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2979 | Attr.setInvalid(); |
| 2980 | return; |
| 2981 | } |
| 2982 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 2983 | llvm::APSInt arg(32); |
| 2984 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 2985 | !sizeExpr->isIntegerConstantExpr(arg, S.Context)) { |
| 2986 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2987 | << "opencl_image_access" << sizeExpr->getSourceRange(); |
| 2988 | Attr.setInvalid(); |
| 2989 | return; |
| 2990 | } |
| 2991 | unsigned iarg = static_cast<unsigned>(arg.getZExtValue()); |
| 2992 | switch (iarg) { |
| 2993 | case CLIA_read_only: |
| 2994 | case CLIA_write_only: |
| 2995 | case CLIA_read_write: |
| 2996 | // Implemented in a separate patch |
| 2997 | break; |
| 2998 | default: |
| 2999 | // Implemented in a separate patch |
| 3000 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 3001 | << sizeExpr->getSourceRange(); |
| 3002 | Attr.setInvalid(); |
| 3003 | break; |
| 3004 | } |
| 3005 | } |
| 3006 | |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3007 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral |
| 3008 | /// and float scalars, although arrays, pointers, and function return values are |
| 3009 | /// allowed in conjunction with this construct. Aggregates with this attribute |
| 3010 | /// are invalid, even if they are of the same size as a corresponding scalar. |
| 3011 | /// The raw attribute should contain precisely 1 argument, the vector size for |
| 3012 | /// the variable, measured in bytes. If curType and rawAttr are well formed, |
| 3013 | /// this routine will return a new vector type. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3014 | static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, |
| 3015 | Sema &S) { |
Bob Wilson | 56affbc | 2010-11-16 00:32:16 +0000 | [diff] [blame] | 3016 | // Check the attribute arguments. |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3017 | if (Attr.getNumArgs() != 1) { |
| 3018 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3019 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3020 | return; |
| 3021 | } |
| 3022 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3023 | llvm::APSInt vecSize(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3024 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 3025 | !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3026 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3027 | << "vector_size" << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3028 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3029 | return; |
| 3030 | } |
| 3031 | // the base type must be integer or float, and can't already be a vector. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3032 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3033 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3034 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3035 | return; |
| 3036 | } |
| 3037 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 3038 | // vecSize is specified in bytes - convert to bits. |
| 3039 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 3040 | |
| 3041 | // the vector size needs to be an integral multiple of the type size. |
| 3042 | if (vectorSize % typeSize) { |
| 3043 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 3044 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3045 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3046 | return; |
| 3047 | } |
| 3048 | if (vectorSize == 0) { |
| 3049 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 3050 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3051 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3052 | return; |
| 3053 | } |
| 3054 | |
| 3055 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 3056 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3057 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3058 | VectorType::GenericVector); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3061 | /// HandleNeonVectorTypeAttr - The "neon_vector_type" and |
| 3062 | /// "neon_polyvector_type" attributes are used to create vector types that |
| 3063 | /// are mangled according to ARM's ABI. Otherwise, these types are identical |
| 3064 | /// to those created with the "vector_size" attribute. Unlike "vector_size" |
| 3065 | /// the argument to these Neon attributes is the number of vector elements, |
| 3066 | /// not the vector size in bytes. The vector width and element type must |
| 3067 | /// match one of the standard Neon vector types. |
| 3068 | static void HandleNeonVectorTypeAttr(QualType& CurType, |
| 3069 | const AttributeList &Attr, Sema &S, |
| 3070 | VectorType::VectorKind VecKind, |
| 3071 | const char *AttrName) { |
| 3072 | // Check the attribute arguments. |
| 3073 | if (Attr.getNumArgs() != 1) { |
| 3074 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3075 | Attr.setInvalid(); |
| 3076 | return; |
| 3077 | } |
| 3078 | // The number of elements must be an ICE. |
| 3079 | Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3080 | llvm::APSInt numEltsInt(32); |
| 3081 | if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() || |
| 3082 | !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) { |
| 3083 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3084 | << AttrName << numEltsExpr->getSourceRange(); |
| 3085 | Attr.setInvalid(); |
| 3086 | return; |
| 3087 | } |
| 3088 | // Only certain element types are supported for Neon vectors. |
| 3089 | const BuiltinType* BTy = CurType->getAs<BuiltinType>(); |
| 3090 | if (!BTy || |
| 3091 | (VecKind == VectorType::NeonPolyVector && |
| 3092 | BTy->getKind() != BuiltinType::SChar && |
| 3093 | BTy->getKind() != BuiltinType::Short) || |
| 3094 | (BTy->getKind() != BuiltinType::SChar && |
| 3095 | BTy->getKind() != BuiltinType::UChar && |
| 3096 | BTy->getKind() != BuiltinType::Short && |
| 3097 | BTy->getKind() != BuiltinType::UShort && |
| 3098 | BTy->getKind() != BuiltinType::Int && |
| 3099 | BTy->getKind() != BuiltinType::UInt && |
| 3100 | BTy->getKind() != BuiltinType::LongLong && |
| 3101 | BTy->getKind() != BuiltinType::ULongLong && |
| 3102 | BTy->getKind() != BuiltinType::Float)) { |
| 3103 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType; |
| 3104 | Attr.setInvalid(); |
| 3105 | return; |
| 3106 | } |
| 3107 | // The total size of the vector must be 64 or 128 bits. |
| 3108 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 3109 | unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue()); |
| 3110 | unsigned vecSize = typeSize * numElts; |
| 3111 | if (vecSize != 64 && vecSize != 128) { |
| 3112 | S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType; |
| 3113 | Attr.setInvalid(); |
| 3114 | return; |
| 3115 | } |
| 3116 | |
| 3117 | CurType = S.Context.getVectorType(CurType, numElts, VecKind); |
| 3118 | } |
| 3119 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3120 | static void processTypeAttrs(TypeProcessingState &state, QualType &type, |
| 3121 | bool isDeclSpec, AttributeList *attrs) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3122 | // Scan through and apply attributes to this type where it makes sense. Some |
| 3123 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 3124 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 3125 | // apply to the decl. Here we apply type attributes and ignore the rest. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3126 | |
| 3127 | AttributeList *next; |
| 3128 | do { |
| 3129 | AttributeList &attr = *attrs; |
| 3130 | next = attr.getNext(); |
| 3131 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3132 | // Skip attributes that were marked to be invalid. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3133 | if (attr.isInvalid()) |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3134 | continue; |
| 3135 | |
Abramo Bagnara | b1f1b26 | 2010-04-30 09:13:03 +0000 | [diff] [blame] | 3136 | // If this is an attribute we can handle, do so now, |
| 3137 | // otherwise, add it to the FnAttrs list for rechaining. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3138 | switch (attr.getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3139 | default: break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3140 | |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3141 | case AttributeList::AT_address_space: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3142 | HandleAddressSpaceTypeAttribute(type, attr, state.getSema()); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3143 | break; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3144 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 3145 | if (!handleObjCPointerTypeAttr(state, attr, type)) |
| 3146 | distributeObjCPointerTypeAttr(state, attr, type); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3147 | break; |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3148 | case AttributeList::AT_vector_size: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3149 | HandleVectorSizeAttr(type, attr, state.getSema()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3150 | break; |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3151 | case AttributeList::AT_neon_vector_type: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3152 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 3153 | VectorType::NeonVector, "neon_vector_type"); |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3154 | break; |
| 3155 | case AttributeList::AT_neon_polyvector_type: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3156 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 3157 | VectorType::NeonPolyVector, |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3158 | "neon_polyvector_type"); |
| 3159 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3160 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3161 | case AttributeList::AT_opencl_image_access: |
| 3162 | HandleOpenCLImageAccessAttribute(type, attr, state.getSema()); |
| 3163 | break; |
| 3164 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3165 | FUNCTION_TYPE_ATTRS_CASELIST: |
| 3166 | // Never process function type attributes as part of the |
| 3167 | // declaration-specifiers. |
| 3168 | if (isDeclSpec) |
| 3169 | distributeFunctionTypeAttrFromDeclSpec(state, attr, type); |
| 3170 | |
| 3171 | // Otherwise, handle the possible delays. |
| 3172 | else if (!handleFunctionTypeAttr(state, attr, type)) |
| 3173 | distributeFunctionTypeAttr(state, attr, type); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3174 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3175 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3176 | } while ((attrs = next)); |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3180 | /// |
| 3181 | /// This routine checks whether the type @p T is complete in any |
| 3182 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 3183 | /// type, returns false. If @p T is a class template specialization, |
| 3184 | /// this routine then attempts to perform class template |
| 3185 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 3186 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 3187 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3188 | /// |
| 3189 | /// @param Loc The location in the source that the incomplete type |
| 3190 | /// diagnostic should refer to. |
| 3191 | /// |
| 3192 | /// @param T The type that this routine is examining for completeness. |
| 3193 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | /// @param PD The partial diagnostic that will be printed out if T is not a |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3195 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3196 | /// |
| 3197 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 3198 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 3199 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3200 | const PartialDiagnostic &PD, |
| 3201 | std::pair<SourceLocation, |
| 3202 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 3203 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3205 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 3206 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3207 | // FIXME: Add this assertion to help us flush out problems with |
| 3208 | // checking for dependent types and type-dependent expressions. |
| 3209 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3211 | // "Can't ask whether a dependent type is complete"); |
| 3212 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3213 | // If we have a complete type, we're done. |
| 3214 | if (!T->isIncompleteType()) |
| 3215 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 3217 | // If we have a class template specialization or a class member of a |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 3218 | // class template specialization, or an array with known size of such, |
| 3219 | // try to instantiate it. |
| 3220 | QualType MaybeTemplate = T; |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3221 | if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 3222 | MaybeTemplate = Array->getElementType(); |
| 3223 | if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3224 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 3225 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 3226 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) |
| 3227 | return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3228 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3229 | /*Complain=*/diag != 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 3231 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 3232 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3233 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 3234 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 3235 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3236 | if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 3237 | != TSK_ExplicitSpecialization) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3238 | return InstantiateClass(Loc, Rec, Pattern, |
| 3239 | getTemplateInstantiationArgs(Rec), |
| 3240 | TSK_ImplicitInstantiation, |
| 3241 | /*Complain=*/diag != 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3246 | if (diag == 0) |
| 3247 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
John McCall | 916c870 | 2010-11-16 01:44:35 +0000 | [diff] [blame] | 3249 | const TagType *Tag = T->getAs<TagType>(); |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Avoid diagnosing invalid decls as incomplete. |
| 3252 | if (Tag && Tag->getDecl()->isInvalidDecl()) |
| 3253 | return true; |
| 3254 | |
John McCall | 916c870 | 2010-11-16 01:44:35 +0000 | [diff] [blame] | 3255 | // Give the external AST source a chance to complete the type. |
| 3256 | if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) { |
| 3257 | Context.getExternalSource()->CompleteType(Tag->getDecl()); |
| 3258 | if (!Tag->isIncompleteType()) |
| 3259 | return false; |
| 3260 | } |
| 3261 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3262 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 3263 | Diag(Loc, PD) << T; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3264 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3265 | // If we have a note, produce it. |
| 3266 | if (!Note.first.isInvalid()) |
| 3267 | Diag(Note.first, Note.second); |
| 3268 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3269 | // If the type was a forward declaration of a class/struct/union |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 3270 | // type, produce a note. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3271 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3273 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 3274 | : diag::note_forward_declaration) |
| 3275 | << QualType(Tag, 0); |
| 3276 | |
| 3277 | return true; |
| 3278 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 3279 | |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3280 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 3281 | const PartialDiagnostic &PD) { |
| 3282 | return RequireCompleteType(Loc, T, PD, |
| 3283 | std::make_pair(SourceLocation(), PDiag(0))); |
| 3284 | } |
| 3285 | |
| 3286 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 3287 | unsigned DiagID) { |
| 3288 | return RequireCompleteType(Loc, T, PDiag(DiagID), |
| 3289 | std::make_pair(SourceLocation(), PDiag(0))); |
| 3290 | } |
| 3291 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3292 | /// \brief Retrieve a version of the type 'T' that is elaborated by Keyword |
| 3293 | /// and qualified by the nested-name-specifier contained in SS. |
| 3294 | QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3295 | const CXXScopeSpec &SS, QualType T) { |
| 3296 | if (T.isNull()) |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 3297 | return T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3298 | NestedNameSpecifier *NNS; |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3299 | if (SS.isValid()) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3300 | NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3301 | else { |
| 3302 | if (Keyword == ETK_None) |
| 3303 | return T; |
| 3304 | NNS = 0; |
| 3305 | } |
| 3306 | return Context.getElaboratedType(Keyword, NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 3307 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 3308 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 3309 | QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) { |
| 3310 | ExprResult ER = CheckPlaceholderExpr(E, Loc); |
| 3311 | if (ER.isInvalid()) return QualType(); |
| 3312 | E = ER.take(); |
| 3313 | |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 3314 | if (!E->isTypeDependent()) { |
| 3315 | QualType T = E->getType(); |
Fariborz Jahanian | aca7f7b | 2010-10-06 00:23:25 +0000 | [diff] [blame] | 3316 | if (const TagType *TT = T->getAs<TagType>()) |
| 3317 | DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc()); |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 3318 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 3319 | return Context.getTypeOfExprType(E); |
| 3320 | } |
| 3321 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 3322 | QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) { |
| 3323 | ExprResult ER = CheckPlaceholderExpr(E, Loc); |
| 3324 | if (ER.isInvalid()) return QualType(); |
| 3325 | E = ER.take(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3326 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 3327 | return Context.getDecltypeType(E); |
| 3328 | } |