blob: 9c70d250ddc352f6293dbe3e0417abcdf0367850 [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"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000018#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000020#include "clang/AST/DeclTemplate.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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/AST/Expr.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"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000028#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000029#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Douglas Gregor2dc0e642009-03-23 23:06:20 +000032/// \brief Perform adjustment on the parameter type of a function.
33///
34/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000035/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
36/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000037QualType Sema::adjustParameterType(QualType T) {
38 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000039 // A declaration of a parameter as "array of type" shall be
40 // adjusted to "qualified pointer to type", where the type
41 // qualifiers (if any) are those specified within the [ and ] of
42 // the array type derivation.
43 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000044 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000045
46 // C99 6.7.5.3p8:
47 // A declaration of a parameter as "function returning type"
48 // shall be adjusted to "pointer to function returning type", as
49 // in 6.3.2.1.
50 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000051 return Context.getPointerType(T);
52
53 return T;
54}
55
Chris Lattner5db2bb12009-10-25 18:21:37 +000056
57
58/// isOmittedBlockReturnType - Return true if this declarator is missing a
59/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000060static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000061 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000062 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000063 return false;
64
65 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000066 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000067
68 if (D.getNumTypeObjects() == 1 &&
69 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000070 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000071
72 return false;
73}
74
John McCall2792fa52011-03-08 04:17:03 +000075/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
76/// doesn't apply to the given type.
77static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
78 QualType type) {
79 bool useInstantiationLoc = false;
80
81 unsigned diagID = 0;
82 switch (attr.getKind()) {
83 case AttributeList::AT_objc_gc:
84 diagID = diag::warn_pointer_attribute_wrong_type;
85 useInstantiationLoc = true;
86 break;
87
88 default:
89 // Assume everything else was a function attribute.
90 diagID = diag::warn_function_attribute_wrong_type;
91 break;
92 }
93
94 SourceLocation loc = attr.getLoc();
95 llvm::StringRef name = attr.getName()->getName();
96
97 // The GC attributes are usually written with macros; special-case them.
98 if (useInstantiationLoc && loc.isMacroID() && attr.getParameterName()) {
John McCall834e3f62011-03-08 07:59:04 +000099 if (attr.getParameterName()->isStr("strong")) {
100 if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
101 } else if (attr.getParameterName()->isStr("weak")) {
102 if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
John McCall2792fa52011-03-08 04:17:03 +0000103 }
104 }
105
106 S.Diag(loc, diagID) << name << type;
107}
108
John McCall711c52b2011-01-05 12:14:39 +0000109// objc_gc applies to Objective-C pointers or, otherwise, to the
110// smallest available pointer type (i.e. 'void*' in 'void**').
111#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
112 case AttributeList::AT_objc_gc
John McCall04a67a62010-02-05 21:31:56 +0000113
John McCall711c52b2011-01-05 12:14:39 +0000114// Function type attributes.
115#define FUNCTION_TYPE_ATTRS_CASELIST \
116 case AttributeList::AT_noreturn: \
117 case AttributeList::AT_cdecl: \
118 case AttributeList::AT_fastcall: \
119 case AttributeList::AT_stdcall: \
120 case AttributeList::AT_thiscall: \
121 case AttributeList::AT_pascal: \
122 case AttributeList::AT_regparm
John McCall04a67a62010-02-05 21:31:56 +0000123
John McCall711c52b2011-01-05 12:14:39 +0000124namespace {
125 /// An object which stores processing state for the entire
126 /// GetTypeForDeclarator process.
127 class TypeProcessingState {
128 Sema &sema;
129
130 /// The declarator being processed.
131 Declarator &declarator;
132
133 /// The index of the declarator chunk we're currently processing.
134 /// May be the total number of valid chunks, indicating the
135 /// DeclSpec.
136 unsigned chunkIndex;
137
138 /// Whether there are non-trivial modifications to the decl spec.
139 bool trivial;
140
John McCall7ea21932011-03-26 01:39:56 +0000141 /// Whether we saved the attributes in the decl spec.
142 bool hasSavedAttrs;
143
John McCall711c52b2011-01-05 12:14:39 +0000144 /// The original set of attributes on the DeclSpec.
145 llvm::SmallVector<AttributeList*, 2> savedAttrs;
146
147 /// A list of attributes to diagnose the uselessness of when the
148 /// processing is complete.
149 llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
150
151 public:
152 TypeProcessingState(Sema &sema, Declarator &declarator)
153 : sema(sema), declarator(declarator),
154 chunkIndex(declarator.getNumTypeObjects()),
John McCall7ea21932011-03-26 01:39:56 +0000155 trivial(true), hasSavedAttrs(false) {}
John McCall711c52b2011-01-05 12:14:39 +0000156
157 Sema &getSema() const {
158 return sema;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000159 }
John McCall711c52b2011-01-05 12:14:39 +0000160
161 Declarator &getDeclarator() const {
162 return declarator;
163 }
164
165 unsigned getCurrentChunkIndex() const {
166 return chunkIndex;
167 }
168
169 void setCurrentChunkIndex(unsigned idx) {
170 assert(idx <= declarator.getNumTypeObjects());
171 chunkIndex = idx;
172 }
173
174 AttributeList *&getCurrentAttrListRef() const {
175 assert(chunkIndex <= declarator.getNumTypeObjects());
176 if (chunkIndex == declarator.getNumTypeObjects())
177 return getMutableDeclSpec().getAttributes().getListRef();
178 return declarator.getTypeObject(chunkIndex).getAttrListRef();
179 }
180
181 /// Save the current set of attributes on the DeclSpec.
182 void saveDeclSpecAttrs() {
183 // Don't try to save them multiple times.
John McCall7ea21932011-03-26 01:39:56 +0000184 if (hasSavedAttrs) return;
John McCall711c52b2011-01-05 12:14:39 +0000185
186 DeclSpec &spec = getMutableDeclSpec();
187 for (AttributeList *attr = spec.getAttributes().getList(); attr;
188 attr = attr->getNext())
189 savedAttrs.push_back(attr);
190 trivial &= savedAttrs.empty();
John McCall7ea21932011-03-26 01:39:56 +0000191 hasSavedAttrs = true;
John McCall711c52b2011-01-05 12:14:39 +0000192 }
193
194 /// Record that we had nowhere to put the given type attribute.
195 /// We will diagnose such attributes later.
196 void addIgnoredTypeAttr(AttributeList &attr) {
197 ignoredTypeAttrs.push_back(&attr);
198 }
199
200 /// Diagnose all the ignored type attributes, given that the
201 /// declarator worked out to the given type.
202 void diagnoseIgnoredTypeAttrs(QualType type) const {
203 for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
204 i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
John McCall2792fa52011-03-08 04:17:03 +0000205 i != e; ++i)
206 diagnoseBadTypeAttribute(getSema(), **i, type);
John McCall711c52b2011-01-05 12:14:39 +0000207 }
208
209 ~TypeProcessingState() {
210 if (trivial) return;
211
212 restoreDeclSpecAttrs();
213 }
214
215 private:
216 DeclSpec &getMutableDeclSpec() const {
217 return const_cast<DeclSpec&>(declarator.getDeclSpec());
218 }
219
220 void restoreDeclSpecAttrs() {
John McCall7ea21932011-03-26 01:39:56 +0000221 assert(hasSavedAttrs);
222
223 if (savedAttrs.empty()) {
224 getMutableDeclSpec().getAttributes().set(0);
225 return;
226 }
227
John McCall711c52b2011-01-05 12:14:39 +0000228 getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
229 for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
230 savedAttrs[i]->setNext(savedAttrs[i+1]);
231 savedAttrs.back()->setNext(0);
232 }
233 };
234
235 /// Basically std::pair except that we really want to avoid an
236 /// implicit operator= for safety concerns. It's also a minor
237 /// link-time optimization for this to be a private type.
238 struct AttrAndList {
239 /// The attribute.
240 AttributeList &first;
241
242 /// The head of the list the attribute is currently in.
243 AttributeList *&second;
244
245 AttrAndList(AttributeList &attr, AttributeList *&head)
246 : first(attr), second(head) {}
247 };
John McCall04a67a62010-02-05 21:31:56 +0000248}
249
John McCall711c52b2011-01-05 12:14:39 +0000250namespace llvm {
251 template <> struct isPodLike<AttrAndList> {
252 static const bool value = true;
253 };
254}
255
256static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
257 attr.setNext(head);
258 head = &attr;
259}
260
261static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
262 if (head == &attr) {
263 head = attr.getNext();
264 return;
John McCall04a67a62010-02-05 21:31:56 +0000265 }
John McCall711c52b2011-01-05 12:14:39 +0000266
267 AttributeList *cur = head;
268 while (true) {
269 assert(cur && cur->getNext() && "ran out of attrs?");
270 if (cur->getNext() == &attr) {
271 cur->setNext(attr.getNext());
272 return;
273 }
274 cur = cur->getNext();
275 }
276}
277
278static void moveAttrFromListToList(AttributeList &attr,
279 AttributeList *&fromList,
280 AttributeList *&toList) {
281 spliceAttrOutOfList(attr, fromList);
282 spliceAttrIntoList(attr, toList);
283}
284
285static void processTypeAttrs(TypeProcessingState &state,
286 QualType &type, bool isDeclSpec,
287 AttributeList *attrs);
288
289static bool handleFunctionTypeAttr(TypeProcessingState &state,
290 AttributeList &attr,
291 QualType &type);
292
293static bool handleObjCGCTypeAttr(TypeProcessingState &state,
294 AttributeList &attr, QualType &type);
295
296static bool handleObjCPointerTypeAttr(TypeProcessingState &state,
297 AttributeList &attr, QualType &type) {
298 // Right now, we have exactly one of these attributes: objc_gc.
299 assert(attr.getKind() == AttributeList::AT_objc_gc);
300 return handleObjCGCTypeAttr(state, attr, type);
301}
302
303/// Given that an objc_gc attribute was written somewhere on a
304/// declaration *other* than on the declarator itself (for which, use
305/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
306/// didn't apply in whatever position it was written in, try to move
307/// it to a more appropriate position.
308static void distributeObjCPointerTypeAttr(TypeProcessingState &state,
309 AttributeList &attr,
310 QualType type) {
311 Declarator &declarator = state.getDeclarator();
312 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
313 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
314 switch (chunk.Kind) {
315 case DeclaratorChunk::Pointer:
316 case DeclaratorChunk::BlockPointer:
317 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
318 chunk.getAttrListRef());
319 return;
320
321 case DeclaratorChunk::Paren:
322 case DeclaratorChunk::Array:
323 continue;
324
325 // Don't walk through these.
326 case DeclaratorChunk::Reference:
327 case DeclaratorChunk::Function:
328 case DeclaratorChunk::MemberPointer:
329 goto error;
330 }
331 }
332 error:
John McCall2792fa52011-03-08 04:17:03 +0000333
334 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000335}
336
337/// Distribute an objc_gc type attribute that was written on the
338/// declarator.
339static void
340distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
341 AttributeList &attr,
342 QualType &declSpecType) {
343 Declarator &declarator = state.getDeclarator();
344
345 // objc_gc goes on the innermost pointer to something that's not a
346 // pointer.
347 unsigned innermost = -1U;
348 bool considerDeclSpec = true;
349 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
350 DeclaratorChunk &chunk = declarator.getTypeObject(i);
351 switch (chunk.Kind) {
352 case DeclaratorChunk::Pointer:
353 case DeclaratorChunk::BlockPointer:
354 innermost = i;
John McCallae278a32011-01-12 00:34:59 +0000355 continue;
John McCall711c52b2011-01-05 12:14:39 +0000356
357 case DeclaratorChunk::Reference:
358 case DeclaratorChunk::MemberPointer:
359 case DeclaratorChunk::Paren:
360 case DeclaratorChunk::Array:
361 continue;
362
363 case DeclaratorChunk::Function:
364 considerDeclSpec = false;
365 goto done;
366 }
367 }
368 done:
369
370 // That might actually be the decl spec if we weren't blocked by
371 // anything in the declarator.
372 if (considerDeclSpec) {
John McCall7ea21932011-03-26 01:39:56 +0000373 if (handleObjCPointerTypeAttr(state, attr, declSpecType)) {
374 // Splice the attribute into the decl spec. Prevents the
375 // attribute from being applied multiple times and gives
376 // the source-location-filler something to work with.
377 state.saveDeclSpecAttrs();
378 moveAttrFromListToList(attr, declarator.getAttrListRef(),
379 declarator.getMutableDeclSpec().getAttributes().getListRef());
John McCall711c52b2011-01-05 12:14:39 +0000380 return;
John McCall7ea21932011-03-26 01:39:56 +0000381 }
John McCall711c52b2011-01-05 12:14:39 +0000382 }
383
384 // Otherwise, if we found an appropriate chunk, splice the attribute
385 // into it.
386 if (innermost != -1U) {
387 moveAttrFromListToList(attr, declarator.getAttrListRef(),
388 declarator.getTypeObject(innermost).getAttrListRef());
389 return;
390 }
391
392 // Otherwise, diagnose when we're done building the type.
393 spliceAttrOutOfList(attr, declarator.getAttrListRef());
394 state.addIgnoredTypeAttr(attr);
395}
396
397/// A function type attribute was written somewhere in a declaration
398/// *other* than on the declarator itself or in the decl spec. Given
399/// that it didn't apply in whatever position it was written in, try
400/// to move it to a more appropriate position.
401static void distributeFunctionTypeAttr(TypeProcessingState &state,
402 AttributeList &attr,
403 QualType type) {
404 Declarator &declarator = state.getDeclarator();
405
406 // Try to push the attribute from the return type of a function to
407 // the function itself.
408 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
409 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
410 switch (chunk.Kind) {
411 case DeclaratorChunk::Function:
412 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
413 chunk.getAttrListRef());
414 return;
415
416 case DeclaratorChunk::Paren:
417 case DeclaratorChunk::Pointer:
418 case DeclaratorChunk::BlockPointer:
419 case DeclaratorChunk::Array:
420 case DeclaratorChunk::Reference:
421 case DeclaratorChunk::MemberPointer:
422 continue;
423 }
424 }
425
John McCall2792fa52011-03-08 04:17:03 +0000426 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000427}
428
429/// Try to distribute a function type attribute to the innermost
430/// function chunk or type. Returns true if the attribute was
431/// distributed, false if no location was found.
432static bool
433distributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
434 AttributeList &attr,
435 AttributeList *&attrList,
436 QualType &declSpecType) {
437 Declarator &declarator = state.getDeclarator();
438
439 // Put it on the innermost function chunk, if there is one.
440 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
441 DeclaratorChunk &chunk = declarator.getTypeObject(i);
442 if (chunk.Kind != DeclaratorChunk::Function) continue;
443
444 moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
445 return true;
446 }
447
448 return handleFunctionTypeAttr(state, attr, declSpecType);
449}
450
451/// A function type attribute was written in the decl spec. Try to
452/// apply it somewhere.
453static void
454distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
455 AttributeList &attr,
456 QualType &declSpecType) {
457 state.saveDeclSpecAttrs();
458
459 // Try to distribute to the innermost.
460 if (distributeFunctionTypeAttrToInnermost(state, attr,
461 state.getCurrentAttrListRef(),
462 declSpecType))
463 return;
464
465 // If that failed, diagnose the bad attribute when the declarator is
466 // fully built.
467 state.addIgnoredTypeAttr(attr);
468}
469
470/// A function type attribute was written on the declarator. Try to
471/// apply it somewhere.
472static void
473distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
474 AttributeList &attr,
475 QualType &declSpecType) {
476 Declarator &declarator = state.getDeclarator();
477
478 // Try to distribute to the innermost.
479 if (distributeFunctionTypeAttrToInnermost(state, attr,
480 declarator.getAttrListRef(),
481 declSpecType))
482 return;
483
484 // If that failed, diagnose the bad attribute when the declarator is
485 // fully built.
486 spliceAttrOutOfList(attr, declarator.getAttrListRef());
487 state.addIgnoredTypeAttr(attr);
488}
489
490/// \brief Given that there are attributes written on the declarator
491/// itself, try to distribute any type attributes to the appropriate
492/// declarator chunk.
493///
494/// These are attributes like the following:
495/// int f ATTR;
496/// int (f ATTR)();
497/// but not necessarily this:
498/// int f() ATTR;
499static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
500 QualType &declSpecType) {
501 // Collect all the type attributes from the declarator itself.
502 assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
503 AttributeList *attr = state.getDeclarator().getAttributes();
504 AttributeList *next;
505 do {
506 next = attr->getNext();
507
508 switch (attr->getKind()) {
509 OBJC_POINTER_TYPE_ATTRS_CASELIST:
510 distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
511 break;
512
513 FUNCTION_TYPE_ATTRS_CASELIST:
514 distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
515 break;
516
517 default:
518 break;
519 }
520 } while ((attr = next));
521}
522
523/// Add a synthetic '()' to a block-literal declarator if it is
524/// required, given the return type.
525static void maybeSynthesizeBlockSignature(TypeProcessingState &state,
526 QualType declSpecType) {
527 Declarator &declarator = state.getDeclarator();
528
529 // First, check whether the declarator would produce a function,
530 // i.e. whether the innermost semantic chunk is a function.
531 if (declarator.isFunctionDeclarator()) {
532 // If so, make that declarator a prototyped declarator.
533 declarator.getFunctionTypeInfo().hasPrototype = true;
534 return;
535 }
536
John McCallda263792011-02-08 01:59:10 +0000537 // If there are any type objects, the type as written won't name a
538 // function, regardless of the decl spec type. This is because a
539 // block signature declarator is always an abstract-declarator, and
540 // abstract-declarators can't just be parentheses chunks. Therefore
541 // we need to build a function chunk unless there are no type
542 // objects and the decl spec type is a function.
John McCall711c52b2011-01-05 12:14:39 +0000543 if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
544 return;
545
John McCallda263792011-02-08 01:59:10 +0000546 // Note that there *are* cases with invalid declarators where
547 // declarators consist solely of parentheses. In general, these
548 // occur only in failed efforts to make function declarators, so
549 // faking up the function chunk is still the right thing to do.
John McCall711c52b2011-01-05 12:14:39 +0000550
551 // Otherwise, we need to fake up a function declarator.
552 SourceLocation loc = declarator.getSourceRange().getBegin();
553
554 // ...and *prepend* it to the declarator.
555 declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
John McCall711c52b2011-01-05 12:14:39 +0000556 /*proto*/ true,
557 /*variadic*/ false, SourceLocation(),
558 /*args*/ 0, 0,
559 /*type quals*/ 0,
Douglas Gregor83f51722011-01-26 03:43:54 +0000560 /*ref-qualifier*/true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000561 /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0,
John McCall711c52b2011-01-05 12:14:39 +0000562 /*parens*/ loc, loc,
563 declarator));
564
565 // For consistency, make sure the state still has us as processing
566 // the decl spec.
567 assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
568 state.setCurrentChunkIndex(declarator.getNumTypeObjects());
John McCall04a67a62010-02-05 21:31:56 +0000569}
570
Douglas Gregor930d8b52009-01-30 22:09:00 +0000571/// \brief Convert the specified declspec to the appropriate type
572/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000573/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000574/// \returns The type described by the declaration specifiers. This function
575/// never returns null.
John McCall711c52b2011-01-05 12:14:39 +0000576static QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
578 // checking.
John McCall711c52b2011-01-05 12:14:39 +0000579
580 Declarator &declarator = state.getDeclarator();
581 const DeclSpec &DS = declarator.getDeclSpec();
582 SourceLocation DeclLoc = declarator.getIdentifierLoc();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000583 if (DeclLoc.isInvalid())
584 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000585
John McCall711c52b2011-01-05 12:14:39 +0000586 ASTContext &Context = S.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner5db2bb12009-10-25 18:21:37 +0000588 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000590 case DeclSpec::TST_void:
591 Result = Context.VoidTy;
592 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 case DeclSpec::TST_char:
594 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000595 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000597 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 else {
599 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
600 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000601 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 }
Chris Lattner958858e2008-02-20 21:40:32 +0000603 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000604 case DeclSpec::TST_wchar:
605 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
606 Result = Context.WCharTy;
607 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
John McCall711c52b2011-01-05 12:14:39 +0000608 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000609 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000610 Result = Context.getSignedWCharType();
611 } else {
612 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
613 "Unknown TSS value");
John McCall711c52b2011-01-05 12:14:39 +0000614 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000615 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000616 Result = Context.getUnsignedWCharType();
617 }
618 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000619 case DeclSpec::TST_char16:
620 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
621 "Unknown TSS value");
622 Result = Context.Char16Ty;
623 break;
624 case DeclSpec::TST_char32:
625 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
626 "Unknown TSS value");
627 Result = Context.Char32Ty;
628 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000629 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000630 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000631 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000632 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
633 (ObjCProtocolDecl**)PQ,
634 DS.getNumProtocolQualifiers());
635 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000636 break;
637 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000638
639 // If this is a missing declspec in a block literal return context, then it
640 // is inferred from the return statements inside the block.
John McCall711c52b2011-01-05 12:14:39 +0000641 if (isOmittedBlockReturnType(declarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000642 Result = Context.DependentTy;
643 break;
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattnerd658b562008-04-05 06:32:51 +0000646 // Unspecified typespec defaults to int in C90. However, the C90 grammar
647 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
648 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
649 // Note that the one exception to this is function definitions, which are
650 // allowed to be completely missing a declspec. This is handled in the
651 // parser already though by it pretending to have seen an 'int' in this
652 // case.
John McCall711c52b2011-01-05 12:14:39 +0000653 if (S.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000654 // In C89 mode, we only warn if there is a completely missing declspec
655 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000656 if (DS.isEmpty()) {
John McCall711c52b2011-01-05 12:14:39 +0000657 S.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000658 << DS.getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000659 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000660 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000661 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000662 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
663 // "At least one type specifier shall be given in the declaration
664 // specifiers in each declaration, and in the specifier-qualifier list in
665 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000666 // FIXME: Does Microsoft really have the implicit int extension in C++?
John McCall711c52b2011-01-05 12:14:39 +0000667 if (S.getLangOptions().CPlusPlus &&
668 !S.getLangOptions().Microsoft) {
669 S.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000670 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattnerb78d8332009-06-26 04:45:06 +0000672 // When this occurs in C++ code, often something is very broken with the
673 // value being declared, poison it as invalid so we don't get chains of
674 // errors.
John McCall711c52b2011-01-05 12:14:39 +0000675 declarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000676 } else {
John McCall711c52b2011-01-05 12:14:39 +0000677 S.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000678 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000679 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000680 }
Mike Stump1eb44332009-09-09 15:08:12 +0000681
682 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000683 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
685 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000686 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
687 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
688 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000689 case DeclSpec::TSW_longlong:
690 Result = Context.LongLongTy;
691
692 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000693 if (!S.getLangOptions().C99 &&
694 !S.getLangOptions().CPlusPlus0x)
695 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000696 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 }
698 } else {
699 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000700 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
701 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
702 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000703 case DeclSpec::TSW_longlong:
704 Result = Context.UnsignedLongLongTy;
705
706 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000707 if (!S.getLangOptions().C99 &&
708 !S.getLangOptions().CPlusPlus0x)
709 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000710 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 }
712 }
Chris Lattner958858e2008-02-20 21:40:32 +0000713 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000714 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000715 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000716 case DeclSpec::TST_double:
717 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000718 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000719 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000720 Result = Context.DoubleTy;
Peter Collingbourne39d3e7a2011-02-15 19:46:23 +0000721
722 if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
723 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
724 declarator.setInvalidType(true);
725 }
Chris Lattner958858e2008-02-20 21:40:32 +0000726 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000727 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 case DeclSpec::TST_decimal32: // _Decimal32
729 case DeclSpec::TST_decimal64: // _Decimal64
730 case DeclSpec::TST_decimal128: // _Decimal128
John McCall711c52b2011-01-05 12:14:39 +0000731 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000732 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000733 declarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000734 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000735 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 case DeclSpec::TST_enum:
737 case DeclSpec::TST_union:
738 case DeclSpec::TST_struct: {
John McCallb3d87482010-08-24 05:47:05 +0000739 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000740 if (!D) {
741 // This can happen in C++ with ambiguous lookups.
742 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000743 declarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000744 break;
745 }
746
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000747 // If the type is deprecated or unavailable, diagnose it.
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000748 S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000751 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000754 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000755
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000756 // In both C and C++, make an ElaboratedType.
757 ElaboratedTypeKeyword Keyword
758 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
759 Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
760
Chris Lattner5153ee62009-04-25 08:47:54 +0000761 if (D->isInvalidDecl())
John McCall711c52b2011-01-05 12:14:39 +0000762 declarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000763 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000764 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000765 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
767 DS.getTypeSpecSign() == 0 &&
768 "Can't handle qualifiers on typedef names yet!");
John McCall711c52b2011-01-05 12:14:39 +0000769 Result = S.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000770 if (Result.isNull())
John McCall711c52b2011-01-05 12:14:39 +0000771 declarator.setInvalidType(true);
John McCall27940d22010-07-30 05:17:22 +0000772 else if (DeclSpec::ProtocolQualifierListTy PQ
773 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000774 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
775 // Silently drop any existing protocol qualifiers.
776 // TODO: determine whether that's the right thing to do.
777 if (ObjT->getNumProtocols())
778 Result = ObjT->getBaseType();
779
780 if (DS.getNumProtocolQualifiers())
781 Result = Context.getObjCObjectType(Result,
782 (ObjCProtocolDecl**) PQ,
783 DS.getNumProtocolQualifiers());
784 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000785 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000786 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
787 (ObjCProtocolDecl**) PQ,
788 DS.getNumProtocolQualifiers());
789 Result = Context.getObjCObjectPointerType(Result);
790 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000791 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000792 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
793 (ObjCProtocolDecl**) PQ,
794 DS.getNumProtocolQualifiers());
795 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000796 } else {
John McCall711c52b2011-01-05 12:14:39 +0000797 S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000798 << DS.getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +0000799 declarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000800 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000804 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 }
Chris Lattner958858e2008-02-20 21:40:32 +0000806 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000807 // FIXME: Preserve type source info.
John McCall711c52b2011-01-05 12:14:39 +0000808 Result = S.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000809 assert(!Result.isNull() && "Didn't get a type for typeof?");
Fariborz Jahanian730e1752010-10-06 17:00:02 +0000810 if (!Result->isDependentType())
811 if (const TagType *TT = Result->getAs<TagType>())
John McCall711c52b2011-01-05 12:14:39 +0000812 S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
Steve Naroffd1861fd2007-07-31 12:34:36 +0000813 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000814 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000815 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000816 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000817 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000818 assert(E && "Didn't get an expression for typeof?");
819 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000820 Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
Douglas Gregor4b52e252009-12-21 23:17:24 +0000821 if (Result.isNull()) {
822 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000823 declarator.setInvalidType(true);
Douglas Gregor4b52e252009-12-21 23:17:24 +0000824 }
Chris Lattner958858e2008-02-20 21:40:32 +0000825 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000826 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000827 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000828 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000829 assert(E && "Didn't get an expression for decltype?");
830 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000831 Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
Anders Carlssonaf017e62009-06-29 22:58:55 +0000832 if (Result.isNull()) {
833 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000834 declarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000835 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000836 break;
837 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000838 case DeclSpec::TST_auto: {
839 // TypeQuals handled by caller.
Richard Smith34b41d92011-02-20 03:19:35 +0000840 Result = Context.getAutoType(QualType());
Anders Carlssone89d1592009-06-26 18:41:36 +0000841 break;
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
John McCalla5fc4722011-04-09 22:50:59 +0000844 case DeclSpec::TST_unknown_anytype:
845 Result = Context.UnknownAnyTy;
846 break;
847
Douglas Gregor809070a2009-02-18 17:45:20 +0000848 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000849 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000850 declarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000851 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner958858e2008-02-20 21:40:32 +0000854 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000855 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
John McCall711c52b2011-01-05 12:14:39 +0000856 if (S.getLangOptions().Freestanding)
857 S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000858 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000859 } else if (DS.isTypeAltiVecVector()) {
860 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
861 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Bob Wilsone86d78c2010-11-10 21:56:12 +0000862 VectorType::VectorKind VecKind = VectorType::AltiVecVector;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000863 if (DS.isTypeAltiVecPixel())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000864 VecKind = VectorType::AltiVecPixel;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000865 else if (DS.isTypeAltiVecBool())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000866 VecKind = VectorType::AltiVecBool;
867 Result = Context.getVectorType(Result, 128/typeSize, VecKind);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Argyrios Kyrtzidis47423bd2010-09-23 09:40:31 +0000870 // FIXME: Imaginary.
871 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
John McCall711c52b2011-01-05 12:14:39 +0000872 S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
Mike Stump1eb44332009-09-09 15:08:12 +0000873
John McCall711c52b2011-01-05 12:14:39 +0000874 // Before we process any type attributes, synthesize a block literal
875 // function declarator if necessary.
876 if (declarator.getContext() == Declarator::BlockLiteralContext)
877 maybeSynthesizeBlockSignature(state, Result);
878
879 // Apply any type attributes from the decl spec. This may cause the
880 // list of type attributes to be temporarily saved while the type
881 // attributes are pushed around.
882 if (AttributeList *attrs = DS.getAttributes().getList())
883 processTypeAttrs(state, Result, true, attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner96b77fc2008-04-02 06:50:17 +0000885 // Apply const/volatile/restrict qualifiers to T.
886 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
887
888 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
889 // or incomplete types shall not be restrict-qualified." C++ also allows
890 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000891 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000892 if (Result->isAnyPointerType() || Result->isReferenceType()) {
893 QualType EltTy;
894 if (Result->isObjCObjectPointerType())
895 EltTy = Result;
896 else
897 EltTy = Result->isPointerType() ?
898 Result->getAs<PointerType>()->getPointeeType() :
899 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregorbad0e652009-03-24 20:32:41 +0000901 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000902 // incomplete type.
903 if (!EltTy->isIncompleteOrObjectType()) {
John McCall711c52b2011-01-05 12:14:39 +0000904 S.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000905 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000906 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000907 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000908 }
909 } else {
John McCall711c52b2011-01-05 12:14:39 +0000910 S.Diag(DS.getRestrictSpecLoc(),
911 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000912 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000913 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000914 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner96b77fc2008-04-02 06:50:17 +0000917 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
918 // of a function type includes any type qualifiers, the behavior is
919 // undefined."
920 if (Result->isFunctionType() && TypeQuals) {
921 // Get some location to point at, either the C or V location.
922 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000923 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000924 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000925 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000926 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000927 else {
928 assert((TypeQuals & DeclSpec::TQ_restrict) &&
929 "Has CVR quals but not C, V, or R?");
930 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000931 }
John McCall711c52b2011-01-05 12:14:39 +0000932 S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000933 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000936 // C++ [dcl.ref]p1:
937 // Cv-qualified references are ill-formed except when the
938 // cv-qualifiers are introduced through the use of a typedef
939 // (7.1.3) or of a template type argument (14.3), in which
940 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000941 // FIXME: Shouldn't we be checking SCS_typedef here?
942 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000943 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000944 TypeQuals &= ~DeclSpec::TQ_const;
945 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000946 }
947
John McCall0953e762009-09-24 19:53:00 +0000948 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
949 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000950 }
John McCall0953e762009-09-24 19:53:00 +0000951
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000952 return Result;
953}
954
Douglas Gregorcd281c32009-02-28 00:25:32 +0000955static std::string getPrintableNameForEntity(DeclarationName Entity) {
956 if (Entity)
957 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregorcd281c32009-02-28 00:25:32 +0000959 return "type name";
960}
961
John McCall28654742010-06-05 06:41:15 +0000962QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
963 Qualifiers Qs) {
964 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
965 // object or incomplete types shall not be restrict-qualified."
966 if (Qs.hasRestrict()) {
967 unsigned DiagID = 0;
968 QualType ProblemTy;
969
970 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
971 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
972 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
973 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
974 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
975 }
976 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
977 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
978 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
979 ProblemTy = T->getAs<PointerType>()->getPointeeType();
980 }
981 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
982 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
983 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
984 ProblemTy = T->getAs<PointerType>()->getPointeeType();
985 }
986 } else if (!Ty->isDependentType()) {
987 // FIXME: this deserves a proper diagnostic
988 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
989 ProblemTy = T;
990 }
991
992 if (DiagID) {
993 Diag(Loc, DiagID) << ProblemTy;
994 Qs.removeRestrict();
995 }
996 }
997
998 return Context.getQualifiedType(T, Qs);
999}
1000
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001001/// \brief Build a paren type including \p T.
1002QualType Sema::BuildParenType(QualType T) {
1003 return Context.getParenType(T);
1004}
1005
Douglas Gregorcd281c32009-02-28 00:25:32 +00001006/// \brief Build a pointer type.
1007///
1008/// \param T The type to which we'll be building a pointer.
1009///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001010/// \param Loc The location of the entity whose type involves this
1011/// pointer type or, if there is no such entity, the location of the
1012/// type that will have pointer type.
1013///
1014/// \param Entity The name of the entity that involves the pointer
1015/// type, if known.
1016///
1017/// \returns A suitable pointer type, if there are no
1018/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +00001019QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001020 SourceLocation Loc, DeclarationName Entity) {
1021 if (T->isReferenceType()) {
1022 // C++ 8.3.2p4: There shall be no ... pointers to references ...
1023 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001024 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001025 return QualType();
1026 }
1027
John McCallc12c5bb2010-05-15 11:32:37 +00001028 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +00001029
Douglas Gregorcd281c32009-02-28 00:25:32 +00001030 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +00001031 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001032}
1033
1034/// \brief Build a reference type.
1035///
1036/// \param T The type to which we'll be building a reference.
1037///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001038/// \param Loc The location of the entity whose type involves this
1039/// reference type or, if there is no such entity, the location of the
1040/// type that will have reference type.
1041///
1042/// \param Entity The name of the entity that involves the reference
1043/// type, if known.
1044///
1045/// \returns A suitable reference type, if there are no
1046/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +00001047QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +00001048 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +00001049 DeclarationName Entity) {
Douglas Gregor69d83162011-01-20 16:08:06 +00001050 // C++0x [dcl.ref]p6:
1051 // If a typedef (7.1.3), a type template-parameter (14.3.1), or a
1052 // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
1053 // type T, an attempt to create the type "lvalue reference to cv TR" creates
1054 // the type "lvalue reference to T", while an attempt to create the type
1055 // "rvalue reference to cv TR" creates the type TR.
John McCall54e14c42009-10-22 22:37:11 +00001056 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
1057
John McCall54e14c42009-10-22 22:37:11 +00001058 // C++ [dcl.ref]p4: There shall be no references to references.
1059 //
1060 // According to C++ DR 106, references to references are only
1061 // diagnosed when they are written directly (e.g., "int & &"),
1062 // but not when they happen via a typedef:
1063 //
1064 // typedef int& intref;
1065 // typedef intref& intref2;
1066 //
1067 // Parser::ParseDeclaratorInternal diagnoses the case where
1068 // references are written directly; here, we handle the
Douglas Gregor69d83162011-01-20 16:08:06 +00001069 // collapsing of references-to-references as described in C++0x.
1070 // DR 106 and 540 introduce reference-collapsing into C++98/03.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001071
1072 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +00001073 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +00001074 // is ill-formed.
1075 if (T->isVoidType()) {
1076 Diag(Loc, diag::err_reference_to_void);
1077 return QualType();
1078 }
1079
Douglas Gregorcd281c32009-02-28 00:25:32 +00001080 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001081 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +00001082 return Context.getLValueReferenceType(T, SpelledAsLValue);
1083 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001084}
1085
1086/// \brief Build an array type.
1087///
1088/// \param T The type of each element in the array.
1089///
1090/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +00001091///
1092/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001093///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001094/// \param Loc The location of the entity whose type involves this
1095/// array type or, if there is no such entity, the location of the
1096/// type that will have array type.
1097///
1098/// \param Entity The name of the entity that involves the array
1099/// type, if known.
1100///
1101/// \returns A suitable array type, if there are no errors. Otherwise,
1102/// returns a NULL type.
1103QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1104 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001105 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001106
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001107 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001108 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +00001109 // C++ [dcl.array]p1:
1110 // T is called the array element type; this type shall not be a reference
1111 // type, the (possibly cv-qualified) type void, a function type or an
1112 // abstract class type.
1113 //
1114 // Note: function types are handled in the common path with C.
1115 if (T->isReferenceType()) {
1116 Diag(Loc, diag::err_illegal_decl_array_of_references)
1117 << getPrintableNameForEntity(Entity) << T;
1118 return QualType();
1119 }
1120
Sebastian Redl923d56d2009-11-05 15:52:31 +00001121 if (T->isVoidType()) {
1122 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1123 return QualType();
1124 }
Douglas Gregor138bb232010-04-27 19:38:14 +00001125
1126 if (RequireNonAbstractType(Brackets.getBegin(), T,
1127 diag::err_array_of_abstract_type))
1128 return QualType();
1129
Sebastian Redl923d56d2009-11-05 15:52:31 +00001130 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +00001131 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1132 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +00001133 if (RequireCompleteType(Loc, T,
1134 diag::err_illegal_decl_array_incomplete_type))
1135 return QualType();
1136 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001137
1138 if (T->isFunctionType()) {
1139 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +00001140 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001141 return QualType();
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Richard Smith34b41d92011-02-20 03:19:35 +00001144 if (T->getContainedAutoType()) {
1145 Diag(Loc, diag::err_illegal_decl_array_of_auto)
1146 << getPrintableNameForEntity(Entity) << T;
Anders Carlssone7cf07d2009-06-26 19:33:28 +00001147 return QualType();
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenek6217b802009-07-29 21:53:49 +00001150 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001151 // If the element type is a struct or union that contains a variadic
1152 // array, accept it as a GNU extension: C99 6.7.2.1p2.
1153 if (EltTy->getDecl()->hasFlexibleArrayMember())
1154 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +00001155 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +00001156 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1157 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
John McCall5e3c67b2010-12-15 04:42:30 +00001160 // Do lvalue-to-rvalue conversions on the array size expression.
John Wiegley429bb272011-04-08 18:41:53 +00001161 if (ArraySize && !ArraySize->isRValue()) {
1162 ExprResult Result = DefaultLvalueConversion(ArraySize);
1163 if (Result.isInvalid())
1164 return QualType();
1165
1166 ArraySize = Result.take();
1167 }
John McCall5e3c67b2010-12-15 04:42:30 +00001168
Douglas Gregorcd281c32009-02-28 00:25:32 +00001169 // C99 6.7.5.2p1: The size expression shall have integer type.
John McCall5e3c67b2010-12-15 04:42:30 +00001170 // TODO: in theory, if we were insane, we could allow contextual
1171 // conversions to integer type here.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001172 if (ArraySize && !ArraySize->isTypeDependent() &&
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001173 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001174 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1175 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001176 return QualType();
1177 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001178 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +00001179 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001180 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001181 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001182 else
1183 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001184 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001185 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001186 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +00001187 (!T->isDependentType() && !T->isIncompleteType() &&
1188 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001189 // Per C99, a variable array is an array with either a non-constant
1190 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001191 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001192 } else {
1193 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1194 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +00001195 if (ConstVal.isSigned() && ConstVal.isNegative()) {
Chandler Carruthb2b5cc02011-01-04 04:44:35 +00001196 if (Entity)
1197 Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1198 << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1199 else
1200 Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1201 << ArraySize->getSourceRange();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001202 return QualType();
1203 }
1204 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001205 // GCC accepts zero sized static arrays. We allow them when
1206 // we're not in a SFINAE context.
1207 Diag(ArraySize->getLocStart(),
1208 isSFINAEContext()? diag::err_typecheck_zero_array_size
1209 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +00001210 << ArraySize->getSourceRange();
Douglas Gregor2767ce22010-08-18 00:39:00 +00001211 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1212 !T->isIncompleteType()) {
1213 // Is the array too large?
1214 unsigned ActiveSizeBits
1215 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1216 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
1217 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
1218 << ConstVal.toString(10)
1219 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001220 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001221
John McCall46a617a2009-10-16 00:14:28 +00001222 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001223 }
David Chisnallaf407762010-01-11 23:08:08 +00001224 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1225 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001226 if (T->isVariableArrayType()) {
1227 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +00001228 if (!T->isDependentType() &&
1229 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001230 Diag(Loc, diag::err_vla_non_pod)
1231 << Context.getBaseElementType(T);
1232 return QualType();
1233 }
Douglas Gregora481ec42010-05-23 19:57:01 +00001234 // Prohibit the use of VLAs during template argument deduction.
1235 else if (isSFINAEContext()) {
1236 Diag(Loc, diag::err_vla_in_sfinae);
1237 return QualType();
1238 }
Douglas Gregor0fddb972010-05-22 16:17:30 +00001239 // Just extwarn about VLAs.
1240 else
1241 Diag(Loc, diag::ext_vla);
1242 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +00001243 Diag(Loc,
1244 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1245 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001246 }
1247
1248 return T;
1249}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001250
1251/// \brief Build an ext-vector type.
1252///
1253/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +00001254QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001255 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001256 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1257 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +00001258 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001259 !T->isIntegerType() && !T->isRealFloatingType()) {
1260 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
1261 return QualType();
1262 }
1263
John McCall9ae2f072010-08-23 23:25:46 +00001264 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001265 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +00001266 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001267 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +00001268 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001269 return QualType();
1270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
1272 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001273 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +00001274 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
1275
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001276 if (vectorSize == 0) {
1277 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +00001278 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001279 return QualType();
1280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001282 if (!T->isDependentType())
1283 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +00001284 }
1285
John McCall9ae2f072010-08-23 23:25:46 +00001286 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001287}
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Douglas Gregor724651c2009-02-28 01:04:19 +00001289/// \brief Build a function type.
1290///
1291/// This routine checks the function type according to C++ rules and
1292/// under the assumption that the result type and parameter types have
1293/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +00001294/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +00001295/// simpler form that is only suitable for this narrow use case.
1296///
1297/// \param T The return type of the function.
1298///
1299/// \param ParamTypes The parameter types of the function. This array
1300/// will be modified to account for adjustments to the types of the
1301/// function parameters.
1302///
1303/// \param NumParamTypes The number of parameter types in ParamTypes.
1304///
1305/// \param Variadic Whether this is a variadic function type.
1306///
1307/// \param Quals The cvr-qualifiers to be applied to the function type.
1308///
1309/// \param Loc The location of the entity whose type involves this
1310/// function type or, if there is no such entity, the location of the
1311/// type that will have function type.
1312///
1313/// \param Entity The name of the entity that involves the function
1314/// type, if known.
1315///
1316/// \returns A suitable function type, if there are no
1317/// errors. Otherwise, returns a NULL type.
1318QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001319 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +00001320 unsigned NumParamTypes,
1321 bool Variadic, unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00001322 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00001323 SourceLocation Loc, DeclarationName Entity,
John McCalle23cf432010-12-14 08:05:40 +00001324 FunctionType::ExtInfo Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001325 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001326 Diag(Loc, diag::err_func_returning_array_function)
1327 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +00001328 return QualType();
1329 }
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001330
Douglas Gregor724651c2009-02-28 01:04:19 +00001331 bool Invalid = false;
1332 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001333 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
1334 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001335 Diag(Loc, diag::err_param_with_void_type);
1336 Invalid = true;
1337 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001338
John McCall54e14c42009-10-22 22:37:11 +00001339 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +00001340 }
1341
1342 if (Invalid)
1343 return QualType();
1344
John McCalle23cf432010-12-14 08:05:40 +00001345 FunctionProtoType::ExtProtoInfo EPI;
1346 EPI.Variadic = Variadic;
1347 EPI.TypeQuals = Quals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001348 EPI.RefQualifier = RefQualifier;
John McCalle23cf432010-12-14 08:05:40 +00001349 EPI.ExtInfo = Info;
1350
1351 return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
Douglas Gregor724651c2009-02-28 01:04:19 +00001352}
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor949bf692009-06-09 22:17:39 +00001354/// \brief Build a member pointer type \c T Class::*.
1355///
1356/// \param T the type to which the member pointer refers.
1357/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +00001358/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +00001359/// \param Loc the location where this type begins
1360/// \param Entity the name of the entity that will have this member pointer type
1361///
1362/// \returns a member pointer type, if successful, or a NULL type if there was
1363/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001364QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +00001365 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +00001366 DeclarationName Entity) {
1367 // Verify that we're not building a pointer to pointer to function with
1368 // exception specification.
1369 if (CheckDistantExceptionSpec(T)) {
1370 Diag(Loc, diag::err_distant_exception_spec);
1371
1372 // FIXME: If we're doing this as part of template instantiation,
1373 // we should return immediately.
1374
1375 // Build the type anyway, but use the canonical type so that the
1376 // exception specifiers are stripped off.
1377 T = Context.getCanonicalType(T);
1378 }
1379
Sebastian Redl73780122010-06-09 21:19:43 +00001380 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +00001381 // with reference type, or "cv void."
1382 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +00001383 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001384 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +00001385 return QualType();
1386 }
1387
1388 if (T->isVoidType()) {
1389 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1390 << (Entity? Entity.getAsString() : "type name");
1391 return QualType();
1392 }
1393
Douglas Gregor949bf692009-06-09 22:17:39 +00001394 if (!Class->isDependentType() && !Class->isRecordType()) {
1395 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1396 return QualType();
1397 }
1398
Charles Davisd18f9f92010-08-16 04:01:50 +00001399 // In the Microsoft ABI, the class is allowed to be an incomplete
1400 // type. In such cases, the compiler makes a worst-case assumption.
1401 // We make no such assumption right now, so emit an error if the
1402 // class isn't a complete type.
Charles Davis20cf7172010-08-19 02:18:14 +00001403 if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
Charles Davisd18f9f92010-08-16 04:01:50 +00001404 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1405 return QualType();
1406
John McCall28654742010-06-05 06:41:15 +00001407 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +00001408}
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Anders Carlsson9a917e42009-06-12 22:56:54 +00001410/// \brief Build a block pointer type.
1411///
1412/// \param T The type to which we'll be building a block pointer.
1413///
John McCall0953e762009-09-24 19:53:00 +00001414/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +00001415///
1416/// \param Loc The location of the entity whose type involves this
1417/// block pointer type or, if there is no such entity, the location of the
1418/// type that will have block pointer type.
1419///
1420/// \param Entity The name of the entity that involves the block pointer
1421/// type, if known.
1422///
1423/// \returns A suitable block pointer type, if there are no
1424/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +00001425QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001426 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +00001427 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001428 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +00001429 Diag(Loc, diag::err_nonfunction_block_type);
1430 return QualType();
1431 }
Mike Stump1eb44332009-09-09 15:08:12 +00001432
John McCall28654742010-06-05 06:41:15 +00001433 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +00001434}
1435
John McCallb3d87482010-08-24 05:47:05 +00001436QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1437 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001438 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +00001439 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001440 return QualType();
1441 }
1442
John McCalla93c9342009-12-07 02:54:59 +00001443 TypeSourceInfo *DI = 0;
John McCallf4c73712011-01-19 06:33:43 +00001444 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001445 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +00001446 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
John McCalla93c9342009-12-07 02:54:59 +00001449 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001450 return QT;
1451}
1452
Chandler Carruthd067c072011-02-23 18:51:59 +00001453static void DiagnoseIgnoredQualifiers(unsigned Quals,
1454 SourceLocation ConstQualLoc,
1455 SourceLocation VolatileQualLoc,
1456 SourceLocation RestrictQualLoc,
1457 Sema& S) {
1458 std::string QualStr;
1459 unsigned NumQuals = 0;
1460 SourceLocation Loc;
1461
1462 FixItHint ConstFixIt;
1463 FixItHint VolatileFixIt;
1464 FixItHint RestrictFixIt;
1465
1466 // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1467 // find a range and grow it to encompass all the qualifiers, regardless of
1468 // the order in which they textually appear.
1469 if (Quals & Qualifiers::Const) {
1470 ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1471 Loc = ConstQualLoc;
1472 ++NumQuals;
1473 QualStr = "const";
1474 }
1475 if (Quals & Qualifiers::Volatile) {
1476 VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1477 if (NumQuals == 0) {
1478 Loc = VolatileQualLoc;
1479 QualStr = "volatile";
1480 } else {
1481 QualStr += " volatile";
1482 }
1483 ++NumQuals;
1484 }
1485 if (Quals & Qualifiers::Restrict) {
1486 RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1487 if (NumQuals == 0) {
1488 Loc = RestrictQualLoc;
1489 QualStr = "restrict";
1490 } else {
1491 QualStr += " restrict";
1492 }
1493 ++NumQuals;
1494 }
1495
1496 assert(NumQuals > 0 && "No known qualifiers?");
1497
1498 S.Diag(Loc, diag::warn_qual_return_type)
1499 << QualStr << NumQuals
1500 << ConstFixIt << VolatileFixIt << RestrictFixIt;
1501}
1502
Mike Stump98eb8a72009-02-04 22:31:32 +00001503/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001504/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +00001505///
1506/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1507/// owns the declaration of a type (e.g., the definition of a struct
1508/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +00001509///
1510/// The result of this call will never be null, but the associated
1511/// type may be a null type if there's an unrecoverable error.
1512TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Richard Smith34b41d92011-02-20 03:19:35 +00001513 TagDecl **OwnedDecl,
1514 bool AutoAllowedInTypeName) {
Douglas Gregor930d8b52009-01-30 22:09:00 +00001515 // Determine the type of the declarator. Not all forms of declarator
1516 // have a type.
1517 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +00001518 TypeSourceInfo *ReturnTypeInfo = 0;
John McCall711c52b2011-01-05 12:14:39 +00001519
1520 TypeProcessingState state(*this, D);
John McCall04a67a62010-02-05 21:31:56 +00001521
Sebastian Redl8999fe12011-03-14 18:08:30 +00001522 // In C++0x, deallocation functions (normal and array operator delete)
1523 // are implicitly noexcept.
1524 bool ImplicitlyNoexcept = false;
1525
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001526 switch (D.getName().getKind()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001527 case UnqualifiedId::IK_OperatorFunctionId:
Sebastian Redl8999fe12011-03-14 18:08:30 +00001528 if (getLangOptions().CPlusPlus0x) {
1529 OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator;
1530 if (OO == OO_Delete || OO == OO_Array_Delete)
1531 ImplicitlyNoexcept = true;
1532 }
1533 // Intentional fall-through.
1534 case UnqualifiedId::IK_Identifier:
Sean Hunt0486d742009-11-28 04:44:28 +00001535 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001536 case UnqualifiedId::IK_TemplateId:
John McCall711c52b2011-01-05 12:14:39 +00001537 T = ConvertDeclSpecToType(*this, state);
Chris Lattner5db2bb12009-10-25 18:21:37 +00001538
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001539 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
John McCallb3d87482010-08-24 05:47:05 +00001540 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Abramo Bagnara15987972011-03-08 22:33:38 +00001541 // Owned declaration is embedded in declarator.
1542 Owned->setEmbeddedInDeclarator(true);
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001543 if (OwnedDecl) *OwnedDecl = Owned;
1544 }
Douglas Gregor930d8b52009-01-30 22:09:00 +00001545 break;
1546
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001547 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001548 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001549 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +00001550 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +00001551 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +00001552 T = Context.VoidTy;
1553 break;
Douglas Gregor48026d22010-01-11 18:40:55 +00001554
1555 case UnqualifiedId::IK_ConversionFunctionId:
1556 // The result type of a conversion function is the type that it
1557 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001558 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +00001559 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +00001560 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +00001561 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00001562
John McCall711c52b2011-01-05 12:14:39 +00001563 if (D.getAttributes())
1564 distributeTypeAttrsFromDeclarator(state, T);
1565
Richard Smithe7397c62011-02-22 00:36:53 +00001566 // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
Richard Smith8110f042011-02-22 01:22:29 +00001567 // In C++0x, a function declarator using 'auto' must have a trailing return
1568 // type (this is checked later) and we can skip this. In other languages
1569 // using auto, we need to check regardless.
Richard Smith34b41d92011-02-20 03:19:35 +00001570 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
Richard Smith8110f042011-02-22 01:22:29 +00001571 (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001572 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001574 switch (D.getContext()) {
1575 case Declarator::KNRTypeListContext:
1576 assert(0 && "K&R type lists aren't allowed in C++");
1577 break;
John McCallc05a94b2011-03-23 23:43:04 +00001578 case Declarator::ObjCPrototypeContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001579 case Declarator::PrototypeContext:
1580 Error = 0; // Function prototype
1581 break;
1582 case Declarator::MemberContext:
1583 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001584 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1585 case TTK_Struct: Error = 1; /* Struct member */ break;
1586 case TTK_Union: Error = 2; /* Union member */ break;
1587 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001588 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001589 break;
1590 case Declarator::CXXCatchContext:
1591 Error = 4; // Exception declaration
1592 break;
1593 case Declarator::TemplateParamContext:
1594 Error = 5; // Template parameter
1595 break;
1596 case Declarator::BlockLiteralContext:
Richard Smith34b41d92011-02-20 03:19:35 +00001597 Error = 6; // Block literal
1598 break;
1599 case Declarator::TemplateTypeArgContext:
1600 Error = 7; // Template type argument
1601 break;
1602 case Declarator::TypeNameContext:
1603 if (!AutoAllowedInTypeName)
Richard Smith8110f042011-02-22 01:22:29 +00001604 Error = 10; // Generic
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001605 break;
1606 case Declarator::FileContext:
1607 case Declarator::BlockContext:
1608 case Declarator::ForContext:
1609 case Declarator::ConditionContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001610 break;
1611 }
1612
Richard Smithddc83f92011-02-21 23:18:00 +00001613 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1614 Error = 8;
1615
Richard Smith8110f042011-02-22 01:22:29 +00001616 // In Objective-C it is an error to use 'auto' on a function declarator.
1617 if (D.isFunctionDeclarator())
1618 Error = 9;
1619
Richard Smithe7397c62011-02-22 00:36:53 +00001620 // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1621 // contains a trailing return type. That is only legal at the outermost
1622 // level. Check all declarator chunks (outermost first) anyway, to give
1623 // better diagnostics.
Richard Smith8110f042011-02-22 01:22:29 +00001624 if (getLangOptions().CPlusPlus0x && Error != -1) {
Richard Smithe7397c62011-02-22 00:36:53 +00001625 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1626 unsigned chunkIndex = e - i - 1;
1627 state.setCurrentChunkIndex(chunkIndex);
1628 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1629 if (DeclType.Kind == DeclaratorChunk::Function) {
1630 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1631 if (FTI.TrailingReturnType) {
1632 Error = -1;
1633 break;
1634 }
1635 }
1636 }
1637 }
1638
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001639 if (Error != -1) {
1640 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1641 << Error;
1642 T = Context.IntTy;
1643 D.setInvalidType(true);
1644 }
1645 }
Richard Smithe7397c62011-02-22 00:36:53 +00001646
Richard Smith34b41d92011-02-20 03:19:35 +00001647 if (T.isNull())
1648 return Context.getNullTypeSourceInfo();
1649
Douglas Gregorcd281c32009-02-28 00:25:32 +00001650 // The name we're declaring, if any.
1651 DeclarationName Name;
1652 if (D.getIdentifier())
1653 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Mike Stump98eb8a72009-02-04 22:31:32 +00001655 // Walk the DeclTypeInfo, building the recursive type as we go.
1656 // DeclTypeInfos are ordered from the identifier out, which is
1657 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001658 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall711c52b2011-01-05 12:14:39 +00001659 unsigned chunkIndex = e - i - 1;
1660 state.setCurrentChunkIndex(chunkIndex);
1661 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 switch (DeclType.Kind) {
1663 default: assert(0 && "Unknown decltype!");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001664 case DeclaratorChunk::Paren:
1665 T = BuildParenType(T);
1666 break;
Steve Naroff5618bd42008-08-27 16:04:49 +00001667 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001668 // If blocks are disabled, emit an error.
1669 if (!LangOpts.Blocks)
1670 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001671
John McCall28654742010-06-05 06:41:15 +00001672 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1673 if (DeclType.Cls.TypeQuals)
1674 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001675 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001677 // Verify that we're not building a pointer to pointer to function with
1678 // exception specification.
1679 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1680 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1681 D.setInvalidType(true);
1682 // Build the type anyway.
1683 }
John McCallc12c5bb2010-05-15 11:32:37 +00001684 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1685 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001686 if (DeclType.Ptr.TypeQuals)
1687 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001688 break;
1689 }
John McCall28654742010-06-05 06:41:15 +00001690 T = BuildPointerType(T, DeclType.Loc, Name);
1691 if (DeclType.Ptr.TypeQuals)
1692 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
John McCall711c52b2011-01-05 12:14:39 +00001693
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 break;
John McCall0953e762009-09-24 19:53:00 +00001695 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001696 // Verify that we're not building a reference to pointer to function with
1697 // exception specification.
1698 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1699 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1700 D.setInvalidType(true);
1701 // Build the type anyway.
1702 }
John McCall28654742010-06-05 06:41:15 +00001703 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1704
1705 Qualifiers Quals;
1706 if (DeclType.Ref.HasRestrict)
1707 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 break;
John McCall0953e762009-09-24 19:53:00 +00001709 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001711 // Verify that we're not building an array of pointers to function with
1712 // exception specification.
1713 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1714 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1715 D.setInvalidType(true);
1716 // Build the type anyway.
1717 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001718 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001719 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 ArrayType::ArraySizeModifier ASM;
1721 if (ATI.isStar)
1722 ASM = ArrayType::Star;
1723 else if (ATI.hasStatic)
1724 ASM = ArrayType::Static;
1725 else
1726 ASM = ArrayType::Normal;
John McCallc05a94b2011-03-23 23:43:04 +00001727 if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001728 // FIXME: This check isn't quite right: it allows star in prototypes
1729 // for function definitions, and disallows some edge cases detailed
1730 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1731 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1732 ASM = ArrayType::Normal;
1733 D.setInvalidType(true);
1734 }
John McCall0953e762009-09-24 19:53:00 +00001735 T = BuildArrayType(T, ASM, ArraySize,
1736 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001737 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 break;
1739 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001740 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 // If the function declarator has a prototype (i.e. it is not () and
1742 // does not have a K&R-style identifier list), then the arguments are part
1743 // of the type, otherwise the argument list is ().
1744 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001745
Richard Smithe7397c62011-02-22 00:36:53 +00001746 // Check for auto functions and trailing return type and adjust the
1747 // return type accordingly.
1748 if (!D.isInvalidType()) {
1749 // trailing-return-type is only required if we're declaring a function,
1750 // and not, for instance, a pointer to a function.
1751 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
1752 !FTI.TrailingReturnType && chunkIndex == 0) {
1753 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1754 diag::err_auto_missing_trailing_return);
1755 T = Context.IntTy;
1756 D.setInvalidType(true);
1757 } else if (FTI.TrailingReturnType) {
1758 // T must be exactly 'auto' at this point. See CWG issue 681.
1759 if (isa<ParenType>(T)) {
1760 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1761 diag::err_trailing_return_in_parens)
1762 << T << D.getDeclSpec().getSourceRange();
1763 D.setInvalidType(true);
1764 } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
1765 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1766 diag::err_trailing_return_without_auto)
1767 << T << D.getDeclSpec().getSourceRange();
1768 D.setInvalidType(true);
1769 }
1770
1771 T = GetTypeFromParser(
1772 ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
1773 &ReturnTypeInfo);
1774 }
1775 }
1776
Chris Lattnercd881292007-12-19 05:31:29 +00001777 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001778 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001779 if ((T->isArrayType() || T->isFunctionType()) &&
1780 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001781 unsigned diagID = diag::err_func_returning_array_function;
Argyrios Kyrtzidisa4356ad2011-01-26 01:26:44 +00001782 // Last processing chunk in block context means this function chunk
1783 // represents the block.
1784 if (chunkIndex == 0 &&
1785 D.getContext() == Declarator::BlockLiteralContext)
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001786 diagID = diag::err_block_returning_array_function;
1787 Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001788 T = Context.IntTy;
1789 D.setInvalidType(true);
1790 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001791
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001792 // cv-qualifiers on return types are pointless except when the type is a
1793 // class type in C++.
Douglas Gregorfff95132011-03-01 17:04:42 +00001794 if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
Rafael Espindola1e153942011-03-11 04:56:58 +00001795 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
Chandler Carruthd067c072011-02-23 18:51:59 +00001796 (!getLangOptions().CPlusPlus || !T->isDependentType())) {
1797 assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
1798 DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
1799 assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
1800
1801 DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
1802
1803 DiagnoseIgnoredQualifiers(PTI.TypeQuals,
1804 SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
1805 SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
1806 SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
1807 *this);
1808
1809 } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001810 (!getLangOptions().CPlusPlus ||
1811 (!T->isDependentType() && !T->isRecordType()))) {
Chandler Carruthd067c072011-02-23 18:51:59 +00001812
1813 DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
1814 D.getDeclSpec().getConstSpecLoc(),
1815 D.getDeclSpec().getVolatileSpecLoc(),
1816 D.getDeclSpec().getRestrictSpecLoc(),
1817 *this);
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001818 }
Chandler Carruthd067c072011-02-23 18:51:59 +00001819
Douglas Gregor402abb52009-05-28 23:31:59 +00001820 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1821 // C++ [dcl.fct]p6:
1822 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00001823 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregor402abb52009-05-28 23:31:59 +00001824 if (Tag->isDefinition())
1825 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1826 << Context.getTypeDeclType(Tag);
1827 }
1828
Sebastian Redl3cc97262009-05-31 11:47:27 +00001829 // Exception specs are not allowed in typedefs. Complain, but add it
1830 // anyway.
Sebastian Redl6e5d3192011-03-05 22:42:13 +00001831 if (FTI.getExceptionSpecType() != EST_None &&
Sebastian Redl3cc97262009-05-31 11:47:27 +00001832 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
Sebastian Redl6e5d3192011-03-05 22:42:13 +00001833 Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef);
Sebastian Redl3cc97262009-05-31 11:47:27 +00001834
John McCall28654742010-06-05 06:41:15 +00001835 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1836 // Simple void foo(), where the incoming T is the result type.
1837 T = Context.getFunctionNoProtoType(T);
1838 } else {
1839 // We allow a zero-parameter variadic function in C if the
1840 // function is marked with the "overloadable" attribute. Scan
1841 // for this attribute now.
1842 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001843 bool Overloadable = false;
1844 for (const AttributeList *Attrs = D.getAttributes();
1845 Attrs; Attrs = Attrs->getNext()) {
1846 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1847 Overloadable = true;
1848 break;
1849 }
1850 }
1851
1852 if (!Overloadable)
1853 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001854 }
John McCall28654742010-06-05 06:41:15 +00001855
1856 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001857 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1858 // definition.
John McCall28654742010-06-05 06:41:15 +00001859 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1860 D.setInvalidType(true);
1861 break;
1862 }
1863
John McCalle23cf432010-12-14 08:05:40 +00001864 FunctionProtoType::ExtProtoInfo EPI;
1865 EPI.Variadic = FTI.isVariadic;
1866 EPI.TypeQuals = FTI.TypeQuals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001867 EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1868 : FTI.RefQualifierIsLValueRef? RQ_LValue
1869 : RQ_RValue;
1870
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 // Otherwise, we have a function with an argument list that is
1872 // potentially variadic.
1873 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001874 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001877 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00001878 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001879 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001880
1881 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001882 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001883
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 // Look for 'void'. void is allowed only as a single argument to a
1885 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001886 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001887 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 // If this is something like 'float(int, void)', reject it. 'void'
1889 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1890 // have arguments of incomplete type.
1891 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1892 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001893 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001894 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001895 } else if (FTI.ArgInfo[i].Ident) {
1896 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001898 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001899 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001900 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001901 } else {
1902 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001903 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001904 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Chris Lattner2ff54262007-07-21 05:18:12 +00001906 // Do not add 'void' to the ArgTys list.
1907 break;
1908 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001909 } else if (!FTI.hasPrototype) {
1910 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001911 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCalleecf5fa2011-03-09 04:22:44 +00001912 Param->setKNRPromoted(true);
John McCall183700f2009-09-21 23:43:11 +00001913 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
John McCalleecf5fa2011-03-09 04:22:44 +00001914 if (BTy->getKind() == BuiltinType::Float) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001915 ArgTy = Context.DoubleTy;
John McCalleecf5fa2011-03-09 04:22:44 +00001916 Param->setKNRPromoted(true);
1917 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001918 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 }
Fariborz Jahanian56a965c2010-09-08 21:36:35 +00001920
John McCall54e14c42009-10-22 22:37:11 +00001921 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001923
1924 llvm::SmallVector<QualType, 4> Exceptions;
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001925 EPI.ExceptionSpecType = FTI.getExceptionSpecType();
1926 if (FTI.getExceptionSpecType() == EST_Dynamic) {
John McCalle23cf432010-12-14 08:05:40 +00001927 Exceptions.reserve(FTI.NumExceptions);
1928 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1929 // FIXME: Preserve type source info.
1930 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1931 // Check that the type is valid for an exception spec, and
1932 // drop it if not.
1933 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1934 Exceptions.push_back(ET);
1935 }
John McCall373920b2010-12-14 16:45:57 +00001936 EPI.NumExceptions = Exceptions.size();
John McCalle23cf432010-12-14 08:05:40 +00001937 EPI.Exceptions = Exceptions.data();
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001938 } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
Sebastian Redl60618fa2011-03-12 11:50:43 +00001939 // If an error occurred, there's no expression here.
1940 if (Expr *NoexceptExpr = FTI.NoexceptExpr) {
1941 assert((NoexceptExpr->isTypeDependent() ||
1942 NoexceptExpr->getType()->getCanonicalTypeUnqualified() ==
1943 Context.BoolTy) &&
1944 "Parser should have made sure that the expression is boolean");
1945 SourceLocation ErrLoc;
1946 llvm::APSInt Dummy;
1947 if (!NoexceptExpr->isValueDependent() &&
1948 !NoexceptExpr->isIntegerConstantExpr(Dummy, Context, &ErrLoc,
1949 /*evaluated*/false))
1950 Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
1951 << NoexceptExpr->getSourceRange();
1952 else
1953 EPI.NoexceptExpr = NoexceptExpr;
1954 }
Sebastian Redl8999fe12011-03-14 18:08:30 +00001955 } else if (FTI.getExceptionSpecType() == EST_None &&
1956 ImplicitlyNoexcept && chunkIndex == 0) {
1957 // Only the outermost chunk is marked noexcept, of course.
1958 EPI.ExceptionSpecType = EST_BasicNoexcept;
Sebastian Redlef65f062009-05-29 18:02:33 +00001959 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001960
John McCalle23cf432010-12-14 08:05:40 +00001961 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 }
John McCall04a67a62010-02-05 21:31:56 +00001963
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 break;
1965 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001966 case DeclaratorChunk::MemberPointer:
1967 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001968 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001969 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001970 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001971 // Avoid emitting extra errors if we already errored on the scope.
1972 D.setInvalidType(true);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001973 } else if (isDependentScopeSpecifier(SS) ||
1974 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001975 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001976 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001977 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1978 switch (NNS->getKind()) {
1979 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001980 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001981 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001982 break;
1983
1984 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00001985 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor87c12c42009-11-04 16:49:01 +00001986 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001987 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001988 break;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001989
Douglas Gregor87c12c42009-11-04 16:49:01 +00001990 case NestedNameSpecifier::TypeSpec:
1991 case NestedNameSpecifier::TypeSpecWithTemplate:
1992 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara91ce2c42011-03-10 10:18:27 +00001993 // Note: if the NNS has a prefix and ClsType is a nondependent
1994 // TemplateSpecializationType, then the NNS prefix is NOT included
1995 // in ClsType; hence we wrap ClsType into an ElaboratedType.
1996 // NOTE: in particular, no wrap occurs if ClsType already is an
1997 // Elaborated, DependentName, or DependentTemplateSpecialization.
1998 if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001999 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00002000 break;
2001 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002002 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00002003 Diag(DeclType.Mem.Scope().getBeginLoc(),
2004 diag::err_illegal_decl_mempointer_in_nonclass)
2005 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
2006 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002007 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002008 }
2009
Douglas Gregor949bf692009-06-09 22:17:39 +00002010 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00002011 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00002012 if (T.isNull()) {
2013 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002014 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00002015 } else if (DeclType.Mem.TypeQuals) {
2016 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002017 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002018 break;
2019 }
2020
Douglas Gregorcd281c32009-02-28 00:25:32 +00002021 if (T.isNull()) {
2022 D.setInvalidType(true);
2023 T = Context.IntTy;
2024 }
2025
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002026 // See if there are any attributes on this declarator chunk.
John McCall711c52b2011-01-05 12:14:39 +00002027 if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2028 processTypeAttrs(state, T, false, attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002030
2031 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00002032 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00002033 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002034
Douglas Gregor708f3b82010-10-14 22:03:51 +00002035 // C++ 8.3.5p4:
2036 // A cv-qualifier-seq shall only be part of the function type
2037 // for a nonstatic member function, the function type to which a pointer
2038 // to member refers, or the top-level function type of a function typedef
2039 // declaration.
Douglas Gregor683a81f2011-01-31 16:09:46 +00002040 //
2041 // Core issue 547 also allows cv-qualifiers on function types that are
2042 // top-level template type arguments.
John McCall613ef3d2010-10-19 01:54:45 +00002043 bool FreeFunction;
2044 if (!D.getCXXScopeSpec().isSet()) {
2045 FreeFunction = (D.getContext() != Declarator::MemberContext ||
2046 D.getDeclSpec().isFriendSpecified());
2047 } else {
2048 DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
2049 FreeFunction = (DC && !DC->isRecord());
2050 }
2051
Douglas Gregorc938c162011-01-26 05:01:58 +00002052 // C++0x [dcl.fct]p6:
2053 // A ref-qualifier shall only be part of the function type for a
2054 // non-static member function, the function type to which a pointer to
2055 // member refers, or the top-level function type of a function typedef
2056 // declaration.
2057 if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
Douglas Gregor683a81f2011-01-31 16:09:46 +00002058 !(D.getContext() == Declarator::TemplateTypeArgContext &&
2059 !D.isFunctionDeclarator()) &&
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002060 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00002061 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002062 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Douglas Gregor683a81f2011-01-31 16:09:46 +00002063 if (D.getContext() == Declarator::TemplateTypeArgContext) {
2064 // Accept qualified function types as template type arguments as a GNU
2065 // extension. This is also the subject of C++ core issue 547.
2066 std::string Quals;
2067 if (FnTy->getTypeQuals() != 0)
2068 Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2069
2070 switch (FnTy->getRefQualifier()) {
2071 case RQ_None:
2072 break;
2073
2074 case RQ_LValue:
2075 if (!Quals.empty())
2076 Quals += ' ';
2077 Quals += '&';
2078 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00002079
Douglas Gregor683a81f2011-01-31 16:09:46 +00002080 case RQ_RValue:
2081 if (!Quals.empty())
2082 Quals += ' ';
2083 Quals += "&&";
2084 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00002085 }
Douglas Gregor683a81f2011-01-31 16:09:46 +00002086
2087 Diag(D.getIdentifierLoc(),
2088 diag::ext_qualified_function_type_template_arg)
2089 << Quals;
2090 } else {
2091 if (FnTy->getTypeQuals() != 0) {
2092 if (D.isFunctionDeclarator())
2093 Diag(D.getIdentifierLoc(),
2094 diag::err_invalid_qualified_function_type);
2095 else
2096 Diag(D.getIdentifierLoc(),
2097 diag::err_invalid_qualified_typedef_function_type_use)
2098 << FreeFunction;
2099 }
2100
2101 if (FnTy->getRefQualifier()) {
2102 if (D.isFunctionDeclarator()) {
2103 SourceLocation Loc = D.getIdentifierLoc();
2104 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
2105 const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
2106 if (Chunk.Kind == DeclaratorChunk::Function &&
2107 Chunk.Fun.hasRefQualifier()) {
2108 Loc = Chunk.Fun.getRefQualifierLoc();
2109 break;
2110 }
2111 }
2112
2113 Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
2114 << (FnTy->getRefQualifier() == RQ_LValue)
2115 << FixItHint::CreateRemoval(Loc);
2116 } else {
2117 Diag(D.getIdentifierLoc(),
2118 diag::err_invalid_ref_qualifier_typedef_function_type_use)
2119 << FreeFunction
2120 << (FnTy->getRefQualifier() == RQ_LValue);
2121 }
2122 }
2123
2124 // Strip the cv-qualifiers and ref-qualifiers from the type.
2125 FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2126 EPI.TypeQuals = 0;
2127 EPI.RefQualifier = RQ_None;
2128
2129 T = Context.getFunctionType(FnTy->getResultType(),
2130 FnTy->arg_type_begin(),
2131 FnTy->getNumArgs(), EPI);
Douglas Gregorc938c162011-01-26 05:01:58 +00002132 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002133 }
2134 }
Mike Stump1eb44332009-09-09 15:08:12 +00002135
John McCall711c52b2011-01-05 12:14:39 +00002136 // Apply any undistributed attributes from the declarator.
2137 if (!T.isNull())
2138 if (AttributeList *attrs = D.getAttributes())
2139 processTypeAttrs(state, T, false, attrs);
2140
2141 // Diagnose any ignored type attributes.
2142 if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2143
Peter Collingbourne148f1f72011-03-20 08:06:45 +00002144 // C++0x [dcl.constexpr]p9:
2145 // A constexpr specifier used in an object declaration declares the object
2146 // as const.
2147 if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
Sebastian Redl73780122010-06-09 21:19:43 +00002148 T.addConst();
2149 }
2150
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002151 // If there was an ellipsis in the declarator, the declaration declares a
2152 // parameter pack whose type may be a pack expansion type.
2153 if (D.hasEllipsis() && !T.isNull()) {
2154 // C++0x [dcl.fct]p13:
2155 // A declarator-id or abstract-declarator containing an ellipsis shall
2156 // only be used in a parameter-declaration. Such a parameter-declaration
2157 // is a parameter pack (14.5.3). [...]
2158 switch (D.getContext()) {
2159 case Declarator::PrototypeContext:
2160 // C++0x [dcl.fct]p13:
2161 // [...] When it is part of a parameter-declaration-clause, the
2162 // parameter pack is a function parameter pack (14.5.3). The type T
2163 // of the declarator-id of the function parameter pack shall contain
2164 // a template parameter pack; each template parameter pack in T is
2165 // expanded by the function parameter pack.
2166 //
2167 // We represent function parameter packs as function parameters whose
2168 // type is a pack expansion.
2169 if (!T->containsUnexpandedParameterPack()) {
2170 Diag(D.getEllipsisLoc(),
2171 diag::err_function_parameter_pack_without_parameter_packs)
2172 << T << D.getSourceRange();
2173 D.setEllipsisLoc(SourceLocation());
2174 } else {
Douglas Gregorcded4f62011-01-14 17:04:44 +00002175 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002176 }
2177 break;
2178
2179 case Declarator::TemplateParamContext:
2180 // C++0x [temp.param]p15:
2181 // If a template-parameter is a [...] is a parameter-declaration that
2182 // declares a parameter pack (8.3.5), then the template-parameter is a
2183 // template parameter pack (14.5.3).
2184 //
2185 // Note: core issue 778 clarifies that, if there are any unexpanded
2186 // parameter packs in the type of the non-type template parameter, then
2187 // it expands those parameter packs.
2188 if (T->containsUnexpandedParameterPack())
Douglas Gregorcded4f62011-01-14 17:04:44 +00002189 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregor10738d32010-12-23 23:51:58 +00002190 else if (!getLangOptions().CPlusPlus0x)
Douglas Gregor5ce5f522011-01-19 21:59:15 +00002191 Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002192 break;
2193
2194 case Declarator::FileContext:
2195 case Declarator::KNRTypeListContext:
John McCallc05a94b2011-03-23 23:43:04 +00002196 case Declarator::ObjCPrototypeContext: // FIXME: special diagnostic here?
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002197 case Declarator::TypeNameContext:
2198 case Declarator::MemberContext:
2199 case Declarator::BlockContext:
2200 case Declarator::ForContext:
2201 case Declarator::ConditionContext:
2202 case Declarator::CXXCatchContext:
2203 case Declarator::BlockLiteralContext:
Douglas Gregor683a81f2011-01-31 16:09:46 +00002204 case Declarator::TemplateTypeArgContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002205 // FIXME: We may want to allow parameter packs in block-literal contexts
2206 // in the future.
2207 Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2208 D.setEllipsisLoc(SourceLocation());
2209 break;
2210 }
2211 }
Richard Smithe7397c62011-02-22 00:36:53 +00002212
John McCallbf1a0282010-06-04 23:28:52 +00002213 if (T.isNull())
2214 return Context.getNullTypeSourceInfo();
2215 else if (D.isInvalidType())
2216 return Context.getTrivialTypeSourceInfo(T);
2217 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002218}
2219
John McCall14aa2172011-03-04 04:00:19 +00002220/// Map an AttributedType::Kind to an AttributeList::Kind.
2221static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
2222 switch (kind) {
2223 case AttributedType::attr_address_space:
2224 return AttributeList::AT_address_space;
2225 case AttributedType::attr_regparm:
2226 return AttributeList::AT_regparm;
2227 case AttributedType::attr_vector_size:
2228 return AttributeList::AT_vector_size;
2229 case AttributedType::attr_neon_vector_type:
2230 return AttributeList::AT_neon_vector_type;
2231 case AttributedType::attr_neon_polyvector_type:
2232 return AttributeList::AT_neon_polyvector_type;
2233 case AttributedType::attr_objc_gc:
2234 return AttributeList::AT_objc_gc;
2235 case AttributedType::attr_noreturn:
2236 return AttributeList::AT_noreturn;
2237 case AttributedType::attr_cdecl:
2238 return AttributeList::AT_cdecl;
2239 case AttributedType::attr_fastcall:
2240 return AttributeList::AT_fastcall;
2241 case AttributedType::attr_stdcall:
2242 return AttributeList::AT_stdcall;
2243 case AttributedType::attr_thiscall:
2244 return AttributeList::AT_thiscall;
2245 case AttributedType::attr_pascal:
2246 return AttributeList::AT_pascal;
2247 }
2248 llvm_unreachable("unexpected attribute kind!");
2249 return AttributeList::Kind();
2250}
2251
2252static void fillAttributedTypeLoc(AttributedTypeLoc TL,
2253 const AttributeList *attrs) {
2254 AttributedType::Kind kind = TL.getAttrKind();
2255
2256 assert(attrs && "no type attributes in the expected location!");
2257 AttributeList::Kind parsedKind = getAttrListKind(kind);
2258 while (attrs->getKind() != parsedKind) {
2259 attrs = attrs->getNext();
2260 assert(attrs && "no matching attribute in expected location!");
2261 }
2262
2263 TL.setAttrNameLoc(attrs->getLoc());
2264 if (TL.hasAttrExprOperand())
2265 TL.setAttrExprOperand(attrs->getArg(0));
2266 else if (TL.hasAttrEnumOperand())
2267 TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
2268
2269 // FIXME: preserve this information to here.
2270 if (TL.hasAttrOperand())
2271 TL.setAttrOperandParensRange(SourceRange());
2272}
2273
John McCall51bd8032009-10-18 01:05:36 +00002274namespace {
2275 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002276 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00002277 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002278
John McCall51bd8032009-10-18 01:05:36 +00002279 public:
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002280 TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2281 : Context(Context), DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002282
John McCall14aa2172011-03-04 04:00:19 +00002283 void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
2284 fillAttributedTypeLoc(TL, DS.getAttributes().getList());
2285 Visit(TL.getModifiedLoc());
2286 }
John McCall51bd8032009-10-18 01:05:36 +00002287 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
2288 Visit(TL.getUnqualifiedLoc());
2289 }
2290 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2291 TL.setNameLoc(DS.getTypeSpecTypeLoc());
2292 }
2293 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2294 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00002295 }
2296 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2297 // Handle the base type, which might not have been written explicitly.
2298 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2299 TL.setHasBaseTypeAsWritten(false);
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002300 TL.getBaseLoc().initialize(Context, SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002301 } else {
2302 TL.setHasBaseTypeAsWritten(true);
2303 Visit(TL.getBaseLoc());
2304 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00002305
John McCallc12c5bb2010-05-15 11:32:37 +00002306 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00002307 if (DS.getProtocolQualifiers()) {
2308 assert(TL.getNumProtocols() > 0);
2309 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
2310 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
2311 TL.setRAngleLoc(DS.getSourceRange().getEnd());
2312 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
2313 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
2314 } else {
2315 assert(TL.getNumProtocols() == 0);
2316 TL.setLAngleLoc(SourceLocation());
2317 TL.setRAngleLoc(SourceLocation());
2318 }
2319 }
2320 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00002321 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002322 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002323 }
John McCall833ca992009-10-29 08:12:44 +00002324 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00002325 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002326 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00002327
2328 // If we got no declarator info from previous Sema routines,
2329 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00002330 if (!TInfo) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002331 TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
John McCall833ca992009-10-29 08:12:44 +00002332 return;
2333 }
2334
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002335 TypeLoc OldTL = TInfo->getTypeLoc();
2336 if (TInfo->getType()->getAs<ElaboratedType>()) {
2337 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2338 TemplateSpecializationTypeLoc NamedTL =
2339 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2340 TL.copy(NamedTL);
2341 }
2342 else
2343 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00002344 }
John McCallcfb708c2010-01-13 20:03:27 +00002345 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2346 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2347 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2348 TL.setParensRange(DS.getTypeofParensRange());
2349 }
2350 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2351 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2352 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2353 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00002354 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00002355 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002356 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00002357 TL.setUnderlyingTInfo(TInfo);
2358 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00002359 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2360 // By default, use the source location of the type specifier.
2361 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2362 if (TL.needsExtraLocalData()) {
2363 // Set info for the written builtin specifiers.
2364 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2365 // Try to have a meaningful source location.
2366 if (TL.getWrittenSignSpec() != TSS_unspecified)
2367 // Sign spec loc overrides the others (e.g., 'unsigned long').
2368 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2369 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2370 // Width spec loc overrides type spec loc (e.g., 'short int').
2371 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2372 }
2373 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002374 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2375 ElaboratedTypeKeyword Keyword
2376 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002377 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002378 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002379 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002380 if (TInfo) {
2381 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2382 return;
2383 }
2384 }
2385 TL.setKeywordLoc(Keyword != ETK_None
2386 ? DS.getTypeSpecTypeLoc()
2387 : SourceLocation());
2388 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor9e876872011-03-01 18:12:44 +00002389 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002390 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2391 }
2392 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2393 ElaboratedTypeKeyword Keyword
2394 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002395 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002396 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002397 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002398 if (TInfo) {
2399 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2400 return;
2401 }
2402 }
2403 TL.setKeywordLoc(Keyword != ETK_None
2404 ? DS.getTypeSpecTypeLoc()
2405 : SourceLocation());
2406 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor2494dd02011-03-01 01:34:45 +00002407 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002408 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002409 }
John McCall33500952010-06-11 00:33:02 +00002410 void VisitDependentTemplateSpecializationTypeLoc(
2411 DependentTemplateSpecializationTypeLoc TL) {
2412 ElaboratedTypeKeyword Keyword
2413 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2414 if (Keyword == ETK_Typename) {
2415 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002416 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall33500952010-06-11 00:33:02 +00002417 if (TInfo) {
2418 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
2419 TInfo->getTypeLoc()));
2420 return;
2421 }
2422 }
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002423 TL.initializeLocal(Context, SourceLocation());
John McCall33500952010-06-11 00:33:02 +00002424 TL.setKeywordLoc(Keyword != ETK_None
2425 ? DS.getTypeSpecTypeLoc()
2426 : SourceLocation());
2427 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002428 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002429 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
2430 }
2431 void VisitTagTypeLoc(TagTypeLoc TL) {
2432 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
John McCall33500952010-06-11 00:33:02 +00002433 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002434
John McCall51bd8032009-10-18 01:05:36 +00002435 void VisitTypeLoc(TypeLoc TL) {
2436 // FIXME: add other typespec types and change this to an assert.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002437 TL.initialize(Context, DS.getTypeSpecTypeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002438 }
2439 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002440
John McCall51bd8032009-10-18 01:05:36 +00002441 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002442 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00002443 const DeclaratorChunk &Chunk;
2444
2445 public:
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002446 DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
2447 : Context(Context), Chunk(Chunk) {}
John McCall51bd8032009-10-18 01:05:36 +00002448
2449 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002450 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00002451 }
2452
2453 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2454 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
2455 TL.setCaretLoc(Chunk.Loc);
2456 }
2457 void VisitPointerTypeLoc(PointerTypeLoc TL) {
2458 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2459 TL.setStarLoc(Chunk.Loc);
2460 }
2461 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2462 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2463 TL.setStarLoc(Chunk.Loc);
2464 }
2465 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2466 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002467 const CXXScopeSpec& SS = Chunk.Mem.Scope();
2468 NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
2469
2470 const Type* ClsTy = TL.getClass();
2471 QualType ClsQT = QualType(ClsTy, 0);
2472 TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
2473 // Now copy source location info into the type loc component.
2474 TypeLoc ClsTL = ClsTInfo->getTypeLoc();
2475 switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
2476 case NestedNameSpecifier::Identifier:
2477 assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
2478 {
Abramo Bagnarafd9c42e2011-03-06 22:48:24 +00002479 DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002480 DNTLoc.setKeywordLoc(SourceLocation());
2481 DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
2482 DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
2483 }
2484 break;
2485
2486 case NestedNameSpecifier::TypeSpec:
2487 case NestedNameSpecifier::TypeSpecWithTemplate:
2488 if (isa<ElaboratedType>(ClsTy)) {
2489 ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
2490 ETLoc.setKeywordLoc(SourceLocation());
2491 ETLoc.setQualifierLoc(NNSLoc.getPrefix());
2492 TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
2493 NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
2494 } else {
2495 ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
2496 }
2497 break;
2498
2499 case NestedNameSpecifier::Namespace:
2500 case NestedNameSpecifier::NamespaceAlias:
2501 case NestedNameSpecifier::Global:
2502 llvm_unreachable("Nested-name-specifier must name a type");
2503 break;
2504 }
2505
2506 // Finally fill in MemberPointerLocInfo fields.
John McCall51bd8032009-10-18 01:05:36 +00002507 TL.setStarLoc(Chunk.Loc);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002508 TL.setClassTInfo(ClsTInfo);
John McCall51bd8032009-10-18 01:05:36 +00002509 }
2510 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2511 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00002512 // 'Amp' is misleading: this might have been originally
2513 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00002514 TL.setAmpLoc(Chunk.Loc);
2515 }
2516 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2517 assert(Chunk.Kind == DeclaratorChunk::Reference);
2518 assert(!Chunk.Ref.LValueRef);
2519 TL.setAmpAmpLoc(Chunk.Loc);
2520 }
2521 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
2522 assert(Chunk.Kind == DeclaratorChunk::Array);
2523 TL.setLBracketLoc(Chunk.Loc);
2524 TL.setRBracketLoc(Chunk.EndLoc);
2525 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
2526 }
2527 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2528 assert(Chunk.Kind == DeclaratorChunk::Function);
Abramo Bagnara796aa442011-03-12 11:17:06 +00002529 TL.setLocalRangeBegin(Chunk.Loc);
2530 TL.setLocalRangeEnd(Chunk.EndLoc);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002531 TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
John McCall51bd8032009-10-18 01:05:36 +00002532
2533 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00002534 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002535 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00002536 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00002537 }
2538 // FIXME: exception specs
2539 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002540 void VisitParenTypeLoc(ParenTypeLoc TL) {
2541 assert(Chunk.Kind == DeclaratorChunk::Paren);
2542 TL.setLParenLoc(Chunk.Loc);
2543 TL.setRParenLoc(Chunk.EndLoc);
2544 }
John McCall51bd8032009-10-18 01:05:36 +00002545
2546 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002547 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00002548 }
2549 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002550}
2551
John McCalla93c9342009-12-07 02:54:59 +00002552/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002553///
2554/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00002555///
2556/// \param ReturnTypeInfo For declarators whose return type does not show
2557/// up in the normal place in the declaration specifiers (such as a C++
2558/// conversion function), this pointer will refer to a type source information
2559/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00002560TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00002561Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
2562 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00002563 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2564 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002565
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002566 // Handle parameter packs whose type is a pack expansion.
2567 if (isa<PackExpansionType>(T)) {
2568 cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2569 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2570 }
2571
Sebastian Redl8ce35b02009-10-25 21:45:37 +00002572 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall14aa2172011-03-04 04:00:19 +00002573 while (isa<AttributedTypeLoc>(CurrTL)) {
2574 AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
2575 fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
2576 CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
2577 }
2578
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002579 DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
John McCall51bd8032009-10-18 01:05:36 +00002580 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002581 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002582
John McCallb3d87482010-08-24 05:47:05 +00002583 // If we have different source information for the return type, use
2584 // that. This really only applies to C++ conversion functions.
2585 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00002586 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
2587 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
2588 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00002589 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002590 TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00002591 }
2592
John McCalla93c9342009-12-07 02:54:59 +00002593 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002594}
2595
John McCalla93c9342009-12-07 02:54:59 +00002596/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00002597ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002598 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
2599 // and Sema during declaration parsing. Try deallocating/caching them when
2600 // it's appropriate, instead of allocating them and keeping them around.
Douglas Gregoreb0eb492010-12-10 08:12:03 +00002601 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2602 TypeAlignment);
John McCalla93c9342009-12-07 02:54:59 +00002603 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002604 assert(LocT->getTypeClass() != T->getTypeClass() &&
2605 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00002606 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002607}
2608
2609void LocInfoType::getAsStringInternal(std::string &Str,
2610 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00002611 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
2612 " was used directly instead of getting the QualType through"
2613 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002614}
2615
John McCallf312b1e2010-08-26 23:41:50 +00002616TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002617 // C99 6.7.6: Type names have no identifier. This is already validated by
2618 // the parser.
2619 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Douglas Gregor402abb52009-05-28 23:31:59 +00002621 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002622 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2623 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00002624 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00002625 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00002626
Douglas Gregor402abb52009-05-28 23:31:59 +00002627 if (getLangOptions().CPlusPlus) {
2628 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00002629 CheckExtraCXXDefaultArguments(D);
2630
Douglas Gregor402abb52009-05-28 23:31:59 +00002631 // C++0x [dcl.type]p3:
2632 // A type-specifier-seq shall not define a class or enumeration
2633 // unless it appears in the type-id of an alias-declaration
2634 // (7.1.3).
2635 if (OwnedTag && OwnedTag->isDefinition())
2636 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2637 << Context.getTypeDeclType(OwnedTag);
2638 }
2639
John McCallb3d87482010-08-24 05:47:05 +00002640 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002641}
2642
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002643
2644
2645//===----------------------------------------------------------------------===//
2646// Type Attribute Processing
2647//===----------------------------------------------------------------------===//
2648
2649/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2650/// specified type. The attribute contains 1 argument, the id of the address
2651/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00002652static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002653 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00002654
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002655 // If this type is already address space qualified, reject it.
2656 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2657 // for two or more different address spaces."
2658 if (Type.getAddressSpace()) {
2659 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00002660 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002661 return;
2662 }
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002664 // Check the attribute arguments.
2665 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00002666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002667 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002668 return;
2669 }
2670 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2671 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002672 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2673 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00002674 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2675 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002676 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002677 return;
2678 }
2679
John McCallefadb772009-07-28 06:52:18 +00002680 // Bounds checking.
2681 if (addrSpace.isSigned()) {
2682 if (addrSpace.isNegative()) {
2683 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2684 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002685 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002686 return;
2687 }
2688 addrSpace.setIsSigned(false);
2689 }
2690 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00002691 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00002692 if (addrSpace > max) {
2693 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00002694 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002695 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002696 return;
2697 }
2698
Mike Stump1eb44332009-09-09 15:08:12 +00002699 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002700 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002701}
2702
John McCall711c52b2011-01-05 12:14:39 +00002703/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2704/// attribute on the specified type. Returns true to indicate that
2705/// the attribute was handled, false to indicate that the type does
2706/// not permit the attribute.
2707static bool handleObjCGCTypeAttr(TypeProcessingState &state,
2708 AttributeList &attr,
2709 QualType &type) {
2710 Sema &S = state.getSema();
2711
2712 // Delay if this isn't some kind of pointer.
2713 if (!type->isPointerType() &&
2714 !type->isObjCObjectPointerType() &&
2715 !type->isBlockPointerType())
2716 return false;
2717
2718 if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2719 S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2720 attr.setInvalid();
2721 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002722 }
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002724 // Check the attribute arguments.
John McCall711c52b2011-01-05 12:14:39 +00002725 if (!attr.getParameterName()) {
2726 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002727 << "objc_gc" << 1;
John McCall711c52b2011-01-05 12:14:39 +00002728 attr.setInvalid();
2729 return true;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002730 }
John McCall0953e762009-09-24 19:53:00 +00002731 Qualifiers::GC GCAttr;
John McCall711c52b2011-01-05 12:14:39 +00002732 if (attr.getNumArgs() != 0) {
2733 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2734 attr.setInvalid();
2735 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002736 }
John McCall711c52b2011-01-05 12:14:39 +00002737 if (attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00002738 GCAttr = Qualifiers::Weak;
John McCall711c52b2011-01-05 12:14:39 +00002739 else if (attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00002740 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002741 else {
John McCall711c52b2011-01-05 12:14:39 +00002742 S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2743 << "objc_gc" << attr.getParameterName();
2744 attr.setInvalid();
2745 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002746 }
Mike Stump1eb44332009-09-09 15:08:12 +00002747
John McCall14aa2172011-03-04 04:00:19 +00002748 QualType origType = type;
2749 type = S.Context.getObjCGCQualType(origType, GCAttr);
2750
2751 // Make an attributed type to preserve the source information.
2752 if (attr.getLoc().isValid())
2753 type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
2754 origType, type);
2755
John McCall711c52b2011-01-05 12:14:39 +00002756 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002757}
2758
John McCalle6a365d2010-12-19 02:44:49 +00002759namespace {
2760 /// A helper class to unwrap a type down to a function for the
2761 /// purposes of applying attributes there.
2762 ///
2763 /// Use:
2764 /// FunctionTypeUnwrapper unwrapped(SemaRef, T);
2765 /// if (unwrapped.isFunctionType()) {
2766 /// const FunctionType *fn = unwrapped.get();
2767 /// // change fn somehow
2768 /// T = unwrapped.wrap(fn);
2769 /// }
2770 struct FunctionTypeUnwrapper {
2771 enum WrapKind {
2772 Desugar,
2773 Parens,
2774 Pointer,
2775 BlockPointer,
2776 Reference,
2777 MemberPointer
2778 };
2779
2780 QualType Original;
2781 const FunctionType *Fn;
2782 llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2783
2784 FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2785 while (true) {
2786 const Type *Ty = T.getTypePtr();
2787 if (isa<FunctionType>(Ty)) {
2788 Fn = cast<FunctionType>(Ty);
2789 return;
2790 } else if (isa<ParenType>(Ty)) {
2791 T = cast<ParenType>(Ty)->getInnerType();
2792 Stack.push_back(Parens);
2793 } else if (isa<PointerType>(Ty)) {
2794 T = cast<PointerType>(Ty)->getPointeeType();
2795 Stack.push_back(Pointer);
2796 } else if (isa<BlockPointerType>(Ty)) {
2797 T = cast<BlockPointerType>(Ty)->getPointeeType();
2798 Stack.push_back(BlockPointer);
2799 } else if (isa<MemberPointerType>(Ty)) {
2800 T = cast<MemberPointerType>(Ty)->getPointeeType();
2801 Stack.push_back(MemberPointer);
2802 } else if (isa<ReferenceType>(Ty)) {
2803 T = cast<ReferenceType>(Ty)->getPointeeType();
2804 Stack.push_back(Reference);
2805 } else {
2806 const Type *DTy = Ty->getUnqualifiedDesugaredType();
2807 if (Ty == DTy) {
2808 Fn = 0;
2809 return;
2810 }
2811
2812 T = QualType(DTy, 0);
2813 Stack.push_back(Desugar);
2814 }
2815 }
2816 }
2817
2818 bool isFunctionType() const { return (Fn != 0); }
2819 const FunctionType *get() const { return Fn; }
2820
2821 QualType wrap(Sema &S, const FunctionType *New) {
2822 // If T wasn't modified from the unwrapped type, do nothing.
2823 if (New == get()) return Original;
2824
2825 Fn = New;
2826 return wrap(S.Context, Original, 0);
2827 }
2828
2829 private:
2830 QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2831 if (I == Stack.size())
2832 return C.getQualifiedType(Fn, Old.getQualifiers());
2833
2834 // Build up the inner type, applying the qualifiers from the old
2835 // type to the new type.
2836 SplitQualType SplitOld = Old.split();
2837
2838 // As a special case, tail-recurse if there are no qualifiers.
2839 if (SplitOld.second.empty())
2840 return wrap(C, SplitOld.first, I);
2841 return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2842 }
2843
2844 QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2845 if (I == Stack.size()) return QualType(Fn, 0);
2846
2847 switch (static_cast<WrapKind>(Stack[I++])) {
2848 case Desugar:
2849 // This is the point at which we potentially lose source
2850 // information.
2851 return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2852
2853 case Parens: {
2854 QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2855 return C.getParenType(New);
2856 }
2857
2858 case Pointer: {
2859 QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2860 return C.getPointerType(New);
2861 }
2862
2863 case BlockPointer: {
2864 QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2865 return C.getBlockPointerType(New);
2866 }
2867
2868 case MemberPointer: {
2869 const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2870 QualType New = wrap(C, OldMPT->getPointeeType(), I);
2871 return C.getMemberPointerType(New, OldMPT->getClass());
2872 }
2873
2874 case Reference: {
2875 const ReferenceType *OldRef = cast<ReferenceType>(Old);
2876 QualType New = wrap(C, OldRef->getPointeeType(), I);
2877 if (isa<LValueReferenceType>(OldRef))
2878 return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2879 else
2880 return C.getRValueReferenceType(New);
2881 }
2882 }
2883
2884 llvm_unreachable("unknown wrapping kind");
2885 return QualType();
2886 }
2887 };
2888}
2889
John McCall711c52b2011-01-05 12:14:39 +00002890/// Process an individual function attribute. Returns true to
2891/// indicate that the attribute was handled, false if it wasn't.
2892static bool handleFunctionTypeAttr(TypeProcessingState &state,
2893 AttributeList &attr,
2894 QualType &type) {
2895 Sema &S = state.getSema();
John McCalle6a365d2010-12-19 02:44:49 +00002896
John McCall711c52b2011-01-05 12:14:39 +00002897 FunctionTypeUnwrapper unwrapped(S, type);
Mike Stump24556362009-07-25 21:26:53 +00002898
John McCall711c52b2011-01-05 12:14:39 +00002899 if (attr.getKind() == AttributeList::AT_noreturn) {
2900 if (S.CheckNoReturnAttr(attr))
John McCall04a67a62010-02-05 21:31:56 +00002901 return true;
John McCalle6a365d2010-12-19 02:44:49 +00002902
John McCall711c52b2011-01-05 12:14:39 +00002903 // Delay if this is not a function type.
2904 if (!unwrapped.isFunctionType())
2905 return false;
2906
John McCall04a67a62010-02-05 21:31:56 +00002907 // Otherwise we can process right away.
John McCall711c52b2011-01-05 12:14:39 +00002908 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2909 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2910 return true;
John McCall04a67a62010-02-05 21:31:56 +00002911 }
Mike Stump24556362009-07-25 21:26:53 +00002912
John McCall711c52b2011-01-05 12:14:39 +00002913 if (attr.getKind() == AttributeList::AT_regparm) {
2914 unsigned value;
2915 if (S.CheckRegparmAttr(attr, value))
Rafael Espindola425ef722010-03-30 22:15:11 +00002916 return true;
2917
John McCall711c52b2011-01-05 12:14:39 +00002918 // Delay if this is not a function type.
2919 if (!unwrapped.isFunctionType())
Rafael Espindola425ef722010-03-30 22:15:11 +00002920 return false;
2921
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002922 // Diagnose regparm with fastcall.
2923 const FunctionType *fn = unwrapped.get();
2924 CallingConv CC = fn->getCallConv();
2925 if (CC == CC_X86FastCall) {
2926 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2927 << FunctionType::getNameForCallConv(CC)
2928 << "regparm";
2929 attr.setInvalid();
2930 return true;
2931 }
2932
John McCalle6a365d2010-12-19 02:44:49 +00002933 FunctionType::ExtInfo EI =
John McCall711c52b2011-01-05 12:14:39 +00002934 unwrapped.get()->getExtInfo().withRegParm(value);
2935 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2936 return true;
Rafael Espindola425ef722010-03-30 22:15:11 +00002937 }
2938
John McCall04a67a62010-02-05 21:31:56 +00002939 // Otherwise, a calling convention.
John McCall711c52b2011-01-05 12:14:39 +00002940 CallingConv CC;
2941 if (S.CheckCallingConvAttr(attr, CC))
2942 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002943
John McCall04a67a62010-02-05 21:31:56 +00002944 // Delay if the type didn't work out to a function.
John McCall711c52b2011-01-05 12:14:39 +00002945 if (!unwrapped.isFunctionType()) return false;
John McCall04a67a62010-02-05 21:31:56 +00002946
John McCall711c52b2011-01-05 12:14:39 +00002947 const FunctionType *fn = unwrapped.get();
2948 CallingConv CCOld = fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00002949 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00002950 S.Context.getCanonicalCallConv(CCOld)) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002951 FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2952 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
John McCall711c52b2011-01-05 12:14:39 +00002953 return true;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002954 }
John McCall04a67a62010-02-05 21:31:56 +00002955
2956 if (CCOld != CC_Default) {
2957 // Should we diagnose reapplications of the same convention?
John McCall711c52b2011-01-05 12:14:39 +00002958 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
John McCall04a67a62010-02-05 21:31:56 +00002959 << FunctionType::getNameForCallConv(CC)
2960 << FunctionType::getNameForCallConv(CCOld);
John McCall711c52b2011-01-05 12:14:39 +00002961 attr.setInvalid();
2962 return true;
John McCall04a67a62010-02-05 21:31:56 +00002963 }
2964
2965 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
2966 if (CC == CC_X86FastCall) {
John McCall711c52b2011-01-05 12:14:39 +00002967 if (isa<FunctionNoProtoType>(fn)) {
2968 S.Diag(attr.getLoc(), diag::err_cconv_knr)
John McCall04a67a62010-02-05 21:31:56 +00002969 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002970 attr.setInvalid();
2971 return true;
John McCall04a67a62010-02-05 21:31:56 +00002972 }
2973
John McCall711c52b2011-01-05 12:14:39 +00002974 const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
John McCall04a67a62010-02-05 21:31:56 +00002975 if (FnP->isVariadic()) {
John McCall711c52b2011-01-05 12:14:39 +00002976 S.Diag(attr.getLoc(), diag::err_cconv_varargs)
John McCall04a67a62010-02-05 21:31:56 +00002977 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002978 attr.setInvalid();
2979 return true;
John McCall04a67a62010-02-05 21:31:56 +00002980 }
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002981
2982 // Also diagnose fastcall with regparm.
Eli Friedmana49218e2011-04-09 08:18:08 +00002983 if (fn->getHasRegParm()) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002984 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2985 << "regparm"
2986 << FunctionType::getNameForCallConv(CC);
2987 attr.setInvalid();
2988 return true;
2989 }
John McCall04a67a62010-02-05 21:31:56 +00002990 }
2991
John McCall711c52b2011-01-05 12:14:39 +00002992 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2993 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2994 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002995}
2996
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002997/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
2998static void HandleOpenCLImageAccessAttribute(QualType& CurType,
2999 const AttributeList &Attr,
3000 Sema &S) {
3001 // Check the attribute arguments.
3002 if (Attr.getNumArgs() != 1) {
3003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3004 Attr.setInvalid();
3005 return;
3006 }
3007 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3008 llvm::APSInt arg(32);
3009 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3010 !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
3011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3012 << "opencl_image_access" << sizeExpr->getSourceRange();
3013 Attr.setInvalid();
3014 return;
3015 }
3016 unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
3017 switch (iarg) {
3018 case CLIA_read_only:
3019 case CLIA_write_only:
3020 case CLIA_read_write:
3021 // Implemented in a separate patch
3022 break;
3023 default:
3024 // Implemented in a separate patch
3025 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3026 << sizeExpr->getSourceRange();
3027 Attr.setInvalid();
3028 break;
3029 }
3030}
3031
John Thompson6e132aa2009-12-04 21:51:28 +00003032/// HandleVectorSizeAttribute - this attribute is only applicable to integral
3033/// and float scalars, although arrays, pointers, and function return values are
3034/// allowed in conjunction with this construct. Aggregates with this attribute
3035/// are invalid, even if they are of the same size as a corresponding scalar.
3036/// The raw attribute should contain precisely 1 argument, the vector size for
3037/// the variable, measured in bytes. If curType and rawAttr are well formed,
3038/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00003039static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
3040 Sema &S) {
Bob Wilson56affbc2010-11-16 00:32:16 +00003041 // Check the attribute arguments.
John Thompson6e132aa2009-12-04 21:51:28 +00003042 if (Attr.getNumArgs() != 1) {
3043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003044 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003045 return;
3046 }
3047 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3048 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003049 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3050 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00003051 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3052 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003053 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003054 return;
3055 }
3056 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00003057 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00003058 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003059 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003060 return;
3061 }
3062 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
3063 // vecSize is specified in bytes - convert to bits.
3064 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
3065
3066 // the vector size needs to be an integral multiple of the type size.
3067 if (vectorSize % typeSize) {
3068 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3069 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003070 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003071 return;
3072 }
3073 if (vectorSize == 0) {
3074 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
3075 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003076 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003077 return;
3078 }
3079
3080 // Success! Instantiate the vector type, the number of elements is > 0, and
3081 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00003082 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003083 VectorType::GenericVector);
John Thompson6e132aa2009-12-04 21:51:28 +00003084}
3085
Bob Wilson4211bb62010-11-16 00:32:24 +00003086/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
3087/// "neon_polyvector_type" attributes are used to create vector types that
3088/// are mangled according to ARM's ABI. Otherwise, these types are identical
3089/// to those created with the "vector_size" attribute. Unlike "vector_size"
3090/// the argument to these Neon attributes is the number of vector elements,
3091/// not the vector size in bytes. The vector width and element type must
3092/// match one of the standard Neon vector types.
3093static void HandleNeonVectorTypeAttr(QualType& CurType,
3094 const AttributeList &Attr, Sema &S,
3095 VectorType::VectorKind VecKind,
3096 const char *AttrName) {
3097 // Check the attribute arguments.
3098 if (Attr.getNumArgs() != 1) {
3099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3100 Attr.setInvalid();
3101 return;
3102 }
3103 // The number of elements must be an ICE.
3104 Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
3105 llvm::APSInt numEltsInt(32);
3106 if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
3107 !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
3108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3109 << AttrName << numEltsExpr->getSourceRange();
3110 Attr.setInvalid();
3111 return;
3112 }
3113 // Only certain element types are supported for Neon vectors.
3114 const BuiltinType* BTy = CurType->getAs<BuiltinType>();
3115 if (!BTy ||
3116 (VecKind == VectorType::NeonPolyVector &&
3117 BTy->getKind() != BuiltinType::SChar &&
3118 BTy->getKind() != BuiltinType::Short) ||
3119 (BTy->getKind() != BuiltinType::SChar &&
3120 BTy->getKind() != BuiltinType::UChar &&
3121 BTy->getKind() != BuiltinType::Short &&
3122 BTy->getKind() != BuiltinType::UShort &&
3123 BTy->getKind() != BuiltinType::Int &&
3124 BTy->getKind() != BuiltinType::UInt &&
3125 BTy->getKind() != BuiltinType::LongLong &&
3126 BTy->getKind() != BuiltinType::ULongLong &&
3127 BTy->getKind() != BuiltinType::Float)) {
3128 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
3129 Attr.setInvalid();
3130 return;
3131 }
3132 // The total size of the vector must be 64 or 128 bits.
3133 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
3134 unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
3135 unsigned vecSize = typeSize * numElts;
3136 if (vecSize != 64 && vecSize != 128) {
3137 S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
3138 Attr.setInvalid();
3139 return;
3140 }
3141
3142 CurType = S.Context.getVectorType(CurType, numElts, VecKind);
3143}
3144
John McCall711c52b2011-01-05 12:14:39 +00003145static void processTypeAttrs(TypeProcessingState &state, QualType &type,
3146 bool isDeclSpec, AttributeList *attrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00003147 // Scan through and apply attributes to this type where it makes sense. Some
3148 // attributes (such as __address_space__, __vector_size__, etc) apply to the
3149 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00003150 // apply to the decl. Here we apply type attributes and ignore the rest.
John McCall711c52b2011-01-05 12:14:39 +00003151
3152 AttributeList *next;
3153 do {
3154 AttributeList &attr = *attrs;
3155 next = attr.getNext();
3156
Abramo Bagnarae215f722010-04-30 13:10:51 +00003157 // Skip attributes that were marked to be invalid.
John McCall711c52b2011-01-05 12:14:39 +00003158 if (attr.isInvalid())
Abramo Bagnarae215f722010-04-30 13:10:51 +00003159 continue;
3160
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00003161 // If this is an attribute we can handle, do so now,
3162 // otherwise, add it to the FnAttrs list for rechaining.
John McCall711c52b2011-01-05 12:14:39 +00003163 switch (attr.getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00003164 default: break;
John McCall04a67a62010-02-05 21:31:56 +00003165
Chris Lattner232e8822008-02-21 01:08:11 +00003166 case AttributeList::AT_address_space:
John McCall711c52b2011-01-05 12:14:39 +00003167 HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003168 break;
John McCall711c52b2011-01-05 12:14:39 +00003169 OBJC_POINTER_TYPE_ATTRS_CASELIST:
3170 if (!handleObjCPointerTypeAttr(state, attr, type))
3171 distributeObjCPointerTypeAttr(state, attr, type);
Mike Stump24556362009-07-25 21:26:53 +00003172 break;
John Thompson6e132aa2009-12-04 21:51:28 +00003173 case AttributeList::AT_vector_size:
John McCall711c52b2011-01-05 12:14:39 +00003174 HandleVectorSizeAttr(type, attr, state.getSema());
John McCall04a67a62010-02-05 21:31:56 +00003175 break;
Bob Wilson4211bb62010-11-16 00:32:24 +00003176 case AttributeList::AT_neon_vector_type:
John McCall711c52b2011-01-05 12:14:39 +00003177 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3178 VectorType::NeonVector, "neon_vector_type");
Bob Wilson4211bb62010-11-16 00:32:24 +00003179 break;
3180 case AttributeList::AT_neon_polyvector_type:
John McCall711c52b2011-01-05 12:14:39 +00003181 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3182 VectorType::NeonPolyVector,
Bob Wilson4211bb62010-11-16 00:32:24 +00003183 "neon_polyvector_type");
3184 break;
John McCall04a67a62010-02-05 21:31:56 +00003185
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003186 case AttributeList::AT_opencl_image_access:
3187 HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
3188 break;
3189
John McCall711c52b2011-01-05 12:14:39 +00003190 FUNCTION_TYPE_ATTRS_CASELIST:
3191 // Never process function type attributes as part of the
3192 // declaration-specifiers.
3193 if (isDeclSpec)
3194 distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
3195
3196 // Otherwise, handle the possible delays.
3197 else if (!handleFunctionTypeAttr(state, attr, type))
3198 distributeFunctionTypeAttr(state, attr, type);
John Thompson6e132aa2009-12-04 21:51:28 +00003199 break;
Chris Lattner232e8822008-02-21 01:08:11 +00003200 }
John McCall711c52b2011-01-05 12:14:39 +00003201 } while ((attrs = next));
Chris Lattner232e8822008-02-21 01:08:11 +00003202}
3203
Mike Stump1eb44332009-09-09 15:08:12 +00003204/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003205///
3206/// This routine checks whether the type @p T is complete in any
3207/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00003208/// type, returns false. If @p T is a class template specialization,
3209/// this routine then attempts to perform class template
3210/// instantiation. If instantiation fails, or if @p T is incomplete
3211/// and cannot be completed, issues the diagnostic @p diag (giving it
3212/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003213///
3214/// @param Loc The location in the source that the incomplete type
3215/// diagnostic should refer to.
3216///
3217/// @param T The type that this routine is examining for completeness.
3218///
Mike Stump1eb44332009-09-09 15:08:12 +00003219/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00003220/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003221///
3222/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
3223/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003224bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00003225 const PartialDiagnostic &PD,
3226 std::pair<SourceLocation,
3227 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003228 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Douglas Gregor573d9c32009-10-21 23:19:44 +00003230 // FIXME: Add this assertion to make sure we always get instantiation points.
3231 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00003232 // FIXME: Add this assertion to help us flush out problems with
3233 // checking for dependent types and type-dependent expressions.
3234 //
Mike Stump1eb44332009-09-09 15:08:12 +00003235 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00003236 // "Can't ask whether a dependent type is complete");
3237
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003238 // If we have a complete type, we're done.
3239 if (!T->isIncompleteType())
3240 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00003241
Douglas Gregord475b8d2009-03-25 21:17:03 +00003242 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00003243 // class template specialization, or an array with known size of such,
3244 // try to instantiate it.
3245 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00003246 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00003247 MaybeTemplate = Array->getElementType();
3248 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00003249 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00003250 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00003251 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
3252 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003253 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00003254 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003255 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00003256 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
3257 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003258 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
3259 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00003260 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003261 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00003262 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00003263 return InstantiateClass(Loc, Rec, Pattern,
3264 getTemplateInstantiationArgs(Rec),
3265 TSK_ImplicitInstantiation,
3266 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00003267 }
3268 }
3269 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00003270
Douglas Gregor5842ba92009-08-24 15:23:48 +00003271 if (diag == 0)
3272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003273
John McCall916c8702010-11-16 01:44:35 +00003274 const TagType *Tag = T->getAs<TagType>();
Rafael Espindola01620702010-03-21 22:56:43 +00003275
3276 // Avoid diagnosing invalid decls as incomplete.
3277 if (Tag && Tag->getDecl()->isInvalidDecl())
3278 return true;
3279
John McCall916c8702010-11-16 01:44:35 +00003280 // Give the external AST source a chance to complete the type.
3281 if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
3282 Context.getExternalSource()->CompleteType(Tag->getDecl());
3283 if (!Tag->isIncompleteType())
3284 return false;
3285 }
3286
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003287 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003288 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003289
Anders Carlsson8c8d9192009-10-09 23:51:55 +00003290 // If we have a note, produce it.
3291 if (!Note.first.isInvalid())
3292 Diag(Note.first, Note.second);
3293
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003294 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00003295 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003296 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00003297 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003298 Tag->isBeingDefined() ? diag::note_type_being_defined
3299 : diag::note_forward_declaration)
3300 << QualType(Tag, 0);
3301
3302 return true;
3303}
Douglas Gregore6258932009-03-19 00:39:20 +00003304
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00003305bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3306 const PartialDiagnostic &PD) {
3307 return RequireCompleteType(Loc, T, PD,
3308 std::make_pair(SourceLocation(), PDiag(0)));
3309}
3310
3311bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3312 unsigned DiagID) {
3313 return RequireCompleteType(Loc, T, PDiag(DiagID),
3314 std::make_pair(SourceLocation(), PDiag(0)));
3315}
3316
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003317/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3318/// and qualified by the nested-name-specifier contained in SS.
3319QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3320 const CXXScopeSpec &SS, QualType T) {
3321 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00003322 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003323 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003324 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003325 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3326 else {
3327 if (Keyword == ETK_None)
3328 return T;
3329 NNS = 0;
3330 }
3331 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00003332}
Anders Carlssonaf017e62009-06-29 22:58:55 +00003333
John McCall2a984ca2010-10-12 00:20:44 +00003334QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
John McCallfb8721c2011-04-10 19:13:55 +00003335 ExprResult ER = CheckPlaceholderExpr(E);
John McCall2a984ca2010-10-12 00:20:44 +00003336 if (ER.isInvalid()) return QualType();
3337 E = ER.take();
3338
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003339 if (!E->isTypeDependent()) {
3340 QualType T = E->getType();
Fariborz Jahanianaca7f7b2010-10-06 00:23:25 +00003341 if (const TagType *TT = T->getAs<TagType>())
3342 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003343 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00003344 return Context.getTypeOfExprType(E);
3345}
3346
John McCall2a984ca2010-10-12 00:20:44 +00003347QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
John McCallfb8721c2011-04-10 19:13:55 +00003348 ExprResult ER = CheckPlaceholderExpr(E);
John McCall2a984ca2010-10-12 00:20:44 +00003349 if (ER.isInvalid()) return QualType();
3350 E = ER.take();
Douglas Gregor4b52e252009-12-21 23:17:24 +00003351
Anders Carlssonaf017e62009-06-29 22:58:55 +00003352 return Context.getDecltypeType(E);
3353}