blob: 2fb5c84ac90e230a7e2748a2d97ea6278093d5c3 [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,
Charles Davis328ce342010-02-24 02:27:18 +000075 bool IsDeclSpec,
John McCall04a67a62010-02-05 21:31:56 +000076 const AttributeList *Attrs,
77 DelayedAttributeSet &DelayedFnAttrs);
78static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
79
80static void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
81 DelayedAttributeSet &Attrs) {
82 for (DelayedAttributeSet::iterator I = Attrs.begin(),
83 E = Attrs.end(); I != E; ++I)
84 if (ProcessFnAttr(S, Type, *I->first))
85 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
86 << I->first->getName() << I->second;
87 Attrs.clear();
88}
89
90static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
91 for (DelayedAttributeSet::iterator I = Attrs.begin(),
92 E = Attrs.end(); I != E; ++I) {
93 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
94 << I->first->getName() << I->second;
95 }
96 Attrs.clear();
97}
98
Douglas Gregor930d8b52009-01-30 22:09:00 +000099/// \brief Convert the specified declspec to the appropriate type
100/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000101/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000102/// \returns The type described by the declaration specifiers. This function
103/// never returns null.
John McCall04a67a62010-02-05 21:31:56 +0000104static QualType ConvertDeclSpecToType(Sema &TheSema,
105 Declarator &TheDeclarator,
106 DelayedAttributeSet &Delayed) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
108 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000109 const DeclSpec &DS = TheDeclarator.getDeclSpec();
110 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
111 if (DeclLoc.isInvalid())
112 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000113
114 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner5db2bb12009-10-25 18:21:37 +0000116 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000118 case DeclSpec::TST_void:
119 Result = Context.VoidTy;
120 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 case DeclSpec::TST_char:
122 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000123 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000125 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 else {
127 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
128 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000129 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
Chris Lattner958858e2008-02-20 21:40:32 +0000131 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000132 case DeclSpec::TST_wchar:
133 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
134 Result = Context.WCharTy;
135 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000136 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000137 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000138 Result = Context.getSignedWCharType();
139 } else {
140 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
141 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000142 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000143 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000144 Result = Context.getUnsignedWCharType();
145 }
146 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000147 case DeclSpec::TST_char16:
148 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
149 "Unknown TSS value");
150 Result = Context.Char16Ty;
151 break;
152 case DeclSpec::TST_char32:
153 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
154 "Unknown TSS value");
155 Result = Context.Char32Ty;
156 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000157 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000158 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000159 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000160 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000161 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000162 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000163 break;
164 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000165
166 // If this is a missing declspec in a block literal return context, then it
167 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000168 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000169 Result = Context.DependentTy;
170 break;
171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerd658b562008-04-05 06:32:51 +0000173 // Unspecified typespec defaults to int in C90. However, the C90 grammar
174 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
175 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
176 // Note that the one exception to this is function definitions, which are
177 // allowed to be completely missing a declspec. This is handled in the
178 // parser already though by it pretending to have seen an 'int' in this
179 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000180 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000181 // In C89 mode, we only warn if there is a completely missing declspec
182 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000183 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000184 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000185 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000186 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
187 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000188 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000189 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000190 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
191 // "At least one type specifier shall be given in the declaration
192 // specifiers in each declaration, and in the specifier-qualifier list in
193 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000194 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000195 if (TheSema.getLangOptions().CPlusPlus &&
196 !TheSema.getLangOptions().Microsoft) {
197 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000198 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattnerb78d8332009-06-26 04:45:06 +0000200 // When this occurs in C++ code, often something is very broken with the
201 // value being declared, poison it as invalid so we don't get chains of
202 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000203 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000204 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000205 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000206 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000207 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
210 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000211 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
213 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000214 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
215 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
216 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000217 case DeclSpec::TSW_longlong:
218 Result = Context.LongLongTy;
219
220 // long long is a C99 feature.
221 if (!TheSema.getLangOptions().C99 &&
222 !TheSema.getLangOptions().CPlusPlus0x)
223 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
224 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
226 } else {
227 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000228 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
229 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
230 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000231 case DeclSpec::TSW_longlong:
232 Result = Context.UnsignedLongLongTy;
233
234 // long long is a C99 feature.
235 if (!TheSema.getLangOptions().C99 &&
236 !TheSema.getLangOptions().CPlusPlus0x)
237 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
238 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 }
240 }
Chris Lattner958858e2008-02-20 21:40:32 +0000241 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000242 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000243 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000244 case DeclSpec::TST_double:
245 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000246 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000247 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000248 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000249 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000250 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 case DeclSpec::TST_decimal32: // _Decimal32
252 case DeclSpec::TST_decimal64: // _Decimal64
253 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000254 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000255 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000256 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000257 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000258 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 case DeclSpec::TST_enum:
260 case DeclSpec::TST_union:
261 case DeclSpec::TST_struct: {
Douglas Gregorc7621a62009-11-05 20:54:04 +0000262 TypeDecl *D
263 = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000264 if (!D) {
265 // This can happen in C++ with ambiguous lookups.
266 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000267 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000268 break;
269 }
270
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000271 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000272 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000275 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000278 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000279
280 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000281 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000282 TagDecl::TagKind Tag
283 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
284 Result = Context.getElaboratedType(Result, Tag);
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner5153ee62009-04-25 08:47:54 +0000287 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000288 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000289 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000290 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000291 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
293 DS.getTypeSpecSign() == 0 &&
294 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000295 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000296
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000297 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000298 if (const ObjCInterfaceType *
299 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000300 // It would be nice if protocol qualifiers were only stored with the
301 // ObjCObjectPointerType. Unfortunately, this isn't possible due
302 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000303 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000304 // typedef Foo<P> T;
305 // static void func() {
306 // Foo<P> *yy;
307 // T *zz;
308 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000309 Result = Context.getObjCInterfaceType(Interface->getDecl(),
310 (ObjCProtocolDecl**)PQ,
311 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000312 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000313 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000314 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000315 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
316 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000317 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000318 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000319 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000320 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000321 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000322 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000323 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000324 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000328 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Chris Lattner958858e2008-02-20 21:40:32 +0000330 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000331 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000332 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000333 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000334 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000335 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000336 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000337 case DeclSpec::TST_typeofExpr: {
338 Expr *E = static_cast<Expr *>(DS.getTypeRep());
339 assert(E && "Didn't get an expression for typeof?");
340 // TypeQuals handled by caller.
Douglas Gregor4b52e252009-12-21 23:17:24 +0000341 Result = TheSema.BuildTypeofExprType(E);
342 if (Result.isNull()) {
343 Result = Context.IntTy;
344 TheDeclarator.setInvalidType(true);
345 }
Chris Lattner958858e2008-02-20 21:40:32 +0000346 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000347 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000348 case DeclSpec::TST_decltype: {
349 Expr *E = static_cast<Expr *>(DS.getTypeRep());
350 assert(E && "Didn't get an expression for decltype?");
351 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000352 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000353 if (Result.isNull()) {
354 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000355 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000356 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000357 break;
358 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000359 case DeclSpec::TST_auto: {
360 // TypeQuals handled by caller.
361 Result = Context.UndeducedAutoTy;
362 break;
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregor809070a2009-02-18 17:45:20 +0000365 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000366 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000367 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000368 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattner958858e2008-02-20 21:40:32 +0000371 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000372 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000373 if (TheSema.getLangOptions().Freestanding)
374 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000375 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000376 } else if (DS.isTypeAltiVecVector()) {
377 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
378 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
379 Result = Context.getVectorType(Result, 128/typeSize, true,
380 DS.isTypeAltiVecPixel());
Douglas Gregorf244cd72009-02-14 21:06:05 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner958858e2008-02-20 21:40:32 +0000383 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
384 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattner38d8b982008-02-20 22:04:11 +0000386 // See if there are any attributes on the declspec that apply to the type (as
387 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000388 if (const AttributeList *AL = DS.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +0000389 ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner96b77fc2008-04-02 06:50:17 +0000391 // Apply const/volatile/restrict qualifiers to T.
392 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
393
394 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
395 // or incomplete types shall not be restrict-qualified." C++ also allows
396 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000397 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000398 if (Result->isAnyPointerType() || Result->isReferenceType()) {
399 QualType EltTy;
400 if (Result->isObjCObjectPointerType())
401 EltTy = Result;
402 else
403 EltTy = Result->isPointerType() ?
404 Result->getAs<PointerType>()->getPointeeType() :
405 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregorbad0e652009-03-24 20:32:41 +0000407 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000408 // incomplete type.
409 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000410 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000411 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000412 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000413 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000414 }
415 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000416 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000417 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000418 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000419 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000420 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner96b77fc2008-04-02 06:50:17 +0000423 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
424 // of a function type includes any type qualifiers, the behavior is
425 // undefined."
426 if (Result->isFunctionType() && TypeQuals) {
427 // Get some location to point at, either the C or V location.
428 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000429 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000430 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000431 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000432 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000433 else {
434 assert((TypeQuals & DeclSpec::TQ_restrict) &&
435 "Has CVR quals but not C, V, or R?");
436 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000437 }
Chris Lattner1564e392009-10-25 18:07:27 +0000438 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000439 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000442 // C++ [dcl.ref]p1:
443 // Cv-qualified references are ill-formed except when the
444 // cv-qualifiers are introduced through the use of a typedef
445 // (7.1.3) or of a template type argument (14.3), in which
446 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000447 // FIXME: Shouldn't we be checking SCS_typedef here?
448 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000449 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000450 TypeQuals &= ~DeclSpec::TQ_const;
451 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000452 }
453
John McCall0953e762009-09-24 19:53:00 +0000454 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
455 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000456 }
John McCall0953e762009-09-24 19:53:00 +0000457
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000458 return Result;
459}
460
Douglas Gregorcd281c32009-02-28 00:25:32 +0000461static std::string getPrintableNameForEntity(DeclarationName Entity) {
462 if (Entity)
463 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Douglas Gregorcd281c32009-02-28 00:25:32 +0000465 return "type name";
466}
467
468/// \brief Build a pointer type.
469///
470/// \param T The type to which we'll be building a pointer.
471///
472/// \param Quals The cvr-qualifiers to be applied to the pointer type.
473///
474/// \param Loc The location of the entity whose type involves this
475/// pointer type or, if there is no such entity, the location of the
476/// type that will have pointer type.
477///
478/// \param Entity The name of the entity that involves the pointer
479/// type, if known.
480///
481/// \returns A suitable pointer type, if there are no
482/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000483QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000484 SourceLocation Loc, DeclarationName Entity) {
485 if (T->isReferenceType()) {
486 // C++ 8.3.2p4: There shall be no ... pointers to references ...
487 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000488 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000489 return QualType();
490 }
491
John McCall0953e762009-09-24 19:53:00 +0000492 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
493
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
495 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000496 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
498 << T;
John McCall0953e762009-09-24 19:53:00 +0000499 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000500 }
501
502 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000503 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000504}
505
506/// \brief Build a reference type.
507///
508/// \param T The type to which we'll be building a reference.
509///
John McCall0953e762009-09-24 19:53:00 +0000510/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000511///
512/// \param Loc The location of the entity whose type involves this
513/// reference type or, if there is no such entity, the location of the
514/// type that will have reference type.
515///
516/// \param Entity The name of the entity that involves the reference
517/// type, if known.
518///
519/// \returns A suitable reference type, if there are no
520/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000521QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
522 unsigned CVR, SourceLocation Loc,
523 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000524 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000525
526 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
527
528 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
529 // reference to a type T, and attempt to create the type "lvalue
530 // reference to cv TD" creates the type "lvalue reference to T".
531 // We use the qualifiers (restrict or none) of the original reference,
532 // not the new ones. This is consistent with GCC.
533
534 // C++ [dcl.ref]p4: There shall be no references to references.
535 //
536 // According to C++ DR 106, references to references are only
537 // diagnosed when they are written directly (e.g., "int & &"),
538 // but not when they happen via a typedef:
539 //
540 // typedef int& intref;
541 // typedef intref& intref2;
542 //
543 // Parser::ParseDeclaratorInternal diagnoses the case where
544 // references are written directly; here, we handle the
545 // collapsing of references-to-references as described in C++
546 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000547
548 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000549 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000550 // is ill-formed.
551 if (T->isVoidType()) {
552 Diag(Loc, diag::err_reference_to_void);
553 return QualType();
554 }
555
556 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
557 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000558 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000559 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
560 << T;
John McCall0953e762009-09-24 19:53:00 +0000561 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000562 }
563
564 // C++ [dcl.ref]p1:
565 // [...] Cv-qualified references are ill-formed except when the
566 // cv-qualifiers are introduced through the use of a typedef
567 // (7.1.3) or of a template type argument (14.3), in which case
568 // the cv-qualifiers are ignored.
569 //
570 // We diagnose extraneous cv-qualifiers for the non-typedef,
571 // non-template type argument case within the parser. Here, we just
572 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000573 Quals.removeConst();
574 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000575
576 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000577 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000578 return Context.getQualifiedType(
579 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000580 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581}
582
583/// \brief Build an array type.
584///
585/// \param T The type of each element in the array.
586///
587/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000588///
589/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000590///
591/// \param Quals The cvr-qualifiers to be applied to the array's
592/// element type.
593///
594/// \param Loc The location of the entity whose type involves this
595/// array type or, if there is no such entity, the location of the
596/// type that will have array type.
597///
598/// \param Entity The name of the entity that involves the array
599/// type, if known.
600///
601/// \returns A suitable array type, if there are no errors. Otherwise,
602/// returns a NULL type.
603QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
604 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000605 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000606
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000607 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000609 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000610 // Not in C++, though. There we only dislike void.
611 if (getLangOptions().CPlusPlus) {
612 if (T->isVoidType()) {
613 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
614 return QualType();
615 }
616 } else {
617 if (RequireCompleteType(Loc, T,
618 diag::err_illegal_decl_array_incomplete_type))
619 return QualType();
620 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000621
622 if (T->isFunctionType()) {
623 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000624 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000625 return QualType();
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregorcd281c32009-02-28 00:25:32 +0000628 // C++ 8.3.2p4: There shall be no ... arrays of references ...
629 if (T->isReferenceType()) {
630 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000631 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000632 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000633 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000634
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000635 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000636 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000637 << getPrintableNameForEntity(Entity);
638 return QualType();
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek6217b802009-07-29 21:53:49 +0000641 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000642 // If the element type is a struct or union that contains a variadic
643 // array, accept it as a GNU extension: C99 6.7.2.1p2.
644 if (EltTy->getDecl()->hasFlexibleArrayMember())
645 Diag(Loc, diag::ext_flexible_array_in_array) << T;
646 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000647 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
648 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Douglas Gregorcd281c32009-02-28 00:25:32 +0000651 // C99 6.7.5.2p1: The size expression shall have integer type.
652 if (ArraySize && !ArraySize->isTypeDependent() &&
653 !ArraySize->getType()->isIntegerType()) {
654 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
655 << ArraySize->getType() << ArraySize->getSourceRange();
656 ArraySize->Destroy(Context);
657 return QualType();
658 }
659 llvm::APSInt ConstVal(32);
660 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000661 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000662 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000663 else
664 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000665 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000666 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000667 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000668 (!T->isDependentType() && !T->isIncompleteType() &&
669 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000670 // Per C99, a variable array is an array with either a non-constant
671 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000672 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000673 } else {
674 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
675 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000676 if (ConstVal.isSigned() && ConstVal.isNegative()) {
677 Diag(ArraySize->getLocStart(),
678 diag::err_typecheck_negative_array_size)
679 << ArraySize->getSourceRange();
680 return QualType();
681 }
682 if (ConstVal == 0) {
683 // GCC accepts zero sized static arrays.
684 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
685 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000686 }
John McCall46a617a2009-10-16 00:14:28 +0000687 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000688 }
David Chisnallaf407762010-01-11 23:08:08 +0000689 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
690 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000691 if (ArraySize && !ArraySize->isTypeDependent() &&
692 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000693 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000694 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000695 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000696 Diag(Loc,
697 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
698 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000699 }
700
701 return T;
702}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000703
704/// \brief Build an ext-vector type.
705///
706/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000707QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000708 SourceLocation AttrLoc) {
709
710 Expr *Arg = (Expr *)ArraySize.get();
711
712 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
713 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000714 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000715 !T->isIntegerType() && !T->isRealFloatingType()) {
716 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
717 return QualType();
718 }
719
720 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
721 llvm::APSInt vecSize(32);
722 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
723 Diag(AttrLoc, diag::err_attribute_argument_not_int)
724 << "ext_vector_type" << Arg->getSourceRange();
725 return QualType();
726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
728 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000729 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000730 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
731
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000732 if (vectorSize == 0) {
733 Diag(AttrLoc, diag::err_attribute_zero_size)
734 << Arg->getSourceRange();
735 return QualType();
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000738 if (!T->isDependentType())
739 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000740 }
741
742 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000743 AttrLoc);
744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregor724651c2009-02-28 01:04:19 +0000746/// \brief Build a function type.
747///
748/// This routine checks the function type according to C++ rules and
749/// under the assumption that the result type and parameter types have
750/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000751/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000752/// simpler form that is only suitable for this narrow use case.
753///
754/// \param T The return type of the function.
755///
756/// \param ParamTypes The parameter types of the function. This array
757/// will be modified to account for adjustments to the types of the
758/// function parameters.
759///
760/// \param NumParamTypes The number of parameter types in ParamTypes.
761///
762/// \param Variadic Whether this is a variadic function type.
763///
764/// \param Quals The cvr-qualifiers to be applied to the function type.
765///
766/// \param Loc The location of the entity whose type involves this
767/// function type or, if there is no such entity, the location of the
768/// type that will have function type.
769///
770/// \param Entity The name of the entity that involves the function
771/// type, if known.
772///
773/// \returns A suitable function type, if there are no
774/// errors. Otherwise, returns a NULL type.
775QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000776 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000777 unsigned NumParamTypes,
778 bool Variadic, unsigned Quals,
779 SourceLocation Loc, DeclarationName Entity) {
780 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000781 Diag(Loc, diag::err_func_returning_array_function)
782 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000783 return QualType();
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregor724651c2009-02-28 01:04:19 +0000786 bool Invalid = false;
787 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000788 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
789 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000790 Diag(Loc, diag::err_param_with_void_type);
791 Invalid = true;
792 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000793
John McCall54e14c42009-10-22 22:37:11 +0000794 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000795 }
796
797 if (Invalid)
798 return QualType();
799
Mike Stump1eb44332009-09-09 15:08:12 +0000800 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregorce056bc2010-02-21 22:15:06 +0000801 Quals, false, false, 0, 0, false, CC_Default);
Douglas Gregor724651c2009-02-28 01:04:19 +0000802}
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Douglas Gregor949bf692009-06-09 22:17:39 +0000804/// \brief Build a member pointer type \c T Class::*.
805///
806/// \param T the type to which the member pointer refers.
807/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000808/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000809/// \param Loc the location where this type begins
810/// \param Entity the name of the entity that will have this member pointer type
811///
812/// \returns a member pointer type, if successful, or a NULL type if there was
813/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000814QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000815 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000816 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000817 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
818
Douglas Gregor949bf692009-06-09 22:17:39 +0000819 // Verify that we're not building a pointer to pointer to function with
820 // exception specification.
821 if (CheckDistantExceptionSpec(T)) {
822 Diag(Loc, diag::err_distant_exception_spec);
823
824 // FIXME: If we're doing this as part of template instantiation,
825 // we should return immediately.
826
827 // Build the type anyway, but use the canonical type so that the
828 // exception specifiers are stripped off.
829 T = Context.getCanonicalType(T);
830 }
831
832 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
833 // with reference type, or "cv void."
834 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000835 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000836 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000837 return QualType();
838 }
839
840 if (T->isVoidType()) {
841 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
842 << (Entity? Entity.getAsString() : "type name");
843 return QualType();
844 }
845
846 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
847 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000848 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000849 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
850 << T;
851
852 // FIXME: If we're doing this as part of template instantiation,
853 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000854 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000855 }
856
857 if (!Class->isDependentType() && !Class->isRecordType()) {
858 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
859 return QualType();
860 }
861
John McCall0953e762009-09-24 19:53:00 +0000862 return Context.getQualifiedType(
863 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000864}
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Anders Carlsson9a917e42009-06-12 22:56:54 +0000866/// \brief Build a block pointer type.
867///
868/// \param T The type to which we'll be building a block pointer.
869///
John McCall0953e762009-09-24 19:53:00 +0000870/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000871///
872/// \param Loc The location of the entity whose type involves this
873/// block pointer type or, if there is no such entity, the location of the
874/// type that will have block pointer type.
875///
876/// \param Entity The name of the entity that involves the block pointer
877/// type, if known.
878///
879/// \returns A suitable block pointer type, if there are no
880/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000881QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000882 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000883 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000884 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000885 Diag(Loc, diag::err_nonfunction_block_type);
886 return QualType();
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
John McCall0953e762009-09-24 19:53:00 +0000889 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
890 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000891}
892
John McCalla93c9342009-12-07 02:54:59 +0000893QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000894 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000895 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000896 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000897 return QualType();
898 }
899
John McCalla93c9342009-12-07 02:54:59 +0000900 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000901 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
902 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000903 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
John McCalla93c9342009-12-07 02:54:59 +0000906 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000907 return QT;
908}
909
Mike Stump98eb8a72009-02-04 22:31:32 +0000910/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000911/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000912///
913/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
914/// owns the declaration of a type (e.g., the definition of a struct
915/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000916QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
John McCalla93c9342009-12-07 02:54:59 +0000917 TypeSourceInfo **TInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000918 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000919 // Determine the type of the declarator. Not all forms of declarator
920 // have a type.
921 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000922
John McCall04a67a62010-02-05 21:31:56 +0000923 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
924
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000925 switch (D.getName().getKind()) {
926 case UnqualifiedId::IK_Identifier:
927 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000928 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000929 case UnqualifiedId::IK_TemplateId:
John McCall04a67a62010-02-05 21:31:56 +0000930 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000931
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000932 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
933 TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000934 // Owned is embedded if it was defined here, or if it is the
935 // very first (i.e., canonical) declaration of this tag type.
936 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
937 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000938 if (OwnedDecl) *OwnedDecl = Owned;
939 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000940 break;
941
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000942 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000943 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000944 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000945 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000946 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000947 T = Context.VoidTy;
948 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000949
950 case UnqualifiedId::IK_ConversionFunctionId:
951 // The result type of a conversion function is the type that it
952 // converts to.
953 T = GetTypeFromParser(D.getName().ConversionFunctionId);
954 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000955 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000956
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000957 if (T.isNull())
958 return T;
959
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000960 if (T == Context.UndeducedAutoTy) {
961 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000963 switch (D.getContext()) {
964 case Declarator::KNRTypeListContext:
965 assert(0 && "K&R type lists aren't allowed in C++");
966 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000967 case Declarator::PrototypeContext:
968 Error = 0; // Function prototype
969 break;
970 case Declarator::MemberContext:
971 switch (cast<TagDecl>(CurContext)->getTagKind()) {
972 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
973 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
974 case TagDecl::TK_union: Error = 2; /* Union member */ break;
975 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000976 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000977 break;
978 case Declarator::CXXCatchContext:
979 Error = 4; // Exception declaration
980 break;
981 case Declarator::TemplateParamContext:
982 Error = 5; // Template parameter
983 break;
984 case Declarator::BlockLiteralContext:
985 Error = 6; // Block literal
986 break;
987 case Declarator::FileContext:
988 case Declarator::BlockContext:
989 case Declarator::ForContext:
990 case Declarator::ConditionContext:
991 case Declarator::TypeNameContext:
992 break;
993 }
994
995 if (Error != -1) {
996 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
997 << Error;
998 T = Context.IntTy;
999 D.setInvalidType(true);
1000 }
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregorcd281c32009-02-28 00:25:32 +00001003 // The name we're declaring, if any.
1004 DeclarationName Name;
1005 if (D.getIdentifier())
1006 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001007
John McCall04a67a62010-02-05 21:31:56 +00001008 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
1009
Mike Stump98eb8a72009-02-04 22:31:32 +00001010 // Walk the DeclTypeInfo, building the recursive type as we go.
1011 // DeclTypeInfos are ordered from the identifier out, which is
1012 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001013 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1014 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 switch (DeclType.Kind) {
1016 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +00001017 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001018 // If blocks are disabled, emit an error.
1019 if (!LangOpts.Blocks)
1020 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
1022 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +00001023 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +00001024 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001026 // Verify that we're not building a pointer to pointer to function with
1027 // exception specification.
1028 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1029 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1030 D.setInvalidType(true);
1031 // Build the type anyway.
1032 }
Steve Naroff14108da2009-07-10 23:34:53 +00001033 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +00001034 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +00001035 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +00001036 (ObjCProtocolDecl **)OIT->qual_begin(),
1037 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +00001038 break;
1039 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001040 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 break;
John McCall0953e762009-09-24 19:53:00 +00001042 case DeclaratorChunk::Reference: {
1043 Qualifiers Quals;
1044 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
1045
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001046 // Verify that we're not building a reference to pointer to function with
1047 // exception specification.
1048 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1049 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1050 D.setInvalidType(true);
1051 // Build the type anyway.
1052 }
John McCall0953e762009-09-24 19:53:00 +00001053 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001054 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 break;
John McCall0953e762009-09-24 19:53:00 +00001056 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001058 // Verify that we're not building an array of pointers to function with
1059 // exception specification.
1060 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1061 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1062 D.setInvalidType(true);
1063 // Build the type anyway.
1064 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001065 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001066 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 ArrayType::ArraySizeModifier ASM;
1068 if (ATI.isStar)
1069 ASM = ArrayType::Star;
1070 else if (ATI.hasStatic)
1071 ASM = ArrayType::Static;
1072 else
1073 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001074 if (ASM == ArrayType::Star &&
1075 D.getContext() != Declarator::PrototypeContext) {
1076 // FIXME: This check isn't quite right: it allows star in prototypes
1077 // for function definitions, and disallows some edge cases detailed
1078 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1079 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1080 ASM = ArrayType::Normal;
1081 D.setInvalidType(true);
1082 }
John McCall0953e762009-09-24 19:53:00 +00001083 T = BuildArrayType(T, ASM, ArraySize,
1084 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001085 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 break;
1087 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001088 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 // If the function declarator has a prototype (i.e. it is not () and
1090 // does not have a K&R-style identifier list), then the arguments are part
1091 // of the type, otherwise the argument list is ().
1092 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001093
Chris Lattnercd881292007-12-19 05:31:29 +00001094 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001095 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001096 if ((T->isArrayType() || T->isFunctionType()) &&
1097 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001098 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1099 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001100 T = Context.IntTy;
1101 D.setInvalidType(true);
1102 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001103
Douglas Gregor402abb52009-05-28 23:31:59 +00001104 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1105 // C++ [dcl.fct]p6:
1106 // Types shall not be defined in return or parameter types.
1107 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1108 if (Tag->isDefinition())
1109 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1110 << Context.getTypeDeclType(Tag);
1111 }
1112
Sebastian Redl3cc97262009-05-31 11:47:27 +00001113 // Exception specs are not allowed in typedefs. Complain, but add it
1114 // anyway.
1115 if (FTI.hasExceptionSpec &&
1116 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1117 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1118
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001119 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001120 if (getLangOptions().CPlusPlus) {
1121 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1122 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001123 llvm::SmallVector<QualType, 4> Exceptions;
1124 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001125 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001126 // FIXME: Preserve type source info.
1127 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001128 // Check that the type is valid for an exception spec, and drop it
1129 // if not.
1130 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1131 Exceptions.push_back(ET);
1132 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001133 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1134 FTI.hasExceptionSpec,
1135 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001136 Exceptions.size(), Exceptions.data(),
1137 false, CC_Default);
Douglas Gregor965acbb2009-02-18 07:07:28 +00001138 } else if (FTI.isVariadic) {
1139 // We allow a zero-parameter variadic function in C if the
1140 // function is marked with the "overloadable"
1141 // attribute. Scan for this attribute now.
1142 bool Overloadable = false;
1143 for (const AttributeList *Attrs = D.getAttributes();
1144 Attrs; Attrs = Attrs->getNext()) {
1145 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1146 Overloadable = true;
1147 break;
1148 }
1149 }
1150
1151 if (!Overloadable)
1152 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Douglas Gregorce056bc2010-02-21 22:15:06 +00001153 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0,
1154 false, false, 0, 0, false, CC_Default);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001155 } else {
1156 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001157 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001158 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001159 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001161 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001162 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 } else {
1164 // Otherwise, we have a function with an argument list that is
1165 // potentially variadic.
1166 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001169 ParmVarDecl *Param =
1170 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001171 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001172 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001173
1174 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001175 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // Look for 'void'. void is allowed only as a single argument to a
1178 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001179 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001180 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // If this is something like 'float(int, void)', reject it. 'void'
1182 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1183 // have arguments of incomplete type.
1184 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1185 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001186 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001187 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001188 } else if (FTI.ArgInfo[i].Ident) {
1189 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001191 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001192 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001193 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001194 } else {
1195 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001196 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001197 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattner2ff54262007-07-21 05:18:12 +00001199 // Do not add 'void' to the ArgTys list.
1200 break;
1201 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001202 } else if (!FTI.hasPrototype) {
1203 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001204 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001205 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001206 if (BTy->getKind() == BuiltinType::Float)
1207 ArgTy = Context.DoubleTy;
1208 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
John McCall54e14c42009-10-22 22:37:11 +00001211 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001213
1214 llvm::SmallVector<QualType, 4> Exceptions;
1215 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001216 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001217 // FIXME: Preserve type source info.
1218 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001219 // Check that the type is valid for an exception spec, and drop it if
1220 // not.
1221 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1222 Exceptions.push_back(ET);
1223 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001224
Jay Foadbeaaccd2009-05-21 09:52:38 +00001225 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001226 FTI.isVariadic, FTI.TypeQuals,
1227 FTI.hasExceptionSpec,
1228 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001229 Exceptions.size(), Exceptions.data(),
1230 false, CC_Default);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 }
John McCall04a67a62010-02-05 21:31:56 +00001232
1233 // For GCC compatibility, we allow attributes that apply only to
1234 // function types to be placed on a function's return type
1235 // instead (as long as that type doesn't happen to be function
1236 // or function-pointer itself).
1237 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
1238
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 break;
1240 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001241 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001242 // Verify that we're not building a pointer to pointer to function with
1243 // exception specification.
1244 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1245 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1246 D.setInvalidType(true);
1247 // Build the type anyway.
1248 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001249 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001250 QualType ClsType;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001251 if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1252 || dyn_cast_or_null<CXXRecordDecl>(
1253 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001254 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001255 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001256 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1257 switch (NNS->getKind()) {
1258 case NestedNameSpecifier::Identifier:
1259 ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
1260 break;
1261
1262 case NestedNameSpecifier::Namespace:
1263 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001264 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001265 break;
1266
1267 case NestedNameSpecifier::TypeSpec:
1268 case NestedNameSpecifier::TypeSpecWithTemplate:
1269 ClsType = QualType(NNS->getAsType(), 0);
1270 if (NNSPrefix)
1271 ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
1272 break;
1273 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001274 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001275 Diag(DeclType.Mem.Scope().getBeginLoc(),
1276 diag::err_illegal_decl_mempointer_in_nonclass)
1277 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1278 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001279 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001280 }
1281
Douglas Gregor949bf692009-06-09 22:17:39 +00001282 if (!ClsType.isNull())
1283 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1284 DeclType.Loc, D.getIdentifier());
1285 if (T.isNull()) {
1286 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001287 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001288 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001289 break;
1290 }
1291
Douglas Gregorcd281c32009-02-28 00:25:32 +00001292 if (T.isNull()) {
1293 D.setInvalidType(true);
1294 T = Context.IntTy;
1295 }
1296
John McCall04a67a62010-02-05 21:31:56 +00001297 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
1298
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001299 // See if there are any attributes on this declarator chunk.
1300 if (const AttributeList *AL = DeclType.getAttrs())
Charles Davis328ce342010-02-24 02:27:18 +00001301 ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001303
1304 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001305 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001306 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001307
1308 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1309 // for a nonstatic member function, the function type to which a pointer
1310 // to member refers, or the top-level function type of a function typedef
1311 // declaration.
1312 if (FnTy->getTypeQuals() != 0 &&
1313 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001314 ((D.getContext() != Declarator::MemberContext &&
1315 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001316 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1317 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001318 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001319 if (D.isFunctionDeclarator())
1320 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1321 else
1322 Diag(D.getIdentifierLoc(),
1323 diag::err_invalid_qualified_typedef_function_type_use);
1324
1325 // Strip the cv-quals from the type.
1326 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00001327 FnTy->getNumArgs(), FnTy->isVariadic(), 0,
1328 false, false, 0, 0, false, CC_Default);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001329 }
1330 }
Mike Stump1eb44332009-09-09 15:08:12 +00001331
John McCall04a67a62010-02-05 21:31:56 +00001332 // Process any function attributes we might have delayed from the
1333 // declaration-specifiers.
1334 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
1335
1336 // If there were any type attributes applied to the decl itself, not
1337 // the type, apply them to the result type. But don't do this for
1338 // block-literal expressions, which are parsed wierdly.
1339 if (D.getContext() != Declarator::BlockLiteralContext)
1340 if (const AttributeList *Attrs = D.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +00001341 ProcessTypeAttributeList(*this, T, false, Attrs,
1342 FnAttrsFromPreviousChunk);
John McCall04a67a62010-02-05 21:31:56 +00001343
1344 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001345
John McCalla93c9342009-12-07 02:54:59 +00001346 if (TInfo) {
John McCall54e14c42009-10-22 22:37:11 +00001347 if (D.isInvalidType())
John McCalla93c9342009-12-07 02:54:59 +00001348 *TInfo = 0;
John McCall54e14c42009-10-22 22:37:11 +00001349 else
John McCalla93c9342009-12-07 02:54:59 +00001350 *TInfo = GetTypeSourceInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001351 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001352
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 return T;
1354}
1355
John McCall51bd8032009-10-18 01:05:36 +00001356namespace {
1357 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1358 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001359
John McCall51bd8032009-10-18 01:05:36 +00001360 public:
1361 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001362
John McCall51bd8032009-10-18 01:05:36 +00001363 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1364 Visit(TL.getUnqualifiedLoc());
1365 }
1366 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1367 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1368 }
1369 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1370 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001371
John McCall54e14c42009-10-22 22:37:11 +00001372 if (DS.getProtocolQualifiers()) {
1373 assert(TL.getNumProtocols() > 0);
1374 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1375 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1376 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1377 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1378 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1379 } else {
1380 assert(TL.getNumProtocols() == 0);
1381 TL.setLAngleLoc(SourceLocation());
1382 TL.setRAngleLoc(SourceLocation());
1383 }
1384 }
1385 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1386 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1387
1388 TL.setStarLoc(SourceLocation());
1389
1390 if (DS.getProtocolQualifiers()) {
1391 assert(TL.getNumProtocols() > 0);
1392 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1393 TL.setHasProtocolsAsWritten(true);
1394 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1395 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1396 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1397 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1398
1399 } else {
1400 assert(TL.getNumProtocols() == 0);
1401 TL.setHasProtocolsAsWritten(false);
1402 TL.setLAngleLoc(SourceLocation());
1403 TL.setRAngleLoc(SourceLocation());
1404 }
1405
1406 // This might not have been written with an inner type.
1407 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1408 TL.setHasBaseTypeAsWritten(false);
1409 TL.getBaseTypeLoc().initialize(SourceLocation());
1410 } else {
1411 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001412 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001413 }
John McCall51bd8032009-10-18 01:05:36 +00001414 }
John McCall833ca992009-10-29 08:12:44 +00001415 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001416 TypeSourceInfo *TInfo = 0;
1417 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001418
1419 // If we got no declarator info from previous Sema routines,
1420 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001421 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001422 TL.initialize(DS.getTypeSpecTypeLoc());
1423 return;
1424 }
1425
1426 TemplateSpecializationTypeLoc OldTL =
John McCalla93c9342009-12-07 02:54:59 +00001427 cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc());
John McCall833ca992009-10-29 08:12:44 +00001428 TL.copy(OldTL);
1429 }
John McCallcfb708c2010-01-13 20:03:27 +00001430 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1431 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1432 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1433 TL.setParensRange(DS.getTypeofParensRange());
1434 }
1435 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1436 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1437 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1438 TL.setParensRange(DS.getTypeofParensRange());
1439 assert(DS.getTypeRep());
1440 TypeSourceInfo *TInfo = 0;
1441 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1442 TL.setUnderlyingTInfo(TInfo);
1443 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001444 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1445 // By default, use the source location of the type specifier.
1446 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1447 if (TL.needsExtraLocalData()) {
1448 // Set info for the written builtin specifiers.
1449 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1450 // Try to have a meaningful source location.
1451 if (TL.getWrittenSignSpec() != TSS_unspecified)
1452 // Sign spec loc overrides the others (e.g., 'unsigned long').
1453 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1454 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1455 // Width spec loc overrides type spec loc (e.g., 'short int').
1456 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1457 }
1458 }
John McCall51bd8032009-10-18 01:05:36 +00001459 void VisitTypeLoc(TypeLoc TL) {
1460 // FIXME: add other typespec types and change this to an assert.
1461 TL.initialize(DS.getTypeSpecTypeLoc());
1462 }
1463 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001464
John McCall51bd8032009-10-18 01:05:36 +00001465 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1466 const DeclaratorChunk &Chunk;
1467
1468 public:
1469 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1470
1471 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001472 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001473 }
1474
1475 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1476 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1477 TL.setCaretLoc(Chunk.Loc);
1478 }
1479 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1480 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1481 TL.setStarLoc(Chunk.Loc);
1482 }
1483 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1484 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1485 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001486 TL.setHasBaseTypeAsWritten(true);
1487 TL.setHasProtocolsAsWritten(false);
1488 TL.setLAngleLoc(SourceLocation());
1489 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001490 }
1491 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1492 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1493 TL.setStarLoc(Chunk.Loc);
1494 // FIXME: nested name specifier
1495 }
1496 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1497 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001498 // 'Amp' is misleading: this might have been originally
1499 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001500 TL.setAmpLoc(Chunk.Loc);
1501 }
1502 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1503 assert(Chunk.Kind == DeclaratorChunk::Reference);
1504 assert(!Chunk.Ref.LValueRef);
1505 TL.setAmpAmpLoc(Chunk.Loc);
1506 }
1507 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1508 assert(Chunk.Kind == DeclaratorChunk::Array);
1509 TL.setLBracketLoc(Chunk.Loc);
1510 TL.setRBracketLoc(Chunk.EndLoc);
1511 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1512 }
1513 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1514 assert(Chunk.Kind == DeclaratorChunk::Function);
1515 TL.setLParenLoc(Chunk.Loc);
1516 TL.setRParenLoc(Chunk.EndLoc);
1517
1518 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001519 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001520 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001521 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001522 }
1523 // FIXME: exception specs
1524 }
1525
1526 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001527 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001528 }
1529 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001530}
1531
John McCalla93c9342009-12-07 02:54:59 +00001532/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001533///
1534/// \param T QualType referring to the type as written in source code.
John McCalla93c9342009-12-07 02:54:59 +00001535TypeSourceInfo *
1536Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T) {
1537 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1538 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001539
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001540 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001541 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1542 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001543 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001544
John McCall51bd8032009-10-18 01:05:36 +00001545 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001546
John McCalla93c9342009-12-07 02:54:59 +00001547 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001548}
1549
John McCalla93c9342009-12-07 02:54:59 +00001550/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1551QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001552 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1553 // and Sema during declaration parsing. Try deallocating/caching them when
1554 // it's appropriate, instead of allocating them and keeping them around.
1555 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
John McCalla93c9342009-12-07 02:54:59 +00001556 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001557 assert(LocT->getTypeClass() != T->getTypeClass() &&
1558 "LocInfoType's TypeClass conflicts with an existing Type class");
1559 return QualType(LocT, 0);
1560}
1561
1562void LocInfoType::getAsStringInternal(std::string &Str,
1563 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001564 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1565 " was used directly instead of getting the QualType through"
1566 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001567}
1568
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001569/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1570/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1571/// they point to and return true. If T1 and T2 aren't pointer types
1572/// or pointer-to-member types, or if they are not similar at this
1573/// level, returns false and leaves T1 and T2 unchanged. Top-level
1574/// qualifiers on T1 and T2 are ignored. This function will typically
1575/// be called in a loop that successively "unwraps" pointer and
1576/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001577bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001578 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1579 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001580 if (T1PtrType && T2PtrType) {
1581 T1 = T1PtrType->getPointeeType();
1582 T2 = T2PtrType->getPointeeType();
1583 return true;
1584 }
1585
Ted Kremenek6217b802009-07-29 21:53:49 +00001586 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1587 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001588 if (T1MPType && T2MPType &&
1589 Context.getCanonicalType(T1MPType->getClass()) ==
1590 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001591 T1 = T1MPType->getPointeeType();
1592 T2 = T2MPType->getPointeeType();
1593 return true;
1594 }
Douglas Gregor57373262008-10-22 14:17:15 +00001595 return false;
1596}
1597
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001598Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 // C99 6.7.6: Type names have no identifier. This is already validated by
1600 // the parser.
1601 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001602
John McCalla93c9342009-12-07 02:54:59 +00001603 TypeSourceInfo *TInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001604 TagDecl *OwnedTag = 0;
John McCalla93c9342009-12-07 02:54:59 +00001605 QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001606 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001607 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001608
Douglas Gregor402abb52009-05-28 23:31:59 +00001609 if (getLangOptions().CPlusPlus) {
1610 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001611 CheckExtraCXXDefaultArguments(D);
1612
Douglas Gregor402abb52009-05-28 23:31:59 +00001613 // C++0x [dcl.type]p3:
1614 // A type-specifier-seq shall not define a class or enumeration
1615 // unless it appears in the type-id of an alias-declaration
1616 // (7.1.3).
1617 if (OwnedTag && OwnedTag->isDefinition())
1618 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1619 << Context.getTypeDeclType(OwnedTag);
1620 }
1621
John McCalla93c9342009-12-07 02:54:59 +00001622 if (TInfo)
1623 T = CreateLocInfoType(T, TInfo);
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001624
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 return T.getAsOpaquePtr();
1626}
1627
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001628
1629
1630//===----------------------------------------------------------------------===//
1631// Type Attribute Processing
1632//===----------------------------------------------------------------------===//
1633
1634/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1635/// specified type. The attribute contains 1 argument, the id of the address
1636/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001637static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001638 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001639
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001640 // If this type is already address space qualified, reject it.
1641 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1642 // for two or more different address spaces."
1643 if (Type.getAddressSpace()) {
1644 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1645 return;
1646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001648 // Check the attribute arguments.
1649 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001651 return;
1652 }
1653 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1654 llvm::APSInt addrSpace(32);
1655 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001656 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1657 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001658 return;
1659 }
1660
John McCallefadb772009-07-28 06:52:18 +00001661 // Bounds checking.
1662 if (addrSpace.isSigned()) {
1663 if (addrSpace.isNegative()) {
1664 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1665 << ASArgExpr->getSourceRange();
1666 return;
1667 }
1668 addrSpace.setIsSigned(false);
1669 }
1670 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001671 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001672 if (addrSpace > max) {
1673 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001674 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001675 return;
1676 }
1677
Mike Stump1eb44332009-09-09 15:08:12 +00001678 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001679 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001680}
1681
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001682/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1683/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001684static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001685 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001686 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001687 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001688 return;
1689 }
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001691 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001692 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001693 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1694 << "objc_gc" << 1;
1695 return;
1696 }
John McCall0953e762009-09-24 19:53:00 +00001697 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001698 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001699 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1700 return;
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001703 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001704 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001705 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001706 else {
1707 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1708 << "objc_gc" << Attr.getParameterName();
1709 return;
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001712 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001713}
1714
John McCall04a67a62010-02-05 21:31:56 +00001715/// Process an individual function attribute. Returns true if the
1716/// attribute does not make sense to apply to this type.
1717bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
1718 if (Attr.getKind() == AttributeList::AT_noreturn) {
1719 // Complain immediately if the arg count is wrong.
1720 if (Attr.getNumArgs() != 0) {
1721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1722 return false;
1723 }
Mike Stump24556362009-07-25 21:26:53 +00001724
John McCall04a67a62010-02-05 21:31:56 +00001725 // Delay if this is not a function or pointer to block.
1726 if (!Type->isFunctionPointerType()
1727 && !Type->isBlockPointerType()
1728 && !Type->isFunctionType())
1729 return true;
Mike Stump24556362009-07-25 21:26:53 +00001730
John McCall04a67a62010-02-05 21:31:56 +00001731 // Otherwise we can process right away.
1732 Type = S.Context.getNoReturnType(Type);
1733 return false;
1734 }
Mike Stump24556362009-07-25 21:26:53 +00001735
John McCall04a67a62010-02-05 21:31:56 +00001736 // Otherwise, a calling convention.
1737 if (Attr.getNumArgs() != 0) {
1738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1739 return false;
1740 }
John McCallf82b4e82010-02-04 05:44:44 +00001741
John McCall04a67a62010-02-05 21:31:56 +00001742 QualType T = Type;
1743 if (const PointerType *PT = Type->getAs<PointerType>())
1744 T = PT->getPointeeType();
1745 const FunctionType *Fn = T->getAs<FunctionType>();
John McCallf82b4e82010-02-04 05:44:44 +00001746
John McCall04a67a62010-02-05 21:31:56 +00001747 // Delay if the type didn't work out to a function.
1748 if (!Fn) return true;
1749
1750 // TODO: diagnose uses of these conventions on the wrong target.
1751 CallingConv CC;
1752 switch (Attr.getKind()) {
1753 case AttributeList::AT_cdecl: CC = CC_C; break;
1754 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
1755 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
1756 default: llvm_unreachable("unexpected attribute kind"); return false;
1757 }
1758
1759 CallingConv CCOld = Fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00001760 if (S.Context.getCanonicalCallConv(CC) ==
1761 S.Context.getCanonicalCallConv(CCOld)) return false;
John McCall04a67a62010-02-05 21:31:56 +00001762
1763 if (CCOld != CC_Default) {
1764 // Should we diagnose reapplications of the same convention?
1765 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1766 << FunctionType::getNameForCallConv(CC)
1767 << FunctionType::getNameForCallConv(CCOld);
1768 return false;
1769 }
1770
1771 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
1772 if (CC == CC_X86FastCall) {
1773 if (isa<FunctionNoProtoType>(Fn)) {
1774 S.Diag(Attr.getLoc(), diag::err_cconv_knr)
1775 << FunctionType::getNameForCallConv(CC);
1776 return false;
1777 }
1778
1779 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
1780 if (FnP->isVariadic()) {
1781 S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
1782 << FunctionType::getNameForCallConv(CC);
1783 return false;
1784 }
1785 }
1786
1787 Type = S.Context.getCallConvType(Type, CC);
1788 return false;
John McCallf82b4e82010-02-04 05:44:44 +00001789}
1790
John Thompson6e132aa2009-12-04 21:51:28 +00001791/// HandleVectorSizeAttribute - this attribute is only applicable to integral
1792/// and float scalars, although arrays, pointers, and function return values are
1793/// allowed in conjunction with this construct. Aggregates with this attribute
1794/// are invalid, even if they are of the same size as a corresponding scalar.
1795/// The raw attribute should contain precisely 1 argument, the vector size for
1796/// the variable, measured in bytes. If curType and rawAttr are well formed,
1797/// this routine will return a new vector type.
1798static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
1799 // Check the attribute arugments.
1800 if (Attr.getNumArgs() != 1) {
1801 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1802 return;
1803 }
1804 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
1805 llvm::APSInt vecSize(32);
1806 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
1807 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1808 << "vector_size" << sizeExpr->getSourceRange();
1809 return;
1810 }
1811 // the base type must be integer or float, and can't already be a vector.
1812 if (CurType->isVectorType() ||
1813 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
1814 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
1815 return;
1816 }
1817 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
1818 // vecSize is specified in bytes - convert to bits.
1819 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
1820
1821 // the vector size needs to be an integral multiple of the type size.
1822 if (vectorSize % typeSize) {
1823 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
1824 << sizeExpr->getSourceRange();
1825 return;
1826 }
1827 if (vectorSize == 0) {
1828 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
1829 << sizeExpr->getSourceRange();
1830 return;
1831 }
1832
1833 // Success! Instantiate the vector type, the number of elements is > 0, and
1834 // not required to be a power of 2, unlike GCC.
John Thompson82287d12010-02-05 00:12:22 +00001835 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, false, false);
John Thompson6e132aa2009-12-04 21:51:28 +00001836}
1837
John McCall04a67a62010-02-05 21:31:56 +00001838void ProcessTypeAttributeList(Sema &S, QualType &Result,
Charles Davis328ce342010-02-24 02:27:18 +00001839 bool IsDeclSpec, const AttributeList *AL,
John McCall04a67a62010-02-05 21:31:56 +00001840 DelayedAttributeSet &FnAttrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00001841 // Scan through and apply attributes to this type where it makes sense. Some
1842 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1843 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001844 // apply to the decl. Here we apply type attributes and ignore the rest.
1845 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001846 // If this is an attribute we can handle, do so now, otherwise, add it to
1847 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001848 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001849 default: break;
John McCall04a67a62010-02-05 21:31:56 +00001850
Chris Lattner232e8822008-02-21 01:08:11 +00001851 case AttributeList::AT_address_space:
John McCall04a67a62010-02-05 21:31:56 +00001852 HandleAddressSpaceTypeAttribute(Result, *AL, S);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001853 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001854 case AttributeList::AT_objc_gc:
John McCall04a67a62010-02-05 21:31:56 +00001855 HandleObjCGCTypeAttribute(Result, *AL, S);
Mike Stump24556362009-07-25 21:26:53 +00001856 break;
John Thompson6e132aa2009-12-04 21:51:28 +00001857 case AttributeList::AT_vector_size:
John McCall04a67a62010-02-05 21:31:56 +00001858 HandleVectorSizeAttr(Result, *AL, S);
1859 break;
1860
1861 case AttributeList::AT_noreturn:
1862 case AttributeList::AT_cdecl:
1863 case AttributeList::AT_fastcall:
1864 case AttributeList::AT_stdcall:
Charles Davis328ce342010-02-24 02:27:18 +00001865 // Don't process these on the DeclSpec.
1866 if (IsDeclSpec ||
1867 ProcessFnAttr(S, Result, *AL))
John McCall04a67a62010-02-05 21:31:56 +00001868 FnAttrs.push_back(DelayedAttribute(AL, Result));
John Thompson6e132aa2009-12-04 21:51:28 +00001869 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001870 }
Chris Lattner232e8822008-02-21 01:08:11 +00001871 }
Chris Lattner232e8822008-02-21 01:08:11 +00001872}
1873
Mike Stump1eb44332009-09-09 15:08:12 +00001874/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001875///
1876/// This routine checks whether the type @p T is complete in any
1877/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001878/// type, returns false. If @p T is a class template specialization,
1879/// this routine then attempts to perform class template
1880/// instantiation. If instantiation fails, or if @p T is incomplete
1881/// and cannot be completed, issues the diagnostic @p diag (giving it
1882/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001883///
1884/// @param Loc The location in the source that the incomplete type
1885/// diagnostic should refer to.
1886///
1887/// @param T The type that this routine is examining for completeness.
1888///
Mike Stump1eb44332009-09-09 15:08:12 +00001889/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001890/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001891///
1892/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1893/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001894bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001895 const PartialDiagnostic &PD,
1896 std::pair<SourceLocation,
1897 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001898 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Douglas Gregor573d9c32009-10-21 23:19:44 +00001900 // FIXME: Add this assertion to make sure we always get instantiation points.
1901 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001902 // FIXME: Add this assertion to help us flush out problems with
1903 // checking for dependent types and type-dependent expressions.
1904 //
Mike Stump1eb44332009-09-09 15:08:12 +00001905 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001906 // "Can't ask whether a dependent type is complete");
1907
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001908 // If we have a complete type, we're done.
1909 if (!T->isIncompleteType())
1910 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001911
Douglas Gregord475b8d2009-03-25 21:17:03 +00001912 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00001913 // class template specialization, or an array with known size of such,
1914 // try to instantiate it.
1915 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00001916 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00001917 MaybeTemplate = Array->getElementType();
1918 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001919 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001920 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001921 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1922 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001923 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001924 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001925 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001926 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1927 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001928 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1929 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001930 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001931 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001932 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001933 return InstantiateClass(Loc, Rec, Pattern,
1934 getTemplateInstantiationArgs(Rec),
1935 TSK_ImplicitInstantiation,
1936 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001937 }
1938 }
1939 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001940
Douglas Gregor5842ba92009-08-24 15:23:48 +00001941 if (diag == 0)
1942 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001944 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001945 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001946
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001947 // If we have a note, produce it.
1948 if (!Note.first.isInvalid())
1949 Diag(Note.first, Note.second);
1950
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001951 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001952 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001953 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001954 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001955 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001956 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001957 Tag = Enum;
1958
1959 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001960 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001961 Tag->isBeingDefined() ? diag::note_type_being_defined
1962 : diag::note_forward_declaration)
1963 << QualType(Tag, 0);
1964
1965 return true;
1966}
Douglas Gregore6258932009-03-19 00:39:20 +00001967
1968/// \brief Retrieve a version of the type 'T' that is qualified by the
1969/// nested-name-specifier contained in SS.
1970QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1971 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1972 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Douglas Gregorab452ba2009-03-26 23:50:42 +00001974 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001975 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001976 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001977}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001978
1979QualType Sema::BuildTypeofExprType(Expr *E) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00001980 if (E->getType() == Context.OverloadTy) {
1981 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
1982 // function template specialization wherever deduction cannot occur.
1983 if (FunctionDecl *Specialization
1984 = ResolveSingleFunctionTemplateSpecialization(E)) {
1985 E = FixOverloadedFunctionReference(E, Specialization);
1986 if (!E)
1987 return QualType();
1988 } else {
1989 Diag(E->getLocStart(),
1990 diag::err_cannot_determine_declared_type_of_overloaded_function)
1991 << false << E->getSourceRange();
1992 return QualType();
1993 }
1994 }
1995
Anders Carlssonaf017e62009-06-29 22:58:55 +00001996 return Context.getTypeOfExprType(E);
1997}
1998
1999QualType Sema::BuildDecltypeType(Expr *E) {
2000 if (E->getType() == Context.OverloadTy) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002001 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
2002 // function template specialization wherever deduction cannot occur.
2003 if (FunctionDecl *Specialization
2004 = ResolveSingleFunctionTemplateSpecialization(E)) {
2005 E = FixOverloadedFunctionReference(E, Specialization);
2006 if (!E)
2007 return QualType();
2008 } else {
2009 Diag(E->getLocStart(),
2010 diag::err_cannot_determine_declared_type_of_overloaded_function)
2011 << true << E->getSourceRange();
2012 return QualType();
2013 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00002014 }
Douglas Gregor4b52e252009-12-21 23:17:24 +00002015
Anders Carlssonaf017e62009-06-29 22:58:55 +00002016 return Context.getDecltypeType(E);
2017}