blob: 24b32e8147c85303a470862a0f0876e28f84679f [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.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000322QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000323 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000324 if (LValueRef) {
325 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
326 // FIXME: Find the C++0x reference for reference collapsing.
327 // In reference collapsing, lvalue refs win over rvalue refs.
328 return Context.getLValueReferenceType(R->getPointeeType()).
329 getQualifiedType(Quals);
330 }
331 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000332 if (T->isReferenceType()) {
333 // C++ [dcl.ref]p4: There shall be no references to references.
334 //
335 // According to C++ DR 106, references to references are only
336 // diagnosed when they are written directly (e.g., "int & &"),
337 // but not when they happen via a typedef:
338 //
339 // typedef int& intref;
340 // typedef intref& intref2;
341 //
342 // Parser::ParserDeclaratorInternal diagnoses the case where
343 // references are written directly; here, we handle the
344 // collapsing of references-to-references as described in C++
345 // DR 106 and amended by C++ DR 540.
346 return T;
347 }
348
349 // C++ [dcl.ref]p1:
350 // A declarator that specifies the type “reference to cv void”
351 // is ill-formed.
352 if (T->isVoidType()) {
353 Diag(Loc, diag::err_reference_to_void);
354 return QualType();
355 }
356
357 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
358 // object or incomplete types shall not be restrict-qualified."
359 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
360 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
361 << T;
362 Quals &= ~QualType::Restrict;
363 }
364
365 // C++ [dcl.ref]p1:
366 // [...] Cv-qualified references are ill-formed except when the
367 // cv-qualifiers are introduced through the use of a typedef
368 // (7.1.3) or of a template type argument (14.3), in which case
369 // the cv-qualifiers are ignored.
370 //
371 // We diagnose extraneous cv-qualifiers for the non-typedef,
372 // non-template type argument case within the parser. Here, we just
373 // ignore any extraneous cv-qualifiers.
374 Quals &= ~QualType::Const;
375 Quals &= ~QualType::Volatile;
376
377 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000378 if (LValueRef)
379 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
380 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000381}
382
383/// \brief Build an array type.
384///
385/// \param T The type of each element in the array.
386///
387/// \param ASM C99 array size modifier (e.g., '*', 'static').
388///
389/// \param ArraySize Expression describing the size of the array.
390///
391/// \param Quals The cvr-qualifiers to be applied to the array's
392/// element type.
393///
394/// \param Loc The location of the entity whose type involves this
395/// array type or, if there is no such entity, the location of the
396/// type that will have array type.
397///
398/// \param Entity The name of the entity that involves the array
399/// type, if known.
400///
401/// \returns A suitable array type, if there are no errors. Otherwise,
402/// returns a NULL type.
403QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
404 Expr *ArraySize, unsigned Quals,
405 SourceLocation Loc, DeclarationName Entity) {
406 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
407 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000408 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000409 diag::err_illegal_decl_array_incomplete_type))
410 return QualType();
411
412 if (T->isFunctionType()) {
413 Diag(Loc, diag::err_illegal_decl_array_of_functions)
414 << getPrintableNameForEntity(Entity);
415 return QualType();
416 }
417
418 // C++ 8.3.2p4: There shall be no ... arrays of references ...
419 if (T->isReferenceType()) {
420 Diag(Loc, diag::err_illegal_decl_array_of_references)
421 << getPrintableNameForEntity(Entity);
422 return QualType();
423 }
424
425 if (const RecordType *EltTy = T->getAsRecordType()) {
426 // If the element type is a struct or union that contains a variadic
427 // array, accept it as a GNU extension: C99 6.7.2.1p2.
428 if (EltTy->getDecl()->hasFlexibleArrayMember())
429 Diag(Loc, diag::ext_flexible_array_in_array) << T;
430 } else if (T->isObjCInterfaceType()) {
431 Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
432 }
433
434 // C99 6.7.5.2p1: The size expression shall have integer type.
435 if (ArraySize && !ArraySize->isTypeDependent() &&
436 !ArraySize->getType()->isIntegerType()) {
437 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
438 << ArraySize->getType() << ArraySize->getSourceRange();
439 ArraySize->Destroy(Context);
440 return QualType();
441 }
442 llvm::APSInt ConstVal(32);
443 if (!ArraySize) {
444 T = Context.getIncompleteArrayType(T, ASM, Quals);
445 } else if (ArraySize->isValueDependent()) {
446 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
447 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
448 (!T->isDependentType() && !T->isConstantSizeType())) {
449 // Per C99, a variable array is an array with either a non-constant
450 // size or an element type that has a non-constant-size
451 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
452 } else {
453 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
454 // have a value greater than zero.
455 if (ConstVal.isSigned()) {
456 if (ConstVal.isNegative()) {
457 Diag(ArraySize->getLocStart(),
458 diag::err_typecheck_negative_array_size)
459 << ArraySize->getSourceRange();
460 return QualType();
461 } else if (ConstVal == 0) {
462 // GCC accepts zero sized static arrays.
463 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
464 << ArraySize->getSourceRange();
465 }
466 }
467 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
468 }
469 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
470 if (!getLangOptions().C99) {
471 if (ArraySize && !ArraySize->isTypeDependent() &&
472 !ArraySize->isValueDependent() &&
473 !ArraySize->isIntegerConstantExpr(Context))
474 Diag(Loc, diag::ext_vla);
475 else if (ASM != ArrayType::Normal || Quals != 0)
476 Diag(Loc, diag::ext_c99_array_usage);
477 }
478
479 return T;
480}
481
Douglas Gregor724651c2009-02-28 01:04:19 +0000482/// \brief Build a function type.
483///
484/// This routine checks the function type according to C++ rules and
485/// under the assumption that the result type and parameter types have
486/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000487/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000488/// simpler form that is only suitable for this narrow use case.
489///
490/// \param T The return type of the function.
491///
492/// \param ParamTypes The parameter types of the function. This array
493/// will be modified to account for adjustments to the types of the
494/// function parameters.
495///
496/// \param NumParamTypes The number of parameter types in ParamTypes.
497///
498/// \param Variadic Whether this is a variadic function type.
499///
500/// \param Quals The cvr-qualifiers to be applied to the function type.
501///
502/// \param Loc The location of the entity whose type involves this
503/// function type or, if there is no such entity, the location of the
504/// type that will have function type.
505///
506/// \param Entity The name of the entity that involves the function
507/// type, if known.
508///
509/// \returns A suitable function type, if there are no
510/// errors. Otherwise, returns a NULL type.
511QualType Sema::BuildFunctionType(QualType T,
512 QualType *ParamTypes,
513 unsigned NumParamTypes,
514 bool Variadic, unsigned Quals,
515 SourceLocation Loc, DeclarationName Entity) {
516 if (T->isArrayType() || T->isFunctionType()) {
517 Diag(Loc, diag::err_func_returning_array_function) << T;
518 return QualType();
519 }
520
521 bool Invalid = false;
522 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
523 QualType ParamType = ParamTypes[Idx];
524 if (ParamType->isArrayType())
525 ParamType = Context.getArrayDecayedType(ParamType);
526 else if (ParamType->isFunctionType())
527 ParamType = Context.getPointerType(ParamType);
528 else if (ParamType->isVoidType()) {
529 Diag(Loc, diag::err_param_with_void_type);
530 Invalid = true;
531 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000532
Douglas Gregor724651c2009-02-28 01:04:19 +0000533 ParamTypes[Idx] = ParamType;
534 }
535
536 if (Invalid)
537 return QualType();
538
539 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
540 Quals);
541}
542
Mike Stump98eb8a72009-02-04 22:31:32 +0000543/// GetTypeForDeclarator - Convert the type for the specified
544/// declarator to Type instances. Skip the outermost Skip type
545/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000546QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000547 bool OmittedReturnType = false;
548
549 if (D.getContext() == Declarator::BlockLiteralContext
550 && Skip == 0
551 && !D.getDeclSpec().hasTypeSpecifier()
552 && (D.getNumTypeObjects() == 0
553 || (D.getNumTypeObjects() == 1
554 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
555 OmittedReturnType = true;
556
Chris Lattnerb23deda2007-08-28 16:40:32 +0000557 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000558 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000559 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
560 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000561
562 // Determine the type of the declarator. Not all forms of declarator
563 // have a type.
564 QualType T;
565 switch (D.getKind()) {
566 case Declarator::DK_Abstract:
567 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000568 case Declarator::DK_Operator: {
569 const DeclSpec& DS = D.getDeclSpec();
570 if (OmittedReturnType)
571 // We default to a dependent type initially. Can be modified by
572 // the first return statement.
573 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000574 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000575 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000576 if (T.isNull())
577 return T;
578 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000579 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000580 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000581
582 case Declarator::DK_Constructor:
583 case Declarator::DK_Destructor:
584 case Declarator::DK_Conversion:
585 // Constructors and destructors don't have return types. Use
586 // "void" instead. Conversion operators will check their return
587 // types separately.
588 T = Context.VoidTy;
589 break;
590 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000591
Douglas Gregorcd281c32009-02-28 00:25:32 +0000592 // The name we're declaring, if any.
593 DeclarationName Name;
594 if (D.getIdentifier())
595 Name = D.getIdentifier();
596
Mike Stump98eb8a72009-02-04 22:31:32 +0000597 // Walk the DeclTypeInfo, building the recursive type as we go.
598 // DeclTypeInfos are ordered from the identifier out, which is
599 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000600 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
601 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 switch (DeclType.Kind) {
603 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000604 case DeclaratorChunk::BlockPointer:
605 if (DeclType.Cls.TypeQuals)
606 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
607 if (!T.getTypePtr()->isFunctionType())
608 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
609 else
610 T = Context.getBlockPointerType(T);
611 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000613 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000615 case DeclaratorChunk::Reference:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000616 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
617 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000618 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 break;
620 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000621 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000622 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 ArrayType::ArraySizeModifier ASM;
624 if (ATI.isStar)
625 ASM = ArrayType::Star;
626 else if (ATI.hasStatic)
627 ASM = ArrayType::Static;
628 else
629 ASM = ArrayType::Normal;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 break;
632 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000633 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // If the function declarator has a prototype (i.e. it is not () and
635 // does not have a K&R-style identifier list), then the arguments are part
636 // of the type, otherwise the argument list is ().
637 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000638
Chris Lattnercd881292007-12-19 05:31:29 +0000639 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000640 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000641 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000642 T = Context.IntTy;
643 D.setInvalidType(true);
644 }
645
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000646 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000647 if (getLangOptions().CPlusPlus) {
648 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
649 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000650 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000651 } else if (FTI.isVariadic) {
652 // We allow a zero-parameter variadic function in C if the
653 // function is marked with the "overloadable"
654 // attribute. Scan for this attribute now.
655 bool Overloadable = false;
656 for (const AttributeList *Attrs = D.getAttributes();
657 Attrs; Attrs = Attrs->getNext()) {
658 if (Attrs->getKind() == AttributeList::AT_overloadable) {
659 Overloadable = true;
660 break;
661 }
662 }
663
664 if (!Overloadable)
665 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
666 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000667 } else {
668 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000669 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000670 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000671 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000673 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 } else {
675 // Otherwise, we have a function with an argument list that is
676 // potentially variadic.
677 llvm::SmallVector<QualType, 16> ArgTys;
678
679 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000680 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
681 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000682 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000683 //
684 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
685 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000686 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000687 // argument type in the function prototype *will not* match the
688 // type in ParmVarDecl (which makes the code generator unhappy).
689 //
690 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000691 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000692 // it should be driving off the type being created here.
693 //
694 // FIXME: If a source translation tool needs to see the original type,
695 // then we need to consider storing both types somewhere...
696 //
Chris Lattnere6327742008-04-02 05:18:44 +0000697 if (ArgTy->isArrayType()) {
698 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000699 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000700 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Look for 'void'. void is allowed only as a single argument to a
703 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000704 // int(void) as a FunctionProtoType with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000705 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // If this is something like 'float(int, void)', reject it. 'void'
707 // is an incomplete type (C99 6.2.5p19) and function decls cannot
708 // have arguments of incomplete type.
709 if (FTI.NumArgs != 1 || FTI.isVariadic) {
710 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000711 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000712 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000713 } else if (FTI.ArgInfo[i].Ident) {
714 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000716 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000717 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000718 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000719 } else {
720 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000721 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000722 Diag(DeclType.Loc, diag::err_void_param_qualified);
723
724 // Do not add 'void' to the ArgTys list.
725 break;
726 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000727 } else if (!FTI.hasPrototype) {
728 if (ArgTy->isPromotableIntegerType()) {
729 ArgTy = Context.IntTy;
730 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
731 if (BTy->getKind() == BuiltinType::Float)
732 ArgTy = Context.DoubleTy;
733 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 }
735
736 ArgTys.push_back(ArgTy);
737 }
738 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000739 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 }
741 break;
742 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000743 case DeclaratorChunk::MemberPointer:
744 // The scope spec must refer to a class, or be dependent.
Douglas Gregore4e5b052009-03-19 00:18:19 +0000745 DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000746 QualType ClsType;
747 // FIXME: Extend for dependent types when it's actually supported.
748 // See ActOnCXXNestedNameSpecifier.
749 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
750 ClsType = Context.getTagDeclType(RD);
751 } else {
752 if (DC) {
753 Diag(DeclType.Mem.Scope().getBeginLoc(),
754 diag::err_illegal_decl_mempointer_in_nonclass)
755 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
756 << DeclType.Mem.Scope().getRange();
757 }
758 D.setInvalidType(true);
759 ClsType = Context.IntTy;
760 }
761
762 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
763 // with reference type, or "cv void."
764 if (T->isReferenceType()) {
765 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
766 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
767 D.setInvalidType(true);
768 T = Context.IntTy;
769 }
770 if (T->isVoidType()) {
771 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
772 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
773 T = Context.IntTy;
774 }
775
776 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
777 // object or incomplete types shall not be restrict-qualified."
778 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
779 !T->isIncompleteOrObjectType()) {
780 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
781 << T;
782 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
783 }
784
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000785 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
786 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000787
788 break;
789 }
790
Douglas Gregorcd281c32009-02-28 00:25:32 +0000791 if (T.isNull()) {
792 D.setInvalidType(true);
793 T = Context.IntTy;
794 }
795
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000796 // See if there are any attributes on this declarator chunk.
797 if (const AttributeList *AL = DeclType.getAttrs())
798 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000800
801 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000802 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
803 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000804
805 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
806 // for a nonstatic member function, the function type to which a pointer
807 // to member refers, or the top-level function type of a function typedef
808 // declaration.
809 if (FnTy->getTypeQuals() != 0 &&
810 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000811 ((D.getContext() != Declarator::MemberContext &&
812 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +0000813 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000814 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000815 if (D.isFunctionDeclarator())
816 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
817 else
818 Diag(D.getIdentifierLoc(),
819 diag::err_invalid_qualified_typedef_function_type_use);
820
821 // Strip the cv-quals from the type.
822 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000823 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000824 }
825 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000826
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000827 // If there were any type attributes applied to the decl itself (not the
828 // type, apply the type attribute to the type!)
829 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000830 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 return T;
833}
834
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000836/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000837QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000838 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000839 QualType T = MDecl->getResultType();
840 llvm::SmallVector<QualType, 16> ArgTys;
841
Fariborz Jahanian35600022007-11-09 17:18:29 +0000842 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000843 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000844 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000845 selfTy = Context.getPointerType(selfTy);
846 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000847 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 ArgTys.push_back(Context.getObjCIdType());
849 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000850
Chris Lattner89951a82009-02-20 18:43:26 +0000851 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
852 E = MDecl->param_end(); PI != E; ++PI) {
853 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000854 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000855 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
856 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000857 // Sema::ActOnParamDeclarator().
858 if (ArgTy->isArrayType())
859 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000860 else if (ArgTy->isFunctionType())
861 ArgTy = Context.getPointerType(ArgTy);
862 ArgTys.push_back(ArgTy);
863 }
864 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000865 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000866 return T;
867}
868
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000869/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
870/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
871/// they point to and return true. If T1 and T2 aren't pointer types
872/// or pointer-to-member types, or if they are not similar at this
873/// level, returns false and leaves T1 and T2 unchanged. Top-level
874/// qualifiers on T1 and T2 are ignored. This function will typically
875/// be called in a loop that successively "unwraps" pointer and
876/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000877bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000878 const PointerType *T1PtrType = T1->getAsPointerType(),
879 *T2PtrType = T2->getAsPointerType();
880 if (T1PtrType && T2PtrType) {
881 T1 = T1PtrType->getPointeeType();
882 T2 = T2PtrType->getPointeeType();
883 return true;
884 }
885
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000886 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
887 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000888 if (T1MPType && T2MPType &&
889 Context.getCanonicalType(T1MPType->getClass()) ==
890 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000891 T1 = T1MPType->getPointeeType();
892 T2 = T2MPType->getPointeeType();
893 return true;
894 }
Douglas Gregor57373262008-10-22 14:17:15 +0000895 return false;
896}
897
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000898Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 // C99 6.7.6: Type names have no identifier. This is already validated by
900 // the parser.
901 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
902
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000903 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000904 if (T.isNull())
905 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000906
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000907 // Check that there are no default arguments (C++ only).
908 if (getLangOptions().CPlusPlus)
909 CheckExtraCXXDefaultArguments(D);
910
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 return T.getAsOpaquePtr();
912}
913
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000914
915
916//===----------------------------------------------------------------------===//
917// Type Attribute Processing
918//===----------------------------------------------------------------------===//
919
920/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
921/// specified type. The attribute contains 1 argument, the id of the address
922/// space for the type.
923static void HandleAddressSpaceTypeAttribute(QualType &Type,
924 const AttributeList &Attr, Sema &S){
925 // If this type is already address space qualified, reject it.
926 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
927 // for two or more different address spaces."
928 if (Type.getAddressSpace()) {
929 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
930 return;
931 }
932
933 // Check the attribute arguments.
934 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000936 return;
937 }
938 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
939 llvm::APSInt addrSpace(32);
940 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
942 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000943 return;
944 }
945
946 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000947 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000948}
949
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000950/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
951/// specified type. The attribute contains 1 argument, weak or strong.
952static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000953 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000954 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000955 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000956 return;
957 }
958
959 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000960 if (!Attr.getParameterName()) {
961 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
962 << "objc_gc" << 1;
963 return;
964 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000965 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000966 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000967 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
968 return;
969 }
970 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000971 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000972 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000973 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000974 else {
975 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
976 << "objc_gc" << Attr.getParameterName();
977 return;
978 }
979
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000980 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000981}
982
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000983void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000984 // Scan through and apply attributes to this type where it makes sense. Some
985 // attributes (such as __address_space__, __vector_size__, etc) apply to the
986 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000987 // apply to the decl. Here we apply type attributes and ignore the rest.
988 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000989 // If this is an attribute we can handle, do so now, otherwise, add it to
990 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000991 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000992 default: break;
993 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000994 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
995 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000996 case AttributeList::AT_objc_gc:
997 HandleObjCGCTypeAttribute(Result, *AL, *this);
998 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000999 }
Chris Lattner232e8822008-02-21 01:08:11 +00001000 }
Chris Lattner232e8822008-02-21 01:08:11 +00001001}
1002
Douglas Gregor86447ec2009-03-09 16:13:40 +00001003/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001004///
1005/// This routine checks whether the type @p T is complete in any
1006/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001007/// type, returns false. If @p T is a class template specialization,
1008/// this routine then attempts to perform class template
1009/// instantiation. If instantiation fails, or if @p T is incomplete
1010/// and cannot be completed, issues the diagnostic @p diag (giving it
1011/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001012///
1013/// @param Loc The location in the source that the incomplete type
1014/// diagnostic should refer to.
1015///
1016/// @param T The type that this routine is examining for completeness.
1017///
1018/// @param diag The diagnostic value (e.g.,
1019/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1020/// for the error message if @p T is incomplete.
1021///
1022/// @param Range1 An optional range in the source code that will be a
1023/// part of the "incomplete type" error message.
1024///
1025/// @param Range2 An optional range in the source code that will be a
1026/// part of the "incomplete type" error message.
1027///
1028/// @param PrintType If non-NULL, the type that should be printed
1029/// instead of @p T. This parameter should be used when the type that
1030/// we're checking for incompleteness isn't the type that should be
1031/// displayed to the user, e.g., when T is a type and PrintType is a
1032/// pointer to T.
1033///
1034/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1035/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001036bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001037 SourceRange Range1, SourceRange Range2,
1038 QualType PrintType) {
1039 // If we have a complete type, we're done.
1040 if (!T->isIncompleteType())
1041 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001042
Douglas Gregor2943aed2009-03-03 04:44:36 +00001043 // If we have a class template specialization, try to instantiate
1044 // it.
1045 if (const RecordType *Record = T->getAsRecordType())
1046 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
1047 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl()))
1048 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1049 // Update the class template specialization's location to
1050 // refer to the point of instantiation.
1051 if (Loc.isValid())
1052 ClassTemplateSpec->setLocation(Loc);
1053 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1054 /*ExplicitInstantiation=*/false);
1055 }
1056
1057
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001058 if (PrintType.isNull())
1059 PrintType = T;
1060
1061 // We have an incomplete type. Produce a diagnostic.
1062 Diag(Loc, diag) << PrintType << Range1 << Range2;
1063
1064 // If the type was a forward declaration of a class/struct/union
1065 // type, produce
1066 const TagType *Tag = 0;
1067 if (const RecordType *Record = T->getAsRecordType())
1068 Tag = Record;
1069 else if (const EnumType *Enum = T->getAsEnumType())
1070 Tag = Enum;
1071
1072 if (Tag && !Tag->getDecl()->isInvalidDecl())
1073 Diag(Tag->getDecl()->getLocation(),
1074 Tag->isBeingDefined() ? diag::note_type_being_defined
1075 : diag::note_forward_declaration)
1076 << QualType(Tag, 0);
1077
1078 return true;
1079}
Douglas Gregore6258932009-03-19 00:39:20 +00001080
1081/// \brief Retrieve a version of the type 'T' that is qualified by the
1082/// nested-name-specifier contained in SS.
1083QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1084 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1085 return T;
1086
1087 llvm::SmallVector<NestedNameSpecifier, 4> Specs;
1088 for (CXXScopeSpec::iterator Spec = SS.begin(), SpecEnd = SS.end();
1089 Spec != SpecEnd; ++Spec)
1090 Specs.push_back(NestedNameSpecifier::getFromOpaquePtr(*Spec));
1091 return Context.getQualifiedNameType(&Specs[0], Specs.size(), T);
1092}