blob: e6aafdab08908b83275c2fc17a26dcdf88b6fd0f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Douglas Gregord45210d2009-01-30 22:09:00 +000021/// \brief Convert the specified declspec to the appropriate type
22/// object.
23/// \param DS the declaration specifiers
24/// \returns The type described by the declaration specifiers, or NULL
25/// if there was an error.
Chris Lattner99dbc962008-06-26 06:27:57 +000026QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000027 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
28 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000029 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000030
31 switch (DS.getTypeSpecType()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000032 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner5fcb38b2008-04-02 06:50:17 +000033 case DeclSpec::TST_void:
34 Result = Context.VoidTy;
35 break;
Chris Lattner4b009652007-07-25 00:24:17 +000036 case DeclSpec::TST_char:
37 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000038 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000039 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000040 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000041 else {
42 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
43 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000044 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000045 }
Chris Lattner06fb8672008-02-20 21:40:32 +000046 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000047 case DeclSpec::TST_wchar:
48 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
49 Result = Context.WCharTy;
50 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +000051 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
52 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000053 Result = Context.getSignedWCharType();
54 } else {
55 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56 "Unknown TSS value");
Chris Lattner10f2c2e2008-11-20 06:38:18 +000057 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
58 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000059 Result = Context.getUnsignedWCharType();
60 }
61 break;
Chris Lattner6ab935b2008-04-05 06:32:51 +000062 case DeclSpec::TST_unspecified:
Chris Lattner4a68fe02008-07-26 00:46:50 +000063 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnercce42242008-10-20 02:01:50 +000064 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerada63792008-07-26 01:53:50 +000065 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner4a68fe02008-07-26 00:46:50 +000066 DS.getNumProtocolQualifiers());
67 break;
68 }
69
Chris Lattner6ab935b2008-04-05 06:32:51 +000070 // Unspecified typespec defaults to int in C90. However, the C90 grammar
71 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
72 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
73 // Note that the one exception to this is function definitions, which are
74 // allowed to be completely missing a declspec. This is handled in the
75 // parser already though by it pretending to have seen an 'int' in this
76 // case.
77 if (getLangOptions().ImplicitInt) {
78 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
79 DeclSpec::PQ_TypeSpecifier |
80 DeclSpec::PQ_TypeQualifier)) == 0)
81 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
Douglas Gregorae17c7a2009-02-16 22:38:20 +000082 } else if (!DS.hasTypeSpecifier()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000083 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
84 // "At least one type specifier shall be given in the declaration
85 // specifiers in each declaration, and in the specifier-qualifier list in
86 // each struct declaration and type name."
Douglas Gregorae17c7a2009-02-16 22:38:20 +000087 // FIXME: Does Microsoft really have the implicit int extension in C++?
88 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
89 diag::err_missing_type_specifier
90 : diag::ext_missing_type_specifier;
91 Diag(DS.getSourceRange().getBegin(), DK);
Chris Lattner6ab935b2008-04-05 06:32:51 +000092 }
93
94 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000095 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000096 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
97 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000098 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
99 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
100 case DeclSpec::TSW_long: Result = Context.LongTy; break;
101 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 }
103 } else {
104 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +0000105 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
106 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
107 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
108 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000109 }
110 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000111 break;
Chris Lattner5328f312007-08-21 17:02:28 +0000112 }
Chris Lattner726c5452008-02-20 23:53:49 +0000113 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +0000114 case DeclSpec::TST_double:
115 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +0000116 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000117 else
Chris Lattner726c5452008-02-20 23:53:49 +0000118 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000119 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000120 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +0000121 case DeclSpec::TST_decimal32: // _Decimal32
122 case DeclSpec::TST_decimal64: // _Decimal64
123 case DeclSpec::TST_decimal128: // _Decimal128
124 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +0000125 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000126 case DeclSpec::TST_enum:
127 case DeclSpec::TST_union:
128 case DeclSpec::TST_struct: {
129 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000130 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000131 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
132 DS.getTypeSpecSign() == 0 &&
133 "Can't handle qualifiers on typedef names yet!");
134 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000135 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000136 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000137 }
Douglas Gregora60c62e2009-02-09 15:09:02 +0000138 case DeclSpec::TST_typename: {
Chris Lattner4b009652007-07-25 00:24:17 +0000139 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
140 DS.getTypeSpecSign() == 0 &&
141 "Can't handle qualifiers on typedef names yet!");
Douglas Gregora60c62e2009-02-09 15:09:02 +0000142 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor1d661552008-04-13 21:07:44 +0000143
Douglas Gregora60c62e2009-02-09 15:09:02 +0000144 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
145 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
146 // we have this "hack" for now...
147 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
148 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
149 (ObjCProtocolDecl**)PQ,
150 DS.getNumProtocolQualifiers());
151 else if (Result == Context.getObjCIdType())
Chris Lattnerada63792008-07-26 01:53:50 +0000152 // id<protocol-list>
153 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
154 DS.getNumProtocolQualifiers());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000155 }
Chris Lattner4b009652007-07-25 00:24:17 +0000156 // TypeQuals handled by caller.
Chris Lattner06fb8672008-02-20 21:40:32 +0000157 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000158 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000159 case DeclSpec::TST_typeofType:
160 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
161 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000162 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000163 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000164 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000165 case DeclSpec::TST_typeofExpr: {
166 Expr *E = static_cast<Expr *>(DS.getTypeRep());
167 assert(E && "Didn't get an expression for typeof?");
168 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000169 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000170 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000171 }
Chris Lattner4b009652007-07-25 00:24:17 +0000172 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000173
174 // Handle complex types.
Douglas Gregor91ca7f02009-02-14 21:06:05 +0000175 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
176 if (getLangOptions().Freestanding)
177 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattner726c5452008-02-20 23:53:49 +0000178 Result = Context.getComplexType(Result);
Douglas Gregor91ca7f02009-02-14 21:06:05 +0000179 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000180
181 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
182 "FIXME: imaginary types not supported yet!");
183
Chris Lattnerd496fb92008-02-20 22:04:11 +0000184 // See if there are any attributes on the declspec that apply to the type (as
185 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000186 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000187 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000188
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000189 // Apply const/volatile/restrict qualifiers to T.
190 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
191
192 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
193 // or incomplete types shall not be restrict-qualified." C++ also allows
194 // restrict-qualified references.
195 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000196 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
197 QualType EltTy = PT->getPointeeType();
198
199 // If we have a pointer or reference, the pointee must have an object or
200 // incomplete type.
201 if (!EltTy->isIncompleteOrObjectType()) {
202 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000203 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000204 << EltTy << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000205 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
206 }
207 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000208 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000209 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000210 << Result << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000211 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000212 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000213 }
214
215 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
216 // of a function type includes any type qualifiers, the behavior is
217 // undefined."
218 if (Result->isFunctionType() && TypeQuals) {
219 // Get some location to point at, either the C or V location.
220 SourceLocation Loc;
221 if (TypeQuals & QualType::Const)
222 Loc = DS.getConstSpecLoc();
223 else {
224 assert((TypeQuals & QualType::Volatile) &&
225 "Has CV quals but not C or V?");
226 Loc = DS.getVolatileSpecLoc();
227 }
Chris Lattner77d52da2008-11-20 06:06:08 +0000228 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000229 << Result << DS.getSourceRange();
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000230 }
231
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000232 // C++ [dcl.ref]p1:
233 // Cv-qualified references are ill-formed except when the
234 // cv-qualifiers are introduced through the use of a typedef
235 // (7.1.3) or of a template type argument (14.3), in which
236 // case the cv-qualifiers are ignored.
Douglas Gregora60c62e2009-02-09 15:09:02 +0000237 // FIXME: Shouldn't we be checking SCS_typedef here?
238 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000239 TypeQuals && Result->isReferenceType()) {
240 TypeQuals &= ~QualType::Const;
241 TypeQuals &= ~QualType::Volatile;
242 }
243
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000244 Result = Result.getQualifiedType(TypeQuals);
245 }
Chris Lattner9e982502008-02-21 01:07:18 +0000246 return Result;
247}
248
Mike Stumpc1fddff2009-02-04 22:31:32 +0000249/// GetTypeForDeclarator - Convert the type for the specified
250/// declarator to Type instances. Skip the outermost Skip type
251/// objects.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000252QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stumpc1fddff2009-02-04 22:31:32 +0000253 bool OmittedReturnType = false;
254
255 if (D.getContext() == Declarator::BlockLiteralContext
256 && Skip == 0
257 && !D.getDeclSpec().hasTypeSpecifier()
258 && (D.getNumTypeObjects() == 0
259 || (D.getNumTypeObjects() == 1
260 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
261 OmittedReturnType = true;
262
Chris Lattner11f20f92007-08-28 16:40:32 +0000263 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000264 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000265 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
266 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregord45210d2009-01-30 22:09:00 +0000267
268 // Determine the type of the declarator. Not all forms of declarator
269 // have a type.
270 QualType T;
271 switch (D.getKind()) {
272 case Declarator::DK_Abstract:
273 case Declarator::DK_Normal:
Mike Stumpc1fddff2009-02-04 22:31:32 +0000274 case Declarator::DK_Operator: {
275 const DeclSpec& DS = D.getDeclSpec();
276 if (OmittedReturnType)
277 // We default to a dependent type initially. Can be modified by
278 // the first return statement.
279 T = Context.DependentTy;
280 else
281 T = ConvertDeclSpecToType(DS);
Douglas Gregord45210d2009-01-30 22:09:00 +0000282 break;
Mike Stumpc1fddff2009-02-04 22:31:32 +0000283 }
Douglas Gregord45210d2009-01-30 22:09:00 +0000284
285 case Declarator::DK_Constructor:
286 case Declarator::DK_Destructor:
287 case Declarator::DK_Conversion:
288 // Constructors and destructors don't have return types. Use
289 // "void" instead. Conversion operators will check their return
290 // types separately.
291 T = Context.VoidTy;
292 break;
293 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000294
Mike Stumpc1fddff2009-02-04 22:31:32 +0000295 // Walk the DeclTypeInfo, building the recursive type as we go.
296 // DeclTypeInfos are ordered from the identifier out, which is
297 // opposite of what we want :).
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000298 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
299 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Chris Lattner4b009652007-07-25 00:24:17 +0000300 switch (DeclType.Kind) {
301 default: assert(0 && "Unknown decltype!");
Steve Naroff7aa54752008-08-27 16:04:49 +0000302 case DeclaratorChunk::BlockPointer:
303 if (DeclType.Cls.TypeQuals)
304 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
305 if (!T.getTypePtr()->isFunctionType())
306 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
307 else
308 T = Context.getBlockPointerType(T);
309 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000310 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000311 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000313 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
314 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000315 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000316 T = Context.IntTy;
317 }
318
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000319 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
320 // object or incomplete types shall not be restrict-qualified."
321 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000322 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000323 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000324 << T;
Sebastian Redl75555032009-01-24 21:16:55 +0000325 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
326 }
327
Chris Lattner4b009652007-07-25 00:24:17 +0000328 // Apply the pointer typequals to the pointer object.
329 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
330 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000331 case DeclaratorChunk::Reference: {
332 // Whether we should suppress the creation of the reference.
333 bool SuppressReference = false;
334 if (T->isReferenceType()) {
335 // C++ [dcl.ref]p4: There shall be no references to references.
336 //
337 // According to C++ DR 106, references to references are only
338 // diagnosed when they are written directly (e.g., "int & &"),
339 // but not when they happen via a typedef:
340 //
341 // typedef int& intref;
342 // typedef intref& intref2;
343 //
344 // Parser::ParserDeclaratorInternal diagnoses the case where
345 // references are written directly; here, we handle the
346 // collapsing of references-to-references as described in C++
347 // DR 106 and amended by C++ DR 540.
348 SuppressReference = true;
349 }
350
351 // C++ [dcl.ref]p1:
352 // A declarator that specifies the type “reference to cv void”
353 // is ill-formed.
354 if (T->isVoidType()) {
355 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroff91b03f72007-08-28 03:03:08 +0000356 D.setInvalidType(true);
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000357 T = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000358 }
359
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000360 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
361 // object or incomplete types shall not be restrict-qualified."
362 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000363 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000364 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000365 << T;
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000366 DeclType.Ref.HasRestrict = false;
367 }
368
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000369 if (!SuppressReference)
370 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000371
372 // Handle restrict on references.
373 if (DeclType.Ref.HasRestrict)
374 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000375 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000376 }
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000378 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000379 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000380 ArrayType::ArraySizeModifier ASM;
381 if (ATI.isStar)
382 ASM = ArrayType::Star;
383 else if (ATI.hasStatic)
384 ASM = ArrayType::Static;
385 else
386 ASM = ArrayType::Normal;
387
388 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
389 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000390 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
391 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroff91b03f72007-08-28 03:03:08 +0000392 T = Context.IntTy;
393 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000394 } else if (T->isFunctionType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000395 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
396 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000397 T = Context.getPointerType(T);
398 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000399 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000400 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000401 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
402 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000403 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000404 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000405 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000406 // If the element type is a struct or union that contains a variadic
Douglas Gregorec793e82009-02-10 21:49:46 +0000407 // array, accept it as a GNU extension: C99 6.7.2.1p2.
408 if (EltTy->getDecl()->hasFlexibleArrayMember())
409 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner31fccaf2008-08-18 22:49:54 +0000410 } else if (T->isObjCInterfaceType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000411 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Chris Lattner4b009652007-07-25 00:24:17 +0000412 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000413
Steve Naroff63b6a642007-08-30 22:35:45 +0000414 // C99 6.7.5.2p1: The size expression shall have integer type.
415 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000416 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000417 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000418 D.setInvalidType(true);
Ted Kremenek06f217c2009-02-07 01:51:40 +0000419 ArraySize->Destroy(Context);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000420 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000421 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000422 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000423 if (!ArraySize) {
424 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000425 } else if (ArraySize->isValueDependent()) {
426 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000427 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000428 !T->isConstantSizeType()) {
Eli Friedman2ff28d12008-05-14 00:40:18 +0000429 // Per C99, a variable array is an array with either a non-constant
430 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000431 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000432 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000433 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
434 // have a value greater than zero.
435 if (ConstVal.isSigned()) {
436 if (ConstVal.isNegative()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000437 Diag(ArraySize->getLocStart(),
438 diag::err_typecheck_negative_array_size)
439 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000440 D.setInvalidType(true);
441 } else if (ConstVal == 0) {
442 // GCC accepts zero sized static arrays.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000443 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
444 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000445 }
446 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000447 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000448 }
Chris Lattner47958f62007-08-28 16:54:00 +0000449 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattner306d4df2008-12-18 06:50:14 +0000450 if (!getLangOptions().C99) {
451 if (ArraySize && !ArraySize->isValueDependent() &&
452 !ArraySize->isIntegerConstantExpr(Context))
453 Diag(D.getIdentifierLoc(), diag::ext_vla);
454 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
455 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
456 }
Chris Lattner4b009652007-07-25 00:24:17 +0000457 break;
458 }
Sebastian Redl75555032009-01-24 21:16:55 +0000459 case DeclaratorChunk::Function: {
Chris Lattner4b009652007-07-25 00:24:17 +0000460 // If the function declarator has a prototype (i.e. it is not () and
461 // does not have a K&R-style identifier list), then the arguments are part
462 // of the type, otherwise the argument list is ().
463 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000464
Chris Lattner6ad7e882007-12-19 05:31:29 +0000465 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000466 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000467 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattner6ad7e882007-12-19 05:31:29 +0000468 T = Context.IntTy;
469 D.setInvalidType(true);
470 }
471
Eli Friedman769e7302008-08-25 21:31:01 +0000472 if (FTI.NumArgs == 0) {
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000473 if (getLangOptions().CPlusPlus) {
474 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
475 // function takes no arguments.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000476 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000477 } else {
478 // Simple void foo(), where the incoming T is the result type.
479 T = Context.getFunctionTypeNoProto(T);
480 }
Eli Friedman769e7302008-08-25 21:31:01 +0000481 } else if (FTI.ArgInfo[0].Param == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000482 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedman769e7302008-08-25 21:31:01 +0000483 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Chris Lattner4b009652007-07-25 00:24:17 +0000484 } else {
485 // Otherwise, we have a function with an argument list that is
486 // potentially variadic.
487 llvm::SmallVector<QualType, 16> ArgTys;
488
489 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000490 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
491 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000492 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000493 //
494 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
495 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000496 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000497 // argument type in the function prototype *will not* match the
498 // type in ParmVarDecl (which makes the code generator unhappy).
499 //
500 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000501 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000502 // it should be driving off the type being created here.
503 //
504 // FIXME: If a source translation tool needs to see the original type,
505 // then we need to consider storing both types somewhere...
506 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000507 if (ArgTy->isArrayType()) {
508 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000509 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000510 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000511
Chris Lattner4b009652007-07-25 00:24:17 +0000512 // Look for 'void'. void is allowed only as a single argument to a
513 // function with no other parameters (C99 6.7.5.3p10). We record
514 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000515 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000516 // If this is something like 'float(int, void)', reject it. 'void'
517 // is an incomplete type (C99 6.2.5p19) and function decls cannot
518 // have arguments of incomplete type.
519 if (FTI.NumArgs != 1 || FTI.isVariadic) {
520 Diag(DeclType.Loc, diag::err_void_only_param);
521 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000522 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000523 } else if (FTI.ArgInfo[i].Ident) {
524 // Reject, but continue to parse 'int(void abc)'.
525 Diag(FTI.ArgInfo[i].IdentLoc,
526 diag::err_param_with_void_type);
527 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000528 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000529 } else {
530 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000531 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000532 Diag(DeclType.Loc, diag::err_void_param_qualified);
533
534 // Do not add 'void' to the ArgTys list.
535 break;
536 }
Eli Friedman769e7302008-08-25 21:31:01 +0000537 } else if (!FTI.hasPrototype) {
538 if (ArgTy->isPromotableIntegerType()) {
539 ArgTy = Context.IntTy;
540 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
541 if (BTy->getKind() == BuiltinType::Float)
542 ArgTy = Context.DoubleTy;
543 }
Chris Lattner4b009652007-07-25 00:24:17 +0000544 }
545
546 ArgTys.push_back(ArgTy);
547 }
548 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000549 FTI.isVariadic, FTI.TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000550 }
551 break;
552 }
Sebastian Redl75555032009-01-24 21:16:55 +0000553 case DeclaratorChunk::MemberPointer:
554 // The scope spec must refer to a class, or be dependent.
555 DeclContext *DC = static_cast<DeclContext*>(
556 DeclType.Mem.Scope().getScopeRep());
557 QualType ClsType;
558 // FIXME: Extend for dependent types when it's actually supported.
559 // See ActOnCXXNestedNameSpecifier.
560 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
561 ClsType = Context.getTagDeclType(RD);
562 } else {
563 if (DC) {
564 Diag(DeclType.Mem.Scope().getBeginLoc(),
565 diag::err_illegal_decl_mempointer_in_nonclass)
566 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
567 << DeclType.Mem.Scope().getRange();
568 }
569 D.setInvalidType(true);
570 ClsType = Context.IntTy;
571 }
572
573 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
574 // with reference type, or "cv void."
575 if (T->isReferenceType()) {
576 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
577 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
578 D.setInvalidType(true);
579 T = Context.IntTy;
580 }
581 if (T->isVoidType()) {
582 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
583 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
584 T = Context.IntTy;
585 }
586
587 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
588 // object or incomplete types shall not be restrict-qualified."
589 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
590 !T->isIncompleteOrObjectType()) {
591 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
592 << T;
593 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
594 }
595
Sebastian Redlba387562009-01-25 19:43:20 +0000596 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
597 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redl75555032009-01-24 21:16:55 +0000598
599 break;
600 }
601
Chris Lattner65a57042008-06-29 00:50:08 +0000602 // See if there are any attributes on this declarator chunk.
603 if (const AttributeList *AL = DeclType.getAttrs())
604 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000605 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000606
607 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
608 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
609 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
610
611 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
612 // for a nonstatic member function, the function type to which a pointer
613 // to member refers, or the top-level function type of a function typedef
614 // declaration.
615 if (FnTy->getTypeQuals() != 0 &&
616 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregorc5cbc242008-12-15 23:53:10 +0000617 ((D.getContext() != Declarator::MemberContext &&
618 (!D.getCXXScopeSpec().isSet() ||
619 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregor723d3332009-01-07 00:43:41 +0000620 ->isRecord())) ||
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000621 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000622 if (D.isFunctionDeclarator())
623 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
624 else
625 Diag(D.getIdentifierLoc(),
626 diag::err_invalid_qualified_typedef_function_type_use);
627
628 // Strip the cv-quals from the type.
629 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000630 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000631 }
632 }
Chris Lattner4b009652007-07-25 00:24:17 +0000633
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000634 // If there were any type attributes applied to the decl itself (not the
635 // type, apply the type attribute to the type!)
636 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000637 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000638
Chris Lattner4b009652007-07-25 00:24:17 +0000639 return T;
640}
641
Ted Kremenek42730c52008-01-07 19:49:32 +0000642/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000643/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000644QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
645 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000646 QualType T = MDecl->getResultType();
647 llvm::SmallVector<QualType, 16> ArgTys;
648
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000649 // Add the first two invisible argument types for self and _cmd.
Douglas Gregor5d764842009-01-09 17:18:27 +0000650 if (MDecl->isInstanceMethod()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000651 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000652 selfTy = Context.getPointerType(selfTy);
653 ArgTys.push_back(selfTy);
654 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000655 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000656 ArgTys.push_back(Context.getObjCIdType());
657 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000658
Chris Lattner685d7922008-03-16 01:07:14 +0000659 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000660 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
661 QualType ArgTy = PDecl->getType();
662 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000663 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
664 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000665 // Sema::ActOnParamDeclarator().
666 if (ArgTy->isArrayType())
667 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000668 else if (ArgTy->isFunctionType())
669 ArgTy = Context.getPointerType(ArgTy);
670 ArgTys.push_back(ArgTy);
671 }
672 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000673 MDecl->isVariadic(), 0);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000674 return T;
675}
676
Sebastian Redl8ebd8fb2009-01-26 19:54:48 +0000677/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
678/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
679/// they point to and return true. If T1 and T2 aren't pointer types
680/// or pointer-to-member types, or if they are not similar at this
681/// level, returns false and leaves T1 and T2 unchanged. Top-level
682/// qualifiers on T1 and T2 are ignored. This function will typically
683/// be called in a loop that successively "unwraps" pointer and
684/// pointer-to-member types to compare them at each level.
Chris Lattner6d0d8712009-02-16 21:43:00 +0000685bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000686 const PointerType *T1PtrType = T1->getAsPointerType(),
687 *T2PtrType = T2->getAsPointerType();
688 if (T1PtrType && T2PtrType) {
689 T1 = T1PtrType->getPointeeType();
690 T2 = T2PtrType->getPointeeType();
691 return true;
692 }
693
Sebastian Redlba387562009-01-25 19:43:20 +0000694 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
695 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redlf41a58c2009-01-28 18:33:18 +0000696 if (T1MPType && T2MPType &&
697 Context.getCanonicalType(T1MPType->getClass()) ==
698 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redlba387562009-01-25 19:43:20 +0000699 T1 = T1MPType->getPointeeType();
700 T2 = T2MPType->getPointeeType();
701 return true;
702 }
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000703 return false;
704}
705
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000706Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000707 // C99 6.7.6: Type names have no identifier. This is already validated by
708 // the parser.
709 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
710
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000711 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000712
713 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000714
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000715 // Check that there are no default arguments (C++ only).
716 if (getLangOptions().CPlusPlus)
717 CheckExtraCXXDefaultArguments(D);
718
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000719 // In this context, we *do not* check D.getInvalidType(). If the declarator
720 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
721 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000722 return T.getAsOpaquePtr();
723}
724
Chris Lattner65a57042008-06-29 00:50:08 +0000725
726
727//===----------------------------------------------------------------------===//
728// Type Attribute Processing
729//===----------------------------------------------------------------------===//
730
731/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
732/// specified type. The attribute contains 1 argument, the id of the address
733/// space for the type.
734static void HandleAddressSpaceTypeAttribute(QualType &Type,
735 const AttributeList &Attr, Sema &S){
736 // If this type is already address space qualified, reject it.
737 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
738 // for two or more different address spaces."
739 if (Type.getAddressSpace()) {
740 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
741 return;
742 }
743
744 // Check the attribute arguments.
745 if (Attr.getNumArgs() != 1) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner65a57042008-06-29 00:50:08 +0000747 return;
748 }
749 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
750 llvm::APSInt addrSpace(32);
751 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
753 << ASArgExpr->getSourceRange();
Chris Lattner65a57042008-06-29 00:50:08 +0000754 return;
755 }
756
757 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000758 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattner65a57042008-06-29 00:50:08 +0000759}
760
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000761/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
762/// specified type. The attribute contains 1 argument, weak or strong.
763static void HandleObjCGCTypeAttribute(QualType &Type,
764 const AttributeList &Attr, Sema &S){
765 // FIXME. Needs more work for this to make sense.
766 if (Type.getObjCGCAttr() != QualType::GCNone) {
767 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
768 return;
769 }
770
771 // Check the attribute arguments.
772 QualType::GCAttrTypes attr;
773 if (!Attr.getParameterName() || Attr.getNumArgs() != 0) {
774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
775 return;
776 }
777 if (Attr.getParameterName()->isStr("weak"))
778 attr = QualType::Weak;
779 else if (Attr.getParameterName()->isStr("strong"))
780 attr = QualType::Strong;
781 else {
782 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
783 << "objc_gc" << Attr.getParameterName();
784 return;
785 }
786
787 Type = S.Context.getObjCGCQualType(Type, attr);
788}
789
Chris Lattner65a57042008-06-29 00:50:08 +0000790void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000791 // Scan through and apply attributes to this type where it makes sense. Some
792 // attributes (such as __address_space__, __vector_size__, etc) apply to the
793 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000794 // apply to the decl. Here we apply type attributes and ignore the rest.
795 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000796 // If this is an attribute we can handle, do so now, otherwise, add it to
797 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000798 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000799 default: break;
800 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000801 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
802 break;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000803 case AttributeList::AT_objc_gc:
804 HandleObjCGCTypeAttribute(Result, *AL, *this);
805 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000806 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000807 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000808}
809
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000810/// @brief If the type T is incomplete and cannot be completed,
811/// produce a suitable diagnostic.
812///
813/// This routine checks whether the type @p T is complete in any
814/// context where a complete type is required. If @p T is a complete
815/// type, returns false. If @p T is incomplete, issues the diagnostic
816/// @p diag (giving it the type @p T) and returns true.
817///
818/// @param Loc The location in the source that the incomplete type
819/// diagnostic should refer to.
820///
821/// @param T The type that this routine is examining for completeness.
822///
823/// @param diag The diagnostic value (e.g.,
824/// @c diag::err_typecheck_decl_incomplete_type) that will be used
825/// for the error message if @p T is incomplete.
826///
827/// @param Range1 An optional range in the source code that will be a
828/// part of the "incomplete type" error message.
829///
830/// @param Range2 An optional range in the source code that will be a
831/// part of the "incomplete type" error message.
832///
833/// @param PrintType If non-NULL, the type that should be printed
834/// instead of @p T. This parameter should be used when the type that
835/// we're checking for incompleteness isn't the type that should be
836/// displayed to the user, e.g., when T is a type and PrintType is a
837/// pointer to T.
838///
839/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
840/// @c false otherwise.
841///
842/// @todo When Clang gets proper support for C++ templates, this
843/// routine will also be able perform template instantiation when @p T
844/// is a class template specialization.
845bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
846 SourceRange Range1, SourceRange Range2,
847 QualType PrintType) {
848 // If we have a complete type, we're done.
849 if (!T->isIncompleteType())
850 return false;
Eli Friedman86ad5222008-05-27 03:33:27 +0000851
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000852 if (PrintType.isNull())
853 PrintType = T;
854
855 // We have an incomplete type. Produce a diagnostic.
856 Diag(Loc, diag) << PrintType << Range1 << Range2;
857
858 // If the type was a forward declaration of a class/struct/union
859 // type, produce
860 const TagType *Tag = 0;
861 if (const RecordType *Record = T->getAsRecordType())
862 Tag = Record;
863 else if (const EnumType *Enum = T->getAsEnumType())
864 Tag = Enum;
865
866 if (Tag && !Tag->getDecl()->isInvalidDecl())
867 Diag(Tag->getDecl()->getLocation(),
868 Tag->isBeingDefined() ? diag::note_type_being_defined
869 : diag::note_forward_declaration)
870 << QualType(Tag, 0);
871
872 return true;
873}