blob: 7c96a3f2de3b2193e435bb654ec61663b28a7510 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000017#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000019#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000020#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000021#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000023#include "clang/Basic/PartialDiagnostic.h"
Charles Davisd18f9f92010-08-16 04:01:50 +000024#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000026#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000027#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Douglas Gregor2dc0e642009-03-23 23:06:20 +000030/// \brief Perform adjustment on the parameter type of a function.
31///
32/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000033/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
34/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000035QualType Sema::adjustParameterType(QualType T) {
36 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000037 // A declaration of a parameter as "array of type" shall be
38 // adjusted to "qualified pointer to type", where the type
39 // qualifiers (if any) are those specified within the [ and ] of
40 // the array type derivation.
41 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000042 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000043
44 // C99 6.7.5.3p8:
45 // A declaration of a parameter as "function returning type"
46 // shall be adjusted to "pointer to function returning type", as
47 // in 6.3.2.1.
48 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000049 return Context.getPointerType(T);
50
51 return T;
52}
53
Chris Lattner5db2bb12009-10-25 18:21:37 +000054
55
56/// isOmittedBlockReturnType - Return true if this declarator is missing a
57/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000060 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000061 return false;
62
63 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000064 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000065
66 if (D.getNumTypeObjects() == 1 &&
67 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000068 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000069
70 return false;
71}
72
John McCall711c52b2011-01-05 12:14:39 +000073// objc_gc applies to Objective-C pointers or, otherwise, to the
74// smallest available pointer type (i.e. 'void*' in 'void**').
75#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
76 case AttributeList::AT_objc_gc
John McCall04a67a62010-02-05 21:31:56 +000077
John McCall711c52b2011-01-05 12:14:39 +000078// Function type attributes.
79#define FUNCTION_TYPE_ATTRS_CASELIST \
80 case AttributeList::AT_noreturn: \
81 case AttributeList::AT_cdecl: \
82 case AttributeList::AT_fastcall: \
83 case AttributeList::AT_stdcall: \
84 case AttributeList::AT_thiscall: \
85 case AttributeList::AT_pascal: \
86 case AttributeList::AT_regparm
John McCall04a67a62010-02-05 21:31:56 +000087
John McCall711c52b2011-01-05 12:14:39 +000088namespace {
89 /// An object which stores processing state for the entire
90 /// GetTypeForDeclarator process.
91 class TypeProcessingState {
92 Sema &sema;
93
94 /// The declarator being processed.
95 Declarator &declarator;
96
97 /// The index of the declarator chunk we're currently processing.
98 /// May be the total number of valid chunks, indicating the
99 /// DeclSpec.
100 unsigned chunkIndex;
101
102 /// Whether there are non-trivial modifications to the decl spec.
103 bool trivial;
104
105 /// The original set of attributes on the DeclSpec.
106 llvm::SmallVector<AttributeList*, 2> savedAttrs;
107
108 /// A list of attributes to diagnose the uselessness of when the
109 /// processing is complete.
110 llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
111
112 public:
113 TypeProcessingState(Sema &sema, Declarator &declarator)
114 : sema(sema), declarator(declarator),
115 chunkIndex(declarator.getNumTypeObjects()),
116 trivial(true) {}
117
118 Sema &getSema() const {
119 return sema;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000120 }
John McCall711c52b2011-01-05 12:14:39 +0000121
122 Declarator &getDeclarator() const {
123 return declarator;
124 }
125
126 unsigned getCurrentChunkIndex() const {
127 return chunkIndex;
128 }
129
130 void setCurrentChunkIndex(unsigned idx) {
131 assert(idx <= declarator.getNumTypeObjects());
132 chunkIndex = idx;
133 }
134
135 AttributeList *&getCurrentAttrListRef() const {
136 assert(chunkIndex <= declarator.getNumTypeObjects());
137 if (chunkIndex == declarator.getNumTypeObjects())
138 return getMutableDeclSpec().getAttributes().getListRef();
139 return declarator.getTypeObject(chunkIndex).getAttrListRef();
140 }
141
142 /// Save the current set of attributes on the DeclSpec.
143 void saveDeclSpecAttrs() {
144 // Don't try to save them multiple times.
145 if (!savedAttrs.empty()) return;
146
147 DeclSpec &spec = getMutableDeclSpec();
148 for (AttributeList *attr = spec.getAttributes().getList(); attr;
149 attr = attr->getNext())
150 savedAttrs.push_back(attr);
151 trivial &= savedAttrs.empty();
152 }
153
154 /// Record that we had nowhere to put the given type attribute.
155 /// We will diagnose such attributes later.
156 void addIgnoredTypeAttr(AttributeList &attr) {
157 ignoredTypeAttrs.push_back(&attr);
158 }
159
160 /// Diagnose all the ignored type attributes, given that the
161 /// declarator worked out to the given type.
162 void diagnoseIgnoredTypeAttrs(QualType type) const {
163 for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
164 i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
165 i != e; ++i) {
166 AttributeList &attr = **i;
167 getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
168 << attr.getName() << type;
169 }
170 }
171
172 ~TypeProcessingState() {
173 if (trivial) return;
174
175 restoreDeclSpecAttrs();
176 }
177
178 private:
179 DeclSpec &getMutableDeclSpec() const {
180 return const_cast<DeclSpec&>(declarator.getDeclSpec());
181 }
182
183 void restoreDeclSpecAttrs() {
184 assert(!savedAttrs.empty());
185 getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
186 for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
187 savedAttrs[i]->setNext(savedAttrs[i+1]);
188 savedAttrs.back()->setNext(0);
189 }
190 };
191
192 /// Basically std::pair except that we really want to avoid an
193 /// implicit operator= for safety concerns. It's also a minor
194 /// link-time optimization for this to be a private type.
195 struct AttrAndList {
196 /// The attribute.
197 AttributeList &first;
198
199 /// The head of the list the attribute is currently in.
200 AttributeList *&second;
201
202 AttrAndList(AttributeList &attr, AttributeList *&head)
203 : first(attr), second(head) {}
204 };
John McCall04a67a62010-02-05 21:31:56 +0000205}
206
John McCall711c52b2011-01-05 12:14:39 +0000207namespace llvm {
208 template <> struct isPodLike<AttrAndList> {
209 static const bool value = true;
210 };
211}
212
213static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
214 attr.setNext(head);
215 head = &attr;
216}
217
218static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
219 if (head == &attr) {
220 head = attr.getNext();
221 return;
John McCall04a67a62010-02-05 21:31:56 +0000222 }
John McCall711c52b2011-01-05 12:14:39 +0000223
224 AttributeList *cur = head;
225 while (true) {
226 assert(cur && cur->getNext() && "ran out of attrs?");
227 if (cur->getNext() == &attr) {
228 cur->setNext(attr.getNext());
229 return;
230 }
231 cur = cur->getNext();
232 }
233}
234
235static void moveAttrFromListToList(AttributeList &attr,
236 AttributeList *&fromList,
237 AttributeList *&toList) {
238 spliceAttrOutOfList(attr, fromList);
239 spliceAttrIntoList(attr, toList);
240}
241
242static void processTypeAttrs(TypeProcessingState &state,
243 QualType &type, bool isDeclSpec,
244 AttributeList *attrs);
245
246static bool handleFunctionTypeAttr(TypeProcessingState &state,
247 AttributeList &attr,
248 QualType &type);
249
250static bool handleObjCGCTypeAttr(TypeProcessingState &state,
251 AttributeList &attr, QualType &type);
252
253static bool handleObjCPointerTypeAttr(TypeProcessingState &state,
254 AttributeList &attr, QualType &type) {
255 // Right now, we have exactly one of these attributes: objc_gc.
256 assert(attr.getKind() == AttributeList::AT_objc_gc);
257 return handleObjCGCTypeAttr(state, attr, type);
258}
259
260/// Given that an objc_gc attribute was written somewhere on a
261/// declaration *other* than on the declarator itself (for which, use
262/// distributeObjCPointerTypeAttrFromDeclarator), and given that it
263/// didn't apply in whatever position it was written in, try to move
264/// it to a more appropriate position.
265static void distributeObjCPointerTypeAttr(TypeProcessingState &state,
266 AttributeList &attr,
267 QualType type) {
268 Declarator &declarator = state.getDeclarator();
269 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
270 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
271 switch (chunk.Kind) {
272 case DeclaratorChunk::Pointer:
273 case DeclaratorChunk::BlockPointer:
274 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
275 chunk.getAttrListRef());
276 return;
277
278 case DeclaratorChunk::Paren:
279 case DeclaratorChunk::Array:
280 continue;
281
282 // Don't walk through these.
283 case DeclaratorChunk::Reference:
284 case DeclaratorChunk::Function:
285 case DeclaratorChunk::MemberPointer:
286 goto error;
287 }
288 }
289 error:
290
291 state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
292 << attr.getName() << type;
293}
294
295/// Distribute an objc_gc type attribute that was written on the
296/// declarator.
297static void
298distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state,
299 AttributeList &attr,
300 QualType &declSpecType) {
301 Declarator &declarator = state.getDeclarator();
302
303 // objc_gc goes on the innermost pointer to something that's not a
304 // pointer.
305 unsigned innermost = -1U;
306 bool considerDeclSpec = true;
307 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
308 DeclaratorChunk &chunk = declarator.getTypeObject(i);
309 switch (chunk.Kind) {
310 case DeclaratorChunk::Pointer:
311 case DeclaratorChunk::BlockPointer:
312 innermost = i;
John McCallae278a32011-01-12 00:34:59 +0000313 continue;
John McCall711c52b2011-01-05 12:14:39 +0000314
315 case DeclaratorChunk::Reference:
316 case DeclaratorChunk::MemberPointer:
317 case DeclaratorChunk::Paren:
318 case DeclaratorChunk::Array:
319 continue;
320
321 case DeclaratorChunk::Function:
322 considerDeclSpec = false;
323 goto done;
324 }
325 }
326 done:
327
328 // That might actually be the decl spec if we weren't blocked by
329 // anything in the declarator.
330 if (considerDeclSpec) {
331 if (handleObjCPointerTypeAttr(state, attr, declSpecType))
332 return;
333 }
334
335 // Otherwise, if we found an appropriate chunk, splice the attribute
336 // into it.
337 if (innermost != -1U) {
338 moveAttrFromListToList(attr, declarator.getAttrListRef(),
339 declarator.getTypeObject(innermost).getAttrListRef());
340 return;
341 }
342
343 // Otherwise, diagnose when we're done building the type.
344 spliceAttrOutOfList(attr, declarator.getAttrListRef());
345 state.addIgnoredTypeAttr(attr);
346}
347
348/// A function type attribute was written somewhere in a declaration
349/// *other* than on the declarator itself or in the decl spec. Given
350/// that it didn't apply in whatever position it was written in, try
351/// to move it to a more appropriate position.
352static void distributeFunctionTypeAttr(TypeProcessingState &state,
353 AttributeList &attr,
354 QualType type) {
355 Declarator &declarator = state.getDeclarator();
356
357 // Try to push the attribute from the return type of a function to
358 // the function itself.
359 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
360 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
361 switch (chunk.Kind) {
362 case DeclaratorChunk::Function:
363 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
364 chunk.getAttrListRef());
365 return;
366
367 case DeclaratorChunk::Paren:
368 case DeclaratorChunk::Pointer:
369 case DeclaratorChunk::BlockPointer:
370 case DeclaratorChunk::Array:
371 case DeclaratorChunk::Reference:
372 case DeclaratorChunk::MemberPointer:
373 continue;
374 }
375 }
376
377 state.getSema().Diag(attr.getLoc(), diag::warn_function_attribute_wrong_type)
378 << attr.getName() << type;
379}
380
381/// Try to distribute a function type attribute to the innermost
382/// function chunk or type. Returns true if the attribute was
383/// distributed, false if no location was found.
384static bool
385distributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
386 AttributeList &attr,
387 AttributeList *&attrList,
388 QualType &declSpecType) {
389 Declarator &declarator = state.getDeclarator();
390
391 // Put it on the innermost function chunk, if there is one.
392 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
393 DeclaratorChunk &chunk = declarator.getTypeObject(i);
394 if (chunk.Kind != DeclaratorChunk::Function) continue;
395
396 moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
397 return true;
398 }
399
400 return handleFunctionTypeAttr(state, attr, declSpecType);
401}
402
403/// A function type attribute was written in the decl spec. Try to
404/// apply it somewhere.
405static void
406distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
407 AttributeList &attr,
408 QualType &declSpecType) {
409 state.saveDeclSpecAttrs();
410
411 // Try to distribute to the innermost.
412 if (distributeFunctionTypeAttrToInnermost(state, attr,
413 state.getCurrentAttrListRef(),
414 declSpecType))
415 return;
416
417 // If that failed, diagnose the bad attribute when the declarator is
418 // fully built.
419 state.addIgnoredTypeAttr(attr);
420}
421
422/// A function type attribute was written on the declarator. Try to
423/// apply it somewhere.
424static void
425distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
426 AttributeList &attr,
427 QualType &declSpecType) {
428 Declarator &declarator = state.getDeclarator();
429
430 // Try to distribute to the innermost.
431 if (distributeFunctionTypeAttrToInnermost(state, attr,
432 declarator.getAttrListRef(),
433 declSpecType))
434 return;
435
436 // If that failed, diagnose the bad attribute when the declarator is
437 // fully built.
438 spliceAttrOutOfList(attr, declarator.getAttrListRef());
439 state.addIgnoredTypeAttr(attr);
440}
441
442/// \brief Given that there are attributes written on the declarator
443/// itself, try to distribute any type attributes to the appropriate
444/// declarator chunk.
445///
446/// These are attributes like the following:
447/// int f ATTR;
448/// int (f ATTR)();
449/// but not necessarily this:
450/// int f() ATTR;
451static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
452 QualType &declSpecType) {
453 // Collect all the type attributes from the declarator itself.
454 assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
455 AttributeList *attr = state.getDeclarator().getAttributes();
456 AttributeList *next;
457 do {
458 next = attr->getNext();
459
460 switch (attr->getKind()) {
461 OBJC_POINTER_TYPE_ATTRS_CASELIST:
462 distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
463 break;
464
465 FUNCTION_TYPE_ATTRS_CASELIST:
466 distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
467 break;
468
469 default:
470 break;
471 }
472 } while ((attr = next));
473}
474
475/// Add a synthetic '()' to a block-literal declarator if it is
476/// required, given the return type.
477static void maybeSynthesizeBlockSignature(TypeProcessingState &state,
478 QualType declSpecType) {
479 Declarator &declarator = state.getDeclarator();
480
481 // First, check whether the declarator would produce a function,
482 // i.e. whether the innermost semantic chunk is a function.
483 if (declarator.isFunctionDeclarator()) {
484 // If so, make that declarator a prototyped declarator.
485 declarator.getFunctionTypeInfo().hasPrototype = true;
486 return;
487 }
488
John McCallda263792011-02-08 01:59:10 +0000489 // If there are any type objects, the type as written won't name a
490 // function, regardless of the decl spec type. This is because a
491 // block signature declarator is always an abstract-declarator, and
492 // abstract-declarators can't just be parentheses chunks. Therefore
493 // we need to build a function chunk unless there are no type
494 // objects and the decl spec type is a function.
John McCall711c52b2011-01-05 12:14:39 +0000495 if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
496 return;
497
John McCallda263792011-02-08 01:59:10 +0000498 // Note that there *are* cases with invalid declarators where
499 // declarators consist solely of parentheses. In general, these
500 // occur only in failed efforts to make function declarators, so
501 // faking up the function chunk is still the right thing to do.
John McCall711c52b2011-01-05 12:14:39 +0000502
503 // Otherwise, we need to fake up a function declarator.
504 SourceLocation loc = declarator.getSourceRange().getBegin();
505
506 // ...and *prepend* it to the declarator.
507 declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
508 ParsedAttributes(),
509 /*proto*/ true,
510 /*variadic*/ false, SourceLocation(),
511 /*args*/ 0, 0,
512 /*type quals*/ 0,
Douglas Gregor83f51722011-01-26 03:43:54 +0000513 /*ref-qualifier*/true, SourceLocation(),
John McCall711c52b2011-01-05 12:14:39 +0000514 /*EH*/ false, SourceLocation(), false, 0, 0, 0,
515 /*parens*/ loc, loc,
516 declarator));
517
518 // For consistency, make sure the state still has us as processing
519 // the decl spec.
520 assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
521 state.setCurrentChunkIndex(declarator.getNumTypeObjects());
John McCall04a67a62010-02-05 21:31:56 +0000522}
523
Douglas Gregor930d8b52009-01-30 22:09:00 +0000524/// \brief Convert the specified declspec to the appropriate type
525/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000526/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000527/// \returns The type described by the declaration specifiers. This function
528/// never returns null.
John McCall711c52b2011-01-05 12:14:39 +0000529static QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
531 // checking.
John McCall711c52b2011-01-05 12:14:39 +0000532
533 Declarator &declarator = state.getDeclarator();
534 const DeclSpec &DS = declarator.getDeclSpec();
535 SourceLocation DeclLoc = declarator.getIdentifierLoc();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000536 if (DeclLoc.isInvalid())
537 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000538
John McCall711c52b2011-01-05 12:14:39 +0000539 ASTContext &Context = S.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner5db2bb12009-10-25 18:21:37 +0000541 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000543 case DeclSpec::TST_void:
544 Result = Context.VoidTy;
545 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case DeclSpec::TST_char:
547 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000548 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000550 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 else {
552 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
553 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000554 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
Chris Lattner958858e2008-02-20 21:40:32 +0000556 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000557 case DeclSpec::TST_wchar:
558 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
559 Result = Context.WCharTy;
560 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
John McCall711c52b2011-01-05 12:14:39 +0000561 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000562 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000563 Result = Context.getSignedWCharType();
564 } else {
565 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
566 "Unknown TSS value");
John McCall711c52b2011-01-05 12:14:39 +0000567 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000568 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000569 Result = Context.getUnsignedWCharType();
570 }
571 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000572 case DeclSpec::TST_char16:
573 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
574 "Unknown TSS value");
575 Result = Context.Char16Ty;
576 break;
577 case DeclSpec::TST_char32:
578 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
579 "Unknown TSS value");
580 Result = Context.Char32Ty;
581 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000582 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000583 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000584 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000585 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
586 (ObjCProtocolDecl**)PQ,
587 DS.getNumProtocolQualifiers());
588 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000589 break;
590 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000591
592 // If this is a missing declspec in a block literal return context, then it
593 // is inferred from the return statements inside the block.
John McCall711c52b2011-01-05 12:14:39 +0000594 if (isOmittedBlockReturnType(declarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000595 Result = Context.DependentTy;
596 break;
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattnerd658b562008-04-05 06:32:51 +0000599 // Unspecified typespec defaults to int in C90. However, the C90 grammar
600 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
601 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
602 // Note that the one exception to this is function definitions, which are
603 // allowed to be completely missing a declspec. This is handled in the
604 // parser already though by it pretending to have seen an 'int' in this
605 // case.
John McCall711c52b2011-01-05 12:14:39 +0000606 if (S.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000607 // In C89 mode, we only warn if there is a completely missing declspec
608 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000609 if (DS.isEmpty()) {
John McCall711c52b2011-01-05 12:14:39 +0000610 S.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000611 << DS.getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000612 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000613 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000614 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000615 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
616 // "At least one type specifier shall be given in the declaration
617 // specifiers in each declaration, and in the specifier-qualifier list in
618 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000619 // FIXME: Does Microsoft really have the implicit int extension in C++?
John McCall711c52b2011-01-05 12:14:39 +0000620 if (S.getLangOptions().CPlusPlus &&
621 !S.getLangOptions().Microsoft) {
622 S.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000623 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattnerb78d8332009-06-26 04:45:06 +0000625 // When this occurs in C++ code, often something is very broken with the
626 // value being declared, poison it as invalid so we don't get chains of
627 // errors.
John McCall711c52b2011-01-05 12:14:39 +0000628 declarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000629 } else {
John McCall711c52b2011-01-05 12:14:39 +0000630 S.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000631 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000632 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
635 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000636 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
638 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000639 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
640 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
641 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000642 case DeclSpec::TSW_longlong:
643 Result = Context.LongLongTy;
644
645 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000646 if (!S.getLangOptions().C99 &&
647 !S.getLangOptions().CPlusPlus0x)
648 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000649 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
651 } else {
652 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000653 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
654 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
655 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000656 case DeclSpec::TSW_longlong:
657 Result = Context.UnsignedLongLongTy;
658
659 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000660 if (!S.getLangOptions().C99 &&
661 !S.getLangOptions().CPlusPlus0x)
662 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000663 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 }
665 }
Chris Lattner958858e2008-02-20 21:40:32 +0000666 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000667 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000668 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000669 case DeclSpec::TST_double:
670 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000671 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000672 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000673 Result = Context.DoubleTy;
Peter Collingbourne39d3e7a2011-02-15 19:46:23 +0000674
675 if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
676 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
677 declarator.setInvalidType(true);
678 }
Chris Lattner958858e2008-02-20 21:40:32 +0000679 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000680 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 case DeclSpec::TST_decimal32: // _Decimal32
682 case DeclSpec::TST_decimal64: // _Decimal64
683 case DeclSpec::TST_decimal128: // _Decimal128
John McCall711c52b2011-01-05 12:14:39 +0000684 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000685 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000686 declarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000687 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000688 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 case DeclSpec::TST_enum:
690 case DeclSpec::TST_union:
691 case DeclSpec::TST_struct: {
John McCallb3d87482010-08-24 05:47:05 +0000692 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000693 if (!D) {
694 // This can happen in C++ with ambiguous lookups.
695 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000696 declarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000697 break;
698 }
699
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000700 // If the type is deprecated or unavailable, diagnose it.
John McCall711c52b2011-01-05 12:14:39 +0000701 S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000704 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000707 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000708
709 // In C++, make an ElaboratedType.
John McCall711c52b2011-01-05 12:14:39 +0000710 if (S.getLangOptions().CPlusPlus) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000711 ElaboratedTypeKeyword Keyword
712 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
John McCall711c52b2011-01-05 12:14:39 +0000713 Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
John McCall2191b202009-09-05 06:31:47 +0000714 }
Chris Lattner5153ee62009-04-25 08:47:54 +0000715 if (D->isInvalidDecl())
John McCall711c52b2011-01-05 12:14:39 +0000716 declarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000717 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000718 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000719 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
721 DS.getTypeSpecSign() == 0 &&
722 "Can't handle qualifiers on typedef names yet!");
John McCall711c52b2011-01-05 12:14:39 +0000723 Result = S.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000724 if (Result.isNull())
John McCall711c52b2011-01-05 12:14:39 +0000725 declarator.setInvalidType(true);
John McCall27940d22010-07-30 05:17:22 +0000726 else if (DeclSpec::ProtocolQualifierListTy PQ
727 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000728 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
729 // Silently drop any existing protocol qualifiers.
730 // TODO: determine whether that's the right thing to do.
731 if (ObjT->getNumProtocols())
732 Result = ObjT->getBaseType();
733
734 if (DS.getNumProtocolQualifiers())
735 Result = Context.getObjCObjectType(Result,
736 (ObjCProtocolDecl**) PQ,
737 DS.getNumProtocolQualifiers());
738 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000739 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000740 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
741 (ObjCProtocolDecl**) PQ,
742 DS.getNumProtocolQualifiers());
743 Result = Context.getObjCObjectPointerType(Result);
744 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000745 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000746 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
747 (ObjCProtocolDecl**) PQ,
748 DS.getNumProtocolQualifiers());
749 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000750 } else {
John McCall711c52b2011-01-05 12:14:39 +0000751 S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000752 << DS.getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +0000753 declarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000754 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000758 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
Chris Lattner958858e2008-02-20 21:40:32 +0000760 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000761 // FIXME: Preserve type source info.
John McCall711c52b2011-01-05 12:14:39 +0000762 Result = S.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000763 assert(!Result.isNull() && "Didn't get a type for typeof?");
Fariborz Jahanian730e1752010-10-06 17:00:02 +0000764 if (!Result->isDependentType())
765 if (const TagType *TT = Result->getAs<TagType>())
John McCall711c52b2011-01-05 12:14:39 +0000766 S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
Steve Naroffd1861fd2007-07-31 12:34:36 +0000767 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000768 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000769 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000770 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000771 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000772 assert(E && "Didn't get an expression for typeof?");
773 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000774 Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
Douglas Gregor4b52e252009-12-21 23:17:24 +0000775 if (Result.isNull()) {
776 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000777 declarator.setInvalidType(true);
Douglas Gregor4b52e252009-12-21 23:17:24 +0000778 }
Chris Lattner958858e2008-02-20 21:40:32 +0000779 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000780 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000781 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000782 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000783 assert(E && "Didn't get an expression for decltype?");
784 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000785 Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
Anders Carlssonaf017e62009-06-29 22:58:55 +0000786 if (Result.isNull()) {
787 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000788 declarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000789 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000790 break;
791 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000792 case DeclSpec::TST_auto: {
793 // TypeQuals handled by caller.
Richard Smith34b41d92011-02-20 03:19:35 +0000794 Result = Context.getAutoType(QualType());
Anders Carlssone89d1592009-06-26 18:41:36 +0000795 break;
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor809070a2009-02-18 17:45:20 +0000798 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000799 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000800 declarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000801 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattner958858e2008-02-20 21:40:32 +0000804 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000805 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
John McCall711c52b2011-01-05 12:14:39 +0000806 if (S.getLangOptions().Freestanding)
807 S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000808 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000809 } else if (DS.isTypeAltiVecVector()) {
810 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
811 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Bob Wilsone86d78c2010-11-10 21:56:12 +0000812 VectorType::VectorKind VecKind = VectorType::AltiVecVector;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000813 if (DS.isTypeAltiVecPixel())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000814 VecKind = VectorType::AltiVecPixel;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000815 else if (DS.isTypeAltiVecBool())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000816 VecKind = VectorType::AltiVecBool;
817 Result = Context.getVectorType(Result, 128/typeSize, VecKind);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Argyrios Kyrtzidis47423bd2010-09-23 09:40:31 +0000820 // FIXME: Imaginary.
821 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
John McCall711c52b2011-01-05 12:14:39 +0000822 S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
John McCall711c52b2011-01-05 12:14:39 +0000824 // Before we process any type attributes, synthesize a block literal
825 // function declarator if necessary.
826 if (declarator.getContext() == Declarator::BlockLiteralContext)
827 maybeSynthesizeBlockSignature(state, Result);
828
829 // Apply any type attributes from the decl spec. This may cause the
830 // list of type attributes to be temporarily saved while the type
831 // attributes are pushed around.
832 if (AttributeList *attrs = DS.getAttributes().getList())
833 processTypeAttrs(state, Result, true, attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattner96b77fc2008-04-02 06:50:17 +0000835 // Apply const/volatile/restrict qualifiers to T.
836 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
837
838 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
839 // or incomplete types shall not be restrict-qualified." C++ also allows
840 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000841 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000842 if (Result->isAnyPointerType() || Result->isReferenceType()) {
843 QualType EltTy;
844 if (Result->isObjCObjectPointerType())
845 EltTy = Result;
846 else
847 EltTy = Result->isPointerType() ?
848 Result->getAs<PointerType>()->getPointeeType() :
849 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregorbad0e652009-03-24 20:32:41 +0000851 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000852 // incomplete type.
853 if (!EltTy->isIncompleteOrObjectType()) {
John McCall711c52b2011-01-05 12:14:39 +0000854 S.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000855 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000856 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000857 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000858 }
859 } else {
John McCall711c52b2011-01-05 12:14:39 +0000860 S.Diag(DS.getRestrictSpecLoc(),
861 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000862 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000863 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000864 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner96b77fc2008-04-02 06:50:17 +0000867 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
868 // of a function type includes any type qualifiers, the behavior is
869 // undefined."
870 if (Result->isFunctionType() && TypeQuals) {
871 // Get some location to point at, either the C or V location.
872 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000873 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000874 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000875 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000876 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000877 else {
878 assert((TypeQuals & DeclSpec::TQ_restrict) &&
879 "Has CVR quals but not C, V, or R?");
880 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000881 }
John McCall711c52b2011-01-05 12:14:39 +0000882 S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000883 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000886 // C++ [dcl.ref]p1:
887 // Cv-qualified references are ill-formed except when the
888 // cv-qualifiers are introduced through the use of a typedef
889 // (7.1.3) or of a template type argument (14.3), in which
890 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000891 // FIXME: Shouldn't we be checking SCS_typedef here?
892 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000893 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000894 TypeQuals &= ~DeclSpec::TQ_const;
895 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000896 }
897
John McCall0953e762009-09-24 19:53:00 +0000898 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
899 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000900 }
John McCall0953e762009-09-24 19:53:00 +0000901
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000902 return Result;
903}
904
Douglas Gregorcd281c32009-02-28 00:25:32 +0000905static std::string getPrintableNameForEntity(DeclarationName Entity) {
906 if (Entity)
907 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregorcd281c32009-02-28 00:25:32 +0000909 return "type name";
910}
911
John McCall28654742010-06-05 06:41:15 +0000912QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
913 Qualifiers Qs) {
914 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
915 // object or incomplete types shall not be restrict-qualified."
916 if (Qs.hasRestrict()) {
917 unsigned DiagID = 0;
918 QualType ProblemTy;
919
920 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
921 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
922 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
923 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
924 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
925 }
926 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
927 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
928 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
929 ProblemTy = T->getAs<PointerType>()->getPointeeType();
930 }
931 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
932 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
933 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
934 ProblemTy = T->getAs<PointerType>()->getPointeeType();
935 }
936 } else if (!Ty->isDependentType()) {
937 // FIXME: this deserves a proper diagnostic
938 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
939 ProblemTy = T;
940 }
941
942 if (DiagID) {
943 Diag(Loc, DiagID) << ProblemTy;
944 Qs.removeRestrict();
945 }
946 }
947
948 return Context.getQualifiedType(T, Qs);
949}
950
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000951/// \brief Build a paren type including \p T.
952QualType Sema::BuildParenType(QualType T) {
953 return Context.getParenType(T);
954}
955
Douglas Gregorcd281c32009-02-28 00:25:32 +0000956/// \brief Build a pointer type.
957///
958/// \param T The type to which we'll be building a pointer.
959///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000960/// \param Loc The location of the entity whose type involves this
961/// pointer type or, if there is no such entity, the location of the
962/// type that will have pointer type.
963///
964/// \param Entity The name of the entity that involves the pointer
965/// type, if known.
966///
967/// \returns A suitable pointer type, if there are no
968/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000969QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000970 SourceLocation Loc, DeclarationName Entity) {
971 if (T->isReferenceType()) {
972 // C++ 8.3.2p4: There shall be no ... pointers to references ...
973 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000974 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000975 return QualType();
976 }
977
John McCallc12c5bb2010-05-15 11:32:37 +0000978 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +0000979
Douglas Gregorcd281c32009-02-28 00:25:32 +0000980 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +0000981 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000982}
983
984/// \brief Build a reference type.
985///
986/// \param T The type to which we'll be building a reference.
987///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000988/// \param Loc The location of the entity whose type involves this
989/// reference type or, if there is no such entity, the location of the
990/// type that will have reference type.
991///
992/// \param Entity The name of the entity that involves the reference
993/// type, if known.
994///
995/// \returns A suitable reference type, if there are no
996/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000997QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +0000998 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +0000999 DeclarationName Entity) {
Douglas Gregor69d83162011-01-20 16:08:06 +00001000 // C++0x [dcl.ref]p6:
1001 // If a typedef (7.1.3), a type template-parameter (14.3.1), or a
1002 // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
1003 // type T, an attempt to create the type "lvalue reference to cv TR" creates
1004 // the type "lvalue reference to T", while an attempt to create the type
1005 // "rvalue reference to cv TR" creates the type TR.
John McCall54e14c42009-10-22 22:37:11 +00001006 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
1007
John McCall54e14c42009-10-22 22:37:11 +00001008 // C++ [dcl.ref]p4: There shall be no references to references.
1009 //
1010 // According to C++ DR 106, references to references are only
1011 // diagnosed when they are written directly (e.g., "int & &"),
1012 // but not when they happen via a typedef:
1013 //
1014 // typedef int& intref;
1015 // typedef intref& intref2;
1016 //
1017 // Parser::ParseDeclaratorInternal diagnoses the case where
1018 // references are written directly; here, we handle the
Douglas Gregor69d83162011-01-20 16:08:06 +00001019 // collapsing of references-to-references as described in C++0x.
1020 // DR 106 and 540 introduce reference-collapsing into C++98/03.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001021
1022 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +00001023 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +00001024 // is ill-formed.
1025 if (T->isVoidType()) {
1026 Diag(Loc, diag::err_reference_to_void);
1027 return QualType();
1028 }
1029
Douglas Gregorcd281c32009-02-28 00:25:32 +00001030 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001031 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +00001032 return Context.getLValueReferenceType(T, SpelledAsLValue);
1033 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001034}
1035
1036/// \brief Build an array type.
1037///
1038/// \param T The type of each element in the array.
1039///
1040/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +00001041///
1042/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001043///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001044/// \param Loc The location of the entity whose type involves this
1045/// array type or, if there is no such entity, the location of the
1046/// type that will have array type.
1047///
1048/// \param Entity The name of the entity that involves the array
1049/// type, if known.
1050///
1051/// \returns A suitable array type, if there are no errors. Otherwise,
1052/// returns a NULL type.
1053QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1054 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001055 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001056
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001057 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001058 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +00001059 // C++ [dcl.array]p1:
1060 // T is called the array element type; this type shall not be a reference
1061 // type, the (possibly cv-qualified) type void, a function type or an
1062 // abstract class type.
1063 //
1064 // Note: function types are handled in the common path with C.
1065 if (T->isReferenceType()) {
1066 Diag(Loc, diag::err_illegal_decl_array_of_references)
1067 << getPrintableNameForEntity(Entity) << T;
1068 return QualType();
1069 }
1070
Sebastian Redl923d56d2009-11-05 15:52:31 +00001071 if (T->isVoidType()) {
1072 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1073 return QualType();
1074 }
Douglas Gregor138bb232010-04-27 19:38:14 +00001075
1076 if (RequireNonAbstractType(Brackets.getBegin(), T,
1077 diag::err_array_of_abstract_type))
1078 return QualType();
1079
Sebastian Redl923d56d2009-11-05 15:52:31 +00001080 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +00001081 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1082 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +00001083 if (RequireCompleteType(Loc, T,
1084 diag::err_illegal_decl_array_incomplete_type))
1085 return QualType();
1086 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001087
1088 if (T->isFunctionType()) {
1089 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +00001090 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001091 return QualType();
1092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Richard Smith34b41d92011-02-20 03:19:35 +00001094 if (T->getContainedAutoType()) {
1095 Diag(Loc, diag::err_illegal_decl_array_of_auto)
1096 << getPrintableNameForEntity(Entity) << T;
Anders Carlssone7cf07d2009-06-26 19:33:28 +00001097 return QualType();
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Ted Kremenek6217b802009-07-29 21:53:49 +00001100 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001101 // If the element type is a struct or union that contains a variadic
1102 // array, accept it as a GNU extension: C99 6.7.2.1p2.
1103 if (EltTy->getDecl()->hasFlexibleArrayMember())
1104 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +00001105 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +00001106 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1107 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
John McCall5e3c67b2010-12-15 04:42:30 +00001110 // Do lvalue-to-rvalue conversions on the array size expression.
1111 if (ArraySize && !ArraySize->isRValue())
1112 DefaultLvalueConversion(ArraySize);
1113
Douglas Gregorcd281c32009-02-28 00:25:32 +00001114 // C99 6.7.5.2p1: The size expression shall have integer type.
John McCall5e3c67b2010-12-15 04:42:30 +00001115 // TODO: in theory, if we were insane, we could allow contextual
1116 // conversions to integer type here.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001117 if (ArraySize && !ArraySize->isTypeDependent() &&
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001118 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001119 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1120 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001121 return QualType();
1122 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001123 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +00001124 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001125 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001126 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001127 else
1128 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001129 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001130 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001131 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +00001132 (!T->isDependentType() && !T->isIncompleteType() &&
1133 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001134 // Per C99, a variable array is an array with either a non-constant
1135 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001136 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001137 } else {
1138 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1139 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +00001140 if (ConstVal.isSigned() && ConstVal.isNegative()) {
Chandler Carruthb2b5cc02011-01-04 04:44:35 +00001141 if (Entity)
1142 Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1143 << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1144 else
1145 Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1146 << ArraySize->getSourceRange();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001147 return QualType();
1148 }
1149 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001150 // GCC accepts zero sized static arrays. We allow them when
1151 // we're not in a SFINAE context.
1152 Diag(ArraySize->getLocStart(),
1153 isSFINAEContext()? diag::err_typecheck_zero_array_size
1154 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +00001155 << ArraySize->getSourceRange();
Douglas Gregor2767ce22010-08-18 00:39:00 +00001156 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1157 !T->isIncompleteType()) {
1158 // Is the array too large?
1159 unsigned ActiveSizeBits
1160 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1161 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
1162 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
1163 << ConstVal.toString(10)
1164 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001165 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001166
John McCall46a617a2009-10-16 00:14:28 +00001167 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001168 }
David Chisnallaf407762010-01-11 23:08:08 +00001169 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1170 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001171 if (T->isVariableArrayType()) {
1172 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +00001173 if (!T->isDependentType() &&
1174 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001175 Diag(Loc, diag::err_vla_non_pod)
1176 << Context.getBaseElementType(T);
1177 return QualType();
1178 }
Douglas Gregora481ec42010-05-23 19:57:01 +00001179 // Prohibit the use of VLAs during template argument deduction.
1180 else if (isSFINAEContext()) {
1181 Diag(Loc, diag::err_vla_in_sfinae);
1182 return QualType();
1183 }
Douglas Gregor0fddb972010-05-22 16:17:30 +00001184 // Just extwarn about VLAs.
1185 else
1186 Diag(Loc, diag::ext_vla);
1187 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +00001188 Diag(Loc,
1189 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1190 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001191 }
1192
1193 return T;
1194}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001195
1196/// \brief Build an ext-vector type.
1197///
1198/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +00001199QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001200 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001201 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1202 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +00001203 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001204 !T->isIntegerType() && !T->isRealFloatingType()) {
1205 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
1206 return QualType();
1207 }
1208
John McCall9ae2f072010-08-23 23:25:46 +00001209 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001210 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +00001211 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001212 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +00001213 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001214 return QualType();
1215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
1217 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001218 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +00001219 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
1220
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001221 if (vectorSize == 0) {
1222 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +00001223 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001224 return QualType();
1225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001227 if (!T->isDependentType())
1228 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +00001229 }
1230
John McCall9ae2f072010-08-23 23:25:46 +00001231 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001232}
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor724651c2009-02-28 01:04:19 +00001234/// \brief Build a function type.
1235///
1236/// This routine checks the function type according to C++ rules and
1237/// under the assumption that the result type and parameter types have
1238/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +00001239/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +00001240/// simpler form that is only suitable for this narrow use case.
1241///
1242/// \param T The return type of the function.
1243///
1244/// \param ParamTypes The parameter types of the function. This array
1245/// will be modified to account for adjustments to the types of the
1246/// function parameters.
1247///
1248/// \param NumParamTypes The number of parameter types in ParamTypes.
1249///
1250/// \param Variadic Whether this is a variadic function type.
1251///
1252/// \param Quals The cvr-qualifiers to be applied to the function type.
1253///
1254/// \param Loc The location of the entity whose type involves this
1255/// function type or, if there is no such entity, the location of the
1256/// type that will have function type.
1257///
1258/// \param Entity The name of the entity that involves the function
1259/// type, if known.
1260///
1261/// \returns A suitable function type, if there are no
1262/// errors. Otherwise, returns a NULL type.
1263QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001264 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +00001265 unsigned NumParamTypes,
1266 bool Variadic, unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00001267 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00001268 SourceLocation Loc, DeclarationName Entity,
John McCalle23cf432010-12-14 08:05:40 +00001269 FunctionType::ExtInfo Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001270 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001271 Diag(Loc, diag::err_func_returning_array_function)
1272 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +00001273 return QualType();
1274 }
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001275
Douglas Gregor724651c2009-02-28 01:04:19 +00001276 bool Invalid = false;
1277 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001278 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
1279 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001280 Diag(Loc, diag::err_param_with_void_type);
1281 Invalid = true;
1282 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001283
John McCall54e14c42009-10-22 22:37:11 +00001284 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +00001285 }
1286
1287 if (Invalid)
1288 return QualType();
1289
John McCalle23cf432010-12-14 08:05:40 +00001290 FunctionProtoType::ExtProtoInfo EPI;
1291 EPI.Variadic = Variadic;
1292 EPI.TypeQuals = Quals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001293 EPI.RefQualifier = RefQualifier;
John McCalle23cf432010-12-14 08:05:40 +00001294 EPI.ExtInfo = Info;
1295
1296 return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
Douglas Gregor724651c2009-02-28 01:04:19 +00001297}
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Douglas Gregor949bf692009-06-09 22:17:39 +00001299/// \brief Build a member pointer type \c T Class::*.
1300///
1301/// \param T the type to which the member pointer refers.
1302/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +00001303/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +00001304/// \param Loc the location where this type begins
1305/// \param Entity the name of the entity that will have this member pointer type
1306///
1307/// \returns a member pointer type, if successful, or a NULL type if there was
1308/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001309QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +00001310 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +00001311 DeclarationName Entity) {
1312 // Verify that we're not building a pointer to pointer to function with
1313 // exception specification.
1314 if (CheckDistantExceptionSpec(T)) {
1315 Diag(Loc, diag::err_distant_exception_spec);
1316
1317 // FIXME: If we're doing this as part of template instantiation,
1318 // we should return immediately.
1319
1320 // Build the type anyway, but use the canonical type so that the
1321 // exception specifiers are stripped off.
1322 T = Context.getCanonicalType(T);
1323 }
1324
Sebastian Redl73780122010-06-09 21:19:43 +00001325 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +00001326 // with reference type, or "cv void."
1327 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +00001328 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001329 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +00001330 return QualType();
1331 }
1332
1333 if (T->isVoidType()) {
1334 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1335 << (Entity? Entity.getAsString() : "type name");
1336 return QualType();
1337 }
1338
Douglas Gregor949bf692009-06-09 22:17:39 +00001339 if (!Class->isDependentType() && !Class->isRecordType()) {
1340 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1341 return QualType();
1342 }
1343
Charles Davisd18f9f92010-08-16 04:01:50 +00001344 // In the Microsoft ABI, the class is allowed to be an incomplete
1345 // type. In such cases, the compiler makes a worst-case assumption.
1346 // We make no such assumption right now, so emit an error if the
1347 // class isn't a complete type.
Charles Davis20cf7172010-08-19 02:18:14 +00001348 if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
Charles Davisd18f9f92010-08-16 04:01:50 +00001349 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1350 return QualType();
1351
John McCall28654742010-06-05 06:41:15 +00001352 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +00001353}
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Anders Carlsson9a917e42009-06-12 22:56:54 +00001355/// \brief Build a block pointer type.
1356///
1357/// \param T The type to which we'll be building a block pointer.
1358///
John McCall0953e762009-09-24 19:53:00 +00001359/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +00001360///
1361/// \param Loc The location of the entity whose type involves this
1362/// block pointer type or, if there is no such entity, the location of the
1363/// type that will have block pointer type.
1364///
1365/// \param Entity The name of the entity that involves the block pointer
1366/// type, if known.
1367///
1368/// \returns A suitable block pointer type, if there are no
1369/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +00001370QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001371 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +00001372 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001373 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +00001374 Diag(Loc, diag::err_nonfunction_block_type);
1375 return QualType();
1376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
John McCall28654742010-06-05 06:41:15 +00001378 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +00001379}
1380
John McCallb3d87482010-08-24 05:47:05 +00001381QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1382 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001383 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +00001384 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001385 return QualType();
1386 }
1387
John McCalla93c9342009-12-07 02:54:59 +00001388 TypeSourceInfo *DI = 0;
John McCallf4c73712011-01-19 06:33:43 +00001389 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001390 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +00001391 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
John McCalla93c9342009-12-07 02:54:59 +00001394 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001395 return QT;
1396}
1397
Mike Stump98eb8a72009-02-04 22:31:32 +00001398/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001399/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +00001400///
1401/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1402/// owns the declaration of a type (e.g., the definition of a struct
1403/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +00001404///
1405/// The result of this call will never be null, but the associated
1406/// type may be a null type if there's an unrecoverable error.
1407TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Richard Smith34b41d92011-02-20 03:19:35 +00001408 TagDecl **OwnedDecl,
1409 bool AutoAllowedInTypeName) {
Douglas Gregor930d8b52009-01-30 22:09:00 +00001410 // Determine the type of the declarator. Not all forms of declarator
1411 // have a type.
1412 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +00001413 TypeSourceInfo *ReturnTypeInfo = 0;
John McCall711c52b2011-01-05 12:14:39 +00001414
1415 TypeProcessingState state(*this, D);
John McCall04a67a62010-02-05 21:31:56 +00001416
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001417 switch (D.getName().getKind()) {
1418 case UnqualifiedId::IK_Identifier:
1419 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +00001420 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001421 case UnqualifiedId::IK_TemplateId:
John McCall711c52b2011-01-05 12:14:39 +00001422 T = ConvertDeclSpecToType(*this, state);
Chris Lattner5db2bb12009-10-25 18:21:37 +00001423
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001424 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
John McCallb3d87482010-08-24 05:47:05 +00001425 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregorb37b6482010-02-12 17:40:34 +00001426 // Owned is embedded if it was defined here, or if it is the
1427 // very first (i.e., canonical) declaration of this tag type.
1428 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
1429 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001430 if (OwnedDecl) *OwnedDecl = Owned;
1431 }
Douglas Gregor930d8b52009-01-30 22:09:00 +00001432 break;
1433
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001434 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001435 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001436 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +00001437 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +00001438 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +00001439 T = Context.VoidTy;
1440 break;
Douglas Gregor48026d22010-01-11 18:40:55 +00001441
1442 case UnqualifiedId::IK_ConversionFunctionId:
1443 // The result type of a conversion function is the type that it
1444 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001445 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +00001446 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +00001447 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +00001448 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00001449
John McCall711c52b2011-01-05 12:14:39 +00001450 if (D.getAttributes())
1451 distributeTypeAttrsFromDeclarator(state, T);
1452
Richard Smithe7397c62011-02-22 00:36:53 +00001453 // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
Richard Smith8110f042011-02-22 01:22:29 +00001454 // In C++0x, a function declarator using 'auto' must have a trailing return
1455 // type (this is checked later) and we can skip this. In other languages
1456 // using auto, we need to check regardless.
Richard Smith34b41d92011-02-20 03:19:35 +00001457 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
Richard Smith8110f042011-02-22 01:22:29 +00001458 (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001459 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001461 switch (D.getContext()) {
1462 case Declarator::KNRTypeListContext:
1463 assert(0 && "K&R type lists aren't allowed in C++");
1464 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001465 case Declarator::PrototypeContext:
1466 Error = 0; // Function prototype
1467 break;
1468 case Declarator::MemberContext:
1469 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001470 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1471 case TTK_Struct: Error = 1; /* Struct member */ break;
1472 case TTK_Union: Error = 2; /* Union member */ break;
1473 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001474 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001475 break;
1476 case Declarator::CXXCatchContext:
1477 Error = 4; // Exception declaration
1478 break;
1479 case Declarator::TemplateParamContext:
1480 Error = 5; // Template parameter
1481 break;
1482 case Declarator::BlockLiteralContext:
Richard Smith34b41d92011-02-20 03:19:35 +00001483 Error = 6; // Block literal
1484 break;
1485 case Declarator::TemplateTypeArgContext:
1486 Error = 7; // Template type argument
1487 break;
1488 case Declarator::TypeNameContext:
1489 if (!AutoAllowedInTypeName)
Richard Smith8110f042011-02-22 01:22:29 +00001490 Error = 10; // Generic
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001491 break;
1492 case Declarator::FileContext:
1493 case Declarator::BlockContext:
1494 case Declarator::ForContext:
1495 case Declarator::ConditionContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001496 break;
1497 }
1498
Richard Smithddc83f92011-02-21 23:18:00 +00001499 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1500 Error = 8;
1501
Richard Smith8110f042011-02-22 01:22:29 +00001502 // In Objective-C it is an error to use 'auto' on a function declarator.
1503 if (D.isFunctionDeclarator())
1504 Error = 9;
1505
Richard Smithe7397c62011-02-22 00:36:53 +00001506 // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1507 // contains a trailing return type. That is only legal at the outermost
1508 // level. Check all declarator chunks (outermost first) anyway, to give
1509 // better diagnostics.
Richard Smith8110f042011-02-22 01:22:29 +00001510 if (getLangOptions().CPlusPlus0x && Error != -1) {
Richard Smithe7397c62011-02-22 00:36:53 +00001511 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1512 unsigned chunkIndex = e - i - 1;
1513 state.setCurrentChunkIndex(chunkIndex);
1514 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1515 if (DeclType.Kind == DeclaratorChunk::Function) {
1516 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1517 if (FTI.TrailingReturnType) {
1518 Error = -1;
1519 break;
1520 }
1521 }
1522 }
1523 }
1524
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001525 if (Error != -1) {
1526 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1527 << Error;
1528 T = Context.IntTy;
1529 D.setInvalidType(true);
1530 }
1531 }
Richard Smithe7397c62011-02-22 00:36:53 +00001532
Richard Smith34b41d92011-02-20 03:19:35 +00001533 if (T.isNull())
1534 return Context.getNullTypeSourceInfo();
1535
Douglas Gregorcd281c32009-02-28 00:25:32 +00001536 // The name we're declaring, if any.
1537 DeclarationName Name;
1538 if (D.getIdentifier())
1539 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Mike Stump98eb8a72009-02-04 22:31:32 +00001541 // Walk the DeclTypeInfo, building the recursive type as we go.
1542 // DeclTypeInfos are ordered from the identifier out, which is
1543 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001544 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall711c52b2011-01-05 12:14:39 +00001545 unsigned chunkIndex = e - i - 1;
1546 state.setCurrentChunkIndex(chunkIndex);
1547 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 switch (DeclType.Kind) {
1549 default: assert(0 && "Unknown decltype!");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001550 case DeclaratorChunk::Paren:
1551 T = BuildParenType(T);
1552 break;
Steve Naroff5618bd42008-08-27 16:04:49 +00001553 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001554 // If blocks are disabled, emit an error.
1555 if (!LangOpts.Blocks)
1556 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001557
John McCall28654742010-06-05 06:41:15 +00001558 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1559 if (DeclType.Cls.TypeQuals)
1560 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001561 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001563 // Verify that we're not building a pointer to pointer to function with
1564 // exception specification.
1565 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1566 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1567 D.setInvalidType(true);
1568 // Build the type anyway.
1569 }
John McCallc12c5bb2010-05-15 11:32:37 +00001570 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1571 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001572 if (DeclType.Ptr.TypeQuals)
1573 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001574 break;
1575 }
John McCall28654742010-06-05 06:41:15 +00001576 T = BuildPointerType(T, DeclType.Loc, Name);
1577 if (DeclType.Ptr.TypeQuals)
1578 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
John McCall711c52b2011-01-05 12:14:39 +00001579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 break;
John McCall0953e762009-09-24 19:53:00 +00001581 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001582 // Verify that we're not building a reference to pointer to function with
1583 // exception specification.
1584 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1585 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1586 D.setInvalidType(true);
1587 // Build the type anyway.
1588 }
John McCall28654742010-06-05 06:41:15 +00001589 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1590
1591 Qualifiers Quals;
1592 if (DeclType.Ref.HasRestrict)
1593 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 break;
John McCall0953e762009-09-24 19:53:00 +00001595 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001597 // Verify that we're not building an array of pointers to function with
1598 // exception specification.
1599 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1600 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1601 D.setInvalidType(true);
1602 // Build the type anyway.
1603 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001604 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001605 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 ArrayType::ArraySizeModifier ASM;
1607 if (ATI.isStar)
1608 ASM = ArrayType::Star;
1609 else if (ATI.hasStatic)
1610 ASM = ArrayType::Static;
1611 else
1612 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001613 if (ASM == ArrayType::Star &&
1614 D.getContext() != Declarator::PrototypeContext) {
1615 // FIXME: This check isn't quite right: it allows star in prototypes
1616 // for function definitions, and disallows some edge cases detailed
1617 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1618 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1619 ASM = ArrayType::Normal;
1620 D.setInvalidType(true);
1621 }
John McCall0953e762009-09-24 19:53:00 +00001622 T = BuildArrayType(T, ASM, ArraySize,
1623 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001624 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 break;
1626 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001627 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 // If the function declarator has a prototype (i.e. it is not () and
1629 // does not have a K&R-style identifier list), then the arguments are part
1630 // of the type, otherwise the argument list is ().
1631 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001632
Richard Smithe7397c62011-02-22 00:36:53 +00001633 // Check for auto functions and trailing return type and adjust the
1634 // return type accordingly.
1635 if (!D.isInvalidType()) {
1636 // trailing-return-type is only required if we're declaring a function,
1637 // and not, for instance, a pointer to a function.
1638 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
1639 !FTI.TrailingReturnType && chunkIndex == 0) {
1640 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1641 diag::err_auto_missing_trailing_return);
1642 T = Context.IntTy;
1643 D.setInvalidType(true);
1644 } else if (FTI.TrailingReturnType) {
1645 // T must be exactly 'auto' at this point. See CWG issue 681.
1646 if (isa<ParenType>(T)) {
1647 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1648 diag::err_trailing_return_in_parens)
1649 << T << D.getDeclSpec().getSourceRange();
1650 D.setInvalidType(true);
1651 } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
1652 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1653 diag::err_trailing_return_without_auto)
1654 << T << D.getDeclSpec().getSourceRange();
1655 D.setInvalidType(true);
1656 }
1657
1658 T = GetTypeFromParser(
1659 ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
1660 &ReturnTypeInfo);
1661 }
1662 }
1663
Chris Lattnercd881292007-12-19 05:31:29 +00001664 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001665 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001666 if ((T->isArrayType() || T->isFunctionType()) &&
1667 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001668 unsigned diagID = diag::err_func_returning_array_function;
Argyrios Kyrtzidisa4356ad2011-01-26 01:26:44 +00001669 // Last processing chunk in block context means this function chunk
1670 // represents the block.
1671 if (chunkIndex == 0 &&
1672 D.getContext() == Declarator::BlockLiteralContext)
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001673 diagID = diag::err_block_returning_array_function;
1674 Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001675 T = Context.IntTy;
1676 D.setInvalidType(true);
1677 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001678
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001679 // cv-qualifiers on return types are pointless except when the type is a
1680 // class type in C++.
1681 if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
1682 (!getLangOptions().CPlusPlus ||
1683 (!T->isDependentType() && !T->isRecordType()))) {
1684 unsigned Quals = D.getDeclSpec().getTypeQualifiers();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001685 std::string QualStr;
1686 unsigned NumQuals = 0;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001687 SourceLocation Loc;
Douglas Gregorde80ec12010-07-13 08:50:30 +00001688 if (Quals & Qualifiers::Const) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001689 Loc = D.getDeclSpec().getConstSpecLoc();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001690 ++NumQuals;
1691 QualStr = "const";
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001692 }
Douglas Gregorde80ec12010-07-13 08:50:30 +00001693 if (Quals & Qualifiers::Volatile) {
1694 if (NumQuals == 0) {
1695 Loc = D.getDeclSpec().getVolatileSpecLoc();
1696 QualStr = "volatile";
1697 } else
1698 QualStr += " volatile";
1699 ++NumQuals;
1700 }
1701 if (Quals & Qualifiers::Restrict) {
1702 if (NumQuals == 0) {
1703 Loc = D.getDeclSpec().getRestrictSpecLoc();
1704 QualStr = "restrict";
1705 } else
1706 QualStr += " restrict";
1707 ++NumQuals;
1708 }
1709 assert(NumQuals > 0 && "No known qualifiers?");
1710
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001711 SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
Douglas Gregorde80ec12010-07-13 08:50:30 +00001712 DB << QualStr << NumQuals;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001713 if (Quals & Qualifiers::Const)
1714 DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
1715 if (Quals & Qualifiers::Volatile)
1716 DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
1717 if (Quals & Qualifiers::Restrict)
1718 DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
1719 }
1720
Douglas Gregor402abb52009-05-28 23:31:59 +00001721 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1722 // C++ [dcl.fct]p6:
1723 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00001724 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregor402abb52009-05-28 23:31:59 +00001725 if (Tag->isDefinition())
1726 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1727 << Context.getTypeDeclType(Tag);
1728 }
1729
Sebastian Redl3cc97262009-05-31 11:47:27 +00001730 // Exception specs are not allowed in typedefs. Complain, but add it
1731 // anyway.
1732 if (FTI.hasExceptionSpec &&
1733 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1734 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1735
John McCall28654742010-06-05 06:41:15 +00001736 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1737 // Simple void foo(), where the incoming T is the result type.
1738 T = Context.getFunctionNoProtoType(T);
1739 } else {
1740 // We allow a zero-parameter variadic function in C if the
1741 // function is marked with the "overloadable" attribute. Scan
1742 // for this attribute now.
1743 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001744 bool Overloadable = false;
1745 for (const AttributeList *Attrs = D.getAttributes();
1746 Attrs; Attrs = Attrs->getNext()) {
1747 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1748 Overloadable = true;
1749 break;
1750 }
1751 }
1752
1753 if (!Overloadable)
1754 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001755 }
John McCall28654742010-06-05 06:41:15 +00001756
1757 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001758 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1759 // definition.
John McCall28654742010-06-05 06:41:15 +00001760 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1761 D.setInvalidType(true);
1762 break;
1763 }
1764
John McCalle23cf432010-12-14 08:05:40 +00001765 FunctionProtoType::ExtProtoInfo EPI;
1766 EPI.Variadic = FTI.isVariadic;
1767 EPI.TypeQuals = FTI.TypeQuals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001768 EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1769 : FTI.RefQualifierIsLValueRef? RQ_LValue
1770 : RQ_RValue;
1771
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 // Otherwise, we have a function with an argument list that is
1773 // potentially variadic.
1774 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001775 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001778 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00001779 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001780 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001781
1782 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001783 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001784
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 // Look for 'void'. void is allowed only as a single argument to a
1786 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001787 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001788 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 // If this is something like 'float(int, void)', reject it. 'void'
1790 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1791 // have arguments of incomplete type.
1792 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1793 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001794 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001795 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001796 } else if (FTI.ArgInfo[i].Ident) {
1797 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001799 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001800 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001801 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001802 } else {
1803 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001804 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001805 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattner2ff54262007-07-21 05:18:12 +00001807 // Do not add 'void' to the ArgTys list.
1808 break;
1809 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001810 } else if (!FTI.hasPrototype) {
1811 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001812 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001813 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001814 if (BTy->getKind() == BuiltinType::Float)
1815 ArgTy = Context.DoubleTy;
1816 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001817 }
Fariborz Jahanian56a965c2010-09-08 21:36:35 +00001818
John McCall54e14c42009-10-22 22:37:11 +00001819 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001821
1822 llvm::SmallVector<QualType, 4> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00001823 if (FTI.hasExceptionSpec) {
1824 EPI.HasExceptionSpec = FTI.hasExceptionSpec;
1825 EPI.HasAnyExceptionSpec = FTI.hasAnyExceptionSpec;
John McCalle23cf432010-12-14 08:05:40 +00001826 Exceptions.reserve(FTI.NumExceptions);
1827 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1828 // FIXME: Preserve type source info.
1829 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1830 // Check that the type is valid for an exception spec, and
1831 // drop it if not.
1832 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1833 Exceptions.push_back(ET);
1834 }
John McCall373920b2010-12-14 16:45:57 +00001835 EPI.NumExceptions = Exceptions.size();
John McCalle23cf432010-12-14 08:05:40 +00001836 EPI.Exceptions = Exceptions.data();
Sebastian Redlef65f062009-05-29 18:02:33 +00001837 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001838
John McCalle23cf432010-12-14 08:05:40 +00001839 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 }
John McCall04a67a62010-02-05 21:31:56 +00001841
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 break;
1843 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001844 case DeclaratorChunk::MemberPointer:
1845 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001846 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001847 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001848 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001849 // Avoid emitting extra errors if we already errored on the scope.
1850 D.setInvalidType(true);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001851 } else if (isDependentScopeSpecifier(SS) ||
1852 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001853 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001854 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001855 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1856 switch (NNS->getKind()) {
1857 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001858 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001859 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001860 break;
1861
1862 case NestedNameSpecifier::Namespace:
1863 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001864 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001865 break;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001866
Douglas Gregor87c12c42009-11-04 16:49:01 +00001867 case NestedNameSpecifier::TypeSpec:
1868 case NestedNameSpecifier::TypeSpecWithTemplate:
1869 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001870 // Note: if NNS is dependent, then its prefix (if any) is already
1871 // included in ClsType; this does not hold if the NNS is
1872 // nondependent: in this case (if there is indeed a prefix)
1873 // ClsType needs to be wrapped into an elaborated type.
1874 if (NNSPrefix && !NNS->isDependent())
1875 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00001876 break;
1877 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001878 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001879 Diag(DeclType.Mem.Scope().getBeginLoc(),
1880 diag::err_illegal_decl_mempointer_in_nonclass)
1881 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1882 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001883 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001884 }
1885
Douglas Gregor949bf692009-06-09 22:17:39 +00001886 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00001887 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00001888 if (T.isNull()) {
1889 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001890 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00001891 } else if (DeclType.Mem.TypeQuals) {
1892 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001893 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001894 break;
1895 }
1896
Douglas Gregorcd281c32009-02-28 00:25:32 +00001897 if (T.isNull()) {
1898 D.setInvalidType(true);
1899 T = Context.IntTy;
1900 }
1901
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001902 // See if there are any attributes on this declarator chunk.
John McCall711c52b2011-01-05 12:14:39 +00001903 if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
1904 processTypeAttrs(state, T, false, attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001906
1907 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001908 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001909 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001910
Douglas Gregor708f3b82010-10-14 22:03:51 +00001911 // C++ 8.3.5p4:
1912 // A cv-qualifier-seq shall only be part of the function type
1913 // for a nonstatic member function, the function type to which a pointer
1914 // to member refers, or the top-level function type of a function typedef
1915 // declaration.
Douglas Gregor683a81f2011-01-31 16:09:46 +00001916 //
1917 // Core issue 547 also allows cv-qualifiers on function types that are
1918 // top-level template type arguments.
John McCall613ef3d2010-10-19 01:54:45 +00001919 bool FreeFunction;
1920 if (!D.getCXXScopeSpec().isSet()) {
1921 FreeFunction = (D.getContext() != Declarator::MemberContext ||
1922 D.getDeclSpec().isFriendSpecified());
1923 } else {
1924 DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
1925 FreeFunction = (DC && !DC->isRecord());
1926 }
1927
Douglas Gregorc938c162011-01-26 05:01:58 +00001928 // C++0x [dcl.fct]p6:
1929 // A ref-qualifier shall only be part of the function type for a
1930 // non-static member function, the function type to which a pointer to
1931 // member refers, or the top-level function type of a function typedef
1932 // declaration.
1933 if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
Douglas Gregor683a81f2011-01-31 16:09:46 +00001934 !(D.getContext() == Declarator::TemplateTypeArgContext &&
1935 !D.isFunctionDeclarator()) &&
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001936 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00001937 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001938 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Douglas Gregor683a81f2011-01-31 16:09:46 +00001939 if (D.getContext() == Declarator::TemplateTypeArgContext) {
1940 // Accept qualified function types as template type arguments as a GNU
1941 // extension. This is also the subject of C++ core issue 547.
1942 std::string Quals;
1943 if (FnTy->getTypeQuals() != 0)
1944 Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
1945
1946 switch (FnTy->getRefQualifier()) {
1947 case RQ_None:
1948 break;
1949
1950 case RQ_LValue:
1951 if (!Quals.empty())
1952 Quals += ' ';
1953 Quals += '&';
1954 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00001955
Douglas Gregor683a81f2011-01-31 16:09:46 +00001956 case RQ_RValue:
1957 if (!Quals.empty())
1958 Quals += ' ';
1959 Quals += "&&";
1960 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00001961 }
Douglas Gregor683a81f2011-01-31 16:09:46 +00001962
1963 Diag(D.getIdentifierLoc(),
1964 diag::ext_qualified_function_type_template_arg)
1965 << Quals;
1966 } else {
1967 if (FnTy->getTypeQuals() != 0) {
1968 if (D.isFunctionDeclarator())
1969 Diag(D.getIdentifierLoc(),
1970 diag::err_invalid_qualified_function_type);
1971 else
1972 Diag(D.getIdentifierLoc(),
1973 diag::err_invalid_qualified_typedef_function_type_use)
1974 << FreeFunction;
1975 }
1976
1977 if (FnTy->getRefQualifier()) {
1978 if (D.isFunctionDeclarator()) {
1979 SourceLocation Loc = D.getIdentifierLoc();
1980 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
1981 const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
1982 if (Chunk.Kind == DeclaratorChunk::Function &&
1983 Chunk.Fun.hasRefQualifier()) {
1984 Loc = Chunk.Fun.getRefQualifierLoc();
1985 break;
1986 }
1987 }
1988
1989 Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
1990 << (FnTy->getRefQualifier() == RQ_LValue)
1991 << FixItHint::CreateRemoval(Loc);
1992 } else {
1993 Diag(D.getIdentifierLoc(),
1994 diag::err_invalid_ref_qualifier_typedef_function_type_use)
1995 << FreeFunction
1996 << (FnTy->getRefQualifier() == RQ_LValue);
1997 }
1998 }
1999
2000 // Strip the cv-qualifiers and ref-qualifiers from the type.
2001 FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2002 EPI.TypeQuals = 0;
2003 EPI.RefQualifier = RQ_None;
2004
2005 T = Context.getFunctionType(FnTy->getResultType(),
2006 FnTy->arg_type_begin(),
2007 FnTy->getNumArgs(), EPI);
Douglas Gregorc938c162011-01-26 05:01:58 +00002008 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002009 }
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
John McCall711c52b2011-01-05 12:14:39 +00002012 // Apply any undistributed attributes from the declarator.
2013 if (!T.isNull())
2014 if (AttributeList *attrs = D.getAttributes())
2015 processTypeAttrs(state, T, false, attrs);
2016
2017 // Diagnose any ignored type attributes.
2018 if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2019
Sebastian Redl73780122010-06-09 21:19:43 +00002020 // If there's a constexpr specifier, treat it as a top-level const.
2021 if (D.getDeclSpec().isConstexprSpecified()) {
2022 T.addConst();
2023 }
2024
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002025 // If there was an ellipsis in the declarator, the declaration declares a
2026 // parameter pack whose type may be a pack expansion type.
2027 if (D.hasEllipsis() && !T.isNull()) {
2028 // C++0x [dcl.fct]p13:
2029 // A declarator-id or abstract-declarator containing an ellipsis shall
2030 // only be used in a parameter-declaration. Such a parameter-declaration
2031 // is a parameter pack (14.5.3). [...]
2032 switch (D.getContext()) {
2033 case Declarator::PrototypeContext:
2034 // C++0x [dcl.fct]p13:
2035 // [...] When it is part of a parameter-declaration-clause, the
2036 // parameter pack is a function parameter pack (14.5.3). The type T
2037 // of the declarator-id of the function parameter pack shall contain
2038 // a template parameter pack; each template parameter pack in T is
2039 // expanded by the function parameter pack.
2040 //
2041 // We represent function parameter packs as function parameters whose
2042 // type is a pack expansion.
2043 if (!T->containsUnexpandedParameterPack()) {
2044 Diag(D.getEllipsisLoc(),
2045 diag::err_function_parameter_pack_without_parameter_packs)
2046 << T << D.getSourceRange();
2047 D.setEllipsisLoc(SourceLocation());
2048 } else {
Douglas Gregorcded4f62011-01-14 17:04:44 +00002049 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002050 }
2051 break;
2052
2053 case Declarator::TemplateParamContext:
2054 // C++0x [temp.param]p15:
2055 // If a template-parameter is a [...] is a parameter-declaration that
2056 // declares a parameter pack (8.3.5), then the template-parameter is a
2057 // template parameter pack (14.5.3).
2058 //
2059 // Note: core issue 778 clarifies that, if there are any unexpanded
2060 // parameter packs in the type of the non-type template parameter, then
2061 // it expands those parameter packs.
2062 if (T->containsUnexpandedParameterPack())
Douglas Gregorcded4f62011-01-14 17:04:44 +00002063 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregor10738d32010-12-23 23:51:58 +00002064 else if (!getLangOptions().CPlusPlus0x)
Douglas Gregor5ce5f522011-01-19 21:59:15 +00002065 Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002066 break;
2067
2068 case Declarator::FileContext:
2069 case Declarator::KNRTypeListContext:
2070 case Declarator::TypeNameContext:
2071 case Declarator::MemberContext:
2072 case Declarator::BlockContext:
2073 case Declarator::ForContext:
2074 case Declarator::ConditionContext:
2075 case Declarator::CXXCatchContext:
2076 case Declarator::BlockLiteralContext:
Douglas Gregor683a81f2011-01-31 16:09:46 +00002077 case Declarator::TemplateTypeArgContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002078 // FIXME: We may want to allow parameter packs in block-literal contexts
2079 // in the future.
2080 Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2081 D.setEllipsisLoc(SourceLocation());
2082 break;
2083 }
2084 }
Richard Smithe7397c62011-02-22 00:36:53 +00002085
John McCallbf1a0282010-06-04 23:28:52 +00002086 if (T.isNull())
2087 return Context.getNullTypeSourceInfo();
2088 else if (D.isInvalidType())
2089 return Context.getTrivialTypeSourceInfo(T);
2090 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002091}
2092
John McCall51bd8032009-10-18 01:05:36 +00002093namespace {
2094 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002095 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00002096 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002097
John McCall51bd8032009-10-18 01:05:36 +00002098 public:
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002099 TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2100 : Context(Context), DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002101
John McCall51bd8032009-10-18 01:05:36 +00002102 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
2103 Visit(TL.getUnqualifiedLoc());
2104 }
2105 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2106 TL.setNameLoc(DS.getTypeSpecTypeLoc());
2107 }
2108 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2109 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00002110 }
2111 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2112 // Handle the base type, which might not have been written explicitly.
2113 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2114 TL.setHasBaseTypeAsWritten(false);
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002115 TL.getBaseLoc().initialize(Context, SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002116 } else {
2117 TL.setHasBaseTypeAsWritten(true);
2118 Visit(TL.getBaseLoc());
2119 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00002120
John McCallc12c5bb2010-05-15 11:32:37 +00002121 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00002122 if (DS.getProtocolQualifiers()) {
2123 assert(TL.getNumProtocols() > 0);
2124 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
2125 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
2126 TL.setRAngleLoc(DS.getSourceRange().getEnd());
2127 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
2128 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
2129 } else {
2130 assert(TL.getNumProtocols() == 0);
2131 TL.setLAngleLoc(SourceLocation());
2132 TL.setRAngleLoc(SourceLocation());
2133 }
2134 }
2135 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00002136 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002137 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002138 }
John McCall833ca992009-10-29 08:12:44 +00002139 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00002140 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002141 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00002142
2143 // If we got no declarator info from previous Sema routines,
2144 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00002145 if (!TInfo) {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002146 TL.initialize(Context, DS.getTypeSpecTypeLoc());
John McCall833ca992009-10-29 08:12:44 +00002147 return;
2148 }
2149
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002150 TypeLoc OldTL = TInfo->getTypeLoc();
2151 if (TInfo->getType()->getAs<ElaboratedType>()) {
2152 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2153 TemplateSpecializationTypeLoc NamedTL =
2154 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2155 TL.copy(NamedTL);
2156 }
2157 else
2158 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00002159 }
John McCallcfb708c2010-01-13 20:03:27 +00002160 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2161 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2162 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2163 TL.setParensRange(DS.getTypeofParensRange());
2164 }
2165 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2166 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2167 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2168 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00002169 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00002170 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002171 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00002172 TL.setUnderlyingTInfo(TInfo);
2173 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00002174 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2175 // By default, use the source location of the type specifier.
2176 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2177 if (TL.needsExtraLocalData()) {
2178 // Set info for the written builtin specifiers.
2179 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2180 // Try to have a meaningful source location.
2181 if (TL.getWrittenSignSpec() != TSS_unspecified)
2182 // Sign spec loc overrides the others (e.g., 'unsigned long').
2183 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2184 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2185 // Width spec loc overrides type spec loc (e.g., 'short int').
2186 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2187 }
2188 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002189 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2190 ElaboratedTypeKeyword Keyword
2191 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002192 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002193 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002194 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002195 if (TInfo) {
2196 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2197 return;
2198 }
2199 }
2200 TL.setKeywordLoc(Keyword != ETK_None
2201 ? DS.getTypeSpecTypeLoc()
2202 : SourceLocation());
2203 const CXXScopeSpec& SS = DS.getTypeSpecScope();
2204 TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
2205 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2206 }
2207 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2208 ElaboratedTypeKeyword Keyword
2209 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002210 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002211 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002212 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002213 if (TInfo) {
2214 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2215 return;
2216 }
2217 }
2218 TL.setKeywordLoc(Keyword != ETK_None
2219 ? DS.getTypeSpecTypeLoc()
2220 : SourceLocation());
2221 const CXXScopeSpec& SS = DS.getTypeSpecScope();
2222 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
2223 // FIXME: load appropriate source location.
2224 TL.setNameLoc(DS.getTypeSpecTypeLoc());
2225 }
John McCall33500952010-06-11 00:33:02 +00002226 void VisitDependentTemplateSpecializationTypeLoc(
2227 DependentTemplateSpecializationTypeLoc TL) {
2228 ElaboratedTypeKeyword Keyword
2229 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2230 if (Keyword == ETK_Typename) {
2231 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002232 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall33500952010-06-11 00:33:02 +00002233 if (TInfo) {
2234 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
2235 TInfo->getTypeLoc()));
2236 return;
2237 }
2238 }
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002239 TL.initializeLocal(Context, SourceLocation());
John McCall33500952010-06-11 00:33:02 +00002240 TL.setKeywordLoc(Keyword != ETK_None
2241 ? DS.getTypeSpecTypeLoc()
2242 : SourceLocation());
2243 const CXXScopeSpec& SS = DS.getTypeSpecScope();
2244 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
2245 // FIXME: load appropriate source location.
2246 TL.setNameLoc(DS.getTypeSpecTypeLoc());
2247 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002248
John McCall51bd8032009-10-18 01:05:36 +00002249 void VisitTypeLoc(TypeLoc TL) {
2250 // FIXME: add other typespec types and change this to an assert.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002251 TL.initialize(Context, DS.getTypeSpecTypeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002252 }
2253 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002254
John McCall51bd8032009-10-18 01:05:36 +00002255 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
2256 const DeclaratorChunk &Chunk;
2257
2258 public:
2259 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
2260
2261 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002262 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00002263 }
2264
2265 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2266 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
2267 TL.setCaretLoc(Chunk.Loc);
2268 }
2269 void VisitPointerTypeLoc(PointerTypeLoc TL) {
2270 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2271 TL.setStarLoc(Chunk.Loc);
2272 }
2273 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2274 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2275 TL.setStarLoc(Chunk.Loc);
2276 }
2277 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2278 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
2279 TL.setStarLoc(Chunk.Loc);
2280 // FIXME: nested name specifier
2281 }
2282 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2283 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00002284 // 'Amp' is misleading: this might have been originally
2285 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00002286 TL.setAmpLoc(Chunk.Loc);
2287 }
2288 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2289 assert(Chunk.Kind == DeclaratorChunk::Reference);
2290 assert(!Chunk.Ref.LValueRef);
2291 TL.setAmpAmpLoc(Chunk.Loc);
2292 }
2293 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
2294 assert(Chunk.Kind == DeclaratorChunk::Array);
2295 TL.setLBracketLoc(Chunk.Loc);
2296 TL.setRBracketLoc(Chunk.EndLoc);
2297 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
2298 }
2299 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2300 assert(Chunk.Kind == DeclaratorChunk::Function);
2301 TL.setLParenLoc(Chunk.Loc);
2302 TL.setRParenLoc(Chunk.EndLoc);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002303 TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
John McCall51bd8032009-10-18 01:05:36 +00002304
2305 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00002306 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002307 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00002308 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00002309 }
2310 // FIXME: exception specs
2311 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002312 void VisitParenTypeLoc(ParenTypeLoc TL) {
2313 assert(Chunk.Kind == DeclaratorChunk::Paren);
2314 TL.setLParenLoc(Chunk.Loc);
2315 TL.setRParenLoc(Chunk.EndLoc);
2316 }
John McCall51bd8032009-10-18 01:05:36 +00002317
2318 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002319 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00002320 }
2321 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002322}
2323
John McCalla93c9342009-12-07 02:54:59 +00002324/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002325///
2326/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00002327///
2328/// \param ReturnTypeInfo For declarators whose return type does not show
2329/// up in the normal place in the declaration specifiers (such as a C++
2330/// conversion function), this pointer will refer to a type source information
2331/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00002332TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00002333Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
2334 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00002335 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2336 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002337
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002338 // Handle parameter packs whose type is a pack expansion.
2339 if (isa<PackExpansionType>(T)) {
2340 cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2341 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2342 }
2343
Sebastian Redl8ce35b02009-10-25 21:45:37 +00002344 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00002345 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
2346 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002347 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002348
John McCallb3d87482010-08-24 05:47:05 +00002349 // If we have different source information for the return type, use
2350 // that. This really only applies to C++ conversion functions.
2351 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00002352 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
2353 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
2354 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00002355 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002356 TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00002357 }
2358
John McCalla93c9342009-12-07 02:54:59 +00002359 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002360}
2361
John McCalla93c9342009-12-07 02:54:59 +00002362/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00002363ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002364 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
2365 // and Sema during declaration parsing. Try deallocating/caching them when
2366 // it's appropriate, instead of allocating them and keeping them around.
Douglas Gregoreb0eb492010-12-10 08:12:03 +00002367 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2368 TypeAlignment);
John McCalla93c9342009-12-07 02:54:59 +00002369 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002370 assert(LocT->getTypeClass() != T->getTypeClass() &&
2371 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00002372 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002373}
2374
2375void LocInfoType::getAsStringInternal(std::string &Str,
2376 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00002377 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
2378 " was used directly instead of getting the QualType through"
2379 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002380}
2381
John McCallf312b1e2010-08-26 23:41:50 +00002382TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002383 // C99 6.7.6: Type names have no identifier. This is already validated by
2384 // the parser.
2385 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Douglas Gregor402abb52009-05-28 23:31:59 +00002387 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002388 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2389 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00002390 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00002391 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00002392
Douglas Gregor402abb52009-05-28 23:31:59 +00002393 if (getLangOptions().CPlusPlus) {
2394 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00002395 CheckExtraCXXDefaultArguments(D);
2396
Douglas Gregor402abb52009-05-28 23:31:59 +00002397 // C++0x [dcl.type]p3:
2398 // A type-specifier-seq shall not define a class or enumeration
2399 // unless it appears in the type-id of an alias-declaration
2400 // (7.1.3).
2401 if (OwnedTag && OwnedTag->isDefinition())
2402 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2403 << Context.getTypeDeclType(OwnedTag);
2404 }
2405
John McCallb3d87482010-08-24 05:47:05 +00002406 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002407}
2408
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002409
2410
2411//===----------------------------------------------------------------------===//
2412// Type Attribute Processing
2413//===----------------------------------------------------------------------===//
2414
2415/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2416/// specified type. The attribute contains 1 argument, the id of the address
2417/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00002418static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002419 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00002420
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002421 // If this type is already address space qualified, reject it.
2422 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2423 // for two or more different address spaces."
2424 if (Type.getAddressSpace()) {
2425 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00002426 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002427 return;
2428 }
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002430 // Check the attribute arguments.
2431 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00002432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002433 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002434 return;
2435 }
2436 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2437 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002438 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2439 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00002440 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2441 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002442 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002443 return;
2444 }
2445
John McCallefadb772009-07-28 06:52:18 +00002446 // Bounds checking.
2447 if (addrSpace.isSigned()) {
2448 if (addrSpace.isNegative()) {
2449 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2450 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002451 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002452 return;
2453 }
2454 addrSpace.setIsSigned(false);
2455 }
2456 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00002457 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00002458 if (addrSpace > max) {
2459 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00002460 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002461 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002462 return;
2463 }
2464
Mike Stump1eb44332009-09-09 15:08:12 +00002465 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002466 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002467}
2468
John McCall711c52b2011-01-05 12:14:39 +00002469/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2470/// attribute on the specified type. Returns true to indicate that
2471/// the attribute was handled, false to indicate that the type does
2472/// not permit the attribute.
2473static bool handleObjCGCTypeAttr(TypeProcessingState &state,
2474 AttributeList &attr,
2475 QualType &type) {
2476 Sema &S = state.getSema();
2477
2478 // Delay if this isn't some kind of pointer.
2479 if (!type->isPointerType() &&
2480 !type->isObjCObjectPointerType() &&
2481 !type->isBlockPointerType())
2482 return false;
2483
2484 if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2485 S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2486 attr.setInvalid();
2487 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002488 }
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002490 // Check the attribute arguments.
John McCall711c52b2011-01-05 12:14:39 +00002491 if (!attr.getParameterName()) {
2492 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002493 << "objc_gc" << 1;
John McCall711c52b2011-01-05 12:14:39 +00002494 attr.setInvalid();
2495 return true;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002496 }
John McCall0953e762009-09-24 19:53:00 +00002497 Qualifiers::GC GCAttr;
John McCall711c52b2011-01-05 12:14:39 +00002498 if (attr.getNumArgs() != 0) {
2499 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2500 attr.setInvalid();
2501 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002502 }
John McCall711c52b2011-01-05 12:14:39 +00002503 if (attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00002504 GCAttr = Qualifiers::Weak;
John McCall711c52b2011-01-05 12:14:39 +00002505 else if (attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00002506 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002507 else {
John McCall711c52b2011-01-05 12:14:39 +00002508 S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2509 << "objc_gc" << attr.getParameterName();
2510 attr.setInvalid();
2511 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002512 }
Mike Stump1eb44332009-09-09 15:08:12 +00002513
John McCall711c52b2011-01-05 12:14:39 +00002514 type = S.Context.getObjCGCQualType(type, GCAttr);
2515 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002516}
2517
John McCalle6a365d2010-12-19 02:44:49 +00002518namespace {
2519 /// A helper class to unwrap a type down to a function for the
2520 /// purposes of applying attributes there.
2521 ///
2522 /// Use:
2523 /// FunctionTypeUnwrapper unwrapped(SemaRef, T);
2524 /// if (unwrapped.isFunctionType()) {
2525 /// const FunctionType *fn = unwrapped.get();
2526 /// // change fn somehow
2527 /// T = unwrapped.wrap(fn);
2528 /// }
2529 struct FunctionTypeUnwrapper {
2530 enum WrapKind {
2531 Desugar,
2532 Parens,
2533 Pointer,
2534 BlockPointer,
2535 Reference,
2536 MemberPointer
2537 };
2538
2539 QualType Original;
2540 const FunctionType *Fn;
2541 llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2542
2543 FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2544 while (true) {
2545 const Type *Ty = T.getTypePtr();
2546 if (isa<FunctionType>(Ty)) {
2547 Fn = cast<FunctionType>(Ty);
2548 return;
2549 } else if (isa<ParenType>(Ty)) {
2550 T = cast<ParenType>(Ty)->getInnerType();
2551 Stack.push_back(Parens);
2552 } else if (isa<PointerType>(Ty)) {
2553 T = cast<PointerType>(Ty)->getPointeeType();
2554 Stack.push_back(Pointer);
2555 } else if (isa<BlockPointerType>(Ty)) {
2556 T = cast<BlockPointerType>(Ty)->getPointeeType();
2557 Stack.push_back(BlockPointer);
2558 } else if (isa<MemberPointerType>(Ty)) {
2559 T = cast<MemberPointerType>(Ty)->getPointeeType();
2560 Stack.push_back(MemberPointer);
2561 } else if (isa<ReferenceType>(Ty)) {
2562 T = cast<ReferenceType>(Ty)->getPointeeType();
2563 Stack.push_back(Reference);
2564 } else {
2565 const Type *DTy = Ty->getUnqualifiedDesugaredType();
2566 if (Ty == DTy) {
2567 Fn = 0;
2568 return;
2569 }
2570
2571 T = QualType(DTy, 0);
2572 Stack.push_back(Desugar);
2573 }
2574 }
2575 }
2576
2577 bool isFunctionType() const { return (Fn != 0); }
2578 const FunctionType *get() const { return Fn; }
2579
2580 QualType wrap(Sema &S, const FunctionType *New) {
2581 // If T wasn't modified from the unwrapped type, do nothing.
2582 if (New == get()) return Original;
2583
2584 Fn = New;
2585 return wrap(S.Context, Original, 0);
2586 }
2587
2588 private:
2589 QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2590 if (I == Stack.size())
2591 return C.getQualifiedType(Fn, Old.getQualifiers());
2592
2593 // Build up the inner type, applying the qualifiers from the old
2594 // type to the new type.
2595 SplitQualType SplitOld = Old.split();
2596
2597 // As a special case, tail-recurse if there are no qualifiers.
2598 if (SplitOld.second.empty())
2599 return wrap(C, SplitOld.first, I);
2600 return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2601 }
2602
2603 QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2604 if (I == Stack.size()) return QualType(Fn, 0);
2605
2606 switch (static_cast<WrapKind>(Stack[I++])) {
2607 case Desugar:
2608 // This is the point at which we potentially lose source
2609 // information.
2610 return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2611
2612 case Parens: {
2613 QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2614 return C.getParenType(New);
2615 }
2616
2617 case Pointer: {
2618 QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2619 return C.getPointerType(New);
2620 }
2621
2622 case BlockPointer: {
2623 QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2624 return C.getBlockPointerType(New);
2625 }
2626
2627 case MemberPointer: {
2628 const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2629 QualType New = wrap(C, OldMPT->getPointeeType(), I);
2630 return C.getMemberPointerType(New, OldMPT->getClass());
2631 }
2632
2633 case Reference: {
2634 const ReferenceType *OldRef = cast<ReferenceType>(Old);
2635 QualType New = wrap(C, OldRef->getPointeeType(), I);
2636 if (isa<LValueReferenceType>(OldRef))
2637 return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2638 else
2639 return C.getRValueReferenceType(New);
2640 }
2641 }
2642
2643 llvm_unreachable("unknown wrapping kind");
2644 return QualType();
2645 }
2646 };
2647}
2648
John McCall711c52b2011-01-05 12:14:39 +00002649/// Process an individual function attribute. Returns true to
2650/// indicate that the attribute was handled, false if it wasn't.
2651static bool handleFunctionTypeAttr(TypeProcessingState &state,
2652 AttributeList &attr,
2653 QualType &type) {
2654 Sema &S = state.getSema();
John McCalle6a365d2010-12-19 02:44:49 +00002655
John McCall711c52b2011-01-05 12:14:39 +00002656 FunctionTypeUnwrapper unwrapped(S, type);
Mike Stump24556362009-07-25 21:26:53 +00002657
John McCall711c52b2011-01-05 12:14:39 +00002658 if (attr.getKind() == AttributeList::AT_noreturn) {
2659 if (S.CheckNoReturnAttr(attr))
John McCall04a67a62010-02-05 21:31:56 +00002660 return true;
John McCalle6a365d2010-12-19 02:44:49 +00002661
John McCall711c52b2011-01-05 12:14:39 +00002662 // Delay if this is not a function type.
2663 if (!unwrapped.isFunctionType())
2664 return false;
2665
John McCall04a67a62010-02-05 21:31:56 +00002666 // Otherwise we can process right away.
John McCall711c52b2011-01-05 12:14:39 +00002667 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2668 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2669 return true;
John McCall04a67a62010-02-05 21:31:56 +00002670 }
Mike Stump24556362009-07-25 21:26:53 +00002671
John McCall711c52b2011-01-05 12:14:39 +00002672 if (attr.getKind() == AttributeList::AT_regparm) {
2673 unsigned value;
2674 if (S.CheckRegparmAttr(attr, value))
Rafael Espindola425ef722010-03-30 22:15:11 +00002675 return true;
2676
John McCall711c52b2011-01-05 12:14:39 +00002677 // Delay if this is not a function type.
2678 if (!unwrapped.isFunctionType())
Rafael Espindola425ef722010-03-30 22:15:11 +00002679 return false;
2680
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002681 // Diagnose regparm with fastcall.
2682 const FunctionType *fn = unwrapped.get();
2683 CallingConv CC = fn->getCallConv();
2684 if (CC == CC_X86FastCall) {
2685 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2686 << FunctionType::getNameForCallConv(CC)
2687 << "regparm";
2688 attr.setInvalid();
2689 return true;
2690 }
2691
John McCalle6a365d2010-12-19 02:44:49 +00002692 FunctionType::ExtInfo EI =
John McCall711c52b2011-01-05 12:14:39 +00002693 unwrapped.get()->getExtInfo().withRegParm(value);
2694 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2695 return true;
Rafael Espindola425ef722010-03-30 22:15:11 +00002696 }
2697
John McCall04a67a62010-02-05 21:31:56 +00002698 // Otherwise, a calling convention.
John McCall711c52b2011-01-05 12:14:39 +00002699 CallingConv CC;
2700 if (S.CheckCallingConvAttr(attr, CC))
2701 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002702
John McCall04a67a62010-02-05 21:31:56 +00002703 // Delay if the type didn't work out to a function.
John McCall711c52b2011-01-05 12:14:39 +00002704 if (!unwrapped.isFunctionType()) return false;
John McCall04a67a62010-02-05 21:31:56 +00002705
John McCall711c52b2011-01-05 12:14:39 +00002706 const FunctionType *fn = unwrapped.get();
2707 CallingConv CCOld = fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00002708 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00002709 S.Context.getCanonicalCallConv(CCOld)) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002710 FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2711 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
John McCall711c52b2011-01-05 12:14:39 +00002712 return true;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002713 }
John McCall04a67a62010-02-05 21:31:56 +00002714
2715 if (CCOld != CC_Default) {
2716 // Should we diagnose reapplications of the same convention?
John McCall711c52b2011-01-05 12:14:39 +00002717 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
John McCall04a67a62010-02-05 21:31:56 +00002718 << FunctionType::getNameForCallConv(CC)
2719 << FunctionType::getNameForCallConv(CCOld);
John McCall711c52b2011-01-05 12:14:39 +00002720 attr.setInvalid();
2721 return true;
John McCall04a67a62010-02-05 21:31:56 +00002722 }
2723
2724 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
2725 if (CC == CC_X86FastCall) {
John McCall711c52b2011-01-05 12:14:39 +00002726 if (isa<FunctionNoProtoType>(fn)) {
2727 S.Diag(attr.getLoc(), diag::err_cconv_knr)
John McCall04a67a62010-02-05 21:31:56 +00002728 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002729 attr.setInvalid();
2730 return true;
John McCall04a67a62010-02-05 21:31:56 +00002731 }
2732
John McCall711c52b2011-01-05 12:14:39 +00002733 const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
John McCall04a67a62010-02-05 21:31:56 +00002734 if (FnP->isVariadic()) {
John McCall711c52b2011-01-05 12:14:39 +00002735 S.Diag(attr.getLoc(), diag::err_cconv_varargs)
John McCall04a67a62010-02-05 21:31:56 +00002736 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002737 attr.setInvalid();
2738 return true;
John McCall04a67a62010-02-05 21:31:56 +00002739 }
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002740
2741 // Also diagnose fastcall with regparm.
2742 if (fn->getRegParmType()) {
2743 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2744 << "regparm"
2745 << FunctionType::getNameForCallConv(CC);
2746 attr.setInvalid();
2747 return true;
2748 }
John McCall04a67a62010-02-05 21:31:56 +00002749 }
2750
John McCall711c52b2011-01-05 12:14:39 +00002751 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2752 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2753 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002754}
2755
John Thompson6e132aa2009-12-04 21:51:28 +00002756/// HandleVectorSizeAttribute - this attribute is only applicable to integral
2757/// and float scalars, although arrays, pointers, and function return values are
2758/// allowed in conjunction with this construct. Aggregates with this attribute
2759/// are invalid, even if they are of the same size as a corresponding scalar.
2760/// The raw attribute should contain precisely 1 argument, the vector size for
2761/// the variable, measured in bytes. If curType and rawAttr are well formed,
2762/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00002763static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
2764 Sema &S) {
Bob Wilson56affbc2010-11-16 00:32:16 +00002765 // Check the attribute arguments.
John Thompson6e132aa2009-12-04 21:51:28 +00002766 if (Attr.getNumArgs() != 1) {
2767 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002768 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002769 return;
2770 }
2771 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
2772 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002773 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2774 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00002775 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2776 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002777 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002778 return;
2779 }
2780 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00002781 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00002782 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002783 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002784 return;
2785 }
2786 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
2787 // vecSize is specified in bytes - convert to bits.
2788 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
2789
2790 // the vector size needs to be an integral multiple of the type size.
2791 if (vectorSize % typeSize) {
2792 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
2793 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002794 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002795 return;
2796 }
2797 if (vectorSize == 0) {
2798 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
2799 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002800 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002801 return;
2802 }
2803
2804 // Success! Instantiate the vector type, the number of elements is > 0, and
2805 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00002806 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002807 VectorType::GenericVector);
John Thompson6e132aa2009-12-04 21:51:28 +00002808}
2809
Bob Wilson4211bb62010-11-16 00:32:24 +00002810/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
2811/// "neon_polyvector_type" attributes are used to create vector types that
2812/// are mangled according to ARM's ABI. Otherwise, these types are identical
2813/// to those created with the "vector_size" attribute. Unlike "vector_size"
2814/// the argument to these Neon attributes is the number of vector elements,
2815/// not the vector size in bytes. The vector width and element type must
2816/// match one of the standard Neon vector types.
2817static void HandleNeonVectorTypeAttr(QualType& CurType,
2818 const AttributeList &Attr, Sema &S,
2819 VectorType::VectorKind VecKind,
2820 const char *AttrName) {
2821 // Check the attribute arguments.
2822 if (Attr.getNumArgs() != 1) {
2823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2824 Attr.setInvalid();
2825 return;
2826 }
2827 // The number of elements must be an ICE.
2828 Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
2829 llvm::APSInt numEltsInt(32);
2830 if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
2831 !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
2832 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2833 << AttrName << numEltsExpr->getSourceRange();
2834 Attr.setInvalid();
2835 return;
2836 }
2837 // Only certain element types are supported for Neon vectors.
2838 const BuiltinType* BTy = CurType->getAs<BuiltinType>();
2839 if (!BTy ||
2840 (VecKind == VectorType::NeonPolyVector &&
2841 BTy->getKind() != BuiltinType::SChar &&
2842 BTy->getKind() != BuiltinType::Short) ||
2843 (BTy->getKind() != BuiltinType::SChar &&
2844 BTy->getKind() != BuiltinType::UChar &&
2845 BTy->getKind() != BuiltinType::Short &&
2846 BTy->getKind() != BuiltinType::UShort &&
2847 BTy->getKind() != BuiltinType::Int &&
2848 BTy->getKind() != BuiltinType::UInt &&
2849 BTy->getKind() != BuiltinType::LongLong &&
2850 BTy->getKind() != BuiltinType::ULongLong &&
2851 BTy->getKind() != BuiltinType::Float)) {
2852 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
2853 Attr.setInvalid();
2854 return;
2855 }
2856 // The total size of the vector must be 64 or 128 bits.
2857 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
2858 unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
2859 unsigned vecSize = typeSize * numElts;
2860 if (vecSize != 64 && vecSize != 128) {
2861 S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
2862 Attr.setInvalid();
2863 return;
2864 }
2865
2866 CurType = S.Context.getVectorType(CurType, numElts, VecKind);
2867}
2868
John McCall711c52b2011-01-05 12:14:39 +00002869static void processTypeAttrs(TypeProcessingState &state, QualType &type,
2870 bool isDeclSpec, AttributeList *attrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00002871 // Scan through and apply attributes to this type where it makes sense. Some
2872 // attributes (such as __address_space__, __vector_size__, etc) apply to the
2873 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00002874 // apply to the decl. Here we apply type attributes and ignore the rest.
John McCall711c52b2011-01-05 12:14:39 +00002875
2876 AttributeList *next;
2877 do {
2878 AttributeList &attr = *attrs;
2879 next = attr.getNext();
2880
Abramo Bagnarae215f722010-04-30 13:10:51 +00002881 // Skip attributes that were marked to be invalid.
John McCall711c52b2011-01-05 12:14:39 +00002882 if (attr.isInvalid())
Abramo Bagnarae215f722010-04-30 13:10:51 +00002883 continue;
2884
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00002885 // If this is an attribute we can handle, do so now,
2886 // otherwise, add it to the FnAttrs list for rechaining.
John McCall711c52b2011-01-05 12:14:39 +00002887 switch (attr.getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00002888 default: break;
John McCall04a67a62010-02-05 21:31:56 +00002889
Chris Lattner232e8822008-02-21 01:08:11 +00002890 case AttributeList::AT_address_space:
John McCall711c52b2011-01-05 12:14:39 +00002891 HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002892 break;
John McCall711c52b2011-01-05 12:14:39 +00002893 OBJC_POINTER_TYPE_ATTRS_CASELIST:
2894 if (!handleObjCPointerTypeAttr(state, attr, type))
2895 distributeObjCPointerTypeAttr(state, attr, type);
Mike Stump24556362009-07-25 21:26:53 +00002896 break;
John Thompson6e132aa2009-12-04 21:51:28 +00002897 case AttributeList::AT_vector_size:
John McCall711c52b2011-01-05 12:14:39 +00002898 HandleVectorSizeAttr(type, attr, state.getSema());
John McCall04a67a62010-02-05 21:31:56 +00002899 break;
Bob Wilson4211bb62010-11-16 00:32:24 +00002900 case AttributeList::AT_neon_vector_type:
John McCall711c52b2011-01-05 12:14:39 +00002901 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2902 VectorType::NeonVector, "neon_vector_type");
Bob Wilson4211bb62010-11-16 00:32:24 +00002903 break;
2904 case AttributeList::AT_neon_polyvector_type:
John McCall711c52b2011-01-05 12:14:39 +00002905 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
2906 VectorType::NeonPolyVector,
Bob Wilson4211bb62010-11-16 00:32:24 +00002907 "neon_polyvector_type");
2908 break;
John McCall04a67a62010-02-05 21:31:56 +00002909
John McCall711c52b2011-01-05 12:14:39 +00002910 FUNCTION_TYPE_ATTRS_CASELIST:
2911 // Never process function type attributes as part of the
2912 // declaration-specifiers.
2913 if (isDeclSpec)
2914 distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
2915
2916 // Otherwise, handle the possible delays.
2917 else if (!handleFunctionTypeAttr(state, attr, type))
2918 distributeFunctionTypeAttr(state, attr, type);
John Thompson6e132aa2009-12-04 21:51:28 +00002919 break;
Chris Lattner232e8822008-02-21 01:08:11 +00002920 }
John McCall711c52b2011-01-05 12:14:39 +00002921 } while ((attrs = next));
Chris Lattner232e8822008-02-21 01:08:11 +00002922}
2923
Mike Stump1eb44332009-09-09 15:08:12 +00002924/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002925///
2926/// This routine checks whether the type @p T is complete in any
2927/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00002928/// type, returns false. If @p T is a class template specialization,
2929/// this routine then attempts to perform class template
2930/// instantiation. If instantiation fails, or if @p T is incomplete
2931/// and cannot be completed, issues the diagnostic @p diag (giving it
2932/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002933///
2934/// @param Loc The location in the source that the incomplete type
2935/// diagnostic should refer to.
2936///
2937/// @param T The type that this routine is examining for completeness.
2938///
Mike Stump1eb44332009-09-09 15:08:12 +00002939/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00002940/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002941///
2942/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
2943/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002944bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002945 const PartialDiagnostic &PD,
2946 std::pair<SourceLocation,
2947 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002948 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Douglas Gregor573d9c32009-10-21 23:19:44 +00002950 // FIXME: Add this assertion to make sure we always get instantiation points.
2951 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002952 // FIXME: Add this assertion to help us flush out problems with
2953 // checking for dependent types and type-dependent expressions.
2954 //
Mike Stump1eb44332009-09-09 15:08:12 +00002955 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002956 // "Can't ask whether a dependent type is complete");
2957
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002958 // If we have a complete type, we're done.
2959 if (!T->isIncompleteType())
2960 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00002961
Douglas Gregord475b8d2009-03-25 21:17:03 +00002962 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00002963 // class template specialization, or an array with known size of such,
2964 // try to instantiate it.
2965 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00002966 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00002967 MaybeTemplate = Array->getElementType();
2968 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00002969 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002970 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002971 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2972 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002973 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002974 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002975 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002976 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2977 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002978 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2979 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00002980 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002981 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002982 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002983 return InstantiateClass(Loc, Rec, Pattern,
2984 getTemplateInstantiationArgs(Rec),
2985 TSK_ImplicitInstantiation,
2986 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00002987 }
2988 }
2989 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002990
Douglas Gregor5842ba92009-08-24 15:23:48 +00002991 if (diag == 0)
2992 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002993
John McCall916c8702010-11-16 01:44:35 +00002994 const TagType *Tag = T->getAs<TagType>();
Rafael Espindola01620702010-03-21 22:56:43 +00002995
2996 // Avoid diagnosing invalid decls as incomplete.
2997 if (Tag && Tag->getDecl()->isInvalidDecl())
2998 return true;
2999
John McCall916c8702010-11-16 01:44:35 +00003000 // Give the external AST source a chance to complete the type.
3001 if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
3002 Context.getExternalSource()->CompleteType(Tag->getDecl());
3003 if (!Tag->isIncompleteType())
3004 return false;
3005 }
3006
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003007 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003008 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003009
Anders Carlsson8c8d9192009-10-09 23:51:55 +00003010 // If we have a note, produce it.
3011 if (!Note.first.isInvalid())
3012 Diag(Note.first, Note.second);
3013
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003014 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00003015 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003016 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00003017 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003018 Tag->isBeingDefined() ? diag::note_type_being_defined
3019 : diag::note_forward_declaration)
3020 << QualType(Tag, 0);
3021
3022 return true;
3023}
Douglas Gregore6258932009-03-19 00:39:20 +00003024
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00003025bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3026 const PartialDiagnostic &PD) {
3027 return RequireCompleteType(Loc, T, PD,
3028 std::make_pair(SourceLocation(), PDiag(0)));
3029}
3030
3031bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3032 unsigned DiagID) {
3033 return RequireCompleteType(Loc, T, PDiag(DiagID),
3034 std::make_pair(SourceLocation(), PDiag(0)));
3035}
3036
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003037/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3038/// and qualified by the nested-name-specifier contained in SS.
3039QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3040 const CXXScopeSpec &SS, QualType T) {
3041 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00003042 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003043 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003044 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003045 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3046 else {
3047 if (Keyword == ETK_None)
3048 return T;
3049 NNS = 0;
3050 }
3051 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00003052}
Anders Carlssonaf017e62009-06-29 22:58:55 +00003053
John McCall2a984ca2010-10-12 00:20:44 +00003054QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
3055 ExprResult ER = CheckPlaceholderExpr(E, Loc);
3056 if (ER.isInvalid()) return QualType();
3057 E = ER.take();
3058
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003059 if (!E->isTypeDependent()) {
3060 QualType T = E->getType();
Fariborz Jahanianaca7f7b2010-10-06 00:23:25 +00003061 if (const TagType *TT = T->getAs<TagType>())
3062 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003063 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00003064 return Context.getTypeOfExprType(E);
3065}
3066
John McCall2a984ca2010-10-12 00:20:44 +00003067QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
3068 ExprResult ER = CheckPlaceholderExpr(E, Loc);
3069 if (ER.isInvalid()) return QualType();
3070 E = ER.take();
Douglas Gregor4b52e252009-12-21 23:17:24 +00003071
Anders Carlssonaf017e62009-06-29 22:58:55 +00003072 return Context.getDecltypeType(E);
3073}