blob: 7551d93c66be5eabd4217063381b1ded3d584e5c [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"
Sebastian Redl23c7d062009-07-07 20:29:57 +000015#include "SemaInherit.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000020#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000021#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor2dc0e642009-03-23 23:06:20 +000024/// \brief Perform adjustment on the parameter type of a function.
25///
26/// This routine adjusts the given parameter type @p T to the actual
27/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
28/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
29QualType Sema::adjustParameterType(QualType T) {
30 // C99 6.7.5.3p7:
31 if (T->isArrayType()) {
32 // C99 6.7.5.3p7:
33 // A declaration of a parameter as "array of type" shall be
34 // adjusted to "qualified pointer to type", where the type
35 // qualifiers (if any) are those specified within the [ and ] of
36 // the array type derivation.
37 return Context.getArrayDecayedType(T);
38 } else if (T->isFunctionType())
39 // C99 6.7.5.3p8:
40 // A declaration of a parameter as "function returning type"
41 // shall be adjusted to "pointer to function returning type", as
42 // in 6.3.2.1.
43 return Context.getPointerType(T);
44
45 return T;
46}
47
Douglas Gregor930d8b52009-01-30 22:09:00 +000048/// \brief Convert the specified declspec to the appropriate type
49/// object.
50/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000051/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000052/// \returns The type described by the declaration specifiers. This function
53/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000054QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000055 SourceLocation DeclLoc,
56 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000057 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
58 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000059 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000060
61 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000062 case DeclSpec::TST_void:
63 Result = Context.VoidTy;
64 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 case DeclSpec::TST_char:
66 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000069 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 else {
71 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
72 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000073 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
Chris Lattner958858e2008-02-20 21:40:32 +000075 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000076 case DeclSpec::TST_wchar:
77 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
78 Result = Context.WCharTy;
79 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000080 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
81 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000082 Result = Context.getSignedWCharType();
83 } else {
84 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
85 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000086 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
87 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000088 Result = Context.getUnsignedWCharType();
89 }
90 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000091 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000092 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000093 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Steve Naroff14108da2009-07-10 23:34:53 +000094 Result = Context.getObjCObjectPointerType(QualType(),
95 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +000096 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +000097 break;
98 }
99
Chris Lattnerd658b562008-04-05 06:32:51 +0000100 // Unspecified typespec defaults to int in C90. However, the C90 grammar
101 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
102 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
103 // Note that the one exception to this is function definitions, which are
104 // allowed to be completely missing a declspec. This is handled in the
105 // parser already though by it pretending to have seen an 'int' in this
106 // case.
107 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000108 // In C89 mode, we only warn if there is a completely missing declspec
109 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000110 if (DS.isEmpty()) {
111 if (DeclLoc.isInvalid())
112 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000113 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000114 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000115 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
116 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000117 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000118 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000119 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
120 // "At least one type specifier shall be given in the declaration
121 // specifiers in each declaration, and in the specifier-qualifier list in
122 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000123 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000124 if (DeclLoc.isInvalid())
125 DeclLoc = DS.getSourceRange().getBegin();
126
Chris Lattnerb78d8332009-06-26 04:45:06 +0000127 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000128 Diag(DeclLoc, diag::err_missing_type_specifier)
129 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000130
131 // When this occurs in C++ code, often something is very broken with the
132 // value being declared, poison it as invalid so we don't get chains of
133 // errors.
134 isInvalid = true;
135 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000136 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000137 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000138 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000139 }
140
141 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000142 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
144 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000145 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
146 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
147 case DeclSpec::TSW_long: Result = Context.LongTy; break;
148 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 } else {
151 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000152 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
153 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
154 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
155 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
Chris Lattner958858e2008-02-20 21:40:32 +0000158 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000159 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000160 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000161 case DeclSpec::TST_double:
162 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000163 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000164 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000165 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000166 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000167 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 case DeclSpec::TST_decimal32: // _Decimal32
169 case DeclSpec::TST_decimal64: // _Decimal64
170 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000171 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
172 Result = Context.IntTy;
173 isInvalid = true;
174 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000175 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 case DeclSpec::TST_enum:
177 case DeclSpec::TST_union:
178 case DeclSpec::TST_struct: {
179 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000180 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
182 DS.getTypeSpecSign() == 0 &&
183 "Can't handle qualifiers on typedef names yet!");
184 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000185 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000186
187 if (D->isInvalidDecl())
188 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000189 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000191 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
193 DS.getTypeSpecSign() == 0 &&
194 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000195 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000196
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000197 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000198 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
199 // this "hack" for now...
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000200 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
Steve Naroff14108da2009-07-10 23:34:53 +0000201 // FIXME: Remove ObjCQualifiedInterfaceType (by moving the list of
202 // protocols 'up' to ObjCInterfaceType).
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000203 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
204 (ObjCProtocolDecl**)PQ,
205 DS.getNumProtocolQualifiers());
Steve Naroff14108da2009-07-10 23:34:53 +0000206 else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000207 // id<protocol-list>
Steve Naroff14108da2009-07-10 23:34:53 +0000208 Result = Context.getObjCObjectPointerType(QualType(),
209 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
210 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000211 if (DeclLoc.isInvalid())
212 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000213 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000214 Diag(DeclLoc, diag::err_qualified_class_unsupported)
215 << DS.getSourceRange();
216 } else {
217 if (DeclLoc.isInvalid())
218 DeclLoc = DS.getSourceRange().getBegin();
219 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
220 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000221 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000222 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000223 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000224
225 // If this is a reference to an invalid typedef, propagate the invalidity.
226 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
227 if (TDT->getDecl()->isInvalidDecl())
228 isInvalid = true;
229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000231 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
Chris Lattner958858e2008-02-20 21:40:32 +0000233 case DeclSpec::TST_typeofType:
234 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
235 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000236 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000237 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000238 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000239 case DeclSpec::TST_typeofExpr: {
240 Expr *E = static_cast<Expr *>(DS.getTypeRep());
241 assert(E && "Didn't get an expression for typeof?");
242 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000243 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000244 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000245 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000246 case DeclSpec::TST_decltype: {
247 Expr *E = static_cast<Expr *>(DS.getTypeRep());
248 assert(E && "Didn't get an expression for decltype?");
249 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000250 Result = BuildDecltypeType(E);
251 if (Result.isNull()) {
252 Result = Context.IntTy;
253 isInvalid = true;
254 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000255 break;
256 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000257 case DeclSpec::TST_auto: {
258 // TypeQuals handled by caller.
259 Result = Context.UndeducedAutoTy;
260 break;
261 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000262
Douglas Gregor809070a2009-02-18 17:45:20 +0000263 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000264 Result = Context.IntTy;
265 isInvalid = true;
266 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 }
Chris Lattner958858e2008-02-20 21:40:32 +0000268
269 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000270 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
271 if (getLangOptions().Freestanding)
272 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000273 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000274 }
Chris Lattner958858e2008-02-20 21:40:32 +0000275
276 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
277 "FIXME: imaginary types not supported yet!");
278
Chris Lattner38d8b982008-02-20 22:04:11 +0000279 // See if there are any attributes on the declspec that apply to the type (as
280 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000281 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000282 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000283
Chris Lattner96b77fc2008-04-02 06:50:17 +0000284 // Apply const/volatile/restrict qualifiers to T.
285 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
286
287 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
288 // or incomplete types shall not be restrict-qualified." C++ also allows
289 // restrict-qualified references.
290 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000291 if (Result->isPointerType() || Result->isReferenceType()) {
292 QualType EltTy = Result->isPointerType() ?
293 Result->getAsPointerType()->getPointeeType() :
294 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000295
Douglas Gregorbad0e652009-03-24 20:32:41 +0000296 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000297 // incomplete type.
298 if (!EltTy->isIncompleteOrObjectType()) {
299 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000300 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000301 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000302 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
303 }
304 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000305 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000306 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000307 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000308 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000309 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000310 }
311
312 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
313 // of a function type includes any type qualifiers, the behavior is
314 // undefined."
315 if (Result->isFunctionType() && TypeQuals) {
316 // Get some location to point at, either the C or V location.
317 SourceLocation Loc;
318 if (TypeQuals & QualType::Const)
319 Loc = DS.getConstSpecLoc();
320 else {
321 assert((TypeQuals & QualType::Volatile) &&
322 "Has CV quals but not C or V?");
323 Loc = DS.getVolatileSpecLoc();
324 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000325 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000326 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000327 }
328
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000329 // C++ [dcl.ref]p1:
330 // Cv-qualified references are ill-formed except when the
331 // cv-qualifiers are introduced through the use of a typedef
332 // (7.1.3) or of a template type argument (14.3), in which
333 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000334 // FIXME: Shouldn't we be checking SCS_typedef here?
335 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000336 TypeQuals && Result->isReferenceType()) {
337 TypeQuals &= ~QualType::Const;
338 TypeQuals &= ~QualType::Volatile;
339 }
340
Chris Lattner96b77fc2008-04-02 06:50:17 +0000341 Result = Result.getQualifiedType(TypeQuals);
342 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000343 return Result;
344}
345
Douglas Gregorcd281c32009-02-28 00:25:32 +0000346static std::string getPrintableNameForEntity(DeclarationName Entity) {
347 if (Entity)
348 return Entity.getAsString();
349
350 return "type name";
351}
352
353/// \brief Build a pointer type.
354///
355/// \param T The type to which we'll be building a pointer.
356///
357/// \param Quals The cvr-qualifiers to be applied to the pointer type.
358///
359/// \param Loc The location of the entity whose type involves this
360/// pointer type or, if there is no such entity, the location of the
361/// type that will have pointer type.
362///
363/// \param Entity The name of the entity that involves the pointer
364/// type, if known.
365///
366/// \returns A suitable pointer type, if there are no
367/// errors. Otherwise, returns a NULL type.
368QualType Sema::BuildPointerType(QualType T, unsigned Quals,
369 SourceLocation Loc, DeclarationName Entity) {
370 if (T->isReferenceType()) {
371 // C++ 8.3.2p4: There shall be no ... pointers to references ...
372 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
373 << getPrintableNameForEntity(Entity);
374 return QualType();
375 }
376
377 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
378 // object or incomplete types shall not be restrict-qualified."
379 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
380 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
381 << T;
382 Quals &= ~QualType::Restrict;
383 }
384
385 // Build the pointer type.
386 return Context.getPointerType(T).getQualifiedType(Quals);
387}
388
389/// \brief Build a reference type.
390///
391/// \param T The type to which we'll be building a reference.
392///
393/// \param Quals The cvr-qualifiers to be applied to the reference type.
394///
395/// \param Loc The location of the entity whose type involves this
396/// reference type or, if there is no such entity, the location of the
397/// type that will have reference type.
398///
399/// \param Entity The name of the entity that involves the reference
400/// type, if known.
401///
402/// \returns A suitable reference type, if there are no
403/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000404QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000405 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000406 if (LValueRef) {
407 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000408 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
409 // reference to a type T, and attempt to create the type "lvalue
410 // reference to cv TD" creates the type "lvalue reference to T".
411 // We use the qualifiers (restrict or none) of the original reference,
412 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000413 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000414 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000415 }
416 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000417 if (T->isReferenceType()) {
418 // C++ [dcl.ref]p4: There shall be no references to references.
419 //
420 // According to C++ DR 106, references to references are only
421 // diagnosed when they are written directly (e.g., "int & &"),
422 // but not when they happen via a typedef:
423 //
424 // typedef int& intref;
425 // typedef intref& intref2;
426 //
427 // Parser::ParserDeclaratorInternal diagnoses the case where
428 // references are written directly; here, we handle the
429 // collapsing of references-to-references as described in C++
430 // DR 106 and amended by C++ DR 540.
431 return T;
432 }
433
434 // C++ [dcl.ref]p1:
435 // A declarator that specifies the type “reference to cv void”
436 // is ill-formed.
437 if (T->isVoidType()) {
438 Diag(Loc, diag::err_reference_to_void);
439 return QualType();
440 }
441
442 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
443 // object or incomplete types shall not be restrict-qualified."
444 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
445 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
446 << T;
447 Quals &= ~QualType::Restrict;
448 }
449
450 // C++ [dcl.ref]p1:
451 // [...] Cv-qualified references are ill-formed except when the
452 // cv-qualifiers are introduced through the use of a typedef
453 // (7.1.3) or of a template type argument (14.3), in which case
454 // the cv-qualifiers are ignored.
455 //
456 // We diagnose extraneous cv-qualifiers for the non-typedef,
457 // non-template type argument case within the parser. Here, we just
458 // ignore any extraneous cv-qualifiers.
459 Quals &= ~QualType::Const;
460 Quals &= ~QualType::Volatile;
461
462 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000463 if (LValueRef)
464 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
465 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000466}
467
468/// \brief Build an array type.
469///
470/// \param T The type of each element in the array.
471///
472/// \param ASM C99 array size modifier (e.g., '*', 'static').
473///
474/// \param ArraySize Expression describing the size of the array.
475///
476/// \param Quals The cvr-qualifiers to be applied to the array's
477/// element type.
478///
479/// \param Loc The location of the entity whose type involves this
480/// array type or, if there is no such entity, the location of the
481/// type that will have array type.
482///
483/// \param Entity The name of the entity that involves the array
484/// type, if known.
485///
486/// \returns A suitable array type, if there are no errors. Otherwise,
487/// returns a NULL type.
488QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
489 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000490 SourceRange Brackets, DeclarationName Entity) {
491 SourceLocation Loc = Brackets.getBegin();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000492 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
493 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000494 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000495 diag::err_illegal_decl_array_incomplete_type))
496 return QualType();
497
498 if (T->isFunctionType()) {
499 Diag(Loc, diag::err_illegal_decl_array_of_functions)
500 << getPrintableNameForEntity(Entity);
501 return QualType();
502 }
503
504 // C++ 8.3.2p4: There shall be no ... arrays of references ...
505 if (T->isReferenceType()) {
506 Diag(Loc, diag::err_illegal_decl_array_of_references)
507 << getPrintableNameForEntity(Entity);
508 return QualType();
509 }
510
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000511 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
512 Diag(Loc, diag::err_illegal_decl_array_of_auto)
513 << getPrintableNameForEntity(Entity);
514 return QualType();
515 }
516
Douglas Gregorcd281c32009-02-28 00:25:32 +0000517 if (const RecordType *EltTy = T->getAsRecordType()) {
518 // If the element type is a struct or union that contains a variadic
519 // array, accept it as a GNU extension: C99 6.7.2.1p2.
520 if (EltTy->getDecl()->hasFlexibleArrayMember())
521 Diag(Loc, diag::ext_flexible_array_in_array) << T;
522 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000523 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
524 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000525 }
526
527 // C99 6.7.5.2p1: The size expression shall have integer type.
528 if (ArraySize && !ArraySize->isTypeDependent() &&
529 !ArraySize->getType()->isIntegerType()) {
530 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
531 << ArraySize->getType() << ArraySize->getSourceRange();
532 ArraySize->Destroy(Context);
533 return QualType();
534 }
535 llvm::APSInt ConstVal(32);
536 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000537 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000538 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000539 else
540 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000541 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000542 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
544 (!T->isDependentType() && !T->isConstantSizeType())) {
545 // Per C99, a variable array is an array with either a non-constant
546 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000547 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548 } else {
549 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
550 // have a value greater than zero.
551 if (ConstVal.isSigned()) {
552 if (ConstVal.isNegative()) {
553 Diag(ArraySize->getLocStart(),
554 diag::err_typecheck_negative_array_size)
555 << ArraySize->getSourceRange();
556 return QualType();
557 } else if (ConstVal == 0) {
558 // GCC accepts zero sized static arrays.
559 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
560 << ArraySize->getSourceRange();
561 }
562 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000563 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
564 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000565 }
566 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
567 if (!getLangOptions().C99) {
568 if (ArraySize && !ArraySize->isTypeDependent() &&
569 !ArraySize->isValueDependent() &&
570 !ArraySize->isIntegerConstantExpr(Context))
571 Diag(Loc, diag::ext_vla);
572 else if (ASM != ArrayType::Normal || Quals != 0)
573 Diag(Loc, diag::ext_c99_array_usage);
574 }
575
576 return T;
577}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000578
579/// \brief Build an ext-vector type.
580///
581/// Run the required checks for the extended vector type.
582QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
583 SourceLocation AttrLoc) {
584
585 Expr *Arg = (Expr *)ArraySize.get();
586
587 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
588 // in conjunction with complex types (pointers, arrays, functions, etc.).
589 if (!T->isDependentType() &&
590 !T->isIntegerType() && !T->isRealFloatingType()) {
591 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
592 return QualType();
593 }
594
595 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
596 llvm::APSInt vecSize(32);
597 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
598 Diag(AttrLoc, diag::err_attribute_argument_not_int)
599 << "ext_vector_type" << Arg->getSourceRange();
600 return QualType();
601 }
602
603 // unlike gcc's vector_size attribute, the size is specified as the
604 // number of elements, not the number of bytes.
605 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
606
607 if (vectorSize == 0) {
608 Diag(AttrLoc, diag::err_attribute_zero_size)
609 << Arg->getSourceRange();
610 return QualType();
611 }
612
613 if (!T->isDependentType())
614 return Context.getExtVectorType(T, vectorSize);
615 }
616
617 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
618 AttrLoc);
619}
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620
Douglas Gregor724651c2009-02-28 01:04:19 +0000621/// \brief Build a function type.
622///
623/// This routine checks the function type according to C++ rules and
624/// under the assumption that the result type and parameter types have
625/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000626/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000627/// simpler form that is only suitable for this narrow use case.
628///
629/// \param T The return type of the function.
630///
631/// \param ParamTypes The parameter types of the function. This array
632/// will be modified to account for adjustments to the types of the
633/// function parameters.
634///
635/// \param NumParamTypes The number of parameter types in ParamTypes.
636///
637/// \param Variadic Whether this is a variadic function type.
638///
639/// \param Quals The cvr-qualifiers to be applied to the function type.
640///
641/// \param Loc The location of the entity whose type involves this
642/// function type or, if there is no such entity, the location of the
643/// type that will have function type.
644///
645/// \param Entity The name of the entity that involves the function
646/// type, if known.
647///
648/// \returns A suitable function type, if there are no
649/// errors. Otherwise, returns a NULL type.
650QualType Sema::BuildFunctionType(QualType T,
651 QualType *ParamTypes,
652 unsigned NumParamTypes,
653 bool Variadic, unsigned Quals,
654 SourceLocation Loc, DeclarationName Entity) {
655 if (T->isArrayType() || T->isFunctionType()) {
656 Diag(Loc, diag::err_func_returning_array_function) << T;
657 return QualType();
658 }
659
660 bool Invalid = false;
661 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000662 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
663 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000664 Diag(Loc, diag::err_param_with_void_type);
665 Invalid = true;
666 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000667
Douglas Gregor724651c2009-02-28 01:04:19 +0000668 ParamTypes[Idx] = ParamType;
669 }
670
671 if (Invalid)
672 return QualType();
673
674 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
675 Quals);
676}
Douglas Gregor949bf692009-06-09 22:17:39 +0000677
678/// \brief Build a member pointer type \c T Class::*.
679///
680/// \param T the type to which the member pointer refers.
681/// \param Class the class type into which the member pointer points.
682/// \param Quals Qualifiers applied to the member pointer type
683/// \param Loc the location where this type begins
684/// \param Entity the name of the entity that will have this member pointer type
685///
686/// \returns a member pointer type, if successful, or a NULL type if there was
687/// an error.
688QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
689 unsigned Quals, SourceLocation Loc,
690 DeclarationName Entity) {
691 // Verify that we're not building a pointer to pointer to function with
692 // exception specification.
693 if (CheckDistantExceptionSpec(T)) {
694 Diag(Loc, diag::err_distant_exception_spec);
695
696 // FIXME: If we're doing this as part of template instantiation,
697 // we should return immediately.
698
699 // Build the type anyway, but use the canonical type so that the
700 // exception specifiers are stripped off.
701 T = Context.getCanonicalType(T);
702 }
703
704 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
705 // with reference type, or "cv void."
706 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000707 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000708 << (Entity? Entity.getAsString() : "type name");
709 return QualType();
710 }
711
712 if (T->isVoidType()) {
713 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
714 << (Entity? Entity.getAsString() : "type name");
715 return QualType();
716 }
717
718 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
719 // object or incomplete types shall not be restrict-qualified."
720 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
721 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
722 << T;
723
724 // FIXME: If we're doing this as part of template instantiation,
725 // we should return immediately.
726 Quals &= ~QualType::Restrict;
727 }
728
729 if (!Class->isDependentType() && !Class->isRecordType()) {
730 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
731 return QualType();
732 }
733
734 return Context.getMemberPointerType(T, Class.getTypePtr())
735 .getQualifiedType(Quals);
736}
Anders Carlsson9a917e42009-06-12 22:56:54 +0000737
738/// \brief Build a block pointer type.
739///
740/// \param T The type to which we'll be building a block pointer.
741///
742/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
743///
744/// \param Loc The location of the entity whose type involves this
745/// block pointer type or, if there is no such entity, the location of the
746/// type that will have block pointer type.
747///
748/// \param Entity The name of the entity that involves the block pointer
749/// type, if known.
750///
751/// \returns A suitable block pointer type, if there are no
752/// errors. Otherwise, returns a NULL type.
753QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
754 SourceLocation Loc,
755 DeclarationName Entity) {
756 if (!T.getTypePtr()->isFunctionType()) {
757 Diag(Loc, diag::err_nonfunction_block_type);
758 return QualType();
759 }
760
761 return Context.getBlockPointerType(T).getQualifiedType(Quals);
762}
763
Mike Stump98eb8a72009-02-04 22:31:32 +0000764/// GetTypeForDeclarator - Convert the type for the specified
765/// declarator to Type instances. Skip the outermost Skip type
766/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000767///
768/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
769/// owns the declaration of a type (e.g., the definition of a struct
770/// type), then *OwnedDecl will receive the owned declaration.
771QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
772 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000773 bool OmittedReturnType = false;
774
775 if (D.getContext() == Declarator::BlockLiteralContext
776 && Skip == 0
777 && !D.getDeclSpec().hasTypeSpecifier()
778 && (D.getNumTypeObjects() == 0
779 || (D.getNumTypeObjects() == 1
780 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
781 OmittedReturnType = true;
782
Chris Lattnerb23deda2007-08-28 16:40:32 +0000783 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000784 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000785 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
786 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000787
788 // Determine the type of the declarator. Not all forms of declarator
789 // have a type.
790 QualType T;
791 switch (D.getKind()) {
792 case Declarator::DK_Abstract:
793 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000794 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000795 const DeclSpec &DS = D.getDeclSpec();
796 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000797 // We default to a dependent type initially. Can be modified by
798 // the first return statement.
799 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000800 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000801 bool isInvalid = false;
802 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000803 if (isInvalid)
804 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000805 else if (OwnedDecl && DS.isTypeSpecOwned())
806 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000807 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000808 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000809 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000810
811 case Declarator::DK_Constructor:
812 case Declarator::DK_Destructor:
813 case Declarator::DK_Conversion:
814 // Constructors and destructors don't have return types. Use
815 // "void" instead. Conversion operators will check their return
816 // types separately.
817 T = Context.VoidTy;
818 break;
819 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000820
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000821 if (T == Context.UndeducedAutoTy) {
822 int Error = -1;
823
824 switch (D.getContext()) {
825 case Declarator::KNRTypeListContext:
826 assert(0 && "K&R type lists aren't allowed in C++");
827 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000828 case Declarator::PrototypeContext:
829 Error = 0; // Function prototype
830 break;
831 case Declarator::MemberContext:
832 switch (cast<TagDecl>(CurContext)->getTagKind()) {
833 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
834 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
835 case TagDecl::TK_union: Error = 2; /* Union member */ break;
836 case TagDecl::TK_class: Error = 3; /* Class member */ break;
837 }
838 break;
839 case Declarator::CXXCatchContext:
840 Error = 4; // Exception declaration
841 break;
842 case Declarator::TemplateParamContext:
843 Error = 5; // Template parameter
844 break;
845 case Declarator::BlockLiteralContext:
846 Error = 6; // Block literal
847 break;
848 case Declarator::FileContext:
849 case Declarator::BlockContext:
850 case Declarator::ForContext:
851 case Declarator::ConditionContext:
852 case Declarator::TypeNameContext:
853 break;
854 }
855
856 if (Error != -1) {
857 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
858 << Error;
859 T = Context.IntTy;
860 D.setInvalidType(true);
861 }
862 }
863
Douglas Gregorcd281c32009-02-28 00:25:32 +0000864 // The name we're declaring, if any.
865 DeclarationName Name;
866 if (D.getIdentifier())
867 Name = D.getIdentifier();
868
Mike Stump98eb8a72009-02-04 22:31:32 +0000869 // Walk the DeclTypeInfo, building the recursive type as we go.
870 // DeclTypeInfos are ordered from the identifier out, which is
871 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000872 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
873 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 switch (DeclType.Kind) {
875 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000876 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000877 // If blocks are disabled, emit an error.
878 if (!LangOpts.Blocks)
879 Diag(DeclType.Loc, diag::err_blocks_disable);
880
Anders Carlsson9a917e42009-06-12 22:56:54 +0000881 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
882 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000883 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000885 // Verify that we're not building a pointer to pointer to function with
886 // exception specification.
887 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
888 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
889 D.setInvalidType(true);
890 // Build the type anyway.
891 }
Steve Naroff14108da2009-07-10 23:34:53 +0000892 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
893 const ObjCInterfaceType *OIT = T->getAsObjCInterfaceType();
894 T = Context.getObjCObjectPointerType(T,
895 (ObjCProtocolDecl **)OIT->qual_begin(),
896 OIT->getNumProtocols());
897 break;
898 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000899 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000901 case DeclaratorChunk::Reference:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000902 // Verify that we're not building a reference to pointer to function with
903 // exception specification.
904 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
905 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
906 D.setInvalidType(true);
907 // Build the type anyway.
908 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000909 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
910 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000911 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 break;
913 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000914 // Verify that we're not building an array of pointers to function with
915 // exception specification.
916 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
917 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
918 D.setInvalidType(true);
919 // Build the type anyway.
920 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000921 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000922 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 ArrayType::ArraySizeModifier ASM;
924 if (ATI.isStar)
925 ASM = ArrayType::Star;
926 else if (ATI.hasStatic)
927 ASM = ArrayType::Static;
928 else
929 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000930 if (ASM == ArrayType::Star &&
931 D.getContext() != Declarator::PrototypeContext) {
932 // FIXME: This check isn't quite right: it allows star in prototypes
933 // for function definitions, and disallows some edge cases detailed
934 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
935 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
936 ASM = ArrayType::Normal;
937 D.setInvalidType(true);
938 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000939 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
940 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 break;
942 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000943 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // If the function declarator has a prototype (i.e. it is not () and
945 // does not have a K&R-style identifier list), then the arguments are part
946 // of the type, otherwise the argument list is ().
947 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +0000948
Chris Lattnercd881292007-12-19 05:31:29 +0000949 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000950 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000951 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000952 T = Context.IntTy;
953 D.setInvalidType(true);
954 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000955
Douglas Gregor402abb52009-05-28 23:31:59 +0000956 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
957 // C++ [dcl.fct]p6:
958 // Types shall not be defined in return or parameter types.
959 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
960 if (Tag->isDefinition())
961 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
962 << Context.getTypeDeclType(Tag);
963 }
964
Sebastian Redl3cc97262009-05-31 11:47:27 +0000965 // Exception specs are not allowed in typedefs. Complain, but add it
966 // anyway.
967 if (FTI.hasExceptionSpec &&
968 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
969 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
970
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000971 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000972 if (getLangOptions().CPlusPlus) {
973 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
974 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +0000975 llvm::SmallVector<QualType, 4> Exceptions;
976 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +0000977 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
978 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
979 // Check that the type is valid for an exception spec, and drop it
980 // if not.
981 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
982 Exceptions.push_back(ET);
983 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000984 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
985 FTI.hasExceptionSpec,
986 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +0000987 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +0000988 } else if (FTI.isVariadic) {
989 // We allow a zero-parameter variadic function in C if the
990 // function is marked with the "overloadable"
991 // attribute. Scan for this attribute now.
992 bool Overloadable = false;
993 for (const AttributeList *Attrs = D.getAttributes();
994 Attrs; Attrs = Attrs->getNext()) {
995 if (Attrs->getKind() == AttributeList::AT_overloadable) {
996 Overloadable = true;
997 break;
998 }
999 }
1000
1001 if (!Overloadable)
1002 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1003 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001004 } else {
1005 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001006 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001007 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001008 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001010 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 } else {
1012 // Otherwise, we have a function with an argument list that is
1013 // potentially variadic.
1014 llvm::SmallVector<QualType, 16> ArgTys;
1015
1016 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001017 ParmVarDecl *Param =
1018 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001019 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001020 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001021
1022 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001023 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001024
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 // Look for 'void'. void is allowed only as a single argument to a
1026 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001027 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001028 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 // If this is something like 'float(int, void)', reject it. 'void'
1030 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1031 // have arguments of incomplete type.
1032 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1033 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001034 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001035 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001036 } else if (FTI.ArgInfo[i].Ident) {
1037 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001039 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001040 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001041 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001042 } else {
1043 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001044 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001045 Diag(DeclType.Loc, diag::err_void_param_qualified);
1046
1047 // Do not add 'void' to the ArgTys list.
1048 break;
1049 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001050 } else if (!FTI.hasPrototype) {
1051 if (ArgTy->isPromotableIntegerType()) {
1052 ArgTy = Context.IntTy;
1053 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1054 if (BTy->getKind() == BuiltinType::Float)
1055 ArgTy = Context.DoubleTy;
1056 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
1058
1059 ArgTys.push_back(ArgTy);
1060 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001061
1062 llvm::SmallVector<QualType, 4> Exceptions;
1063 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +00001064 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1065 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
1066 // Check that the type is valid for an exception spec, and drop it if
1067 // not.
1068 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1069 Exceptions.push_back(ET);
1070 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001071
Jay Foadbeaaccd2009-05-21 09:52:38 +00001072 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001073 FTI.isVariadic, FTI.TypeQuals,
1074 FTI.hasExceptionSpec,
1075 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001076 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 }
1078 break;
1079 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001080 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001081 // Verify that we're not building a pointer to pointer to function with
1082 // exception specification.
1083 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1084 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1085 D.setInvalidType(true);
1086 // Build the type anyway.
1087 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001088 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001089 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001090 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
1091 NestedNameSpecifier *NNS
1092 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1093 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1094 ClsType = QualType(NNS->getAsType(), 0);
1095 } else if (CXXRecordDecl *RD
1096 = dyn_cast_or_null<CXXRecordDecl>(
1097 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001098 ClsType = Context.getTagDeclType(RD);
1099 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001100 Diag(DeclType.Mem.Scope().getBeginLoc(),
1101 diag::err_illegal_decl_mempointer_in_nonclass)
1102 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1103 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001104 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001105 }
1106
Douglas Gregor949bf692009-06-09 22:17:39 +00001107 if (!ClsType.isNull())
1108 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1109 DeclType.Loc, D.getIdentifier());
1110 if (T.isNull()) {
1111 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001112 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001113 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001114 break;
1115 }
1116
Douglas Gregorcd281c32009-02-28 00:25:32 +00001117 if (T.isNull()) {
1118 D.setInvalidType(true);
1119 T = Context.IntTy;
1120 }
1121
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001122 // See if there are any attributes on this declarator chunk.
1123 if (const AttributeList *AL = DeclType.getAttrs())
1124 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001126
1127 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001128 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
1129 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001130
1131 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1132 // for a nonstatic member function, the function type to which a pointer
1133 // to member refers, or the top-level function type of a function typedef
1134 // declaration.
1135 if (FnTy->getTypeQuals() != 0 &&
1136 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001137 ((D.getContext() != Declarator::MemberContext &&
1138 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +00001139 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001140 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001141 if (D.isFunctionDeclarator())
1142 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1143 else
1144 Diag(D.getIdentifierLoc(),
1145 diag::err_invalid_qualified_typedef_function_type_use);
1146
1147 // Strip the cv-quals from the type.
1148 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001149 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001150 }
1151 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001152
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001153 // If there were any type attributes applied to the decl itself (not the
1154 // type, apply the type attribute to the type!)
1155 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001156 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001157
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 return T;
1159}
1160
Sebastian Redlef65f062009-05-29 18:02:33 +00001161/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1162/// exception specification. Incomplete types, or pointers to incomplete types
1163/// other than void are not allowed.
1164bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1165 // FIXME: This may not correctly work with the fix for core issue 437,
1166 // where a class's own type is considered complete within its body.
1167
1168 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1169 // an incomplete type.
1170 if (T->isIncompleteType())
1171 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1172 << Range << T << /*direct*/0;
1173
1174 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1175 // an incomplete type a pointer or reference to an incomplete type, other
1176 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001177 int kind;
1178 if (const PointerType* IT = T->getAsPointerType()) {
1179 T = IT->getPointeeType();
1180 kind = 1;
Sebastian Redlef65f062009-05-29 18:02:33 +00001181 } else if (const ReferenceType* IT = T->getAsReferenceType()) {
1182 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001183 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001184 } else
1185 return false;
1186
1187 if (T->isIncompleteType() && !T->isVoidType())
1188 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1189 << Range << T << /*indirect*/kind;
1190
1191 return false;
1192}
1193
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001194/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1195/// to member to a function with an exception specification. This means that
1196/// it is invalid to add another level of indirection.
1197bool Sema::CheckDistantExceptionSpec(QualType T) {
1198 if (const PointerType *PT = T->getAsPointerType())
1199 T = PT->getPointeeType();
1200 else if (const MemberPointerType *PT = T->getAsMemberPointerType())
1201 T = PT->getPointeeType();
1202 else
1203 return false;
1204
1205 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
1206 if (!FnT)
1207 return false;
1208
1209 return FnT->hasExceptionSpec();
1210}
1211
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001212/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1213/// exception specifications. Exception specifications are equivalent if
1214/// they allow exactly the same set of exception types. It does not matter how
1215/// that is achieved. See C++ [except.spec]p2.
1216bool Sema::CheckEquivalentExceptionSpec(
1217 const FunctionProtoType *Old, SourceLocation OldLoc,
1218 const FunctionProtoType *New, SourceLocation NewLoc) {
1219 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1220 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1221 if (OldAny && NewAny)
1222 return false;
1223 if (OldAny || NewAny) {
1224 Diag(NewLoc, diag::err_mismatched_exception_spec);
1225 Diag(OldLoc, diag::note_previous_declaration);
1226 return true;
1227 }
1228
1229 bool Success = true;
1230 // Both have a definite exception spec. Collect the first set, then compare
1231 // to the second.
1232 llvm::SmallPtrSet<const Type*, 8> Types;
1233 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1234 E = Old->exception_end(); I != E; ++I)
1235 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1236
1237 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1238 E = New->exception_end(); I != E && Success; ++I)
1239 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1240
1241 Success = Success && Types.empty();
1242
1243 if (Success) {
1244 return false;
1245 }
1246 Diag(NewLoc, diag::err_mismatched_exception_spec);
1247 Diag(OldLoc, diag::note_previous_declaration);
1248 return true;
1249}
1250
Sebastian Redl23c7d062009-07-07 20:29:57 +00001251/// CheckExceptionSpecSubset - Check whether the second function type's
1252/// exception specification is a subset (or equivalent) of the first function
1253/// type. This is used by override and pointer assignment checks.
1254bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
1255 const FunctionProtoType *Superset, SourceLocation SuperLoc,
1256 const FunctionProtoType *Subset, SourceLocation SubLoc)
1257{
1258 // FIXME: As usual, we could be more specific in our error messages, but
1259 // that better waits until we've got types with source locations.
1260
1261 // If superset contains everything, we're done.
1262 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
1263 return false;
1264
1265 // It does not. If the subset contains everything, we've failed.
1266 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
1267 Diag(SubLoc, DiagID);
1268 Diag(SuperLoc, NoteID);
1269 return true;
1270 }
1271
1272 // Neither contains everything. Do a proper comparison.
1273 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
1274 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
1275 // Take one type from the subset.
1276 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
1277 bool SubIsPointer = false;
1278 if (const ReferenceType *RefTy = CanonicalSubT->getAsReferenceType())
1279 CanonicalSubT = RefTy->getPointeeType();
1280 if (const PointerType *PtrTy = CanonicalSubT->getAsPointerType()) {
1281 CanonicalSubT = PtrTy->getPointeeType();
1282 SubIsPointer = true;
1283 }
1284 bool SubIsClass = CanonicalSubT->isRecordType();
1285 CanonicalSubT.setCVRQualifiers(0);
1286
1287 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
1288 /*DetectVirtual=*/false);
1289
1290 bool Contained = false;
1291 // Make sure it's in the superset.
1292 for (FunctionProtoType::exception_iterator SuperI =
1293 Superset->exception_begin(), SuperE = Superset->exception_end();
1294 SuperI != SuperE; ++SuperI) {
1295 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
1296 // SubT must be SuperT or derived from it, or pointer or reference to
1297 // such types.
1298 if (const ReferenceType *RefTy = CanonicalSuperT->getAsReferenceType())
1299 CanonicalSuperT = RefTy->getPointeeType();
1300 if (SubIsPointer) {
1301 if (const PointerType *PtrTy = CanonicalSuperT->getAsPointerType())
1302 CanonicalSuperT = PtrTy->getPointeeType();
1303 else {
1304 continue;
1305 }
1306 }
1307 CanonicalSuperT.setCVRQualifiers(0);
1308 // If the types are the same, move on to the next type in the subset.
1309 if (CanonicalSubT == CanonicalSuperT) {
1310 Contained = true;
1311 break;
1312 }
1313
1314 // Otherwise we need to check the inheritance.
1315 if (!SubIsClass || !CanonicalSuperT->isRecordType())
1316 continue;
1317
1318 Paths.clear();
1319 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
1320 continue;
1321
1322 if (Paths.isAmbiguous(CanonicalSuperT))
1323 continue;
1324
1325 // FIXME: Check base access. Don't forget to enable path recording.
1326
1327 Contained = true;
1328 break;
1329 }
1330 if (!Contained) {
1331 Diag(SubLoc, DiagID);
1332 Diag(SuperLoc, NoteID);
1333 return true;
1334 }
1335 }
1336 // We've run the gauntlet.
1337 return false;
1338}
1339
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001341/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001342QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1343 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001344 QualType T = MDecl->getResultType();
1345 llvm::SmallVector<QualType, 16> ArgTys;
1346
Fariborz Jahanian35600022007-11-09 17:18:29 +00001347 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001348 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001349 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001350 selfTy = Context.getPointerType(selfTy);
1351 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001352 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001353 ArgTys.push_back(Context.getObjCIdType());
1354 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +00001355
Chris Lattner89951a82009-02-20 18:43:26 +00001356 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1357 E = MDecl->param_end(); PI != E; ++PI) {
1358 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001359 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001360 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001361 ArgTys.push_back(ArgTy);
1362 }
1363 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001364 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001365 return T;
1366}
1367
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001368/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1369/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1370/// they point to and return true. If T1 and T2 aren't pointer types
1371/// or pointer-to-member types, or if they are not similar at this
1372/// level, returns false and leaves T1 and T2 unchanged. Top-level
1373/// qualifiers on T1 and T2 are ignored. This function will typically
1374/// be called in a loop that successively "unwraps" pointer and
1375/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001376bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +00001377 const PointerType *T1PtrType = T1->getAsPointerType(),
1378 *T2PtrType = T2->getAsPointerType();
1379 if (T1PtrType && T2PtrType) {
1380 T1 = T1PtrType->getPointeeType();
1381 T2 = T2PtrType->getPointeeType();
1382 return true;
1383 }
1384
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001385 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
1386 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001387 if (T1MPType && T2MPType &&
1388 Context.getCanonicalType(T1MPType->getClass()) ==
1389 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001390 T1 = T1MPType->getPointeeType();
1391 T2 = T2MPType->getPointeeType();
1392 return true;
1393 }
Douglas Gregor57373262008-10-22 14:17:15 +00001394 return false;
1395}
1396
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001397Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 // C99 6.7.6: Type names have no identifier. This is already validated by
1399 // the parser.
1400 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
1401
Douglas Gregor402abb52009-05-28 23:31:59 +00001402 TagDecl *OwnedTag = 0;
1403 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001404 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001405 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001406
Douglas Gregor402abb52009-05-28 23:31:59 +00001407 if (getLangOptions().CPlusPlus) {
1408 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001409 CheckExtraCXXDefaultArguments(D);
1410
Douglas Gregor402abb52009-05-28 23:31:59 +00001411 // C++0x [dcl.type]p3:
1412 // A type-specifier-seq shall not define a class or enumeration
1413 // unless it appears in the type-id of an alias-declaration
1414 // (7.1.3).
1415 if (OwnedTag && OwnedTag->isDefinition())
1416 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1417 << Context.getTypeDeclType(OwnedTag);
1418 }
1419
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 return T.getAsOpaquePtr();
1421}
1422
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001423
1424
1425//===----------------------------------------------------------------------===//
1426// Type Attribute Processing
1427//===----------------------------------------------------------------------===//
1428
1429/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1430/// specified type. The attribute contains 1 argument, the id of the address
1431/// space for the type.
1432static void HandleAddressSpaceTypeAttribute(QualType &Type,
1433 const AttributeList &Attr, Sema &S){
1434 // If this type is already address space qualified, reject it.
1435 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1436 // for two or more different address spaces."
1437 if (Type.getAddressSpace()) {
1438 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1439 return;
1440 }
1441
1442 // Check the attribute arguments.
1443 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001445 return;
1446 }
1447 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1448 llvm::APSInt addrSpace(32);
1449 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001450 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1451 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001452 return;
1453 }
1454
1455 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001456 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001457}
1458
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001459/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1460/// specified type. The attribute contains 1 argument, weak or strong.
1461static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001462 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001463 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001465 return;
1466 }
1467
1468 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001469 if (!Attr.getParameterName()) {
1470 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1471 << "objc_gc" << 1;
1472 return;
1473 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001474 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001475 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001476 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1477 return;
1478 }
1479 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001480 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001481 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001482 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001483 else {
1484 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1485 << "objc_gc" << Attr.getParameterName();
1486 return;
1487 }
1488
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001489 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001490}
1491
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001492void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001493 // Scan through and apply attributes to this type where it makes sense. Some
1494 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1495 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001496 // apply to the decl. Here we apply type attributes and ignore the rest.
1497 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001498 // If this is an attribute we can handle, do so now, otherwise, add it to
1499 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001500 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001501 default: break;
1502 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001503 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1504 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001505 case AttributeList::AT_objc_gc:
1506 HandleObjCGCTypeAttribute(Result, *AL, *this);
1507 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001508 }
Chris Lattner232e8822008-02-21 01:08:11 +00001509 }
Chris Lattner232e8822008-02-21 01:08:11 +00001510}
1511
Douglas Gregor86447ec2009-03-09 16:13:40 +00001512/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001513///
1514/// This routine checks whether the type @p T is complete in any
1515/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001516/// type, returns false. If @p T is a class template specialization,
1517/// this routine then attempts to perform class template
1518/// instantiation. If instantiation fails, or if @p T is incomplete
1519/// and cannot be completed, issues the diagnostic @p diag (giving it
1520/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001521///
1522/// @param Loc The location in the source that the incomplete type
1523/// diagnostic should refer to.
1524///
1525/// @param T The type that this routine is examining for completeness.
1526///
1527/// @param diag The diagnostic value (e.g.,
1528/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1529/// for the error message if @p T is incomplete.
1530///
1531/// @param Range1 An optional range in the source code that will be a
1532/// part of the "incomplete type" error message.
1533///
1534/// @param Range2 An optional range in the source code that will be a
1535/// part of the "incomplete type" error message.
1536///
1537/// @param PrintType If non-NULL, the type that should be printed
1538/// instead of @p T. This parameter should be used when the type that
1539/// we're checking for incompleteness isn't the type that should be
1540/// displayed to the user, e.g., when T is a type and PrintType is a
1541/// pointer to T.
1542///
1543/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1544/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001545bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001546 SourceRange Range1, SourceRange Range2,
1547 QualType PrintType) {
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001548 // FIXME: Add this assertion to help us flush out problems with
1549 // checking for dependent types and type-dependent expressions.
1550 //
1551 // assert(!T->isDependentType() &&
1552 // "Can't ask whether a dependent type is complete");
1553
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001554 // If we have a complete type, we're done.
1555 if (!T->isIncompleteType())
1556 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001557
Douglas Gregord475b8d2009-03-25 21:17:03 +00001558 // If we have a class template specialization or a class member of a
1559 // class template specialization, try to instantiate it.
1560 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001561 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001562 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001563 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1564 // Update the class template specialization's location to
1565 // refer to the point of instantiation.
1566 if (Loc.isValid())
1567 ClassTemplateSpec->setLocation(Loc);
1568 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1569 /*ExplicitInstantiation=*/false);
1570 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001571 } else if (CXXRecordDecl *Rec
1572 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1573 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1574 // Find the class template specialization that surrounds this
1575 // member class.
1576 ClassTemplateSpecializationDecl *Spec = 0;
1577 for (DeclContext *Parent = Rec->getDeclContext();
1578 Parent && !Spec; Parent = Parent->getParent())
1579 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1580 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001581 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1582 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001583 }
1584 }
1585 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001586
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001587 if (PrintType.isNull())
1588 PrintType = T;
1589
1590 // We have an incomplete type. Produce a diagnostic.
1591 Diag(Loc, diag) << PrintType << Range1 << Range2;
1592
1593 // If the type was a forward declaration of a class/struct/union
1594 // type, produce
1595 const TagType *Tag = 0;
1596 if (const RecordType *Record = T->getAsRecordType())
1597 Tag = Record;
1598 else if (const EnumType *Enum = T->getAsEnumType())
1599 Tag = Enum;
1600
1601 if (Tag && !Tag->getDecl()->isInvalidDecl())
1602 Diag(Tag->getDecl()->getLocation(),
1603 Tag->isBeingDefined() ? diag::note_type_being_defined
1604 : diag::note_forward_declaration)
1605 << QualType(Tag, 0);
1606
1607 return true;
1608}
Douglas Gregore6258932009-03-19 00:39:20 +00001609
1610/// \brief Retrieve a version of the type 'T' that is qualified by the
1611/// nested-name-specifier contained in SS.
1612QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1613 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1614 return T;
1615
Douglas Gregorab452ba2009-03-26 23:50:42 +00001616 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001617 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001618 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001619}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001620
1621QualType Sema::BuildTypeofExprType(Expr *E) {
1622 return Context.getTypeOfExprType(E);
1623}
1624
1625QualType Sema::BuildDecltypeType(Expr *E) {
1626 if (E->getType() == Context.OverloadTy) {
1627 Diag(E->getLocStart(),
1628 diag::err_cannot_determine_declared_type_of_overloaded_function);
1629 return QualType();
1630 }
1631 return Context.getDecltypeType(E);
1632}