blob: bfbc6c370809bf2ae269954bf0963d2628cb30c3 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Douglas Gregor930d8b52009-01-30 22:09:00 +000022/// \brief Convert the specified declspec to the appropriate type
23/// object.
24/// \param DS the declaration specifiers
25/// \returns The type described by the declaration specifiers, or NULL
26/// if there was an error.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +000027QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000028 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
29 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000030 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000031
32 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000033 case DeclSpec::TST_void:
34 Result = Context.VoidTy;
35 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 case DeclSpec::TST_char:
37 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000038 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000040 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 else {
42 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
43 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000044 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 }
Chris Lattner958858e2008-02-20 21:40:32 +000046 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000047 case DeclSpec::TST_wchar:
48 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
49 Result = Context.WCharTy;
50 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000051 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
52 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000053 Result = Context.getSignedWCharType();
54 } else {
55 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000057 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
58 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000059 Result = Context.getUnsignedWCharType();
60 }
61 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000062 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000063 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000064 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000065 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000066 DS.getNumProtocolQualifiers());
67 break;
68 }
69
Chris Lattnerd658b562008-04-05 06:32:51 +000070 // Unspecified typespec defaults to int in C90. However, the C90 grammar
71 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
72 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
73 // Note that the one exception to this is function definitions, which are
74 // allowed to be completely missing a declspec. This is handled in the
75 // parser already though by it pretending to have seen an 'int' in this
76 // case.
77 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +000078 // In C89 mode, we only warn if there is a completely missing declspec
79 // when one is not allowed.
80 if (DS.isEmpty())
Chris Lattner173144a2009-02-27 22:31:56 +000081 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
82 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
83 "int");
Douglas Gregor4310f4e2009-02-16 22:38:20 +000084 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000085 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
86 // "At least one type specifier shall be given in the declaration
87 // specifiers in each declaration, and in the specifier-qualifier list in
88 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +000089 // FIXME: Does Microsoft really have the implicit int extension in C++?
90 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
91 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +000092 : diag::warn_missing_type_specifier;
Chris Lattner173144a2009-02-27 22:31:56 +000093 Diag(DS.getSourceRange().getBegin(), DK)
94 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
95 "int");
Chris Lattnerd658b562008-04-05 06:32:51 +000096 }
97
98 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000099 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
101 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000102 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
103 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
104 case DeclSpec::TSW_long: Result = Context.LongTy; break;
105 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 }
107 } else {
108 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000109 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
110 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
111 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
112 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 }
114 }
Chris Lattner958858e2008-02-20 21:40:32 +0000115 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000116 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000117 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000118 case DeclSpec::TST_double:
119 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000120 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000121 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000122 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000123 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000124 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 case DeclSpec::TST_decimal32: // _Decimal32
126 case DeclSpec::TST_decimal64: // _Decimal64
127 case DeclSpec::TST_decimal128: // _Decimal128
128 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000129 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 case DeclSpec::TST_enum:
131 case DeclSpec::TST_union:
132 case DeclSpec::TST_struct: {
133 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000134 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
136 DS.getTypeSpecSign() == 0 &&
137 "Can't handle qualifiers on typedef names yet!");
138 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000139 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000140 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000142 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
144 DS.getTypeSpecSign() == 0 &&
145 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000146 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000147
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000148 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
149 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
150 // we have this "hack" for now...
151 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
152 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
153 (ObjCProtocolDecl**)PQ,
154 DS.getNumProtocolQualifiers());
155 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000156 // id<protocol-list>
157 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
158 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000159 else if (Result == Context.getObjCClassType())
160 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000161 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000162 diag::err_qualified_class_unsupported) << DS.getSourceRange();
163 else
164 Diag(DS.getSourceRange().getBegin(),
165 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000168 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
Chris Lattner958858e2008-02-20 21:40:32 +0000170 case DeclSpec::TST_typeofType:
171 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
172 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000173 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000174 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000175 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000176 case DeclSpec::TST_typeofExpr: {
177 Expr *E = static_cast<Expr *>(DS.getTypeRep());
178 assert(E && "Didn't get an expression for typeof?");
179 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000180 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000181 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000182 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000183 case DeclSpec::TST_error:
184 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
Chris Lattner958858e2008-02-20 21:40:32 +0000186
187 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000188 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
189 if (getLangOptions().Freestanding)
190 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000191 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000192 }
Chris Lattner958858e2008-02-20 21:40:32 +0000193
194 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
195 "FIXME: imaginary types not supported yet!");
196
Chris Lattner38d8b982008-02-20 22:04:11 +0000197 // See if there are any attributes on the declspec that apply to the type (as
198 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000199 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000200 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000201
Chris Lattner96b77fc2008-04-02 06:50:17 +0000202 // Apply const/volatile/restrict qualifiers to T.
203 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
204
205 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
206 // or incomplete types shall not be restrict-qualified." C++ also allows
207 // restrict-qualified references.
208 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000209 if (Result->isPointerType() || Result->isReferenceType()) {
210 QualType EltTy = Result->isPointerType() ?
211 Result->getAsPointerType()->getPointeeType() :
212 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000213
214 // If we have a pointer or reference, the pointee must have an object or
215 // incomplete type.
216 if (!EltTy->isIncompleteOrObjectType()) {
217 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000218 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000219 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000220 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
221 }
222 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000223 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000224 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000225 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000226 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000227 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000228 }
229
230 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
231 // of a function type includes any type qualifiers, the behavior is
232 // undefined."
233 if (Result->isFunctionType() && TypeQuals) {
234 // Get some location to point at, either the C or V location.
235 SourceLocation Loc;
236 if (TypeQuals & QualType::Const)
237 Loc = DS.getConstSpecLoc();
238 else {
239 assert((TypeQuals & QualType::Volatile) &&
240 "Has CV quals but not C or V?");
241 Loc = DS.getVolatileSpecLoc();
242 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000243 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000244 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000245 }
246
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000247 // C++ [dcl.ref]p1:
248 // Cv-qualified references are ill-formed except when the
249 // cv-qualifiers are introduced through the use of a typedef
250 // (7.1.3) or of a template type argument (14.3), in which
251 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000252 // FIXME: Shouldn't we be checking SCS_typedef here?
253 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000254 TypeQuals && Result->isReferenceType()) {
255 TypeQuals &= ~QualType::Const;
256 TypeQuals &= ~QualType::Volatile;
257 }
258
Chris Lattner96b77fc2008-04-02 06:50:17 +0000259 Result = Result.getQualifiedType(TypeQuals);
260 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000261 return Result;
262}
263
Douglas Gregorcd281c32009-02-28 00:25:32 +0000264static std::string getPrintableNameForEntity(DeclarationName Entity) {
265 if (Entity)
266 return Entity.getAsString();
267
268 return "type name";
269}
270
271/// \brief Build a pointer type.
272///
273/// \param T The type to which we'll be building a pointer.
274///
275/// \param Quals The cvr-qualifiers to be applied to the pointer type.
276///
277/// \param Loc The location of the entity whose type involves this
278/// pointer type or, if there is no such entity, the location of the
279/// type that will have pointer type.
280///
281/// \param Entity The name of the entity that involves the pointer
282/// type, if known.
283///
284/// \returns A suitable pointer type, if there are no
285/// errors. Otherwise, returns a NULL type.
286QualType Sema::BuildPointerType(QualType T, unsigned Quals,
287 SourceLocation Loc, DeclarationName Entity) {
288 if (T->isReferenceType()) {
289 // C++ 8.3.2p4: There shall be no ... pointers to references ...
290 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
291 << getPrintableNameForEntity(Entity);
292 return QualType();
293 }
294
295 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
296 // object or incomplete types shall not be restrict-qualified."
297 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
298 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
299 << T;
300 Quals &= ~QualType::Restrict;
301 }
302
303 // Build the pointer type.
304 return Context.getPointerType(T).getQualifiedType(Quals);
305}
306
307/// \brief Build a reference type.
308///
309/// \param T The type to which we'll be building a reference.
310///
311/// \param Quals The cvr-qualifiers to be applied to the reference type.
312///
313/// \param Loc The location of the entity whose type involves this
314/// reference type or, if there is no such entity, the location of the
315/// type that will have reference type.
316///
317/// \param Entity The name of the entity that involves the reference
318/// type, if known.
319///
320/// \returns A suitable reference type, if there are no
321/// errors. Otherwise, returns a NULL type.
322QualType Sema::BuildReferenceType(QualType T, unsigned Quals,
323 SourceLocation Loc, DeclarationName Entity) {
324 if (T->isReferenceType()) {
325 // C++ [dcl.ref]p4: There shall be no references to references.
326 //
327 // According to C++ DR 106, references to references are only
328 // diagnosed when they are written directly (e.g., "int & &"),
329 // but not when they happen via a typedef:
330 //
331 // typedef int& intref;
332 // typedef intref& intref2;
333 //
334 // Parser::ParserDeclaratorInternal diagnoses the case where
335 // references are written directly; here, we handle the
336 // collapsing of references-to-references as described in C++
337 // DR 106 and amended by C++ DR 540.
338 return T;
339 }
340
341 // C++ [dcl.ref]p1:
342 // A declarator that specifies the type “reference to cv void”
343 // is ill-formed.
344 if (T->isVoidType()) {
345 Diag(Loc, diag::err_reference_to_void);
346 return QualType();
347 }
348
349 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
350 // object or incomplete types shall not be restrict-qualified."
351 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
352 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
353 << T;
354 Quals &= ~QualType::Restrict;
355 }
356
357 // C++ [dcl.ref]p1:
358 // [...] Cv-qualified references are ill-formed except when the
359 // cv-qualifiers are introduced through the use of a typedef
360 // (7.1.3) or of a template type argument (14.3), in which case
361 // the cv-qualifiers are ignored.
362 //
363 // We diagnose extraneous cv-qualifiers for the non-typedef,
364 // non-template type argument case within the parser. Here, we just
365 // ignore any extraneous cv-qualifiers.
366 Quals &= ~QualType::Const;
367 Quals &= ~QualType::Volatile;
368
369 // Handle restrict on references.
370 return Context.getReferenceType(T).getQualifiedType(Quals);
371}
372
373/// \brief Build an array type.
374///
375/// \param T The type of each element in the array.
376///
377/// \param ASM C99 array size modifier (e.g., '*', 'static').
378///
379/// \param ArraySize Expression describing the size of the array.
380///
381/// \param Quals The cvr-qualifiers to be applied to the array's
382/// element type.
383///
384/// \param Loc The location of the entity whose type involves this
385/// array type or, if there is no such entity, the location of the
386/// type that will have array type.
387///
388/// \param Entity The name of the entity that involves the array
389/// type, if known.
390///
391/// \returns A suitable array type, if there are no errors. Otherwise,
392/// returns a NULL type.
393QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
394 Expr *ArraySize, unsigned Quals,
395 SourceLocation Loc, DeclarationName Entity) {
396 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
397 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000398 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000399 diag::err_illegal_decl_array_incomplete_type))
400 return QualType();
401
402 if (T->isFunctionType()) {
403 Diag(Loc, diag::err_illegal_decl_array_of_functions)
404 << getPrintableNameForEntity(Entity);
405 return QualType();
406 }
407
408 // C++ 8.3.2p4: There shall be no ... arrays of references ...
409 if (T->isReferenceType()) {
410 Diag(Loc, diag::err_illegal_decl_array_of_references)
411 << getPrintableNameForEntity(Entity);
412 return QualType();
413 }
414
415 if (const RecordType *EltTy = T->getAsRecordType()) {
416 // If the element type is a struct or union that contains a variadic
417 // array, accept it as a GNU extension: C99 6.7.2.1p2.
418 if (EltTy->getDecl()->hasFlexibleArrayMember())
419 Diag(Loc, diag::ext_flexible_array_in_array) << T;
420 } else if (T->isObjCInterfaceType()) {
421 Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
422 }
423
424 // C99 6.7.5.2p1: The size expression shall have integer type.
425 if (ArraySize && !ArraySize->isTypeDependent() &&
426 !ArraySize->getType()->isIntegerType()) {
427 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
428 << ArraySize->getType() << ArraySize->getSourceRange();
429 ArraySize->Destroy(Context);
430 return QualType();
431 }
432 llvm::APSInt ConstVal(32);
433 if (!ArraySize) {
434 T = Context.getIncompleteArrayType(T, ASM, Quals);
435 } else if (ArraySize->isValueDependent()) {
436 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
437 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
438 (!T->isDependentType() && !T->isConstantSizeType())) {
439 // Per C99, a variable array is an array with either a non-constant
440 // size or an element type that has a non-constant-size
441 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
442 } else {
443 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
444 // have a value greater than zero.
445 if (ConstVal.isSigned()) {
446 if (ConstVal.isNegative()) {
447 Diag(ArraySize->getLocStart(),
448 diag::err_typecheck_negative_array_size)
449 << ArraySize->getSourceRange();
450 return QualType();
451 } else if (ConstVal == 0) {
452 // GCC accepts zero sized static arrays.
453 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
454 << ArraySize->getSourceRange();
455 }
456 }
457 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
458 }
459 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
460 if (!getLangOptions().C99) {
461 if (ArraySize && !ArraySize->isTypeDependent() &&
462 !ArraySize->isValueDependent() &&
463 !ArraySize->isIntegerConstantExpr(Context))
464 Diag(Loc, diag::ext_vla);
465 else if (ASM != ArrayType::Normal || Quals != 0)
466 Diag(Loc, diag::ext_c99_array_usage);
467 }
468
469 return T;
470}
471
Douglas Gregor724651c2009-02-28 01:04:19 +0000472/// \brief Build a function type.
473///
474/// This routine checks the function type according to C++ rules and
475/// under the assumption that the result type and parameter types have
476/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000477/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000478/// simpler form that is only suitable for this narrow use case.
479///
480/// \param T The return type of the function.
481///
482/// \param ParamTypes The parameter types of the function. This array
483/// will be modified to account for adjustments to the types of the
484/// function parameters.
485///
486/// \param NumParamTypes The number of parameter types in ParamTypes.
487///
488/// \param Variadic Whether this is a variadic function type.
489///
490/// \param Quals The cvr-qualifiers to be applied to the function type.
491///
492/// \param Loc The location of the entity whose type involves this
493/// function type or, if there is no such entity, the location of the
494/// type that will have function type.
495///
496/// \param Entity The name of the entity that involves the function
497/// type, if known.
498///
499/// \returns A suitable function type, if there are no
500/// errors. Otherwise, returns a NULL type.
501QualType Sema::BuildFunctionType(QualType T,
502 QualType *ParamTypes,
503 unsigned NumParamTypes,
504 bool Variadic, unsigned Quals,
505 SourceLocation Loc, DeclarationName Entity) {
506 if (T->isArrayType() || T->isFunctionType()) {
507 Diag(Loc, diag::err_func_returning_array_function) << T;
508 return QualType();
509 }
510
511 bool Invalid = false;
512 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
513 QualType ParamType = ParamTypes[Idx];
514 if (ParamType->isArrayType())
515 ParamType = Context.getArrayDecayedType(ParamType);
516 else if (ParamType->isFunctionType())
517 ParamType = Context.getPointerType(ParamType);
518 else if (ParamType->isVoidType()) {
519 Diag(Loc, diag::err_param_with_void_type);
520 Invalid = true;
521 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000522
Douglas Gregor724651c2009-02-28 01:04:19 +0000523 ParamTypes[Idx] = ParamType;
524 }
525
526 if (Invalid)
527 return QualType();
528
529 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
530 Quals);
531}
532
Mike Stump98eb8a72009-02-04 22:31:32 +0000533/// GetTypeForDeclarator - Convert the type for the specified
534/// declarator to Type instances. Skip the outermost Skip type
535/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000536QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000537 bool OmittedReturnType = false;
538
539 if (D.getContext() == Declarator::BlockLiteralContext
540 && Skip == 0
541 && !D.getDeclSpec().hasTypeSpecifier()
542 && (D.getNumTypeObjects() == 0
543 || (D.getNumTypeObjects() == 1
544 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
545 OmittedReturnType = true;
546
Chris Lattnerb23deda2007-08-28 16:40:32 +0000547 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000548 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000549 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
550 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000551
552 // Determine the type of the declarator. Not all forms of declarator
553 // have a type.
554 QualType T;
555 switch (D.getKind()) {
556 case Declarator::DK_Abstract:
557 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000558 case Declarator::DK_Operator: {
559 const DeclSpec& DS = D.getDeclSpec();
560 if (OmittedReturnType)
561 // We default to a dependent type initially. Can be modified by
562 // the first return statement.
563 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000564 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000565 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000566 if (T.isNull())
567 return T;
568 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000569 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000570 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000571
572 case Declarator::DK_Constructor:
573 case Declarator::DK_Destructor:
574 case Declarator::DK_Conversion:
575 // Constructors and destructors don't have return types. Use
576 // "void" instead. Conversion operators will check their return
577 // types separately.
578 T = Context.VoidTy;
579 break;
580 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000581
Douglas Gregorcd281c32009-02-28 00:25:32 +0000582 // The name we're declaring, if any.
583 DeclarationName Name;
584 if (D.getIdentifier())
585 Name = D.getIdentifier();
586
Mike Stump98eb8a72009-02-04 22:31:32 +0000587 // Walk the DeclTypeInfo, building the recursive type as we go.
588 // DeclTypeInfos are ordered from the identifier out, which is
589 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000590 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
591 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 switch (DeclType.Kind) {
593 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000594 case DeclaratorChunk::BlockPointer:
595 if (DeclType.Cls.TypeQuals)
596 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
597 if (!T.getTypePtr()->isFunctionType())
598 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
599 else
600 T = Context.getBlockPointerType(T);
601 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000603 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000605 case DeclaratorChunk::Reference:
606 T = BuildReferenceType(T,
607 DeclType.Ref.HasRestrict? QualType::Restrict : 0,
608 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 break;
610 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000611 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000612 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 ArrayType::ArraySizeModifier ASM;
614 if (ATI.isStar)
615 ASM = ArrayType::Star;
616 else if (ATI.hasStatic)
617 ASM = ArrayType::Static;
618 else
619 ASM = ArrayType::Normal;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 break;
622 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000623 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 // If the function declarator has a prototype (i.e. it is not () and
625 // does not have a K&R-style identifier list), then the arguments are part
626 // of the type, otherwise the argument list is ().
627 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000628
Chris Lattnercd881292007-12-19 05:31:29 +0000629 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000630 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000631 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000632 T = Context.IntTy;
633 D.setInvalidType(true);
634 }
635
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000636 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000637 if (getLangOptions().CPlusPlus) {
638 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
639 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000640 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000641 } else if (FTI.isVariadic) {
642 // We allow a zero-parameter variadic function in C if the
643 // function is marked with the "overloadable"
644 // attribute. Scan for this attribute now.
645 bool Overloadable = false;
646 for (const AttributeList *Attrs = D.getAttributes();
647 Attrs; Attrs = Attrs->getNext()) {
648 if (Attrs->getKind() == AttributeList::AT_overloadable) {
649 Overloadable = true;
650 break;
651 }
652 }
653
654 if (!Overloadable)
655 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
656 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000657 } else {
658 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000659 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000660 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000661 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000663 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 } else {
665 // Otherwise, we have a function with an argument list that is
666 // potentially variadic.
667 llvm::SmallVector<QualType, 16> ArgTys;
668
669 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000670 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
671 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000672 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000673 //
674 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
675 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000676 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000677 // argument type in the function prototype *will not* match the
678 // type in ParmVarDecl (which makes the code generator unhappy).
679 //
680 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000681 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000682 // it should be driving off the type being created here.
683 //
684 // FIXME: If a source translation tool needs to see the original type,
685 // then we need to consider storing both types somewhere...
686 //
Chris Lattnere6327742008-04-02 05:18:44 +0000687 if (ArgTy->isArrayType()) {
688 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000689 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000690 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 // Look for 'void'. void is allowed only as a single argument to a
693 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000694 // int(void) as a FunctionProtoType with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000695 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 // If this is something like 'float(int, void)', reject it. 'void'
697 // is an incomplete type (C99 6.2.5p19) and function decls cannot
698 // have arguments of incomplete type.
699 if (FTI.NumArgs != 1 || FTI.isVariadic) {
700 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000701 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000702 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000703 } else if (FTI.ArgInfo[i].Ident) {
704 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000706 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000707 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000708 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000709 } else {
710 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000711 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000712 Diag(DeclType.Loc, diag::err_void_param_qualified);
713
714 // Do not add 'void' to the ArgTys list.
715 break;
716 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000717 } else if (!FTI.hasPrototype) {
718 if (ArgTy->isPromotableIntegerType()) {
719 ArgTy = Context.IntTy;
720 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
721 if (BTy->getKind() == BuiltinType::Float)
722 ArgTy = Context.DoubleTy;
723 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 }
725
726 ArgTys.push_back(ArgTy);
727 }
728 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000729 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 }
731 break;
732 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000733 case DeclaratorChunk::MemberPointer:
734 // The scope spec must refer to a class, or be dependent.
735 DeclContext *DC = static_cast<DeclContext*>(
736 DeclType.Mem.Scope().getScopeRep());
737 QualType ClsType;
738 // FIXME: Extend for dependent types when it's actually supported.
739 // See ActOnCXXNestedNameSpecifier.
740 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
741 ClsType = Context.getTagDeclType(RD);
742 } else {
743 if (DC) {
744 Diag(DeclType.Mem.Scope().getBeginLoc(),
745 diag::err_illegal_decl_mempointer_in_nonclass)
746 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
747 << DeclType.Mem.Scope().getRange();
748 }
749 D.setInvalidType(true);
750 ClsType = Context.IntTy;
751 }
752
753 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
754 // with reference type, or "cv void."
755 if (T->isReferenceType()) {
756 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
757 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
758 D.setInvalidType(true);
759 T = Context.IntTy;
760 }
761 if (T->isVoidType()) {
762 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
763 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
764 T = Context.IntTy;
765 }
766
767 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
768 // object or incomplete types shall not be restrict-qualified."
769 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
770 !T->isIncompleteOrObjectType()) {
771 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
772 << T;
773 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
774 }
775
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000776 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
777 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000778
779 break;
780 }
781
Douglas Gregorcd281c32009-02-28 00:25:32 +0000782 if (T.isNull()) {
783 D.setInvalidType(true);
784 T = Context.IntTy;
785 }
786
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000787 // See if there are any attributes on this declarator chunk.
788 if (const AttributeList *AL = DeclType.getAttrs())
789 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000791
792 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000793 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
794 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000795
796 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
797 // for a nonstatic member function, the function type to which a pointer
798 // to member refers, or the top-level function type of a function typedef
799 // declaration.
800 if (FnTy->getTypeQuals() != 0 &&
801 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000802 ((D.getContext() != Declarator::MemberContext &&
803 (!D.getCXXScopeSpec().isSet() ||
804 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000805 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000806 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000807 if (D.isFunctionDeclarator())
808 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
809 else
810 Diag(D.getIdentifierLoc(),
811 diag::err_invalid_qualified_typedef_function_type_use);
812
813 // Strip the cv-quals from the type.
814 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000815 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000816 }
817 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000818
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000819 // If there were any type attributes applied to the decl itself (not the
820 // type, apply the type attribute to the type!)
821 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000822 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 return T;
825}
826
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000828/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000829QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000830 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000831 QualType T = MDecl->getResultType();
832 llvm::SmallVector<QualType, 16> ArgTys;
833
Fariborz Jahanian35600022007-11-09 17:18:29 +0000834 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000835 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000837 selfTy = Context.getPointerType(selfTy);
838 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000839 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000840 ArgTys.push_back(Context.getObjCIdType());
841 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000842
Chris Lattner89951a82009-02-20 18:43:26 +0000843 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
844 E = MDecl->param_end(); PI != E; ++PI) {
845 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000846 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000847 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
848 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000849 // Sema::ActOnParamDeclarator().
850 if (ArgTy->isArrayType())
851 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000852 else if (ArgTy->isFunctionType())
853 ArgTy = Context.getPointerType(ArgTy);
854 ArgTys.push_back(ArgTy);
855 }
856 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000857 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000858 return T;
859}
860
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000861/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
862/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
863/// they point to and return true. If T1 and T2 aren't pointer types
864/// or pointer-to-member types, or if they are not similar at this
865/// level, returns false and leaves T1 and T2 unchanged. Top-level
866/// qualifiers on T1 and T2 are ignored. This function will typically
867/// be called in a loop that successively "unwraps" pointer and
868/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000869bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000870 const PointerType *T1PtrType = T1->getAsPointerType(),
871 *T2PtrType = T2->getAsPointerType();
872 if (T1PtrType && T2PtrType) {
873 T1 = T1PtrType->getPointeeType();
874 T2 = T2PtrType->getPointeeType();
875 return true;
876 }
877
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000878 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
879 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000880 if (T1MPType && T2MPType &&
881 Context.getCanonicalType(T1MPType->getClass()) ==
882 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000883 T1 = T1MPType->getPointeeType();
884 T2 = T2MPType->getPointeeType();
885 return true;
886 }
Douglas Gregor57373262008-10-22 14:17:15 +0000887 return false;
888}
889
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000890Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // C99 6.7.6: Type names have no identifier. This is already validated by
892 // the parser.
893 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
894
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000895 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000896 if (T.isNull())
897 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000898
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000899 // Check that there are no default arguments (C++ only).
900 if (getLangOptions().CPlusPlus)
901 CheckExtraCXXDefaultArguments(D);
902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 return T.getAsOpaquePtr();
904}
905
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000906
907
908//===----------------------------------------------------------------------===//
909// Type Attribute Processing
910//===----------------------------------------------------------------------===//
911
912/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
913/// specified type. The attribute contains 1 argument, the id of the address
914/// space for the type.
915static void HandleAddressSpaceTypeAttribute(QualType &Type,
916 const AttributeList &Attr, Sema &S){
917 // If this type is already address space qualified, reject it.
918 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
919 // for two or more different address spaces."
920 if (Type.getAddressSpace()) {
921 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
922 return;
923 }
924
925 // Check the attribute arguments.
926 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000928 return;
929 }
930 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
931 llvm::APSInt addrSpace(32);
932 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000933 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
934 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000935 return;
936 }
937
938 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000939 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000940}
941
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000942/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
943/// specified type. The attribute contains 1 argument, weak or strong.
944static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000945 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000946 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000947 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000948 return;
949 }
950
951 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000952 if (!Attr.getParameterName()) {
953 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
954 << "objc_gc" << 1;
955 return;
956 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000957 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000958 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
960 return;
961 }
962 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000963 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000964 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000965 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000966 else {
967 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
968 << "objc_gc" << Attr.getParameterName();
969 return;
970 }
971
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000972 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000973}
974
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000975void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000976 // Scan through and apply attributes to this type where it makes sense. Some
977 // attributes (such as __address_space__, __vector_size__, etc) apply to the
978 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000979 // apply to the decl. Here we apply type attributes and ignore the rest.
980 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000981 // If this is an attribute we can handle, do so now, otherwise, add it to
982 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000983 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000984 default: break;
985 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000986 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
987 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000988 case AttributeList::AT_objc_gc:
989 HandleObjCGCTypeAttribute(Result, *AL, *this);
990 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000991 }
Chris Lattner232e8822008-02-21 01:08:11 +0000992 }
Chris Lattner232e8822008-02-21 01:08:11 +0000993}
994
Douglas Gregor86447ec2009-03-09 16:13:40 +0000995/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000996///
997/// This routine checks whether the type @p T is complete in any
998/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +0000999/// type, returns false. If @p T is a class template specialization,
1000/// this routine then attempts to perform class template
1001/// instantiation. If instantiation fails, or if @p T is incomplete
1002/// and cannot be completed, issues the diagnostic @p diag (giving it
1003/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001004///
1005/// @param Loc The location in the source that the incomplete type
1006/// diagnostic should refer to.
1007///
1008/// @param T The type that this routine is examining for completeness.
1009///
1010/// @param diag The diagnostic value (e.g.,
1011/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1012/// for the error message if @p T is incomplete.
1013///
1014/// @param Range1 An optional range in the source code that will be a
1015/// part of the "incomplete type" error message.
1016///
1017/// @param Range2 An optional range in the source code that will be a
1018/// part of the "incomplete type" error message.
1019///
1020/// @param PrintType If non-NULL, the type that should be printed
1021/// instead of @p T. This parameter should be used when the type that
1022/// we're checking for incompleteness isn't the type that should be
1023/// displayed to the user, e.g., when T is a type and PrintType is a
1024/// pointer to T.
1025///
1026/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1027/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001028bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001029 SourceRange Range1, SourceRange Range2,
1030 QualType PrintType) {
1031 // If we have a complete type, we're done.
1032 if (!T->isIncompleteType())
1033 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001034
Douglas Gregor2943aed2009-03-03 04:44:36 +00001035 // If we have a class template specialization, try to instantiate
1036 // it.
1037 if (const RecordType *Record = T->getAsRecordType())
1038 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1039 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl()))
1040 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1041 // Update the class template specialization's location to
1042 // refer to the point of instantiation.
1043 if (Loc.isValid())
1044 ClassTemplateSpec->setLocation(Loc);
1045 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1046 /*ExplicitInstantiation=*/false);
1047 }
1048
1049
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001050 if (PrintType.isNull())
1051 PrintType = T;
1052
1053 // We have an incomplete type. Produce a diagnostic.
1054 Diag(Loc, diag) << PrintType << Range1 << Range2;
1055
1056 // If the type was a forward declaration of a class/struct/union
1057 // type, produce
1058 const TagType *Tag = 0;
1059 if (const RecordType *Record = T->getAsRecordType())
1060 Tag = Record;
1061 else if (const EnumType *Enum = T->getAsEnumType())
1062 Tag = Enum;
1063
1064 if (Tag && !Tag->getDecl()->isInvalidDecl())
1065 Diag(Tag->getDecl()->getLocation(),
1066 Tag->isBeingDefined() ? diag::note_type_being_defined
1067 : diag::note_forward_declaration)
1068 << QualType(Tag, 0);
1069
1070 return true;
1071}