blob: 57989a021b5aad30095cc1c5559536f4ee5a68c3 [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;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000091 case DeclSpec::TST_char16:
92 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
93 "Unknown TSS value");
94 Result = Context.Char16Ty;
95 break;
96 case DeclSpec::TST_char32:
97 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
98 "Unknown TSS value");
99 Result = Context.Char32Ty;
100 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000101 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000102 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000103 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000104 Result = Context.getObjCObjectPointerType(QualType(),
105 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000106 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000107 break;
108 }
109
Chris Lattnerd658b562008-04-05 06:32:51 +0000110 // Unspecified typespec defaults to int in C90. However, the C90 grammar
111 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
112 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
113 // Note that the one exception to this is function definitions, which are
114 // allowed to be completely missing a declspec. This is handled in the
115 // parser already though by it pretending to have seen an 'int' in this
116 // case.
117 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000118 // In C89 mode, we only warn if there is a completely missing declspec
119 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000120 if (DS.isEmpty()) {
121 if (DeclLoc.isInvalid())
122 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000123 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000124 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000125 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
126 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000127 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000128 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000129 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
130 // "At least one type specifier shall be given in the declaration
131 // specifiers in each declaration, and in the specifier-qualifier list in
132 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000133 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000134 if (DeclLoc.isInvalid())
135 DeclLoc = DS.getSourceRange().getBegin();
136
Chris Lattnerb78d8332009-06-26 04:45:06 +0000137 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000138 Diag(DeclLoc, diag::err_missing_type_specifier)
139 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000140
141 // When this occurs in C++ code, often something is very broken with the
142 // value being declared, poison it as invalid so we don't get chains of
143 // errors.
144 isInvalid = true;
145 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000146 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000147 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000148 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000149 }
150
151 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000152 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
154 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000155 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
156 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
157 case DeclSpec::TSW_long: Result = Context.LongTy; break;
158 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
161 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000162 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
163 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
164 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
165 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 }
Chris Lattner958858e2008-02-20 21:40:32 +0000168 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000169 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000170 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000171 case DeclSpec::TST_double:
172 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000173 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000174 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000175 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000176 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000177 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 case DeclSpec::TST_decimal32: // _Decimal32
179 case DeclSpec::TST_decimal64: // _Decimal64
180 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000181 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
182 Result = Context.IntTy;
183 isInvalid = true;
184 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000185 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 case DeclSpec::TST_enum:
187 case DeclSpec::TST_union:
188 case DeclSpec::TST_struct: {
189 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000190 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
192 DS.getTypeSpecSign() == 0 &&
193 "Can't handle qualifiers on typedef names yet!");
194 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000195 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000196
197 if (D->isInvalidDecl())
198 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000199 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000201 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
203 DS.getTypeSpecSign() == 0 &&
204 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000205 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000206
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000207 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000208 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
209 // this "hack" for now...
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000210 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000211 // FIXME: Investigate removing the protocol list in ObjCInterfaceType.
212 // To simply this, Sema::GetTypeForDeclarator() uses the declspec
213 // protocol list, not the list we are storing here.
214 Result = Context.getObjCInterfaceType(Interface->getDecl(),
215 (ObjCProtocolDecl**)PQ,
216 DS.getNumProtocolQualifiers());
Steve Naroff14108da2009-07-10 23:34:53 +0000217 else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000218 // id<protocol-list>
Steve Naroff14108da2009-07-10 23:34:53 +0000219 Result = Context.getObjCObjectPointerType(QualType(),
220 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
221 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000222 if (DeclLoc.isInvalid())
223 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000224 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000225 Diag(DeclLoc, diag::err_qualified_class_unsupported)
226 << DS.getSourceRange();
227 } else {
228 if (DeclLoc.isInvalid())
229 DeclLoc = DS.getSourceRange().getBegin();
230 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
231 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000232 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000233 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000234 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000235
236 // If this is a reference to an invalid typedef, propagate the invalidity.
237 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
238 if (TDT->getDecl()->isInvalidDecl())
239 isInvalid = true;
240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000242 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
Chris Lattner958858e2008-02-20 21:40:32 +0000244 case DeclSpec::TST_typeofType:
245 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
246 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000247 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000248 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000249 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000250 case DeclSpec::TST_typeofExpr: {
251 Expr *E = static_cast<Expr *>(DS.getTypeRep());
252 assert(E && "Didn't get an expression for typeof?");
253 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000254 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000255 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000256 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000257 case DeclSpec::TST_decltype: {
258 Expr *E = static_cast<Expr *>(DS.getTypeRep());
259 assert(E && "Didn't get an expression for decltype?");
260 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000261 Result = BuildDecltypeType(E);
262 if (Result.isNull()) {
263 Result = Context.IntTy;
264 isInvalid = true;
265 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000266 break;
267 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000268 case DeclSpec::TST_auto: {
269 // TypeQuals handled by caller.
270 Result = Context.UndeducedAutoTy;
271 break;
272 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000273
Douglas Gregor809070a2009-02-18 17:45:20 +0000274 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000275 Result = Context.IntTy;
276 isInvalid = true;
277 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 }
Chris Lattner958858e2008-02-20 21:40:32 +0000279
280 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000281 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
282 if (getLangOptions().Freestanding)
283 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000284 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000285 }
Chris Lattner958858e2008-02-20 21:40:32 +0000286
287 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
288 "FIXME: imaginary types not supported yet!");
289
Chris Lattner38d8b982008-02-20 22:04:11 +0000290 // See if there are any attributes on the declspec that apply to the type (as
291 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000292 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000293 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000294
Chris Lattner96b77fc2008-04-02 06:50:17 +0000295 // Apply const/volatile/restrict qualifiers to T.
296 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
297
298 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
299 // or incomplete types shall not be restrict-qualified." C++ also allows
300 // restrict-qualified references.
301 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000302 if (Result->isPointerType() || Result->isReferenceType()) {
303 QualType EltTy = Result->isPointerType() ?
Ted Kremenek35366a62009-07-17 17:50:17 +0000304 Result->getAsPointerType()->getPointeeType() :
305 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000306
Douglas Gregorbad0e652009-03-24 20:32:41 +0000307 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000308 // incomplete type.
309 if (!EltTy->isIncompleteOrObjectType()) {
310 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000311 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000312 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000313 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
314 }
315 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000316 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000317 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000318 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000319 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000320 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000321 }
322
323 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
324 // of a function type includes any type qualifiers, the behavior is
325 // undefined."
326 if (Result->isFunctionType() && TypeQuals) {
327 // Get some location to point at, either the C or V location.
328 SourceLocation Loc;
329 if (TypeQuals & QualType::Const)
330 Loc = DS.getConstSpecLoc();
331 else {
332 assert((TypeQuals & QualType::Volatile) &&
333 "Has CV quals but not C or V?");
334 Loc = DS.getVolatileSpecLoc();
335 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000336 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000337 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000338 }
339
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000340 // C++ [dcl.ref]p1:
341 // Cv-qualified references are ill-formed except when the
342 // cv-qualifiers are introduced through the use of a typedef
343 // (7.1.3) or of a template type argument (14.3), in which
344 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000345 // FIXME: Shouldn't we be checking SCS_typedef here?
346 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000347 TypeQuals && Result->isReferenceType()) {
348 TypeQuals &= ~QualType::Const;
349 TypeQuals &= ~QualType::Volatile;
350 }
351
Chris Lattner96b77fc2008-04-02 06:50:17 +0000352 Result = Result.getQualifiedType(TypeQuals);
353 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000354 return Result;
355}
356
Douglas Gregorcd281c32009-02-28 00:25:32 +0000357static std::string getPrintableNameForEntity(DeclarationName Entity) {
358 if (Entity)
359 return Entity.getAsString();
360
361 return "type name";
362}
363
364/// \brief Build a pointer type.
365///
366/// \param T The type to which we'll be building a pointer.
367///
368/// \param Quals The cvr-qualifiers to be applied to the pointer type.
369///
370/// \param Loc The location of the entity whose type involves this
371/// pointer type or, if there is no such entity, the location of the
372/// type that will have pointer type.
373///
374/// \param Entity The name of the entity that involves the pointer
375/// type, if known.
376///
377/// \returns A suitable pointer type, if there are no
378/// errors. Otherwise, returns a NULL type.
379QualType Sema::BuildPointerType(QualType T, unsigned Quals,
380 SourceLocation Loc, DeclarationName Entity) {
381 if (T->isReferenceType()) {
382 // C++ 8.3.2p4: There shall be no ... pointers to references ...
383 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
384 << getPrintableNameForEntity(Entity);
385 return QualType();
386 }
387
388 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
389 // object or incomplete types shall not be restrict-qualified."
390 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
391 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
392 << T;
393 Quals &= ~QualType::Restrict;
394 }
395
396 // Build the pointer type.
397 return Context.getPointerType(T).getQualifiedType(Quals);
398}
399
400/// \brief Build a reference type.
401///
402/// \param T The type to which we'll be building a reference.
403///
404/// \param Quals The cvr-qualifiers to be applied to the reference type.
405///
406/// \param Loc The location of the entity whose type involves this
407/// reference type or, if there is no such entity, the location of the
408/// type that will have reference type.
409///
410/// \param Entity The name of the entity that involves the reference
411/// type, if known.
412///
413/// \returns A suitable reference type, if there are no
414/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000415QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000416 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000417 if (LValueRef) {
Ted Kremenek35366a62009-07-17 17:50:17 +0000418 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000419 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
420 // reference to a type T, and attempt to create the type "lvalue
421 // reference to cv TD" creates the type "lvalue reference to T".
422 // We use the qualifiers (restrict or none) of the original reference,
423 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000424 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000425 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000426 }
427 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000428 if (T->isReferenceType()) {
429 // C++ [dcl.ref]p4: There shall be no references to references.
430 //
431 // According to C++ DR 106, references to references are only
432 // diagnosed when they are written directly (e.g., "int & &"),
433 // but not when they happen via a typedef:
434 //
435 // typedef int& intref;
436 // typedef intref& intref2;
437 //
438 // Parser::ParserDeclaratorInternal diagnoses the case where
439 // references are written directly; here, we handle the
440 // collapsing of references-to-references as described in C++
441 // DR 106 and amended by C++ DR 540.
442 return T;
443 }
444
445 // C++ [dcl.ref]p1:
446 // A declarator that specifies the type “reference to cv void”
447 // is ill-formed.
448 if (T->isVoidType()) {
449 Diag(Loc, diag::err_reference_to_void);
450 return QualType();
451 }
452
453 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
454 // object or incomplete types shall not be restrict-qualified."
455 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
456 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
457 << T;
458 Quals &= ~QualType::Restrict;
459 }
460
461 // C++ [dcl.ref]p1:
462 // [...] Cv-qualified references are ill-formed except when the
463 // cv-qualifiers are introduced through the use of a typedef
464 // (7.1.3) or of a template type argument (14.3), in which case
465 // the cv-qualifiers are ignored.
466 //
467 // We diagnose extraneous cv-qualifiers for the non-typedef,
468 // non-template type argument case within the parser. Here, we just
469 // ignore any extraneous cv-qualifiers.
470 Quals &= ~QualType::Const;
471 Quals &= ~QualType::Volatile;
472
473 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000474 if (LValueRef)
475 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
476 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000477}
478
479/// \brief Build an array type.
480///
481/// \param T The type of each element in the array.
482///
483/// \param ASM C99 array size modifier (e.g., '*', 'static').
484///
485/// \param ArraySize Expression describing the size of the array.
486///
487/// \param Quals The cvr-qualifiers to be applied to the array's
488/// element type.
489///
490/// \param Loc The location of the entity whose type involves this
491/// array type or, if there is no such entity, the location of the
492/// type that will have array type.
493///
494/// \param Entity The name of the entity that involves the array
495/// type, if known.
496///
497/// \returns A suitable array type, if there are no errors. Otherwise,
498/// returns a NULL type.
499QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
500 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000501 SourceRange Brackets, DeclarationName Entity) {
502 SourceLocation Loc = Brackets.getBegin();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000503 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
504 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000505 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000506 diag::err_illegal_decl_array_incomplete_type))
507 return QualType();
508
509 if (T->isFunctionType()) {
510 Diag(Loc, diag::err_illegal_decl_array_of_functions)
511 << getPrintableNameForEntity(Entity);
512 return QualType();
513 }
514
515 // C++ 8.3.2p4: There shall be no ... arrays of references ...
516 if (T->isReferenceType()) {
517 Diag(Loc, diag::err_illegal_decl_array_of_references)
518 << getPrintableNameForEntity(Entity);
519 return QualType();
520 }
521
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000522 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
523 Diag(Loc, diag::err_illegal_decl_array_of_auto)
524 << getPrintableNameForEntity(Entity);
525 return QualType();
526 }
527
Ted Kremenek35366a62009-07-17 17:50:17 +0000528 if (const RecordType *EltTy = T->getAsRecordType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000529 // If the element type is a struct or union that contains a variadic
530 // array, accept it as a GNU extension: C99 6.7.2.1p2.
531 if (EltTy->getDecl()->hasFlexibleArrayMember())
532 Diag(Loc, diag::ext_flexible_array_in_array) << T;
533 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000534 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
535 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000536 }
537
538 // C99 6.7.5.2p1: The size expression shall have integer type.
539 if (ArraySize && !ArraySize->isTypeDependent() &&
540 !ArraySize->getType()->isIntegerType()) {
541 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
542 << ArraySize->getType() << ArraySize->getSourceRange();
543 ArraySize->Destroy(Context);
544 return QualType();
545 }
546 llvm::APSInt ConstVal(32);
547 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000548 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000549 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000550 else
551 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000552 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000553 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000554 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
555 (!T->isDependentType() && !T->isConstantSizeType())) {
556 // Per C99, a variable array is an array with either a non-constant
557 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000558 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000559 } else {
560 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
561 // have a value greater than zero.
562 if (ConstVal.isSigned()) {
563 if (ConstVal.isNegative()) {
564 Diag(ArraySize->getLocStart(),
565 diag::err_typecheck_negative_array_size)
566 << ArraySize->getSourceRange();
567 return QualType();
568 } else if (ConstVal == 0) {
569 // GCC accepts zero sized static arrays.
570 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
571 << ArraySize->getSourceRange();
572 }
573 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000574 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
575 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000576 }
577 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
578 if (!getLangOptions().C99) {
579 if (ArraySize && !ArraySize->isTypeDependent() &&
580 !ArraySize->isValueDependent() &&
581 !ArraySize->isIntegerConstantExpr(Context))
582 Diag(Loc, diag::ext_vla);
583 else if (ASM != ArrayType::Normal || Quals != 0)
584 Diag(Loc, diag::ext_c99_array_usage);
585 }
586
587 return T;
588}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000589
590/// \brief Build an ext-vector type.
591///
592/// Run the required checks for the extended vector type.
593QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
594 SourceLocation AttrLoc) {
595
596 Expr *Arg = (Expr *)ArraySize.get();
597
598 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
599 // in conjunction with complex types (pointers, arrays, functions, etc.).
600 if (!T->isDependentType() &&
601 !T->isIntegerType() && !T->isRealFloatingType()) {
602 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
603 return QualType();
604 }
605
606 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
607 llvm::APSInt vecSize(32);
608 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
609 Diag(AttrLoc, diag::err_attribute_argument_not_int)
610 << "ext_vector_type" << Arg->getSourceRange();
611 return QualType();
612 }
613
614 // unlike gcc's vector_size attribute, the size is specified as the
615 // number of elements, not the number of bytes.
616 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
617
618 if (vectorSize == 0) {
619 Diag(AttrLoc, diag::err_attribute_zero_size)
620 << Arg->getSourceRange();
621 return QualType();
622 }
623
624 if (!T->isDependentType())
625 return Context.getExtVectorType(T, vectorSize);
626 }
627
628 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
629 AttrLoc);
630}
Douglas Gregorcd281c32009-02-28 00:25:32 +0000631
Douglas Gregor724651c2009-02-28 01:04:19 +0000632/// \brief Build a function type.
633///
634/// This routine checks the function type according to C++ rules and
635/// under the assumption that the result type and parameter types have
636/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000637/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000638/// simpler form that is only suitable for this narrow use case.
639///
640/// \param T The return type of the function.
641///
642/// \param ParamTypes The parameter types of the function. This array
643/// will be modified to account for adjustments to the types of the
644/// function parameters.
645///
646/// \param NumParamTypes The number of parameter types in ParamTypes.
647///
648/// \param Variadic Whether this is a variadic function type.
649///
650/// \param Quals The cvr-qualifiers to be applied to the function type.
651///
652/// \param Loc The location of the entity whose type involves this
653/// function type or, if there is no such entity, the location of the
654/// type that will have function type.
655///
656/// \param Entity The name of the entity that involves the function
657/// type, if known.
658///
659/// \returns A suitable function type, if there are no
660/// errors. Otherwise, returns a NULL type.
661QualType Sema::BuildFunctionType(QualType T,
662 QualType *ParamTypes,
663 unsigned NumParamTypes,
664 bool Variadic, unsigned Quals,
665 SourceLocation Loc, DeclarationName Entity) {
666 if (T->isArrayType() || T->isFunctionType()) {
667 Diag(Loc, diag::err_func_returning_array_function) << T;
668 return QualType();
669 }
670
671 bool Invalid = false;
672 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000673 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
674 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000675 Diag(Loc, diag::err_param_with_void_type);
676 Invalid = true;
677 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000678
Douglas Gregor724651c2009-02-28 01:04:19 +0000679 ParamTypes[Idx] = ParamType;
680 }
681
682 if (Invalid)
683 return QualType();
684
685 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
686 Quals);
687}
Douglas Gregor949bf692009-06-09 22:17:39 +0000688
689/// \brief Build a member pointer type \c T Class::*.
690///
691/// \param T the type to which the member pointer refers.
692/// \param Class the class type into which the member pointer points.
693/// \param Quals Qualifiers applied to the member pointer type
694/// \param Loc the location where this type begins
695/// \param Entity the name of the entity that will have this member pointer type
696///
697/// \returns a member pointer type, if successful, or a NULL type if there was
698/// an error.
699QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
700 unsigned Quals, SourceLocation Loc,
701 DeclarationName Entity) {
702 // Verify that we're not building a pointer to pointer to function with
703 // exception specification.
704 if (CheckDistantExceptionSpec(T)) {
705 Diag(Loc, diag::err_distant_exception_spec);
706
707 // FIXME: If we're doing this as part of template instantiation,
708 // we should return immediately.
709
710 // Build the type anyway, but use the canonical type so that the
711 // exception specifiers are stripped off.
712 T = Context.getCanonicalType(T);
713 }
714
715 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
716 // with reference type, or "cv void."
717 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000718 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000719 << (Entity? Entity.getAsString() : "type name");
720 return QualType();
721 }
722
723 if (T->isVoidType()) {
724 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
725 << (Entity? Entity.getAsString() : "type name");
726 return QualType();
727 }
728
729 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
730 // object or incomplete types shall not be restrict-qualified."
731 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
732 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
733 << T;
734
735 // FIXME: If we're doing this as part of template instantiation,
736 // we should return immediately.
737 Quals &= ~QualType::Restrict;
738 }
739
740 if (!Class->isDependentType() && !Class->isRecordType()) {
741 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
742 return QualType();
743 }
744
745 return Context.getMemberPointerType(T, Class.getTypePtr())
746 .getQualifiedType(Quals);
747}
Anders Carlsson9a917e42009-06-12 22:56:54 +0000748
749/// \brief Build a block pointer type.
750///
751/// \param T The type to which we'll be building a block pointer.
752///
753/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
754///
755/// \param Loc The location of the entity whose type involves this
756/// block pointer type or, if there is no such entity, the location of the
757/// type that will have block pointer type.
758///
759/// \param Entity The name of the entity that involves the block pointer
760/// type, if known.
761///
762/// \returns A suitable block pointer type, if there are no
763/// errors. Otherwise, returns a NULL type.
764QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
765 SourceLocation Loc,
766 DeclarationName Entity) {
767 if (!T.getTypePtr()->isFunctionType()) {
768 Diag(Loc, diag::err_nonfunction_block_type);
769 return QualType();
770 }
771
772 return Context.getBlockPointerType(T).getQualifiedType(Quals);
773}
774
Mike Stump98eb8a72009-02-04 22:31:32 +0000775/// GetTypeForDeclarator - Convert the type for the specified
776/// declarator to Type instances. Skip the outermost Skip type
777/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000778///
779/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
780/// owns the declaration of a type (e.g., the definition of a struct
781/// type), then *OwnedDecl will receive the owned declaration.
782QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
783 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000784 bool OmittedReturnType = false;
785
786 if (D.getContext() == Declarator::BlockLiteralContext
787 && Skip == 0
788 && !D.getDeclSpec().hasTypeSpecifier()
789 && (D.getNumTypeObjects() == 0
790 || (D.getNumTypeObjects() == 1
791 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
792 OmittedReturnType = true;
793
Chris Lattnerb23deda2007-08-28 16:40:32 +0000794 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000795 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000796 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
797 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000798
799 // Determine the type of the declarator. Not all forms of declarator
800 // have a type.
801 QualType T;
802 switch (D.getKind()) {
803 case Declarator::DK_Abstract:
804 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000805 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000806 const DeclSpec &DS = D.getDeclSpec();
807 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000808 // We default to a dependent type initially. Can be modified by
809 // the first return statement.
810 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000811 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000812 bool isInvalid = false;
813 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000814 if (isInvalid)
815 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000816 else if (OwnedDecl && DS.isTypeSpecOwned())
817 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000818 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000819 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000820 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000821
822 case Declarator::DK_Constructor:
823 case Declarator::DK_Destructor:
824 case Declarator::DK_Conversion:
825 // Constructors and destructors don't have return types. Use
826 // "void" instead. Conversion operators will check their return
827 // types separately.
828 T = Context.VoidTy;
829 break;
830 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000831
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000832 if (T == Context.UndeducedAutoTy) {
833 int Error = -1;
834
835 switch (D.getContext()) {
836 case Declarator::KNRTypeListContext:
837 assert(0 && "K&R type lists aren't allowed in C++");
838 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000839 case Declarator::PrototypeContext:
840 Error = 0; // Function prototype
841 break;
842 case Declarator::MemberContext:
843 switch (cast<TagDecl>(CurContext)->getTagKind()) {
844 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
845 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
846 case TagDecl::TK_union: Error = 2; /* Union member */ break;
847 case TagDecl::TK_class: Error = 3; /* Class member */ break;
848 }
849 break;
850 case Declarator::CXXCatchContext:
851 Error = 4; // Exception declaration
852 break;
853 case Declarator::TemplateParamContext:
854 Error = 5; // Template parameter
855 break;
856 case Declarator::BlockLiteralContext:
857 Error = 6; // Block literal
858 break;
859 case Declarator::FileContext:
860 case Declarator::BlockContext:
861 case Declarator::ForContext:
862 case Declarator::ConditionContext:
863 case Declarator::TypeNameContext:
864 break;
865 }
866
867 if (Error != -1) {
868 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
869 << Error;
870 T = Context.IntTy;
871 D.setInvalidType(true);
872 }
873 }
874
Douglas Gregorcd281c32009-02-28 00:25:32 +0000875 // The name we're declaring, if any.
876 DeclarationName Name;
877 if (D.getIdentifier())
878 Name = D.getIdentifier();
879
Mike Stump98eb8a72009-02-04 22:31:32 +0000880 // Walk the DeclTypeInfo, building the recursive type as we go.
881 // DeclTypeInfos are ordered from the identifier out, which is
882 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000883 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
884 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 switch (DeclType.Kind) {
886 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000887 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000888 // If blocks are disabled, emit an error.
889 if (!LangOpts.Blocks)
890 Diag(DeclType.Loc, diag::err_blocks_disable);
891
Anders Carlsson9a917e42009-06-12 22:56:54 +0000892 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
893 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000894 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000896 // Verify that we're not building a pointer to pointer to function with
897 // exception specification.
898 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
899 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
900 D.setInvalidType(true);
901 // Build the type anyway.
902 }
Steve Naroff14108da2009-07-10 23:34:53 +0000903 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000904 const DeclSpec &DS = D.getDeclSpec();
Steve Naroff14108da2009-07-10 23:34:53 +0000905 T = Context.getObjCObjectPointerType(T,
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000906 (ObjCProtocolDecl **)DS.getProtocolQualifiers(),
907 DS.getNumProtocolQualifiers());
Steve Naroff14108da2009-07-10 23:34:53 +0000908 break;
909 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000910 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000912 case DeclaratorChunk::Reference:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000913 // Verify that we're not building a reference to pointer to function with
914 // exception specification.
915 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
916 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
917 D.setInvalidType(true);
918 // Build the type anyway.
919 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000920 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
921 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000922 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 break;
924 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000925 // Verify that we're not building an array of pointers to function with
926 // exception specification.
927 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
928 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
929 D.setInvalidType(true);
930 // Build the type anyway.
931 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000932 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000933 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 ArrayType::ArraySizeModifier ASM;
935 if (ATI.isStar)
936 ASM = ArrayType::Star;
937 else if (ATI.hasStatic)
938 ASM = ArrayType::Static;
939 else
940 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000941 if (ASM == ArrayType::Star &&
942 D.getContext() != Declarator::PrototypeContext) {
943 // FIXME: This check isn't quite right: it allows star in prototypes
944 // for function definitions, and disallows some edge cases detailed
945 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
946 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
947 ASM = ArrayType::Normal;
948 D.setInvalidType(true);
949 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000950 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
951 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 break;
953 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000954 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // If the function declarator has a prototype (i.e. it is not () and
956 // does not have a K&R-style identifier list), then the arguments are part
957 // of the type, otherwise the argument list is ().
958 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +0000959
Chris Lattnercd881292007-12-19 05:31:29 +0000960 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000961 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000962 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000963 T = Context.IntTy;
964 D.setInvalidType(true);
965 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000966
Douglas Gregor402abb52009-05-28 23:31:59 +0000967 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
968 // C++ [dcl.fct]p6:
969 // Types shall not be defined in return or parameter types.
970 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
971 if (Tag->isDefinition())
972 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
973 << Context.getTypeDeclType(Tag);
974 }
975
Sebastian Redl3cc97262009-05-31 11:47:27 +0000976 // Exception specs are not allowed in typedefs. Complain, but add it
977 // anyway.
978 if (FTI.hasExceptionSpec &&
979 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
980 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
981
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000982 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000983 if (getLangOptions().CPlusPlus) {
984 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
985 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +0000986 llvm::SmallVector<QualType, 4> Exceptions;
987 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +0000988 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
989 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
990 // Check that the type is valid for an exception spec, and drop it
991 // if not.
992 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
993 Exceptions.push_back(ET);
994 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000995 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
996 FTI.hasExceptionSpec,
997 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +0000998 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +0000999 } else if (FTI.isVariadic) {
1000 // We allow a zero-parameter variadic function in C if the
1001 // function is marked with the "overloadable"
1002 // attribute. Scan for this attribute now.
1003 bool Overloadable = false;
1004 for (const AttributeList *Attrs = D.getAttributes();
1005 Attrs; Attrs = Attrs->getNext()) {
1006 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1007 Overloadable = true;
1008 break;
1009 }
1010 }
1011
1012 if (!Overloadable)
1013 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1014 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001015 } else {
1016 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001017 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001018 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001019 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001021 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 } else {
1023 // Otherwise, we have a function with an argument list that is
1024 // potentially variadic.
1025 llvm::SmallVector<QualType, 16> ArgTys;
1026
1027 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001028 ParmVarDecl *Param =
1029 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001030 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001031 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001032
1033 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001034 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001035
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 // Look for 'void'. void is allowed only as a single argument to a
1037 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001038 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001039 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 // If this is something like 'float(int, void)', reject it. 'void'
1041 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1042 // have arguments of incomplete type.
1043 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1044 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001045 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001046 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001047 } else if (FTI.ArgInfo[i].Ident) {
1048 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001050 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001051 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001052 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001053 } else {
1054 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001055 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001056 Diag(DeclType.Loc, diag::err_void_param_qualified);
1057
1058 // Do not add 'void' to the ArgTys list.
1059 break;
1060 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001061 } else if (!FTI.hasPrototype) {
1062 if (ArgTy->isPromotableIntegerType()) {
1063 ArgTy = Context.IntTy;
1064 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1065 if (BTy->getKind() == BuiltinType::Float)
1066 ArgTy = Context.DoubleTy;
1067 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 }
1069
1070 ArgTys.push_back(ArgTy);
1071 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001072
1073 llvm::SmallVector<QualType, 4> Exceptions;
1074 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +00001075 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1076 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
1077 // Check that the type is valid for an exception spec, and drop it if
1078 // not.
1079 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1080 Exceptions.push_back(ET);
1081 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001082
Jay Foadbeaaccd2009-05-21 09:52:38 +00001083 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001084 FTI.isVariadic, FTI.TypeQuals,
1085 FTI.hasExceptionSpec,
1086 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001087 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 }
1089 break;
1090 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001091 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001092 // Verify that we're not building a pointer to pointer to function with
1093 // exception specification.
1094 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1095 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1096 D.setInvalidType(true);
1097 // Build the type anyway.
1098 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001099 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001100 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001101 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
1102 NestedNameSpecifier *NNS
1103 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1104 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1105 ClsType = QualType(NNS->getAsType(), 0);
1106 } else if (CXXRecordDecl *RD
1107 = dyn_cast_or_null<CXXRecordDecl>(
1108 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001109 ClsType = Context.getTagDeclType(RD);
1110 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001111 Diag(DeclType.Mem.Scope().getBeginLoc(),
1112 diag::err_illegal_decl_mempointer_in_nonclass)
1113 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1114 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001115 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001116 }
1117
Douglas Gregor949bf692009-06-09 22:17:39 +00001118 if (!ClsType.isNull())
1119 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1120 DeclType.Loc, D.getIdentifier());
1121 if (T.isNull()) {
1122 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001123 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001124 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001125 break;
1126 }
1127
Douglas Gregorcd281c32009-02-28 00:25:32 +00001128 if (T.isNull()) {
1129 D.setInvalidType(true);
1130 T = Context.IntTy;
1131 }
1132
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001133 // See if there are any attributes on this declarator chunk.
1134 if (const AttributeList *AL = DeclType.getAttrs())
1135 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001137
1138 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001139 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
1140 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001141
1142 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1143 // for a nonstatic member function, the function type to which a pointer
1144 // to member refers, or the top-level function type of a function typedef
1145 // declaration.
1146 if (FnTy->getTypeQuals() != 0 &&
1147 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001148 ((D.getContext() != Declarator::MemberContext &&
1149 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +00001150 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001151 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001152 if (D.isFunctionDeclarator())
1153 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1154 else
1155 Diag(D.getIdentifierLoc(),
1156 diag::err_invalid_qualified_typedef_function_type_use);
1157
1158 // Strip the cv-quals from the type.
1159 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001160 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001161 }
1162 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001164 // If there were any type attributes applied to the decl itself (not the
1165 // type, apply the type attribute to the type!)
1166 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001167 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 return T;
1170}
1171
Sebastian Redlef65f062009-05-29 18:02:33 +00001172/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1173/// exception specification. Incomplete types, or pointers to incomplete types
1174/// other than void are not allowed.
1175bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1176 // FIXME: This may not correctly work with the fix for core issue 437,
1177 // where a class's own type is considered complete within its body.
1178
1179 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1180 // an incomplete type.
1181 if (T->isIncompleteType())
1182 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1183 << Range << T << /*direct*/0;
1184
1185 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1186 // an incomplete type a pointer or reference to an incomplete type, other
1187 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001188 int kind;
Ted Kremenek35366a62009-07-17 17:50:17 +00001189 if (const PointerType* IT = T->getAsPointerType()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001190 T = IT->getPointeeType();
1191 kind = 1;
Ted Kremenek35366a62009-07-17 17:50:17 +00001192 } else if (const ReferenceType* IT = T->getAsReferenceType()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001193 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001194 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001195 } else
1196 return false;
1197
1198 if (T->isIncompleteType() && !T->isVoidType())
1199 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1200 << Range << T << /*indirect*/kind;
1201
1202 return false;
1203}
1204
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001205/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1206/// to member to a function with an exception specification. This means that
1207/// it is invalid to add another level of indirection.
1208bool Sema::CheckDistantExceptionSpec(QualType T) {
Ted Kremenek35366a62009-07-17 17:50:17 +00001209 if (const PointerType *PT = T->getAsPointerType())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001210 T = PT->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +00001211 else if (const MemberPointerType *PT = T->getAsMemberPointerType())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001212 T = PT->getPointeeType();
1213 else
1214 return false;
1215
1216 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
1217 if (!FnT)
1218 return false;
1219
1220 return FnT->hasExceptionSpec();
1221}
1222
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001223/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1224/// exception specifications. Exception specifications are equivalent if
1225/// they allow exactly the same set of exception types. It does not matter how
1226/// that is achieved. See C++ [except.spec]p2.
1227bool Sema::CheckEquivalentExceptionSpec(
1228 const FunctionProtoType *Old, SourceLocation OldLoc,
1229 const FunctionProtoType *New, SourceLocation NewLoc) {
1230 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1231 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1232 if (OldAny && NewAny)
1233 return false;
1234 if (OldAny || NewAny) {
1235 Diag(NewLoc, diag::err_mismatched_exception_spec);
1236 Diag(OldLoc, diag::note_previous_declaration);
1237 return true;
1238 }
1239
1240 bool Success = true;
1241 // Both have a definite exception spec. Collect the first set, then compare
1242 // to the second.
1243 llvm::SmallPtrSet<const Type*, 8> Types;
1244 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1245 E = Old->exception_end(); I != E; ++I)
1246 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1247
1248 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1249 E = New->exception_end(); I != E && Success; ++I)
1250 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1251
1252 Success = Success && Types.empty();
1253
1254 if (Success) {
1255 return false;
1256 }
1257 Diag(NewLoc, diag::err_mismatched_exception_spec);
1258 Diag(OldLoc, diag::note_previous_declaration);
1259 return true;
1260}
1261
Sebastian Redl23c7d062009-07-07 20:29:57 +00001262/// CheckExceptionSpecSubset - Check whether the second function type's
1263/// exception specification is a subset (or equivalent) of the first function
1264/// type. This is used by override and pointer assignment checks.
1265bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
1266 const FunctionProtoType *Superset, SourceLocation SuperLoc,
1267 const FunctionProtoType *Subset, SourceLocation SubLoc)
1268{
1269 // FIXME: As usual, we could be more specific in our error messages, but
1270 // that better waits until we've got types with source locations.
1271
1272 // If superset contains everything, we're done.
1273 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
1274 return false;
1275
1276 // It does not. If the subset contains everything, we've failed.
1277 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
1278 Diag(SubLoc, DiagID);
1279 Diag(SuperLoc, NoteID);
1280 return true;
1281 }
1282
1283 // Neither contains everything. Do a proper comparison.
1284 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
1285 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
1286 // Take one type from the subset.
1287 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
1288 bool SubIsPointer = false;
Ted Kremenek35366a62009-07-17 17:50:17 +00001289 if (const ReferenceType *RefTy = CanonicalSubT->getAsReferenceType())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001290 CanonicalSubT = RefTy->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +00001291 if (const PointerType *PtrTy = CanonicalSubT->getAsPointerType()) {
Sebastian Redl23c7d062009-07-07 20:29:57 +00001292 CanonicalSubT = PtrTy->getPointeeType();
1293 SubIsPointer = true;
1294 }
1295 bool SubIsClass = CanonicalSubT->isRecordType();
1296 CanonicalSubT.setCVRQualifiers(0);
1297
Sebastian Redl726212f2009-07-18 14:32:15 +00001298 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl23c7d062009-07-07 20:29:57 +00001299 /*DetectVirtual=*/false);
1300
1301 bool Contained = false;
1302 // Make sure it's in the superset.
1303 for (FunctionProtoType::exception_iterator SuperI =
1304 Superset->exception_begin(), SuperE = Superset->exception_end();
1305 SuperI != SuperE; ++SuperI) {
1306 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
1307 // SubT must be SuperT or derived from it, or pointer or reference to
1308 // such types.
Ted Kremenek35366a62009-07-17 17:50:17 +00001309 if (const ReferenceType *RefTy = CanonicalSuperT->getAsReferenceType())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001310 CanonicalSuperT = RefTy->getPointeeType();
1311 if (SubIsPointer) {
Ted Kremenek35366a62009-07-17 17:50:17 +00001312 if (const PointerType *PtrTy = CanonicalSuperT->getAsPointerType())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001313 CanonicalSuperT = PtrTy->getPointeeType();
1314 else {
1315 continue;
1316 }
1317 }
1318 CanonicalSuperT.setCVRQualifiers(0);
1319 // If the types are the same, move on to the next type in the subset.
1320 if (CanonicalSubT == CanonicalSuperT) {
1321 Contained = true;
1322 break;
1323 }
1324
1325 // Otherwise we need to check the inheritance.
1326 if (!SubIsClass || !CanonicalSuperT->isRecordType())
1327 continue;
1328
1329 Paths.clear();
1330 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
1331 continue;
1332
1333 if (Paths.isAmbiguous(CanonicalSuperT))
1334 continue;
1335
Sebastian Redl726212f2009-07-18 14:32:15 +00001336 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
1337 continue;
Sebastian Redl23c7d062009-07-07 20:29:57 +00001338
1339 Contained = true;
1340 break;
1341 }
1342 if (!Contained) {
1343 Diag(SubLoc, DiagID);
1344 Diag(SuperLoc, NoteID);
1345 return true;
1346 }
1347 }
1348 // We've run the gauntlet.
1349 return false;
1350}
1351
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001352/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001353/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001354QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1355 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001356 QualType T = MDecl->getResultType();
1357 llvm::SmallVector<QualType, 16> ArgTys;
1358
Fariborz Jahanian35600022007-11-09 17:18:29 +00001359 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001360 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001361 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001362 selfTy = Context.getPointerType(selfTy);
1363 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001364 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001365 ArgTys.push_back(Context.getObjCIdType());
1366 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +00001367
Chris Lattner89951a82009-02-20 18:43:26 +00001368 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1369 E = MDecl->param_end(); PI != E; ++PI) {
1370 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001371 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001372 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001373 ArgTys.push_back(ArgTy);
1374 }
1375 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001376 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001377 return T;
1378}
1379
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001380/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1381/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1382/// they point to and return true. If T1 and T2 aren't pointer types
1383/// or pointer-to-member types, or if they are not similar at this
1384/// level, returns false and leaves T1 and T2 unchanged. Top-level
1385/// qualifiers on T1 and T2 are ignored. This function will typically
1386/// be called in a loop that successively "unwraps" pointer and
1387/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001388bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek35366a62009-07-17 17:50:17 +00001389 const PointerType *T1PtrType = T1->getAsPointerType(),
1390 *T2PtrType = T2->getAsPointerType();
Douglas Gregor57373262008-10-22 14:17:15 +00001391 if (T1PtrType && T2PtrType) {
1392 T1 = T1PtrType->getPointeeType();
1393 T2 = T2PtrType->getPointeeType();
1394 return true;
1395 }
1396
Ted Kremenek35366a62009-07-17 17:50:17 +00001397 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
1398 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001399 if (T1MPType && T2MPType &&
1400 Context.getCanonicalType(T1MPType->getClass()) ==
1401 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001402 T1 = T1MPType->getPointeeType();
1403 T2 = T2MPType->getPointeeType();
1404 return true;
1405 }
Douglas Gregor57373262008-10-22 14:17:15 +00001406 return false;
1407}
1408
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001409Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 // C99 6.7.6: Type names have no identifier. This is already validated by
1411 // the parser.
1412 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
1413
Douglas Gregor402abb52009-05-28 23:31:59 +00001414 TagDecl *OwnedTag = 0;
1415 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001416 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001417 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001418
Douglas Gregor402abb52009-05-28 23:31:59 +00001419 if (getLangOptions().CPlusPlus) {
1420 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001421 CheckExtraCXXDefaultArguments(D);
1422
Douglas Gregor402abb52009-05-28 23:31:59 +00001423 // C++0x [dcl.type]p3:
1424 // A type-specifier-seq shall not define a class or enumeration
1425 // unless it appears in the type-id of an alias-declaration
1426 // (7.1.3).
1427 if (OwnedTag && OwnedTag->isDefinition())
1428 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1429 << Context.getTypeDeclType(OwnedTag);
1430 }
1431
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 return T.getAsOpaquePtr();
1433}
1434
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001435
1436
1437//===----------------------------------------------------------------------===//
1438// Type Attribute Processing
1439//===----------------------------------------------------------------------===//
1440
1441/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1442/// specified type. The attribute contains 1 argument, the id of the address
1443/// space for the type.
1444static void HandleAddressSpaceTypeAttribute(QualType &Type,
1445 const AttributeList &Attr, Sema &S){
1446 // If this type is already address space qualified, reject it.
1447 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1448 // for two or more different address spaces."
1449 if (Type.getAddressSpace()) {
1450 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1451 return;
1452 }
1453
1454 // Check the attribute arguments.
1455 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001457 return;
1458 }
1459 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1460 llvm::APSInt addrSpace(32);
1461 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001462 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1463 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001464 return;
1465 }
1466
1467 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001468 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001469}
1470
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001471/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1472/// specified type. The attribute contains 1 argument, weak or strong.
1473static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001474 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001475 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001476 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001477 return;
1478 }
1479
1480 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001481 if (!Attr.getParameterName()) {
1482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1483 << "objc_gc" << 1;
1484 return;
1485 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001486 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001487 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1489 return;
1490 }
1491 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001492 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001493 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001494 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001495 else {
1496 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1497 << "objc_gc" << Attr.getParameterName();
1498 return;
1499 }
1500
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001501 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001502}
1503
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001504void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001505 // Scan through and apply attributes to this type where it makes sense. Some
1506 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1507 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001508 // apply to the decl. Here we apply type attributes and ignore the rest.
1509 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001510 // If this is an attribute we can handle, do so now, otherwise, add it to
1511 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001512 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001513 default: break;
1514 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001515 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1516 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001517 case AttributeList::AT_objc_gc:
1518 HandleObjCGCTypeAttribute(Result, *AL, *this);
1519 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001520 }
Chris Lattner232e8822008-02-21 01:08:11 +00001521 }
Chris Lattner232e8822008-02-21 01:08:11 +00001522}
1523
Douglas Gregor86447ec2009-03-09 16:13:40 +00001524/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001525///
1526/// This routine checks whether the type @p T is complete in any
1527/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001528/// type, returns false. If @p T is a class template specialization,
1529/// this routine then attempts to perform class template
1530/// instantiation. If instantiation fails, or if @p T is incomplete
1531/// and cannot be completed, issues the diagnostic @p diag (giving it
1532/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001533///
1534/// @param Loc The location in the source that the incomplete type
1535/// diagnostic should refer to.
1536///
1537/// @param T The type that this routine is examining for completeness.
1538///
1539/// @param diag The diagnostic value (e.g.,
1540/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1541/// for the error message if @p T is incomplete.
1542///
1543/// @param Range1 An optional range in the source code that will be a
1544/// part of the "incomplete type" error message.
1545///
1546/// @param Range2 An optional range in the source code that will be a
1547/// part of the "incomplete type" error message.
1548///
1549/// @param PrintType If non-NULL, the type that should be printed
1550/// instead of @p T. This parameter should be used when the type that
1551/// we're checking for incompleteness isn't the type that should be
1552/// displayed to the user, e.g., when T is a type and PrintType is a
1553/// pointer to T.
1554///
1555/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1556/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001557bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001558 SourceRange Range1, SourceRange Range2,
1559 QualType PrintType) {
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001560 // FIXME: Add this assertion to help us flush out problems with
1561 // checking for dependent types and type-dependent expressions.
1562 //
1563 // assert(!T->isDependentType() &&
1564 // "Can't ask whether a dependent type is complete");
1565
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001566 // If we have a complete type, we're done.
1567 if (!T->isIncompleteType())
1568 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001569
Douglas Gregord475b8d2009-03-25 21:17:03 +00001570 // If we have a class template specialization or a class member of a
1571 // class template specialization, try to instantiate it.
Ted Kremenek35366a62009-07-17 17:50:17 +00001572 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001573 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001574 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001575 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1576 // Update the class template specialization's location to
1577 // refer to the point of instantiation.
1578 if (Loc.isValid())
1579 ClassTemplateSpec->setLocation(Loc);
1580 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1581 /*ExplicitInstantiation=*/false);
1582 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001583 } else if (CXXRecordDecl *Rec
1584 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1585 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1586 // Find the class template specialization that surrounds this
1587 // member class.
1588 ClassTemplateSpecializationDecl *Spec = 0;
1589 for (DeclContext *Parent = Rec->getDeclContext();
1590 Parent && !Spec; Parent = Parent->getParent())
1591 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1592 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001593 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1594 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001595 }
1596 }
1597 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001598
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001599 if (PrintType.isNull())
1600 PrintType = T;
1601
1602 // We have an incomplete type. Produce a diagnostic.
1603 Diag(Loc, diag) << PrintType << Range1 << Range2;
1604
1605 // If the type was a forward declaration of a class/struct/union
1606 // type, produce
1607 const TagType *Tag = 0;
Ted Kremenek35366a62009-07-17 17:50:17 +00001608 if (const RecordType *Record = T->getAsRecordType())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001609 Tag = Record;
1610 else if (const EnumType *Enum = T->getAsEnumType())
1611 Tag = Enum;
1612
1613 if (Tag && !Tag->getDecl()->isInvalidDecl())
1614 Diag(Tag->getDecl()->getLocation(),
1615 Tag->isBeingDefined() ? diag::note_type_being_defined
1616 : diag::note_forward_declaration)
1617 << QualType(Tag, 0);
1618
1619 return true;
1620}
Douglas Gregore6258932009-03-19 00:39:20 +00001621
1622/// \brief Retrieve a version of the type 'T' that is qualified by the
1623/// nested-name-specifier contained in SS.
1624QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1625 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1626 return T;
1627
Douglas Gregorab452ba2009-03-26 23:50:42 +00001628 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001629 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001630 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001631}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001632
1633QualType Sema::BuildTypeofExprType(Expr *E) {
1634 return Context.getTypeOfExprType(E);
1635}
1636
1637QualType Sema::BuildDecltypeType(Expr *E) {
1638 if (E->getType() == Context.OverloadTy) {
1639 Diag(E->getLocStart(),
1640 diag::err_cannot_determine_declared_type_of_overloaded_function);
1641 return QualType();
1642 }
1643 return Context.getDecltypeType(E);
1644}