blob: 00bfb13561fb6bdfb5b2f4653fdb8983ef089bd1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Douglas Gregor930d8b52009-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 Lattnerfca0ddd2008-06-26 06:27:57 +000026QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000027 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
28 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000029 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000030
31 switch (DS.getTypeSpecType()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000032 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner96b77fc2008-04-02 06:50:17 +000033 case DeclSpec::TST_void:
34 Result = Context.VoidTy;
35 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 case DeclSpec::TST_char:
37 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000038 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000040 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 else {
42 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
43 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000044 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 }
Chris Lattner958858e2008-02-20 21:40:32 +000046 break;
Argyrios Kyrtzidis64c438a2008-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 Lattnerf3a41af2008-11-20 06:38:18 +000051 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
52 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000053 Result = Context.getSignedWCharType();
54 } else {
55 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000057 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
58 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000059 Result = Context.getUnsignedWCharType();
60 }
61 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000062 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000063 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000064 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000065 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000066 DS.getNumProtocolQualifiers());
67 break;
68 }
69
Chris Lattnerd658b562008-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);
82 } else {
83 // 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 Gregor930d8b52009-01-30 22:09:00 +000087 // FIXME: this should be a hard error in C++
Chris Lattnerd658b562008-04-05 06:32:51 +000088 if (!DS.hasTypeSpecifier())
89 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
90 }
91
92 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000093 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000094 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
95 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000096 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
97 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
98 case DeclSpec::TSW_long: Result = Context.LongTy; break;
99 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101 } else {
102 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000103 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
104 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
105 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
106 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108 }
Chris Lattner958858e2008-02-20 21:40:32 +0000109 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000110 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000111 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000112 case DeclSpec::TST_double:
113 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000114 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000115 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000116 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000117 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000118 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 case DeclSpec::TST_decimal32: // _Decimal32
120 case DeclSpec::TST_decimal64: // _Decimal64
121 case DeclSpec::TST_decimal128: // _Decimal128
122 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000123 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case DeclSpec::TST_enum:
125 case DeclSpec::TST_union:
126 case DeclSpec::TST_struct: {
127 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000128 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
130 DS.getTypeSpecSign() == 0 &&
131 "Can't handle qualifiers on typedef names yet!");
132 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000133 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000134 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000136 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
138 DS.getTypeSpecSign() == 0 &&
139 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000140 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000141
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000142 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
143 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
144 // we have this "hack" for now...
145 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
146 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
147 (ObjCProtocolDecl**)PQ,
148 DS.getNumProtocolQualifiers());
149 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000150 // id<protocol-list>
151 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
152 DS.getNumProtocolQualifiers());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000153 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000155 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
Chris Lattner958858e2008-02-20 21:40:32 +0000157 case DeclSpec::TST_typeofType:
158 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
159 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000160 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000161 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000162 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000163 case DeclSpec::TST_typeofExpr: {
164 Expr *E = static_cast<Expr *>(DS.getTypeRep());
165 assert(E && "Didn't get an expression for typeof?");
166 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000167 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000168 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000169 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Chris Lattner958858e2008-02-20 21:40:32 +0000171
172 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000173 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
174 if (getLangOptions().Freestanding)
175 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000176 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000177 }
Chris Lattner958858e2008-02-20 21:40:32 +0000178
179 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
180 "FIXME: imaginary types not supported yet!");
181
Chris Lattner38d8b982008-02-20 22:04:11 +0000182 // See if there are any attributes on the declspec that apply to the type (as
183 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000184 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000185 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000186
Chris Lattner96b77fc2008-04-02 06:50:17 +0000187 // Apply const/volatile/restrict qualifiers to T.
188 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
189
190 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
191 // or incomplete types shall not be restrict-qualified." C++ also allows
192 // restrict-qualified references.
193 if (TypeQuals & QualType::Restrict) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000194 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
195 QualType EltTy = PT->getPointeeType();
196
197 // If we have a pointer or reference, the pointee must have an object or
198 // incomplete type.
199 if (!EltTy->isIncompleteOrObjectType()) {
200 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000201 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000202 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000203 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
204 }
205 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000206 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000207 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000208 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000209 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000210 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000211 }
212
213 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
214 // of a function type includes any type qualifiers, the behavior is
215 // undefined."
216 if (Result->isFunctionType() && TypeQuals) {
217 // Get some location to point at, either the C or V location.
218 SourceLocation Loc;
219 if (TypeQuals & QualType::Const)
220 Loc = DS.getConstSpecLoc();
221 else {
222 assert((TypeQuals & QualType::Volatile) &&
223 "Has CV quals but not C or V?");
224 Loc = DS.getVolatileSpecLoc();
225 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000226 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000227 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000228 }
229
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000230 // C++ [dcl.ref]p1:
231 // Cv-qualified references are ill-formed except when the
232 // cv-qualifiers are introduced through the use of a typedef
233 // (7.1.3) or of a template type argument (14.3), in which
234 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000235 // FIXME: Shouldn't we be checking SCS_typedef here?
236 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000237 TypeQuals && Result->isReferenceType()) {
238 TypeQuals &= ~QualType::Const;
239 TypeQuals &= ~QualType::Volatile;
240 }
241
Chris Lattner96b77fc2008-04-02 06:50:17 +0000242 Result = Result.getQualifiedType(TypeQuals);
243 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000244 return Result;
245}
246
Mike Stump98eb8a72009-02-04 22:31:32 +0000247/// GetTypeForDeclarator - Convert the type for the specified
248/// declarator to Type instances. Skip the outermost Skip type
249/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000250QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000251 bool OmittedReturnType = false;
252
253 if (D.getContext() == Declarator::BlockLiteralContext
254 && Skip == 0
255 && !D.getDeclSpec().hasTypeSpecifier()
256 && (D.getNumTypeObjects() == 0
257 || (D.getNumTypeObjects() == 1
258 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
259 OmittedReturnType = true;
260
Chris Lattnerb23deda2007-08-28 16:40:32 +0000261 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000262 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000263 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
264 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000265
266 // Determine the type of the declarator. Not all forms of declarator
267 // have a type.
268 QualType T;
269 switch (D.getKind()) {
270 case Declarator::DK_Abstract:
271 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000272 case Declarator::DK_Operator: {
273 const DeclSpec& DS = D.getDeclSpec();
274 if (OmittedReturnType)
275 // We default to a dependent type initially. Can be modified by
276 // the first return statement.
277 T = Context.DependentTy;
278 else
279 T = ConvertDeclSpecToType(DS);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000280 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000281 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000282
283 case Declarator::DK_Constructor:
284 case Declarator::DK_Destructor:
285 case Declarator::DK_Conversion:
286 // Constructors and destructors don't have return types. Use
287 // "void" instead. Conversion operators will check their return
288 // types separately.
289 T = Context.VoidTy;
290 break;
291 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000292
Mike Stump98eb8a72009-02-04 22:31:32 +0000293 // Walk the DeclTypeInfo, building the recursive type as we go.
294 // DeclTypeInfos are ordered from the identifier out, which is
295 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000296 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
297 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 switch (DeclType.Kind) {
299 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000300 case DeclaratorChunk::BlockPointer:
301 if (DeclType.Cls.TypeQuals)
302 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
303 if (!T.getTypePtr()->isFunctionType())
304 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
305 else
306 T = Context.getBlockPointerType(T);
307 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000309 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000311 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
312 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000313 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000314 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
316
Chris Lattner96b77fc2008-04-02 06:50:17 +0000317 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
318 // object or incomplete types shall not be restrict-qualified."
319 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000320 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000321 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000322 << T;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000323 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
324 }
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // Apply the pointer typequals to the pointer object.
327 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
328 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000329 case DeclaratorChunk::Reference: {
330 // Whether we should suppress the creation of the reference.
331 bool SuppressReference = false;
332 if (T->isReferenceType()) {
333 // C++ [dcl.ref]p4: There shall be no references to references.
334 //
335 // According to C++ DR 106, references to references are only
336 // diagnosed when they are written directly (e.g., "int & &"),
337 // but not when they happen via a typedef:
338 //
339 // typedef int& intref;
340 // typedef intref& intref2;
341 //
342 // Parser::ParserDeclaratorInternal diagnoses the case where
343 // references are written directly; here, we handle the
344 // collapsing of references-to-references as described in C++
345 // DR 106 and amended by C++ DR 540.
346 SuppressReference = true;
347 }
348
349 // C++ [dcl.ref]p1:
350 // A declarator that specifies the type “reference to cv void”
351 // is ill-formed.
352 if (T->isVoidType()) {
353 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroffe1223f72007-08-28 03:03:08 +0000354 D.setInvalidType(true);
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000355 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 }
357
Chris Lattner96b77fc2008-04-02 06:50:17 +0000358 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
359 // object or incomplete types shall not be restrict-qualified."
360 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000361 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000362 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000363 << T;
Chris Lattner96b77fc2008-04-02 06:50:17 +0000364 DeclType.Ref.HasRestrict = false;
365 }
366
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000367 if (!SuppressReference)
368 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000369
370 // Handle restrict on references.
371 if (DeclType.Ref.HasRestrict)
372 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000374 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000376 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000377 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 ArrayType::ArraySizeModifier ASM;
379 if (ATI.isStar)
380 ASM = ArrayType::Star;
381 else if (ATI.hasStatic)
382 ASM = ArrayType::Static;
383 else
384 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
387 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000388 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
389 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroffe1223f72007-08-28 03:03:08 +0000390 T = Context.IntTy;
391 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000392 } else if (T->isFunctionType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000393 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
394 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000395 T = Context.getPointerType(T);
396 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000397 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000399 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
400 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000402 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000403 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 // If the element type is a struct or union that contains a variadic
Douglas Gregor0bfe54f2009-02-10 21:49:46 +0000405 // array, accept it as a GNU extension: C99 6.7.2.1p2.
406 if (EltTy->getDecl()->hasFlexibleArrayMember())
407 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner43477ca2008-08-18 22:49:54 +0000408 } else if (T->isObjCInterfaceType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000409 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000411
Steve Naroff42471f82007-08-30 22:35:45 +0000412 // C99 6.7.5.2p1: The size expression shall have integer type.
413 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000414 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattnerd1625842008-11-24 06:25:27 +0000415 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000416 D.setInvalidType(true);
Ted Kremenek169a2662009-02-07 01:51:40 +0000417 ArraySize->Destroy(Context);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000418 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000419 }
Steve Naroffc9406122007-08-30 18:10:14 +0000420 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000421 if (!ArraySize) {
422 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000423 } else if (ArraySize->isValueDependent()) {
424 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman37148aa2008-05-14 00:40:18 +0000425 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000426 !T->isConstantSizeType()) {
Eli Friedman37148aa2008-05-14 00:40:18 +0000427 // Per C99, a variable array is an array with either a non-constant
428 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000429 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000430 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000431 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
432 // have a value greater than zero.
433 if (ConstVal.isSigned()) {
434 if (ConstVal.isNegative()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000435 Diag(ArraySize->getLocStart(),
436 diag::err_typecheck_negative_array_size)
437 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000438 D.setInvalidType(true);
439 } else if (ConstVal == 0) {
440 // GCC accepts zero sized static arrays.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000441 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
442 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000443 }
444 }
Steve Naroffc9406122007-08-30 18:10:14 +0000445 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000446 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000447 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattnera1fcbad2008-12-18 06:50:14 +0000448 if (!getLangOptions().C99) {
449 if (ArraySize && !ArraySize->isValueDependent() &&
450 !ArraySize->isIntegerConstantExpr(Context))
451 Diag(D.getIdentifierLoc(), diag::ext_vla);
452 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
453 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
454 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 break;
456 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000457 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 // If the function declarator has a prototype (i.e. it is not () and
459 // does not have a K&R-style identifier list), then the arguments are part
460 // of the type, otherwise the argument list is ().
461 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000462
Chris Lattnercd881292007-12-19 05:31:29 +0000463 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000464 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000465 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000466 T = Context.IntTy;
467 D.setInvalidType(true);
468 }
469
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000470 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000471 if (getLangOptions().CPlusPlus) {
472 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
473 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000474 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000475 } else {
476 // Simple void foo(), where the incoming T is the result type.
477 T = Context.getFunctionTypeNoProto(T);
478 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000479 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000481 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 } else {
483 // Otherwise, we have a function with an argument list that is
484 // potentially variadic.
485 llvm::SmallVector<QualType, 16> ArgTys;
486
487 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000488 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
489 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000490 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000491 //
492 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
493 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000494 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000495 // argument type in the function prototype *will not* match the
496 // type in ParmVarDecl (which makes the code generator unhappy).
497 //
498 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000499 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000500 // it should be driving off the type being created here.
501 //
502 // FIXME: If a source translation tool needs to see the original type,
503 // then we need to consider storing both types somewhere...
504 //
Chris Lattnere6327742008-04-02 05:18:44 +0000505 if (ArgTy->isArrayType()) {
506 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000507 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000508 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 // Look for 'void'. void is allowed only as a single argument to a
511 // function with no other parameters (C99 6.7.5.3p10). We record
512 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000513 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 // If this is something like 'float(int, void)', reject it. 'void'
515 // is an incomplete type (C99 6.2.5p19) and function decls cannot
516 // have arguments of incomplete type.
517 if (FTI.NumArgs != 1 || FTI.isVariadic) {
518 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000519 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000520 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000521 } else if (FTI.ArgInfo[i].Ident) {
522 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000524 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000525 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000526 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000527 } else {
528 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000529 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000530 Diag(DeclType.Loc, diag::err_void_param_qualified);
531
532 // Do not add 'void' to the ArgTys list.
533 break;
534 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000535 } else if (!FTI.hasPrototype) {
536 if (ArgTy->isPromotableIntegerType()) {
537 ArgTy = Context.IntTy;
538 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
539 if (BTy->getKind() == BuiltinType::Float)
540 ArgTy = Context.DoubleTy;
541 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 }
543
544 ArgTys.push_back(ArgTy);
545 }
546 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000547 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 }
549 break;
550 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000551 case DeclaratorChunk::MemberPointer:
552 // The scope spec must refer to a class, or be dependent.
553 DeclContext *DC = static_cast<DeclContext*>(
554 DeclType.Mem.Scope().getScopeRep());
555 QualType ClsType;
556 // FIXME: Extend for dependent types when it's actually supported.
557 // See ActOnCXXNestedNameSpecifier.
558 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
559 ClsType = Context.getTagDeclType(RD);
560 } else {
561 if (DC) {
562 Diag(DeclType.Mem.Scope().getBeginLoc(),
563 diag::err_illegal_decl_mempointer_in_nonclass)
564 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
565 << DeclType.Mem.Scope().getRange();
566 }
567 D.setInvalidType(true);
568 ClsType = Context.IntTy;
569 }
570
571 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
572 // with reference type, or "cv void."
573 if (T->isReferenceType()) {
574 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
575 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
576 D.setInvalidType(true);
577 T = Context.IntTy;
578 }
579 if (T->isVoidType()) {
580 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
581 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
582 T = Context.IntTy;
583 }
584
585 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
586 // object or incomplete types shall not be restrict-qualified."
587 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
588 !T->isIncompleteOrObjectType()) {
589 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
590 << T;
591 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
592 }
593
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000594 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
595 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000596
597 break;
598 }
599
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000600 // See if there are any attributes on this declarator chunk.
601 if (const AttributeList *AL = DeclType.getAttrs())
602 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000604
605 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
606 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
607 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
608
609 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
610 // for a nonstatic member function, the function type to which a pointer
611 // to member refers, or the top-level function type of a function typedef
612 // declaration.
613 if (FnTy->getTypeQuals() != 0 &&
614 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000615 ((D.getContext() != Declarator::MemberContext &&
616 (!D.getCXXScopeSpec().isSet() ||
617 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000618 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000619 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000620 if (D.isFunctionDeclarator())
621 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
622 else
623 Diag(D.getIdentifierLoc(),
624 diag::err_invalid_qualified_typedef_function_type_use);
625
626 // Strip the cv-quals from the type.
627 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000628 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000629 }
630 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000631
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000632 // If there were any type attributes applied to the decl itself (not the
633 // type, apply the type attribute to the type!)
634 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000635 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000636
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 return T;
638}
639
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000641/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000642QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
643 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000644 QualType T = MDecl->getResultType();
645 llvm::SmallVector<QualType, 16> ArgTys;
646
Fariborz Jahanian35600022007-11-09 17:18:29 +0000647 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000648 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000650 selfTy = Context.getPointerType(selfTy);
651 ArgTys.push_back(selfTy);
652 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000653 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654 ArgTys.push_back(Context.getObjCIdType());
655 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000656
Chris Lattner58cce3b2008-03-16 01:07:14 +0000657 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000658 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
659 QualType ArgTy = PDecl->getType();
660 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000661 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
662 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000663 // Sema::ActOnParamDeclarator().
664 if (ArgTy->isArrayType())
665 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000666 else if (ArgTy->isFunctionType())
667 ArgTy = Context.getPointerType(ArgTy);
668 ArgTys.push_back(ArgTy);
669 }
670 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000671 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000672 return T;
673}
674
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000675/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
676/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
677/// they point to and return true. If T1 and T2 aren't pointer types
678/// or pointer-to-member types, or if they are not similar at this
679/// level, returns false and leaves T1 and T2 unchanged. Top-level
680/// qualifiers on T1 and T2 are ignored. This function will typically
681/// be called in a loop that successively "unwraps" pointer and
682/// pointer-to-member types to compare them at each level.
Douglas Gregor57373262008-10-22 14:17:15 +0000683bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2)
684{
685 const PointerType *T1PtrType = T1->getAsPointerType(),
686 *T2PtrType = T2->getAsPointerType();
687 if (T1PtrType && T2PtrType) {
688 T1 = T1PtrType->getPointeeType();
689 T2 = T2PtrType->getPointeeType();
690 return true;
691 }
692
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000693 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
694 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000695 if (T1MPType && T2MPType &&
696 Context.getCanonicalType(T1MPType->getClass()) ==
697 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000698 T1 = T1MPType->getPointeeType();
699 T2 = T2MPType->getPointeeType();
700 return true;
701 }
Douglas Gregor57373262008-10-22 14:17:15 +0000702 return false;
703}
704
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000705Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // C99 6.7.6: Type names have no identifier. This is already validated by
707 // the parser.
708 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
709
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000710 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000711
712 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000713
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000714 // Check that there are no default arguments (C++ only).
715 if (getLangOptions().CPlusPlus)
716 CheckExtraCXXDefaultArguments(D);
717
Steve Naroff5912a352007-08-28 20:14:24 +0000718 // In this context, we *do not* check D.getInvalidType(). If the declarator
719 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
720 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 return T.getAsOpaquePtr();
722}
723
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000724
725
726//===----------------------------------------------------------------------===//
727// Type Attribute Processing
728//===----------------------------------------------------------------------===//
729
730/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
731/// specified type. The attribute contains 1 argument, the id of the address
732/// space for the type.
733static void HandleAddressSpaceTypeAttribute(QualType &Type,
734 const AttributeList &Attr, Sema &S){
735 // If this type is already address space qualified, reject it.
736 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
737 // for two or more different address spaces."
738 if (Type.getAddressSpace()) {
739 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
740 return;
741 }
742
743 // Check the attribute arguments.
744 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000745 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000746 return;
747 }
748 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
749 llvm::APSInt addrSpace(32);
750 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
752 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000753 return;
754 }
755
756 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
757 Type = S.Context.getASQualType(Type, ASIdx);
758}
759
760void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000761 // Scan through and apply attributes to this type where it makes sense. Some
762 // attributes (such as __address_space__, __vector_size__, etc) apply to the
763 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000764 // apply to the decl. Here we apply type attributes and ignore the rest.
765 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000766 // If this is an attribute we can handle, do so now, otherwise, add it to
767 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000768 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000769 default: break;
770 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000771 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
772 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000773 }
Chris Lattner232e8822008-02-21 01:08:11 +0000774 }
Chris Lattner232e8822008-02-21 01:08:11 +0000775}
776
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000777/// @brief If the type T is incomplete and cannot be completed,
778/// produce a suitable diagnostic.
779///
780/// This routine checks whether the type @p T is complete in any
781/// context where a complete type is required. If @p T is a complete
782/// type, returns false. If @p T is incomplete, issues the diagnostic
783/// @p diag (giving it the type @p T) and returns true.
784///
785/// @param Loc The location in the source that the incomplete type
786/// diagnostic should refer to.
787///
788/// @param T The type that this routine is examining for completeness.
789///
790/// @param diag The diagnostic value (e.g.,
791/// @c diag::err_typecheck_decl_incomplete_type) that will be used
792/// for the error message if @p T is incomplete.
793///
794/// @param Range1 An optional range in the source code that will be a
795/// part of the "incomplete type" error message.
796///
797/// @param Range2 An optional range in the source code that will be a
798/// part of the "incomplete type" error message.
799///
800/// @param PrintType If non-NULL, the type that should be printed
801/// instead of @p T. This parameter should be used when the type that
802/// we're checking for incompleteness isn't the type that should be
803/// displayed to the user, e.g., when T is a type and PrintType is a
804/// pointer to T.
805///
806/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
807/// @c false otherwise.
808///
809/// @todo When Clang gets proper support for C++ templates, this
810/// routine will also be able perform template instantiation when @p T
811/// is a class template specialization.
812bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
813 SourceRange Range1, SourceRange Range2,
814 QualType PrintType) {
815 // If we have a complete type, we're done.
816 if (!T->isIncompleteType())
817 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +0000818
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000819 if (PrintType.isNull())
820 PrintType = T;
821
822 // We have an incomplete type. Produce a diagnostic.
823 Diag(Loc, diag) << PrintType << Range1 << Range2;
824
825 // If the type was a forward declaration of a class/struct/union
826 // type, produce
827 const TagType *Tag = 0;
828 if (const RecordType *Record = T->getAsRecordType())
829 Tag = Record;
830 else if (const EnumType *Enum = T->getAsEnumType())
831 Tag = Enum;
832
833 if (Tag && !Tag->getDecl()->isInvalidDecl())
834 Diag(Tag->getDecl()->getLocation(),
835 Tag->isBeingDefined() ? diag::note_type_being_defined
836 : diag::note_forward_declaration)
837 << QualType(Tag, 0);
838
839 return true;
840}