blob: 3756df870c2c2b00b27e069bf6f895c4ec5104be [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"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000019#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000020#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Douglas Gregor2dc0e642009-03-23 23:06:20 +000023/// \brief Perform adjustment on the parameter type of a function.
24///
25/// This routine adjusts the given parameter type @p T to the actual
26/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
27/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
28QualType Sema::adjustParameterType(QualType T) {
29 // C99 6.7.5.3p7:
30 if (T->isArrayType()) {
31 // C99 6.7.5.3p7:
32 // A declaration of a parameter as "array of type" shall be
33 // adjusted to "qualified pointer to type", where the type
34 // qualifiers (if any) are those specified within the [ and ] of
35 // the array type derivation.
36 return Context.getArrayDecayedType(T);
37 } else if (T->isFunctionType())
38 // C99 6.7.5.3p8:
39 // A declaration of a parameter as "function returning type"
40 // shall be adjusted to "pointer to function returning type", as
41 // in 6.3.2.1.
42 return Context.getPointerType(T);
43
44 return T;
45}
46
Douglas Gregor930d8b52009-01-30 22:09:00 +000047/// \brief Convert the specified declspec to the appropriate type
48/// object.
49/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000050/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000051/// \returns The type described by the declaration specifiers. This function
52/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000053QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000054 SourceLocation DeclLoc,
55 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
57 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000058 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000059
60 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000061 case DeclSpec::TST_void:
62 Result = Context.VoidTy;
63 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 case DeclSpec::TST_char:
65 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000066 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000068 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 else {
70 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
71 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000072 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
Chris Lattner958858e2008-02-20 21:40:32 +000074 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000075 case DeclSpec::TST_wchar:
76 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
77 Result = Context.WCharTy;
78 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000079 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
80 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000081 Result = Context.getSignedWCharType();
82 } else {
83 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
84 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000085 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
86 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000087 Result = Context.getUnsignedWCharType();
88 }
89 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000090 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000091 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000092 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Steve Naroff683087f2009-06-29 16:22:52 +000093 Result = Context.getObjCObjectPointerType(0, (ObjCProtocolDecl**)PQ,
94 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +000095 break;
96 }
97
Chris Lattnerd658b562008-04-05 06:32:51 +000098 // Unspecified typespec defaults to int in C90. However, the C90 grammar
99 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
100 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
101 // Note that the one exception to this is function definitions, which are
102 // allowed to be completely missing a declspec. This is handled in the
103 // parser already though by it pretending to have seen an 'int' in this
104 // case.
105 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000106 // In C89 mode, we only warn if there is a completely missing declspec
107 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000108 if (DS.isEmpty()) {
109 if (DeclLoc.isInvalid())
110 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000111 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000112 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000113 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
114 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000115 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000116 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000117 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
118 // "At least one type specifier shall be given in the declaration
119 // specifiers in each declaration, and in the specifier-qualifier list in
120 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000121 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000122 if (DeclLoc.isInvalid())
123 DeclLoc = DS.getSourceRange().getBegin();
124
Chris Lattnerb78d8332009-06-26 04:45:06 +0000125 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000126 Diag(DeclLoc, diag::err_missing_type_specifier)
127 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000128
129 // When this occurs in C++ code, often something is very broken with the
130 // value being declared, poison it as invalid so we don't get chains of
131 // errors.
132 isInvalid = true;
133 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000134 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000135 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000136 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000137 }
138
139 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000140 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
142 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000143 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
144 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
145 case DeclSpec::TSW_long: Result = Context.LongTy; break;
146 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
148 } else {
149 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000150 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
151 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
152 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
153 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
155 }
Chris Lattner958858e2008-02-20 21:40:32 +0000156 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000157 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000158 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000159 case DeclSpec::TST_double:
160 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000161 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000162 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000163 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000164 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000165 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 case DeclSpec::TST_decimal32: // _Decimal32
167 case DeclSpec::TST_decimal64: // _Decimal64
168 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000169 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
170 Result = Context.IntTy;
171 isInvalid = true;
172 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000173 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 case DeclSpec::TST_enum:
175 case DeclSpec::TST_union:
176 case DeclSpec::TST_struct: {
177 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000178 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
180 DS.getTypeSpecSign() == 0 &&
181 "Can't handle qualifiers on typedef names yet!");
182 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000183 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000184
185 if (D->isInvalidDecl())
186 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000187 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000189 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
191 DS.getTypeSpecSign() == 0 &&
192 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000193 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000194
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000195 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000196 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
197 // this "hack" for now...
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000198 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
199 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
200 (ObjCProtocolDecl**)PQ,
201 DS.getNumProtocolQualifiers());
202 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000203 // id<protocol-list>
Steve Naroff683087f2009-06-29 16:22:52 +0000204 Result = Context.getObjCObjectPointerType(0, (ObjCProtocolDecl**)PQ,
205 DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000206 else if (Result == Context.getObjCClassType()) {
207 if (DeclLoc.isInvalid())
208 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000209 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000210 Diag(DeclLoc, diag::err_qualified_class_unsupported)
211 << DS.getSourceRange();
212 } else {
213 if (DeclLoc.isInvalid())
214 DeclLoc = DS.getSourceRange().getBegin();
215 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
216 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000217 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000218 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000219 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000220
221 // If this is a reference to an invalid typedef, propagate the invalidity.
222 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
223 if (TDT->getDecl()->isInvalidDecl())
224 isInvalid = true;
225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000227 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
Chris Lattner958858e2008-02-20 21:40:32 +0000229 case DeclSpec::TST_typeofType:
230 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
231 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000232 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000233 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000234 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000235 case DeclSpec::TST_typeofExpr: {
236 Expr *E = static_cast<Expr *>(DS.getTypeRep());
237 assert(E && "Didn't get an expression for typeof?");
238 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000239 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000240 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000241 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000242 case DeclSpec::TST_decltype: {
243 Expr *E = static_cast<Expr *>(DS.getTypeRep());
244 assert(E && "Didn't get an expression for decltype?");
245 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000246 Result = BuildDecltypeType(E);
247 if (Result.isNull()) {
248 Result = Context.IntTy;
249 isInvalid = true;
250 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000251 break;
252 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000253 case DeclSpec::TST_auto: {
254 // TypeQuals handled by caller.
255 Result = Context.UndeducedAutoTy;
256 break;
257 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000258
Douglas Gregor809070a2009-02-18 17:45:20 +0000259 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000260 Result = Context.IntTy;
261 isInvalid = true;
262 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 }
Chris Lattner958858e2008-02-20 21:40:32 +0000264
265 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000266 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
267 if (getLangOptions().Freestanding)
268 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000269 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000270 }
Chris Lattner958858e2008-02-20 21:40:32 +0000271
272 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
273 "FIXME: imaginary types not supported yet!");
274
Chris Lattner38d8b982008-02-20 22:04:11 +0000275 // See if there are any attributes on the declspec that apply to the type (as
276 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000277 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000278 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000279
Chris Lattner96b77fc2008-04-02 06:50:17 +0000280 // Apply const/volatile/restrict qualifiers to T.
281 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
282
283 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
284 // or incomplete types shall not be restrict-qualified." C++ also allows
285 // restrict-qualified references.
286 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000287 if (Result->isPointerType() || Result->isReferenceType()) {
288 QualType EltTy = Result->isPointerType() ?
289 Result->getAsPointerType()->getPointeeType() :
290 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000291
Douglas Gregorbad0e652009-03-24 20:32:41 +0000292 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000293 // incomplete type.
294 if (!EltTy->isIncompleteOrObjectType()) {
295 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000296 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000297 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000298 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
299 }
300 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000301 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000302 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000303 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000304 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000305 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000306 }
307
308 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
309 // of a function type includes any type qualifiers, the behavior is
310 // undefined."
311 if (Result->isFunctionType() && TypeQuals) {
312 // Get some location to point at, either the C or V location.
313 SourceLocation Loc;
314 if (TypeQuals & QualType::Const)
315 Loc = DS.getConstSpecLoc();
316 else {
317 assert((TypeQuals & QualType::Volatile) &&
318 "Has CV quals but not C or V?");
319 Loc = DS.getVolatileSpecLoc();
320 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000321 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000322 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000323 }
324
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000325 // C++ [dcl.ref]p1:
326 // Cv-qualified references are ill-formed except when the
327 // cv-qualifiers are introduced through the use of a typedef
328 // (7.1.3) or of a template type argument (14.3), in which
329 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000330 // FIXME: Shouldn't we be checking SCS_typedef here?
331 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000332 TypeQuals && Result->isReferenceType()) {
333 TypeQuals &= ~QualType::Const;
334 TypeQuals &= ~QualType::Volatile;
335 }
336
Chris Lattner96b77fc2008-04-02 06:50:17 +0000337 Result = Result.getQualifiedType(TypeQuals);
338 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000339 return Result;
340}
341
Douglas Gregorcd281c32009-02-28 00:25:32 +0000342static std::string getPrintableNameForEntity(DeclarationName Entity) {
343 if (Entity)
344 return Entity.getAsString();
345
346 return "type name";
347}
348
349/// \brief Build a pointer type.
350///
351/// \param T The type to which we'll be building a pointer.
352///
353/// \param Quals The cvr-qualifiers to be applied to the pointer type.
354///
355/// \param Loc The location of the entity whose type involves this
356/// pointer type or, if there is no such entity, the location of the
357/// type that will have pointer type.
358///
359/// \param Entity The name of the entity that involves the pointer
360/// type, if known.
361///
362/// \returns A suitable pointer type, if there are no
363/// errors. Otherwise, returns a NULL type.
364QualType Sema::BuildPointerType(QualType T, unsigned Quals,
365 SourceLocation Loc, DeclarationName Entity) {
366 if (T->isReferenceType()) {
367 // C++ 8.3.2p4: There shall be no ... pointers to references ...
368 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
369 << getPrintableNameForEntity(Entity);
370 return QualType();
371 }
372
373 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
374 // object or incomplete types shall not be restrict-qualified."
375 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
376 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
377 << T;
378 Quals &= ~QualType::Restrict;
379 }
380
381 // Build the pointer type.
382 return Context.getPointerType(T).getQualifiedType(Quals);
383}
384
385/// \brief Build a reference type.
386///
387/// \param T The type to which we'll be building a reference.
388///
389/// \param Quals The cvr-qualifiers to be applied to the reference type.
390///
391/// \param Loc The location of the entity whose type involves this
392/// reference type or, if there is no such entity, the location of the
393/// type that will have reference type.
394///
395/// \param Entity The name of the entity that involves the reference
396/// type, if known.
397///
398/// \returns A suitable reference type, if there are no
399/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000400QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000401 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000402 if (LValueRef) {
403 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000404 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
405 // reference to a type T, and attempt to create the type "lvalue
406 // reference to cv TD" creates the type "lvalue reference to T".
407 // We use the qualifiers (restrict or none) of the original reference,
408 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000409 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000410 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000411 }
412 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000413 if (T->isReferenceType()) {
414 // C++ [dcl.ref]p4: There shall be no references to references.
415 //
416 // According to C++ DR 106, references to references are only
417 // diagnosed when they are written directly (e.g., "int & &"),
418 // but not when they happen via a typedef:
419 //
420 // typedef int& intref;
421 // typedef intref& intref2;
422 //
423 // Parser::ParserDeclaratorInternal diagnoses the case where
424 // references are written directly; here, we handle the
425 // collapsing of references-to-references as described in C++
426 // DR 106 and amended by C++ DR 540.
427 return T;
428 }
429
430 // C++ [dcl.ref]p1:
431 // A declarator that specifies the type “reference to cv void”
432 // is ill-formed.
433 if (T->isVoidType()) {
434 Diag(Loc, diag::err_reference_to_void);
435 return QualType();
436 }
437
438 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
439 // object or incomplete types shall not be restrict-qualified."
440 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
441 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
442 << T;
443 Quals &= ~QualType::Restrict;
444 }
445
446 // C++ [dcl.ref]p1:
447 // [...] Cv-qualified references are ill-formed except when the
448 // cv-qualifiers are introduced through the use of a typedef
449 // (7.1.3) or of a template type argument (14.3), in which case
450 // the cv-qualifiers are ignored.
451 //
452 // We diagnose extraneous cv-qualifiers for the non-typedef,
453 // non-template type argument case within the parser. Here, we just
454 // ignore any extraneous cv-qualifiers.
455 Quals &= ~QualType::Const;
456 Quals &= ~QualType::Volatile;
457
458 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000459 if (LValueRef)
460 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
461 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000462}
463
464/// \brief Build an array type.
465///
466/// \param T The type of each element in the array.
467///
468/// \param ASM C99 array size modifier (e.g., '*', 'static').
469///
470/// \param ArraySize Expression describing the size of the array.
471///
472/// \param Quals The cvr-qualifiers to be applied to the array's
473/// element type.
474///
475/// \param Loc The location of the entity whose type involves this
476/// array type or, if there is no such entity, the location of the
477/// type that will have array type.
478///
479/// \param Entity The name of the entity that involves the array
480/// type, if known.
481///
482/// \returns A suitable array type, if there are no errors. Otherwise,
483/// returns a NULL type.
484QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
485 Expr *ArraySize, unsigned Quals,
486 SourceLocation Loc, DeclarationName Entity) {
487 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
488 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000489 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000490 diag::err_illegal_decl_array_incomplete_type))
491 return QualType();
492
493 if (T->isFunctionType()) {
494 Diag(Loc, diag::err_illegal_decl_array_of_functions)
495 << getPrintableNameForEntity(Entity);
496 return QualType();
497 }
498
499 // C++ 8.3.2p4: There shall be no ... arrays of references ...
500 if (T->isReferenceType()) {
501 Diag(Loc, diag::err_illegal_decl_array_of_references)
502 << getPrintableNameForEntity(Entity);
503 return QualType();
504 }
505
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000506 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
507 Diag(Loc, diag::err_illegal_decl_array_of_auto)
508 << getPrintableNameForEntity(Entity);
509 return QualType();
510 }
511
Douglas Gregorcd281c32009-02-28 00:25:32 +0000512 if (const RecordType *EltTy = T->getAsRecordType()) {
513 // If the element type is a struct or union that contains a variadic
514 // array, accept it as a GNU extension: C99 6.7.2.1p2.
515 if (EltTy->getDecl()->hasFlexibleArrayMember())
516 Diag(Loc, diag::ext_flexible_array_in_array) << T;
517 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000518 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
519 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000520 }
521
522 // C99 6.7.5.2p1: The size expression shall have integer type.
523 if (ArraySize && !ArraySize->isTypeDependent() &&
524 !ArraySize->getType()->isIntegerType()) {
525 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
526 << ArraySize->getType() << ArraySize->getSourceRange();
527 ArraySize->Destroy(Context);
528 return QualType();
529 }
530 llvm::APSInt ConstVal(32);
531 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000532 if (ASM == ArrayType::Star)
533 T = Context.getVariableArrayType(T, 0, ASM, Quals);
534 else
535 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000536 } else if (ArraySize->isValueDependent()) {
537 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
538 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
539 (!T->isDependentType() && !T->isConstantSizeType())) {
540 // Per C99, a variable array is an array with either a non-constant
541 // size or an element type that has a non-constant-size
542 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
543 } else {
544 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
545 // have a value greater than zero.
546 if (ConstVal.isSigned()) {
547 if (ConstVal.isNegative()) {
548 Diag(ArraySize->getLocStart(),
549 diag::err_typecheck_negative_array_size)
550 << ArraySize->getSourceRange();
551 return QualType();
552 } else if (ConstVal == 0) {
553 // GCC accepts zero sized static arrays.
554 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
555 << ArraySize->getSourceRange();
556 }
557 }
558 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
559 }
560 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
561 if (!getLangOptions().C99) {
562 if (ArraySize && !ArraySize->isTypeDependent() &&
563 !ArraySize->isValueDependent() &&
564 !ArraySize->isIntegerConstantExpr(Context))
565 Diag(Loc, diag::ext_vla);
566 else if (ASM != ArrayType::Normal || Quals != 0)
567 Diag(Loc, diag::ext_c99_array_usage);
568 }
569
570 return T;
571}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000572
573/// \brief Build an ext-vector type.
574///
575/// Run the required checks for the extended vector type.
576QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
577 SourceLocation AttrLoc) {
578
579 Expr *Arg = (Expr *)ArraySize.get();
580
581 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
582 // in conjunction with complex types (pointers, arrays, functions, etc.).
583 if (!T->isDependentType() &&
584 !T->isIntegerType() && !T->isRealFloatingType()) {
585 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
586 return QualType();
587 }
588
589 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
590 llvm::APSInt vecSize(32);
591 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
592 Diag(AttrLoc, diag::err_attribute_argument_not_int)
593 << "ext_vector_type" << Arg->getSourceRange();
594 return QualType();
595 }
596
597 // unlike gcc's vector_size attribute, the size is specified as the
598 // number of elements, not the number of bytes.
599 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
600
601 if (vectorSize == 0) {
602 Diag(AttrLoc, diag::err_attribute_zero_size)
603 << Arg->getSourceRange();
604 return QualType();
605 }
606
607 if (!T->isDependentType())
608 return Context.getExtVectorType(T, vectorSize);
609 }
610
611 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
612 AttrLoc);
613}
Douglas Gregorcd281c32009-02-28 00:25:32 +0000614
Douglas Gregor724651c2009-02-28 01:04:19 +0000615/// \brief Build a function type.
616///
617/// This routine checks the function type according to C++ rules and
618/// under the assumption that the result type and parameter types have
619/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000620/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000621/// simpler form that is only suitable for this narrow use case.
622///
623/// \param T The return type of the function.
624///
625/// \param ParamTypes The parameter types of the function. This array
626/// will be modified to account for adjustments to the types of the
627/// function parameters.
628///
629/// \param NumParamTypes The number of parameter types in ParamTypes.
630///
631/// \param Variadic Whether this is a variadic function type.
632///
633/// \param Quals The cvr-qualifiers to be applied to the function type.
634///
635/// \param Loc The location of the entity whose type involves this
636/// function type or, if there is no such entity, the location of the
637/// type that will have function type.
638///
639/// \param Entity The name of the entity that involves the function
640/// type, if known.
641///
642/// \returns A suitable function type, if there are no
643/// errors. Otherwise, returns a NULL type.
644QualType Sema::BuildFunctionType(QualType T,
645 QualType *ParamTypes,
646 unsigned NumParamTypes,
647 bool Variadic, unsigned Quals,
648 SourceLocation Loc, DeclarationName Entity) {
649 if (T->isArrayType() || T->isFunctionType()) {
650 Diag(Loc, diag::err_func_returning_array_function) << T;
651 return QualType();
652 }
653
654 bool Invalid = false;
655 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000656 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
657 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000658 Diag(Loc, diag::err_param_with_void_type);
659 Invalid = true;
660 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000661
Douglas Gregor724651c2009-02-28 01:04:19 +0000662 ParamTypes[Idx] = ParamType;
663 }
664
665 if (Invalid)
666 return QualType();
667
668 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
669 Quals);
670}
Douglas Gregor949bf692009-06-09 22:17:39 +0000671
672/// \brief Build a member pointer type \c T Class::*.
673///
674/// \param T the type to which the member pointer refers.
675/// \param Class the class type into which the member pointer points.
676/// \param Quals Qualifiers applied to the member pointer type
677/// \param Loc the location where this type begins
678/// \param Entity the name of the entity that will have this member pointer type
679///
680/// \returns a member pointer type, if successful, or a NULL type if there was
681/// an error.
682QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
683 unsigned Quals, SourceLocation Loc,
684 DeclarationName Entity) {
685 // Verify that we're not building a pointer to pointer to function with
686 // exception specification.
687 if (CheckDistantExceptionSpec(T)) {
688 Diag(Loc, diag::err_distant_exception_spec);
689
690 // FIXME: If we're doing this as part of template instantiation,
691 // we should return immediately.
692
693 // Build the type anyway, but use the canonical type so that the
694 // exception specifiers are stripped off.
695 T = Context.getCanonicalType(T);
696 }
697
698 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
699 // with reference type, or "cv void."
700 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000701 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000702 << (Entity? Entity.getAsString() : "type name");
703 return QualType();
704 }
705
706 if (T->isVoidType()) {
707 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
708 << (Entity? Entity.getAsString() : "type name");
709 return QualType();
710 }
711
712 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
713 // object or incomplete types shall not be restrict-qualified."
714 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
715 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
716 << T;
717
718 // FIXME: If we're doing this as part of template instantiation,
719 // we should return immediately.
720 Quals &= ~QualType::Restrict;
721 }
722
723 if (!Class->isDependentType() && !Class->isRecordType()) {
724 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
725 return QualType();
726 }
727
728 return Context.getMemberPointerType(T, Class.getTypePtr())
729 .getQualifiedType(Quals);
730}
Anders Carlsson9a917e42009-06-12 22:56:54 +0000731
732/// \brief Build a block pointer type.
733///
734/// \param T The type to which we'll be building a block pointer.
735///
736/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
737///
738/// \param Loc The location of the entity whose type involves this
739/// block pointer type or, if there is no such entity, the location of the
740/// type that will have block pointer type.
741///
742/// \param Entity The name of the entity that involves the block pointer
743/// type, if known.
744///
745/// \returns A suitable block pointer type, if there are no
746/// errors. Otherwise, returns a NULL type.
747QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
748 SourceLocation Loc,
749 DeclarationName Entity) {
750 if (!T.getTypePtr()->isFunctionType()) {
751 Diag(Loc, diag::err_nonfunction_block_type);
752 return QualType();
753 }
754
755 return Context.getBlockPointerType(T).getQualifiedType(Quals);
756}
757
Mike Stump98eb8a72009-02-04 22:31:32 +0000758/// GetTypeForDeclarator - Convert the type for the specified
759/// declarator to Type instances. Skip the outermost Skip type
760/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000761///
762/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
763/// owns the declaration of a type (e.g., the definition of a struct
764/// type), then *OwnedDecl will receive the owned declaration.
765QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
766 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000767 bool OmittedReturnType = false;
768
769 if (D.getContext() == Declarator::BlockLiteralContext
770 && Skip == 0
771 && !D.getDeclSpec().hasTypeSpecifier()
772 && (D.getNumTypeObjects() == 0
773 || (D.getNumTypeObjects() == 1
774 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
775 OmittedReturnType = true;
776
Chris Lattnerb23deda2007-08-28 16:40:32 +0000777 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000778 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000779 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
780 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000781
782 // Determine the type of the declarator. Not all forms of declarator
783 // have a type.
784 QualType T;
785 switch (D.getKind()) {
786 case Declarator::DK_Abstract:
787 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000788 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000789 const DeclSpec &DS = D.getDeclSpec();
790 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000791 // We default to a dependent type initially. Can be modified by
792 // the first return statement.
793 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000794 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000795 bool isInvalid = false;
796 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000797 if (isInvalid)
798 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000799 else if (OwnedDecl && DS.isTypeSpecOwned())
800 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000801 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000802 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000803 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000804
805 case Declarator::DK_Constructor:
806 case Declarator::DK_Destructor:
807 case Declarator::DK_Conversion:
808 // Constructors and destructors don't have return types. Use
809 // "void" instead. Conversion operators will check their return
810 // types separately.
811 T = Context.VoidTy;
812 break;
813 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000814
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000815 if (T == Context.UndeducedAutoTy) {
816 int Error = -1;
817
818 switch (D.getContext()) {
819 case Declarator::KNRTypeListContext:
820 assert(0 && "K&R type lists aren't allowed in C++");
821 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000822 case Declarator::PrototypeContext:
823 Error = 0; // Function prototype
824 break;
825 case Declarator::MemberContext:
826 switch (cast<TagDecl>(CurContext)->getTagKind()) {
827 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
828 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
829 case TagDecl::TK_union: Error = 2; /* Union member */ break;
830 case TagDecl::TK_class: Error = 3; /* Class member */ break;
831 }
832 break;
833 case Declarator::CXXCatchContext:
834 Error = 4; // Exception declaration
835 break;
836 case Declarator::TemplateParamContext:
837 Error = 5; // Template parameter
838 break;
839 case Declarator::BlockLiteralContext:
840 Error = 6; // Block literal
841 break;
842 case Declarator::FileContext:
843 case Declarator::BlockContext:
844 case Declarator::ForContext:
845 case Declarator::ConditionContext:
846 case Declarator::TypeNameContext:
847 break;
848 }
849
850 if (Error != -1) {
851 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
852 << Error;
853 T = Context.IntTy;
854 D.setInvalidType(true);
855 }
856 }
857
Douglas Gregorcd281c32009-02-28 00:25:32 +0000858 // The name we're declaring, if any.
859 DeclarationName Name;
860 if (D.getIdentifier())
861 Name = D.getIdentifier();
862
Mike Stump98eb8a72009-02-04 22:31:32 +0000863 // Walk the DeclTypeInfo, building the recursive type as we go.
864 // DeclTypeInfos are ordered from the identifier out, which is
865 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000866 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
867 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 switch (DeclType.Kind) {
869 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000870 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000871 // If blocks are disabled, emit an error.
872 if (!LangOpts.Blocks)
873 Diag(DeclType.Loc, diag::err_blocks_disable);
874
Anders Carlsson9a917e42009-06-12 22:56:54 +0000875 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
876 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000877 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000879 // Verify that we're not building a pointer to pointer to function with
880 // exception specification.
881 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
882 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
883 D.setInvalidType(true);
884 // Build the type anyway.
885 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000886 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000888 case DeclaratorChunk::Reference:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000889 // Verify that we're not building a reference to pointer to function with
890 // exception specification.
891 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
892 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
893 D.setInvalidType(true);
894 // Build the type anyway.
895 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000896 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
897 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000898 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 break;
900 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000901 // Verify that we're not building an array of pointers to function with
902 // exception specification.
903 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
904 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
905 D.setInvalidType(true);
906 // Build the type anyway.
907 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000908 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000909 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 ArrayType::ArraySizeModifier ASM;
911 if (ATI.isStar)
912 ASM = ArrayType::Star;
913 else if (ATI.hasStatic)
914 ASM = ArrayType::Static;
915 else
916 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000917 if (ASM == ArrayType::Star &&
918 D.getContext() != Declarator::PrototypeContext) {
919 // FIXME: This check isn't quite right: it allows star in prototypes
920 // for function definitions, and disallows some edge cases detailed
921 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
922 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
923 ASM = ArrayType::Normal;
924 D.setInvalidType(true);
925 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000926 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 break;
928 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000929 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // If the function declarator has a prototype (i.e. it is not () and
931 // does not have a K&R-style identifier list), then the arguments are part
932 // of the type, otherwise the argument list is ().
933 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +0000934
Chris Lattnercd881292007-12-19 05:31:29 +0000935 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000936 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000937 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000938 T = Context.IntTy;
939 D.setInvalidType(true);
940 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000941
Douglas Gregor402abb52009-05-28 23:31:59 +0000942 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
943 // C++ [dcl.fct]p6:
944 // Types shall not be defined in return or parameter types.
945 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
946 if (Tag->isDefinition())
947 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
948 << Context.getTypeDeclType(Tag);
949 }
950
Sebastian Redl3cc97262009-05-31 11:47:27 +0000951 // Exception specs are not allowed in typedefs. Complain, but add it
952 // anyway.
953 if (FTI.hasExceptionSpec &&
954 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
955 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
956
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000957 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000958 if (getLangOptions().CPlusPlus) {
959 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
960 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +0000961 llvm::SmallVector<QualType, 4> Exceptions;
962 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +0000963 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
964 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
965 // Check that the type is valid for an exception spec, and drop it
966 // if not.
967 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
968 Exceptions.push_back(ET);
969 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000970 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
971 FTI.hasExceptionSpec,
972 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +0000973 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +0000974 } else if (FTI.isVariadic) {
975 // We allow a zero-parameter variadic function in C if the
976 // function is marked with the "overloadable"
977 // attribute. Scan for this attribute now.
978 bool Overloadable = false;
979 for (const AttributeList *Attrs = D.getAttributes();
980 Attrs; Attrs = Attrs->getNext()) {
981 if (Attrs->getKind() == AttributeList::AT_overloadable) {
982 Overloadable = true;
983 break;
984 }
985 }
986
987 if (!Overloadable)
988 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
989 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000990 } else {
991 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000992 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000993 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000994 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000996 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 } else {
998 // Otherwise, we have a function with an argument list that is
999 // potentially variadic.
1000 llvm::SmallVector<QualType, 16> ArgTys;
1001
1002 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001003 ParmVarDecl *Param =
1004 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001005 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001006 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001007
1008 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001009 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 // Look for 'void'. void is allowed only as a single argument to a
1012 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001013 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001014 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 // If this is something like 'float(int, void)', reject it. 'void'
1016 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1017 // have arguments of incomplete type.
1018 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1019 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001020 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001021 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001022 } else if (FTI.ArgInfo[i].Ident) {
1023 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001025 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001026 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001027 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001028 } else {
1029 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001030 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001031 Diag(DeclType.Loc, diag::err_void_param_qualified);
1032
1033 // Do not add 'void' to the ArgTys list.
1034 break;
1035 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001036 } else if (!FTI.hasPrototype) {
1037 if (ArgTy->isPromotableIntegerType()) {
1038 ArgTy = Context.IntTy;
1039 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1040 if (BTy->getKind() == BuiltinType::Float)
1041 ArgTy = Context.DoubleTy;
1042 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 }
1044
1045 ArgTys.push_back(ArgTy);
1046 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001047
1048 llvm::SmallVector<QualType, 4> Exceptions;
1049 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +00001050 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1051 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
1052 // Check that the type is valid for an exception spec, and drop it if
1053 // not.
1054 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1055 Exceptions.push_back(ET);
1056 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001057
Jay Foadbeaaccd2009-05-21 09:52:38 +00001058 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001059 FTI.isVariadic, FTI.TypeQuals,
1060 FTI.hasExceptionSpec,
1061 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001062 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 }
1064 break;
1065 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001066 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001067 // Verify that we're not building a pointer to pointer to function with
1068 // exception specification.
1069 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1070 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1071 D.setInvalidType(true);
1072 // Build the type anyway.
1073 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001074 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001075 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001076 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
1077 NestedNameSpecifier *NNS
1078 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1079 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1080 ClsType = QualType(NNS->getAsType(), 0);
1081 } else if (CXXRecordDecl *RD
1082 = dyn_cast_or_null<CXXRecordDecl>(
1083 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001084 ClsType = Context.getTagDeclType(RD);
1085 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001086 Diag(DeclType.Mem.Scope().getBeginLoc(),
1087 diag::err_illegal_decl_mempointer_in_nonclass)
1088 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1089 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001090 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001091 }
1092
Douglas Gregor949bf692009-06-09 22:17:39 +00001093 if (!ClsType.isNull())
1094 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1095 DeclType.Loc, D.getIdentifier());
1096 if (T.isNull()) {
1097 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001098 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001099 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001100 break;
1101 }
1102
Douglas Gregorcd281c32009-02-28 00:25:32 +00001103 if (T.isNull()) {
1104 D.setInvalidType(true);
1105 T = Context.IntTy;
1106 }
1107
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001108 // See if there are any attributes on this declarator chunk.
1109 if (const AttributeList *AL = DeclType.getAttrs())
1110 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001112
1113 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001114 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
1115 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001116
1117 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1118 // for a nonstatic member function, the function type to which a pointer
1119 // to member refers, or the top-level function type of a function typedef
1120 // declaration.
1121 if (FnTy->getTypeQuals() != 0 &&
1122 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001123 ((D.getContext() != Declarator::MemberContext &&
1124 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +00001125 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001126 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001127 if (D.isFunctionDeclarator())
1128 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1129 else
1130 Diag(D.getIdentifierLoc(),
1131 diag::err_invalid_qualified_typedef_function_type_use);
1132
1133 // Strip the cv-quals from the type.
1134 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001135 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001136 }
1137 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001139 // If there were any type attributes applied to the decl itself (not the
1140 // type, apply the type attribute to the type!)
1141 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001142 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 return T;
1145}
1146
Sebastian Redlef65f062009-05-29 18:02:33 +00001147/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1148/// exception specification. Incomplete types, or pointers to incomplete types
1149/// other than void are not allowed.
1150bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1151 // FIXME: This may not correctly work with the fix for core issue 437,
1152 // where a class's own type is considered complete within its body.
1153
1154 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1155 // an incomplete type.
1156 if (T->isIncompleteType())
1157 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1158 << Range << T << /*direct*/0;
1159
1160 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1161 // an incomplete type a pointer or reference to an incomplete type, other
1162 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001163 int kind;
1164 if (const PointerType* IT = T->getAsPointerType()) {
1165 T = IT->getPointeeType();
1166 kind = 1;
Sebastian Redlef65f062009-05-29 18:02:33 +00001167 } else if (const ReferenceType* IT = T->getAsReferenceType()) {
1168 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001169 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001170 } else
1171 return false;
1172
1173 if (T->isIncompleteType() && !T->isVoidType())
1174 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1175 << Range << T << /*indirect*/kind;
1176
1177 return false;
1178}
1179
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001180/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1181/// to member to a function with an exception specification. This means that
1182/// it is invalid to add another level of indirection.
1183bool Sema::CheckDistantExceptionSpec(QualType T) {
1184 if (const PointerType *PT = T->getAsPointerType())
1185 T = PT->getPointeeType();
1186 else if (const MemberPointerType *PT = T->getAsMemberPointerType())
1187 T = PT->getPointeeType();
1188 else
1189 return false;
1190
1191 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
1192 if (!FnT)
1193 return false;
1194
1195 return FnT->hasExceptionSpec();
1196}
1197
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001198/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1199/// exception specifications. Exception specifications are equivalent if
1200/// they allow exactly the same set of exception types. It does not matter how
1201/// that is achieved. See C++ [except.spec]p2.
1202bool Sema::CheckEquivalentExceptionSpec(
1203 const FunctionProtoType *Old, SourceLocation OldLoc,
1204 const FunctionProtoType *New, SourceLocation NewLoc) {
1205 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1206 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1207 if (OldAny && NewAny)
1208 return false;
1209 if (OldAny || NewAny) {
1210 Diag(NewLoc, diag::err_mismatched_exception_spec);
1211 Diag(OldLoc, diag::note_previous_declaration);
1212 return true;
1213 }
1214
1215 bool Success = true;
1216 // Both have a definite exception spec. Collect the first set, then compare
1217 // to the second.
1218 llvm::SmallPtrSet<const Type*, 8> Types;
1219 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1220 E = Old->exception_end(); I != E; ++I)
1221 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1222
1223 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1224 E = New->exception_end(); I != E && Success; ++I)
1225 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1226
1227 Success = Success && Types.empty();
1228
1229 if (Success) {
1230 return false;
1231 }
1232 Diag(NewLoc, diag::err_mismatched_exception_spec);
1233 Diag(OldLoc, diag::note_previous_declaration);
1234 return true;
1235}
1236
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001237/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001238/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001239QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1240 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001241 QualType T = MDecl->getResultType();
1242 llvm::SmallVector<QualType, 16> ArgTys;
1243
Fariborz Jahanian35600022007-11-09 17:18:29 +00001244 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001245 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001246 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001247 selfTy = Context.getPointerType(selfTy);
1248 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001249 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001250 ArgTys.push_back(Context.getObjCIdType());
1251 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +00001252
Chris Lattner89951a82009-02-20 18:43:26 +00001253 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1254 E = MDecl->param_end(); PI != E; ++PI) {
1255 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001256 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001257 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001258 ArgTys.push_back(ArgTy);
1259 }
1260 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001261 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001262 return T;
1263}
1264
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001265/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1266/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1267/// they point to and return true. If T1 and T2 aren't pointer types
1268/// or pointer-to-member types, or if they are not similar at this
1269/// level, returns false and leaves T1 and T2 unchanged. Top-level
1270/// qualifiers on T1 and T2 are ignored. This function will typically
1271/// be called in a loop that successively "unwraps" pointer and
1272/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001273bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +00001274 const PointerType *T1PtrType = T1->getAsPointerType(),
1275 *T2PtrType = T2->getAsPointerType();
1276 if (T1PtrType && T2PtrType) {
1277 T1 = T1PtrType->getPointeeType();
1278 T2 = T2PtrType->getPointeeType();
1279 return true;
1280 }
1281
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001282 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
1283 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001284 if (T1MPType && T2MPType &&
1285 Context.getCanonicalType(T1MPType->getClass()) ==
1286 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001287 T1 = T1MPType->getPointeeType();
1288 T2 = T2MPType->getPointeeType();
1289 return true;
1290 }
Douglas Gregor57373262008-10-22 14:17:15 +00001291 return false;
1292}
1293
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001294Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 // C99 6.7.6: Type names have no identifier. This is already validated by
1296 // the parser.
1297 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
1298
Douglas Gregor402abb52009-05-28 23:31:59 +00001299 TagDecl *OwnedTag = 0;
1300 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001301 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001302 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001303
Douglas Gregor402abb52009-05-28 23:31:59 +00001304 if (getLangOptions().CPlusPlus) {
1305 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001306 CheckExtraCXXDefaultArguments(D);
1307
Douglas Gregor402abb52009-05-28 23:31:59 +00001308 // C++0x [dcl.type]p3:
1309 // A type-specifier-seq shall not define a class or enumeration
1310 // unless it appears in the type-id of an alias-declaration
1311 // (7.1.3).
1312 if (OwnedTag && OwnedTag->isDefinition())
1313 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1314 << Context.getTypeDeclType(OwnedTag);
1315 }
1316
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 return T.getAsOpaquePtr();
1318}
1319
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001320
1321
1322//===----------------------------------------------------------------------===//
1323// Type Attribute Processing
1324//===----------------------------------------------------------------------===//
1325
1326/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1327/// specified type. The attribute contains 1 argument, the id of the address
1328/// space for the type.
1329static void HandleAddressSpaceTypeAttribute(QualType &Type,
1330 const AttributeList &Attr, Sema &S){
1331 // If this type is already address space qualified, reject it.
1332 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1333 // for two or more different address spaces."
1334 if (Type.getAddressSpace()) {
1335 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1336 return;
1337 }
1338
1339 // Check the attribute arguments.
1340 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001342 return;
1343 }
1344 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1345 llvm::APSInt addrSpace(32);
1346 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001347 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1348 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001349 return;
1350 }
1351
1352 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001353 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001354}
1355
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001356/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1357/// specified type. The attribute contains 1 argument, weak or strong.
1358static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001359 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001360 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001361 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001362 return;
1363 }
1364
1365 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001366 if (!Attr.getParameterName()) {
1367 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1368 << "objc_gc" << 1;
1369 return;
1370 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001371 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001372 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001373 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1374 return;
1375 }
1376 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001377 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001378 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001379 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001380 else {
1381 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1382 << "objc_gc" << Attr.getParameterName();
1383 return;
1384 }
1385
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001386 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001387}
1388
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001389void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001390 // Scan through and apply attributes to this type where it makes sense. Some
1391 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1392 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001393 // apply to the decl. Here we apply type attributes and ignore the rest.
1394 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001395 // If this is an attribute we can handle, do so now, otherwise, add it to
1396 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001397 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001398 default: break;
1399 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001400 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1401 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001402 case AttributeList::AT_objc_gc:
1403 HandleObjCGCTypeAttribute(Result, *AL, *this);
1404 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001405 }
Chris Lattner232e8822008-02-21 01:08:11 +00001406 }
Chris Lattner232e8822008-02-21 01:08:11 +00001407}
1408
Douglas Gregor86447ec2009-03-09 16:13:40 +00001409/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001410///
1411/// This routine checks whether the type @p T is complete in any
1412/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001413/// type, returns false. If @p T is a class template specialization,
1414/// this routine then attempts to perform class template
1415/// instantiation. If instantiation fails, or if @p T is incomplete
1416/// and cannot be completed, issues the diagnostic @p diag (giving it
1417/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001418///
1419/// @param Loc The location in the source that the incomplete type
1420/// diagnostic should refer to.
1421///
1422/// @param T The type that this routine is examining for completeness.
1423///
1424/// @param diag The diagnostic value (e.g.,
1425/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1426/// for the error message if @p T is incomplete.
1427///
1428/// @param Range1 An optional range in the source code that will be a
1429/// part of the "incomplete type" error message.
1430///
1431/// @param Range2 An optional range in the source code that will be a
1432/// part of the "incomplete type" error message.
1433///
1434/// @param PrintType If non-NULL, the type that should be printed
1435/// instead of @p T. This parameter should be used when the type that
1436/// we're checking for incompleteness isn't the type that should be
1437/// displayed to the user, e.g., when T is a type and PrintType is a
1438/// pointer to T.
1439///
1440/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1441/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001442bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001443 SourceRange Range1, SourceRange Range2,
1444 QualType PrintType) {
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001445 // FIXME: Add this assertion to help us flush out problems with
1446 // checking for dependent types and type-dependent expressions.
1447 //
1448 // assert(!T->isDependentType() &&
1449 // "Can't ask whether a dependent type is complete");
1450
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001451 // If we have a complete type, we're done.
1452 if (!T->isIncompleteType())
1453 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001454
Douglas Gregord475b8d2009-03-25 21:17:03 +00001455 // If we have a class template specialization or a class member of a
1456 // class template specialization, try to instantiate it.
1457 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001458 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001459 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001460 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1461 // Update the class template specialization's location to
1462 // refer to the point of instantiation.
1463 if (Loc.isValid())
1464 ClassTemplateSpec->setLocation(Loc);
1465 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1466 /*ExplicitInstantiation=*/false);
1467 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001468 } else if (CXXRecordDecl *Rec
1469 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1470 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1471 // Find the class template specialization that surrounds this
1472 // member class.
1473 ClassTemplateSpecializationDecl *Spec = 0;
1474 for (DeclContext *Parent = Rec->getDeclContext();
1475 Parent && !Spec; Parent = Parent->getParent())
1476 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1477 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001478 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1479 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001480 }
1481 }
1482 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001483
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001484 if (PrintType.isNull())
1485 PrintType = T;
1486
1487 // We have an incomplete type. Produce a diagnostic.
1488 Diag(Loc, diag) << PrintType << Range1 << Range2;
1489
1490 // If the type was a forward declaration of a class/struct/union
1491 // type, produce
1492 const TagType *Tag = 0;
1493 if (const RecordType *Record = T->getAsRecordType())
1494 Tag = Record;
1495 else if (const EnumType *Enum = T->getAsEnumType())
1496 Tag = Enum;
1497
1498 if (Tag && !Tag->getDecl()->isInvalidDecl())
1499 Diag(Tag->getDecl()->getLocation(),
1500 Tag->isBeingDefined() ? diag::note_type_being_defined
1501 : diag::note_forward_declaration)
1502 << QualType(Tag, 0);
1503
1504 return true;
1505}
Douglas Gregore6258932009-03-19 00:39:20 +00001506
1507/// \brief Retrieve a version of the type 'T' that is qualified by the
1508/// nested-name-specifier contained in SS.
1509QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1510 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1511 return T;
1512
Douglas Gregorab452ba2009-03-26 23:50:42 +00001513 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001514 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001515 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001516}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001517
1518QualType Sema::BuildTypeofExprType(Expr *E) {
1519 return Context.getTypeOfExprType(E);
1520}
1521
1522QualType Sema::BuildDecltypeType(Expr *E) {
1523 if (E->getType() == Context.OverloadTy) {
1524 Diag(E->getLocStart(),
1525 diag::err_cannot_determine_declared_type_of_overloaded_function);
1526 return QualType();
1527 }
1528 return Context.getDecltypeType(E);
1529}