blob: bbd8a7e93563e61eb92ae9d2b98b4177438e0d7f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor36f255c2011-06-03 14:28:43 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000017#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000019#include "clang/AST/DeclTemplate.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/Expr.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000021#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000022#include "clang/AST/TypeLocVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Basic/OpenCL.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000024#include "clang/Basic/PartialDiagnostic.h"
Charles Davisd18f9f92010-08-16 04:01:50 +000025#include "clang/Basic/TargetInfo.h"
John McCall2792fa52011-03-08 04:17:03 +000026#include "clang/Lex/Preprocessor.h"
Eli Friedmanbc1029b2012-04-05 22:47:34 +000027#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000028#include "clang/Sema/DeclSpec.h"
John McCallf85e1932011-06-15 23:02:42 +000029#include "clang/Sema/DelayedDiagnostic.h"
Douglas Gregord07cc362012-01-02 17:18:37 +000030#include "clang/Sema/Lookup.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000031#include "clang/Sema/ScopeInfo.h"
32#include "clang/Sema/Template.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000033#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000034#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Chris Lattner5db2bb12009-10-25 18:21:37 +000037/// isOmittedBlockReturnType - Return true if this declarator is missing a
Chad Rosier91cbbbf2012-06-26 21:41:40 +000038/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000039static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000040 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000041 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000042 return false;
Chad Rosier91cbbbf2012-06-26 21:41:40 +000043
Chris Lattner5db2bb12009-10-25 18:21:37 +000044 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000045 return true; // ^{ ... }
Chad Rosier91cbbbf2012-06-26 21:41:40 +000046
Chris Lattner5db2bb12009-10-25 18:21:37 +000047 if (D.getNumTypeObjects() == 1 &&
48 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000049 return true; // ^(int X, float Y) { ... }
Chad Rosier91cbbbf2012-06-26 21:41:40 +000050
Chris Lattner5db2bb12009-10-25 18:21:37 +000051 return false;
52}
53
John McCall2792fa52011-03-08 04:17:03 +000054/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
55/// doesn't apply to the given type.
56static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
57 QualType type) {
Chandler Carruth108f7562011-07-26 05:40:03 +000058 bool useExpansionLoc = false;
John McCall2792fa52011-03-08 04:17:03 +000059
60 unsigned diagID = 0;
61 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +000062 case AttributeList::AT_ObjCGC:
John McCall2792fa52011-03-08 04:17:03 +000063 diagID = diag::warn_pointer_attribute_wrong_type;
Chandler Carruth108f7562011-07-26 05:40:03 +000064 useExpansionLoc = true;
John McCall2792fa52011-03-08 04:17:03 +000065 break;
66
Sean Hunt8e083e72012-06-19 23:57:03 +000067 case AttributeList::AT_ObjCOwnership:
Argyrios Kyrtzidis05d48762011-07-01 22:23:09 +000068 diagID = diag::warn_objc_object_attribute_wrong_type;
Chandler Carruth108f7562011-07-26 05:40:03 +000069 useExpansionLoc = true;
Argyrios Kyrtzidis05d48762011-07-01 22:23:09 +000070 break;
71
John McCall2792fa52011-03-08 04:17:03 +000072 default:
73 // Assume everything else was a function attribute.
74 diagID = diag::warn_function_attribute_wrong_type;
75 break;
76 }
77
78 SourceLocation loc = attr.getLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +000079 StringRef name = attr.getName()->getName();
John McCall2792fa52011-03-08 04:17:03 +000080
81 // The GC attributes are usually written with macros; special-case them.
Chandler Carruth108f7562011-07-26 05:40:03 +000082 if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) {
John McCall834e3f62011-03-08 07:59:04 +000083 if (attr.getParameterName()->isStr("strong")) {
84 if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
85 } else if (attr.getParameterName()->isStr("weak")) {
86 if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
John McCall2792fa52011-03-08 04:17:03 +000087 }
88 }
89
90 S.Diag(loc, diagID) << name << type;
91}
92
John McCall711c52b2011-01-05 12:14:39 +000093// objc_gc applies to Objective-C pointers or, otherwise, to the
94// smallest available pointer type (i.e. 'void*' in 'void**').
95#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
Sean Hunt8e083e72012-06-19 23:57:03 +000096 case AttributeList::AT_ObjCGC: \
97 case AttributeList::AT_ObjCOwnership
John McCall04a67a62010-02-05 21:31:56 +000098
John McCall711c52b2011-01-05 12:14:39 +000099// Function type attributes.
100#define FUNCTION_TYPE_ATTRS_CASELIST \
Sean Hunt8e083e72012-06-19 23:57:03 +0000101 case AttributeList::AT_NoReturn: \
102 case AttributeList::AT_CDecl: \
103 case AttributeList::AT_FastCall: \
104 case AttributeList::AT_StdCall: \
105 case AttributeList::AT_ThisCall: \
106 case AttributeList::AT_Pascal: \
107 case AttributeList::AT_Regparm: \
Derek Schuff263366f2012-10-16 22:30:41 +0000108 case AttributeList::AT_Pcs: \
Guy Benyei38980082012-12-25 08:53:55 +0000109 case AttributeList::AT_PnaclCall: \
110 case AttributeList::AT_IntelOclBicc \
John McCall04a67a62010-02-05 21:31:56 +0000111
John McCall711c52b2011-01-05 12:14:39 +0000112namespace {
113 /// An object which stores processing state for the entire
114 /// GetTypeForDeclarator process.
115 class TypeProcessingState {
116 Sema &sema;
117
118 /// The declarator being processed.
119 Declarator &declarator;
120
121 /// The index of the declarator chunk we're currently processing.
122 /// May be the total number of valid chunks, indicating the
123 /// DeclSpec.
124 unsigned chunkIndex;
125
126 /// Whether there are non-trivial modifications to the decl spec.
127 bool trivial;
128
John McCall7ea21932011-03-26 01:39:56 +0000129 /// Whether we saved the attributes in the decl spec.
130 bool hasSavedAttrs;
131
John McCall711c52b2011-01-05 12:14:39 +0000132 /// The original set of attributes on the DeclSpec.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 SmallVector<AttributeList*, 2> savedAttrs;
John McCall711c52b2011-01-05 12:14:39 +0000134
135 /// A list of attributes to diagnose the uselessness of when the
136 /// processing is complete.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 SmallVector<AttributeList*, 2> ignoredTypeAttrs;
John McCall711c52b2011-01-05 12:14:39 +0000138
139 public:
140 TypeProcessingState(Sema &sema, Declarator &declarator)
141 : sema(sema), declarator(declarator),
142 chunkIndex(declarator.getNumTypeObjects()),
John McCall7ea21932011-03-26 01:39:56 +0000143 trivial(true), hasSavedAttrs(false) {}
John McCall711c52b2011-01-05 12:14:39 +0000144
145 Sema &getSema() const {
146 return sema;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000147 }
John McCall711c52b2011-01-05 12:14:39 +0000148
149 Declarator &getDeclarator() const {
150 return declarator;
151 }
152
153 unsigned getCurrentChunkIndex() const {
154 return chunkIndex;
155 }
156
157 void setCurrentChunkIndex(unsigned idx) {
158 assert(idx <= declarator.getNumTypeObjects());
159 chunkIndex = idx;
160 }
161
162 AttributeList *&getCurrentAttrListRef() const {
163 assert(chunkIndex <= declarator.getNumTypeObjects());
164 if (chunkIndex == declarator.getNumTypeObjects())
165 return getMutableDeclSpec().getAttributes().getListRef();
166 return declarator.getTypeObject(chunkIndex).getAttrListRef();
167 }
168
169 /// Save the current set of attributes on the DeclSpec.
170 void saveDeclSpecAttrs() {
171 // Don't try to save them multiple times.
John McCall7ea21932011-03-26 01:39:56 +0000172 if (hasSavedAttrs) return;
John McCall711c52b2011-01-05 12:14:39 +0000173
174 DeclSpec &spec = getMutableDeclSpec();
175 for (AttributeList *attr = spec.getAttributes().getList(); attr;
176 attr = attr->getNext())
177 savedAttrs.push_back(attr);
178 trivial &= savedAttrs.empty();
John McCall7ea21932011-03-26 01:39:56 +0000179 hasSavedAttrs = true;
John McCall711c52b2011-01-05 12:14:39 +0000180 }
181
182 /// Record that we had nowhere to put the given type attribute.
183 /// We will diagnose such attributes later.
184 void addIgnoredTypeAttr(AttributeList &attr) {
185 ignoredTypeAttrs.push_back(&attr);
186 }
187
188 /// Diagnose all the ignored type attributes, given that the
189 /// declarator worked out to the given type.
190 void diagnoseIgnoredTypeAttrs(QualType type) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000191 for (SmallVectorImpl<AttributeList*>::const_iterator
John McCall711c52b2011-01-05 12:14:39 +0000192 i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
John McCall2792fa52011-03-08 04:17:03 +0000193 i != e; ++i)
194 diagnoseBadTypeAttribute(getSema(), **i, type);
John McCall711c52b2011-01-05 12:14:39 +0000195 }
196
197 ~TypeProcessingState() {
198 if (trivial) return;
199
200 restoreDeclSpecAttrs();
201 }
202
203 private:
204 DeclSpec &getMutableDeclSpec() const {
205 return const_cast<DeclSpec&>(declarator.getDeclSpec());
206 }
207
208 void restoreDeclSpecAttrs() {
John McCall7ea21932011-03-26 01:39:56 +0000209 assert(hasSavedAttrs);
210
211 if (savedAttrs.empty()) {
212 getMutableDeclSpec().getAttributes().set(0);
213 return;
214 }
215
John McCall711c52b2011-01-05 12:14:39 +0000216 getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
217 for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
218 savedAttrs[i]->setNext(savedAttrs[i+1]);
219 savedAttrs.back()->setNext(0);
220 }
221 };
222
223 /// Basically std::pair except that we really want to avoid an
224 /// implicit operator= for safety concerns. It's also a minor
225 /// link-time optimization for this to be a private type.
226 struct AttrAndList {
227 /// The attribute.
228 AttributeList &first;
229
230 /// The head of the list the attribute is currently in.
231 AttributeList *&second;
232
233 AttrAndList(AttributeList &attr, AttributeList *&head)
234 : first(attr), second(head) {}
235 };
John McCall04a67a62010-02-05 21:31:56 +0000236}
237
John McCall711c52b2011-01-05 12:14:39 +0000238namespace llvm {
239 template <> struct isPodLike<AttrAndList> {
240 static const bool value = true;
241 };
242}
243
244static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
245 attr.setNext(head);
246 head = &attr;
247}
248
249static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
250 if (head == &attr) {
251 head = attr.getNext();
252 return;
John McCall04a67a62010-02-05 21:31:56 +0000253 }
John McCall711c52b2011-01-05 12:14:39 +0000254
255 AttributeList *cur = head;
256 while (true) {
257 assert(cur && cur->getNext() && "ran out of attrs?");
258 if (cur->getNext() == &attr) {
259 cur->setNext(attr.getNext());
260 return;
261 }
262 cur = cur->getNext();
263 }
264}
265
266static void moveAttrFromListToList(AttributeList &attr,
267 AttributeList *&fromList,
268 AttributeList *&toList) {
269 spliceAttrOutOfList(attr, fromList);
270 spliceAttrIntoList(attr, toList);
271}
272
273static void processTypeAttrs(TypeProcessingState &state,
274 QualType &type, bool isDeclSpec,
275 AttributeList *attrs);
276
277static bool handleFunctionTypeAttr(TypeProcessingState &state,
278 AttributeList &attr,
279 QualType &type);
280
281static bool handleObjCGCTypeAttr(TypeProcessingState &state,
282 AttributeList &attr, QualType &type);
283
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000284static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
John McCallf85e1932011-06-15 23:02:42 +0000285 AttributeList &attr, QualType &type);
286
John McCall711c52b2011-01-05 12:14:39 +0000287static bool handleObjCPointerTypeAttr(TypeProcessingState &state,
288 AttributeList &attr, QualType &type) {
Sean Hunt8e083e72012-06-19 23:57:03 +0000289 if (attr.getKind() == AttributeList::AT_ObjCGC)
John McCallf85e1932011-06-15 23:02:42 +0000290 return handleObjCGCTypeAttr(state, attr, type);
Sean Hunt8e083e72012-06-19 23:57:03 +0000291 assert(attr.getKind() == AttributeList::AT_ObjCOwnership);
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000292 return handleObjCOwnershipTypeAttr(state, attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000293}
294
295/// Given that an objc_gc attribute was written somewhere on a
296/// declaration *other* than on the declarator itself (for which, use
297/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
298/// didn't apply in whatever position it was written in, try to move
299/// it to a more appropriate position.
300static void distributeObjCPointerTypeAttr(TypeProcessingState &state,
301 AttributeList &attr,
302 QualType type) {
303 Declarator &declarator = state.getDeclarator();
304 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
305 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
306 switch (chunk.Kind) {
307 case DeclaratorChunk::Pointer:
308 case DeclaratorChunk::BlockPointer:
309 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
310 chunk.getAttrListRef());
311 return;
312
313 case DeclaratorChunk::Paren:
314 case DeclaratorChunk::Array:
315 continue;
316
317 // Don't walk through these.
318 case DeclaratorChunk::Reference:
319 case DeclaratorChunk::Function:
320 case DeclaratorChunk::MemberPointer:
321 goto error;
322 }
323 }
324 error:
John McCall2792fa52011-03-08 04:17:03 +0000325
326 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000327}
328
329/// Distribute an objc_gc type attribute that was written on the
330/// declarator.
331static void
332distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
333 AttributeList &attr,
334 QualType &declSpecType) {
335 Declarator &declarator = state.getDeclarator();
336
337 // objc_gc goes on the innermost pointer to something that's not a
338 // pointer.
339 unsigned innermost = -1U;
340 bool considerDeclSpec = true;
341 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
342 DeclaratorChunk &chunk = declarator.getTypeObject(i);
343 switch (chunk.Kind) {
344 case DeclaratorChunk::Pointer:
345 case DeclaratorChunk::BlockPointer:
346 innermost = i;
John McCallae278a32011-01-12 00:34:59 +0000347 continue;
John McCall711c52b2011-01-05 12:14:39 +0000348
349 case DeclaratorChunk::Reference:
350 case DeclaratorChunk::MemberPointer:
351 case DeclaratorChunk::Paren:
352 case DeclaratorChunk::Array:
353 continue;
354
355 case DeclaratorChunk::Function:
356 considerDeclSpec = false;
357 goto done;
358 }
359 }
360 done:
361
362 // That might actually be the decl spec if we weren't blocked by
363 // anything in the declarator.
364 if (considerDeclSpec) {
John McCall7ea21932011-03-26 01:39:56 +0000365 if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
366 // Splice the attribute into the decl spec. Prevents the
367 // attribute from being applied multiple times and gives
368 // the source-location-filler something to work with.
369 state.saveDeclSpecAttrs();
370 moveAttrFromListToList(attr, declarator.getAttrListRef(),
371 declarator.getMutableDeclSpec().getAttributes().getListRef());
John McCall711c52b2011-01-05 12:14:39 +0000372 return;
John McCall7ea21932011-03-26 01:39:56 +0000373 }
John McCall711c52b2011-01-05 12:14:39 +0000374 }
375
376 // Otherwise, if we found an appropriate chunk, splice the attribute
377 // into it.
378 if (innermost != -1U) {
379 moveAttrFromListToList(attr, declarator.getAttrListRef(),
380 declarator.getTypeObject(innermost).getAttrListRef());
381 return;
382 }
383
384 // Otherwise, diagnose when we're done building the type.
385 spliceAttrOutOfList(attr, declarator.getAttrListRef());
386 state.addIgnoredTypeAttr(attr);
387}
388
389/// A function type attribute was written somewhere in a declaration
390/// *other* than on the declarator itself or in the decl spec. Given
391/// that it didn't apply in whatever position it was written in, try
392/// to move it to a more appropriate position.
393static void distributeFunctionTypeAttr(TypeProcessingState &state,
394 AttributeList &attr,
395 QualType type) {
396 Declarator &declarator = state.getDeclarator();
397
398 // Try to push the attribute from the return type of a function to
399 // the function itself.
400 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
401 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
402 switch (chunk.Kind) {
403 case DeclaratorChunk::Function:
404 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
405 chunk.getAttrListRef());
406 return;
407
408 case DeclaratorChunk::Paren:
409 case DeclaratorChunk::Pointer:
410 case DeclaratorChunk::BlockPointer:
411 case DeclaratorChunk::Array:
412 case DeclaratorChunk::Reference:
413 case DeclaratorChunk::MemberPointer:
414 continue;
415 }
416 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000417
John McCall2792fa52011-03-08 04:17:03 +0000418 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000419}
420
421/// Try to distribute a function type attribute to the innermost
422/// function chunk or type. Returns true if the attribute was
423/// distributed, false if no location was found.
424static bool
425distributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
426 AttributeList &attr,
427 AttributeList *&attrList,
428 QualType &declSpecType) {
429 Declarator &declarator = state.getDeclarator();
430
431 // Put it on the innermost function chunk, if there is one.
432 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
433 DeclaratorChunk &chunk = declarator.getTypeObject(i);
434 if (chunk.Kind != DeclaratorChunk::Function) continue;
435
436 moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
437 return true;
438 }
439
John McCallf85e1932011-06-15 23:02:42 +0000440 if (handleFunctionTypeAttr(state, attr, declSpecType)) {
441 spliceAttrOutOfList(attr, attrList);
442 return true;
443 }
444
445 return false;
John McCall711c52b2011-01-05 12:14:39 +0000446}
447
448/// A function type attribute was written in the decl spec. Try to
449/// apply it somewhere.
450static void
451distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
452 AttributeList &attr,
453 QualType &declSpecType) {
454 state.saveDeclSpecAttrs();
455
456 // Try to distribute to the innermost.
457 if (distributeFunctionTypeAttrToInnermost(state, attr,
458 state.getCurrentAttrListRef(),
459 declSpecType))
460 return;
461
462 // If that failed, diagnose the bad attribute when the declarator is
463 // fully built.
464 state.addIgnoredTypeAttr(attr);
465}
466
467/// A function type attribute was written on the declarator. Try to
468/// apply it somewhere.
469static void
470distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
471 AttributeList &attr,
472 QualType &declSpecType) {
473 Declarator &declarator = state.getDeclarator();
474
475 // Try to distribute to the innermost.
476 if (distributeFunctionTypeAttrToInnermost(state, attr,
477 declarator.getAttrListRef(),
478 declSpecType))
479 return;
480
481 // If that failed, diagnose the bad attribute when the declarator is
482 // fully built.
483 spliceAttrOutOfList(attr, declarator.getAttrListRef());
484 state.addIgnoredTypeAttr(attr);
485}
486
487/// \brief Given that there are attributes written on the declarator
488/// itself, try to distribute any type attributes to the appropriate
489/// declarator chunk.
490///
491/// These are attributes like the following:
492/// int f ATTR;
493/// int (f ATTR)();
494/// but not necessarily this:
495/// int f() ATTR;
496static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
497 QualType &declSpecType) {
498 // Collect all the type attributes from the declarator itself.
499 assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
500 AttributeList *attr = state.getDeclarator().getAttributes();
501 AttributeList *next;
502 do {
503 next = attr->getNext();
504
505 switch (attr->getKind()) {
506 OBJC_POINTER_TYPE_ATTRS_CASELIST:
507 distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
508 break;
509
Sean Hunt8e083e72012-06-19 23:57:03 +0000510 case AttributeList::AT_NSReturnsRetained:
David Blaikie4e4d0842012-03-11 07:00:24 +0000511 if (!state.getSema().getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000512 break;
513 // fallthrough
514
John McCall711c52b2011-01-05 12:14:39 +0000515 FUNCTION_TYPE_ATTRS_CASELIST:
516 distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
517 break;
518
519 default:
520 break;
521 }
522 } while ((attr = next));
523}
524
525/// Add a synthetic '()' to a block-literal declarator if it is
526/// required, given the return type.
527static void maybeSynthesizeBlockSignature(TypeProcessingState &state,
528 QualType declSpecType) {
529 Declarator &declarator = state.getDeclarator();
530
531 // First, check whether the declarator would produce a function,
532 // i.e. whether the innermost semantic chunk is a function.
533 if (declarator.isFunctionDeclarator()) {
534 // If so, make that declarator a prototyped declarator.
535 declarator.getFunctionTypeInfo().hasPrototype = true;
536 return;
537 }
538
John McCallda263792011-02-08 01:59:10 +0000539 // If there are any type objects, the type as written won't name a
540 // function, regardless of the decl spec type. This is because a
541 // block signature declarator is always an abstract-declarator, and
542 // abstract-declarators can't just be parentheses chunks. Therefore
543 // we need to build a function chunk unless there are no type
544 // objects and the decl spec type is a function.
John McCall711c52b2011-01-05 12:14:39 +0000545 if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
546 return;
547
John McCallda263792011-02-08 01:59:10 +0000548 // Note that there *are* cases with invalid declarators where
549 // declarators consist solely of parentheses. In general, these
550 // occur only in failed efforts to make function declarators, so
551 // faking up the function chunk is still the right thing to do.
John McCall711c52b2011-01-05 12:14:39 +0000552
553 // Otherwise, we need to fake up a function declarator.
Daniel Dunbar96a00142012-03-09 18:35:03 +0000554 SourceLocation loc = declarator.getLocStart();
John McCall711c52b2011-01-05 12:14:39 +0000555
556 // ...and *prepend* it to the declarator.
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000557 SourceLocation NoLoc;
John McCall711c52b2011-01-05 12:14:39 +0000558 declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000559 /*HasProto=*/true,
560 /*IsAmbiguous=*/false,
561 /*LParenLoc=*/NoLoc,
562 /*ArgInfo=*/0,
563 /*NumArgs=*/0,
564 /*EllipsisLoc=*/NoLoc,
565 /*RParenLoc=*/NoLoc,
566 /*TypeQuals=*/0,
567 /*RefQualifierIsLvalueRef=*/true,
568 /*RefQualifierLoc=*/NoLoc,
569 /*ConstQualifierLoc=*/NoLoc,
570 /*VolatileQualifierLoc=*/NoLoc,
571 /*MutableLoc=*/NoLoc,
572 EST_None,
573 /*ESpecLoc=*/NoLoc,
574 /*Exceptions=*/0,
575 /*ExceptionRanges=*/0,
576 /*NumExceptions=*/0,
577 /*NoexceptExpr=*/0,
578 loc, loc, declarator));
John McCall711c52b2011-01-05 12:14:39 +0000579
580 // For consistency, make sure the state still has us as processing
581 // the decl spec.
582 assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
583 state.setCurrentChunkIndex(declarator.getNumTypeObjects());
John McCall04a67a62010-02-05 21:31:56 +0000584}
585
Douglas Gregor930d8b52009-01-30 22:09:00 +0000586/// \brief Convert the specified declspec to the appropriate type
587/// object.
James Dennett1dfbd922012-06-14 21:40:34 +0000588/// \param state Specifies the declarator containing the declaration specifier
589/// to be converted, along with other associated processing state.
Chris Lattner5153ee62009-04-25 08:47:54 +0000590/// \returns The type described by the declaration specifiers. This function
591/// never returns null.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +0000592static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
594 // checking.
John McCall711c52b2011-01-05 12:14:39 +0000595
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +0000596 Sema &S = state.getSema();
John McCall711c52b2011-01-05 12:14:39 +0000597 Declarator &declarator = state.getDeclarator();
598 const DeclSpec &DS = declarator.getDeclSpec();
599 SourceLocation DeclLoc = declarator.getIdentifierLoc();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000600 if (DeclLoc.isInvalid())
Daniel Dunbar96a00142012-03-09 18:35:03 +0000601 DeclLoc = DS.getLocStart();
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000602
John McCall711c52b2011-01-05 12:14:39 +0000603 ASTContext &Context = S.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattner5db2bb12009-10-25 18:21:37 +0000605 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000607 case DeclSpec::TST_void:
608 Result = Context.VoidTy;
609 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 case DeclSpec::TST_char:
611 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000612 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000614 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 else {
616 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
617 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000618 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 }
Chris Lattner958858e2008-02-20 21:40:32 +0000620 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000621 case DeclSpec::TST_wchar:
622 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
623 Result = Context.WCharTy;
624 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
John McCall711c52b2011-01-05 12:14:39 +0000625 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000626 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000627 Result = Context.getSignedWCharType();
628 } else {
629 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
630 "Unknown TSS value");
John McCall711c52b2011-01-05 12:14:39 +0000631 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000632 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000633 Result = Context.getUnsignedWCharType();
634 }
635 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000636 case DeclSpec::TST_char16:
637 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
638 "Unknown TSS value");
639 Result = Context.Char16Ty;
640 break;
641 case DeclSpec::TST_char32:
642 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
643 "Unknown TSS value");
644 Result = Context.Char32Ty;
645 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000646 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000647 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000648 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000649 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
Roman Divacky31ba6132012-09-06 15:59:27 +0000650 (ObjCProtocolDecl*const*)PQ,
John McCallc12c5bb2010-05-15 11:32:37 +0000651 DS.getNumProtocolQualifiers());
652 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000653 break;
654 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000655
Chris Lattner5db2bb12009-10-25 18:21:37 +0000656 // If this is a missing declspec in a block literal return context, then it
657 // is inferred from the return statements inside the block.
Eli Friedmanf88c4002012-01-04 04:41:38 +0000658 // The declspec is always missing in a lambda expr context; it is either
659 // specified with a trailing return type or inferred.
660 if (declarator.getContext() == Declarator::LambdaExprContext ||
661 isOmittedBlockReturnType(declarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000662 Result = Context.DependentTy;
663 break;
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Chris Lattnerd658b562008-04-05 06:32:51 +0000666 // Unspecified typespec defaults to int in C90. However, the C90 grammar
667 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
668 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
669 // Note that the one exception to this is function definitions, which are
670 // allowed to be completely missing a declspec. This is handled in the
671 // parser already though by it pretending to have seen an 'int' in this
672 // case.
David Blaikie4e4d0842012-03-11 07:00:24 +0000673 if (S.getLangOpts().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000674 // In C89 mode, we only warn if there is a completely missing declspec
675 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000676 if (DS.isEmpty()) {
John McCall711c52b2011-01-05 12:14:39 +0000677 S.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000678 << DS.getSourceRange()
Daniel Dunbar96a00142012-03-09 18:35:03 +0000679 << FixItHint::CreateInsertion(DS.getLocStart(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000680 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000681 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000682 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
683 // "At least one type specifier shall be given in the declaration
684 // specifiers in each declaration, and in the specifier-qualifier list in
685 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000686 // FIXME: Does Microsoft really have the implicit int extension in C++?
David Blaikie4e4d0842012-03-11 07:00:24 +0000687 if (S.getLangOpts().CPlusPlus &&
688 !S.getLangOpts().MicrosoftExt) {
John McCall711c52b2011-01-05 12:14:39 +0000689 S.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000690 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnerb78d8332009-06-26 04:45:06 +0000692 // When this occurs in C++ code, often something is very broken with the
693 // value being declared, poison it as invalid so we don't get chains of
694 // errors.
John McCall711c52b2011-01-05 12:14:39 +0000695 declarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000696 } else {
John McCall711c52b2011-01-05 12:14:39 +0000697 S.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000698 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000699 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
702 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000703 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
705 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000706 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
707 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
708 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000709 case DeclSpec::TSW_longlong:
710 Result = Context.LongLongTy;
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000711
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000712 // 'long long' is a C99 or C++11 feature.
713 if (!S.getLangOpts().C99) {
714 if (S.getLangOpts().CPlusPlus)
715 S.Diag(DS.getTypeSpecWidthLoc(),
716 S.getLangOpts().CPlusPlus0x ?
717 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
718 else
719 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
720 }
Chris Lattner311157f2009-10-25 18:25:04 +0000721 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 }
723 } else {
724 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000725 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
726 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
727 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000728 case DeclSpec::TSW_longlong:
729 Result = Context.UnsignedLongLongTy;
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000730
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000731 // 'long long' is a C99 or C++11 feature.
732 if (!S.getLangOpts().C99) {
733 if (S.getLangOpts().CPlusPlus)
734 S.Diag(DS.getTypeSpecWidthLoc(),
735 S.getLangOpts().CPlusPlus0x ?
736 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
737 else
738 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong);
739 }
Chris Lattner311157f2009-10-25 18:25:04 +0000740 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
742 }
Chris Lattner958858e2008-02-20 21:40:32 +0000743 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000744 }
Richard Smith5a5a9712012-04-04 06:24:32 +0000745 case DeclSpec::TST_int128:
Richard Smith84268902012-11-29 05:41:51 +0000746 if (!S.PP.getTargetInfo().hasInt128Type())
747 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported);
Richard Smith5a5a9712012-04-04 06:24:32 +0000748 if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned)
749 Result = Context.UnsignedInt128Ty;
750 else
751 Result = Context.Int128Ty;
752 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000753 case DeclSpec::TST_half: Result = Context.HalfTy; break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000754 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000755 case DeclSpec::TST_double:
756 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000757 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000758 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000759 Result = Context.DoubleTy;
Peter Collingbourne39d3e7a2011-02-15 19:46:23 +0000760
David Blaikie4e4d0842012-03-11 07:00:24 +0000761 if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
Peter Collingbourne39d3e7a2011-02-15 19:46:23 +0000762 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
763 declarator.setInvalidType(true);
764 }
Chris Lattner958858e2008-02-20 21:40:32 +0000765 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000766 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 case DeclSpec::TST_decimal32: // _Decimal32
768 case DeclSpec::TST_decimal64: // _Decimal64
769 case DeclSpec::TST_decimal128: // _Decimal128
John McCall711c52b2011-01-05 12:14:39 +0000770 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000771 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000772 declarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000773 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000774 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 case DeclSpec::TST_enum:
776 case DeclSpec::TST_union:
Joao Matos6666ed42012-08-31 18:45:21 +0000777 case DeclSpec::TST_struct:
778 case DeclSpec::TST_interface: {
John McCallb3d87482010-08-24 05:47:05 +0000779 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000780 if (!D) {
781 // This can happen in C++ with ambiguous lookups.
782 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000783 declarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000784 break;
785 }
786
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000787 // If the type is deprecated or unavailable, diagnose it.
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000788 S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000791 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000794 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000795
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000796 // In both C and C++, make an ElaboratedType.
797 ElaboratedTypeKeyword Keyword
798 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
799 Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000800 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000801 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000802 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
804 DS.getTypeSpecSign() == 0 &&
805 "Can't handle qualifiers on typedef names yet!");
John McCall711c52b2011-01-05 12:14:39 +0000806 Result = S.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000807 if (Result.isNull())
John McCall711c52b2011-01-05 12:14:39 +0000808 declarator.setInvalidType(true);
John McCall27940d22010-07-30 05:17:22 +0000809 else if (DeclSpec::ProtocolQualifierListTy PQ
810 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000811 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
812 // Silently drop any existing protocol qualifiers.
813 // TODO: determine whether that's the right thing to do.
814 if (ObjT->getNumProtocols())
815 Result = ObjT->getBaseType();
816
817 if (DS.getNumProtocolQualifiers())
818 Result = Context.getObjCObjectType(Result,
Roman Divacky31ba6132012-09-06 15:59:27 +0000819 (ObjCProtocolDecl*const*) PQ,
John McCallc12c5bb2010-05-15 11:32:37 +0000820 DS.getNumProtocolQualifiers());
821 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000822 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000823 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
Roman Divacky31ba6132012-09-06 15:59:27 +0000824 (ObjCProtocolDecl*const*) PQ,
John McCallc12c5bb2010-05-15 11:32:37 +0000825 DS.getNumProtocolQualifiers());
826 Result = Context.getObjCObjectPointerType(Result);
827 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000828 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000829 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
Roman Divacky31ba6132012-09-06 15:59:27 +0000830 (ObjCProtocolDecl*const*) PQ,
John McCallc12c5bb2010-05-15 11:32:37 +0000831 DS.getNumProtocolQualifiers());
832 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000833 } else {
John McCall711c52b2011-01-05 12:14:39 +0000834 S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000835 << DS.getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +0000836 declarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000837 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000841 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 }
Chris Lattner958858e2008-02-20 21:40:32 +0000843 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000844 // FIXME: Preserve type source info.
John McCall711c52b2011-01-05 12:14:39 +0000845 Result = S.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000846 assert(!Result.isNull() && "Didn't get a type for typeof?");
Fariborz Jahanian730e1752010-10-06 17:00:02 +0000847 if (!Result->isDependentType())
848 if (const TagType *TT = Result->getAs<TagType>())
John McCall711c52b2011-01-05 12:14:39 +0000849 S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
Steve Naroffd1861fd2007-07-31 12:34:36 +0000850 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000851 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000852 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000853 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000854 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000855 assert(E && "Didn't get an expression for typeof?");
856 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000857 Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
Douglas Gregor4b52e252009-12-21 23:17:24 +0000858 if (Result.isNull()) {
859 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000860 declarator.setInvalidType(true);
Douglas Gregor4b52e252009-12-21 23:17:24 +0000861 }
Chris Lattner958858e2008-02-20 21:40:32 +0000862 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000863 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000864 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000865 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000866 assert(E && "Didn't get an expression for decltype?");
867 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000868 Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
Anders Carlssonaf017e62009-06-29 22:58:55 +0000869 if (Result.isNull()) {
870 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000871 declarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000872 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000873 break;
874 }
Sean Huntca63c202011-05-24 22:41:36 +0000875 case DeclSpec::TST_underlyingType:
Sean Huntdb5d44b2011-05-19 05:37:45 +0000876 Result = S.GetTypeFromParser(DS.getRepAsType());
877 assert(!Result.isNull() && "Didn't get a type for __underlying_type?");
Sean Huntca63c202011-05-24 22:41:36 +0000878 Result = S.BuildUnaryTransformType(Result,
879 UnaryTransformType::EnumUnderlyingType,
880 DS.getTypeSpecTypeLoc());
881 if (Result.isNull()) {
882 Result = Context.IntTy;
883 declarator.setInvalidType(true);
Sean Huntdb5d44b2011-05-19 05:37:45 +0000884 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000885 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +0000886
Anders Carlssone89d1592009-06-26 18:41:36 +0000887 case DeclSpec::TST_auto: {
888 // TypeQuals handled by caller.
Richard Smith34b41d92011-02-20 03:19:35 +0000889 Result = Context.getAutoType(QualType());
Anders Carlssone89d1592009-06-26 18:41:36 +0000890 break;
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
John McCalla5fc4722011-04-09 22:50:59 +0000893 case DeclSpec::TST_unknown_anytype:
894 Result = Context.UnknownAnyTy;
895 break;
896
Eli Friedmanb001de72011-10-06 23:00:33 +0000897 case DeclSpec::TST_atomic:
898 Result = S.GetTypeFromParser(DS.getRepAsType());
899 assert(!Result.isNull() && "Didn't get a type for _Atomic?");
900 Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc());
901 if (Result.isNull()) {
902 Result = Context.IntTy;
903 declarator.setInvalidType(true);
904 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +0000905 break;
Eli Friedmanb001de72011-10-06 23:00:33 +0000906
Guy Benyeib13621d2012-12-18 14:38:23 +0000907 case DeclSpec::TST_image1d_t:
908 Result = Context.OCLImage1dTy;
909 break;
910
911 case DeclSpec::TST_image1d_array_t:
912 Result = Context.OCLImage1dArrayTy;
913 break;
914
915 case DeclSpec::TST_image1d_buffer_t:
916 Result = Context.OCLImage1dBufferTy;
917 break;
918
919 case DeclSpec::TST_image2d_t:
920 Result = Context.OCLImage2dTy;
921 break;
922
923 case DeclSpec::TST_image2d_array_t:
924 Result = Context.OCLImage2dArrayTy;
925 break;
926
927 case DeclSpec::TST_image3d_t:
928 Result = Context.OCLImage3dTy;
929 break;
930
Douglas Gregor809070a2009-02-18 17:45:20 +0000931 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000932 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000933 declarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000934 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner958858e2008-02-20 21:40:32 +0000937 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000938 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000939 if (S.getLangOpts().Freestanding)
John McCall711c52b2011-01-05 12:14:39 +0000940 S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000941 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000942 } else if (DS.isTypeAltiVecVector()) {
943 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
944 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Bob Wilsone86d78c2010-11-10 21:56:12 +0000945 VectorType::VectorKind VecKind = VectorType::AltiVecVector;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000946 if (DS.isTypeAltiVecPixel())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000947 VecKind = VectorType::AltiVecPixel;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000948 else if (DS.isTypeAltiVecBool())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000949 VecKind = VectorType::AltiVecBool;
950 Result = Context.getVectorType(Result, 128/typeSize, VecKind);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Argyrios Kyrtzidis47423bd2010-09-23 09:40:31 +0000953 // FIXME: Imaginary.
954 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
John McCall711c52b2011-01-05 12:14:39 +0000955 S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
John McCall711c52b2011-01-05 12:14:39 +0000957 // Before we process any type attributes, synthesize a block literal
958 // function declarator if necessary.
959 if (declarator.getContext() == Declarator::BlockLiteralContext)
960 maybeSynthesizeBlockSignature(state, Result);
961
962 // Apply any type attributes from the decl spec. This may cause the
963 // list of type attributes to be temporarily saved while the type
964 // attributes are pushed around.
965 if (AttributeList *attrs = DS.getAttributes().getList())
966 processTypeAttrs(state, Result, true, attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner96b77fc2008-04-02 06:50:17 +0000968 // Apply const/volatile/restrict qualifiers to T.
969 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
970
971 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
972 // or incomplete types shall not be restrict-qualified." C++ also allows
973 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000974 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000975 if (Result->isAnyPointerType() || Result->isReferenceType()) {
976 QualType EltTy;
977 if (Result->isObjCObjectPointerType())
978 EltTy = Result;
979 else
980 EltTy = Result->isPointerType() ?
981 Result->getAs<PointerType>()->getPointeeType() :
982 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Douglas Gregorbad0e652009-03-24 20:32:41 +0000984 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000985 // incomplete type.
986 if (!EltTy->isIncompleteOrObjectType()) {
John McCall711c52b2011-01-05 12:14:39 +0000987 S.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000988 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000989 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000990 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000991 }
992 } else {
John McCall711c52b2011-01-05 12:14:39 +0000993 S.Diag(DS.getRestrictSpecLoc(),
994 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000995 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000996 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000997 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner96b77fc2008-04-02 06:50:17 +00001000 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
1001 // of a function type includes any type qualifiers, the behavior is
1002 // undefined."
1003 if (Result->isFunctionType() && TypeQuals) {
1004 // Get some location to point at, either the C or V location.
1005 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +00001006 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +00001007 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +00001008 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +00001009 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +00001010 else {
1011 assert((TypeQuals & DeclSpec::TQ_restrict) &&
1012 "Has CVR quals but not C, V, or R?");
1013 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +00001014 }
John McCall711c52b2011-01-05 12:14:39 +00001015 S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +00001016 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +00001017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001019 // C++ [dcl.ref]p1:
1020 // Cv-qualified references are ill-formed except when the
1021 // cv-qualifiers are introduced through the use of a typedef
1022 // (7.1.3) or of a template type argument (14.3), in which
1023 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001024 // FIXME: Shouldn't we be checking SCS_typedef here?
1025 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001026 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +00001027 TypeQuals &= ~DeclSpec::TQ_const;
1028 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +00001029 }
1030
Eli Friedmanbc1029b2012-04-05 22:47:34 +00001031 // C90 6.5.3 constraints: "The same type qualifier shall not appear more
1032 // than once in the same specifier-list or qualifier-list, either directly
1033 // or via one or more typedefs."
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001034 if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus
Eli Friedmanbc1029b2012-04-05 22:47:34 +00001035 && TypeQuals & Result.getCVRQualifiers()) {
1036 if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001037 S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec)
Eli Friedmanbc1029b2012-04-05 22:47:34 +00001038 << "const";
1039 }
1040
1041 if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001042 S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec)
Eli Friedmanbc1029b2012-04-05 22:47:34 +00001043 << "volatile";
1044 }
1045
1046 // C90 doesn't have restrict, so it doesn't force us to produce a warning
1047 // in this case.
1048 }
1049
John McCall0953e762009-09-24 19:53:00 +00001050 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
1051 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +00001052 }
John McCall0953e762009-09-24 19:53:00 +00001053
Chris Lattnerf1d705c2008-02-21 01:07:18 +00001054 return Result;
1055}
1056
Douglas Gregorcd281c32009-02-28 00:25:32 +00001057static std::string getPrintableNameForEntity(DeclarationName Entity) {
1058 if (Entity)
1059 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Douglas Gregorcd281c32009-02-28 00:25:32 +00001061 return "type name";
1062}
1063
John McCall28654742010-06-05 06:41:15 +00001064QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
1065 Qualifiers Qs) {
1066 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
1067 // object or incomplete types shall not be restrict-qualified."
1068 if (Qs.hasRestrict()) {
1069 unsigned DiagID = 0;
1070 QualType ProblemTy;
1071
1072 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
1073 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
1074 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
1075 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1076 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
1077 }
1078 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
1079 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
1080 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1081 ProblemTy = T->getAs<PointerType>()->getPointeeType();
1082 }
1083 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
1084 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
1085 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1086 ProblemTy = T->getAs<PointerType>()->getPointeeType();
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001087 }
John McCall28654742010-06-05 06:41:15 +00001088 } else if (!Ty->isDependentType()) {
1089 // FIXME: this deserves a proper diagnostic
1090 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1091 ProblemTy = T;
1092 }
1093
1094 if (DiagID) {
1095 Diag(Loc, DiagID) << ProblemTy;
1096 Qs.removeRestrict();
1097 }
1098 }
1099
1100 return Context.getQualifiedType(T, Qs);
1101}
1102
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001103/// \brief Build a paren type including \p T.
1104QualType Sema::BuildParenType(QualType T) {
1105 return Context.getParenType(T);
1106}
1107
John McCallf85e1932011-06-15 23:02:42 +00001108/// Given that we're building a pointer or reference to the given
1109static QualType inferARCLifetimeForPointee(Sema &S, QualType type,
1110 SourceLocation loc,
1111 bool isReference) {
1112 // Bail out if retention is unrequired or already specified.
1113 if (!type->isObjCLifetimeType() ||
1114 type.getObjCLifetime() != Qualifiers::OCL_None)
1115 return type;
1116
1117 Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None;
1118
1119 // If the object type is const-qualified, we can safely use
1120 // __unsafe_unretained. This is safe (because there are no read
1121 // barriers), and it'll be safe to coerce anything but __weak* to
1122 // the resulting type.
1123 if (type.isConstQualified()) {
1124 implicitLifetime = Qualifiers::OCL_ExplicitNone;
1125
1126 // Otherwise, check whether the static type does not require
1127 // retaining. This currently only triggers for Class (possibly
1128 // protocol-qualifed, and arrays thereof).
1129 } else if (type->isObjCARCImplicitlyUnretainedType()) {
1130 implicitLifetime = Qualifiers::OCL_ExplicitNone;
1131
Eli Friedmanef331b72012-01-20 01:26:23 +00001132 // If we are in an unevaluated context, like sizeof, skip adding a
1133 // qualification.
David Blaikie71f55f72012-08-06 22:47:24 +00001134 } else if (S.isUnevaluatedContext()) {
Eli Friedmanef331b72012-01-20 01:26:23 +00001135 return type;
Argyrios Kyrtzidis5b76f372011-09-20 23:49:22 +00001136
John McCalle8c904f2012-01-26 20:04:03 +00001137 // If that failed, give an error and recover using __strong. __strong
1138 // is the option most likely to prevent spurious second-order diagnostics,
1139 // like when binding a reference to a field.
John McCallf85e1932011-06-15 23:02:42 +00001140 } else {
1141 // These types can show up in private ivars in system headers, so
1142 // we need this to not be an error in those cases. Instead we
1143 // want to delay.
1144 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
Eli Friedmanef331b72012-01-20 01:26:23 +00001145 S.DelayedDiagnostics.add(
1146 sema::DelayedDiagnostic::makeForbiddenType(loc,
1147 diag::err_arc_indirect_no_ownership, type, isReference));
John McCallf85e1932011-06-15 23:02:42 +00001148 } else {
Eli Friedmanef331b72012-01-20 01:26:23 +00001149 S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference;
John McCallf85e1932011-06-15 23:02:42 +00001150 }
John McCalle8c904f2012-01-26 20:04:03 +00001151 implicitLifetime = Qualifiers::OCL_Strong;
John McCallf85e1932011-06-15 23:02:42 +00001152 }
1153 assert(implicitLifetime && "didn't infer any lifetime!");
1154
1155 Qualifiers qs;
1156 qs.addObjCLifetime(implicitLifetime);
1157 return S.Context.getQualifiedType(type, qs);
1158}
1159
Douglas Gregorcd281c32009-02-28 00:25:32 +00001160/// \brief Build a pointer type.
1161///
1162/// \param T The type to which we'll be building a pointer.
1163///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001164/// \param Loc The location of the entity whose type involves this
1165/// pointer type or, if there is no such entity, the location of the
1166/// type that will have pointer type.
1167///
1168/// \param Entity The name of the entity that involves the pointer
1169/// type, if known.
1170///
1171/// \returns A suitable pointer type, if there are no
1172/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +00001173QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001174 SourceLocation Loc, DeclarationName Entity) {
1175 if (T->isReferenceType()) {
1176 // C++ 8.3.2p4: There shall be no ... pointers to references ...
1177 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001178 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001179 return QualType();
1180 }
1181
John McCallc12c5bb2010-05-15 11:32:37 +00001182 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +00001183
John McCallf85e1932011-06-15 23:02:42 +00001184 // In ARC, it is forbidden to build pointers to unqualified pointers.
David Blaikie4e4d0842012-03-11 07:00:24 +00001185 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001186 T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false);
1187
Douglas Gregorcd281c32009-02-28 00:25:32 +00001188 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +00001189 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001190}
1191
1192/// \brief Build a reference type.
1193///
1194/// \param T The type to which we'll be building a reference.
1195///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001196/// \param Loc The location of the entity whose type involves this
1197/// reference type or, if there is no such entity, the location of the
1198/// type that will have reference type.
1199///
1200/// \param Entity The name of the entity that involves the reference
1201/// type, if known.
1202///
1203/// \returns A suitable reference type, if there are no
1204/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +00001205QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +00001206 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +00001207 DeclarationName Entity) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001208 assert(Context.getCanonicalType(T) != Context.OverloadTy &&
Douglas Gregor9625e442011-05-21 22:16:50 +00001209 "Unresolved overloaded function type");
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001210
Douglas Gregor69d83162011-01-20 16:08:06 +00001211 // C++0x [dcl.ref]p6:
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001212 // If a typedef (7.1.3), a type template-parameter (14.3.1), or a
1213 // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
1214 // type T, an attempt to create the type "lvalue reference to cv TR" creates
1215 // the type "lvalue reference to T", while an attempt to create the type
Douglas Gregor69d83162011-01-20 16:08:06 +00001216 // "rvalue reference to cv TR" creates the type TR.
John McCall54e14c42009-10-22 22:37:11 +00001217 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
1218
John McCall54e14c42009-10-22 22:37:11 +00001219 // C++ [dcl.ref]p4: There shall be no references to references.
1220 //
1221 // According to C++ DR 106, references to references are only
1222 // diagnosed when they are written directly (e.g., "int & &"),
1223 // but not when they happen via a typedef:
1224 //
1225 // typedef int& intref;
1226 // typedef intref& intref2;
1227 //
1228 // Parser::ParseDeclaratorInternal diagnoses the case where
1229 // references are written directly; here, we handle the
Douglas Gregor69d83162011-01-20 16:08:06 +00001230 // collapsing of references-to-references as described in C++0x.
1231 // DR 106 and 540 introduce reference-collapsing into C++98/03.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001232
1233 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +00001234 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +00001235 // is ill-formed.
1236 if (T->isVoidType()) {
1237 Diag(Loc, diag::err_reference_to_void);
1238 return QualType();
1239 }
1240
John McCallf85e1932011-06-15 23:02:42 +00001241 // In ARC, it is forbidden to build references to unqualified pointers.
David Blaikie4e4d0842012-03-11 07:00:24 +00001242 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001243 T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true);
1244
Douglas Gregorcd281c32009-02-28 00:25:32 +00001245 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +00001247 return Context.getLValueReferenceType(T, SpelledAsLValue);
1248 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001249}
1250
Chris Lattnere1eed382011-06-14 06:38:10 +00001251/// Check whether the specified array size makes the array type a VLA. If so,
1252/// return true, if not, return the size of the array in SizeVal.
Richard Smith282e7e62012-02-04 09:53:13 +00001253static bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) {
1254 // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode
1255 // (like gnu99, but not c99) accept any evaluatable value as an extension.
Douglas Gregorab41fe92012-05-04 22:38:52 +00001256 class VLADiagnoser : public Sema::VerifyICEDiagnoser {
1257 public:
1258 VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {}
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001259
Douglas Gregorab41fe92012-05-04 22:38:52 +00001260 virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) {
1261 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001262
Douglas Gregorab41fe92012-05-04 22:38:52 +00001263 virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) {
1264 S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR;
1265 }
1266 } Diagnoser;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001267
Douglas Gregorab41fe92012-05-04 22:38:52 +00001268 return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser,
1269 S.LangOpts.GNUMode).isInvalid();
Chris Lattnere1eed382011-06-14 06:38:10 +00001270}
1271
1272
Douglas Gregorcd281c32009-02-28 00:25:32 +00001273/// \brief Build an array type.
1274///
1275/// \param T The type of each element in the array.
1276///
1277/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +00001278///
1279/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001280///
James Dennettefce31f2012-06-22 08:10:18 +00001281/// \param Brackets The range from the opening '[' to the closing ']'.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001282///
1283/// \param Entity The name of the entity that involves the array
1284/// type, if known.
1285///
1286/// \returns A suitable array type, if there are no errors. Otherwise,
1287/// returns a NULL type.
1288QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1289 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001290 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001291
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001292 SourceLocation Loc = Brackets.getBegin();
David Blaikie4e4d0842012-03-11 07:00:24 +00001293 if (getLangOpts().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +00001294 // C++ [dcl.array]p1:
1295 // T is called the array element type; this type shall not be a reference
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001296 // type, the (possibly cv-qualified) type void, a function type or an
Douglas Gregor138bb232010-04-27 19:38:14 +00001297 // abstract class type.
1298 //
Richard Smithbb351512012-07-07 23:00:31 +00001299 // C++ [dcl.array]p3:
1300 // When several "array of" specifications are adjacent, [...] only the
1301 // first of the constant expressions that specify the bounds of the arrays
1302 // may be omitted.
1303 //
Douglas Gregor138bb232010-04-27 19:38:14 +00001304 // Note: function types are handled in the common path with C.
1305 if (T->isReferenceType()) {
1306 Diag(Loc, diag::err_illegal_decl_array_of_references)
1307 << getPrintableNameForEntity(Entity) << T;
1308 return QualType();
1309 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001310
Richard Smithbb351512012-07-07 23:00:31 +00001311 if (T->isVoidType() || T->isIncompleteArrayType()) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001312 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1313 return QualType();
1314 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001315
1316 if (RequireNonAbstractType(Brackets.getBegin(), T,
Douglas Gregor138bb232010-04-27 19:38:14 +00001317 diag::err_array_of_abstract_type))
1318 return QualType();
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001319
Sebastian Redl923d56d2009-11-05 15:52:31 +00001320 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +00001321 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1322 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +00001323 if (RequireCompleteType(Loc, T,
1324 diag::err_illegal_decl_array_incomplete_type))
1325 return QualType();
1326 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001327
1328 if (T->isFunctionType()) {
1329 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +00001330 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001331 return QualType();
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Richard Smith34b41d92011-02-20 03:19:35 +00001334 if (T->getContainedAutoType()) {
1335 Diag(Loc, diag::err_illegal_decl_array_of_auto)
1336 << getPrintableNameForEntity(Entity) << T;
Anders Carlssone7cf07d2009-06-26 19:33:28 +00001337 return QualType();
1338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek6217b802009-07-29 21:53:49 +00001340 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001341 // If the element type is a struct or union that contains a variadic
1342 // array, accept it as a GNU extension: C99 6.7.2.1p2.
1343 if (EltTy->getDecl()->hasFlexibleArrayMember())
1344 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +00001345 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +00001346 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1347 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001348 }
Mike Stump1eb44332009-09-09 15:08:12 +00001349
John McCall806054d2012-01-11 00:14:46 +00001350 // Do placeholder conversions on the array size expression.
1351 if (ArraySize && ArraySize->hasPlaceholderType()) {
1352 ExprResult Result = CheckPlaceholderExpr(ArraySize);
1353 if (Result.isInvalid()) return QualType();
1354 ArraySize = Result.take();
1355 }
1356
John McCall5e3c67b2010-12-15 04:42:30 +00001357 // Do lvalue-to-rvalue conversions on the array size expression.
John Wiegley429bb272011-04-08 18:41:53 +00001358 if (ArraySize && !ArraySize->isRValue()) {
1359 ExprResult Result = DefaultLvalueConversion(ArraySize);
1360 if (Result.isInvalid())
1361 return QualType();
1362
1363 ArraySize = Result.take();
1364 }
John McCall5e3c67b2010-12-15 04:42:30 +00001365
Douglas Gregorcd281c32009-02-28 00:25:32 +00001366 // C99 6.7.5.2p1: The size expression shall have integer type.
Richard Smith282e7e62012-02-04 09:53:13 +00001367 // C++11 allows contextual conversions to such types.
David Blaikie4e4d0842012-03-11 07:00:24 +00001368 if (!getLangOpts().CPlusPlus0x &&
Richard Smith282e7e62012-02-04 09:53:13 +00001369 ArraySize && !ArraySize->isTypeDependent() &&
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001370 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001371 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1372 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001373 return QualType();
1374 }
Richard Smith282e7e62012-02-04 09:53:13 +00001375
Douglas Gregor2767ce22010-08-18 00:39:00 +00001376 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +00001377 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001378 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001379 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001380 else
1381 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001382 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001383 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Richard Smith282e7e62012-02-04 09:53:13 +00001384 } else if ((!T->isDependentType() && !T->isIncompleteType() &&
1385 !T->isConstantSizeType()) ||
1386 isArraySizeVLA(*this, ArraySize, ConstVal)) {
1387 // Even in C++11, don't allow contextual conversions in the array bound
1388 // of a VLA.
David Blaikie4e4d0842012-03-11 07:00:24 +00001389 if (getLangOpts().CPlusPlus0x &&
Richard Smith282e7e62012-02-04 09:53:13 +00001390 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
1391 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1392 << ArraySize->getType() << ArraySize->getSourceRange();
1393 return QualType();
1394 }
1395
Chris Lattnere1eed382011-06-14 06:38:10 +00001396 // C99: an array with an element type that has a non-constant-size is a VLA.
Chris Lattnere1eed382011-06-14 06:38:10 +00001397 // C99: an array with a non-ICE size is a VLA. We accept any expression
1398 // that we can fold to a non-zero positive value as an extension.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001399 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001400 } else {
1401 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1402 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +00001403 if (ConstVal.isSigned() && ConstVal.isNegative()) {
Chandler Carruthb2b5cc02011-01-04 04:44:35 +00001404 if (Entity)
1405 Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1406 << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1407 else
1408 Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1409 << ArraySize->getSourceRange();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001410 return QualType();
1411 }
1412 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001413 // GCC accepts zero sized static arrays. We allow them when
1414 // we're not in a SFINAE context.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001415 Diag(ArraySize->getLocStart(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001416 isSFINAEContext()? diag::err_typecheck_zero_array_size
1417 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +00001418 << ArraySize->getSourceRange();
Peter Collingbourne20cdbeb2011-10-16 21:17:32 +00001419
1420 if (ASM == ArrayType::Static) {
1421 Diag(ArraySize->getLocStart(),
1422 diag::warn_typecheck_zero_static_array_size)
1423 << ArraySize->getSourceRange();
1424 ASM = ArrayType::Normal;
1425 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001426 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
Douglas Gregor2767ce22010-08-18 00:39:00 +00001427 !T->isIncompleteType()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001428 // Is the array too large?
Douglas Gregor2767ce22010-08-18 00:39:00 +00001429 unsigned ActiveSizeBits
1430 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1431 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
1432 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
1433 << ConstVal.toString(10)
1434 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001435 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001436
John McCall46a617a2009-10-16 00:14:28 +00001437 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001438 }
David Chisnallaf407762010-01-11 23:08:08 +00001439 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
David Blaikie4e4d0842012-03-11 07:00:24 +00001440 if (!getLangOpts().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001441 if (T->isVariableArrayType()) {
1442 // Prohibit the use of non-POD types in VLAs.
John McCallf85e1932011-06-15 23:02:42 +00001443 QualType BaseT = Context.getBaseElementType(T);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001444 if (!T->isDependentType() &&
John McCallf85e1932011-06-15 23:02:42 +00001445 !BaseT.isPODType(Context) &&
1446 !BaseT->isObjCLifetimeType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001447 Diag(Loc, diag::err_vla_non_pod)
John McCallf85e1932011-06-15 23:02:42 +00001448 << BaseT;
Douglas Gregor0fddb972010-05-22 16:17:30 +00001449 return QualType();
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001450 }
Douglas Gregora481ec42010-05-23 19:57:01 +00001451 // Prohibit the use of VLAs during template argument deduction.
1452 else if (isSFINAEContext()) {
1453 Diag(Loc, diag::err_vla_in_sfinae);
1454 return QualType();
1455 }
Douglas Gregor0fddb972010-05-22 16:17:30 +00001456 // Just extwarn about VLAs.
1457 else
1458 Diag(Loc, diag::ext_vla);
1459 } else if (ASM != ArrayType::Normal || Quals != 0)
Richard Smithd7c56e12011-12-29 21:57:33 +00001460 Diag(Loc,
David Blaikie4e4d0842012-03-11 07:00:24 +00001461 getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx
Richard Smithd7c56e12011-12-29 21:57:33 +00001462 : diag::ext_c99_array_usage) << ASM;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001463 }
1464
1465 return T;
1466}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001467
1468/// \brief Build an ext-vector type.
1469///
1470/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +00001471QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001472 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001473 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1474 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +00001475 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001476 !T->isIntegerType() && !T->isRealFloatingType()) {
1477 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
1478 return QualType();
1479 }
1480
John McCall9ae2f072010-08-23 23:25:46 +00001481 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001482 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +00001483 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001484 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +00001485 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001486 return QualType();
1487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
1489 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001490 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +00001491 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
1492
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001493 if (vectorSize == 0) {
1494 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +00001495 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001496 return QualType();
1497 }
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Douglas Gregor4ac01402011-06-15 16:02:29 +00001499 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +00001500 }
1501
John McCall9ae2f072010-08-23 23:25:46 +00001502 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001503}
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregor724651c2009-02-28 01:04:19 +00001505/// \brief Build a function type.
1506///
1507/// This routine checks the function type according to C++ rules and
1508/// under the assumption that the result type and parameter types have
1509/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +00001510/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +00001511/// simpler form that is only suitable for this narrow use case.
1512///
1513/// \param T The return type of the function.
1514///
1515/// \param ParamTypes The parameter types of the function. This array
1516/// will be modified to account for adjustments to the types of the
1517/// function parameters.
1518///
1519/// \param NumParamTypes The number of parameter types in ParamTypes.
1520///
1521/// \param Variadic Whether this is a variadic function type.
1522///
Richard Smitheefb3d52012-02-10 09:58:53 +00001523/// \param HasTrailingReturn Whether this function has a trailing return type.
1524///
Douglas Gregor724651c2009-02-28 01:04:19 +00001525/// \param Quals The cvr-qualifiers to be applied to the function type.
1526///
1527/// \param Loc The location of the entity whose type involves this
1528/// function type or, if there is no such entity, the location of the
1529/// type that will have function type.
1530///
1531/// \param Entity The name of the entity that involves the function
1532/// type, if known.
1533///
1534/// \returns A suitable function type, if there are no
1535/// errors. Otherwise, returns a NULL type.
1536QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001537 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +00001538 unsigned NumParamTypes,
Richard Smitheefb3d52012-02-10 09:58:53 +00001539 bool Variadic, bool HasTrailingReturn,
1540 unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00001541 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00001542 SourceLocation Loc, DeclarationName Entity,
John McCalle23cf432010-12-14 08:05:40 +00001543 FunctionType::ExtInfo Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001544 if (T->isArrayType() || T->isFunctionType()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001545 Diag(Loc, diag::err_func_returning_array_function)
Douglas Gregor58408bc2010-01-11 18:46:21 +00001546 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +00001547 return QualType();
1548 }
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001549
1550 // Functions cannot return half FP.
1551 if (T->isHalfType()) {
1552 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
1553 FixItHint::CreateInsertion(Loc, "*");
1554 return QualType();
1555 }
1556
Douglas Gregor724651c2009-02-28 01:04:19 +00001557 bool Invalid = false;
1558 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001559 // FIXME: Loc is too inprecise here, should use proper locations for args.
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001560 QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001561 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001562 Diag(Loc, diag::err_param_with_void_type);
1563 Invalid = true;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001564 } else if (ParamType->isHalfType()) {
1565 // Disallow half FP arguments.
1566 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
1567 FixItHint::CreateInsertion(Loc, "*");
1568 Invalid = true;
Douglas Gregor724651c2009-02-28 01:04:19 +00001569 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001570
John McCall54e14c42009-10-22 22:37:11 +00001571 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +00001572 }
1573
1574 if (Invalid)
1575 return QualType();
1576
John McCalle23cf432010-12-14 08:05:40 +00001577 FunctionProtoType::ExtProtoInfo EPI;
1578 EPI.Variadic = Variadic;
Richard Smitheefb3d52012-02-10 09:58:53 +00001579 EPI.HasTrailingReturn = HasTrailingReturn;
John McCalle23cf432010-12-14 08:05:40 +00001580 EPI.TypeQuals = Quals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001581 EPI.RefQualifier = RefQualifier;
John McCalle23cf432010-12-14 08:05:40 +00001582 EPI.ExtInfo = Info;
1583
1584 return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
Douglas Gregor724651c2009-02-28 01:04:19 +00001585}
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Douglas Gregor949bf692009-06-09 22:17:39 +00001587/// \brief Build a member pointer type \c T Class::*.
1588///
1589/// \param T the type to which the member pointer refers.
1590/// \param Class the class type into which the member pointer points.
Douglas Gregor949bf692009-06-09 22:17:39 +00001591/// \param Loc the location where this type begins
1592/// \param Entity the name of the entity that will have this member pointer type
1593///
1594/// \returns a member pointer type, if successful, or a NULL type if there was
1595/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001596QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +00001597 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +00001598 DeclarationName Entity) {
1599 // Verify that we're not building a pointer to pointer to function with
1600 // exception specification.
1601 if (CheckDistantExceptionSpec(T)) {
1602 Diag(Loc, diag::err_distant_exception_spec);
1603
1604 // FIXME: If we're doing this as part of template instantiation,
1605 // we should return immediately.
1606
1607 // Build the type anyway, but use the canonical type so that the
1608 // exception specifiers are stripped off.
1609 T = Context.getCanonicalType(T);
1610 }
1611
Sebastian Redl73780122010-06-09 21:19:43 +00001612 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +00001613 // with reference type, or "cv void."
1614 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +00001615 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001616 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +00001617 return QualType();
1618 }
1619
1620 if (T->isVoidType()) {
1621 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1622 << (Entity? Entity.getAsString() : "type name");
1623 return QualType();
1624 }
1625
Douglas Gregor949bf692009-06-09 22:17:39 +00001626 if (!Class->isDependentType() && !Class->isRecordType()) {
1627 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1628 return QualType();
1629 }
1630
Joao Matos679fc932012-09-04 17:18:12 +00001631 // In the Microsoft ABI, the class is allowed to be an incomplete
1632 // type. In such cases, the compiler makes a worst-case assumption.
1633 // We make no such assumption right now, so emit an error if the
1634 // class isn't a complete type.
1635 if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
1636 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1637 return QualType();
1638
John McCall28654742010-06-05 06:41:15 +00001639 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +00001640}
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Anders Carlsson9a917e42009-06-12 22:56:54 +00001642/// \brief Build a block pointer type.
1643///
1644/// \param T The type to which we'll be building a block pointer.
1645///
James Dennettefce31f2012-06-22 08:10:18 +00001646/// \param Loc The source location, used for diagnostics.
Anders Carlsson9a917e42009-06-12 22:56:54 +00001647///
Anders Carlsson9a917e42009-06-12 22:56:54 +00001648/// \param Entity The name of the entity that involves the block pointer
1649/// type, if known.
1650///
1651/// \returns A suitable block pointer type, if there are no
1652/// errors. Otherwise, returns a NULL type.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001653QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001654 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +00001655 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001656 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +00001657 Diag(Loc, diag::err_nonfunction_block_type);
1658 return QualType();
1659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
John McCall28654742010-06-05 06:41:15 +00001661 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +00001662}
1663
John McCallb3d87482010-08-24 05:47:05 +00001664QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1665 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001666 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +00001667 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001668 return QualType();
1669 }
1670
John McCalla93c9342009-12-07 02:54:59 +00001671 TypeSourceInfo *DI = 0;
John McCallf4c73712011-01-19 06:33:43 +00001672 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001673 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +00001674 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001675 }
Mike Stump1eb44332009-09-09 15:08:12 +00001676
John McCalla93c9342009-12-07 02:54:59 +00001677 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001678 return QT;
1679}
1680
Argyrios Kyrtzidisa8349f52011-07-01 22:23:05 +00001681static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
1682 Qualifiers::ObjCLifetime ownership,
1683 unsigned chunkIndex);
1684
John McCallf85e1932011-06-15 23:02:42 +00001685/// Given that this is the declaration of a parameter under ARC,
1686/// attempt to infer attributes and such for pointer-to-whatever
1687/// types.
1688static void inferARCWriteback(TypeProcessingState &state,
1689 QualType &declSpecType) {
1690 Sema &S = state.getSema();
1691 Declarator &declarator = state.getDeclarator();
1692
1693 // TODO: should we care about decl qualifiers?
1694
1695 // Check whether the declarator has the expected form. We walk
1696 // from the inside out in order to make the block logic work.
1697 unsigned outermostPointerIndex = 0;
1698 bool isBlockPointer = false;
1699 unsigned numPointers = 0;
1700 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
1701 unsigned chunkIndex = i;
1702 DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex);
1703 switch (chunk.Kind) {
1704 case DeclaratorChunk::Paren:
1705 // Ignore parens.
1706 break;
1707
1708 case DeclaratorChunk::Reference:
1709 case DeclaratorChunk::Pointer:
1710 // Count the number of pointers. Treat references
1711 // interchangeably as pointers; if they're mis-ordered, normal
1712 // type building will discover that.
1713 outermostPointerIndex = chunkIndex;
1714 numPointers++;
1715 break;
1716
1717 case DeclaratorChunk::BlockPointer:
1718 // If we have a pointer to block pointer, that's an acceptable
1719 // indirect reference; anything else is not an application of
1720 // the rules.
1721 if (numPointers != 1) return;
1722 numPointers++;
1723 outermostPointerIndex = chunkIndex;
1724 isBlockPointer = true;
1725
1726 // We don't care about pointer structure in return values here.
1727 goto done;
1728
1729 case DeclaratorChunk::Array: // suppress if written (id[])?
1730 case DeclaratorChunk::Function:
1731 case DeclaratorChunk::MemberPointer:
1732 return;
1733 }
1734 }
1735 done:
1736
1737 // If we have *one* pointer, then we want to throw the qualifier on
1738 // the declaration-specifiers, which means that it needs to be a
1739 // retainable object type.
1740 if (numPointers == 1) {
1741 // If it's not a retainable object type, the rule doesn't apply.
1742 if (!declSpecType->isObjCRetainableType()) return;
1743
1744 // If it already has lifetime, don't do anything.
1745 if (declSpecType.getObjCLifetime()) return;
1746
1747 // Otherwise, modify the type in-place.
1748 Qualifiers qs;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001749
John McCallf85e1932011-06-15 23:02:42 +00001750 if (declSpecType->isObjCARCImplicitlyUnretainedType())
1751 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
1752 else
1753 qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing);
1754 declSpecType = S.Context.getQualifiedType(declSpecType, qs);
1755
1756 // If we have *two* pointers, then we want to throw the qualifier on
1757 // the outermost pointer.
1758 } else if (numPointers == 2) {
1759 // If we don't have a block pointer, we need to check whether the
1760 // declaration-specifiers gave us something that will turn into a
1761 // retainable object pointer after we slap the first pointer on it.
1762 if (!isBlockPointer && !declSpecType->isObjCObjectType())
1763 return;
1764
1765 // Look for an explicit lifetime attribute there.
1766 DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex);
Argyrios Kyrtzidis1c73dcb2011-07-01 22:23:03 +00001767 if (chunk.Kind != DeclaratorChunk::Pointer &&
1768 chunk.Kind != DeclaratorChunk::BlockPointer)
1769 return;
John McCallf85e1932011-06-15 23:02:42 +00001770 for (const AttributeList *attr = chunk.getAttrs(); attr;
1771 attr = attr->getNext())
Sean Hunt8e083e72012-06-19 23:57:03 +00001772 if (attr->getKind() == AttributeList::AT_ObjCOwnership)
John McCallf85e1932011-06-15 23:02:42 +00001773 return;
1774
Argyrios Kyrtzidisa8349f52011-07-01 22:23:05 +00001775 transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing,
1776 outermostPointerIndex);
John McCallf85e1932011-06-15 23:02:42 +00001777
1778 // Any other number of pointers/references does not trigger the rule.
1779 } else return;
1780
1781 // TODO: mark whether we did this inference?
1782}
1783
Chandler Carruthd067c072011-02-23 18:51:59 +00001784static void DiagnoseIgnoredQualifiers(unsigned Quals,
1785 SourceLocation ConstQualLoc,
1786 SourceLocation VolatileQualLoc,
1787 SourceLocation RestrictQualLoc,
1788 Sema& S) {
1789 std::string QualStr;
1790 unsigned NumQuals = 0;
1791 SourceLocation Loc;
1792
1793 FixItHint ConstFixIt;
1794 FixItHint VolatileFixIt;
1795 FixItHint RestrictFixIt;
1796
Hans Wennborga08fcb82011-06-03 17:37:26 +00001797 const SourceManager &SM = S.getSourceManager();
1798
Chandler Carruthd067c072011-02-23 18:51:59 +00001799 // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1800 // find a range and grow it to encompass all the qualifiers, regardless of
1801 // the order in which they textually appear.
1802 if (Quals & Qualifiers::Const) {
1803 ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
Chandler Carruthd067c072011-02-23 18:51:59 +00001804 QualStr = "const";
Hans Wennborga08fcb82011-06-03 17:37:26 +00001805 ++NumQuals;
1806 if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc))
1807 Loc = ConstQualLoc;
Chandler Carruthd067c072011-02-23 18:51:59 +00001808 }
1809 if (Quals & Qualifiers::Volatile) {
1810 VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
Hans Wennborga08fcb82011-06-03 17:37:26 +00001811 QualStr += (NumQuals == 0 ? "volatile" : " volatile");
Chandler Carruthd067c072011-02-23 18:51:59 +00001812 ++NumQuals;
Hans Wennborga08fcb82011-06-03 17:37:26 +00001813 if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc))
1814 Loc = VolatileQualLoc;
Chandler Carruthd067c072011-02-23 18:51:59 +00001815 }
1816 if (Quals & Qualifiers::Restrict) {
1817 RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
Hans Wennborga08fcb82011-06-03 17:37:26 +00001818 QualStr += (NumQuals == 0 ? "restrict" : " restrict");
Chandler Carruthd067c072011-02-23 18:51:59 +00001819 ++NumQuals;
Hans Wennborga08fcb82011-06-03 17:37:26 +00001820 if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc))
1821 Loc = RestrictQualLoc;
Chandler Carruthd067c072011-02-23 18:51:59 +00001822 }
1823
1824 assert(NumQuals > 0 && "No known qualifiers?");
1825
1826 S.Diag(Loc, diag::warn_qual_return_type)
Hans Wennborga08fcb82011-06-03 17:37:26 +00001827 << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt;
Chandler Carruthd067c072011-02-23 18:51:59 +00001828}
1829
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001830static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
1831 TypeSourceInfo *&ReturnTypeInfo) {
1832 Sema &SemaRef = state.getSema();
1833 Declarator &D = state.getDeclarator();
Douglas Gregor930d8b52009-01-30 22:09:00 +00001834 QualType T;
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001835 ReturnTypeInfo = 0;
1836
1837 // The TagDecl owned by the DeclSpec.
Argyrios Kyrtzidisd3880f82011-06-28 03:01:18 +00001838 TagDecl *OwnedTagDecl = 0;
John McCall711c52b2011-01-05 12:14:39 +00001839
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001840 switch (D.getName().getKind()) {
Fariborz Jahanian98a54032011-07-12 17:16:56 +00001841 case UnqualifiedId::IK_ImplicitSelfParam:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001842 case UnqualifiedId::IK_OperatorFunctionId:
Sebastian Redl8999fe12011-03-14 18:08:30 +00001843 case UnqualifiedId::IK_Identifier:
Sean Hunt0486d742009-11-28 04:44:28 +00001844 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001845 case UnqualifiedId::IK_TemplateId:
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001846 T = ConvertDeclSpecToType(state);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001847
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001848 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
Argyrios Kyrtzidisd3880f82011-06-28 03:01:18 +00001849 OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Abramo Bagnara15987972011-03-08 22:33:38 +00001850 // Owned declaration is embedded in declarator.
Argyrios Kyrtzidisd3880f82011-06-28 03:01:18 +00001851 OwnedTagDecl->setEmbeddedInDeclarator(true);
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001852 }
Douglas Gregor930d8b52009-01-30 22:09:00 +00001853 break;
1854
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001855 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001856 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001857 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +00001858 // Constructors and destructors don't have return types. Use
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001859 // "void" instead.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001860 T = SemaRef.Context.VoidTy;
Rafael Espindolaa78a6402012-07-31 01:54:04 +00001861 if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList())
1862 processTypeAttrs(state, T, true, attrs);
Douglas Gregor930d8b52009-01-30 22:09:00 +00001863 break;
Douglas Gregor48026d22010-01-11 18:40:55 +00001864
1865 case UnqualifiedId::IK_ConversionFunctionId:
1866 // The result type of a conversion function is the type that it
1867 // converts to.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00001868 T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId,
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001869 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +00001870 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +00001871 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00001872
John McCall711c52b2011-01-05 12:14:39 +00001873 if (D.getAttributes())
1874 distributeTypeAttrsFromDeclarator(state, T);
1875
Richard Smithd37b3602012-02-10 11:05:11 +00001876 // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
1877 // In C++11, a function declarator using 'auto' must have a trailing return
Richard Smith8110f042011-02-22 01:22:29 +00001878 // type (this is checked later) and we can skip this. In other languages
1879 // using auto, we need to check regardless.
Richard Smith34b41d92011-02-20 03:19:35 +00001880 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001881 (!SemaRef.getLangOpts().CPlusPlus0x || !D.isFunctionDeclarator())) {
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001882 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001884 switch (D.getContext()) {
1885 case Declarator::KNRTypeListContext:
David Blaikieb219cfc2011-09-23 05:06:16 +00001886 llvm_unreachable("K&R type lists aren't allowed in C++");
Eli Friedmanf88c4002012-01-04 04:41:38 +00001887 case Declarator::LambdaExprContext:
1888 llvm_unreachable("Can't specify a type specifier in lambda grammar");
John McCallcdda47f2011-10-01 09:56:14 +00001889 case Declarator::ObjCParameterContext:
1890 case Declarator::ObjCResultContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001891 case Declarator::PrototypeContext:
1892 Error = 0; // Function prototype
1893 break;
1894 case Declarator::MemberContext:
Richard Smith7a614d82011-06-11 17:19:42 +00001895 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)
1896 break;
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001897 switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) {
David Blaikieeb2d1f12011-09-23 20:26:49 +00001898 case TTK_Enum: llvm_unreachable("unhandled tag kind");
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001899 case TTK_Struct: Error = 1; /* Struct member */ break;
1900 case TTK_Union: Error = 2; /* Union member */ break;
1901 case TTK_Class: Error = 3; /* Class member */ break;
Joao Matos6666ed42012-08-31 18:45:21 +00001902 case TTK_Interface: Error = 4; /* Interface member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001903 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001904 break;
1905 case Declarator::CXXCatchContext:
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001906 case Declarator::ObjCCatchContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001907 Error = 5; // Exception declaration
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001908 break;
1909 case Declarator::TemplateParamContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001910 Error = 6; // Template parameter
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001911 break;
1912 case Declarator::BlockLiteralContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001913 Error = 7; // Block literal
Richard Smith34b41d92011-02-20 03:19:35 +00001914 break;
1915 case Declarator::TemplateTypeArgContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001916 Error = 8; // Template type argument
Richard Smith34b41d92011-02-20 03:19:35 +00001917 break;
Richard Smith162e1c12011-04-15 14:24:37 +00001918 case Declarator::AliasDeclContext:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001919 case Declarator::AliasTemplateContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001920 Error = 10; // Type alias
Richard Smith162e1c12011-04-15 14:24:37 +00001921 break;
Richard Smith7796eb52012-03-12 08:56:40 +00001922 case Declarator::TrailingReturnContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001923 Error = 11; // Function return type
Richard Smith7796eb52012-03-12 08:56:40 +00001924 break;
Richard Smith34b41d92011-02-20 03:19:35 +00001925 case Declarator::TypeNameContext:
Joao Matos6666ed42012-08-31 18:45:21 +00001926 Error = 12; // Generic
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001927 break;
1928 case Declarator::FileContext:
1929 case Declarator::BlockContext:
1930 case Declarator::ForContext:
1931 case Declarator::ConditionContext:
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00001932 case Declarator::CXXNewContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001933 break;
1934 }
1935
Richard Smithddc83f92011-02-21 23:18:00 +00001936 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
Joao Matos6666ed42012-08-31 18:45:21 +00001937 Error = 9;
Richard Smithddc83f92011-02-21 23:18:00 +00001938
Richard Smith8110f042011-02-22 01:22:29 +00001939 // In Objective-C it is an error to use 'auto' on a function declarator.
1940 if (D.isFunctionDeclarator())
Joao Matos6666ed42012-08-31 18:45:21 +00001941 Error = 11;
Richard Smith8110f042011-02-22 01:22:29 +00001942
Richard Smithd37b3602012-02-10 11:05:11 +00001943 // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
Richard Smithe7397c62011-02-22 00:36:53 +00001944 // contains a trailing return type. That is only legal at the outermost
1945 // level. Check all declarator chunks (outermost first) anyway, to give
1946 // better diagnostics.
David Blaikie4e4d0842012-03-11 07:00:24 +00001947 if (SemaRef.getLangOpts().CPlusPlus0x && Error != -1) {
Richard Smithe7397c62011-02-22 00:36:53 +00001948 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1949 unsigned chunkIndex = e - i - 1;
1950 state.setCurrentChunkIndex(chunkIndex);
1951 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1952 if (DeclType.Kind == DeclaratorChunk::Function) {
1953 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Richard Smith54655be2012-06-12 01:51:59 +00001954 if (FTI.hasTrailingReturnType()) {
Richard Smithe7397c62011-02-22 00:36:53 +00001955 Error = -1;
1956 break;
1957 }
1958 }
1959 }
1960 }
1961
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001962 if (Error != -1) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001963 SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1964 diag::err_auto_not_allowed)
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001965 << Error;
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001966 T = SemaRef.Context.IntTy;
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001967 D.setInvalidType(true);
Richard Smith0aa86c02011-10-15 05:42:01 +00001968 } else
1969 SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1970 diag::warn_cxx98_compat_auto_type_specifier);
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001971 }
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001972
David Blaikie4e4d0842012-03-11 07:00:24 +00001973 if (SemaRef.getLangOpts().CPlusPlus &&
John McCall5e1cdac2011-10-07 06:10:15 +00001974 OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001975 // Check the contexts where C++ forbids the declaration of a new class
1976 // or enumeration in a type-specifier-seq.
1977 switch (D.getContext()) {
Richard Smith7796eb52012-03-12 08:56:40 +00001978 case Declarator::TrailingReturnContext:
1979 // Class and enumeration definitions are syntactically not allowed in
1980 // trailing return types.
1981 llvm_unreachable("parser should not have allowed this");
1982 break;
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001983 case Declarator::FileContext:
1984 case Declarator::MemberContext:
1985 case Declarator::BlockContext:
1986 case Declarator::ForContext:
1987 case Declarator::BlockLiteralContext:
Eli Friedmanf88c4002012-01-04 04:41:38 +00001988 case Declarator::LambdaExprContext:
Richard Smithd37b3602012-02-10 11:05:11 +00001989 // C++11 [dcl.type]p3:
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00001990 // A type-specifier-seq shall not define a class or enumeration unless
1991 // it appears in the type-id of an alias-declaration (7.1.3) that is not
1992 // the declaration of a template-declaration.
1993 case Declarator::AliasDeclContext:
1994 break;
1995 case Declarator::AliasTemplateContext:
1996 SemaRef.Diag(OwnedTagDecl->getLocation(),
1997 diag::err_type_defined_in_alias_template)
1998 << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
1999 break;
2000 case Declarator::TypeNameContext:
2001 case Declarator::TemplateParamContext:
2002 case Declarator::CXXNewContext:
2003 case Declarator::CXXCatchContext:
2004 case Declarator::ObjCCatchContext:
2005 case Declarator::TemplateTypeArgContext:
2006 SemaRef.Diag(OwnedTagDecl->getLocation(),
2007 diag::err_type_defined_in_type_specifier)
2008 << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2009 break;
2010 case Declarator::PrototypeContext:
John McCallcdda47f2011-10-01 09:56:14 +00002011 case Declarator::ObjCParameterContext:
2012 case Declarator::ObjCResultContext:
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002013 case Declarator::KNRTypeListContext:
2014 // C++ [dcl.fct]p6:
2015 // Types shall not be defined in return or parameter types.
2016 SemaRef.Diag(OwnedTagDecl->getLocation(),
2017 diag::err_type_defined_in_param_type)
2018 << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
2019 break;
2020 case Declarator::ConditionContext:
2021 // C++ 6.4p2:
2022 // The type-specifier-seq shall not contain typedef and shall not declare
2023 // a new class or enumeration.
2024 SemaRef.Diag(OwnedTagDecl->getLocation(),
2025 diag::err_type_defined_in_condition);
2026 break;
2027 }
2028 }
2029
2030 return T;
2031}
2032
Benjamin Kramera08c2fb2012-02-24 22:19:42 +00002033static std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){
Richard Smithd37b3602012-02-10 11:05:11 +00002034 std::string Quals =
2035 Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2036
2037 switch (FnTy->getRefQualifier()) {
2038 case RQ_None:
2039 break;
2040
2041 case RQ_LValue:
2042 if (!Quals.empty())
2043 Quals += ' ';
2044 Quals += '&';
2045 break;
2046
2047 case RQ_RValue:
2048 if (!Quals.empty())
2049 Quals += ' ';
2050 Quals += "&&";
2051 break;
2052 }
2053
2054 return Quals;
2055}
2056
2057/// Check that the function type T, which has a cv-qualifier or a ref-qualifier,
2058/// can be contained within the declarator chunk DeclType, and produce an
2059/// appropriate diagnostic if not.
2060static void checkQualifiedFunction(Sema &S, QualType T,
2061 DeclaratorChunk &DeclType) {
2062 // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a
2063 // cv-qualifier or a ref-qualifier can only appear at the topmost level
2064 // of a type.
2065 int DiagKind = -1;
2066 switch (DeclType.Kind) {
2067 case DeclaratorChunk::Paren:
2068 case DeclaratorChunk::MemberPointer:
2069 // These cases are permitted.
2070 return;
2071 case DeclaratorChunk::Array:
2072 case DeclaratorChunk::Function:
2073 // These cases don't allow function types at all; no need to diagnose the
2074 // qualifiers separately.
2075 return;
2076 case DeclaratorChunk::BlockPointer:
2077 DiagKind = 0;
2078 break;
2079 case DeclaratorChunk::Pointer:
2080 DiagKind = 1;
2081 break;
2082 case DeclaratorChunk::Reference:
2083 DiagKind = 2;
2084 break;
2085 }
2086
2087 assert(DiagKind != -1);
2088 S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type)
2089 << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T
2090 << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>());
2091}
2092
Richard Smithb9c62612012-07-30 21:30:52 +00002093/// Produce an approprioate diagnostic for an ambiguity between a function
2094/// declarator and a C++ direct-initializer.
2095static void warnAboutAmbiguousFunction(Sema &S, Declarator &D,
2096 DeclaratorChunk &DeclType, QualType RT) {
2097 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
2098 assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity");
2099
2100 // If the return type is void there is no ambiguity.
2101 if (RT->isVoidType())
2102 return;
2103
2104 // An initializer for a non-class type can have at most one argument.
2105 if (!RT->isRecordType() && FTI.NumArgs > 1)
2106 return;
2107
2108 // An initializer for a reference must have exactly one argument.
2109 if (RT->isReferenceType() && FTI.NumArgs != 1)
2110 return;
2111
2112 // Only warn if this declarator is declaring a function at block scope, and
2113 // doesn't have a storage class (such as 'extern') specified.
2114 if (!D.isFunctionDeclarator() ||
2115 D.getFunctionDefinitionKind() != FDK_Declaration ||
2116 !S.CurContext->isFunctionOrMethod() ||
2117 D.getDeclSpec().getStorageClassSpecAsWritten()
2118 != DeclSpec::SCS_unspecified)
2119 return;
2120
2121 // Inside a condition, a direct initializer is not permitted. We allow one to
2122 // be parsed in order to give better diagnostics in condition parsing.
2123 if (D.getContext() == Declarator::ConditionContext)
2124 return;
2125
2126 SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc);
2127
Richard Smithd64effc2012-07-30 21:42:05 +00002128 S.Diag(DeclType.Loc,
2129 FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration
2130 : diag::warn_empty_parens_are_function_decl)
2131 << ParenRange;
Richard Smithb9c62612012-07-30 21:30:52 +00002132
Richard Smithd64effc2012-07-30 21:42:05 +00002133 // If the declaration looks like:
2134 // T var1,
2135 // f();
2136 // and name lookup finds a function named 'f', then the ',' was
2137 // probably intended to be a ';'.
2138 if (!D.isFirstDeclarator() && D.getIdentifier()) {
2139 FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr);
2140 FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr);
2141 if (Comma.getFileID() != Name.getFileID() ||
2142 Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
2143 LookupResult Result(S, D.getIdentifier(), SourceLocation(),
2144 Sema::LookupOrdinaryName);
2145 if (S.LookupName(Result, S.getCurScope()))
2146 S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
2147 << FixItHint::CreateReplacement(D.getCommaLoc(), ";")
2148 << D.getIdentifier();
2149 }
2150 }
2151
2152 if (FTI.NumArgs > 0) {
2153 // For a declaration with parameters, eg. "T var(T());", suggest adding parens
2154 // around the first parameter to turn the declaration into a variable
2155 // declaration.
Richard Smithb9c62612012-07-30 21:30:52 +00002156 SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange();
2157 SourceLocation B = Range.getBegin();
2158 SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd());
2159 // FIXME: Maybe we should suggest adding braces instead of parens
2160 // in C++11 for classes that don't have an initializer_list constructor.
2161 S.Diag(B, diag::note_additional_parens_for_variable_declaration)
2162 << FixItHint::CreateInsertion(B, "(")
2163 << FixItHint::CreateInsertion(E, ")");
Richard Smithd64effc2012-07-30 21:42:05 +00002164 } else {
2165 // For a declaration without parameters, eg. "T var();", suggest replacing the
2166 // parens with an initializer to turn the declaration into a variable
2167 // declaration.
Richard Smithb9c62612012-07-30 21:30:52 +00002168 const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
Richard Smithd64effc2012-07-30 21:42:05 +00002169
Richard Smithb9c62612012-07-30 21:30:52 +00002170 // Empty parens mean value-initialization, and no parens mean
2171 // default initialization. These are equivalent if the default
2172 // constructor is user-provided or if zero-initialization is a
2173 // no-op.
2174 if (RD && RD->hasDefinition() &&
2175 (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
2176 S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
2177 << FixItHint::CreateRemoval(ParenRange);
2178 else {
2179 std::string Init = S.getFixItZeroInitializerForType(RT);
2180 if (Init.empty() && S.LangOpts.CPlusPlus0x)
2181 Init = "{}";
2182 if (!Init.empty())
2183 S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize)
2184 << FixItHint::CreateReplacement(ParenRange, Init);
2185 }
2186 }
2187}
2188
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002189static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
2190 QualType declSpecType,
2191 TypeSourceInfo *TInfo) {
2192
2193 QualType T = declSpecType;
2194 Declarator &D = state.getDeclarator();
2195 Sema &S = state.getSema();
2196 ASTContext &Context = S.Context;
David Blaikie4e4d0842012-03-11 07:00:24 +00002197 const LangOptions &LangOpts = S.getLangOpts();
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002198
Douglas Gregorcd281c32009-02-28 00:25:32 +00002199 // The name we're declaring, if any.
2200 DeclarationName Name;
2201 if (D.getIdentifier())
2202 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002203
Richard Smith162e1c12011-04-15 14:24:37 +00002204 // Does this declaration declare a typedef-name?
2205 bool IsTypedefName =
2206 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef ||
Richard Smith3e4c6c42011-05-05 21:57:07 +00002207 D.getContext() == Declarator::AliasDeclContext ||
2208 D.getContext() == Declarator::AliasTemplateContext;
Richard Smith162e1c12011-04-15 14:24:37 +00002209
Richard Smithd37b3602012-02-10 11:05:11 +00002210 // Does T refer to a function type with a cv-qualifier or a ref-qualifier?
2211 bool IsQualifiedFunction = T->isFunctionProtoType() &&
2212 (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 ||
2213 T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None);
2214
Mike Stump98eb8a72009-02-04 22:31:32 +00002215 // Walk the DeclTypeInfo, building the recursive type as we go.
2216 // DeclTypeInfos are ordered from the identifier out, which is
2217 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00002218 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall711c52b2011-01-05 12:14:39 +00002219 unsigned chunkIndex = e - i - 1;
2220 state.setCurrentChunkIndex(chunkIndex);
2221 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
Richard Smithd37b3602012-02-10 11:05:11 +00002222 if (IsQualifiedFunction) {
2223 checkQualifiedFunction(S, T, DeclType);
2224 IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren;
2225 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002226 switch (DeclType.Kind) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002227 case DeclaratorChunk::Paren:
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002228 T = S.BuildParenType(T);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002229 break;
Steve Naroff5618bd42008-08-27 16:04:49 +00002230 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00002231 // If blocks are disabled, emit an error.
2232 if (!LangOpts.Blocks)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002233 S.Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002235 T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
John McCall28654742010-06-05 06:41:15 +00002236 if (DeclType.Cls.TypeQuals)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002237 T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00002238 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002240 // Verify that we're not building a pointer to pointer to function with
2241 // exception specification.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002242 if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
2243 S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002244 D.setInvalidType(true);
2245 // Build the type anyway.
2246 }
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002247 if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) {
John McCallc12c5bb2010-05-15 11:32:37 +00002248 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00002249 if (DeclType.Ptr.TypeQuals)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002250 T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00002251 break;
2252 }
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002253 T = S.BuildPointerType(T, DeclType.Loc, Name);
John McCall28654742010-06-05 06:41:15 +00002254 if (DeclType.Ptr.TypeQuals)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002255 T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
John McCall711c52b2011-01-05 12:14:39 +00002256
Reid Spencer5f016e22007-07-11 17:01:13 +00002257 break;
John McCall0953e762009-09-24 19:53:00 +00002258 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002259 // Verify that we're not building a reference to pointer to function with
2260 // exception specification.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002261 if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
2262 S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002263 D.setInvalidType(true);
2264 // Build the type anyway.
2265 }
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002266 T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
John McCall28654742010-06-05 06:41:15 +00002267
2268 Qualifiers Quals;
2269 if (DeclType.Ref.HasRestrict)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002270 T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00002271 break;
John McCall0953e762009-09-24 19:53:00 +00002272 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002274 // Verify that we're not building an array of pointers to function with
2275 // exception specification.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002276 if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
2277 S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
Sebastian Redl6a7330c2009-05-29 15:01:05 +00002278 D.setInvalidType(true);
2279 // Build the type anyway.
2280 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00002281 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00002282 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00002283 ArrayType::ArraySizeModifier ASM;
2284 if (ATI.isStar)
2285 ASM = ArrayType::Star;
2286 else if (ATI.hasStatic)
2287 ASM = ArrayType::Static;
2288 else
2289 ASM = ArrayType::Normal;
John McCallc05a94b2011-03-23 23:43:04 +00002290 if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00002291 // FIXME: This check isn't quite right: it allows star in prototypes
2292 // for function definitions, and disallows some edge cases detailed
2293 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002294 S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
Eli Friedmanf91f5c82009-04-26 21:57:51 +00002295 ASM = ArrayType::Normal;
2296 D.setInvalidType(true);
2297 }
Hans Wennborg7f397c52012-08-15 07:42:30 +00002298
2299 // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
2300 // shall appear only in a declaration of a function parameter with an
2301 // array type, ...
2302 if (ASM == ArrayType::Static || ATI.TypeQuals) {
Matt Beaumont-Gay99570a52012-08-15 19:53:19 +00002303 if (!(D.isPrototypeContext() ||
2304 D.getContext() == Declarator::KNRTypeListContext)) {
Hans Wennborg7f397c52012-08-15 07:42:30 +00002305 S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
2306 (ASM == ArrayType::Static ? "'static'" : "type qualifier");
2307 // Remove the 'static' and the type qualifiers.
2308 if (ASM == ArrayType::Static)
2309 ASM = ArrayType::Normal;
2310 ATI.TypeQuals = 0;
2311 D.setInvalidType(true);
2312 }
2313
2314 // C99 6.7.5.2p1: ... and then only in the outermost array type
2315 // derivation.
2316 unsigned x = chunkIndex;
2317 while (x != 0) {
2318 // Walk outwards along the declarator chunks.
2319 x--;
2320 const DeclaratorChunk &DC = D.getTypeObject(x);
2321 switch (DC.Kind) {
2322 case DeclaratorChunk::Paren:
2323 continue;
2324 case DeclaratorChunk::Array:
2325 case DeclaratorChunk::Pointer:
2326 case DeclaratorChunk::Reference:
2327 case DeclaratorChunk::MemberPointer:
2328 S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
2329 (ASM == ArrayType::Static ? "'static'" : "type qualifier");
2330 if (ASM == ArrayType::Static)
2331 ASM = ArrayType::Normal;
2332 ATI.TypeQuals = 0;
2333 D.setInvalidType(true);
2334 break;
2335 case DeclaratorChunk::Function:
2336 case DeclaratorChunk::BlockPointer:
2337 // These are invalid anyway, so just ignore.
2338 break;
2339 }
2340 }
2341 }
2342
Eli Friedman8ac2c662011-11-11 02:00:42 +00002343 T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002344 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 break;
2346 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002347 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00002348 // If the function declarator has a prototype (i.e. it is not () and
2349 // does not have a K&R-style identifier list), then the arguments are part
2350 // of the type, otherwise the argument list is ().
2351 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Richard Smithd37b3602012-02-10 11:05:11 +00002352 IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier();
Sebastian Redl3cc97262009-05-31 11:47:27 +00002353
Richard Smithe7397c62011-02-22 00:36:53 +00002354 // Check for auto functions and trailing return type and adjust the
2355 // return type accordingly.
2356 if (!D.isInvalidType()) {
2357 // trailing-return-type is only required if we're declaring a function,
2358 // and not, for instance, a pointer to a function.
2359 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
Richard Smith54655be2012-06-12 01:51:59 +00002360 !FTI.hasTrailingReturnType() && chunkIndex == 0) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002361 S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
Richard Smithe7397c62011-02-22 00:36:53 +00002362 diag::err_auto_missing_trailing_return);
2363 T = Context.IntTy;
2364 D.setInvalidType(true);
Richard Smith54655be2012-06-12 01:51:59 +00002365 } else if (FTI.hasTrailingReturnType()) {
Richard Smithe7397c62011-02-22 00:36:53 +00002366 // T must be exactly 'auto' at this point. See CWG issue 681.
2367 if (isa<ParenType>(T)) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002368 S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
Richard Smithe7397c62011-02-22 00:36:53 +00002369 diag::err_trailing_return_in_parens)
2370 << T << D.getDeclSpec().getSourceRange();
2371 D.setInvalidType(true);
Eli Friedmanf88c4002012-01-04 04:41:38 +00002372 } else if (D.getContext() != Declarator::LambdaExprContext &&
2373 (T.hasQualifiers() || !isa<AutoType>(T))) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002374 S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
Richard Smithe7397c62011-02-22 00:36:53 +00002375 diag::err_trailing_return_without_auto)
2376 << T << D.getDeclSpec().getSourceRange();
2377 D.setInvalidType(true);
2378 }
Richard Smith54655be2012-06-12 01:51:59 +00002379 T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo);
2380 if (T.isNull()) {
2381 // An error occurred parsing the trailing return type.
2382 T = Context.IntTy;
2383 D.setInvalidType(true);
2384 }
Richard Smithe7397c62011-02-22 00:36:53 +00002385 }
2386 }
2387
Chris Lattnercd881292007-12-19 05:31:29 +00002388 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00002389 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00002390 if ((T->isArrayType() || T->isFunctionType()) &&
2391 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00002392 unsigned diagID = diag::err_func_returning_array_function;
Argyrios Kyrtzidisa4356ad2011-01-26 01:26:44 +00002393 // Last processing chunk in block context means this function chunk
2394 // represents the block.
2395 if (chunkIndex == 0 &&
2396 D.getContext() == Declarator::BlockLiteralContext)
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00002397 diagID = diag::err_block_returning_array_function;
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002398 S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00002399 T = Context.IntTy;
2400 D.setInvalidType(true);
2401 }
Sebastian Redl465226e2009-05-27 22:11:52 +00002402
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002403 // Do not allow returning half FP value.
2404 // FIXME: This really should be in BuildFunctionType.
2405 if (T->isHalfType()) {
2406 S.Diag(D.getIdentifierLoc(),
2407 diag::err_parameters_retval_cannot_have_fp16_type) << 1
2408 << FixItHint::CreateInsertion(D.getIdentifierLoc(), "*");
2409 D.setInvalidType(true);
2410 }
2411
Douglas Gregor5291c3c2010-07-13 08:18:22 +00002412 // cv-qualifiers on return types are pointless except when the type is a
2413 // class type in C++.
Douglas Gregorfff95132011-03-01 17:04:42 +00002414 if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
Rafael Espindola1e153942011-03-11 04:56:58 +00002415 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002416 (!LangOpts.CPlusPlus || !T->isDependentType())) {
Chandler Carruthd067c072011-02-23 18:51:59 +00002417 assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
2418 DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
2419 assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
2420
2421 DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
2422
2423 DiagnoseIgnoredQualifiers(PTI.TypeQuals,
2424 SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
2425 SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
2426 SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002427 S);
Chandler Carruthd067c072011-02-23 18:51:59 +00002428
2429 } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002430 (!LangOpts.CPlusPlus ||
Douglas Gregor5291c3c2010-07-13 08:18:22 +00002431 (!T->isDependentType() && !T->isRecordType()))) {
Chandler Carruthd067c072011-02-23 18:51:59 +00002432
2433 DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
2434 D.getDeclSpec().getConstSpecLoc(),
2435 D.getDeclSpec().getVolatileSpecLoc(),
2436 D.getDeclSpec().getRestrictSpecLoc(),
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002437 S);
Douglas Gregor5291c3c2010-07-13 08:18:22 +00002438 }
Chandler Carruthd067c072011-02-23 18:51:59 +00002439
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002440 if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
Douglas Gregor402abb52009-05-28 23:31:59 +00002441 // C++ [dcl.fct]p6:
2442 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00002443 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
John McCall5e1cdac2011-10-07 06:10:15 +00002444 if (Tag->isCompleteDefinition())
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002445 S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
Douglas Gregor402abb52009-05-28 23:31:59 +00002446 << Context.getTypeDeclType(Tag);
2447 }
2448
Sebastian Redl3cc97262009-05-31 11:47:27 +00002449 // Exception specs are not allowed in typedefs. Complain, but add it
2450 // anyway.
Richard Smith162e1c12011-04-15 14:24:37 +00002451 if (IsTypedefName && FTI.getExceptionSpecType())
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002452 S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef)
Richard Smith3e4c6c42011-05-05 21:57:07 +00002453 << (D.getContext() == Declarator::AliasDeclContext ||
2454 D.getContext() == Declarator::AliasTemplateContext);
Sebastian Redl3cc97262009-05-31 11:47:27 +00002455
Richard Smithb9c62612012-07-30 21:30:52 +00002456 // If we see "T var();" or "T var(T());" at block scope, it is probably
2457 // an attempt to initialize a variable, not a function declaration.
2458 if (FTI.isAmbiguous)
2459 warnAboutAmbiguousFunction(S, D, DeclType, T);
2460
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002461 if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) {
John McCall28654742010-06-05 06:41:15 +00002462 // Simple void foo(), where the incoming T is the result type.
2463 T = Context.getFunctionNoProtoType(T);
2464 } else {
2465 // We allow a zero-parameter variadic function in C if the
2466 // function is marked with the "overloadable" attribute. Scan
2467 // for this attribute now.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002468 if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00002469 bool Overloadable = false;
2470 for (const AttributeList *Attrs = D.getAttributes();
2471 Attrs; Attrs = Attrs->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00002472 if (Attrs->getKind() == AttributeList::AT_Overloadable) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00002473 Overloadable = true;
2474 break;
2475 }
2476 }
2477
2478 if (!Overloadable)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002479 S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00002480 }
John McCall28654742010-06-05 06:41:15 +00002481
2482 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002483 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
2484 // definition.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002485 S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall28654742010-06-05 06:41:15 +00002486 D.setInvalidType(true);
2487 break;
2488 }
2489
John McCalle23cf432010-12-14 08:05:40 +00002490 FunctionProtoType::ExtProtoInfo EPI;
2491 EPI.Variadic = FTI.isVariadic;
Richard Smith54655be2012-06-12 01:51:59 +00002492 EPI.HasTrailingReturn = FTI.hasTrailingReturnType();
John McCalle23cf432010-12-14 08:05:40 +00002493 EPI.TypeQuals = FTI.TypeQuals;
Douglas Gregorc938c162011-01-26 05:01:58 +00002494 EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
2495 : FTI.RefQualifierIsLValueRef? RQ_LValue
2496 : RQ_RValue;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002497
Reid Spencer5f016e22007-07-11 17:01:13 +00002498 // Otherwise, we have a function with an argument list that is
2499 // potentially variadic.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002500 SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00002501 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002502
Chris Lattner5f9e2722011-07-23 10:55:15 +00002503 SmallVector<bool, 16> ConsumedArguments;
John McCallf85e1932011-06-15 23:02:42 +00002504 ConsumedArguments.reserve(FTI.NumArgs);
2505 bool HasAnyConsumedArguments = false;
2506
Reid Spencer5f016e22007-07-11 17:01:13 +00002507 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002508 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00002509 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00002510 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00002511
2512 // Adjust the parameter type.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002513 assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) &&
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002514 "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00002515
Reid Spencer5f016e22007-07-11 17:01:13 +00002516 // Look for 'void'. void is allowed only as a single argument to a
2517 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00002518 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00002519 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 // If this is something like 'float(int, void)', reject it. 'void'
2521 // is an incomplete type (C99 6.2.5p19) and function decls cannot
2522 // have arguments of incomplete type.
2523 if (FTI.NumArgs != 1 || FTI.isVariadic) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002524 S.Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00002525 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00002526 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00002527 } else if (FTI.ArgInfo[i].Ident) {
2528 // Reject, but continue to parse 'int(void abc)'.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002529 S.Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00002530 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00002531 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00002532 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00002533 } else {
2534 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00002535 if (ArgTy.hasQualifiers())
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002536 S.Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00002537
Chris Lattner2ff54262007-07-21 05:18:12 +00002538 // Do not add 'void' to the ArgTys list.
2539 break;
2540 }
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002541 } else if (ArgTy->isHalfType()) {
2542 // Disallow half FP arguments.
2543 // FIXME: This really should be in BuildFunctionType.
2544 S.Diag(Param->getLocation(),
2545 diag::err_parameters_retval_cannot_have_fp16_type) << 0
2546 << FixItHint::CreateInsertion(Param->getLocation(), "*");
2547 D.setInvalidType();
Eli Friedmaneb4b7052008-08-25 21:31:01 +00002548 } else if (!FTI.hasPrototype) {
2549 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00002550 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCalleecf5fa2011-03-09 04:22:44 +00002551 Param->setKNRPromoted(true);
John McCall183700f2009-09-21 23:43:11 +00002552 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
John McCalleecf5fa2011-03-09 04:22:44 +00002553 if (BTy->getKind() == BuiltinType::Float) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00002554 ArgTy = Context.DoubleTy;
John McCalleecf5fa2011-03-09 04:22:44 +00002555 Param->setKNRPromoted(true);
2556 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00002557 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002558 }
Fariborz Jahanian56a965c2010-09-08 21:36:35 +00002559
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002560 if (LangOpts.ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00002561 bool Consumed = Param->hasAttr<NSConsumedAttr>();
2562 ConsumedArguments.push_back(Consumed);
2563 HasAnyConsumedArguments |= Consumed;
2564 }
2565
John McCall54e14c42009-10-22 22:37:11 +00002566 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 }
Sebastian Redl465226e2009-05-27 22:11:52 +00002568
John McCallf85e1932011-06-15 23:02:42 +00002569 if (HasAnyConsumedArguments)
2570 EPI.ConsumedArguments = ConsumedArguments.data();
2571
Chris Lattner5f9e2722011-07-23 10:55:15 +00002572 SmallVector<QualType, 4> Exceptions;
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002573 SmallVector<ParsedType, 2> DynamicExceptions;
2574 SmallVector<SourceRange, 2> DynamicExceptionRanges;
2575 Expr *NoexceptExpr = 0;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002576
Sebastian Redl8b5b4092011-03-06 10:52:04 +00002577 if (FTI.getExceptionSpecType() == EST_Dynamic) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002578 // FIXME: It's rather inefficient to have to split into two vectors
2579 // here.
2580 unsigned N = FTI.NumExceptions;
2581 DynamicExceptions.reserve(N);
2582 DynamicExceptionRanges.reserve(N);
2583 for (unsigned I = 0; I != N; ++I) {
2584 DynamicExceptions.push_back(FTI.Exceptions[I].Ty);
2585 DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range);
John McCalle23cf432010-12-14 08:05:40 +00002586 }
Sebastian Redl8b5b4092011-03-06 10:52:04 +00002587 } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002588 NoexceptExpr = FTI.NoexceptExpr;
2589 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002590
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002591 S.checkExceptionSpecification(FTI.getExceptionSpecType(),
2592 DynamicExceptions,
2593 DynamicExceptionRanges,
2594 NoexceptExpr,
2595 Exceptions,
2596 EPI);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002597
John McCalle23cf432010-12-14 08:05:40 +00002598 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 }
John McCall04a67a62010-02-05 21:31:56 +00002600
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 break;
2602 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002603 case DeclaratorChunk::MemberPointer:
2604 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002605 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002606 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002607 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002608 // Avoid emitting extra errors if we already errored on the scope.
2609 D.setInvalidType(true);
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002610 } else if (S.isDependentScopeSpecifier(SS) ||
2611 dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00002612 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002613 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00002614 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
2615 switch (NNS->getKind()) {
2616 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002617 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002618 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00002619 break;
2620
2621 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002622 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor87c12c42009-11-04 16:49:01 +00002623 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002624 llvm_unreachable("Nested-name-specifier must name a type");
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002625
Douglas Gregor87c12c42009-11-04 16:49:01 +00002626 case NestedNameSpecifier::TypeSpec:
2627 case NestedNameSpecifier::TypeSpecWithTemplate:
2628 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara91ce2c42011-03-10 10:18:27 +00002629 // Note: if the NNS has a prefix and ClsType is a nondependent
2630 // TemplateSpecializationType, then the NNS prefix is NOT included
2631 // in ClsType; hence we wrap ClsType into an ElaboratedType.
2632 // NOTE: in particular, no wrap occurs if ClsType already is an
2633 // Elaborated, DependentName, or DependentTemplateSpecialization.
2634 if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
Abramo Bagnara7bd06762010-08-13 12:56:25 +00002635 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00002636 break;
2637 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002638 } else {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002639 S.Diag(DeclType.Mem.Scope().getBeginLoc(),
Douglas Gregor949bf692009-06-09 22:17:39 +00002640 diag::err_illegal_decl_mempointer_in_nonclass)
2641 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2642 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002643 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002644 }
2645
Douglas Gregor949bf692009-06-09 22:17:39 +00002646 if (!ClsType.isNull())
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002647 T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00002648 if (T.isNull()) {
2649 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002650 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00002651 } else if (DeclType.Mem.TypeQuals) {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002652 T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002653 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002654 break;
2655 }
2656
Douglas Gregorcd281c32009-02-28 00:25:32 +00002657 if (T.isNull()) {
2658 D.setInvalidType(true);
2659 T = Context.IntTy;
2660 }
2661
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002662 // See if there are any attributes on this declarator chunk.
John McCall711c52b2011-01-05 12:14:39 +00002663 if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2664 processTypeAttrs(state, T, false, attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002665 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002666
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002667 if (LangOpts.CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00002668 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00002669 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002670
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002671 // C++ 8.3.5p4:
Douglas Gregor708f3b82010-10-14 22:03:51 +00002672 // A cv-qualifier-seq shall only be part of the function type
2673 // for a nonstatic member function, the function type to which a pointer
2674 // to member refers, or the top-level function type of a function typedef
2675 // declaration.
Douglas Gregor683a81f2011-01-31 16:09:46 +00002676 //
2677 // Core issue 547 also allows cv-qualifiers on function types that are
2678 // top-level template type arguments.
John McCall613ef3d2010-10-19 01:54:45 +00002679 bool FreeFunction;
2680 if (!D.getCXXScopeSpec().isSet()) {
Eli Friedman906a7e12012-01-06 03:05:34 +00002681 FreeFunction = ((D.getContext() != Declarator::MemberContext &&
2682 D.getContext() != Declarator::LambdaExprContext) ||
John McCall613ef3d2010-10-19 01:54:45 +00002683 D.getDeclSpec().isFriendSpecified());
2684 } else {
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002685 DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec());
John McCall613ef3d2010-10-19 01:54:45 +00002686 FreeFunction = (DC && !DC->isRecord());
2687 }
2688
Richard Smith55dec862011-09-30 00:45:47 +00002689 // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member
2690 // function that is not a constructor declares that function to be const.
Richard Smithe9253222012-10-24 23:51:56 +00002691 // FIXME: This should be deferred until we know whether this is a static
2692 // member function (for an out-of-class definition, we don't know
2693 // this until we perform redeclaration lookup).
Richard Smith55dec862011-09-30 00:45:47 +00002694 if (D.getDeclSpec().isConstexprSpecified() && !FreeFunction &&
Richard Smith1bf9a9e2011-11-12 22:28:03 +00002695 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static &&
Richard Smith55dec862011-09-30 00:45:47 +00002696 D.getName().getKind() != UnqualifiedId::IK_ConstructorName &&
2697 D.getName().getKind() != UnqualifiedId::IK_ConstructorTemplateId &&
2698 !(FnTy->getTypeQuals() & DeclSpec::TQ_const)) {
2699 // Rebuild function type adding a 'const' qualifier.
2700 FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2701 EPI.TypeQuals |= DeclSpec::TQ_const;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002702 T = Context.getFunctionType(FnTy->getResultType(),
Richard Smith55dec862011-09-30 00:45:47 +00002703 FnTy->arg_type_begin(),
2704 FnTy->getNumArgs(), EPI);
Richard Smithe9253222012-10-24 23:51:56 +00002705 // Rebuild any parens around the identifier in the function type.
2706 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2707 if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
2708 break;
2709 T = S.BuildParenType(T);
2710 }
Richard Smith55dec862011-09-30 00:45:47 +00002711 }
2712
Richard Smithd37b3602012-02-10 11:05:11 +00002713 // C++11 [dcl.fct]p6 (w/DR1417):
2714 // An attempt to specify a function type with a cv-qualifier-seq or a
2715 // ref-qualifier (including by typedef-name) is ill-formed unless it is:
2716 // - the function type for a non-static member function,
2717 // - the function type to which a pointer to member refers,
2718 // - the top-level function type of a function typedef declaration or
2719 // alias-declaration,
2720 // - the type-id in the default argument of a type-parameter, or
2721 // - the type-id of a template-argument for a type-parameter
2722 if (IsQualifiedFunction &&
2723 !(!FreeFunction &&
2724 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
2725 !IsTypedefName &&
2726 D.getContext() != Declarator::TemplateTypeArgContext) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00002727 SourceLocation Loc = D.getLocStart();
Richard Smithd37b3602012-02-10 11:05:11 +00002728 SourceRange RemovalRange;
2729 unsigned I;
2730 if (D.isFunctionDeclarator(I)) {
2731 SmallVector<SourceLocation, 4> RemovalLocs;
2732 const DeclaratorChunk &Chunk = D.getTypeObject(I);
2733 assert(Chunk.Kind == DeclaratorChunk::Function);
2734 if (Chunk.Fun.hasRefQualifier())
2735 RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc());
2736 if (Chunk.Fun.TypeQuals & Qualifiers::Const)
2737 RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc());
2738 if (Chunk.Fun.TypeQuals & Qualifiers::Volatile)
2739 RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc());
2740 // FIXME: We do not track the location of the __restrict qualifier.
2741 //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict)
2742 // RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc());
2743 if (!RemovalLocs.empty()) {
2744 std::sort(RemovalLocs.begin(), RemovalLocs.end(),
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002745 BeforeThanCompare<SourceLocation>(S.getSourceManager()));
Richard Smithd37b3602012-02-10 11:05:11 +00002746 RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back());
2747 Loc = RemovalLocs.front();
Douglas Gregorc938c162011-01-26 05:01:58 +00002748 }
2749 }
Richard Smithd37b3602012-02-10 11:05:11 +00002750
2751 S.Diag(Loc, diag::err_invalid_qualified_function_type)
2752 << FreeFunction << D.isFunctionDeclarator() << T
2753 << getFunctionQualifiersAsString(FnTy)
2754 << FixItHint::CreateRemoval(RemovalRange);
2755
2756 // Strip the cv-qualifiers and ref-qualifiers from the type.
2757 FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2758 EPI.TypeQuals = 0;
2759 EPI.RefQualifier = RQ_None;
2760
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002761 T = Context.getFunctionType(FnTy->getResultType(),
Richard Smithd37b3602012-02-10 11:05:11 +00002762 FnTy->arg_type_begin(),
2763 FnTy->getNumArgs(), EPI);
Richard Smithe9253222012-10-24 23:51:56 +00002764 // Rebuild any parens around the identifier in the function type.
2765 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2766 if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren)
2767 break;
2768 T = S.BuildParenType(T);
2769 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002770 }
2771 }
Mike Stump1eb44332009-09-09 15:08:12 +00002772
John McCall711c52b2011-01-05 12:14:39 +00002773 // Apply any undistributed attributes from the declarator.
2774 if (!T.isNull())
2775 if (AttributeList *attrs = D.getAttributes())
2776 processTypeAttrs(state, T, false, attrs);
2777
2778 // Diagnose any ignored type attributes.
2779 if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2780
Peter Collingbourne148f1f72011-03-20 08:06:45 +00002781 // C++0x [dcl.constexpr]p9:
2782 // A constexpr specifier used in an object declaration declares the object
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002783 // as const.
Peter Collingbourne148f1f72011-03-20 08:06:45 +00002784 if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
Sebastian Redl73780122010-06-09 21:19:43 +00002785 T.addConst();
2786 }
2787
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002788 // If there was an ellipsis in the declarator, the declaration declares a
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002789 // parameter pack whose type may be a pack expansion type.
2790 if (D.hasEllipsis() && !T.isNull()) {
2791 // C++0x [dcl.fct]p13:
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002792 // A declarator-id or abstract-declarator containing an ellipsis shall
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002793 // only be used in a parameter-declaration. Such a parameter-declaration
2794 // is a parameter pack (14.5.3). [...]
2795 switch (D.getContext()) {
2796 case Declarator::PrototypeContext:
2797 // C++0x [dcl.fct]p13:
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002798 // [...] When it is part of a parameter-declaration-clause, the
2799 // parameter pack is a function parameter pack (14.5.3). The type T
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002800 // of the declarator-id of the function parameter pack shall contain
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002801 // a template parameter pack; each template parameter pack in T is
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002802 // expanded by the function parameter pack.
2803 //
2804 // We represent function parameter packs as function parameters whose
2805 // type is a pack expansion.
2806 if (!T->containsUnexpandedParameterPack()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002807 S.Diag(D.getEllipsisLoc(),
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002808 diag::err_function_parameter_pack_without_parameter_packs)
2809 << T << D.getSourceRange();
2810 D.setEllipsisLoc(SourceLocation());
2811 } else {
Douglas Gregorcded4f62011-01-14 17:04:44 +00002812 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002813 }
2814 break;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002815
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002816 case Declarator::TemplateParamContext:
2817 // C++0x [temp.param]p15:
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002818 // If a template-parameter is a [...] is a parameter-declaration that
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002819 // declares a parameter pack (8.3.5), then the template-parameter is a
2820 // template parameter pack (14.5.3).
2821 //
2822 // Note: core issue 778 clarifies that, if there are any unexpanded
2823 // parameter packs in the type of the non-type template parameter, then
2824 // it expands those parameter packs.
2825 if (T->containsUnexpandedParameterPack())
Douglas Gregorcded4f62011-01-14 17:04:44 +00002826 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Richard Smithe5acd132011-10-14 20:31:37 +00002827 else
2828 S.Diag(D.getEllipsisLoc(),
2829 LangOpts.CPlusPlus0x
2830 ? diag::warn_cxx98_compat_variadic_templates
2831 : diag::ext_variadic_templates);
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002832 break;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002833
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002834 case Declarator::FileContext:
2835 case Declarator::KNRTypeListContext:
John McCallcdda47f2011-10-01 09:56:14 +00002836 case Declarator::ObjCParameterContext: // FIXME: special diagnostic here?
2837 case Declarator::ObjCResultContext: // FIXME: special diagnostic here?
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002838 case Declarator::TypeNameContext:
Argyrios Kyrtzidis0b8c98f2011-06-28 03:01:23 +00002839 case Declarator::CXXNewContext:
Richard Smith162e1c12011-04-15 14:24:37 +00002840 case Declarator::AliasDeclContext:
Richard Smith3e4c6c42011-05-05 21:57:07 +00002841 case Declarator::AliasTemplateContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002842 case Declarator::MemberContext:
2843 case Declarator::BlockContext:
2844 case Declarator::ForContext:
2845 case Declarator::ConditionContext:
2846 case Declarator::CXXCatchContext:
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00002847 case Declarator::ObjCCatchContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002848 case Declarator::BlockLiteralContext:
Eli Friedmanf88c4002012-01-04 04:41:38 +00002849 case Declarator::LambdaExprContext:
Richard Smith7796eb52012-03-12 08:56:40 +00002850 case Declarator::TrailingReturnContext:
Douglas Gregor683a81f2011-01-31 16:09:46 +00002851 case Declarator::TemplateTypeArgContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002852 // FIXME: We may want to allow parameter packs in block-literal contexts
2853 // in the future.
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002854 S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002855 D.setEllipsisLoc(SourceLocation());
2856 break;
2857 }
2858 }
Richard Smithe7397c62011-02-22 00:36:53 +00002859
John McCallbf1a0282010-06-04 23:28:52 +00002860 if (T.isNull())
2861 return Context.getNullTypeSourceInfo();
2862 else if (D.isInvalidType())
2863 return Context.getTrivialTypeSourceInfo(T);
Argyrios Kyrtzidisdb7abf72011-06-28 03:01:12 +00002864
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002865 return S.GetTypeSourceInfoForDeclarator(D, T, TInfo);
2866}
2867
2868/// GetTypeForDeclarator - Convert the type for the specified
2869/// declarator to Type instances.
2870///
2871/// The result of this call will never be null, but the associated
2872/// type may be a null type if there's an unrecoverable error.
2873TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
2874 // Determine the type of the declarator. Not all forms of declarator
2875 // have a type.
2876
2877 TypeProcessingState state(*this, D);
2878
2879 TypeSourceInfo *ReturnTypeInfo = 0;
2880 QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
2881 if (T.isNull())
2882 return Context.getNullTypeSourceInfo();
2883
David Blaikie4e4d0842012-03-11 07:00:24 +00002884 if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount)
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002885 inferARCWriteback(state, T);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002886
Argyrios Kyrtzidis8cfa57b2011-07-01 22:22:42 +00002887 return GetFullTypeForDeclarator(state, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002888}
2889
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002890static void transferARCOwnershipToDeclSpec(Sema &S,
2891 QualType &declSpecTy,
2892 Qualifiers::ObjCLifetime ownership) {
2893 if (declSpecTy->isObjCRetainableType() &&
2894 declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) {
2895 Qualifiers qs;
2896 qs.addObjCLifetime(ownership);
2897 declSpecTy = S.Context.getQualifiedType(declSpecTy, qs);
2898 }
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002899}
2900
2901static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state,
2902 Qualifiers::ObjCLifetime ownership,
2903 unsigned chunkIndex) {
2904 Sema &S = state.getSema();
2905 Declarator &D = state.getDeclarator();
2906
2907 // Look for an explicit lifetime attribute.
2908 DeclaratorChunk &chunk = D.getTypeObject(chunkIndex);
2909 for (const AttributeList *attr = chunk.getAttrs(); attr;
2910 attr = attr->getNext())
Sean Hunt8e083e72012-06-19 23:57:03 +00002911 if (attr->getKind() == AttributeList::AT_ObjCOwnership)
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002912 return;
2913
2914 const char *attrStr = 0;
2915 switch (ownership) {
David Blaikie30263482012-01-20 21:50:17 +00002916 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002917 case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break;
2918 case Qualifiers::OCL_Strong: attrStr = "strong"; break;
2919 case Qualifiers::OCL_Weak: attrStr = "weak"; break;
2920 case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break;
2921 }
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002922
2923 // If there wasn't one, add one (with an invalid source location
2924 // so that we don't make an AttributedType for it).
2925 AttributeList *attr = D.getAttributePool()
2926 .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(),
2927 /*scope*/ 0, SourceLocation(),
2928 &S.Context.Idents.get(attrStr), SourceLocation(),
Sean Hunt93f95f22012-06-18 16:13:52 +00002929 /*args*/ 0, 0, AttributeList::AS_GNU);
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002930 spliceAttrIntoList(*attr, chunk.getAttrListRef());
2931
2932 // TODO: mark whether we did this inference?
2933}
2934
Benjamin Kramer48d798c2012-06-02 10:20:41 +00002935/// \brief Used for transferring ownership in casts resulting in l-values.
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002936static void transferARCOwnership(TypeProcessingState &state,
2937 QualType &declSpecTy,
2938 Qualifiers::ObjCLifetime ownership) {
2939 Sema &S = state.getSema();
2940 Declarator &D = state.getDeclarator();
2941
2942 int inner = -1;
Argyrios Kyrtzidis6ee54922011-10-28 22:54:28 +00002943 bool hasIndirection = false;
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002944 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
2945 DeclaratorChunk &chunk = D.getTypeObject(i);
2946 switch (chunk.Kind) {
2947 case DeclaratorChunk::Paren:
2948 // Ignore parens.
2949 break;
2950
2951 case DeclaratorChunk::Array:
2952 case DeclaratorChunk::Reference:
2953 case DeclaratorChunk::Pointer:
Argyrios Kyrtzidis6ee54922011-10-28 22:54:28 +00002954 if (inner != -1)
2955 hasIndirection = true;
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002956 inner = i;
2957 break;
2958
2959 case DeclaratorChunk::BlockPointer:
Argyrios Kyrtzidis6ee54922011-10-28 22:54:28 +00002960 if (inner != -1)
2961 transferARCOwnershipToDeclaratorChunk(state, ownership, i);
2962 return;
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002963
2964 case DeclaratorChunk::Function:
2965 case DeclaratorChunk::MemberPointer:
2966 return;
2967 }
2968 }
2969
2970 if (inner == -1)
Argyrios Kyrtzidis6ee54922011-10-28 22:54:28 +00002971 return;
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002972
Chad Rosier91cbbbf2012-06-26 21:41:40 +00002973 DeclaratorChunk &chunk = D.getTypeObject(inner);
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002974 if (chunk.Kind == DeclaratorChunk::Pointer) {
2975 if (declSpecTy->isObjCRetainableType())
2976 return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
Argyrios Kyrtzidis6ee54922011-10-28 22:54:28 +00002977 if (declSpecTy->isObjCObjectType() && hasIndirection)
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002978 return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
2979 } else {
2980 assert(chunk.Kind == DeclaratorChunk::Array ||
2981 chunk.Kind == DeclaratorChunk::Reference);
2982 return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
2983 }
2984}
2985
2986TypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) {
2987 TypeProcessingState state(*this, D);
2988
2989 TypeSourceInfo *ReturnTypeInfo = 0;
2990 QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo);
2991 if (declSpecTy.isNull())
2992 return Context.getNullTypeSourceInfo();
2993
David Blaikie4e4d0842012-03-11 07:00:24 +00002994 if (getLangOpts().ObjCAutoRefCount) {
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +00002995 Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy);
2996 if (ownership != Qualifiers::OCL_None)
2997 transferARCOwnership(state, declSpecTy, ownership);
2998 }
2999
3000 return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo);
3001}
3002
John McCall14aa2172011-03-04 04:00:19 +00003003/// Map an AttributedType::Kind to an AttributeList::Kind.
3004static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
3005 switch (kind) {
3006 case AttributedType::attr_address_space:
Sean Hunt8e083e72012-06-19 23:57:03 +00003007 return AttributeList::AT_AddressSpace;
John McCall14aa2172011-03-04 04:00:19 +00003008 case AttributedType::attr_regparm:
Sean Hunt8e083e72012-06-19 23:57:03 +00003009 return AttributeList::AT_Regparm;
John McCall14aa2172011-03-04 04:00:19 +00003010 case AttributedType::attr_vector_size:
Sean Hunt8e083e72012-06-19 23:57:03 +00003011 return AttributeList::AT_VectorSize;
John McCall14aa2172011-03-04 04:00:19 +00003012 case AttributedType::attr_neon_vector_type:
Sean Hunt8e083e72012-06-19 23:57:03 +00003013 return AttributeList::AT_NeonVectorType;
John McCall14aa2172011-03-04 04:00:19 +00003014 case AttributedType::attr_neon_polyvector_type:
Sean Hunt8e083e72012-06-19 23:57:03 +00003015 return AttributeList::AT_NeonPolyVectorType;
John McCall14aa2172011-03-04 04:00:19 +00003016 case AttributedType::attr_objc_gc:
Sean Hunt8e083e72012-06-19 23:57:03 +00003017 return AttributeList::AT_ObjCGC;
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003018 case AttributedType::attr_objc_ownership:
Sean Hunt8e083e72012-06-19 23:57:03 +00003019 return AttributeList::AT_ObjCOwnership;
John McCall14aa2172011-03-04 04:00:19 +00003020 case AttributedType::attr_noreturn:
Sean Hunt8e083e72012-06-19 23:57:03 +00003021 return AttributeList::AT_NoReturn;
John McCall14aa2172011-03-04 04:00:19 +00003022 case AttributedType::attr_cdecl:
Sean Hunt8e083e72012-06-19 23:57:03 +00003023 return AttributeList::AT_CDecl;
John McCall14aa2172011-03-04 04:00:19 +00003024 case AttributedType::attr_fastcall:
Sean Hunt8e083e72012-06-19 23:57:03 +00003025 return AttributeList::AT_FastCall;
John McCall14aa2172011-03-04 04:00:19 +00003026 case AttributedType::attr_stdcall:
Sean Hunt8e083e72012-06-19 23:57:03 +00003027 return AttributeList::AT_StdCall;
John McCall14aa2172011-03-04 04:00:19 +00003028 case AttributedType::attr_thiscall:
Sean Hunt8e083e72012-06-19 23:57:03 +00003029 return AttributeList::AT_ThisCall;
John McCall14aa2172011-03-04 04:00:19 +00003030 case AttributedType::attr_pascal:
Sean Hunt8e083e72012-06-19 23:57:03 +00003031 return AttributeList::AT_Pascal;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003032 case AttributedType::attr_pcs:
Sean Hunt8e083e72012-06-19 23:57:03 +00003033 return AttributeList::AT_Pcs;
Derek Schuff263366f2012-10-16 22:30:41 +00003034 case AttributedType::attr_pnaclcall:
3035 return AttributeList::AT_PnaclCall;
Guy Benyei38980082012-12-25 08:53:55 +00003036 case AttributedType::attr_inteloclbicc:
3037 return AttributeList::AT_IntelOclBicc;
John McCall14aa2172011-03-04 04:00:19 +00003038 }
3039 llvm_unreachable("unexpected attribute kind!");
John McCall14aa2172011-03-04 04:00:19 +00003040}
3041
3042static void fillAttributedTypeLoc(AttributedTypeLoc TL,
3043 const AttributeList *attrs) {
3044 AttributedType::Kind kind = TL.getAttrKind();
3045
3046 assert(attrs && "no type attributes in the expected location!");
3047 AttributeList::Kind parsedKind = getAttrListKind(kind);
3048 while (attrs->getKind() != parsedKind) {
3049 attrs = attrs->getNext();
3050 assert(attrs && "no matching attribute in expected location!");
3051 }
3052
3053 TL.setAttrNameLoc(attrs->getLoc());
3054 if (TL.hasAttrExprOperand())
3055 TL.setAttrExprOperand(attrs->getArg(0));
3056 else if (TL.hasAttrEnumOperand())
3057 TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
3058
3059 // FIXME: preserve this information to here.
3060 if (TL.hasAttrOperand())
3061 TL.setAttrOperandParensRange(SourceRange());
3062}
3063
John McCall51bd8032009-10-18 01:05:36 +00003064namespace {
3065 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003066 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00003067 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00003068
John McCall51bd8032009-10-18 01:05:36 +00003069 public:
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003070 TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003071 : Context(Context), DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00003072
John McCall14aa2172011-03-04 04:00:19 +00003073 void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3074 fillAttributedTypeLoc(TL, DS.getAttributes().getList());
3075 Visit(TL.getModifiedLoc());
3076 }
John McCall51bd8032009-10-18 01:05:36 +00003077 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3078 Visit(TL.getUnqualifiedLoc());
3079 }
3080 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3081 TL.setNameLoc(DS.getTypeSpecTypeLoc());
3082 }
3083 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3084 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Fariborz Jahanian1de6a6c2012-05-09 21:49:29 +00003085 // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires
3086 // addition field. What we have is good enough for dispay of location
3087 // of 'fixit' on interface name.
3088 TL.setNameEndLoc(DS.getLocEnd());
John McCallc12c5bb2010-05-15 11:32:37 +00003089 }
3090 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3091 // Handle the base type, which might not have been written explicitly.
3092 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
3093 TL.setHasBaseTypeAsWritten(false);
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003094 TL.getBaseLoc().initialize(Context, SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00003095 } else {
3096 TL.setHasBaseTypeAsWritten(true);
3097 Visit(TL.getBaseLoc());
3098 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00003099
John McCallc12c5bb2010-05-15 11:32:37 +00003100 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00003101 if (DS.getProtocolQualifiers()) {
3102 assert(TL.getNumProtocols() > 0);
3103 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
3104 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
3105 TL.setRAngleLoc(DS.getSourceRange().getEnd());
3106 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
3107 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
3108 } else {
3109 assert(TL.getNumProtocols() == 0);
3110 TL.setLAngleLoc(SourceLocation());
3111 TL.setRAngleLoc(SourceLocation());
3112 }
3113 }
3114 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00003115 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00003116 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00003117 }
John McCall833ca992009-10-29 08:12:44 +00003118 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00003119 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00003120 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00003121
3122 // If we got no declarator info from previous Sema routines,
3123 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00003124 if (!TInfo) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003125 TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
John McCall833ca992009-10-29 08:12:44 +00003126 return;
3127 }
3128
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003129 TypeLoc OldTL = TInfo->getTypeLoc();
3130 if (TInfo->getType()->getAs<ElaboratedType>()) {
3131 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
3132 TemplateSpecializationTypeLoc NamedTL =
3133 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
3134 TL.copy(NamedTL);
3135 }
3136 else
3137 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00003138 }
John McCallcfb708c2010-01-13 20:03:27 +00003139 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3140 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
3141 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3142 TL.setParensRange(DS.getTypeofParensRange());
3143 }
3144 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3145 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
3146 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
3147 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00003148 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00003149 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00003150 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00003151 TL.setUnderlyingTInfo(TInfo);
3152 }
Sean Huntca63c202011-05-24 22:41:36 +00003153 void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3154 // FIXME: This holds only because we only have one unary transform.
3155 assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType);
3156 TL.setKWLoc(DS.getTypeSpecTypeLoc());
3157 TL.setParensRange(DS.getTypeofParensRange());
3158 assert(DS.getRepAsType());
3159 TypeSourceInfo *TInfo = 0;
3160 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3161 TL.setUnderlyingTInfo(TInfo);
3162 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00003163 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3164 // By default, use the source location of the type specifier.
3165 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
3166 if (TL.needsExtraLocalData()) {
3167 // Set info for the written builtin specifiers.
3168 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
3169 // Try to have a meaningful source location.
3170 if (TL.getWrittenSignSpec() != TSS_unspecified)
3171 // Sign spec loc overrides the others (e.g., 'unsigned long').
3172 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
3173 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
3174 // Width spec loc overrides type spec loc (e.g., 'short int').
3175 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
3176 }
3177 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003178 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3179 ElaboratedTypeKeyword Keyword
3180 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00003181 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003182 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00003183 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003184 if (TInfo) {
3185 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
3186 return;
3187 }
3188 }
Abramo Bagnara38a42912012-02-06 19:09:27 +00003189 TL.setElaboratedKeywordLoc(Keyword != ETK_None
3190 ? DS.getTypeSpecTypeLoc()
3191 : SourceLocation());
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003192 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor9e876872011-03-01 18:12:44 +00003193 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003194 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
3195 }
3196 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara66581d42012-02-06 22:45:07 +00003197 assert(DS.getTypeSpecType() == TST_typename);
3198 TypeSourceInfo *TInfo = 0;
3199 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3200 assert(TInfo);
3201 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003202 }
John McCall33500952010-06-11 00:33:02 +00003203 void VisitDependentTemplateSpecializationTypeLoc(
3204 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara66581d42012-02-06 22:45:07 +00003205 assert(DS.getTypeSpecType() == TST_typename);
3206 TypeSourceInfo *TInfo = 0;
3207 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3208 assert(TInfo);
3209 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
3210 TInfo->getTypeLoc()));
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003211 }
3212 void VisitTagTypeLoc(TagTypeLoc TL) {
3213 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
John McCall33500952010-06-11 00:33:02 +00003214 }
Eli Friedmanb001de72011-10-06 23:00:33 +00003215 void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
3216 TL.setKWLoc(DS.getTypeSpecTypeLoc());
3217 TL.setParensRange(DS.getTypeofParensRange());
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003218
Douglas Gregor43fe2452011-10-09 18:45:17 +00003219 TypeSourceInfo *TInfo = 0;
3220 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
3221 TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc());
Eli Friedmanb001de72011-10-06 23:00:33 +00003222 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003223
John McCall51bd8032009-10-18 01:05:36 +00003224 void VisitTypeLoc(TypeLoc TL) {
3225 // FIXME: add other typespec types and change this to an assert.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003226 TL.initialize(Context, DS.getTypeSpecTypeLoc());
John McCall51bd8032009-10-18 01:05:36 +00003227 }
3228 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00003229
John McCall51bd8032009-10-18 01:05:36 +00003230 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003231 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00003232 const DeclaratorChunk &Chunk;
3233
3234 public:
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003235 DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
3236 : Context(Context), Chunk(Chunk) {}
John McCall51bd8032009-10-18 01:05:36 +00003237
3238 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003239 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00003240 }
3241
John McCallf85e1932011-06-15 23:02:42 +00003242 void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3243 fillAttributedTypeLoc(TL, Chunk.getAttrs());
3244 }
John McCall51bd8032009-10-18 01:05:36 +00003245 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3246 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
3247 TL.setCaretLoc(Chunk.Loc);
3248 }
3249 void VisitPointerTypeLoc(PointerTypeLoc TL) {
3250 assert(Chunk.Kind == DeclaratorChunk::Pointer);
3251 TL.setStarLoc(Chunk.Loc);
3252 }
3253 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3254 assert(Chunk.Kind == DeclaratorChunk::Pointer);
3255 TL.setStarLoc(Chunk.Loc);
3256 }
3257 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3258 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003259 const CXXScopeSpec& SS = Chunk.Mem.Scope();
3260 NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
3261
3262 const Type* ClsTy = TL.getClass();
3263 QualType ClsQT = QualType(ClsTy, 0);
3264 TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
3265 // Now copy source location info into the type loc component.
3266 TypeLoc ClsTL = ClsTInfo->getTypeLoc();
3267 switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
3268 case NestedNameSpecifier::Identifier:
3269 assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
3270 {
Abramo Bagnarafd9c42e2011-03-06 22:48:24 +00003271 DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
Abramo Bagnara38a42912012-02-06 19:09:27 +00003272 DNTLoc.setElaboratedKeywordLoc(SourceLocation());
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003273 DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
3274 DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
3275 }
3276 break;
3277
3278 case NestedNameSpecifier::TypeSpec:
3279 case NestedNameSpecifier::TypeSpecWithTemplate:
3280 if (isa<ElaboratedType>(ClsTy)) {
3281 ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
Abramo Bagnara38a42912012-02-06 19:09:27 +00003282 ETLoc.setElaboratedKeywordLoc(SourceLocation());
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003283 ETLoc.setQualifierLoc(NNSLoc.getPrefix());
3284 TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
3285 NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
3286 } else {
3287 ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
3288 }
3289 break;
3290
3291 case NestedNameSpecifier::Namespace:
3292 case NestedNameSpecifier::NamespaceAlias:
3293 case NestedNameSpecifier::Global:
3294 llvm_unreachable("Nested-name-specifier must name a type");
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003295 }
3296
3297 // Finally fill in MemberPointerLocInfo fields.
John McCall51bd8032009-10-18 01:05:36 +00003298 TL.setStarLoc(Chunk.Loc);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003299 TL.setClassTInfo(ClsTInfo);
John McCall51bd8032009-10-18 01:05:36 +00003300 }
3301 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3302 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00003303 // 'Amp' is misleading: this might have been originally
3304 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00003305 TL.setAmpLoc(Chunk.Loc);
3306 }
3307 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3308 assert(Chunk.Kind == DeclaratorChunk::Reference);
3309 assert(!Chunk.Ref.LValueRef);
3310 TL.setAmpAmpLoc(Chunk.Loc);
3311 }
3312 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
3313 assert(Chunk.Kind == DeclaratorChunk::Array);
3314 TL.setLBracketLoc(Chunk.Loc);
3315 TL.setRBracketLoc(Chunk.EndLoc);
3316 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
3317 }
3318 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3319 assert(Chunk.Kind == DeclaratorChunk::Function);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003320 TL.setLocalRangeBegin(Chunk.Loc);
3321 TL.setLocalRangeEnd(Chunk.EndLoc);
John McCall51bd8032009-10-18 01:05:36 +00003322
3323 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00003324 TL.setLParenLoc(FTI.getLParenLoc());
3325 TL.setRParenLoc(FTI.getRParenLoc());
John McCall54e14c42009-10-22 22:37:11 +00003326 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003327 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00003328 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00003329 }
3330 // FIXME: exception specs
3331 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003332 void VisitParenTypeLoc(ParenTypeLoc TL) {
3333 assert(Chunk.Kind == DeclaratorChunk::Paren);
3334 TL.setLParenLoc(Chunk.Loc);
3335 TL.setRParenLoc(Chunk.EndLoc);
3336 }
John McCall51bd8032009-10-18 01:05:36 +00003337
3338 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003339 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00003340 }
3341 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00003342}
3343
John McCalla93c9342009-12-07 02:54:59 +00003344/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00003345///
3346/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00003347///
3348/// \param ReturnTypeInfo For declarators whose return type does not show
3349/// up in the normal place in the declaration specifiers (such as a C++
3350/// conversion function), this pointer will refer to a type source information
3351/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00003352TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00003353Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
3354 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00003355 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
3356 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00003357
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003358 // Handle parameter packs whose type is a pack expansion.
3359 if (isa<PackExpansionType>(T)) {
3360 cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003361 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003362 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003363
Sebastian Redl8ce35b02009-10-25 21:45:37 +00003364 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall14aa2172011-03-04 04:00:19 +00003365 while (isa<AttributedTypeLoc>(CurrTL)) {
3366 AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
3367 fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
3368 CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
3369 }
3370
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003371 DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
John McCall51bd8032009-10-18 01:05:36 +00003372 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00003373 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003374
John McCallb3d87482010-08-24 05:47:05 +00003375 // If we have different source information for the return type, use
3376 // that. This really only applies to C++ conversion functions.
3377 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00003378 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
3379 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
3380 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00003381 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00003382 TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00003383 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003384
John McCalla93c9342009-12-07 02:54:59 +00003385 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00003386}
3387
John McCalla93c9342009-12-07 02:54:59 +00003388/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00003389ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00003390 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
3391 // and Sema during declaration parsing. Try deallocating/caching them when
3392 // it's appropriate, instead of allocating them and keeping them around.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003393 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
Douglas Gregoreb0eb492010-12-10 08:12:03 +00003394 TypeAlignment);
John McCalla93c9342009-12-07 02:54:59 +00003395 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00003396 assert(LocT->getTypeClass() != T->getTypeClass() &&
3397 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00003398 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00003399}
3400
3401void LocInfoType::getAsStringInternal(std::string &Str,
3402 const PrintingPolicy &Policy) const {
David Blaikieb219cfc2011-09-23 05:06:16 +00003403 llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*"
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00003404 " was used directly instead of getting the QualType through"
3405 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00003406}
3407
John McCallf312b1e2010-08-26 23:41:50 +00003408TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003409 // C99 6.7.6: Type names have no identifier. This is already validated by
3410 // the parser.
3411 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003412
Argyrios Kyrtzidisd3880f82011-06-28 03:01:18 +00003413 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003414 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00003415 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00003416 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00003417
John McCalle82247a2011-10-01 05:17:03 +00003418 // Make sure there are no unused decl attributes on the declarator.
John McCallcdda47f2011-10-01 09:56:14 +00003419 // We don't want to do this for ObjC parameters because we're going
3420 // to apply them to the actual parameter declaration.
3421 if (D.getContext() != Declarator::ObjCParameterContext)
3422 checkUnusedDeclAttributes(D);
John McCalle82247a2011-10-01 05:17:03 +00003423
David Blaikie4e4d0842012-03-11 07:00:24 +00003424 if (getLangOpts().CPlusPlus) {
Douglas Gregor402abb52009-05-28 23:31:59 +00003425 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00003426 CheckExtraCXXDefaultArguments(D);
Douglas Gregor402abb52009-05-28 23:31:59 +00003427 }
3428
John McCallb3d87482010-08-24 05:47:05 +00003429 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00003430}
3431
Douglas Gregore97179c2011-09-08 01:46:34 +00003432ParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) {
3433 QualType T = Context.getObjCInstanceType();
3434 TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc);
3435 return CreateParsedType(T, TInfo);
3436}
3437
3438
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003439//===----------------------------------------------------------------------===//
3440// Type Attribute Processing
3441//===----------------------------------------------------------------------===//
3442
3443/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
3444/// specified type. The attribute contains 1 argument, the id of the address
3445/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00003446static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003447 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00003448
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003449 // If this type is already address space qualified, reject it.
Peter Collingbourne29e3ef82011-07-27 20:29:53 +00003450 // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by
3451 // qualifiers for two or more different address spaces."
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003452 if (Type.getAddressSpace()) {
3453 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00003454 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003455 return;
3456 }
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Peter Collingbourne020972d2011-07-27 20:30:05 +00003458 // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be
3459 // qualified by an address-space qualifier."
3460 if (Type->isFunctionType()) {
3461 S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type);
3462 Attr.setInvalid();
3463 return;
3464 }
3465
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003466 // Check the attribute arguments.
3467 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00003468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003469 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003470 return;
3471 }
3472 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
3473 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003474 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
3475 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00003476 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
3477 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003478 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003479 return;
3480 }
3481
John McCallefadb772009-07-28 06:52:18 +00003482 // Bounds checking.
3483 if (addrSpace.isSigned()) {
3484 if (addrSpace.isNegative()) {
3485 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
3486 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003487 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00003488 return;
3489 }
3490 addrSpace.setIsSigned(false);
3491 }
3492 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00003493 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00003494 if (addrSpace > max) {
3495 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00003496 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003497 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00003498 return;
3499 }
3500
Mike Stump1eb44332009-09-09 15:08:12 +00003501 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00003502 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003503}
3504
John McCalld85bf9d2012-02-08 00:46:41 +00003505/// Does this type have a "direct" ownership qualifier? That is,
3506/// is it written like "__strong id", as opposed to something like
3507/// "typeof(foo)", where that happens to be strong?
3508static bool hasDirectOwnershipQualifier(QualType type) {
3509 // Fast path: no qualifier at all.
3510 assert(type.getQualifiers().hasObjCLifetime());
3511
3512 while (true) {
3513 // __strong id
3514 if (const AttributedType *attr = dyn_cast<AttributedType>(type)) {
3515 if (attr->getAttrKind() == AttributedType::attr_objc_ownership)
3516 return true;
3517
3518 type = attr->getModifiedType();
3519
3520 // X *__strong (...)
3521 } else if (const ParenType *paren = dyn_cast<ParenType>(type)) {
3522 type = paren->getInnerType();
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003523
John McCalld85bf9d2012-02-08 00:46:41 +00003524 // That's it for things we want to complain about. In particular,
3525 // we do not want to look through typedefs, typeof(expr),
3526 // typeof(type), or any other way that the type is somehow
3527 // abstracted.
3528 } else {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003529
John McCalld85bf9d2012-02-08 00:46:41 +00003530 return false;
3531 }
3532 }
3533}
3534
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003535/// handleObjCOwnershipTypeAttr - Process an objc_ownership
John McCallf85e1932011-06-15 23:02:42 +00003536/// attribute on the specified type.
3537///
3538/// Returns 'true' if the attribute was handled.
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003539static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state,
John McCallf85e1932011-06-15 23:02:42 +00003540 AttributeList &attr,
3541 QualType &type) {
Argyrios Kyrtzidise71202e2011-11-04 20:37:24 +00003542 bool NonObjCPointer = false;
3543
3544 if (!type->isDependentType()) {
3545 if (const PointerType *ptr = type->getAs<PointerType>()) {
3546 QualType pointee = ptr->getPointeeType();
3547 if (pointee->isObjCRetainableType() || pointee->isPointerType())
3548 return false;
3549 // It is important not to lose the source info that there was an attribute
3550 // applied to non-objc pointer. We will create an attributed type but
3551 // its type will be the same as the original type.
3552 NonObjCPointer = true;
3553 } else if (!type->isObjCRetainableType()) {
3554 return false;
3555 }
3556 }
John McCallf85e1932011-06-15 23:02:42 +00003557
3558 Sema &S = state.getSema();
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003559 SourceLocation AttrLoc = attr.getLoc();
3560 if (AttrLoc.isMacroID())
3561 AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first;
John McCallf85e1932011-06-15 23:02:42 +00003562
John McCallf85e1932011-06-15 23:02:42 +00003563 if (!attr.getParameterName()) {
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003564 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string)
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003565 << "objc_ownership" << 1;
John McCallf85e1932011-06-15 23:02:42 +00003566 attr.setInvalid();
3567 return true;
3568 }
3569
John McCalld85bf9d2012-02-08 00:46:41 +00003570 // Consume lifetime attributes without further comment outside of
3571 // ARC mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003572 if (!S.getLangOpts().ObjCAutoRefCount)
John McCalld85bf9d2012-02-08 00:46:41 +00003573 return true;
3574
John McCallf85e1932011-06-15 23:02:42 +00003575 Qualifiers::ObjCLifetime lifetime;
3576 if (attr.getParameterName()->isStr("none"))
3577 lifetime = Qualifiers::OCL_ExplicitNone;
3578 else if (attr.getParameterName()->isStr("strong"))
3579 lifetime = Qualifiers::OCL_Strong;
3580 else if (attr.getParameterName()->isStr("weak"))
3581 lifetime = Qualifiers::OCL_Weak;
3582 else if (attr.getParameterName()->isStr("autoreleasing"))
3583 lifetime = Qualifiers::OCL_Autoreleasing;
3584 else {
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003585 S.Diag(AttrLoc, diag::warn_attribute_type_not_supported)
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003586 << "objc_ownership" << attr.getParameterName();
John McCallf85e1932011-06-15 23:02:42 +00003587 attr.setInvalid();
3588 return true;
3589 }
3590
John McCalld85bf9d2012-02-08 00:46:41 +00003591 SplitQualType underlyingType = type.split();
3592
3593 // Check for redundant/conflicting ownership qualifiers.
3594 if (Qualifiers::ObjCLifetime previousLifetime
3595 = type.getQualifiers().getObjCLifetime()) {
3596 // If it's written directly, that's an error.
3597 if (hasDirectOwnershipQualifier(type)) {
3598 S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant)
3599 << type;
3600 return true;
3601 }
3602
3603 // Otherwise, if the qualifiers actually conflict, pull sugar off
3604 // until we reach a type that is directly qualified.
3605 if (previousLifetime != lifetime) {
3606 // This should always terminate: the canonical type is
3607 // qualified, so some bit of sugar must be hiding it.
3608 while (!underlyingType.Quals.hasObjCLifetime()) {
3609 underlyingType = underlyingType.getSingleStepDesugaredType();
3610 }
3611 underlyingType.Quals.removeObjCLifetime();
3612 }
3613 }
3614
3615 underlyingType.Quals.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00003616
Argyrios Kyrtzidise71202e2011-11-04 20:37:24 +00003617 if (NonObjCPointer) {
3618 StringRef name = attr.getName()->getName();
3619 switch (lifetime) {
3620 case Qualifiers::OCL_None:
3621 case Qualifiers::OCL_ExplicitNone:
3622 break;
3623 case Qualifiers::OCL_Strong: name = "__strong"; break;
3624 case Qualifiers::OCL_Weak: name = "__weak"; break;
3625 case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break;
3626 }
3627 S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type)
3628 << name << type;
3629 }
3630
John McCallf85e1932011-06-15 23:02:42 +00003631 QualType origType = type;
Argyrios Kyrtzidise71202e2011-11-04 20:37:24 +00003632 if (!NonObjCPointer)
John McCalld85bf9d2012-02-08 00:46:41 +00003633 type = S.Context.getQualifiedType(underlyingType);
John McCallf85e1932011-06-15 23:02:42 +00003634
3635 // If we have a valid source location for the attribute, use an
3636 // AttributedType instead.
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003637 if (AttrLoc.isValid())
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003638 type = S.Context.getAttributedType(AttributedType::attr_objc_ownership,
John McCallf85e1932011-06-15 23:02:42 +00003639 origType, type);
3640
John McCall9f084a32011-07-06 00:26:06 +00003641 // Forbid __weak if the runtime doesn't support it.
John McCallf85e1932011-06-15 23:02:42 +00003642 if (lifetime == Qualifiers::OCL_Weak &&
John McCall0a7dd782012-08-21 02:47:43 +00003643 !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) {
John McCallf85e1932011-06-15 23:02:42 +00003644
3645 // Actually, delay this until we know what we're parsing.
3646 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
3647 S.DelayedDiagnostics.add(
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003648 sema::DelayedDiagnostic::makeForbiddenType(
3649 S.getSourceManager().getExpansionLoc(AttrLoc),
John McCallf85e1932011-06-15 23:02:42 +00003650 diag::err_arc_weak_no_runtime, type, /*ignored*/ 0));
3651 } else {
Argyrios Kyrtzidis440ec2e2011-09-28 18:35:06 +00003652 S.Diag(AttrLoc, diag::err_arc_weak_no_runtime);
John McCallf85e1932011-06-15 23:02:42 +00003653 }
3654
3655 attr.setInvalid();
3656 return true;
3657 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003658
3659 // Forbid __weak for class objects marked as
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003660 // objc_arc_weak_reference_unavailable
3661 if (lifetime == Qualifiers::OCL_Weak) {
3662 QualType T = type;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003663 while (const PointerType *ptr = T->getAs<PointerType>())
3664 T = ptr->getPointeeType();
3665 if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
Richard Smith4e90bc32012-08-23 04:53:18 +00003666 if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) {
3667 if (Class->isArcWeakrefUnavailable()) {
3668 S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class);
3669 S.Diag(ObjT->getInterfaceDecl()->getLocation(),
3670 diag::note_class_declared);
3671 }
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003672 }
3673 }
3674 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003675
John McCallf85e1932011-06-15 23:02:42 +00003676 return true;
3677}
3678
John McCall711c52b2011-01-05 12:14:39 +00003679/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
3680/// attribute on the specified type. Returns true to indicate that
3681/// the attribute was handled, false to indicate that the type does
3682/// not permit the attribute.
3683static bool handleObjCGCTypeAttr(TypeProcessingState &state,
3684 AttributeList &attr,
3685 QualType &type) {
3686 Sema &S = state.getSema();
3687
3688 // Delay if this isn't some kind of pointer.
3689 if (!type->isPointerType() &&
3690 !type->isObjCObjectPointerType() &&
3691 !type->isBlockPointerType())
3692 return false;
3693
3694 if (type.getObjCGCAttr() != Qualifiers::GCNone) {
3695 S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
3696 attr.setInvalid();
3697 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003698 }
Mike Stump1eb44332009-09-09 15:08:12 +00003699
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003700 // Check the attribute arguments.
John McCall711c52b2011-01-05 12:14:39 +00003701 if (!attr.getParameterName()) {
3702 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003703 << "objc_gc" << 1;
John McCall711c52b2011-01-05 12:14:39 +00003704 attr.setInvalid();
3705 return true;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003706 }
John McCall0953e762009-09-24 19:53:00 +00003707 Qualifiers::GC GCAttr;
John McCall711c52b2011-01-05 12:14:39 +00003708 if (attr.getNumArgs() != 0) {
3709 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3710 attr.setInvalid();
3711 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003712 }
John McCall711c52b2011-01-05 12:14:39 +00003713 if (attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00003714 GCAttr = Qualifiers::Weak;
John McCall711c52b2011-01-05 12:14:39 +00003715 else if (attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00003716 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003717 else {
John McCall711c52b2011-01-05 12:14:39 +00003718 S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
3719 << "objc_gc" << attr.getParameterName();
3720 attr.setInvalid();
3721 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003722 }
Mike Stump1eb44332009-09-09 15:08:12 +00003723
John McCall14aa2172011-03-04 04:00:19 +00003724 QualType origType = type;
3725 type = S.Context.getObjCGCQualType(origType, GCAttr);
3726
3727 // Make an attributed type to preserve the source information.
3728 if (attr.getLoc().isValid())
3729 type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
3730 origType, type);
3731
John McCall711c52b2011-01-05 12:14:39 +00003732 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00003733}
3734
John McCalle6a365d2010-12-19 02:44:49 +00003735namespace {
3736 /// A helper class to unwrap a type down to a function for the
3737 /// purposes of applying attributes there.
3738 ///
3739 /// Use:
3740 /// FunctionTypeUnwrapper unwrapped(SemaRef, T);
3741 /// if (unwrapped.isFunctionType()) {
3742 /// const FunctionType *fn = unwrapped.get();
3743 /// // change fn somehow
3744 /// T = unwrapped.wrap(fn);
3745 /// }
3746 struct FunctionTypeUnwrapper {
3747 enum WrapKind {
3748 Desugar,
3749 Parens,
3750 Pointer,
3751 BlockPointer,
3752 Reference,
3753 MemberPointer
3754 };
3755
3756 QualType Original;
3757 const FunctionType *Fn;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003758 SmallVector<unsigned char /*WrapKind*/, 8> Stack;
John McCalle6a365d2010-12-19 02:44:49 +00003759
3760 FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
3761 while (true) {
3762 const Type *Ty = T.getTypePtr();
3763 if (isa<FunctionType>(Ty)) {
3764 Fn = cast<FunctionType>(Ty);
3765 return;
3766 } else if (isa<ParenType>(Ty)) {
3767 T = cast<ParenType>(Ty)->getInnerType();
3768 Stack.push_back(Parens);
3769 } else if (isa<PointerType>(Ty)) {
3770 T = cast<PointerType>(Ty)->getPointeeType();
3771 Stack.push_back(Pointer);
3772 } else if (isa<BlockPointerType>(Ty)) {
3773 T = cast<BlockPointerType>(Ty)->getPointeeType();
3774 Stack.push_back(BlockPointer);
3775 } else if (isa<MemberPointerType>(Ty)) {
3776 T = cast<MemberPointerType>(Ty)->getPointeeType();
3777 Stack.push_back(MemberPointer);
3778 } else if (isa<ReferenceType>(Ty)) {
3779 T = cast<ReferenceType>(Ty)->getPointeeType();
3780 Stack.push_back(Reference);
3781 } else {
3782 const Type *DTy = Ty->getUnqualifiedDesugaredType();
3783 if (Ty == DTy) {
3784 Fn = 0;
3785 return;
3786 }
3787
3788 T = QualType(DTy, 0);
3789 Stack.push_back(Desugar);
3790 }
3791 }
3792 }
3793
3794 bool isFunctionType() const { return (Fn != 0); }
3795 const FunctionType *get() const { return Fn; }
3796
3797 QualType wrap(Sema &S, const FunctionType *New) {
3798 // If T wasn't modified from the unwrapped type, do nothing.
3799 if (New == get()) return Original;
3800
3801 Fn = New;
3802 return wrap(S.Context, Original, 0);
3803 }
3804
3805 private:
3806 QualType wrap(ASTContext &C, QualType Old, unsigned I) {
3807 if (I == Stack.size())
3808 return C.getQualifiedType(Fn, Old.getQualifiers());
3809
3810 // Build up the inner type, applying the qualifiers from the old
3811 // type to the new type.
3812 SplitQualType SplitOld = Old.split();
3813
3814 // As a special case, tail-recurse if there are no qualifiers.
John McCall200fa532012-02-08 00:46:36 +00003815 if (SplitOld.Quals.empty())
3816 return wrap(C, SplitOld.Ty, I);
3817 return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals);
John McCalle6a365d2010-12-19 02:44:49 +00003818 }
3819
3820 QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
3821 if (I == Stack.size()) return QualType(Fn, 0);
3822
3823 switch (static_cast<WrapKind>(Stack[I++])) {
3824 case Desugar:
3825 // This is the point at which we potentially lose source
3826 // information.
3827 return wrap(C, Old->getUnqualifiedDesugaredType(), I);
3828
3829 case Parens: {
3830 QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
3831 return C.getParenType(New);
3832 }
3833
3834 case Pointer: {
3835 QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
3836 return C.getPointerType(New);
3837 }
3838
3839 case BlockPointer: {
3840 QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
3841 return C.getBlockPointerType(New);
3842 }
3843
3844 case MemberPointer: {
3845 const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
3846 QualType New = wrap(C, OldMPT->getPointeeType(), I);
3847 return C.getMemberPointerType(New, OldMPT->getClass());
3848 }
3849
3850 case Reference: {
3851 const ReferenceType *OldRef = cast<ReferenceType>(Old);
3852 QualType New = wrap(C, OldRef->getPointeeType(), I);
3853 if (isa<LValueReferenceType>(OldRef))
3854 return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
3855 else
3856 return C.getRValueReferenceType(New);
3857 }
3858 }
3859
3860 llvm_unreachable("unknown wrapping kind");
John McCalle6a365d2010-12-19 02:44:49 +00003861 }
3862 };
3863}
3864
John McCall711c52b2011-01-05 12:14:39 +00003865/// Process an individual function attribute. Returns true to
3866/// indicate that the attribute was handled, false if it wasn't.
3867static bool handleFunctionTypeAttr(TypeProcessingState &state,
3868 AttributeList &attr,
3869 QualType &type) {
3870 Sema &S = state.getSema();
John McCalle6a365d2010-12-19 02:44:49 +00003871
John McCall711c52b2011-01-05 12:14:39 +00003872 FunctionTypeUnwrapper unwrapped(S, type);
Mike Stump24556362009-07-25 21:26:53 +00003873
Sean Hunt8e083e72012-06-19 23:57:03 +00003874 if (attr.getKind() == AttributeList::AT_NoReturn) {
John McCall711c52b2011-01-05 12:14:39 +00003875 if (S.CheckNoReturnAttr(attr))
John McCall04a67a62010-02-05 21:31:56 +00003876 return true;
John McCalle6a365d2010-12-19 02:44:49 +00003877
John McCall711c52b2011-01-05 12:14:39 +00003878 // Delay if this is not a function type.
3879 if (!unwrapped.isFunctionType())
3880 return false;
3881
John McCall04a67a62010-02-05 21:31:56 +00003882 // Otherwise we can process right away.
John McCall711c52b2011-01-05 12:14:39 +00003883 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
3884 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3885 return true;
John McCall04a67a62010-02-05 21:31:56 +00003886 }
Mike Stump24556362009-07-25 21:26:53 +00003887
John McCallf85e1932011-06-15 23:02:42 +00003888 // ns_returns_retained is not always a type attribute, but if we got
3889 // here, we're treating it as one right now.
Sean Hunt8e083e72012-06-19 23:57:03 +00003890 if (attr.getKind() == AttributeList::AT_NSReturnsRetained) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003891 assert(S.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00003892 "ns_returns_retained treated as type attribute in non-ARC");
3893 if (attr.getNumArgs()) return true;
3894
3895 // Delay if this is not a function type.
3896 if (!unwrapped.isFunctionType())
3897 return false;
3898
3899 FunctionType::ExtInfo EI
3900 = unwrapped.get()->getExtInfo().withProducesResult(true);
3901 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3902 return true;
3903 }
3904
Sean Hunt8e083e72012-06-19 23:57:03 +00003905 if (attr.getKind() == AttributeList::AT_Regparm) {
John McCall711c52b2011-01-05 12:14:39 +00003906 unsigned value;
3907 if (S.CheckRegparmAttr(attr, value))
Rafael Espindola425ef722010-03-30 22:15:11 +00003908 return true;
3909
John McCall711c52b2011-01-05 12:14:39 +00003910 // Delay if this is not a function type.
3911 if (!unwrapped.isFunctionType())
Rafael Espindola425ef722010-03-30 22:15:11 +00003912 return false;
3913
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00003914 // Diagnose regparm with fastcall.
3915 const FunctionType *fn = unwrapped.get();
3916 CallingConv CC = fn->getCallConv();
3917 if (CC == CC_X86FastCall) {
3918 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3919 << FunctionType::getNameForCallConv(CC)
3920 << "regparm";
3921 attr.setInvalid();
3922 return true;
3923 }
3924
Chad Rosier91cbbbf2012-06-26 21:41:40 +00003925 FunctionType::ExtInfo EI =
John McCall711c52b2011-01-05 12:14:39 +00003926 unwrapped.get()->getExtInfo().withRegParm(value);
3927 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3928 return true;
Rafael Espindola425ef722010-03-30 22:15:11 +00003929 }
3930
Aaron Ballman82bfa192012-10-02 14:26:08 +00003931 // Delay if the type didn't work out to a function.
3932 if (!unwrapped.isFunctionType()) return false;
3933
John McCall04a67a62010-02-05 21:31:56 +00003934 // Otherwise, a calling convention.
John McCall711c52b2011-01-05 12:14:39 +00003935 CallingConv CC;
3936 if (S.CheckCallingConvAttr(attr, CC))
3937 return true;
John McCallf82b4e82010-02-04 05:44:44 +00003938
John McCall711c52b2011-01-05 12:14:39 +00003939 const FunctionType *fn = unwrapped.get();
3940 CallingConv CCOld = fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00003941 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00003942 S.Context.getCanonicalCallConv(CCOld)) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00003943 FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
3944 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
John McCall711c52b2011-01-05 12:14:39 +00003945 return true;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003946 }
John McCall04a67a62010-02-05 21:31:56 +00003947
Roman Divacky8e68f1c2011-08-05 16:37:22 +00003948 if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) {
John McCall04a67a62010-02-05 21:31:56 +00003949 // Should we diagnose reapplications of the same convention?
John McCall711c52b2011-01-05 12:14:39 +00003950 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
John McCall04a67a62010-02-05 21:31:56 +00003951 << FunctionType::getNameForCallConv(CC)
3952 << FunctionType::getNameForCallConv(CCOld);
John McCall711c52b2011-01-05 12:14:39 +00003953 attr.setInvalid();
3954 return true;
John McCall04a67a62010-02-05 21:31:56 +00003955 }
3956
3957 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
3958 if (CC == CC_X86FastCall) {
John McCall711c52b2011-01-05 12:14:39 +00003959 if (isa<FunctionNoProtoType>(fn)) {
3960 S.Diag(attr.getLoc(), diag::err_cconv_knr)
John McCall04a67a62010-02-05 21:31:56 +00003961 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00003962 attr.setInvalid();
3963 return true;
John McCall04a67a62010-02-05 21:31:56 +00003964 }
3965
John McCall711c52b2011-01-05 12:14:39 +00003966 const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
John McCall04a67a62010-02-05 21:31:56 +00003967 if (FnP->isVariadic()) {
John McCall711c52b2011-01-05 12:14:39 +00003968 S.Diag(attr.getLoc(), diag::err_cconv_varargs)
John McCall04a67a62010-02-05 21:31:56 +00003969 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00003970 attr.setInvalid();
3971 return true;
John McCall04a67a62010-02-05 21:31:56 +00003972 }
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00003973
3974 // Also diagnose fastcall with regparm.
Eli Friedmana49218e2011-04-09 08:18:08 +00003975 if (fn->getHasRegParm()) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00003976 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
3977 << "regparm"
3978 << FunctionType::getNameForCallConv(CC);
3979 attr.setInvalid();
3980 return true;
3981 }
John McCall04a67a62010-02-05 21:31:56 +00003982 }
3983
John McCall711c52b2011-01-05 12:14:39 +00003984 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
3985 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
3986 return true;
John McCallf82b4e82010-02-04 05:44:44 +00003987}
3988
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003989/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
3990static void HandleOpenCLImageAccessAttribute(QualType& CurType,
3991 const AttributeList &Attr,
3992 Sema &S) {
3993 // Check the attribute arguments.
3994 if (Attr.getNumArgs() != 1) {
3995 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3996 Attr.setInvalid();
3997 return;
3998 }
3999 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4000 llvm::APSInt arg(32);
4001 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4002 !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
4003 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4004 << "opencl_image_access" << sizeExpr->getSourceRange();
4005 Attr.setInvalid();
4006 return;
4007 }
4008 unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
4009 switch (iarg) {
4010 case CLIA_read_only:
4011 case CLIA_write_only:
4012 case CLIA_read_write:
4013 // Implemented in a separate patch
4014 break;
4015 default:
4016 // Implemented in a separate patch
4017 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4018 << sizeExpr->getSourceRange();
4019 Attr.setInvalid();
4020 break;
4021 }
4022}
4023
John Thompson6e132aa2009-12-04 21:51:28 +00004024/// HandleVectorSizeAttribute - this attribute is only applicable to integral
4025/// and float scalars, although arrays, pointers, and function return values are
4026/// allowed in conjunction with this construct. Aggregates with this attribute
4027/// are invalid, even if they are of the same size as a corresponding scalar.
4028/// The raw attribute should contain precisely 1 argument, the vector size for
4029/// the variable, measured in bytes. If curType and rawAttr are well formed,
4030/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00004031static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
4032 Sema &S) {
Bob Wilson56affbc2010-11-16 00:32:16 +00004033 // Check the attribute arguments.
John Thompson6e132aa2009-12-04 21:51:28 +00004034 if (Attr.getNumArgs() != 1) {
4035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004036 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00004037 return;
4038 }
4039 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
4040 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004041 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
4042 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00004043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4044 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00004045 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00004046 return;
4047 }
4048 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00004049 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00004050 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004051 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00004052 return;
4053 }
4054 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
4055 // vecSize is specified in bytes - convert to bits.
4056 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
4057
4058 // the vector size needs to be an integral multiple of the type size.
4059 if (vectorSize % typeSize) {
4060 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
4061 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00004062 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00004063 return;
4064 }
4065 if (vectorSize == 0) {
4066 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
4067 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00004068 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00004069 return;
4070 }
4071
4072 // Success! Instantiate the vector type, the number of elements is > 0, and
4073 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00004074 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004075 VectorType::GenericVector);
John Thompson6e132aa2009-12-04 21:51:28 +00004076}
4077
Douglas Gregor4ac01402011-06-15 16:02:29 +00004078/// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on
4079/// a type.
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004080static void HandleExtVectorTypeAttr(QualType &CurType,
4081 const AttributeList &Attr,
Douglas Gregor4ac01402011-06-15 16:02:29 +00004082 Sema &S) {
4083 Expr *sizeExpr;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004084
Douglas Gregor4ac01402011-06-15 16:02:29 +00004085 // Special case where the argument is a template id.
4086 if (Attr.getParameterName()) {
4087 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004088 SourceLocation TemplateKWLoc;
Douglas Gregor4ac01402011-06-15 16:02:29 +00004089 UnqualifiedId id;
4090 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004091
4092 ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc,
4093 id, false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +00004094 if (Size.isInvalid())
4095 return;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004096
Douglas Gregor4ac01402011-06-15 16:02:29 +00004097 sizeExpr = Size.get();
4098 } else {
4099 // check the attribute arguments.
4100 if (Attr.getNumArgs() != 1) {
4101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4102 return;
4103 }
4104 sizeExpr = Attr.getArg(0);
4105 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004106
Douglas Gregor4ac01402011-06-15 16:02:29 +00004107 // Create the vector type.
4108 QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc());
4109 if (!T.isNull())
4110 CurType = T;
4111}
4112
Bob Wilson4211bb62010-11-16 00:32:24 +00004113/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
4114/// "neon_polyvector_type" attributes are used to create vector types that
4115/// are mangled according to ARM's ABI. Otherwise, these types are identical
4116/// to those created with the "vector_size" attribute. Unlike "vector_size"
4117/// the argument to these Neon attributes is the number of vector elements,
4118/// not the vector size in bytes. The vector width and element type must
4119/// match one of the standard Neon vector types.
4120static void HandleNeonVectorTypeAttr(QualType& CurType,
4121 const AttributeList &Attr, Sema &S,
4122 VectorType::VectorKind VecKind,
4123 const char *AttrName) {
4124 // Check the attribute arguments.
4125 if (Attr.getNumArgs() != 1) {
4126 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
4127 Attr.setInvalid();
4128 return;
4129 }
4130 // The number of elements must be an ICE.
4131 Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
4132 llvm::APSInt numEltsInt(32);
4133 if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
4134 !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
4135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
4136 << AttrName << numEltsExpr->getSourceRange();
4137 Attr.setInvalid();
4138 return;
4139 }
4140 // Only certain element types are supported for Neon vectors.
4141 const BuiltinType* BTy = CurType->getAs<BuiltinType>();
4142 if (!BTy ||
4143 (VecKind == VectorType::NeonPolyVector &&
4144 BTy->getKind() != BuiltinType::SChar &&
4145 BTy->getKind() != BuiltinType::Short) ||
4146 (BTy->getKind() != BuiltinType::SChar &&
4147 BTy->getKind() != BuiltinType::UChar &&
4148 BTy->getKind() != BuiltinType::Short &&
4149 BTy->getKind() != BuiltinType::UShort &&
4150 BTy->getKind() != BuiltinType::Int &&
4151 BTy->getKind() != BuiltinType::UInt &&
4152 BTy->getKind() != BuiltinType::LongLong &&
4153 BTy->getKind() != BuiltinType::ULongLong &&
4154 BTy->getKind() != BuiltinType::Float)) {
4155 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
4156 Attr.setInvalid();
4157 return;
4158 }
4159 // The total size of the vector must be 64 or 128 bits.
4160 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
4161 unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
4162 unsigned vecSize = typeSize * numElts;
4163 if (vecSize != 64 && vecSize != 128) {
4164 S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
4165 Attr.setInvalid();
4166 return;
4167 }
4168
4169 CurType = S.Context.getVectorType(CurType, numElts, VecKind);
4170}
4171
John McCall711c52b2011-01-05 12:14:39 +00004172static void processTypeAttrs(TypeProcessingState &state, QualType &type,
4173 bool isDeclSpec, AttributeList *attrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00004174 // Scan through and apply attributes to this type where it makes sense. Some
4175 // attributes (such as __address_space__, __vector_size__, etc) apply to the
4176 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00004177 // apply to the decl. Here we apply type attributes and ignore the rest.
John McCall711c52b2011-01-05 12:14:39 +00004178
4179 AttributeList *next;
4180 do {
4181 AttributeList &attr = *attrs;
4182 next = attr.getNext();
4183
Abramo Bagnarae215f722010-04-30 13:10:51 +00004184 // Skip attributes that were marked to be invalid.
John McCall711c52b2011-01-05 12:14:39 +00004185 if (attr.isInvalid())
Abramo Bagnarae215f722010-04-30 13:10:51 +00004186 continue;
4187
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00004188 // If this is an attribute we can handle, do so now,
4189 // otherwise, add it to the FnAttrs list for rechaining.
John McCall711c52b2011-01-05 12:14:39 +00004190 switch (attr.getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00004191 default: break;
John McCall04a67a62010-02-05 21:31:56 +00004192
Sean Hunt8e083e72012-06-19 23:57:03 +00004193 case AttributeList::AT_MayAlias:
Chandler Carruth682eae22011-10-07 18:40:27 +00004194 // FIXME: This attribute needs to actually be handled, but if we ignore
4195 // it it breaks large amounts of Linux software.
4196 attr.setUsedAsTypeAttr();
4197 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004198 case AttributeList::AT_AddressSpace:
John McCall711c52b2011-01-05 12:14:39 +00004199 HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
John McCalle82247a2011-10-01 05:17:03 +00004200 attr.setUsedAsTypeAttr();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00004201 break;
John McCall711c52b2011-01-05 12:14:39 +00004202 OBJC_POINTER_TYPE_ATTRS_CASELIST:
4203 if (!handleObjCPointerTypeAttr(state, attr, type))
4204 distributeObjCPointerTypeAttr(state, attr, type);
John McCalle82247a2011-10-01 05:17:03 +00004205 attr.setUsedAsTypeAttr();
Mike Stump24556362009-07-25 21:26:53 +00004206 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004207 case AttributeList::AT_VectorSize:
John McCall711c52b2011-01-05 12:14:39 +00004208 HandleVectorSizeAttr(type, attr, state.getSema());
John McCalle82247a2011-10-01 05:17:03 +00004209 attr.setUsedAsTypeAttr();
John McCall04a67a62010-02-05 21:31:56 +00004210 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004211 case AttributeList::AT_ExtVectorType:
Douglas Gregor4ac01402011-06-15 16:02:29 +00004212 if (state.getDeclarator().getDeclSpec().getStorageClassSpec()
4213 != DeclSpec::SCS_typedef)
4214 HandleExtVectorTypeAttr(type, attr, state.getSema());
John McCalle82247a2011-10-01 05:17:03 +00004215 attr.setUsedAsTypeAttr();
Douglas Gregor4ac01402011-06-15 16:02:29 +00004216 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004217 case AttributeList::AT_NeonVectorType:
John McCall711c52b2011-01-05 12:14:39 +00004218 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4219 VectorType::NeonVector, "neon_vector_type");
John McCalle82247a2011-10-01 05:17:03 +00004220 attr.setUsedAsTypeAttr();
Bob Wilson4211bb62010-11-16 00:32:24 +00004221 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004222 case AttributeList::AT_NeonPolyVectorType:
John McCall711c52b2011-01-05 12:14:39 +00004223 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
4224 VectorType::NeonPolyVector,
Bob Wilson4211bb62010-11-16 00:32:24 +00004225 "neon_polyvector_type");
John McCalle82247a2011-10-01 05:17:03 +00004226 attr.setUsedAsTypeAttr();
Bob Wilson4211bb62010-11-16 00:32:24 +00004227 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004228 case AttributeList::AT_OpenCLImageAccess:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004229 HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
John McCalle82247a2011-10-01 05:17:03 +00004230 attr.setUsedAsTypeAttr();
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004231 break;
4232
Sean Hunt8e083e72012-06-19 23:57:03 +00004233 case AttributeList::AT_Win64:
4234 case AttributeList::AT_Ptr32:
4235 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004236 // FIXME: don't ignore these
4237 attr.setUsedAsTypeAttr();
4238 break;
4239
Sean Hunt8e083e72012-06-19 23:57:03 +00004240 case AttributeList::AT_NSReturnsRetained:
David Blaikie4e4d0842012-03-11 07:00:24 +00004241 if (!state.getSema().getLangOpts().ObjCAutoRefCount)
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004242 break;
John McCallf85e1932011-06-15 23:02:42 +00004243 // fallthrough into the function attrs
4244
John McCall711c52b2011-01-05 12:14:39 +00004245 FUNCTION_TYPE_ATTRS_CASELIST:
John McCalle82247a2011-10-01 05:17:03 +00004246 attr.setUsedAsTypeAttr();
4247
John McCall711c52b2011-01-05 12:14:39 +00004248 // Never process function type attributes as part of the
4249 // declaration-specifiers.
4250 if (isDeclSpec)
4251 distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
4252
4253 // Otherwise, handle the possible delays.
4254 else if (!handleFunctionTypeAttr(state, attr, type))
4255 distributeFunctionTypeAttr(state, attr, type);
John Thompson6e132aa2009-12-04 21:51:28 +00004256 break;
Chris Lattner232e8822008-02-21 01:08:11 +00004257 }
John McCall711c52b2011-01-05 12:14:39 +00004258 } while ((attrs = next));
Chris Lattner232e8822008-02-21 01:08:11 +00004259}
4260
Chandler Carruthe4d645c2011-05-27 01:33:31 +00004261/// \brief Ensure that the type of the given expression is complete.
4262///
4263/// This routine checks whether the expression \p E has a complete type. If the
4264/// expression refers to an instantiable construct, that instantiation is
4265/// performed as needed to complete its type. Furthermore
4266/// Sema::RequireCompleteType is called for the expression's type (or in the
4267/// case of a reference type, the referred-to type).
4268///
4269/// \param E The expression whose type is required to be complete.
Douglas Gregord10099e2012-05-04 16:32:21 +00004270/// \param Diagnoser The object that will emit a diagnostic if the type is
4271/// incomplete.
Chandler Carruthe4d645c2011-05-27 01:33:31 +00004272///
4273/// \returns \c true if the type of \p E is incomplete and diagnosed, \c false
4274/// otherwise.
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004275bool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){
Chandler Carruthe4d645c2011-05-27 01:33:31 +00004276 QualType T = E->getType();
4277
4278 // Fast path the case where the type is already complete.
4279 if (!T->isIncompleteType())
4280 return false;
4281
4282 // Incomplete array types may be completed by the initializer attached to
4283 // their definitions. For static data members of class templates we need to
4284 // instantiate the definition to get this initializer and complete the type.
4285 if (T->isIncompleteArrayType()) {
4286 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4287 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
4288 if (Var->isStaticDataMember() &&
4289 Var->getInstantiatedFromStaticDataMember()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004290
Douglas Gregor36f255c2011-06-03 14:28:43 +00004291 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
4292 assert(MSInfo && "Missing member specialization information?");
4293 if (MSInfo->getTemplateSpecializationKind()
4294 != TSK_ExplicitSpecialization) {
4295 // If we don't already have a point of instantiation, this is it.
4296 if (MSInfo->getPointOfInstantiation().isInvalid()) {
4297 MSInfo->setPointOfInstantiation(E->getLocStart());
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004298
4299 // This is a modification of an existing AST node. Notify
Douglas Gregor36f255c2011-06-03 14:28:43 +00004300 // listeners.
4301 if (ASTMutationListener *L = getASTMutationListener())
4302 L->StaticDataMemberInstantiated(Var);
4303 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004304
Douglas Gregor36f255c2011-06-03 14:28:43 +00004305 InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004306
Douglas Gregor36f255c2011-06-03 14:28:43 +00004307 // Update the type to the newly instantiated definition's type both
4308 // here and within the expression.
4309 if (VarDecl *Def = Var->getDefinition()) {
4310 DRE->setDecl(Def);
4311 T = Def->getType();
4312 DRE->setType(T);
4313 E->setType(T);
4314 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00004315 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004316
Chandler Carruthe4d645c2011-05-27 01:33:31 +00004317 // We still go on to try to complete the type independently, as it
4318 // may also require instantiations or diagnostics if it remains
4319 // incomplete.
4320 }
4321 }
4322 }
4323 }
4324
4325 // FIXME: Are there other cases which require instantiating something other
4326 // than the type to complete the type of an expression?
4327
4328 // Look through reference types and complete the referred type.
4329 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
4330 T = Ref->getPointeeType();
4331
Douglas Gregord10099e2012-05-04 16:32:21 +00004332 return RequireCompleteType(E->getExprLoc(), T, Diagnoser);
4333}
4334
4335namespace {
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004336 struct TypeDiagnoserDiag : Sema::TypeDiagnoser {
Douglas Gregord10099e2012-05-04 16:32:21 +00004337 unsigned DiagID;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004338
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004339 TypeDiagnoserDiag(unsigned DiagID)
4340 : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {}
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004341
Douglas Gregord10099e2012-05-04 16:32:21 +00004342 virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) {
4343 if (Suppressed) return;
4344 S.Diag(Loc, DiagID) << T;
4345 }
4346 };
4347}
4348
4349bool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) {
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004350 TypeDiagnoserDiag Diagnoser(DiagID);
Douglas Gregord10099e2012-05-04 16:32:21 +00004351 return RequireCompleteExprType(E, Diagnoser);
Chandler Carruthe4d645c2011-05-27 01:33:31 +00004352}
4353
Mike Stump1eb44332009-09-09 15:08:12 +00004354/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004355///
4356/// This routine checks whether the type @p T is complete in any
4357/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00004358/// type, returns false. If @p T is a class template specialization,
4359/// this routine then attempts to perform class template
4360/// instantiation. If instantiation fails, or if @p T is incomplete
4361/// and cannot be completed, issues the diagnostic @p diag (giving it
4362/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004363///
4364/// @param Loc The location in the source that the incomplete type
4365/// diagnostic should refer to.
4366///
4367/// @param T The type that this routine is examining for completeness.
4368///
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004369/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
4370/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00004371bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004372 TypeDiagnoser &Diagnoser) {
Douglas Gregor573d9c32009-10-21 23:19:44 +00004373 // FIXME: Add this assertion to make sure we always get instantiation points.
4374 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00004375 // FIXME: Add this assertion to help us flush out problems with
4376 // checking for dependent types and type-dependent expressions.
4377 //
Mike Stump1eb44332009-09-09 15:08:12 +00004378 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00004379 // "Can't ask whether a dependent type is complete");
4380
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004381 // If we have a complete type, we're done.
Douglas Gregord07cc362012-01-02 17:18:37 +00004382 NamedDecl *Def = 0;
4383 if (!T->isIncompleteType(&Def)) {
4384 // If we know about the definition but it is not visible, complain.
Douglas Gregord10099e2012-05-04 16:32:21 +00004385 if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) {
Douglas Gregord07cc362012-01-02 17:18:37 +00004386 // Suppress this error outside of a SFINAE context if we've already
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004387 // emitted the error once for this type. There's no usefulness in
Douglas Gregord07cc362012-01-02 17:18:37 +00004388 // repeating the diagnostic.
4389 // FIXME: Add a Fix-It that imports the corresponding module or includes
4390 // the header.
4391 if (isSFINAEContext() || HiddenDefinitions.insert(Def)) {
4392 Diag(Loc, diag::err_module_private_definition) << T;
4393 Diag(Def->getLocation(), diag::note_previous_definition);
4394 }
4395 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004396
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004397 return false;
Douglas Gregord07cc362012-01-02 17:18:37 +00004398 }
Eli Friedman3c0eb162008-05-27 03:33:27 +00004399
Sean Callananbd791192011-12-16 00:20:31 +00004400 const TagType *Tag = T->getAs<TagType>();
4401 const ObjCInterfaceType *IFace = 0;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004402
Sean Callananbd791192011-12-16 00:20:31 +00004403 if (Tag) {
4404 // Avoid diagnosing invalid decls as incomplete.
4405 if (Tag->getDecl()->isInvalidDecl())
4406 return true;
4407
4408 // Give the external AST source a chance to complete the type.
4409 if (Tag->getDecl()->hasExternalLexicalStorage()) {
4410 Context.getExternalSource()->CompleteType(Tag->getDecl());
4411 if (!Tag->isIncompleteType())
4412 return false;
4413 }
4414 }
4415 else if ((IFace = T->getAs<ObjCInterfaceType>())) {
4416 // Avoid diagnosing invalid decls as incomplete.
4417 if (IFace->getDecl()->isInvalidDecl())
4418 return true;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004419
Sean Callananbd791192011-12-16 00:20:31 +00004420 // Give the external AST source a chance to complete the type.
4421 if (IFace->getDecl()->hasExternalLexicalStorage()) {
4422 Context.getExternalSource()->CompleteType(IFace->getDecl());
4423 if (!IFace->isIncompleteType())
4424 return false;
4425 }
4426 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004427
Douglas Gregord475b8d2009-03-25 21:17:03 +00004428 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00004429 // class template specialization, or an array with known size of such,
4430 // try to instantiate it.
4431 QualType MaybeTemplate = T;
Douglas Gregore656b832012-04-23 16:42:52 +00004432 while (const ConstantArrayType *Array
4433 = Context.getAsConstantArrayType(MaybeTemplate))
Sebastian Redl923d56d2009-11-05 15:52:31 +00004434 MaybeTemplate = Array->getElementType();
4435 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00004436 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00004437 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00004438 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
4439 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00004440 TSK_ImplicitInstantiation,
Douglas Gregord10099e2012-05-04 16:32:21 +00004441 /*Complain=*/!Diagnoser.Suppressed);
Mike Stump1eb44332009-09-09 15:08:12 +00004442 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00004443 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
Richard Smith564f4c52012-03-22 03:35:28 +00004444 CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass();
4445 if (!Rec->isBeingDefined() && Pattern) {
4446 MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo();
4447 assert(MSI && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00004448 // This record was instantiated from a class within a template.
Richard Smith564f4c52012-03-22 03:35:28 +00004449 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00004450 return InstantiateClass(Loc, Rec, Pattern,
4451 getTemplateInstantiationArgs(Rec),
4452 TSK_ImplicitInstantiation,
Douglas Gregord10099e2012-05-04 16:32:21 +00004453 /*Complain=*/!Diagnoser.Suppressed);
Douglas Gregord475b8d2009-03-25 21:17:03 +00004454 }
4455 }
4456 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00004457
Douglas Gregord10099e2012-05-04 16:32:21 +00004458 if (Diagnoser.Suppressed)
Douglas Gregor5842ba92009-08-24 15:23:48 +00004459 return true;
Douglas Gregord10099e2012-05-04 16:32:21 +00004460
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004461 // We have an incomplete type. Produce a diagnostic.
Douglas Gregord10099e2012-05-04 16:32:21 +00004462 Diagnoser.diagnose(*this, Loc, T);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004463
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004464 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00004465 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004466 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00004467 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004468 Tag->isBeingDefined() ? diag::note_type_being_defined
4469 : diag::note_forward_declaration)
Douglas Gregorb3029962011-11-14 22:10:01 +00004470 << QualType(Tag, 0);
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004471
Douglas Gregorb3029962011-11-14 22:10:01 +00004472 // If the Objective-C class was a forward declaration, produce a note.
4473 if (IFace && !IFace->getDecl()->isInvalidDecl())
4474 Diag(IFace->getDecl()->getLocation(), diag::note_forward_class);
Douglas Gregor4ec339f2009-01-19 19:26:10 +00004475
4476 return true;
4477}
Douglas Gregore6258932009-03-19 00:39:20 +00004478
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004479bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004480 unsigned DiagID) {
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004481 TypeDiagnoserDiag Diagnoser(DiagID);
Douglas Gregord10099e2012-05-04 16:32:21 +00004482 return RequireCompleteType(Loc, T, Diagnoser);
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004483}
4484
Joao Matos6666ed42012-08-31 18:45:21 +00004485/// \brief Get diagnostic %select index for tag kind for
4486/// literal type diagnostic message.
4487/// WARNING: Indexes apply to particular diagnostics only!
4488///
4489/// \returns diagnostic %select index.
Joao Matosf143ae92012-09-01 00:13:24 +00004490static unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) {
Joao Matos6666ed42012-08-31 18:45:21 +00004491 switch (Tag) {
Joao Matosf143ae92012-09-01 00:13:24 +00004492 case TTK_Struct: return 0;
4493 case TTK_Interface: return 1;
4494 case TTK_Class: return 2;
4495 default: llvm_unreachable("Invalid tag kind for literal type diagnostic!");
Joao Matos6666ed42012-08-31 18:45:21 +00004496 }
Joao Matos6666ed42012-08-31 18:45:21 +00004497}
4498
Richard Smith9f569cc2011-10-01 02:31:28 +00004499/// @brief Ensure that the type T is a literal type.
4500///
4501/// This routine checks whether the type @p T is a literal type. If @p T is an
4502/// incomplete type, an attempt is made to complete it. If @p T is a literal
4503/// type, or @p AllowIncompleteType is true and @p T is an incomplete type,
4504/// returns false. Otherwise, this routine issues the diagnostic @p PD (giving
4505/// it the type @p T), along with notes explaining why the type is not a
4506/// literal type, and returns true.
4507///
4508/// @param Loc The location in the source that the non-literal type
4509/// diagnostic should refer to.
4510///
4511/// @param T The type that this routine is examining for literalness.
4512///
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004513/// @param Diagnoser Emits a diagnostic if T is not a literal type.
Richard Smith9f569cc2011-10-01 02:31:28 +00004514///
Richard Smith9f569cc2011-10-01 02:31:28 +00004515/// @returns @c true if @p T is not a literal type and a diagnostic was emitted,
4516/// @c false otherwise.
4517bool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004518 TypeDiagnoser &Diagnoser) {
Richard Smith9f569cc2011-10-01 02:31:28 +00004519 assert(!T->isDependentType() && "type should not be dependent");
4520
Eli Friedmanee065392012-02-20 23:58:14 +00004521 QualType ElemType = Context.getBaseElementType(T);
4522 RequireCompleteType(Loc, ElemType, 0);
4523
Richard Smith86c3ae42012-02-13 03:54:03 +00004524 if (T->isLiteralType())
Richard Smith9f569cc2011-10-01 02:31:28 +00004525 return false;
4526
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004527 if (Diagnoser.Suppressed)
Richard Smith9f569cc2011-10-01 02:31:28 +00004528 return true;
4529
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004530 Diagnoser.diagnose(*this, Loc, T);
Richard Smith9f569cc2011-10-01 02:31:28 +00004531
4532 if (T->isVariableArrayType())
4533 return true;
4534
Eli Friedmanee065392012-02-20 23:58:14 +00004535 const RecordType *RT = ElemType->getAs<RecordType>();
Richard Smith9f569cc2011-10-01 02:31:28 +00004536 if (!RT)
4537 return true;
4538
4539 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
4540
Richard Smithc799a6a2012-04-25 23:23:48 +00004541 // A partially-defined class type can't be a literal type, because a literal
4542 // class type must have a trivial destructor (which can't be checked until
4543 // the class definition is complete).
4544 if (!RD->isCompleteDefinition()) {
Douglas Gregord10099e2012-05-04 16:32:21 +00004545 RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T);
Eli Friedmanee065392012-02-20 23:58:14 +00004546 return true;
Richard Smithc799a6a2012-04-25 23:23:48 +00004547 }
Eli Friedmanee065392012-02-20 23:58:14 +00004548
Richard Smith9f569cc2011-10-01 02:31:28 +00004549 // If the class has virtual base classes, then it's not an aggregate, and
Richard Smith86c3ae42012-02-13 03:54:03 +00004550 // cannot have any constexpr constructors or a trivial default constructor,
4551 // so is non-literal. This is better to diagnose than the resulting absence
4552 // of constexpr constructors.
Richard Smith9f569cc2011-10-01 02:31:28 +00004553 if (RD->getNumVBases()) {
4554 Diag(RD->getLocation(), diag::note_non_literal_virtual_base)
Joao Matos6666ed42012-08-31 18:45:21 +00004555 << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases();
Richard Smith9f569cc2011-10-01 02:31:28 +00004556 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
4557 E = RD->vbases_end(); I != E; ++I)
Daniel Dunbar96a00142012-03-09 18:35:03 +00004558 Diag(I->getLocStart(),
Richard Smith9f569cc2011-10-01 02:31:28 +00004559 diag::note_constexpr_virtual_base_here) << I->getSourceRange();
Richard Smith86c3ae42012-02-13 03:54:03 +00004560 } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() &&
4561 !RD->hasTrivialDefaultConstructor()) {
Richard Smith9f569cc2011-10-01 02:31:28 +00004562 Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD;
Richard Smith9f569cc2011-10-01 02:31:28 +00004563 } else if (RD->hasNonLiteralTypeFieldsOrBases()) {
4564 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
4565 E = RD->bases_end(); I != E; ++I) {
4566 if (!I->getType()->isLiteralType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004567 Diag(I->getLocStart(),
Richard Smith9f569cc2011-10-01 02:31:28 +00004568 diag::note_non_literal_base_class)
4569 << RD << I->getType() << I->getSourceRange();
4570 return true;
4571 }
4572 }
4573 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
4574 E = RD->field_end(); I != E; ++I) {
David Blaikie262bc182012-04-30 02:36:29 +00004575 if (!I->getType()->isLiteralType() ||
4576 I->getType().isVolatileQualified()) {
4577 Diag(I->getLocation(), diag::note_non_literal_field)
David Blaikie581deb32012-06-06 20:45:41 +00004578 << RD << *I << I->getType()
David Blaikie262bc182012-04-30 02:36:29 +00004579 << I->getType().isVolatileQualified();
Richard Smith9f569cc2011-10-01 02:31:28 +00004580 return true;
4581 }
4582 }
4583 } else if (!RD->hasTrivialDestructor()) {
4584 // All fields and bases are of literal types, so have trivial destructors.
4585 // If this class's destructor is non-trivial it must be user-declared.
4586 CXXDestructorDecl *Dtor = RD->getDestructor();
4587 assert(Dtor && "class has literal fields and bases but no dtor?");
4588 if (!Dtor)
4589 return true;
4590
4591 Diag(Dtor->getLocation(), Dtor->isUserProvided() ?
4592 diag::note_non_literal_user_provided_dtor :
4593 diag::note_non_literal_nontrivial_dtor) << RD;
Richard Smithac713512012-12-08 02:53:02 +00004594 if (!Dtor->isUserProvided())
4595 SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true);
Richard Smith9f569cc2011-10-01 02:31:28 +00004596 }
4597
4598 return true;
4599}
4600
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004601bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) {
Douglas Gregorf502d8e2012-05-04 16:48:41 +00004602 TypeDiagnoserDiag Diagnoser(DiagID);
4603 return RequireLiteralType(Loc, T, Diagnoser);
4604}
4605
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004606/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
4607/// and qualified by the nested-name-specifier contained in SS.
4608QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
4609 const CXXScopeSpec &SS, QualType T) {
4610 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00004611 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004612 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00004613 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004614 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4615 else {
4616 if (Keyword == ETK_None)
4617 return T;
4618 NNS = 0;
4619 }
4620 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00004621}
Anders Carlssonaf017e62009-06-29 22:58:55 +00004622
John McCall2a984ca2010-10-12 00:20:44 +00004623QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
John McCallfb8721c2011-04-10 19:13:55 +00004624 ExprResult ER = CheckPlaceholderExpr(E);
John McCall2a984ca2010-10-12 00:20:44 +00004625 if (ER.isInvalid()) return QualType();
4626 E = ER.take();
4627
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00004628 if (!E->isTypeDependent()) {
4629 QualType T = E->getType();
Fariborz Jahanianaca7f7b2010-10-06 00:23:25 +00004630 if (const TagType *TT = T->getAs<TagType>())
4631 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00004632 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00004633 return Context.getTypeOfExprType(E);
4634}
4635
Douglas Gregorf8af9822012-02-12 18:42:33 +00004636/// getDecltypeForExpr - Given an expr, will return the decltype for
4637/// that expression, according to the rules in C++11
4638/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
4639static QualType getDecltypeForExpr(Sema &S, Expr *E) {
4640 if (E->isTypeDependent())
4641 return S.Context.DependentTy;
4642
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004643 // C++11 [dcl.type.simple]p4:
4644 // The type denoted by decltype(e) is defined as follows:
4645 //
4646 // - if e is an unparenthesized id-expression or an unparenthesized class
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004647 // member access (5.2.5), decltype(e) is the type of the entity named
4648 // by e. If there is no such entity, or if e names a set of overloaded
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004649 // functions, the program is ill-formed;
Douglas Gregor84dd82e2012-09-13 23:40:46 +00004650 //
4651 // We apply the same rules for Objective-C ivar and property references.
Douglas Gregorf8af9822012-02-12 18:42:33 +00004652 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
4653 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
4654 return VD->getType();
Douglas Gregor84dd82e2012-09-13 23:40:46 +00004655 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Douglas Gregorf8af9822012-02-12 18:42:33 +00004656 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
4657 return FD->getType();
Douglas Gregor84dd82e2012-09-13 23:40:46 +00004658 } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) {
4659 return IR->getDecl()->getType();
4660 } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) {
4661 if (PR->isExplicitProperty())
4662 return PR->getExplicitProperty()->getType();
Douglas Gregorf8af9822012-02-12 18:42:33 +00004663 }
Douglas Gregor84dd82e2012-09-13 23:40:46 +00004664
Douglas Gregorf8af9822012-02-12 18:42:33 +00004665 // C++11 [expr.lambda.prim]p18:
4666 // Every occurrence of decltype((x)) where x is a possibly
4667 // parenthesized id-expression that names an entity of automatic
4668 // storage duration is treated as if x were transformed into an
4669 // access to a corresponding data member of the closure type that
4670 // would have been declared if x were an odr-use of the denoted
4671 // entity.
4672 using namespace sema;
4673 if (S.getCurLambda()) {
4674 if (isa<ParenExpr>(E)) {
4675 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
4676 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor68932842012-02-18 05:51:20 +00004677 QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation());
4678 if (!T.isNull())
4679 return S.Context.getLValueReferenceType(T);
Douglas Gregorf8af9822012-02-12 18:42:33 +00004680 }
4681 }
4682 }
4683 }
4684
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004685
4686 // C++11 [dcl.type.simple]p4:
4687 // [...]
Douglas Gregorf8af9822012-02-12 18:42:33 +00004688 QualType T = E->getType();
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004689 switch (E->getValueKind()) {
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004690 // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004691 // type of e;
4692 case VK_XValue: T = S.Context.getRValueReferenceType(T); break;
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004693 // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
Douglas Gregor6d9ef302012-02-12 18:57:57 +00004694 // type of e;
4695 case VK_LValue: T = S.Context.getLValueReferenceType(T); break;
4696 // - otherwise, decltype(e) is the type of e.
4697 case VK_RValue: break;
4698 }
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004699
Douglas Gregorf8af9822012-02-12 18:42:33 +00004700 return T;
4701}
4702
John McCall2a984ca2010-10-12 00:20:44 +00004703QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
John McCallfb8721c2011-04-10 19:13:55 +00004704 ExprResult ER = CheckPlaceholderExpr(E);
John McCall2a984ca2010-10-12 00:20:44 +00004705 if (ER.isInvalid()) return QualType();
4706 E = ER.take();
Chad Rosier91cbbbf2012-06-26 21:41:40 +00004707
Douglas Gregorf8af9822012-02-12 18:42:33 +00004708 return Context.getDecltypeType(E, getDecltypeForExpr(*this, E));
Anders Carlssonaf017e62009-06-29 22:58:55 +00004709}
Sean Huntca63c202011-05-24 22:41:36 +00004710
4711QualType Sema::BuildUnaryTransformType(QualType BaseType,
4712 UnaryTransformType::UTTKind UKind,
4713 SourceLocation Loc) {
4714 switch (UKind) {
4715 case UnaryTransformType::EnumUnderlyingType:
4716 if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) {
4717 Diag(Loc, diag::err_only_enums_have_underlying_types);
4718 return QualType();
4719 } else {
4720 QualType Underlying = BaseType;
4721 if (!BaseType->isDependentType()) {
4722 EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl();
4723 assert(ED && "EnumType has no EnumDecl");
4724 DiagnoseUseOfDecl(ED, Loc);
4725 Underlying = ED->getIntegerType();
4726 }
4727 assert(!Underlying.isNull());
4728 return Context.getUnaryTransformType(BaseType, Underlying,
4729 UnaryTransformType::EnumUnderlyingType);
4730 }
4731 }
4732 llvm_unreachable("unknown unary transform type");
4733}
Eli Friedmanb001de72011-10-06 23:00:33 +00004734
4735QualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
4736 if (!T->isDependentType()) {
Richard Smith83271182012-02-11 18:03:45 +00004737 // FIXME: It isn't entirely clear whether incomplete atomic types
4738 // are allowed or not; for simplicity, ban them for the moment.
Douglas Gregord10099e2012-05-04 16:32:21 +00004739 if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0))
Richard Smith83271182012-02-11 18:03:45 +00004740 return QualType();
4741
Eli Friedmanb001de72011-10-06 23:00:33 +00004742 int DisallowedKind = -1;
Richard Smith83271182012-02-11 18:03:45 +00004743 if (T->isArrayType())
Eli Friedmanb001de72011-10-06 23:00:33 +00004744 DisallowedKind = 1;
4745 else if (T->isFunctionType())
4746 DisallowedKind = 2;
4747 else if (T->isReferenceType())
4748 DisallowedKind = 3;
4749 else if (T->isAtomicType())
4750 DisallowedKind = 4;
4751 else if (T.hasQualifiers())
4752 DisallowedKind = 5;
4753 else if (!T.isTriviallyCopyableType(Context))
4754 // Some other non-trivially-copyable type (probably a C++ class)
4755 DisallowedKind = 6;
4756
4757 if (DisallowedKind != -1) {
4758 Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
4759 return QualType();
4760 }
4761
4762 // FIXME: Do we need any handling for ARC here?
4763 }
4764
4765 // Build the pointer type.
4766 return Context.getAtomicType(T);
4767}