blob: f5e6bef7198e0a08a49e680e5db3d0c6bc91041c [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
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000020#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000023#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000024#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Douglas Gregor2dc0e642009-03-23 23:06:20 +000028/// \brief Perform adjustment on the parameter type of a function.
29///
30/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000031/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
32/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000033QualType Sema::adjustParameterType(QualType T) {
34 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000035 // A declaration of a parameter as "array of type" shall be
36 // adjusted to "qualified pointer to type", where the type
37 // qualifiers (if any) are those specified within the [ and ] of
38 // the array type derivation.
39 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000040 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000041
42 // C99 6.7.5.3p8:
43 // A declaration of a parameter as "function returning type"
44 // shall be adjusted to "pointer to function returning type", as
45 // in 6.3.2.1.
46 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000047 return Context.getPointerType(T);
48
49 return T;
50}
51
Chris Lattner5db2bb12009-10-25 18:21:37 +000052
53
54/// isOmittedBlockReturnType - Return true if this declarator is missing a
55/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000056static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000057 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 return false;
60
61 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000062 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000063
64 if (D.getNumTypeObjects() == 1 &&
65 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000066 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000067
68 return false;
69}
70
John McCall04a67a62010-02-05 21:31:56 +000071typedef std::pair<const AttributeList*,QualType> DelayedAttribute;
72typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet;
73
74static void ProcessTypeAttributeList(Sema &S, QualType &Type,
75 const AttributeList *Attrs,
76 DelayedAttributeSet &DelayedFnAttrs);
77static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
78
79static void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
80 DelayedAttributeSet &Attrs) {
81 for (DelayedAttributeSet::iterator I = Attrs.begin(),
82 E = Attrs.end(); I != E; ++I)
83 if (ProcessFnAttr(S, Type, *I->first))
84 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
85 << I->first->getName() << I->second;
86 Attrs.clear();
87}
88
89static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
90 for (DelayedAttributeSet::iterator I = Attrs.begin(),
91 E = Attrs.end(); I != E; ++I) {
92 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
93 << I->first->getName() << I->second;
94 }
95 Attrs.clear();
96}
97
Douglas Gregor930d8b52009-01-30 22:09:00 +000098/// \brief Convert the specified declspec to the appropriate type
99/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000100/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000101/// \returns The type described by the declaration specifiers. This function
102/// never returns null.
John McCall04a67a62010-02-05 21:31:56 +0000103static QualType ConvertDeclSpecToType(Sema &TheSema,
104 Declarator &TheDeclarator,
105 DelayedAttributeSet &Delayed) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
107 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000108 const DeclSpec &DS = TheDeclarator.getDeclSpec();
109 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
110 if (DeclLoc.isInvalid())
111 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000112
113 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner5db2bb12009-10-25 18:21:37 +0000115 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000117 case DeclSpec::TST_void:
118 Result = Context.VoidTy;
119 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 case DeclSpec::TST_char:
121 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000122 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000124 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 else {
126 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
127 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000128 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
Chris Lattner958858e2008-02-20 21:40:32 +0000130 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000131 case DeclSpec::TST_wchar:
132 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
133 Result = Context.WCharTy;
134 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000135 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000136 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000137 Result = Context.getSignedWCharType();
138 } else {
139 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
140 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000141 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000142 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000143 Result = Context.getUnsignedWCharType();
144 }
145 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000146 case DeclSpec::TST_char16:
147 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
148 "Unknown TSS value");
149 Result = Context.Char16Ty;
150 break;
151 case DeclSpec::TST_char32:
152 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
153 "Unknown TSS value");
154 Result = Context.Char32Ty;
155 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000156 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000157 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000158 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000159 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000160 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000161 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000162 break;
163 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000164
165 // If this is a missing declspec in a block literal return context, then it
166 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000167 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000168 Result = Context.DependentTy;
169 break;
170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattnerd658b562008-04-05 06:32:51 +0000172 // Unspecified typespec defaults to int in C90. However, the C90 grammar
173 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
174 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
175 // Note that the one exception to this is function definitions, which are
176 // allowed to be completely missing a declspec. This is handled in the
177 // parser already though by it pretending to have seen an 'int' in this
178 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000179 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000180 // In C89 mode, we only warn if there is a completely missing declspec
181 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000182 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000183 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000184 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000185 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
186 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000187 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000188 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000189 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
190 // "At least one type specifier shall be given in the declaration
191 // specifiers in each declaration, and in the specifier-qualifier list in
192 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000193 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000194 if (TheSema.getLangOptions().CPlusPlus &&
195 !TheSema.getLangOptions().Microsoft) {
196 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000197 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattnerb78d8332009-06-26 04:45:06 +0000199 // When this occurs in C++ code, often something is very broken with the
200 // value being declared, poison it as invalid so we don't get chains of
201 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000202 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000203 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000204 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000205 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000206 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
209 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000210 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
212 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000213 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
214 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
215 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000216 case DeclSpec::TSW_longlong:
217 Result = Context.LongLongTy;
218
219 // long long is a C99 feature.
220 if (!TheSema.getLangOptions().C99 &&
221 !TheSema.getLangOptions().CPlusPlus0x)
222 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
223 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
225 } else {
226 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000227 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
228 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
229 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000230 case DeclSpec::TSW_longlong:
231 Result = Context.UnsignedLongLongTy;
232
233 // long long is a C99 feature.
234 if (!TheSema.getLangOptions().C99 &&
235 !TheSema.getLangOptions().CPlusPlus0x)
236 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
237 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 }
239 }
Chris Lattner958858e2008-02-20 21:40:32 +0000240 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000241 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000242 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000243 case DeclSpec::TST_double:
244 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000245 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000246 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000247 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000248 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000249 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 case DeclSpec::TST_decimal32: // _Decimal32
251 case DeclSpec::TST_decimal64: // _Decimal64
252 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000253 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000254 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000255 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000256 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000257 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 case DeclSpec::TST_enum:
259 case DeclSpec::TST_union:
260 case DeclSpec::TST_struct: {
Douglas Gregorc7621a62009-11-05 20:54:04 +0000261 TypeDecl *D
262 = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000263 if (!D) {
264 // This can happen in C++ with ambiguous lookups.
265 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000266 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000267 break;
268 }
269
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000270 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000271 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000274 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000277 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000278
279 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000280 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000281 TagDecl::TagKind Tag
282 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
283 Result = Context.getElaboratedType(Result, Tag);
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner5153ee62009-04-25 08:47:54 +0000286 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000287 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000288 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000289 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000290 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
292 DS.getTypeSpecSign() == 0 &&
293 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000294 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000295
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000296 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000297 if (const ObjCInterfaceType *
298 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000299 // It would be nice if protocol qualifiers were only stored with the
300 // ObjCObjectPointerType. Unfortunately, this isn't possible due
301 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000302 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000303 // typedef Foo<P> T;
304 // static void func() {
305 // Foo<P> *yy;
306 // T *zz;
307 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000308 Result = Context.getObjCInterfaceType(Interface->getDecl(),
309 (ObjCProtocolDecl**)PQ,
310 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000311 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000312 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000313 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000314 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
315 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000316 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000317 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000318 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000319 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000320 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000321 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000322 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000323 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000327 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 }
Chris Lattner958858e2008-02-20 21:40:32 +0000329 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000330 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000331 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000332 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000333 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000334 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000335 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000336 case DeclSpec::TST_typeofExpr: {
337 Expr *E = static_cast<Expr *>(DS.getTypeRep());
338 assert(E && "Didn't get an expression for typeof?");
339 // TypeQuals handled by caller.
Douglas Gregor4b52e252009-12-21 23:17:24 +0000340 Result = TheSema.BuildTypeofExprType(E);
341 if (Result.isNull()) {
342 Result = Context.IntTy;
343 TheDeclarator.setInvalidType(true);
344 }
Chris Lattner958858e2008-02-20 21:40:32 +0000345 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000346 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000347 case DeclSpec::TST_decltype: {
348 Expr *E = static_cast<Expr *>(DS.getTypeRep());
349 assert(E && "Didn't get an expression for decltype?");
350 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000351 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000352 if (Result.isNull()) {
353 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000354 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000355 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000356 break;
357 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000358 case DeclSpec::TST_auto: {
359 // TypeQuals handled by caller.
360 Result = Context.UndeducedAutoTy;
361 break;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Douglas Gregor809070a2009-02-18 17:45:20 +0000364 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000365 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000366 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000367 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner958858e2008-02-20 21:40:32 +0000370 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000371 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000372 if (TheSema.getLangOptions().Freestanding)
373 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000374 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000375 } else if (DS.isTypeAltiVecVector()) {
376 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
377 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
378 Result = Context.getVectorType(Result, 128/typeSize, true,
379 DS.isTypeAltiVecPixel());
Douglas Gregorf244cd72009-02-14 21:06:05 +0000380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner958858e2008-02-20 21:40:32 +0000382 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
383 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner38d8b982008-02-20 22:04:11 +0000385 // See if there are any attributes on the declspec that apply to the type (as
386 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000387 if (const AttributeList *AL = DS.getAttributes())
John McCall04a67a62010-02-05 21:31:56 +0000388 ProcessTypeAttributeList(TheSema, Result, AL, Delayed);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner96b77fc2008-04-02 06:50:17 +0000390 // Apply const/volatile/restrict qualifiers to T.
391 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
392
393 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
394 // or incomplete types shall not be restrict-qualified." C++ also allows
395 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000396 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000397 if (Result->isAnyPointerType() || Result->isReferenceType()) {
398 QualType EltTy;
399 if (Result->isObjCObjectPointerType())
400 EltTy = Result;
401 else
402 EltTy = Result->isPointerType() ?
403 Result->getAs<PointerType>()->getPointeeType() :
404 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregorbad0e652009-03-24 20:32:41 +0000406 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000407 // incomplete type.
408 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000409 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000410 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000411 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000412 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000413 }
414 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000415 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000416 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000417 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000418 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000419 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner96b77fc2008-04-02 06:50:17 +0000422 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
423 // of a function type includes any type qualifiers, the behavior is
424 // undefined."
425 if (Result->isFunctionType() && TypeQuals) {
426 // Get some location to point at, either the C or V location.
427 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000428 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000429 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000430 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000431 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000432 else {
433 assert((TypeQuals & DeclSpec::TQ_restrict) &&
434 "Has CVR quals but not C, V, or R?");
435 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000436 }
Chris Lattner1564e392009-10-25 18:07:27 +0000437 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000438 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000441 // C++ [dcl.ref]p1:
442 // Cv-qualified references are ill-formed except when the
443 // cv-qualifiers are introduced through the use of a typedef
444 // (7.1.3) or of a template type argument (14.3), in which
445 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000446 // FIXME: Shouldn't we be checking SCS_typedef here?
447 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000448 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000449 TypeQuals &= ~DeclSpec::TQ_const;
450 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000451 }
452
John McCall0953e762009-09-24 19:53:00 +0000453 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
454 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000455 }
John McCall0953e762009-09-24 19:53:00 +0000456
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000457 return Result;
458}
459
Douglas Gregorcd281c32009-02-28 00:25:32 +0000460static std::string getPrintableNameForEntity(DeclarationName Entity) {
461 if (Entity)
462 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Douglas Gregorcd281c32009-02-28 00:25:32 +0000464 return "type name";
465}
466
467/// \brief Build a pointer type.
468///
469/// \param T The type to which we'll be building a pointer.
470///
471/// \param Quals The cvr-qualifiers to be applied to the pointer type.
472///
473/// \param Loc The location of the entity whose type involves this
474/// pointer type or, if there is no such entity, the location of the
475/// type that will have pointer type.
476///
477/// \param Entity The name of the entity that involves the pointer
478/// type, if known.
479///
480/// \returns A suitable pointer type, if there are no
481/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000482QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000483 SourceLocation Loc, DeclarationName Entity) {
484 if (T->isReferenceType()) {
485 // C++ 8.3.2p4: There shall be no ... pointers to references ...
486 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000487 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000488 return QualType();
489 }
490
John McCall0953e762009-09-24 19:53:00 +0000491 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
492
Douglas Gregorcd281c32009-02-28 00:25:32 +0000493 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
494 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000495 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000496 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
497 << T;
John McCall0953e762009-09-24 19:53:00 +0000498 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000499 }
500
501 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000502 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000503}
504
505/// \brief Build a reference type.
506///
507/// \param T The type to which we'll be building a reference.
508///
John McCall0953e762009-09-24 19:53:00 +0000509/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000510///
511/// \param Loc The location of the entity whose type involves this
512/// reference type or, if there is no such entity, the location of the
513/// type that will have reference type.
514///
515/// \param Entity The name of the entity that involves the reference
516/// type, if known.
517///
518/// \returns A suitable reference type, if there are no
519/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000520QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
521 unsigned CVR, SourceLocation Loc,
522 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000523 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000524
525 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
526
527 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
528 // reference to a type T, and attempt to create the type "lvalue
529 // reference to cv TD" creates the type "lvalue reference to T".
530 // We use the qualifiers (restrict or none) of the original reference,
531 // not the new ones. This is consistent with GCC.
532
533 // C++ [dcl.ref]p4: There shall be no references to references.
534 //
535 // According to C++ DR 106, references to references are only
536 // diagnosed when they are written directly (e.g., "int & &"),
537 // but not when they happen via a typedef:
538 //
539 // typedef int& intref;
540 // typedef intref& intref2;
541 //
542 // Parser::ParseDeclaratorInternal diagnoses the case where
543 // references are written directly; here, we handle the
544 // collapsing of references-to-references as described in C++
545 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000546
547 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000548 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000549 // is ill-formed.
550 if (T->isVoidType()) {
551 Diag(Loc, diag::err_reference_to_void);
552 return QualType();
553 }
554
555 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
556 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000557 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000558 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
559 << T;
John McCall0953e762009-09-24 19:53:00 +0000560 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000561 }
562
563 // C++ [dcl.ref]p1:
564 // [...] Cv-qualified references are ill-formed except when the
565 // cv-qualifiers are introduced through the use of a typedef
566 // (7.1.3) or of a template type argument (14.3), in which case
567 // the cv-qualifiers are ignored.
568 //
569 // We diagnose extraneous cv-qualifiers for the non-typedef,
570 // non-template type argument case within the parser. Here, we just
571 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000572 Quals.removeConst();
573 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000574
575 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000576 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000577 return Context.getQualifiedType(
578 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000579 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000580}
581
582/// \brief Build an array type.
583///
584/// \param T The type of each element in the array.
585///
586/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000587///
588/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000589///
590/// \param Quals The cvr-qualifiers to be applied to the array's
591/// element type.
592///
593/// \param Loc The location of the entity whose type involves this
594/// array type or, if there is no such entity, the location of the
595/// type that will have array type.
596///
597/// \param Entity The name of the entity that involves the array
598/// type, if known.
599///
600/// \returns A suitable array type, if there are no errors. Otherwise,
601/// returns a NULL type.
602QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
603 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000604 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000605
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000606 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000607 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000608 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000609 // Not in C++, though. There we only dislike void.
610 if (getLangOptions().CPlusPlus) {
611 if (T->isVoidType()) {
612 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
613 return QualType();
614 }
615 } else {
616 if (RequireCompleteType(Loc, T,
617 diag::err_illegal_decl_array_incomplete_type))
618 return QualType();
619 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620
621 if (T->isFunctionType()) {
622 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000623 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000624 return QualType();
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Douglas Gregorcd281c32009-02-28 00:25:32 +0000627 // C++ 8.3.2p4: There shall be no ... arrays of references ...
628 if (T->isReferenceType()) {
629 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000630 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000631 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000632 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000633
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000634 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000635 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000636 << getPrintableNameForEntity(Entity);
637 return QualType();
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek6217b802009-07-29 21:53:49 +0000640 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000641 // If the element type is a struct or union that contains a variadic
642 // array, accept it as a GNU extension: C99 6.7.2.1p2.
643 if (EltTy->getDecl()->hasFlexibleArrayMember())
644 Diag(Loc, diag::ext_flexible_array_in_array) << T;
645 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000646 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
647 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregorcd281c32009-02-28 00:25:32 +0000650 // C99 6.7.5.2p1: The size expression shall have integer type.
651 if (ArraySize && !ArraySize->isTypeDependent() &&
652 !ArraySize->getType()->isIntegerType()) {
653 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
654 << ArraySize->getType() << ArraySize->getSourceRange();
655 ArraySize->Destroy(Context);
656 return QualType();
657 }
658 llvm::APSInt ConstVal(32);
659 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000660 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000661 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000662 else
663 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000664 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000665 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000666 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000667 (!T->isDependentType() && !T->isIncompleteType() &&
668 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000669 // Per C99, a variable array is an array with either a non-constant
670 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000671 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000672 } else {
673 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
674 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000675 if (ConstVal.isSigned() && ConstVal.isNegative()) {
676 Diag(ArraySize->getLocStart(),
677 diag::err_typecheck_negative_array_size)
678 << ArraySize->getSourceRange();
679 return QualType();
680 }
681 if (ConstVal == 0) {
682 // GCC accepts zero sized static arrays.
683 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
684 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000685 }
John McCall46a617a2009-10-16 00:14:28 +0000686 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000687 }
David Chisnallaf407762010-01-11 23:08:08 +0000688 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
689 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000690 if (ArraySize && !ArraySize->isTypeDependent() &&
691 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000692 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000693 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000695 Diag(Loc,
696 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
697 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000698 }
699
700 return T;
701}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000702
703/// \brief Build an ext-vector type.
704///
705/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000706QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000707 SourceLocation AttrLoc) {
708
709 Expr *Arg = (Expr *)ArraySize.get();
710
711 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
712 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000713 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000714 !T->isIntegerType() && !T->isRealFloatingType()) {
715 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
716 return QualType();
717 }
718
719 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
720 llvm::APSInt vecSize(32);
721 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
722 Diag(AttrLoc, diag::err_attribute_argument_not_int)
723 << "ext_vector_type" << Arg->getSourceRange();
724 return QualType();
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
727 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000728 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000729 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
730
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000731 if (vectorSize == 0) {
732 Diag(AttrLoc, diag::err_attribute_zero_size)
733 << Arg->getSourceRange();
734 return QualType();
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000737 if (!T->isDependentType())
738 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000739 }
740
741 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000742 AttrLoc);
743}
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Douglas Gregor724651c2009-02-28 01:04:19 +0000745/// \brief Build a function type.
746///
747/// This routine checks the function type according to C++ rules and
748/// under the assumption that the result type and parameter types have
749/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000750/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000751/// simpler form that is only suitable for this narrow use case.
752///
753/// \param T The return type of the function.
754///
755/// \param ParamTypes The parameter types of the function. This array
756/// will be modified to account for adjustments to the types of the
757/// function parameters.
758///
759/// \param NumParamTypes The number of parameter types in ParamTypes.
760///
761/// \param Variadic Whether this is a variadic function type.
762///
763/// \param Quals The cvr-qualifiers to be applied to the function type.
764///
765/// \param Loc The location of the entity whose type involves this
766/// function type or, if there is no such entity, the location of the
767/// type that will have function type.
768///
769/// \param Entity The name of the entity that involves the function
770/// type, if known.
771///
772/// \returns A suitable function type, if there are no
773/// errors. Otherwise, returns a NULL type.
774QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000775 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000776 unsigned NumParamTypes,
777 bool Variadic, unsigned Quals,
778 SourceLocation Loc, DeclarationName Entity) {
779 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000780 Diag(Loc, diag::err_func_returning_array_function)
781 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000782 return QualType();
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Douglas Gregor724651c2009-02-28 01:04:19 +0000785 bool Invalid = false;
786 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000787 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
788 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000789 Diag(Loc, diag::err_param_with_void_type);
790 Invalid = true;
791 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000792
John McCall54e14c42009-10-22 22:37:11 +0000793 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000794 }
795
796 if (Invalid)
797 return QualType();
798
Mike Stump1eb44332009-09-09 15:08:12 +0000799 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregorce056bc2010-02-21 22:15:06 +0000800 Quals, false, false, 0, 0, false, CC_Default);
Douglas Gregor724651c2009-02-28 01:04:19 +0000801}
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregor949bf692009-06-09 22:17:39 +0000803/// \brief Build a member pointer type \c T Class::*.
804///
805/// \param T the type to which the member pointer refers.
806/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000807/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000808/// \param Loc the location where this type begins
809/// \param Entity the name of the entity that will have this member pointer type
810///
811/// \returns a member pointer type, if successful, or a NULL type if there was
812/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000813QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000814 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000815 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000816 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
817
Douglas Gregor949bf692009-06-09 22:17:39 +0000818 // Verify that we're not building a pointer to pointer to function with
819 // exception specification.
820 if (CheckDistantExceptionSpec(T)) {
821 Diag(Loc, diag::err_distant_exception_spec);
822
823 // FIXME: If we're doing this as part of template instantiation,
824 // we should return immediately.
825
826 // Build the type anyway, but use the canonical type so that the
827 // exception specifiers are stripped off.
828 T = Context.getCanonicalType(T);
829 }
830
831 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
832 // with reference type, or "cv void."
833 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000834 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000835 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000836 return QualType();
837 }
838
839 if (T->isVoidType()) {
840 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
841 << (Entity? Entity.getAsString() : "type name");
842 return QualType();
843 }
844
845 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
846 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000847 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000848 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
849 << T;
850
851 // FIXME: If we're doing this as part of template instantiation,
852 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000853 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000854 }
855
856 if (!Class->isDependentType() && !Class->isRecordType()) {
857 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
858 return QualType();
859 }
860
John McCall0953e762009-09-24 19:53:00 +0000861 return Context.getQualifiedType(
862 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000863}
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Anders Carlsson9a917e42009-06-12 22:56:54 +0000865/// \brief Build a block pointer type.
866///
867/// \param T The type to which we'll be building a block pointer.
868///
John McCall0953e762009-09-24 19:53:00 +0000869/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000870///
871/// \param Loc The location of the entity whose type involves this
872/// block pointer type or, if there is no such entity, the location of the
873/// type that will have block pointer type.
874///
875/// \param Entity The name of the entity that involves the block pointer
876/// type, if known.
877///
878/// \returns A suitable block pointer type, if there are no
879/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000880QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000881 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000882 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000883 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000884 Diag(Loc, diag::err_nonfunction_block_type);
885 return QualType();
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
John McCall0953e762009-09-24 19:53:00 +0000888 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
889 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000890}
891
John McCalla93c9342009-12-07 02:54:59 +0000892QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000893 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000894 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000895 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000896 return QualType();
897 }
898
John McCalla93c9342009-12-07 02:54:59 +0000899 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000900 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
901 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000902 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
John McCalla93c9342009-12-07 02:54:59 +0000905 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000906 return QT;
907}
908
Mike Stump98eb8a72009-02-04 22:31:32 +0000909/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000910/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000911///
912/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
913/// owns the declaration of a type (e.g., the definition of a struct
914/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000915QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
John McCalla93c9342009-12-07 02:54:59 +0000916 TypeSourceInfo **TInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000917 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000918 // Determine the type of the declarator. Not all forms of declarator
919 // have a type.
920 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000921
John McCall04a67a62010-02-05 21:31:56 +0000922 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
923
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000924 switch (D.getName().getKind()) {
925 case UnqualifiedId::IK_Identifier:
926 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000927 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000928 case UnqualifiedId::IK_TemplateId:
John McCall04a67a62010-02-05 21:31:56 +0000929 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000930
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000931 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
932 TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000933 // Owned is embedded if it was defined here, or if it is the
934 // very first (i.e., canonical) declaration of this tag type.
935 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
936 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000937 if (OwnedDecl) *OwnedDecl = Owned;
938 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000939 break;
940
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000941 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000942 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000943 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000944 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000945 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000946 T = Context.VoidTy;
947 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000948
949 case UnqualifiedId::IK_ConversionFunctionId:
950 // The result type of a conversion function is the type that it
951 // converts to.
952 T = GetTypeFromParser(D.getName().ConversionFunctionId);
953 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000954 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000955
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000956 if (T.isNull())
957 return T;
958
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000959 if (T == Context.UndeducedAutoTy) {
960 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000962 switch (D.getContext()) {
963 case Declarator::KNRTypeListContext:
964 assert(0 && "K&R type lists aren't allowed in C++");
965 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000966 case Declarator::PrototypeContext:
967 Error = 0; // Function prototype
968 break;
969 case Declarator::MemberContext:
970 switch (cast<TagDecl>(CurContext)->getTagKind()) {
971 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
972 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
973 case TagDecl::TK_union: Error = 2; /* Union member */ break;
974 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000975 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000976 break;
977 case Declarator::CXXCatchContext:
978 Error = 4; // Exception declaration
979 break;
980 case Declarator::TemplateParamContext:
981 Error = 5; // Template parameter
982 break;
983 case Declarator::BlockLiteralContext:
984 Error = 6; // Block literal
985 break;
986 case Declarator::FileContext:
987 case Declarator::BlockContext:
988 case Declarator::ForContext:
989 case Declarator::ConditionContext:
990 case Declarator::TypeNameContext:
991 break;
992 }
993
994 if (Error != -1) {
995 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
996 << Error;
997 T = Context.IntTy;
998 D.setInvalidType(true);
999 }
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregorcd281c32009-02-28 00:25:32 +00001002 // The name we're declaring, if any.
1003 DeclarationName Name;
1004 if (D.getIdentifier())
1005 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001006
John McCall04a67a62010-02-05 21:31:56 +00001007 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
1008
Mike Stump98eb8a72009-02-04 22:31:32 +00001009 // Walk the DeclTypeInfo, building the recursive type as we go.
1010 // DeclTypeInfos are ordered from the identifier out, which is
1011 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001012 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1013 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 switch (DeclType.Kind) {
1015 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +00001016 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001017 // If blocks are disabled, emit an error.
1018 if (!LangOpts.Blocks)
1019 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
1021 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +00001022 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +00001023 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001025 // Verify that we're not building a pointer to pointer to function with
1026 // exception specification.
1027 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1028 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1029 D.setInvalidType(true);
1030 // Build the type anyway.
1031 }
Steve Naroff14108da2009-07-10 23:34:53 +00001032 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +00001033 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +00001034 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +00001035 (ObjCProtocolDecl **)OIT->qual_begin(),
1036 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +00001037 break;
1038 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001039 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 break;
John McCall0953e762009-09-24 19:53:00 +00001041 case DeclaratorChunk::Reference: {
1042 Qualifiers Quals;
1043 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
1044
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001045 // Verify that we're not building a reference to pointer to function with
1046 // exception specification.
1047 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1048 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1049 D.setInvalidType(true);
1050 // Build the type anyway.
1051 }
John McCall0953e762009-09-24 19:53:00 +00001052 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001053 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 break;
John McCall0953e762009-09-24 19:53:00 +00001055 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001057 // Verify that we're not building an array of pointers to function with
1058 // exception specification.
1059 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1060 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1061 D.setInvalidType(true);
1062 // Build the type anyway.
1063 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001064 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001065 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 ArrayType::ArraySizeModifier ASM;
1067 if (ATI.isStar)
1068 ASM = ArrayType::Star;
1069 else if (ATI.hasStatic)
1070 ASM = ArrayType::Static;
1071 else
1072 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001073 if (ASM == ArrayType::Star &&
1074 D.getContext() != Declarator::PrototypeContext) {
1075 // FIXME: This check isn't quite right: it allows star in prototypes
1076 // for function definitions, and disallows some edge cases detailed
1077 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1078 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1079 ASM = ArrayType::Normal;
1080 D.setInvalidType(true);
1081 }
John McCall0953e762009-09-24 19:53:00 +00001082 T = BuildArrayType(T, ASM, ArraySize,
1083 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001084 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 break;
1086 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001087 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 // If the function declarator has a prototype (i.e. it is not () and
1089 // does not have a K&R-style identifier list), then the arguments are part
1090 // of the type, otherwise the argument list is ().
1091 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001092
Chris Lattnercd881292007-12-19 05:31:29 +00001093 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001094 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001095 if ((T->isArrayType() || T->isFunctionType()) &&
1096 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001097 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1098 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001099 T = Context.IntTy;
1100 D.setInvalidType(true);
1101 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001102
Douglas Gregor402abb52009-05-28 23:31:59 +00001103 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1104 // C++ [dcl.fct]p6:
1105 // Types shall not be defined in return or parameter types.
1106 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1107 if (Tag->isDefinition())
1108 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1109 << Context.getTypeDeclType(Tag);
1110 }
1111
Sebastian Redl3cc97262009-05-31 11:47:27 +00001112 // Exception specs are not allowed in typedefs. Complain, but add it
1113 // anyway.
1114 if (FTI.hasExceptionSpec &&
1115 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1116 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1117
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001118 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001119 if (getLangOptions().CPlusPlus) {
1120 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1121 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001122 llvm::SmallVector<QualType, 4> Exceptions;
1123 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001124 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001125 // FIXME: Preserve type source info.
1126 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001127 // Check that the type is valid for an exception spec, and drop it
1128 // if not.
1129 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1130 Exceptions.push_back(ET);
1131 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001132 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1133 FTI.hasExceptionSpec,
1134 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001135 Exceptions.size(), Exceptions.data(),
1136 false, CC_Default);
Douglas Gregor965acbb2009-02-18 07:07:28 +00001137 } else if (FTI.isVariadic) {
1138 // We allow a zero-parameter variadic function in C if the
1139 // function is marked with the "overloadable"
1140 // attribute. Scan for this attribute now.
1141 bool Overloadable = false;
1142 for (const AttributeList *Attrs = D.getAttributes();
1143 Attrs; Attrs = Attrs->getNext()) {
1144 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1145 Overloadable = true;
1146 break;
1147 }
1148 }
1149
1150 if (!Overloadable)
1151 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Douglas Gregorce056bc2010-02-21 22:15:06 +00001152 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0,
1153 false, false, 0, 0, false, CC_Default);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001154 } else {
1155 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001156 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001157 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001158 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001160 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001161 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 } else {
1163 // Otherwise, we have a function with an argument list that is
1164 // potentially variadic.
1165 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001168 ParmVarDecl *Param =
1169 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001170 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001171 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001172
1173 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001174 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // Look for 'void'. void is allowed only as a single argument to a
1177 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001178 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001179 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // If this is something like 'float(int, void)', reject it. 'void'
1181 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1182 // have arguments of incomplete type.
1183 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1184 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001185 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001186 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001187 } else if (FTI.ArgInfo[i].Ident) {
1188 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001190 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001191 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001192 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001193 } else {
1194 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001195 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001196 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattner2ff54262007-07-21 05:18:12 +00001198 // Do not add 'void' to the ArgTys list.
1199 break;
1200 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001201 } else if (!FTI.hasPrototype) {
1202 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001203 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001204 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001205 if (BTy->getKind() == BuiltinType::Float)
1206 ArgTy = Context.DoubleTy;
1207 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Mike Stump1eb44332009-09-09 15:08:12 +00001209
John McCall54e14c42009-10-22 22:37:11 +00001210 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001212
1213 llvm::SmallVector<QualType, 4> Exceptions;
1214 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001215 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001216 // FIXME: Preserve type source info.
1217 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001218 // Check that the type is valid for an exception spec, and drop it if
1219 // not.
1220 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1221 Exceptions.push_back(ET);
1222 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001223
Jay Foadbeaaccd2009-05-21 09:52:38 +00001224 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001225 FTI.isVariadic, FTI.TypeQuals,
1226 FTI.hasExceptionSpec,
1227 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001228 Exceptions.size(), Exceptions.data(),
1229 false, CC_Default);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 }
John McCall04a67a62010-02-05 21:31:56 +00001231
1232 // For GCC compatibility, we allow attributes that apply only to
1233 // function types to be placed on a function's return type
1234 // instead (as long as that type doesn't happen to be function
1235 // or function-pointer itself).
1236 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
1237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 break;
1239 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001240 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001241 // Verify that we're not building a pointer to pointer to function with
1242 // exception specification.
1243 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1244 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1245 D.setInvalidType(true);
1246 // Build the type anyway.
1247 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001248 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001249 QualType ClsType;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001250 if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1251 || dyn_cast_or_null<CXXRecordDecl>(
1252 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001253 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001254 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001255 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1256 switch (NNS->getKind()) {
1257 case NestedNameSpecifier::Identifier:
1258 ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
1259 break;
1260
1261 case NestedNameSpecifier::Namespace:
1262 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001263 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001264 break;
1265
1266 case NestedNameSpecifier::TypeSpec:
1267 case NestedNameSpecifier::TypeSpecWithTemplate:
1268 ClsType = QualType(NNS->getAsType(), 0);
1269 if (NNSPrefix)
1270 ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
1271 break;
1272 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001273 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001274 Diag(DeclType.Mem.Scope().getBeginLoc(),
1275 diag::err_illegal_decl_mempointer_in_nonclass)
1276 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1277 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001278 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001279 }
1280
Douglas Gregor949bf692009-06-09 22:17:39 +00001281 if (!ClsType.isNull())
1282 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1283 DeclType.Loc, D.getIdentifier());
1284 if (T.isNull()) {
1285 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001286 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001287 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001288 break;
1289 }
1290
Douglas Gregorcd281c32009-02-28 00:25:32 +00001291 if (T.isNull()) {
1292 D.setInvalidType(true);
1293 T = Context.IntTy;
1294 }
1295
John McCall04a67a62010-02-05 21:31:56 +00001296 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
1297
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001298 // See if there are any attributes on this declarator chunk.
1299 if (const AttributeList *AL = DeclType.getAttrs())
John McCall04a67a62010-02-05 21:31:56 +00001300 ProcessTypeAttributeList(*this, T, AL, FnAttrsFromPreviousChunk);
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001302
1303 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001304 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001305 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001306
1307 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1308 // for a nonstatic member function, the function type to which a pointer
1309 // to member refers, or the top-level function type of a function typedef
1310 // declaration.
1311 if (FnTy->getTypeQuals() != 0 &&
1312 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001313 ((D.getContext() != Declarator::MemberContext &&
1314 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001315 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1316 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001317 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001318 if (D.isFunctionDeclarator())
1319 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1320 else
1321 Diag(D.getIdentifierLoc(),
1322 diag::err_invalid_qualified_typedef_function_type_use);
1323
1324 // Strip the cv-quals from the type.
1325 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00001326 FnTy->getNumArgs(), FnTy->isVariadic(), 0,
1327 false, false, 0, 0, false, CC_Default);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001328 }
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
John McCall04a67a62010-02-05 21:31:56 +00001331 // Process any function attributes we might have delayed from the
1332 // declaration-specifiers.
1333 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
1334
1335 // If there were any type attributes applied to the decl itself, not
1336 // the type, apply them to the result type. But don't do this for
1337 // block-literal expressions, which are parsed wierdly.
1338 if (D.getContext() != Declarator::BlockLiteralContext)
1339 if (const AttributeList *Attrs = D.getAttributes())
1340 ProcessTypeAttributeList(*this, T, Attrs, FnAttrsFromPreviousChunk);
1341
1342 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001343
John McCalla93c9342009-12-07 02:54:59 +00001344 if (TInfo) {
John McCall54e14c42009-10-22 22:37:11 +00001345 if (D.isInvalidType())
John McCalla93c9342009-12-07 02:54:59 +00001346 *TInfo = 0;
John McCall54e14c42009-10-22 22:37:11 +00001347 else
John McCalla93c9342009-12-07 02:54:59 +00001348 *TInfo = GetTypeSourceInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001349 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 return T;
1352}
1353
John McCall51bd8032009-10-18 01:05:36 +00001354namespace {
1355 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1356 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001357
John McCall51bd8032009-10-18 01:05:36 +00001358 public:
1359 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001360
John McCall51bd8032009-10-18 01:05:36 +00001361 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1362 Visit(TL.getUnqualifiedLoc());
1363 }
1364 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1365 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1366 }
1367 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1368 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001369
John McCall54e14c42009-10-22 22:37:11 +00001370 if (DS.getProtocolQualifiers()) {
1371 assert(TL.getNumProtocols() > 0);
1372 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1373 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1374 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1375 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1376 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1377 } else {
1378 assert(TL.getNumProtocols() == 0);
1379 TL.setLAngleLoc(SourceLocation());
1380 TL.setRAngleLoc(SourceLocation());
1381 }
1382 }
1383 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1384 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1385
1386 TL.setStarLoc(SourceLocation());
1387
1388 if (DS.getProtocolQualifiers()) {
1389 assert(TL.getNumProtocols() > 0);
1390 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1391 TL.setHasProtocolsAsWritten(true);
1392 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1393 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1394 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1395 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1396
1397 } else {
1398 assert(TL.getNumProtocols() == 0);
1399 TL.setHasProtocolsAsWritten(false);
1400 TL.setLAngleLoc(SourceLocation());
1401 TL.setRAngleLoc(SourceLocation());
1402 }
1403
1404 // This might not have been written with an inner type.
1405 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1406 TL.setHasBaseTypeAsWritten(false);
1407 TL.getBaseTypeLoc().initialize(SourceLocation());
1408 } else {
1409 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001410 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001411 }
John McCall51bd8032009-10-18 01:05:36 +00001412 }
John McCall833ca992009-10-29 08:12:44 +00001413 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001414 TypeSourceInfo *TInfo = 0;
1415 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001416
1417 // If we got no declarator info from previous Sema routines,
1418 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001419 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001420 TL.initialize(DS.getTypeSpecTypeLoc());
1421 return;
1422 }
1423
1424 TemplateSpecializationTypeLoc OldTL =
John McCalla93c9342009-12-07 02:54:59 +00001425 cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc());
John McCall833ca992009-10-29 08:12:44 +00001426 TL.copy(OldTL);
1427 }
John McCallcfb708c2010-01-13 20:03:27 +00001428 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1429 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1430 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1431 TL.setParensRange(DS.getTypeofParensRange());
1432 }
1433 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1434 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1435 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1436 TL.setParensRange(DS.getTypeofParensRange());
1437 assert(DS.getTypeRep());
1438 TypeSourceInfo *TInfo = 0;
1439 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1440 TL.setUnderlyingTInfo(TInfo);
1441 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001442 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1443 // By default, use the source location of the type specifier.
1444 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1445 if (TL.needsExtraLocalData()) {
1446 // Set info for the written builtin specifiers.
1447 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1448 // Try to have a meaningful source location.
1449 if (TL.getWrittenSignSpec() != TSS_unspecified)
1450 // Sign spec loc overrides the others (e.g., 'unsigned long').
1451 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1452 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1453 // Width spec loc overrides type spec loc (e.g., 'short int').
1454 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1455 }
1456 }
John McCall51bd8032009-10-18 01:05:36 +00001457 void VisitTypeLoc(TypeLoc TL) {
1458 // FIXME: add other typespec types and change this to an assert.
1459 TL.initialize(DS.getTypeSpecTypeLoc());
1460 }
1461 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001462
John McCall51bd8032009-10-18 01:05:36 +00001463 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1464 const DeclaratorChunk &Chunk;
1465
1466 public:
1467 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1468
1469 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001470 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001471 }
1472
1473 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1474 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1475 TL.setCaretLoc(Chunk.Loc);
1476 }
1477 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1478 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1479 TL.setStarLoc(Chunk.Loc);
1480 }
1481 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1482 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1483 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001484 TL.setHasBaseTypeAsWritten(true);
1485 TL.setHasProtocolsAsWritten(false);
1486 TL.setLAngleLoc(SourceLocation());
1487 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001488 }
1489 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1490 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1491 TL.setStarLoc(Chunk.Loc);
1492 // FIXME: nested name specifier
1493 }
1494 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1495 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001496 // 'Amp' is misleading: this might have been originally
1497 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001498 TL.setAmpLoc(Chunk.Loc);
1499 }
1500 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1501 assert(Chunk.Kind == DeclaratorChunk::Reference);
1502 assert(!Chunk.Ref.LValueRef);
1503 TL.setAmpAmpLoc(Chunk.Loc);
1504 }
1505 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1506 assert(Chunk.Kind == DeclaratorChunk::Array);
1507 TL.setLBracketLoc(Chunk.Loc);
1508 TL.setRBracketLoc(Chunk.EndLoc);
1509 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1510 }
1511 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1512 assert(Chunk.Kind == DeclaratorChunk::Function);
1513 TL.setLParenLoc(Chunk.Loc);
1514 TL.setRParenLoc(Chunk.EndLoc);
1515
1516 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001517 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001518 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001519 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001520 }
1521 // FIXME: exception specs
1522 }
1523
1524 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001525 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001526 }
1527 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001528}
1529
John McCalla93c9342009-12-07 02:54:59 +00001530/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001531///
1532/// \param T QualType referring to the type as written in source code.
John McCalla93c9342009-12-07 02:54:59 +00001533TypeSourceInfo *
1534Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T) {
1535 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1536 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001537
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001538 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001539 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1540 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001541 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001542
John McCall51bd8032009-10-18 01:05:36 +00001543 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001544
John McCalla93c9342009-12-07 02:54:59 +00001545 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001546}
1547
John McCalla93c9342009-12-07 02:54:59 +00001548/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1549QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001550 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1551 // and Sema during declaration parsing. Try deallocating/caching them when
1552 // it's appropriate, instead of allocating them and keeping them around.
1553 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
John McCalla93c9342009-12-07 02:54:59 +00001554 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001555 assert(LocT->getTypeClass() != T->getTypeClass() &&
1556 "LocInfoType's TypeClass conflicts with an existing Type class");
1557 return QualType(LocT, 0);
1558}
1559
1560void LocInfoType::getAsStringInternal(std::string &Str,
1561 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001562 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1563 " was used directly instead of getting the QualType through"
1564 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001565}
1566
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001567/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1568/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1569/// they point to and return true. If T1 and T2 aren't pointer types
1570/// or pointer-to-member types, or if they are not similar at this
1571/// level, returns false and leaves T1 and T2 unchanged. Top-level
1572/// qualifiers on T1 and T2 are ignored. This function will typically
1573/// be called in a loop that successively "unwraps" pointer and
1574/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001575bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001576 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1577 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001578 if (T1PtrType && T2PtrType) {
1579 T1 = T1PtrType->getPointeeType();
1580 T2 = T2PtrType->getPointeeType();
1581 return true;
1582 }
1583
Ted Kremenek6217b802009-07-29 21:53:49 +00001584 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1585 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001586 if (T1MPType && T2MPType &&
1587 Context.getCanonicalType(T1MPType->getClass()) ==
1588 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001589 T1 = T1MPType->getPointeeType();
1590 T2 = T2MPType->getPointeeType();
1591 return true;
1592 }
Douglas Gregor57373262008-10-22 14:17:15 +00001593 return false;
1594}
1595
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001596Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 // C99 6.7.6: Type names have no identifier. This is already validated by
1598 // the parser.
1599 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001600
John McCalla93c9342009-12-07 02:54:59 +00001601 TypeSourceInfo *TInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001602 TagDecl *OwnedTag = 0;
John McCalla93c9342009-12-07 02:54:59 +00001603 QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001604 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001605 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001606
Douglas Gregor402abb52009-05-28 23:31:59 +00001607 if (getLangOptions().CPlusPlus) {
1608 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001609 CheckExtraCXXDefaultArguments(D);
1610
Douglas Gregor402abb52009-05-28 23:31:59 +00001611 // C++0x [dcl.type]p3:
1612 // A type-specifier-seq shall not define a class or enumeration
1613 // unless it appears in the type-id of an alias-declaration
1614 // (7.1.3).
1615 if (OwnedTag && OwnedTag->isDefinition())
1616 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1617 << Context.getTypeDeclType(OwnedTag);
1618 }
1619
John McCalla93c9342009-12-07 02:54:59 +00001620 if (TInfo)
1621 T = CreateLocInfoType(T, TInfo);
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001622
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 return T.getAsOpaquePtr();
1624}
1625
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001626
1627
1628//===----------------------------------------------------------------------===//
1629// Type Attribute Processing
1630//===----------------------------------------------------------------------===//
1631
1632/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1633/// specified type. The attribute contains 1 argument, the id of the address
1634/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001635static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001636 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001637
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001638 // If this type is already address space qualified, reject it.
1639 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1640 // for two or more different address spaces."
1641 if (Type.getAddressSpace()) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1643 return;
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001646 // Check the attribute arguments.
1647 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001649 return;
1650 }
1651 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1652 llvm::APSInt addrSpace(32);
1653 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001654 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1655 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001656 return;
1657 }
1658
John McCallefadb772009-07-28 06:52:18 +00001659 // Bounds checking.
1660 if (addrSpace.isSigned()) {
1661 if (addrSpace.isNegative()) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1663 << ASArgExpr->getSourceRange();
1664 return;
1665 }
1666 addrSpace.setIsSigned(false);
1667 }
1668 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001669 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001670 if (addrSpace > max) {
1671 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001672 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001673 return;
1674 }
1675
Mike Stump1eb44332009-09-09 15:08:12 +00001676 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001677 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001678}
1679
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001680/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1681/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001682static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001683 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001684 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001685 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001686 return;
1687 }
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001689 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001691 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1692 << "objc_gc" << 1;
1693 return;
1694 }
John McCall0953e762009-09-24 19:53:00 +00001695 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001696 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001697 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1698 return;
1699 }
Mike Stump1eb44332009-09-09 15:08:12 +00001700 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001701 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001702 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001703 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001704 else {
1705 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1706 << "objc_gc" << Attr.getParameterName();
1707 return;
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001710 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001711}
1712
John McCall04a67a62010-02-05 21:31:56 +00001713/// Process an individual function attribute. Returns true if the
1714/// attribute does not make sense to apply to this type.
1715bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
1716 if (Attr.getKind() == AttributeList::AT_noreturn) {
1717 // Complain immediately if the arg count is wrong.
1718 if (Attr.getNumArgs() != 0) {
1719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1720 return false;
1721 }
Mike Stump24556362009-07-25 21:26:53 +00001722
John McCall04a67a62010-02-05 21:31:56 +00001723 // Delay if this is not a function or pointer to block.
1724 if (!Type->isFunctionPointerType()
1725 && !Type->isBlockPointerType()
1726 && !Type->isFunctionType())
1727 return true;
Mike Stump24556362009-07-25 21:26:53 +00001728
John McCall04a67a62010-02-05 21:31:56 +00001729 // Otherwise we can process right away.
1730 Type = S.Context.getNoReturnType(Type);
1731 return false;
1732 }
Mike Stump24556362009-07-25 21:26:53 +00001733
John McCall04a67a62010-02-05 21:31:56 +00001734 // Otherwise, a calling convention.
1735 if (Attr.getNumArgs() != 0) {
1736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1737 return false;
1738 }
John McCallf82b4e82010-02-04 05:44:44 +00001739
John McCall04a67a62010-02-05 21:31:56 +00001740 QualType T = Type;
1741 if (const PointerType *PT = Type->getAs<PointerType>())
1742 T = PT->getPointeeType();
1743 const FunctionType *Fn = T->getAs<FunctionType>();
John McCallf82b4e82010-02-04 05:44:44 +00001744
John McCall04a67a62010-02-05 21:31:56 +00001745 // Delay if the type didn't work out to a function.
1746 if (!Fn) return true;
1747
1748 // TODO: diagnose uses of these conventions on the wrong target.
1749 CallingConv CC;
1750 switch (Attr.getKind()) {
1751 case AttributeList::AT_cdecl: CC = CC_C; break;
1752 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
1753 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
1754 default: llvm_unreachable("unexpected attribute kind"); return false;
1755 }
1756
1757 CallingConv CCOld = Fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00001758 if (S.Context.getCanonicalCallConv(CC) ==
1759 S.Context.getCanonicalCallConv(CCOld)) return false;
John McCall04a67a62010-02-05 21:31:56 +00001760
1761 if (CCOld != CC_Default) {
1762 // Should we diagnose reapplications of the same convention?
1763 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1764 << FunctionType::getNameForCallConv(CC)
1765 << FunctionType::getNameForCallConv(CCOld);
1766 return false;
1767 }
1768
1769 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
1770 if (CC == CC_X86FastCall) {
1771 if (isa<FunctionNoProtoType>(Fn)) {
1772 S.Diag(Attr.getLoc(), diag::err_cconv_knr)
1773 << FunctionType::getNameForCallConv(CC);
1774 return false;
1775 }
1776
1777 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
1778 if (FnP->isVariadic()) {
1779 S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
1780 << FunctionType::getNameForCallConv(CC);
1781 return false;
1782 }
1783 }
1784
1785 Type = S.Context.getCallConvType(Type, CC);
1786 return false;
John McCallf82b4e82010-02-04 05:44:44 +00001787}
1788
John Thompson6e132aa2009-12-04 21:51:28 +00001789/// HandleVectorSizeAttribute - this attribute is only applicable to integral
1790/// and float scalars, although arrays, pointers, and function return values are
1791/// allowed in conjunction with this construct. Aggregates with this attribute
1792/// are invalid, even if they are of the same size as a corresponding scalar.
1793/// The raw attribute should contain precisely 1 argument, the vector size for
1794/// the variable, measured in bytes. If curType and rawAttr are well formed,
1795/// this routine will return a new vector type.
1796static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
1797 // Check the attribute arugments.
1798 if (Attr.getNumArgs() != 1) {
1799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1800 return;
1801 }
1802 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
1803 llvm::APSInt vecSize(32);
1804 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
1805 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1806 << "vector_size" << sizeExpr->getSourceRange();
1807 return;
1808 }
1809 // the base type must be integer or float, and can't already be a vector.
1810 if (CurType->isVectorType() ||
1811 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
1812 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
1813 return;
1814 }
1815 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
1816 // vecSize is specified in bytes - convert to bits.
1817 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
1818
1819 // the vector size needs to be an integral multiple of the type size.
1820 if (vectorSize % typeSize) {
1821 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
1822 << sizeExpr->getSourceRange();
1823 return;
1824 }
1825 if (vectorSize == 0) {
1826 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
1827 << sizeExpr->getSourceRange();
1828 return;
1829 }
1830
1831 // Success! Instantiate the vector type, the number of elements is > 0, and
1832 // not required to be a power of 2, unlike GCC.
John Thompson82287d12010-02-05 00:12:22 +00001833 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, false, false);
John Thompson6e132aa2009-12-04 21:51:28 +00001834}
1835
John McCall04a67a62010-02-05 21:31:56 +00001836void ProcessTypeAttributeList(Sema &S, QualType &Result,
1837 const AttributeList *AL,
1838 DelayedAttributeSet &FnAttrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00001839 // Scan through and apply attributes to this type where it makes sense. Some
1840 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1841 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001842 // apply to the decl. Here we apply type attributes and ignore the rest.
1843 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001844 // If this is an attribute we can handle, do so now, otherwise, add it to
1845 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001846 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001847 default: break;
John McCall04a67a62010-02-05 21:31:56 +00001848
Chris Lattner232e8822008-02-21 01:08:11 +00001849 case AttributeList::AT_address_space:
John McCall04a67a62010-02-05 21:31:56 +00001850 HandleAddressSpaceTypeAttribute(Result, *AL, S);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001851 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001852 case AttributeList::AT_objc_gc:
John McCall04a67a62010-02-05 21:31:56 +00001853 HandleObjCGCTypeAttribute(Result, *AL, S);
Mike Stump24556362009-07-25 21:26:53 +00001854 break;
John Thompson6e132aa2009-12-04 21:51:28 +00001855 case AttributeList::AT_vector_size:
John McCall04a67a62010-02-05 21:31:56 +00001856 HandleVectorSizeAttr(Result, *AL, S);
1857 break;
1858
1859 case AttributeList::AT_noreturn:
1860 case AttributeList::AT_cdecl:
1861 case AttributeList::AT_fastcall:
1862 case AttributeList::AT_stdcall:
1863 if (ProcessFnAttr(S, Result, *AL))
1864 FnAttrs.push_back(DelayedAttribute(AL, Result));
John Thompson6e132aa2009-12-04 21:51:28 +00001865 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001866 }
Chris Lattner232e8822008-02-21 01:08:11 +00001867 }
Chris Lattner232e8822008-02-21 01:08:11 +00001868}
1869
Mike Stump1eb44332009-09-09 15:08:12 +00001870/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001871///
1872/// This routine checks whether the type @p T is complete in any
1873/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001874/// type, returns false. If @p T is a class template specialization,
1875/// this routine then attempts to perform class template
1876/// instantiation. If instantiation fails, or if @p T is incomplete
1877/// and cannot be completed, issues the diagnostic @p diag (giving it
1878/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001879///
1880/// @param Loc The location in the source that the incomplete type
1881/// diagnostic should refer to.
1882///
1883/// @param T The type that this routine is examining for completeness.
1884///
Mike Stump1eb44332009-09-09 15:08:12 +00001885/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001886/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001887///
1888/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1889/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001890bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001891 const PartialDiagnostic &PD,
1892 std::pair<SourceLocation,
1893 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001894 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregor573d9c32009-10-21 23:19:44 +00001896 // FIXME: Add this assertion to make sure we always get instantiation points.
1897 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001898 // FIXME: Add this assertion to help us flush out problems with
1899 // checking for dependent types and type-dependent expressions.
1900 //
Mike Stump1eb44332009-09-09 15:08:12 +00001901 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001902 // "Can't ask whether a dependent type is complete");
1903
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001904 // If we have a complete type, we're done.
1905 if (!T->isIncompleteType())
1906 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001907
Douglas Gregord475b8d2009-03-25 21:17:03 +00001908 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00001909 // class template specialization, or an array with known size of such,
1910 // try to instantiate it.
1911 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00001912 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00001913 MaybeTemplate = Array->getElementType();
1914 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001915 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001916 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001917 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1918 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001919 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001920 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001921 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001922 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1923 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001924 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1925 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001926 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001927 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001928 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001929 return InstantiateClass(Loc, Rec, Pattern,
1930 getTemplateInstantiationArgs(Rec),
1931 TSK_ImplicitInstantiation,
1932 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001933 }
1934 }
1935 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001936
Douglas Gregor5842ba92009-08-24 15:23:48 +00001937 if (diag == 0)
1938 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001940 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001941 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001942
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001943 // If we have a note, produce it.
1944 if (!Note.first.isInvalid())
1945 Diag(Note.first, Note.second);
1946
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001947 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001948 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001949 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001950 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001951 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001952 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001953 Tag = Enum;
1954
1955 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001956 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001957 Tag->isBeingDefined() ? diag::note_type_being_defined
1958 : diag::note_forward_declaration)
1959 << QualType(Tag, 0);
1960
1961 return true;
1962}
Douglas Gregore6258932009-03-19 00:39:20 +00001963
1964/// \brief Retrieve a version of the type 'T' that is qualified by the
1965/// nested-name-specifier contained in SS.
1966QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1967 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1968 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Douglas Gregorab452ba2009-03-26 23:50:42 +00001970 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001971 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001972 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001973}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001974
1975QualType Sema::BuildTypeofExprType(Expr *E) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00001976 if (E->getType() == Context.OverloadTy) {
1977 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
1978 // function template specialization wherever deduction cannot occur.
1979 if (FunctionDecl *Specialization
1980 = ResolveSingleFunctionTemplateSpecialization(E)) {
1981 E = FixOverloadedFunctionReference(E, Specialization);
1982 if (!E)
1983 return QualType();
1984 } else {
1985 Diag(E->getLocStart(),
1986 diag::err_cannot_determine_declared_type_of_overloaded_function)
1987 << false << E->getSourceRange();
1988 return QualType();
1989 }
1990 }
1991
Anders Carlssonaf017e62009-06-29 22:58:55 +00001992 return Context.getTypeOfExprType(E);
1993}
1994
1995QualType Sema::BuildDecltypeType(Expr *E) {
1996 if (E->getType() == Context.OverloadTy) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00001997 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
1998 // function template specialization wherever deduction cannot occur.
1999 if (FunctionDecl *Specialization
2000 = ResolveSingleFunctionTemplateSpecialization(E)) {
2001 E = FixOverloadedFunctionReference(E, Specialization);
2002 if (!E)
2003 return QualType();
2004 } else {
2005 Diag(E->getLocStart(),
2006 diag::err_cannot_determine_declared_type_of_overloaded_function)
2007 << true << E->getSourceRange();
2008 return QualType();
2009 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00002010 }
Douglas Gregor4b52e252009-12-21 23:17:24 +00002011
Anders Carlssonaf017e62009-06-29 22:58:55 +00002012 return Context.getDecltypeType(E);
2013}