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