blob: f14eb6520324a556df39a989728ed7cc0190597d [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 Gregor2dc0e642009-03-23 23:06:20 +000022/// \brief Perform adjustment on the parameter type of a function.
23///
24/// This routine adjusts the given parameter type @p T to the actual
25/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
26/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
27QualType Sema::adjustParameterType(QualType T) {
28 // C99 6.7.5.3p7:
29 if (T->isArrayType()) {
30 // C99 6.7.5.3p7:
31 // A declaration of a parameter as "array of type" shall be
32 // adjusted to "qualified pointer to type", where the type
33 // qualifiers (if any) are those specified within the [ and ] of
34 // the array type derivation.
35 return Context.getArrayDecayedType(T);
36 } else if (T->isFunctionType())
37 // C99 6.7.5.3p8:
38 // A declaration of a parameter as "function returning type"
39 // shall be adjusted to "pointer to function returning type", as
40 // in 6.3.2.1.
41 return Context.getPointerType(T);
42
43 return T;
44}
45
Douglas Gregor930d8b52009-01-30 22:09:00 +000046/// \brief Convert the specified declspec to the appropriate type
47/// object.
48/// \param DS the declaration specifiers
49/// \returns The type described by the declaration specifiers, or NULL
50/// if there was an error.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +000051QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
53 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000054 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000055
56 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000057 case DeclSpec::TST_void:
58 Result = Context.VoidTy;
59 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 case DeclSpec::TST_char:
61 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000062 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000064 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 else {
66 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
67 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000068 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
Chris Lattner958858e2008-02-20 21:40:32 +000070 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000071 case DeclSpec::TST_wchar:
72 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
73 Result = Context.WCharTy;
74 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000075 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
76 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000077 Result = Context.getSignedWCharType();
78 } else {
79 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
80 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000081 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
82 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000083 Result = Context.getUnsignedWCharType();
84 }
85 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000086 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000087 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000088 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000089 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000090 DS.getNumProtocolQualifiers());
91 break;
92 }
93
Chris Lattnerd658b562008-04-05 06:32:51 +000094 // Unspecified typespec defaults to int in C90. However, the C90 grammar
95 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
96 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
97 // Note that the one exception to this is function definitions, which are
98 // allowed to be completely missing a declspec. This is handled in the
99 // parser already though by it pretending to have seen an 'int' in this
100 // case.
101 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000102 // In C89 mode, we only warn if there is a completely missing declspec
103 // when one is not allowed.
104 if (DS.isEmpty())
Chris Lattner173144a2009-02-27 22:31:56 +0000105 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
106 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
107 "int");
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000108 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000109 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
110 // "At least one type specifier shall be given in the declaration
111 // specifiers in each declaration, and in the specifier-qualifier list in
112 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000113 // FIXME: Does Microsoft really have the implicit int extension in C++?
114 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
115 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +0000116 : diag::warn_missing_type_specifier;
Chris Lattner173144a2009-02-27 22:31:56 +0000117 Diag(DS.getSourceRange().getBegin(), DK)
118 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
119 "int");
Chris Lattnerd658b562008-04-05 06:32:51 +0000120 }
121
122 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000123 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
125 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000126 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
127 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
128 case DeclSpec::TSW_long: Result = Context.LongTy; break;
129 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
131 } else {
132 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000133 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
134 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
135 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
136 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 }
138 }
Chris Lattner958858e2008-02-20 21:40:32 +0000139 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000140 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000141 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000142 case DeclSpec::TST_double:
143 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000144 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000145 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000146 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000147 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000148 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 case DeclSpec::TST_decimal32: // _Decimal32
150 case DeclSpec::TST_decimal64: // _Decimal64
151 case DeclSpec::TST_decimal128: // _Decimal128
152 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000153 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 case DeclSpec::TST_enum:
155 case DeclSpec::TST_union:
156 case DeclSpec::TST_struct: {
157 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000158 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
160 DS.getTypeSpecSign() == 0 &&
161 "Can't handle qualifiers on typedef names yet!");
162 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000163 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000164 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000166 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
168 DS.getTypeSpecSign() == 0 &&
169 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000170 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000171
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000172 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
173 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
174 // we have this "hack" for now...
175 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
176 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
177 (ObjCProtocolDecl**)PQ,
178 DS.getNumProtocolQualifiers());
179 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000180 // id<protocol-list>
181 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
182 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000183 else if (Result == Context.getObjCClassType())
184 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000185 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000186 diag::err_qualified_class_unsupported) << DS.getSourceRange();
187 else
188 Diag(DS.getSourceRange().getBegin(),
189 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000190 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000192 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 }
Chris Lattner958858e2008-02-20 21:40:32 +0000194 case DeclSpec::TST_typeofType:
195 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
196 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000197 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000198 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000199 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000200 case DeclSpec::TST_typeofExpr: {
201 Expr *E = static_cast<Expr *>(DS.getTypeRep());
202 assert(E && "Didn't get an expression for typeof?");
203 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000204 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000205 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000206 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000207 case DeclSpec::TST_error:
208 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
Chris Lattner958858e2008-02-20 21:40:32 +0000210
211 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000212 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
213 if (getLangOptions().Freestanding)
214 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000215 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000216 }
Chris Lattner958858e2008-02-20 21:40:32 +0000217
218 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
219 "FIXME: imaginary types not supported yet!");
220
Chris Lattner38d8b982008-02-20 22:04:11 +0000221 // See if there are any attributes on the declspec that apply to the type (as
222 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000223 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000224 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000225
Chris Lattner96b77fc2008-04-02 06:50:17 +0000226 // Apply const/volatile/restrict qualifiers to T.
227 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
228
229 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
230 // or incomplete types shall not be restrict-qualified." C++ also allows
231 // restrict-qualified references.
232 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000233 if (Result->isPointerType() || Result->isReferenceType()) {
234 QualType EltTy = Result->isPointerType() ?
235 Result->getAsPointerType()->getPointeeType() :
236 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000237
Douglas Gregorbad0e652009-03-24 20:32:41 +0000238 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000239 // incomplete type.
240 if (!EltTy->isIncompleteOrObjectType()) {
241 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000242 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000243 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000244 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
245 }
246 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000247 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000248 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000249 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000250 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000251 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000252 }
253
254 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
255 // of a function type includes any type qualifiers, the behavior is
256 // undefined."
257 if (Result->isFunctionType() && TypeQuals) {
258 // Get some location to point at, either the C or V location.
259 SourceLocation Loc;
260 if (TypeQuals & QualType::Const)
261 Loc = DS.getConstSpecLoc();
262 else {
263 assert((TypeQuals & QualType::Volatile) &&
264 "Has CV quals but not C or V?");
265 Loc = DS.getVolatileSpecLoc();
266 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000267 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000268 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000269 }
270
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000271 // C++ [dcl.ref]p1:
272 // Cv-qualified references are ill-formed except when the
273 // cv-qualifiers are introduced through the use of a typedef
274 // (7.1.3) or of a template type argument (14.3), in which
275 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000276 // FIXME: Shouldn't we be checking SCS_typedef here?
277 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000278 TypeQuals && Result->isReferenceType()) {
279 TypeQuals &= ~QualType::Const;
280 TypeQuals &= ~QualType::Volatile;
281 }
282
Chris Lattner96b77fc2008-04-02 06:50:17 +0000283 Result = Result.getQualifiedType(TypeQuals);
284 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000285 return Result;
286}
287
Douglas Gregorcd281c32009-02-28 00:25:32 +0000288static std::string getPrintableNameForEntity(DeclarationName Entity) {
289 if (Entity)
290 return Entity.getAsString();
291
292 return "type name";
293}
294
295/// \brief Build a pointer type.
296///
297/// \param T The type to which we'll be building a pointer.
298///
299/// \param Quals The cvr-qualifiers to be applied to the pointer type.
300///
301/// \param Loc The location of the entity whose type involves this
302/// pointer type or, if there is no such entity, the location of the
303/// type that will have pointer type.
304///
305/// \param Entity The name of the entity that involves the pointer
306/// type, if known.
307///
308/// \returns A suitable pointer type, if there are no
309/// errors. Otherwise, returns a NULL type.
310QualType Sema::BuildPointerType(QualType T, unsigned Quals,
311 SourceLocation Loc, DeclarationName Entity) {
312 if (T->isReferenceType()) {
313 // C++ 8.3.2p4: There shall be no ... pointers to references ...
314 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
315 << getPrintableNameForEntity(Entity);
316 return QualType();
317 }
318
319 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
320 // object or incomplete types shall not be restrict-qualified."
321 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
322 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
323 << T;
324 Quals &= ~QualType::Restrict;
325 }
326
327 // Build the pointer type.
328 return Context.getPointerType(T).getQualifiedType(Quals);
329}
330
331/// \brief Build a reference type.
332///
333/// \param T The type to which we'll be building a reference.
334///
335/// \param Quals The cvr-qualifiers to be applied to the reference type.
336///
337/// \param Loc The location of the entity whose type involves this
338/// reference type or, if there is no such entity, the location of the
339/// type that will have reference type.
340///
341/// \param Entity The name of the entity that involves the reference
342/// type, if known.
343///
344/// \returns A suitable reference type, if there are no
345/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000346QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000347 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000348 if (LValueRef) {
349 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000350 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
351 // reference to a type T, and attempt to create the type "lvalue
352 // reference to cv TD" creates the type "lvalue reference to T".
353 // We use the qualifiers (restrict or none) of the original reference,
354 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000355 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000356 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000357 }
358 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000359 if (T->isReferenceType()) {
360 // C++ [dcl.ref]p4: There shall be no references to references.
361 //
362 // According to C++ DR 106, references to references are only
363 // diagnosed when they are written directly (e.g., "int & &"),
364 // but not when they happen via a typedef:
365 //
366 // typedef int& intref;
367 // typedef intref& intref2;
368 //
369 // Parser::ParserDeclaratorInternal diagnoses the case where
370 // references are written directly; here, we handle the
371 // collapsing of references-to-references as described in C++
372 // DR 106 and amended by C++ DR 540.
373 return T;
374 }
375
376 // C++ [dcl.ref]p1:
377 // A declarator that specifies the type “reference to cv void”
378 // is ill-formed.
379 if (T->isVoidType()) {
380 Diag(Loc, diag::err_reference_to_void);
381 return QualType();
382 }
383
384 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
385 // object or incomplete types shall not be restrict-qualified."
386 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
387 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
388 << T;
389 Quals &= ~QualType::Restrict;
390 }
391
392 // C++ [dcl.ref]p1:
393 // [...] Cv-qualified references are ill-formed except when the
394 // cv-qualifiers are introduced through the use of a typedef
395 // (7.1.3) or of a template type argument (14.3), in which case
396 // the cv-qualifiers are ignored.
397 //
398 // We diagnose extraneous cv-qualifiers for the non-typedef,
399 // non-template type argument case within the parser. Here, we just
400 // ignore any extraneous cv-qualifiers.
401 Quals &= ~QualType::Const;
402 Quals &= ~QualType::Volatile;
403
404 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000405 if (LValueRef)
406 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
407 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000408}
409
410/// \brief Build an array type.
411///
412/// \param T The type of each element in the array.
413///
414/// \param ASM C99 array size modifier (e.g., '*', 'static').
415///
416/// \param ArraySize Expression describing the size of the array.
417///
418/// \param Quals The cvr-qualifiers to be applied to the array's
419/// element type.
420///
421/// \param Loc The location of the entity whose type involves this
422/// array type or, if there is no such entity, the location of the
423/// type that will have array type.
424///
425/// \param Entity The name of the entity that involves the array
426/// type, if known.
427///
428/// \returns A suitable array type, if there are no errors. Otherwise,
429/// returns a NULL type.
430QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
431 Expr *ArraySize, unsigned Quals,
432 SourceLocation Loc, DeclarationName Entity) {
433 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
434 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000435 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000436 diag::err_illegal_decl_array_incomplete_type))
437 return QualType();
438
439 if (T->isFunctionType()) {
440 Diag(Loc, diag::err_illegal_decl_array_of_functions)
441 << getPrintableNameForEntity(Entity);
442 return QualType();
443 }
444
445 // C++ 8.3.2p4: There shall be no ... arrays of references ...
446 if (T->isReferenceType()) {
447 Diag(Loc, diag::err_illegal_decl_array_of_references)
448 << getPrintableNameForEntity(Entity);
449 return QualType();
450 }
451
452 if (const RecordType *EltTy = T->getAsRecordType()) {
453 // If the element type is a struct or union that contains a variadic
454 // array, accept it as a GNU extension: C99 6.7.2.1p2.
455 if (EltTy->getDecl()->hasFlexibleArrayMember())
456 Diag(Loc, diag::ext_flexible_array_in_array) << T;
457 } else if (T->isObjCInterfaceType()) {
458 Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
459 }
460
461 // C99 6.7.5.2p1: The size expression shall have integer type.
462 if (ArraySize && !ArraySize->isTypeDependent() &&
463 !ArraySize->getType()->isIntegerType()) {
464 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
465 << ArraySize->getType() << ArraySize->getSourceRange();
466 ArraySize->Destroy(Context);
467 return QualType();
468 }
469 llvm::APSInt ConstVal(32);
470 if (!ArraySize) {
471 T = Context.getIncompleteArrayType(T, ASM, Quals);
472 } else if (ArraySize->isValueDependent()) {
473 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
474 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
475 (!T->isDependentType() && !T->isConstantSizeType())) {
476 // Per C99, a variable array is an array with either a non-constant
477 // size or an element type that has a non-constant-size
478 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
479 } else {
480 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
481 // have a value greater than zero.
482 if (ConstVal.isSigned()) {
483 if (ConstVal.isNegative()) {
484 Diag(ArraySize->getLocStart(),
485 diag::err_typecheck_negative_array_size)
486 << ArraySize->getSourceRange();
487 return QualType();
488 } else if (ConstVal == 0) {
489 // GCC accepts zero sized static arrays.
490 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
491 << ArraySize->getSourceRange();
492 }
493 }
494 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
495 }
496 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
497 if (!getLangOptions().C99) {
498 if (ArraySize && !ArraySize->isTypeDependent() &&
499 !ArraySize->isValueDependent() &&
500 !ArraySize->isIntegerConstantExpr(Context))
501 Diag(Loc, diag::ext_vla);
502 else if (ASM != ArrayType::Normal || Quals != 0)
503 Diag(Loc, diag::ext_c99_array_usage);
504 }
505
506 return T;
507}
508
Douglas Gregor724651c2009-02-28 01:04:19 +0000509/// \brief Build a function type.
510///
511/// This routine checks the function type according to C++ rules and
512/// under the assumption that the result type and parameter types have
513/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000514/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000515/// simpler form that is only suitable for this narrow use case.
516///
517/// \param T The return type of the function.
518///
519/// \param ParamTypes The parameter types of the function. This array
520/// will be modified to account for adjustments to the types of the
521/// function parameters.
522///
523/// \param NumParamTypes The number of parameter types in ParamTypes.
524///
525/// \param Variadic Whether this is a variadic function type.
526///
527/// \param Quals The cvr-qualifiers to be applied to the function type.
528///
529/// \param Loc The location of the entity whose type involves this
530/// function type or, if there is no such entity, the location of the
531/// type that will have function type.
532///
533/// \param Entity The name of the entity that involves the function
534/// type, if known.
535///
536/// \returns A suitable function type, if there are no
537/// errors. Otherwise, returns a NULL type.
538QualType Sema::BuildFunctionType(QualType T,
539 QualType *ParamTypes,
540 unsigned NumParamTypes,
541 bool Variadic, unsigned Quals,
542 SourceLocation Loc, DeclarationName Entity) {
543 if (T->isArrayType() || T->isFunctionType()) {
544 Diag(Loc, diag::err_func_returning_array_function) << T;
545 return QualType();
546 }
547
548 bool Invalid = false;
549 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000550 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
551 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000552 Diag(Loc, diag::err_param_with_void_type);
553 Invalid = true;
554 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000555
Douglas Gregor724651c2009-02-28 01:04:19 +0000556 ParamTypes[Idx] = ParamType;
557 }
558
559 if (Invalid)
560 return QualType();
561
562 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
563 Quals);
564}
565
Mike Stump98eb8a72009-02-04 22:31:32 +0000566/// GetTypeForDeclarator - Convert the type for the specified
567/// declarator to Type instances. Skip the outermost Skip type
568/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000569QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000570 bool OmittedReturnType = false;
571
572 if (D.getContext() == Declarator::BlockLiteralContext
573 && Skip == 0
574 && !D.getDeclSpec().hasTypeSpecifier()
575 && (D.getNumTypeObjects() == 0
576 || (D.getNumTypeObjects() == 1
577 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
578 OmittedReturnType = true;
579
Chris Lattnerb23deda2007-08-28 16:40:32 +0000580 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000581 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000582 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
583 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000584
585 // Determine the type of the declarator. Not all forms of declarator
586 // have a type.
587 QualType T;
588 switch (D.getKind()) {
589 case Declarator::DK_Abstract:
590 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000591 case Declarator::DK_Operator: {
592 const DeclSpec& DS = D.getDeclSpec();
593 if (OmittedReturnType)
594 // We default to a dependent type initially. Can be modified by
595 // the first return statement.
596 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000597 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000598 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000599 if (T.isNull())
600 return T;
601 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000602 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000603 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000604
605 case Declarator::DK_Constructor:
606 case Declarator::DK_Destructor:
607 case Declarator::DK_Conversion:
608 // Constructors and destructors don't have return types. Use
609 // "void" instead. Conversion operators will check their return
610 // types separately.
611 T = Context.VoidTy;
612 break;
613 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000614
Douglas Gregorcd281c32009-02-28 00:25:32 +0000615 // The name we're declaring, if any.
616 DeclarationName Name;
617 if (D.getIdentifier())
618 Name = D.getIdentifier();
619
Mike Stump98eb8a72009-02-04 22:31:32 +0000620 // Walk the DeclTypeInfo, building the recursive type as we go.
621 // DeclTypeInfos are ordered from the identifier out, which is
622 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000623 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
624 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 switch (DeclType.Kind) {
626 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000627 case DeclaratorChunk::BlockPointer:
628 if (DeclType.Cls.TypeQuals)
629 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
630 if (!T.getTypePtr()->isFunctionType())
631 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
632 else
633 T = Context.getBlockPointerType(T);
634 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000636 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000638 case DeclaratorChunk::Reference:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000639 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
640 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000641 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 break;
643 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000644 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000645 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 ArrayType::ArraySizeModifier ASM;
647 if (ATI.isStar)
648 ASM = ArrayType::Star;
649 else if (ATI.hasStatic)
650 ASM = ArrayType::Static;
651 else
652 ASM = ArrayType::Normal;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000653 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 break;
655 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000656 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 // If the function declarator has a prototype (i.e. it is not () and
658 // does not have a K&R-style identifier list), then the arguments are part
659 // of the type, otherwise the argument list is ().
660 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000661
Chris Lattnercd881292007-12-19 05:31:29 +0000662 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000663 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000664 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000665 T = Context.IntTy;
666 D.setInvalidType(true);
667 }
668
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000669 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000670 if (getLangOptions().CPlusPlus) {
671 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
672 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000673 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000674 } else if (FTI.isVariadic) {
675 // We allow a zero-parameter variadic function in C if the
676 // function is marked with the "overloadable"
677 // attribute. Scan for this attribute now.
678 bool Overloadable = false;
679 for (const AttributeList *Attrs = D.getAttributes();
680 Attrs; Attrs = Attrs->getNext()) {
681 if (Attrs->getKind() == AttributeList::AT_overloadable) {
682 Overloadable = true;
683 break;
684 }
685 }
686
687 if (!Overloadable)
688 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
689 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000690 } else {
691 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000692 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000693 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000694 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000696 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 } else {
698 // Otherwise, we have a function with an argument list that is
699 // potentially variadic.
700 llvm::SmallVector<QualType, 16> ArgTys;
701
702 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000703 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
704 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000705 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000706
707 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000708 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // Look for 'void'. void is allowed only as a single argument to a
711 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000712 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000713 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 // If this is something like 'float(int, void)', reject it. 'void'
715 // is an incomplete type (C99 6.2.5p19) and function decls cannot
716 // have arguments of incomplete type.
717 if (FTI.NumArgs != 1 || FTI.isVariadic) {
718 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000719 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000720 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000721 } else if (FTI.ArgInfo[i].Ident) {
722 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000724 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000725 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000726 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000727 } else {
728 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000729 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000730 Diag(DeclType.Loc, diag::err_void_param_qualified);
731
732 // Do not add 'void' to the ArgTys list.
733 break;
734 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000735 } else if (!FTI.hasPrototype) {
736 if (ArgTy->isPromotableIntegerType()) {
737 ArgTy = Context.IntTy;
738 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
739 if (BTy->getKind() == BuiltinType::Float)
740 ArgTy = Context.DoubleTy;
741 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 }
743
744 ArgTys.push_back(ArgTy);
745 }
746 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000747 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 }
749 break;
750 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000751 case DeclaratorChunk::MemberPointer:
752 // The scope spec must refer to a class, or be dependent.
Douglas Gregore4e5b052009-03-19 00:18:19 +0000753 DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000754 QualType ClsType;
755 // FIXME: Extend for dependent types when it's actually supported.
756 // See ActOnCXXNestedNameSpecifier.
757 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
758 ClsType = Context.getTagDeclType(RD);
759 } else {
760 if (DC) {
761 Diag(DeclType.Mem.Scope().getBeginLoc(),
762 diag::err_illegal_decl_mempointer_in_nonclass)
763 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
764 << DeclType.Mem.Scope().getRange();
765 }
766 D.setInvalidType(true);
767 ClsType = Context.IntTy;
768 }
769
770 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
771 // with reference type, or "cv void."
772 if (T->isReferenceType()) {
773 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
774 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
775 D.setInvalidType(true);
776 T = Context.IntTy;
777 }
778 if (T->isVoidType()) {
779 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
780 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
781 T = Context.IntTy;
782 }
783
784 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
785 // object or incomplete types shall not be restrict-qualified."
786 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
787 !T->isIncompleteOrObjectType()) {
788 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
789 << T;
790 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
791 }
792
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000793 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
794 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000795
796 break;
797 }
798
Douglas Gregorcd281c32009-02-28 00:25:32 +0000799 if (T.isNull()) {
800 D.setInvalidType(true);
801 T = Context.IntTy;
802 }
803
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000804 // See if there are any attributes on this declarator chunk.
805 if (const AttributeList *AL = DeclType.getAttrs())
806 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000808
809 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000810 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
811 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000812
813 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
814 // for a nonstatic member function, the function type to which a pointer
815 // to member refers, or the top-level function type of a function typedef
816 // declaration.
817 if (FnTy->getTypeQuals() != 0 &&
818 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000819 ((D.getContext() != Declarator::MemberContext &&
820 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +0000821 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000822 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000823 if (D.isFunctionDeclarator())
824 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
825 else
826 Diag(D.getIdentifierLoc(),
827 diag::err_invalid_qualified_typedef_function_type_use);
828
829 // Strip the cv-quals from the type.
830 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000831 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000832 }
833 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000835 // If there were any type attributes applied to the decl itself (not the
836 // type, apply the type attribute to the type!)
837 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000838 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 return T;
841}
842
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000843/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000844/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000845QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000846 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000847 QualType T = MDecl->getResultType();
848 llvm::SmallVector<QualType, 16> ArgTys;
849
Fariborz Jahanian35600022007-11-09 17:18:29 +0000850 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000851 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000852 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000853 selfTy = Context.getPointerType(selfTy);
854 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000855 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 ArgTys.push_back(Context.getObjCIdType());
857 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000858
Chris Lattner89951a82009-02-20 18:43:26 +0000859 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
860 E = MDecl->param_end(); PI != E; ++PI) {
861 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000862 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000863 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000864 ArgTys.push_back(ArgTy);
865 }
866 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000867 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000868 return T;
869}
870
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000871/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
872/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
873/// they point to and return true. If T1 and T2 aren't pointer types
874/// or pointer-to-member types, or if they are not similar at this
875/// level, returns false and leaves T1 and T2 unchanged. Top-level
876/// qualifiers on T1 and T2 are ignored. This function will typically
877/// be called in a loop that successively "unwraps" pointer and
878/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000879bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000880 const PointerType *T1PtrType = T1->getAsPointerType(),
881 *T2PtrType = T2->getAsPointerType();
882 if (T1PtrType && T2PtrType) {
883 T1 = T1PtrType->getPointeeType();
884 T2 = T2PtrType->getPointeeType();
885 return true;
886 }
887
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000888 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
889 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000890 if (T1MPType && T2MPType &&
891 Context.getCanonicalType(T1MPType->getClass()) ==
892 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000893 T1 = T1MPType->getPointeeType();
894 T2 = T2MPType->getPointeeType();
895 return true;
896 }
Douglas Gregor57373262008-10-22 14:17:15 +0000897 return false;
898}
899
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000900Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 // C99 6.7.6: Type names have no identifier. This is already validated by
902 // the parser.
903 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
904
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000905 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000906 if (T.isNull())
907 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000908
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000909 // Check that there are no default arguments (C++ only).
910 if (getLangOptions().CPlusPlus)
911 CheckExtraCXXDefaultArguments(D);
912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 return T.getAsOpaquePtr();
914}
915
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000916
917
918//===----------------------------------------------------------------------===//
919// Type Attribute Processing
920//===----------------------------------------------------------------------===//
921
922/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
923/// specified type. The attribute contains 1 argument, the id of the address
924/// space for the type.
925static void HandleAddressSpaceTypeAttribute(QualType &Type,
926 const AttributeList &Attr, Sema &S){
927 // If this type is already address space qualified, reject it.
928 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
929 // for two or more different address spaces."
930 if (Type.getAddressSpace()) {
931 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
932 return;
933 }
934
935 // Check the attribute arguments.
936 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000938 return;
939 }
940 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
941 llvm::APSInt addrSpace(32);
942 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
944 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000945 return;
946 }
947
948 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000949 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000950}
951
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000952/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
953/// specified type. The attribute contains 1 argument, weak or strong.
954static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000955 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000956 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000957 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000958 return;
959 }
960
961 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000962 if (!Attr.getParameterName()) {
963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
964 << "objc_gc" << 1;
965 return;
966 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000967 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000968 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000969 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
970 return;
971 }
972 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000973 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000974 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000975 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000976 else {
977 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
978 << "objc_gc" << Attr.getParameterName();
979 return;
980 }
981
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000982 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000983}
984
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000985void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000986 // Scan through and apply attributes to this type where it makes sense. Some
987 // attributes (such as __address_space__, __vector_size__, etc) apply to the
988 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000989 // apply to the decl. Here we apply type attributes and ignore the rest.
990 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000991 // If this is an attribute we can handle, do so now, otherwise, add it to
992 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000993 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000994 default: break;
995 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000996 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
997 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000998 case AttributeList::AT_objc_gc:
999 HandleObjCGCTypeAttribute(Result, *AL, *this);
1000 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001001 }
Chris Lattner232e8822008-02-21 01:08:11 +00001002 }
Chris Lattner232e8822008-02-21 01:08:11 +00001003}
1004
Douglas Gregor86447ec2009-03-09 16:13:40 +00001005/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001006///
1007/// This routine checks whether the type @p T is complete in any
1008/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001009/// type, returns false. If @p T is a class template specialization,
1010/// this routine then attempts to perform class template
1011/// instantiation. If instantiation fails, or if @p T is incomplete
1012/// and cannot be completed, issues the diagnostic @p diag (giving it
1013/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001014///
1015/// @param Loc The location in the source that the incomplete type
1016/// diagnostic should refer to.
1017///
1018/// @param T The type that this routine is examining for completeness.
1019///
1020/// @param diag The diagnostic value (e.g.,
1021/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1022/// for the error message if @p T is incomplete.
1023///
1024/// @param Range1 An optional range in the source code that will be a
1025/// part of the "incomplete type" error message.
1026///
1027/// @param Range2 An optional range in the source code that will be a
1028/// part of the "incomplete type" error message.
1029///
1030/// @param PrintType If non-NULL, the type that should be printed
1031/// instead of @p T. This parameter should be used when the type that
1032/// we're checking for incompleteness isn't the type that should be
1033/// displayed to the user, e.g., when T is a type and PrintType is a
1034/// pointer to T.
1035///
1036/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1037/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001038bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001039 SourceRange Range1, SourceRange Range2,
1040 QualType PrintType) {
1041 // If we have a complete type, we're done.
1042 if (!T->isIncompleteType())
1043 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001044
Douglas Gregord475b8d2009-03-25 21:17:03 +00001045 // If we have a class template specialization or a class member of a
1046 // class template specialization, try to instantiate it.
1047 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001048 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001049 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001050 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1051 // Update the class template specialization's location to
1052 // refer to the point of instantiation.
1053 if (Loc.isValid())
1054 ClassTemplateSpec->setLocation(Loc);
1055 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1056 /*ExplicitInstantiation=*/false);
1057 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001058 } else if (CXXRecordDecl *Rec
1059 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1060 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1061 // Find the class template specialization that surrounds this
1062 // member class.
1063 ClassTemplateSpecializationDecl *Spec = 0;
1064 for (DeclContext *Parent = Rec->getDeclContext();
1065 Parent && !Spec; Parent = Parent->getParent())
1066 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1067 assert(Spec && "Not a member of a class template specialization?");
1068 return InstantiateClass(Loc, Rec, Pattern,
1069 Spec->getTemplateArgs(),
1070 Spec->getNumTemplateArgs());
1071 }
1072 }
1073 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001074
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001075 if (PrintType.isNull())
1076 PrintType = T;
1077
1078 // We have an incomplete type. Produce a diagnostic.
1079 Diag(Loc, diag) << PrintType << Range1 << Range2;
1080
1081 // If the type was a forward declaration of a class/struct/union
1082 // type, produce
1083 const TagType *Tag = 0;
1084 if (const RecordType *Record = T->getAsRecordType())
1085 Tag = Record;
1086 else if (const EnumType *Enum = T->getAsEnumType())
1087 Tag = Enum;
1088
1089 if (Tag && !Tag->getDecl()->isInvalidDecl())
1090 Diag(Tag->getDecl()->getLocation(),
1091 Tag->isBeingDefined() ? diag::note_type_being_defined
1092 : diag::note_forward_declaration)
1093 << QualType(Tag, 0);
1094
1095 return true;
1096}
Douglas Gregore6258932009-03-19 00:39:20 +00001097
1098/// \brief Retrieve a version of the type 'T' that is qualified by the
1099/// nested-name-specifier contained in SS.
1100QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1101 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1102 return T;
1103
Douglas Gregorab452ba2009-03-26 23:50:42 +00001104 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001105 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001106 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001107}