blob: d31402de71214f7e43fe031fcf54352337d641ec [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);
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 Gregord45210d2009-01-30 22:09:00 +000087 // FIXME: this should be a hard error in C++
Chris Lattner6ab935b2008-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 Lattner5328f312007-08-21 17:02:28 +000093 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000094 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
95 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000100 }
101 } else {
102 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000107 }
108 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000109 break;
Chris Lattner5328f312007-08-21 17:02:28 +0000110 }
Chris Lattner726c5452008-02-20 23:53:49 +0000111 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +0000112 case DeclSpec::TST_double:
113 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +0000114 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000115 else
Chris Lattner726c5452008-02-20 23:53:49 +0000116 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000117 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000118 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner2e78db32008-04-13 18:59:07 +0000123 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000124 case DeclSpec::TST_enum:
125 case DeclSpec::TST_union:
126 case DeclSpec::TST_struct: {
127 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000128 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor1d661552008-04-13 21:07:44 +0000133 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000134 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000135 }
136 case DeclSpec::TST_typedef: {
137 Decl *D = static_cast<Decl *>(DS.getTypeRep());
138 assert(D && "Didn't get a decl for a typedef?");
139 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
140 DS.getTypeSpecSign() == 0 &&
141 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerada63792008-07-26 01:53:50 +0000142 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor1d661552008-04-13 21:07:44 +0000143
Steve Naroff81f1bba2007-09-06 21:24:23 +0000144 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
145 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000147 if (PQ == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000148 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000149 break;
150 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000151
Chris Lattner726c5452008-02-20 23:53:49 +0000152 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerada63792008-07-26 01:53:50 +0000153 (ObjCProtocolDecl**)PQ,
Chris Lattner69f01932008-02-21 01:32:26 +0000154 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000155 break;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000156 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000157 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
158 // id<protocol-list>
159 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
160 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000161 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000162 }
163 }
Chris Lattner4b009652007-07-25 00:24:17 +0000164 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000165 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000166 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000167 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000168 case DeclSpec::TST_typeofType:
169 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
170 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000171 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000172 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000173 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000174 case DeclSpec::TST_typeofExpr: {
175 Expr *E = static_cast<Expr *>(DS.getTypeRep());
176 assert(E && "Didn't get an expression for typeof?");
177 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000178 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000179 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000180 }
Chris Lattner4b009652007-07-25 00:24:17 +0000181 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000182
183 // Handle complex types.
184 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000185 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000186
187 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
188 "FIXME: imaginary types not supported yet!");
189
Chris Lattnerd496fb92008-02-20 22:04:11 +0000190 // See if there are any attributes on the declspec that apply to the type (as
191 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000192 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000193 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000194
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000195 // Apply const/volatile/restrict qualifiers to T.
196 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
197
198 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
199 // or incomplete types shall not be restrict-qualified." C++ also allows
200 // restrict-qualified references.
201 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000202 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
203 QualType EltTy = PT->getPointeeType();
204
205 // If we have a pointer or reference, the pointee must have an object or
206 // incomplete type.
207 if (!EltTy->isIncompleteOrObjectType()) {
208 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000209 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000210 << EltTy << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000211 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
212 }
213 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000214 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000215 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000216 << Result << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000217 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000218 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000219 }
220
221 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
222 // of a function type includes any type qualifiers, the behavior is
223 // undefined."
224 if (Result->isFunctionType() && TypeQuals) {
225 // Get some location to point at, either the C or V location.
226 SourceLocation Loc;
227 if (TypeQuals & QualType::Const)
228 Loc = DS.getConstSpecLoc();
229 else {
230 assert((TypeQuals & QualType::Volatile) &&
231 "Has CV quals but not C or V?");
232 Loc = DS.getVolatileSpecLoc();
233 }
Chris Lattner77d52da2008-11-20 06:06:08 +0000234 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000235 << Result << DS.getSourceRange();
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000236 }
237
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000238 // C++ [dcl.ref]p1:
239 // Cv-qualified references are ill-formed except when the
240 // cv-qualifiers are introduced through the use of a typedef
241 // (7.1.3) or of a template type argument (14.3), in which
242 // case the cv-qualifiers are ignored.
243 if (DS.getTypeSpecType() == DeclSpec::TST_typedef &&
244 TypeQuals && Result->isReferenceType()) {
245 TypeQuals &= ~QualType::Const;
246 TypeQuals &= ~QualType::Volatile;
247 }
248
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000249 Result = Result.getQualifiedType(TypeQuals);
250 }
Chris Lattner9e982502008-02-21 01:07:18 +0000251 return Result;
252}
253
Mike Stumpc1fddff2009-02-04 22:31:32 +0000254/// GetTypeForDeclarator - Convert the type for the specified
255/// declarator to Type instances. Skip the outermost Skip type
256/// objects.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000257QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stumpc1fddff2009-02-04 22:31:32 +0000258 bool OmittedReturnType = false;
259
260 if (D.getContext() == Declarator::BlockLiteralContext
261 && Skip == 0
262 && !D.getDeclSpec().hasTypeSpecifier()
263 && (D.getNumTypeObjects() == 0
264 || (D.getNumTypeObjects() == 1
265 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
266 OmittedReturnType = true;
267
Chris Lattner11f20f92007-08-28 16:40:32 +0000268 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000269 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000270 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
271 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregord45210d2009-01-30 22:09:00 +0000272
273 // Determine the type of the declarator. Not all forms of declarator
274 // have a type.
275 QualType T;
276 switch (D.getKind()) {
277 case Declarator::DK_Abstract:
278 case Declarator::DK_Normal:
Mike Stumpc1fddff2009-02-04 22:31:32 +0000279 case Declarator::DK_Operator: {
280 const DeclSpec& DS = D.getDeclSpec();
281 if (OmittedReturnType)
282 // We default to a dependent type initially. Can be modified by
283 // the first return statement.
284 T = Context.DependentTy;
285 else
286 T = ConvertDeclSpecToType(DS);
Douglas Gregord45210d2009-01-30 22:09:00 +0000287 break;
Mike Stumpc1fddff2009-02-04 22:31:32 +0000288 }
Douglas Gregord45210d2009-01-30 22:09:00 +0000289
290 case Declarator::DK_Constructor:
291 case Declarator::DK_Destructor:
292 case Declarator::DK_Conversion:
293 // Constructors and destructors don't have return types. Use
294 // "void" instead. Conversion operators will check their return
295 // types separately.
296 T = Context.VoidTy;
297 break;
298 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000299
Mike Stumpc1fddff2009-02-04 22:31:32 +0000300 // Walk the DeclTypeInfo, building the recursive type as we go.
301 // DeclTypeInfos are ordered from the identifier out, which is
302 // opposite of what we want :).
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000303 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
304 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Chris Lattner4b009652007-07-25 00:24:17 +0000305 switch (DeclType.Kind) {
306 default: assert(0 && "Unknown decltype!");
Steve Naroff7aa54752008-08-27 16:04:49 +0000307 case DeclaratorChunk::BlockPointer:
308 if (DeclType.Cls.TypeQuals)
309 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
310 if (!T.getTypePtr()->isFunctionType())
311 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
312 else
313 T = Context.getBlockPointerType(T);
314 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000315 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000316 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000317 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000318 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
319 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000320 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000321 T = Context.IntTy;
322 }
323
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000324 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
325 // object or incomplete types shall not be restrict-qualified."
326 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000327 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000328 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000329 << T;
Sebastian Redl75555032009-01-24 21:16:55 +0000330 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
331 }
332
Chris Lattner4b009652007-07-25 00:24:17 +0000333 // Apply the pointer typequals to the pointer object.
334 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
335 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000336 case DeclaratorChunk::Reference: {
337 // Whether we should suppress the creation of the reference.
338 bool SuppressReference = false;
339 if (T->isReferenceType()) {
340 // C++ [dcl.ref]p4: There shall be no references to references.
341 //
342 // According to C++ DR 106, references to references are only
343 // diagnosed when they are written directly (e.g., "int & &"),
344 // but not when they happen via a typedef:
345 //
346 // typedef int& intref;
347 // typedef intref& intref2;
348 //
349 // Parser::ParserDeclaratorInternal diagnoses the case where
350 // references are written directly; here, we handle the
351 // collapsing of references-to-references as described in C++
352 // DR 106 and amended by C++ DR 540.
353 SuppressReference = true;
354 }
355
356 // C++ [dcl.ref]p1:
357 // A declarator that specifies the type “reference to cv void”
358 // is ill-formed.
359 if (T->isVoidType()) {
360 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroff91b03f72007-08-28 03:03:08 +0000361 D.setInvalidType(true);
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000362 T = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000363 }
364
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000365 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
366 // object or incomplete types shall not be restrict-qualified."
367 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000368 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000369 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000370 << T;
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000371 DeclType.Ref.HasRestrict = false;
372 }
373
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000374 if (!SuppressReference)
375 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000376
377 // Handle restrict on references.
378 if (DeclType.Ref.HasRestrict)
379 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000380 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000381 }
Chris Lattner4b009652007-07-25 00:24:17 +0000382 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000383 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000384 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000385 ArrayType::ArraySizeModifier ASM;
386 if (ATI.isStar)
387 ASM = ArrayType::Star;
388 else if (ATI.hasStatic)
389 ASM = ArrayType::Static;
390 else
391 ASM = ArrayType::Normal;
392
393 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
394 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000395 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
396 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroff91b03f72007-08-28 03:03:08 +0000397 T = Context.IntTy;
398 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000399 } else if (T->isFunctionType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000400 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
401 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000402 T = Context.getPointerType(T);
403 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000404 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000405 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000406 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
407 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000408 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000409 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000410 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000411 // If the element type is a struct or union that contains a variadic
412 // array, reject it: C99 6.7.2.1p2.
413 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000414 Diag(DeclType.Loc, diag::err_flexible_array_in_array) << T;
Steve Naroff91b03f72007-08-28 03:03:08 +0000415 T = Context.IntTy;
416 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000417 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000418 } else if (T->isObjCInterfaceType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000419 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Chris Lattner4b009652007-07-25 00:24:17 +0000420 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000421
Steve Naroff63b6a642007-08-30 22:35:45 +0000422 // C99 6.7.5.2p1: The size expression shall have integer type.
423 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000424 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000425 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000426 D.setInvalidType(true);
Ted Kremenek06f217c2009-02-07 01:51:40 +0000427 ArraySize->Destroy(Context);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000428 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000429 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000430 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000431 if (!ArraySize) {
432 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000433 } else if (ArraySize->isValueDependent()) {
434 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000435 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000436 !T->isConstantSizeType()) {
Eli Friedman2ff28d12008-05-14 00:40:18 +0000437 // Per C99, a variable array is an array with either a non-constant
438 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000439 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000440 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000441 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
442 // have a value greater than zero.
443 if (ConstVal.isSigned()) {
444 if (ConstVal.isNegative()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000445 Diag(ArraySize->getLocStart(),
446 diag::err_typecheck_negative_array_size)
447 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000448 D.setInvalidType(true);
449 } else if (ConstVal == 0) {
450 // GCC accepts zero sized static arrays.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000451 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
452 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000453 }
454 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000455 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000456 }
Chris Lattner47958f62007-08-28 16:54:00 +0000457 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattner306d4df2008-12-18 06:50:14 +0000458 if (!getLangOptions().C99) {
459 if (ArraySize && !ArraySize->isValueDependent() &&
460 !ArraySize->isIntegerConstantExpr(Context))
461 Diag(D.getIdentifierLoc(), diag::ext_vla);
462 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
463 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
464 }
Chris Lattner4b009652007-07-25 00:24:17 +0000465 break;
466 }
Sebastian Redl75555032009-01-24 21:16:55 +0000467 case DeclaratorChunk::Function: {
Chris Lattner4b009652007-07-25 00:24:17 +0000468 // If the function declarator has a prototype (i.e. it is not () and
469 // does not have a K&R-style identifier list), then the arguments are part
470 // of the type, otherwise the argument list is ().
471 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000472
Chris Lattner6ad7e882007-12-19 05:31:29 +0000473 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000474 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000475 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattner6ad7e882007-12-19 05:31:29 +0000476 T = Context.IntTy;
477 D.setInvalidType(true);
478 }
479
Eli Friedman769e7302008-08-25 21:31:01 +0000480 if (FTI.NumArgs == 0) {
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000481 if (getLangOptions().CPlusPlus) {
482 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
483 // function takes no arguments.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000484 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000485 } else {
486 // Simple void foo(), where the incoming T is the result type.
487 T = Context.getFunctionTypeNoProto(T);
488 }
Eli Friedman769e7302008-08-25 21:31:01 +0000489 } else if (FTI.ArgInfo[0].Param == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000490 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedman769e7302008-08-25 21:31:01 +0000491 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Chris Lattner4b009652007-07-25 00:24:17 +0000492 } else {
493 // Otherwise, we have a function with an argument list that is
494 // potentially variadic.
495 llvm::SmallVector<QualType, 16> ArgTys;
496
497 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000498 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
499 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000500 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000501 //
502 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
503 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000504 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000505 // argument type in the function prototype *will not* match the
506 // type in ParmVarDecl (which makes the code generator unhappy).
507 //
508 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000509 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000510 // it should be driving off the type being created here.
511 //
512 // FIXME: If a source translation tool needs to see the original type,
513 // then we need to consider storing both types somewhere...
514 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000515 if (ArgTy->isArrayType()) {
516 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000517 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000518 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000519
Chris Lattner4b009652007-07-25 00:24:17 +0000520 // Look for 'void'. void is allowed only as a single argument to a
521 // function with no other parameters (C99 6.7.5.3p10). We record
522 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000523 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000524 // If this is something like 'float(int, void)', reject it. 'void'
525 // is an incomplete type (C99 6.2.5p19) and function decls cannot
526 // have arguments of incomplete type.
527 if (FTI.NumArgs != 1 || FTI.isVariadic) {
528 Diag(DeclType.Loc, diag::err_void_only_param);
529 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000530 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000531 } else if (FTI.ArgInfo[i].Ident) {
532 // Reject, but continue to parse 'int(void abc)'.
533 Diag(FTI.ArgInfo[i].IdentLoc,
534 diag::err_param_with_void_type);
535 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000536 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000537 } else {
538 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000539 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000540 Diag(DeclType.Loc, diag::err_void_param_qualified);
541
542 // Do not add 'void' to the ArgTys list.
543 break;
544 }
Eli Friedman769e7302008-08-25 21:31:01 +0000545 } else if (!FTI.hasPrototype) {
546 if (ArgTy->isPromotableIntegerType()) {
547 ArgTy = Context.IntTy;
548 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
549 if (BTy->getKind() == BuiltinType::Float)
550 ArgTy = Context.DoubleTy;
551 }
Chris Lattner4b009652007-07-25 00:24:17 +0000552 }
553
554 ArgTys.push_back(ArgTy);
555 }
556 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000557 FTI.isVariadic, FTI.TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000558 }
559 break;
560 }
Sebastian Redl75555032009-01-24 21:16:55 +0000561 case DeclaratorChunk::MemberPointer:
562 // The scope spec must refer to a class, or be dependent.
563 DeclContext *DC = static_cast<DeclContext*>(
564 DeclType.Mem.Scope().getScopeRep());
565 QualType ClsType;
566 // FIXME: Extend for dependent types when it's actually supported.
567 // See ActOnCXXNestedNameSpecifier.
568 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
569 ClsType = Context.getTagDeclType(RD);
570 } else {
571 if (DC) {
572 Diag(DeclType.Mem.Scope().getBeginLoc(),
573 diag::err_illegal_decl_mempointer_in_nonclass)
574 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
575 << DeclType.Mem.Scope().getRange();
576 }
577 D.setInvalidType(true);
578 ClsType = Context.IntTy;
579 }
580
581 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
582 // with reference type, or "cv void."
583 if (T->isReferenceType()) {
584 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
585 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
586 D.setInvalidType(true);
587 T = Context.IntTy;
588 }
589 if (T->isVoidType()) {
590 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
591 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
592 T = Context.IntTy;
593 }
594
595 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
596 // object or incomplete types shall not be restrict-qualified."
597 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
598 !T->isIncompleteOrObjectType()) {
599 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
600 << T;
601 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
602 }
603
Sebastian Redlba387562009-01-25 19:43:20 +0000604 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
605 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redl75555032009-01-24 21:16:55 +0000606
607 break;
608 }
609
Chris Lattner65a57042008-06-29 00:50:08 +0000610 // See if there are any attributes on this declarator chunk.
611 if (const AttributeList *AL = DeclType.getAttrs())
612 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000613 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000614
615 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
616 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
617 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
618
619 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
620 // for a nonstatic member function, the function type to which a pointer
621 // to member refers, or the top-level function type of a function typedef
622 // declaration.
623 if (FnTy->getTypeQuals() != 0 &&
624 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregorc5cbc242008-12-15 23:53:10 +0000625 ((D.getContext() != Declarator::MemberContext &&
626 (!D.getCXXScopeSpec().isSet() ||
627 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregor723d3332009-01-07 00:43:41 +0000628 ->isRecord())) ||
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000629 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000630 if (D.isFunctionDeclarator())
631 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
632 else
633 Diag(D.getIdentifierLoc(),
634 diag::err_invalid_qualified_typedef_function_type_use);
635
636 // Strip the cv-quals from the type.
637 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000638 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000639 }
640 }
Chris Lattner4b009652007-07-25 00:24:17 +0000641
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000642 // If there were any type attributes applied to the decl itself (not the
643 // type, apply the type attribute to the type!)
644 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000645 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000646
Chris Lattner4b009652007-07-25 00:24:17 +0000647 return T;
648}
649
Ted Kremenek42730c52008-01-07 19:49:32 +0000650/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000651/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000652QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
653 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000654 QualType T = MDecl->getResultType();
655 llvm::SmallVector<QualType, 16> ArgTys;
656
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000657 // Add the first two invisible argument types for self and _cmd.
Douglas Gregor5d764842009-01-09 17:18:27 +0000658 if (MDecl->isInstanceMethod()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000659 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000660 selfTy = Context.getPointerType(selfTy);
661 ArgTys.push_back(selfTy);
662 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000663 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000664 ArgTys.push_back(Context.getObjCIdType());
665 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000666
Chris Lattner685d7922008-03-16 01:07:14 +0000667 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000668 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
669 QualType ArgTy = PDecl->getType();
670 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000671 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
672 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000673 // Sema::ActOnParamDeclarator().
674 if (ArgTy->isArrayType())
675 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000676 else if (ArgTy->isFunctionType())
677 ArgTy = Context.getPointerType(ArgTy);
678 ArgTys.push_back(ArgTy);
679 }
680 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000681 MDecl->isVariadic(), 0);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000682 return T;
683}
684
Sebastian Redl8ebd8fb2009-01-26 19:54:48 +0000685/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
686/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
687/// they point to and return true. If T1 and T2 aren't pointer types
688/// or pointer-to-member types, or if they are not similar at this
689/// level, returns false and leaves T1 and T2 unchanged. Top-level
690/// qualifiers on T1 and T2 are ignored. This function will typically
691/// be called in a loop that successively "unwraps" pointer and
692/// pointer-to-member types to compare them at each level.
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000693bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2)
694{
695 const PointerType *T1PtrType = T1->getAsPointerType(),
696 *T2PtrType = T2->getAsPointerType();
697 if (T1PtrType && T2PtrType) {
698 T1 = T1PtrType->getPointeeType();
699 T2 = T2PtrType->getPointeeType();
700 return true;
701 }
702
Sebastian Redlba387562009-01-25 19:43:20 +0000703 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
704 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redlf41a58c2009-01-28 18:33:18 +0000705 if (T1MPType && T2MPType &&
706 Context.getCanonicalType(T1MPType->getClass()) ==
707 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redlba387562009-01-25 19:43:20 +0000708 T1 = T1MPType->getPointeeType();
709 T2 = T2MPType->getPointeeType();
710 return true;
711 }
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000712 return false;
713}
714
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000715Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000716 // C99 6.7.6: Type names have no identifier. This is already validated by
717 // the parser.
718 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
719
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000720 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000721
722 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000723
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000724 // Check that there are no default arguments (C++ only).
725 if (getLangOptions().CPlusPlus)
726 CheckExtraCXXDefaultArguments(D);
727
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000728 // In this context, we *do not* check D.getInvalidType(). If the declarator
729 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
730 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000731 return T.getAsOpaquePtr();
732}
733
Chris Lattner65a57042008-06-29 00:50:08 +0000734
735
736//===----------------------------------------------------------------------===//
737// Type Attribute Processing
738//===----------------------------------------------------------------------===//
739
740/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
741/// specified type. The attribute contains 1 argument, the id of the address
742/// space for the type.
743static void HandleAddressSpaceTypeAttribute(QualType &Type,
744 const AttributeList &Attr, Sema &S){
745 // If this type is already address space qualified, reject it.
746 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
747 // for two or more different address spaces."
748 if (Type.getAddressSpace()) {
749 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
750 return;
751 }
752
753 // Check the attribute arguments.
754 if (Attr.getNumArgs() != 1) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner65a57042008-06-29 00:50:08 +0000756 return;
757 }
758 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
759 llvm::APSInt addrSpace(32);
760 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000761 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
762 << ASArgExpr->getSourceRange();
Chris Lattner65a57042008-06-29 00:50:08 +0000763 return;
764 }
765
766 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
767 Type = S.Context.getASQualType(Type, ASIdx);
768}
769
770void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000771 // Scan through and apply attributes to this type where it makes sense. Some
772 // attributes (such as __address_space__, __vector_size__, etc) apply to the
773 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000774 // apply to the decl. Here we apply type attributes and ignore the rest.
775 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000776 // If this is an attribute we can handle, do so now, otherwise, add it to
777 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000778 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000779 default: break;
780 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000781 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
782 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000783 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000784 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000785}
786
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000787/// @brief If the type T is incomplete and cannot be completed,
788/// produce a suitable diagnostic.
789///
790/// This routine checks whether the type @p T is complete in any
791/// context where a complete type is required. If @p T is a complete
792/// type, returns false. If @p T is incomplete, issues the diagnostic
793/// @p diag (giving it the type @p T) and returns true.
794///
795/// @param Loc The location in the source that the incomplete type
796/// diagnostic should refer to.
797///
798/// @param T The type that this routine is examining for completeness.
799///
800/// @param diag The diagnostic value (e.g.,
801/// @c diag::err_typecheck_decl_incomplete_type) that will be used
802/// for the error message if @p T is incomplete.
803///
804/// @param Range1 An optional range in the source code that will be a
805/// part of the "incomplete type" error message.
806///
807/// @param Range2 An optional range in the source code that will be a
808/// part of the "incomplete type" error message.
809///
810/// @param PrintType If non-NULL, the type that should be printed
811/// instead of @p T. This parameter should be used when the type that
812/// we're checking for incompleteness isn't the type that should be
813/// displayed to the user, e.g., when T is a type and PrintType is a
814/// pointer to T.
815///
816/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
817/// @c false otherwise.
818///
819/// @todo When Clang gets proper support for C++ templates, this
820/// routine will also be able perform template instantiation when @p T
821/// is a class template specialization.
822bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
823 SourceRange Range1, SourceRange Range2,
824 QualType PrintType) {
825 // If we have a complete type, we're done.
826 if (!T->isIncompleteType())
827 return false;
Eli Friedman86ad5222008-05-27 03:33:27 +0000828
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000829 if (PrintType.isNull())
830 PrintType = T;
831
832 // We have an incomplete type. Produce a diagnostic.
833 Diag(Loc, diag) << PrintType << Range1 << Range2;
834
835 // If the type was a forward declaration of a class/struct/union
836 // type, produce
837 const TagType *Tag = 0;
838 if (const RecordType *Record = T->getAsRecordType())
839 Tag = Record;
840 else if (const EnumType *Enum = T->getAsEnumType())
841 Tag = Enum;
842
843 if (Tag && !Tag->getDecl()->isInvalidDecl())
844 Diag(Tag->getDecl()->getLocation(),
845 Tag->isBeingDefined() ? diag::note_type_being_defined
846 : diag::note_forward_declaration)
847 << QualType(Tag, 0);
848
849 return true;
850}