blob: 972ac82b4cb80736d152638868422f5ac8c2536c [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
Chris Lattner3f84ad22009-04-22 05:27:59 +000049/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000050/// \returns The type described by the declaration specifiers. This function
51/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000052QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 SourceLocation DeclLoc,
54 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
56 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000057 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000060 case DeclSpec::TST_void:
61 Result = Context.VoidTy;
62 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 case DeclSpec::TST_char:
64 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000065 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000066 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else {
69 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
70 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Chris Lattner958858e2008-02-20 21:40:32 +000073 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000074 case DeclSpec::TST_wchar:
75 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
76 Result = Context.WCharTy;
77 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000078 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
79 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000080 Result = Context.getSignedWCharType();
81 } else {
82 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
83 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000084 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
85 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000086 Result = Context.getUnsignedWCharType();
87 }
88 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000089 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000090 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000091 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000092 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000093 DS.getNumProtocolQualifiers());
94 break;
95 }
96
Chris Lattnerd658b562008-04-05 06:32:51 +000097 // Unspecified typespec defaults to int in C90. However, the C90 grammar
98 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
99 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
100 // Note that the one exception to this is function definitions, which are
101 // allowed to be completely missing a declspec. This is handled in the
102 // parser already though by it pretending to have seen an 'int' in this
103 // case.
104 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000105 // In C89 mode, we only warn if there is a completely missing declspec
106 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000107 if (DS.isEmpty()) {
108 if (DeclLoc.isInvalid())
109 DeclLoc = DS.getSourceRange().getBegin();
110 Diag(DeclLoc, diag::warn_missing_declspec)
111 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000112 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
113 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000114 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000115 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000116 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
117 // "At least one type specifier shall be given in the declaration
118 // specifiers in each declaration, and in the specifier-qualifier list in
119 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000120 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000121 if (DeclLoc.isInvalid())
122 DeclLoc = DS.getSourceRange().getBegin();
123
124 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft)
125 Diag(DeclLoc, diag::err_missing_type_specifier)
126 << DS.getSourceRange();
127 else
128 Diag(DeclLoc, diag::warn_missing_type_specifier)
129 << DS.getSourceRange();
Douglas Gregord17a9e52009-03-27 05:10:56 +0000130
131 // FIXME: If we could guarantee that the result would be
132 // well-formed, it would be useful to have a code insertion hint
133 // here. However, after emitting this warning/error, we often
134 // emit other errors.
Chris Lattnerd658b562008-04-05 06:32:51 +0000135 }
136
137 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000138 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
140 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000141 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
142 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
143 case DeclSpec::TSW_long: Result = Context.LongTy; break;
144 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
146 } else {
147 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000148 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
149 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
150 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
151 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
Chris Lattner958858e2008-02-20 21:40:32 +0000154 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000155 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000156 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000157 case DeclSpec::TST_double:
158 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000159 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000160 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000161 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000162 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000163 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 case DeclSpec::TST_decimal32: // _Decimal32
165 case DeclSpec::TST_decimal64: // _Decimal64
166 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000167 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
168 Result = Context.IntTy;
169 isInvalid = true;
170 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000171 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 case DeclSpec::TST_enum:
173 case DeclSpec::TST_union:
174 case DeclSpec::TST_struct: {
175 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000176 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
178 DS.getTypeSpecSign() == 0 &&
179 "Can't handle qualifiers on typedef names yet!");
180 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000181 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000182
183 if (D->isInvalidDecl())
184 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000185 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000187 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
189 DS.getTypeSpecSign() == 0 &&
190 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000191 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000192
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000193 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
194 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
195 // we have this "hack" for now...
196 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
197 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
198 (ObjCProtocolDecl**)PQ,
199 DS.getNumProtocolQualifiers());
200 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000201 // id<protocol-list>
202 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
203 DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000204 else if (Result == Context.getObjCClassType()) {
205 if (DeclLoc.isInvalid())
206 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000207 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000208 Diag(DeclLoc, diag::err_qualified_class_unsupported)
209 << DS.getSourceRange();
210 } else {
211 if (DeclLoc.isInvalid())
212 DeclLoc = DS.getSourceRange().getBegin();
213 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
214 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000215 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000216 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000217 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000218
219 // If this is a reference to an invalid typedef, propagate the invalidity.
220 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
221 if (TDT->getDecl()->isInvalidDecl())
222 isInvalid = true;
223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000225 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 }
Chris Lattner958858e2008-02-20 21:40:32 +0000227 case DeclSpec::TST_typeofType:
228 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
229 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000230 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000231 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000232 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000233 case DeclSpec::TST_typeofExpr: {
234 Expr *E = static_cast<Expr *>(DS.getTypeRep());
235 assert(E && "Didn't get an expression for typeof?");
236 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000237 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000238 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000239 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000240 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000241 Result = Context.IntTy;
242 isInvalid = true;
243 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 }
Chris Lattner958858e2008-02-20 21:40:32 +0000245
246 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000247 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
248 if (getLangOptions().Freestanding)
249 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000250 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000251 }
Chris Lattner958858e2008-02-20 21:40:32 +0000252
253 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
254 "FIXME: imaginary types not supported yet!");
255
Chris Lattner38d8b982008-02-20 22:04:11 +0000256 // See if there are any attributes on the declspec that apply to the type (as
257 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000258 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000259 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000260
Chris Lattner96b77fc2008-04-02 06:50:17 +0000261 // Apply const/volatile/restrict qualifiers to T.
262 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
263
264 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
265 // or incomplete types shall not be restrict-qualified." C++ also allows
266 // restrict-qualified references.
267 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000268 if (Result->isPointerType() || Result->isReferenceType()) {
269 QualType EltTy = Result->isPointerType() ?
270 Result->getAsPointerType()->getPointeeType() :
271 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000272
Douglas Gregorbad0e652009-03-24 20:32:41 +0000273 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000274 // incomplete type.
275 if (!EltTy->isIncompleteOrObjectType()) {
276 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000277 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000278 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000279 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
280 }
281 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000282 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000283 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000284 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000285 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000286 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000287 }
288
289 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
290 // of a function type includes any type qualifiers, the behavior is
291 // undefined."
292 if (Result->isFunctionType() && TypeQuals) {
293 // Get some location to point at, either the C or V location.
294 SourceLocation Loc;
295 if (TypeQuals & QualType::Const)
296 Loc = DS.getConstSpecLoc();
297 else {
298 assert((TypeQuals & QualType::Volatile) &&
299 "Has CV quals but not C or V?");
300 Loc = DS.getVolatileSpecLoc();
301 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000302 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000303 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000304 }
305
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000306 // C++ [dcl.ref]p1:
307 // Cv-qualified references are ill-formed except when the
308 // cv-qualifiers are introduced through the use of a typedef
309 // (7.1.3) or of a template type argument (14.3), in which
310 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000311 // FIXME: Shouldn't we be checking SCS_typedef here?
312 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000313 TypeQuals && Result->isReferenceType()) {
314 TypeQuals &= ~QualType::Const;
315 TypeQuals &= ~QualType::Volatile;
316 }
317
Chris Lattner96b77fc2008-04-02 06:50:17 +0000318 Result = Result.getQualifiedType(TypeQuals);
319 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000320 return Result;
321}
322
Douglas Gregorcd281c32009-02-28 00:25:32 +0000323static std::string getPrintableNameForEntity(DeclarationName Entity) {
324 if (Entity)
325 return Entity.getAsString();
326
327 return "type name";
328}
329
330/// \brief Build a pointer type.
331///
332/// \param T The type to which we'll be building a pointer.
333///
334/// \param Quals The cvr-qualifiers to be applied to the pointer type.
335///
336/// \param Loc The location of the entity whose type involves this
337/// pointer type or, if there is no such entity, the location of the
338/// type that will have pointer type.
339///
340/// \param Entity The name of the entity that involves the pointer
341/// type, if known.
342///
343/// \returns A suitable pointer type, if there are no
344/// errors. Otherwise, returns a NULL type.
345QualType Sema::BuildPointerType(QualType T, unsigned Quals,
346 SourceLocation Loc, DeclarationName Entity) {
347 if (T->isReferenceType()) {
348 // C++ 8.3.2p4: There shall be no ... pointers to references ...
349 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
350 << getPrintableNameForEntity(Entity);
351 return QualType();
352 }
353
354 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
355 // object or incomplete types shall not be restrict-qualified."
356 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
357 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
358 << T;
359 Quals &= ~QualType::Restrict;
360 }
361
362 // Build the pointer type.
363 return Context.getPointerType(T).getQualifiedType(Quals);
364}
365
366/// \brief Build a reference type.
367///
368/// \param T The type to which we'll be building a reference.
369///
370/// \param Quals The cvr-qualifiers to be applied to the reference type.
371///
372/// \param Loc The location of the entity whose type involves this
373/// reference type or, if there is no such entity, the location of the
374/// type that will have reference type.
375///
376/// \param Entity The name of the entity that involves the reference
377/// type, if known.
378///
379/// \returns A suitable reference type, if there are no
380/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000381QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000382 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000383 if (LValueRef) {
384 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000385 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
386 // reference to a type T, and attempt to create the type "lvalue
387 // reference to cv TD" creates the type "lvalue reference to T".
388 // We use the qualifiers (restrict or none) of the original reference,
389 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000390 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000391 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000392 }
393 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000394 if (T->isReferenceType()) {
395 // C++ [dcl.ref]p4: There shall be no references to references.
396 //
397 // According to C++ DR 106, references to references are only
398 // diagnosed when they are written directly (e.g., "int & &"),
399 // but not when they happen via a typedef:
400 //
401 // typedef int& intref;
402 // typedef intref& intref2;
403 //
404 // Parser::ParserDeclaratorInternal diagnoses the case where
405 // references are written directly; here, we handle the
406 // collapsing of references-to-references as described in C++
407 // DR 106 and amended by C++ DR 540.
408 return T;
409 }
410
411 // C++ [dcl.ref]p1:
412 // A declarator that specifies the type “reference to cv void”
413 // is ill-formed.
414 if (T->isVoidType()) {
415 Diag(Loc, diag::err_reference_to_void);
416 return QualType();
417 }
418
419 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
420 // object or incomplete types shall not be restrict-qualified."
421 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
422 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
423 << T;
424 Quals &= ~QualType::Restrict;
425 }
426
427 // C++ [dcl.ref]p1:
428 // [...] Cv-qualified references are ill-formed except when the
429 // cv-qualifiers are introduced through the use of a typedef
430 // (7.1.3) or of a template type argument (14.3), in which case
431 // the cv-qualifiers are ignored.
432 //
433 // We diagnose extraneous cv-qualifiers for the non-typedef,
434 // non-template type argument case within the parser. Here, we just
435 // ignore any extraneous cv-qualifiers.
436 Quals &= ~QualType::Const;
437 Quals &= ~QualType::Volatile;
438
439 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000440 if (LValueRef)
441 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
442 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000443}
444
445/// \brief Build an array type.
446///
447/// \param T The type of each element in the array.
448///
449/// \param ASM C99 array size modifier (e.g., '*', 'static').
450///
451/// \param ArraySize Expression describing the size of the array.
452///
453/// \param Quals The cvr-qualifiers to be applied to the array's
454/// element type.
455///
456/// \param Loc The location of the entity whose type involves this
457/// array type or, if there is no such entity, the location of the
458/// type that will have array type.
459///
460/// \param Entity The name of the entity that involves the array
461/// type, if known.
462///
463/// \returns A suitable array type, if there are no errors. Otherwise,
464/// returns a NULL type.
465QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
466 Expr *ArraySize, unsigned Quals,
467 SourceLocation Loc, DeclarationName Entity) {
468 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
469 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000470 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000471 diag::err_illegal_decl_array_incomplete_type))
472 return QualType();
473
474 if (T->isFunctionType()) {
475 Diag(Loc, diag::err_illegal_decl_array_of_functions)
476 << getPrintableNameForEntity(Entity);
477 return QualType();
478 }
479
480 // C++ 8.3.2p4: There shall be no ... arrays of references ...
481 if (T->isReferenceType()) {
482 Diag(Loc, diag::err_illegal_decl_array_of_references)
483 << getPrintableNameForEntity(Entity);
484 return QualType();
485 }
486
487 if (const RecordType *EltTy = T->getAsRecordType()) {
488 // If the element type is a struct or union that contains a variadic
489 // array, accept it as a GNU extension: C99 6.7.2.1p2.
490 if (EltTy->getDecl()->hasFlexibleArrayMember())
491 Diag(Loc, diag::ext_flexible_array_in_array) << T;
492 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000493 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
494 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000495 }
496
497 // C99 6.7.5.2p1: The size expression shall have integer type.
498 if (ArraySize && !ArraySize->isTypeDependent() &&
499 !ArraySize->getType()->isIntegerType()) {
500 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
501 << ArraySize->getType() << ArraySize->getSourceRange();
502 ArraySize->Destroy(Context);
503 return QualType();
504 }
505 llvm::APSInt ConstVal(32);
506 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000507 if (ASM == ArrayType::Star)
508 T = Context.getVariableArrayType(T, 0, ASM, Quals);
509 else
510 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000511 } else if (ArraySize->isValueDependent()) {
512 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
513 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
514 (!T->isDependentType() && !T->isConstantSizeType())) {
515 // Per C99, a variable array is an array with either a non-constant
516 // size or an element type that has a non-constant-size
517 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
518 } else {
519 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
520 // have a value greater than zero.
521 if (ConstVal.isSigned()) {
522 if (ConstVal.isNegative()) {
523 Diag(ArraySize->getLocStart(),
524 diag::err_typecheck_negative_array_size)
525 << ArraySize->getSourceRange();
526 return QualType();
527 } else if (ConstVal == 0) {
528 // GCC accepts zero sized static arrays.
529 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
530 << ArraySize->getSourceRange();
531 }
532 }
533 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
534 }
535 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
536 if (!getLangOptions().C99) {
537 if (ArraySize && !ArraySize->isTypeDependent() &&
538 !ArraySize->isValueDependent() &&
539 !ArraySize->isIntegerConstantExpr(Context))
540 Diag(Loc, diag::ext_vla);
541 else if (ASM != ArrayType::Normal || Quals != 0)
542 Diag(Loc, diag::ext_c99_array_usage);
543 }
544
545 return T;
546}
547
Douglas Gregor724651c2009-02-28 01:04:19 +0000548/// \brief Build a function type.
549///
550/// This routine checks the function type according to C++ rules and
551/// under the assumption that the result type and parameter types have
552/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000553/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000554/// simpler form that is only suitable for this narrow use case.
555///
556/// \param T The return type of the function.
557///
558/// \param ParamTypes The parameter types of the function. This array
559/// will be modified to account for adjustments to the types of the
560/// function parameters.
561///
562/// \param NumParamTypes The number of parameter types in ParamTypes.
563///
564/// \param Variadic Whether this is a variadic function type.
565///
566/// \param Quals The cvr-qualifiers to be applied to the function type.
567///
568/// \param Loc The location of the entity whose type involves this
569/// function type or, if there is no such entity, the location of the
570/// type that will have function type.
571///
572/// \param Entity The name of the entity that involves the function
573/// type, if known.
574///
575/// \returns A suitable function type, if there are no
576/// errors. Otherwise, returns a NULL type.
577QualType Sema::BuildFunctionType(QualType T,
578 QualType *ParamTypes,
579 unsigned NumParamTypes,
580 bool Variadic, unsigned Quals,
581 SourceLocation Loc, DeclarationName Entity) {
582 if (T->isArrayType() || T->isFunctionType()) {
583 Diag(Loc, diag::err_func_returning_array_function) << T;
584 return QualType();
585 }
586
587 bool Invalid = false;
588 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000589 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
590 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000591 Diag(Loc, diag::err_param_with_void_type);
592 Invalid = true;
593 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000594
Douglas Gregor724651c2009-02-28 01:04:19 +0000595 ParamTypes[Idx] = ParamType;
596 }
597
598 if (Invalid)
599 return QualType();
600
601 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
602 Quals);
603}
604
Mike Stump98eb8a72009-02-04 22:31:32 +0000605/// GetTypeForDeclarator - Convert the type for the specified
606/// declarator to Type instances. Skip the outermost Skip type
607/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000608QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000609 bool OmittedReturnType = false;
610
611 if (D.getContext() == Declarator::BlockLiteralContext
612 && Skip == 0
613 && !D.getDeclSpec().hasTypeSpecifier()
614 && (D.getNumTypeObjects() == 0
615 || (D.getNumTypeObjects() == 1
616 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
617 OmittedReturnType = true;
618
Chris Lattnerb23deda2007-08-28 16:40:32 +0000619 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000620 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000621 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
622 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000623
624 // Determine the type of the declarator. Not all forms of declarator
625 // have a type.
626 QualType T;
627 switch (D.getKind()) {
628 case Declarator::DK_Abstract:
629 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000630 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000631 const DeclSpec &DS = D.getDeclSpec();
632 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000633 // We default to a dependent type initially. Can be modified by
634 // the first return statement.
635 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000636 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000637 bool isInvalid = false;
638 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000639 if (isInvalid)
640 D.setInvalidType(true);
Douglas Gregor809070a2009-02-18 17:45:20 +0000641 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000642 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000643 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000644
645 case Declarator::DK_Constructor:
646 case Declarator::DK_Destructor:
647 case Declarator::DK_Conversion:
648 // Constructors and destructors don't have return types. Use
649 // "void" instead. Conversion operators will check their return
650 // types separately.
651 T = Context.VoidTy;
652 break;
653 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000654
Douglas Gregorcd281c32009-02-28 00:25:32 +0000655 // The name we're declaring, if any.
656 DeclarationName Name;
657 if (D.getIdentifier())
658 Name = D.getIdentifier();
659
Mike Stump98eb8a72009-02-04 22:31:32 +0000660 // Walk the DeclTypeInfo, building the recursive type as we go.
661 // DeclTypeInfos are ordered from the identifier out, which is
662 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000663 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
664 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 switch (DeclType.Kind) {
666 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000667 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000668 // If blocks are disabled, emit an error.
669 if (!LangOpts.Blocks)
670 Diag(DeclType.Loc, diag::err_blocks_disable);
671
Steve Naroff5618bd42008-08-27 16:04:49 +0000672 if (!T.getTypePtr()->isFunctionType())
673 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
674 else
Mike Stump7bc8d962009-05-07 21:56:17 +0000675 T = (Context.getBlockPointerType(T)
676 .getQualifiedType(DeclType.Cls.TypeQuals));
Steve Naroff5618bd42008-08-27 16:04:49 +0000677 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000679 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000681 case DeclaratorChunk::Reference:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000682 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
683 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000684 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 break;
686 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000687 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000688 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 ArrayType::ArraySizeModifier ASM;
690 if (ATI.isStar)
691 ASM = ArrayType::Star;
692 else if (ATI.hasStatic)
693 ASM = ArrayType::Static;
694 else
695 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000696 if (ASM == ArrayType::Star &&
697 D.getContext() != Declarator::PrototypeContext) {
698 // FIXME: This check isn't quite right: it allows star in prototypes
699 // for function definitions, and disallows some edge cases detailed
700 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
701 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
702 ASM = ArrayType::Normal;
703 D.setInvalidType(true);
704 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000705 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 break;
707 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000708 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 // If the function declarator has a prototype (i.e. it is not () and
710 // does not have a K&R-style identifier list), then the arguments are part
711 // of the type, otherwise the argument list is ().
712 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000713
Chris Lattnercd881292007-12-19 05:31:29 +0000714 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000715 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000716 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000717 T = Context.IntTy;
718 D.setInvalidType(true);
719 }
720
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000721 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000722 if (getLangOptions().CPlusPlus) {
723 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
724 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000725 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000726 } else if (FTI.isVariadic) {
727 // We allow a zero-parameter variadic function in C if the
728 // function is marked with the "overloadable"
729 // attribute. Scan for this attribute now.
730 bool Overloadable = false;
731 for (const AttributeList *Attrs = D.getAttributes();
732 Attrs; Attrs = Attrs->getNext()) {
733 if (Attrs->getKind() == AttributeList::AT_overloadable) {
734 Overloadable = true;
735 break;
736 }
737 }
738
739 if (!Overloadable)
740 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
741 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000742 } else {
743 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000744 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000745 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000746 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000748 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 } else {
750 // Otherwise, we have a function with an argument list that is
751 // potentially variadic.
752 llvm::SmallVector<QualType, 16> ArgTys;
753
754 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000755 ParmVarDecl *Param =
756 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +0000757 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000758 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000759
760 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000761 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000762
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 // Look for 'void'. void is allowed only as a single argument to a
764 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000765 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000766 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 // If this is something like 'float(int, void)', reject it. 'void'
768 // is an incomplete type (C99 6.2.5p19) and function decls cannot
769 // have arguments of incomplete type.
770 if (FTI.NumArgs != 1 || FTI.isVariadic) {
771 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000772 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000773 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000774 } else if (FTI.ArgInfo[i].Ident) {
775 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000777 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000778 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000779 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000780 } else {
781 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000782 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000783 Diag(DeclType.Loc, diag::err_void_param_qualified);
784
785 // Do not add 'void' to the ArgTys list.
786 break;
787 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000788 } else if (!FTI.hasPrototype) {
789 if (ArgTy->isPromotableIntegerType()) {
790 ArgTy = Context.IntTy;
791 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
792 if (BTy->getKind() == BuiltinType::Float)
793 ArgTy = Context.DoubleTy;
794 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 }
796
797 ArgTys.push_back(ArgTy);
798 }
799 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000800 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 }
802 break;
803 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000804 case DeclaratorChunk::MemberPointer:
805 // The scope spec must refer to a class, or be dependent.
Douglas Gregore4e5b052009-03-19 00:18:19 +0000806 DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000807 QualType ClsType;
808 // FIXME: Extend for dependent types when it's actually supported.
809 // See ActOnCXXNestedNameSpecifier.
810 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
811 ClsType = Context.getTagDeclType(RD);
812 } else {
813 if (DC) {
814 Diag(DeclType.Mem.Scope().getBeginLoc(),
815 diag::err_illegal_decl_mempointer_in_nonclass)
816 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
817 << DeclType.Mem.Scope().getRange();
818 }
819 D.setInvalidType(true);
820 ClsType = Context.IntTy;
821 }
822
823 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
824 // with reference type, or "cv void."
825 if (T->isReferenceType()) {
826 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
827 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
828 D.setInvalidType(true);
829 T = Context.IntTy;
830 }
831 if (T->isVoidType()) {
832 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
833 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
834 T = Context.IntTy;
835 }
836
837 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
838 // object or incomplete types shall not be restrict-qualified."
839 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
840 !T->isIncompleteOrObjectType()) {
841 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
842 << T;
843 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
844 }
845
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000846 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
847 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000848
849 break;
850 }
851
Douglas Gregorcd281c32009-02-28 00:25:32 +0000852 if (T.isNull()) {
853 D.setInvalidType(true);
854 T = Context.IntTy;
855 }
856
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000857 // See if there are any attributes on this declarator chunk.
858 if (const AttributeList *AL = DeclType.getAttrs())
859 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000861
862 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000863 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
864 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000865
866 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
867 // for a nonstatic member function, the function type to which a pointer
868 // to member refers, or the top-level function type of a function typedef
869 // declaration.
870 if (FnTy->getTypeQuals() != 0 &&
871 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000872 ((D.getContext() != Declarator::MemberContext &&
873 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +0000874 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000875 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000876 if (D.isFunctionDeclarator())
877 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
878 else
879 Diag(D.getIdentifierLoc(),
880 diag::err_invalid_qualified_typedef_function_type_use);
881
882 // Strip the cv-quals from the type.
883 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000884 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000885 }
886 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000887
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000888 // If there were any type attributes applied to the decl itself (not the
889 // type, apply the type attribute to the type!)
890 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000891 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000892
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 return T;
894}
895
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000896/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000897/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +0000898QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
899 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000900 QualType T = MDecl->getResultType();
901 llvm::SmallVector<QualType, 16> ArgTys;
902
Fariborz Jahanian35600022007-11-09 17:18:29 +0000903 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000904 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000905 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000906 selfTy = Context.getPointerType(selfTy);
907 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000908 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 ArgTys.push_back(Context.getObjCIdType());
910 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000911
Chris Lattner89951a82009-02-20 18:43:26 +0000912 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
913 E = MDecl->param_end(); PI != E; ++PI) {
914 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000915 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000916 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000917 ArgTys.push_back(ArgTy);
918 }
919 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000920 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000921 return T;
922}
923
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000924/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
925/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
926/// they point to and return true. If T1 and T2 aren't pointer types
927/// or pointer-to-member types, or if they are not similar at this
928/// level, returns false and leaves T1 and T2 unchanged. Top-level
929/// qualifiers on T1 and T2 are ignored. This function will typically
930/// be called in a loop that successively "unwraps" pointer and
931/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000932bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000933 const PointerType *T1PtrType = T1->getAsPointerType(),
934 *T2PtrType = T2->getAsPointerType();
935 if (T1PtrType && T2PtrType) {
936 T1 = T1PtrType->getPointeeType();
937 T2 = T2PtrType->getPointeeType();
938 return true;
939 }
940
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000941 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
942 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000943 if (T1MPType && T2MPType &&
944 Context.getCanonicalType(T1MPType->getClass()) ==
945 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000946 T1 = T1MPType->getPointeeType();
947 T2 = T2MPType->getPointeeType();
948 return true;
949 }
Douglas Gregor57373262008-10-22 14:17:15 +0000950 return false;
951}
952
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000953Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 // C99 6.7.6: Type names have no identifier. This is already validated by
955 // the parser.
956 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
957
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000958 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner5153ee62009-04-25 08:47:54 +0000959 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +0000960 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000961
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000962 // Check that there are no default arguments (C++ only).
963 if (getLangOptions().CPlusPlus)
964 CheckExtraCXXDefaultArguments(D);
965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 return T.getAsOpaquePtr();
967}
968
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000969
970
971//===----------------------------------------------------------------------===//
972// Type Attribute Processing
973//===----------------------------------------------------------------------===//
974
975/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
976/// specified type. The attribute contains 1 argument, the id of the address
977/// space for the type.
978static void HandleAddressSpaceTypeAttribute(QualType &Type,
979 const AttributeList &Attr, Sema &S){
980 // If this type is already address space qualified, reject it.
981 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
982 // for two or more different address spaces."
983 if (Type.getAddressSpace()) {
984 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
985 return;
986 }
987
988 // Check the attribute arguments.
989 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000990 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000991 return;
992 }
993 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
994 llvm::APSInt addrSpace(32);
995 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000996 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
997 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000998 return;
999 }
1000
1001 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001002 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001003}
1004
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001005/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1006/// specified type. The attribute contains 1 argument, weak or strong.
1007static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001008 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001009 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001011 return;
1012 }
1013
1014 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001015 if (!Attr.getParameterName()) {
1016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1017 << "objc_gc" << 1;
1018 return;
1019 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001020 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001021 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001022 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1023 return;
1024 }
1025 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001026 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001027 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001028 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001029 else {
1030 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1031 << "objc_gc" << Attr.getParameterName();
1032 return;
1033 }
1034
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001035 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001036}
1037
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001038void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001039 // Scan through and apply attributes to this type where it makes sense. Some
1040 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1041 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001042 // apply to the decl. Here we apply type attributes and ignore the rest.
1043 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001044 // If this is an attribute we can handle, do so now, otherwise, add it to
1045 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001046 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001047 default: break;
1048 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001049 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1050 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001051 case AttributeList::AT_objc_gc:
1052 HandleObjCGCTypeAttribute(Result, *AL, *this);
1053 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001054 }
Chris Lattner232e8822008-02-21 01:08:11 +00001055 }
Chris Lattner232e8822008-02-21 01:08:11 +00001056}
1057
Douglas Gregor86447ec2009-03-09 16:13:40 +00001058/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001059///
1060/// This routine checks whether the type @p T is complete in any
1061/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001062/// type, returns false. If @p T is a class template specialization,
1063/// this routine then attempts to perform class template
1064/// instantiation. If instantiation fails, or if @p T is incomplete
1065/// and cannot be completed, issues the diagnostic @p diag (giving it
1066/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001067///
1068/// @param Loc The location in the source that the incomplete type
1069/// diagnostic should refer to.
1070///
1071/// @param T The type that this routine is examining for completeness.
1072///
1073/// @param diag The diagnostic value (e.g.,
1074/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1075/// for the error message if @p T is incomplete.
1076///
1077/// @param Range1 An optional range in the source code that will be a
1078/// part of the "incomplete type" error message.
1079///
1080/// @param Range2 An optional range in the source code that will be a
1081/// part of the "incomplete type" error message.
1082///
1083/// @param PrintType If non-NULL, the type that should be printed
1084/// instead of @p T. This parameter should be used when the type that
1085/// we're checking for incompleteness isn't the type that should be
1086/// displayed to the user, e.g., when T is a type and PrintType is a
1087/// pointer to T.
1088///
1089/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1090/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001091bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001092 SourceRange Range1, SourceRange Range2,
1093 QualType PrintType) {
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001094 // If we have a complete type, we're done.
1095 if (!T->isIncompleteType())
1096 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001097
Douglas Gregord475b8d2009-03-25 21:17:03 +00001098 // If we have a class template specialization or a class member of a
1099 // class template specialization, try to instantiate it.
1100 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001101 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001102 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001103 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1104 // Update the class template specialization's location to
1105 // refer to the point of instantiation.
1106 if (Loc.isValid())
1107 ClassTemplateSpec->setLocation(Loc);
1108 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1109 /*ExplicitInstantiation=*/false);
1110 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001111 } else if (CXXRecordDecl *Rec
1112 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1113 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1114 // Find the class template specialization that surrounds this
1115 // member class.
1116 ClassTemplateSpecializationDecl *Spec = 0;
1117 for (DeclContext *Parent = Rec->getDeclContext();
1118 Parent && !Spec; Parent = Parent->getParent())
1119 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1120 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001121 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1122 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001123 }
1124 }
1125 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001126
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001127 if (PrintType.isNull())
1128 PrintType = T;
1129
1130 // We have an incomplete type. Produce a diagnostic.
1131 Diag(Loc, diag) << PrintType << Range1 << Range2;
1132
1133 // If the type was a forward declaration of a class/struct/union
1134 // type, produce
1135 const TagType *Tag = 0;
1136 if (const RecordType *Record = T->getAsRecordType())
1137 Tag = Record;
1138 else if (const EnumType *Enum = T->getAsEnumType())
1139 Tag = Enum;
1140
1141 if (Tag && !Tag->getDecl()->isInvalidDecl())
1142 Diag(Tag->getDecl()->getLocation(),
1143 Tag->isBeingDefined() ? diag::note_type_being_defined
1144 : diag::note_forward_declaration)
1145 << QualType(Tag, 0);
1146
1147 return true;
1148}
Douglas Gregore6258932009-03-19 00:39:20 +00001149
1150/// \brief Retrieve a version of the type 'T' that is qualified by the
1151/// nested-name-specifier contained in SS.
1152QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1153 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1154 return T;
1155
Douglas Gregorab452ba2009-03-26 23:50:42 +00001156 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001157 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001158 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001159}