blob: ecae79320dd44fc5d297c4df69e709bc40719452 [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/Basic/Diagnostic.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
23/// type object. This returns null on error.
Chris Lattner99dbc962008-06-26 06:27:57 +000024QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000027 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000028
29 switch (DS.getTypeSpecType()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000030 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner5fcb38b2008-04-02 06:50:17 +000031 case DeclSpec::TST_void:
32 Result = Context.VoidTy;
33 break;
Chris Lattner4b009652007-07-25 00:24:17 +000034 case DeclSpec::TST_char:
35 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000036 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000037 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000038 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000039 else {
40 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
41 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000042 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000043 }
Chris Lattner06fb8672008-02-20 21:40:32 +000044 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000045 case DeclSpec::TST_wchar:
46 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
47 Result = Context.WCharTy;
48 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +000049 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
50 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000051 Result = Context.getSignedWCharType();
52 } else {
53 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
54 "Unknown TSS value");
Chris Lattner10f2c2e2008-11-20 06:38:18 +000055 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
56 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000057 Result = Context.getUnsignedWCharType();
58 }
59 break;
Chris Lattner6ab935b2008-04-05 06:32:51 +000060 case DeclSpec::TST_unspecified:
Chris Lattner4a68fe02008-07-26 00:46:50 +000061 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnercce42242008-10-20 02:01:50 +000062 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerada63792008-07-26 01:53:50 +000063 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner4a68fe02008-07-26 00:46:50 +000064 DS.getNumProtocolQualifiers());
65 break;
66 }
67
Chris Lattner6ab935b2008-04-05 06:32:51 +000068 // Unspecified typespec defaults to int in C90. However, the C90 grammar
69 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
70 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
71 // Note that the one exception to this is function definitions, which are
72 // allowed to be completely missing a declspec. This is handled in the
73 // parser already though by it pretending to have seen an 'int' in this
74 // case.
75 if (getLangOptions().ImplicitInt) {
76 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
77 DeclSpec::PQ_TypeSpecifier |
78 DeclSpec::PQ_TypeQualifier)) == 0)
79 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
80 } else {
81 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
82 // "At least one type specifier shall be given in the declaration
83 // specifiers in each declaration, and in the specifier-qualifier list in
84 // each struct declaration and type name."
85 if (!DS.hasTypeSpecifier())
86 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
87 }
88
89 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000090 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000091 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
92 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000093 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
94 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
95 case DeclSpec::TSW_long: Result = Context.LongTy; break;
96 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000097 }
98 } else {
99 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +0000100 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
101 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
102 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
103 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 }
105 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000106 break;
Chris Lattner5328f312007-08-21 17:02:28 +0000107 }
Chris Lattner726c5452008-02-20 23:53:49 +0000108 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +0000109 case DeclSpec::TST_double:
110 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +0000111 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000112 else
Chris Lattner726c5452008-02-20 23:53:49 +0000113 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000114 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000115 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +0000116 case DeclSpec::TST_decimal32: // _Decimal32
117 case DeclSpec::TST_decimal64: // _Decimal64
118 case DeclSpec::TST_decimal128: // _Decimal128
119 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +0000120 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000121 case DeclSpec::TST_enum:
122 case DeclSpec::TST_union:
123 case DeclSpec::TST_struct: {
124 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000125 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000126 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
127 DS.getTypeSpecSign() == 0 &&
128 "Can't handle qualifiers on typedef names yet!");
129 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000130 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000131 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000132 }
133 case DeclSpec::TST_typedef: {
134 Decl *D = static_cast<Decl *>(DS.getTypeRep());
135 assert(D && "Didn't get a decl for a typedef?");
136 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
137 DS.getTypeSpecSign() == 0 &&
138 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerada63792008-07-26 01:53:50 +0000139 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor1d661552008-04-13 21:07:44 +0000140
Steve Naroff81f1bba2007-09-06 21:24:23 +0000141 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
142 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000143 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000144 if (PQ == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000145 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000146 break;
147 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000148
Chris Lattner726c5452008-02-20 23:53:49 +0000149 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerada63792008-07-26 01:53:50 +0000150 (ObjCProtocolDecl**)PQ,
Chris Lattner69f01932008-02-21 01:32:26 +0000151 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000152 break;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000153 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000154 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
155 // id<protocol-list>
156 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
157 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000158 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000159 }
160 }
Chris Lattner4b009652007-07-25 00:24:17 +0000161 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000162 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000163 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000164 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000165 case DeclSpec::TST_typeofType:
166 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
167 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000168 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000169 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000170 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000171 case DeclSpec::TST_typeofExpr: {
172 Expr *E = static_cast<Expr *>(DS.getTypeRep());
173 assert(E && "Didn't get an expression for typeof?");
174 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000175 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000176 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000177 }
Chris Lattner4b009652007-07-25 00:24:17 +0000178 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000179
180 // Handle complex types.
181 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000182 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000183
184 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
185 "FIXME: imaginary types not supported yet!");
186
Chris Lattnerd496fb92008-02-20 22:04:11 +0000187 // See if there are any attributes on the declspec that apply to the type (as
188 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000189 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000190 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000191
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000192 // Apply const/volatile/restrict qualifiers to T.
193 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
194
195 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
196 // or incomplete types shall not be restrict-qualified." C++ also allows
197 // restrict-qualified references.
198 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000199 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
200 QualType EltTy = PT->getPointeeType();
201
202 // If we have a pointer or reference, the pointee must have an object or
203 // incomplete type.
204 if (!EltTy->isIncompleteOrObjectType()) {
205 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000206 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000207 << EltTy << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000208 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
209 }
210 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000211 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000212 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000213 << Result << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000214 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000215 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000216 }
217
218 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
219 // of a function type includes any type qualifiers, the behavior is
220 // undefined."
221 if (Result->isFunctionType() && TypeQuals) {
222 // Get some location to point at, either the C or V location.
223 SourceLocation Loc;
224 if (TypeQuals & QualType::Const)
225 Loc = DS.getConstSpecLoc();
226 else {
227 assert((TypeQuals & QualType::Volatile) &&
228 "Has CV quals but not C or V?");
229 Loc = DS.getVolatileSpecLoc();
230 }
Chris Lattner77d52da2008-11-20 06:06:08 +0000231 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000232 << Result << DS.getSourceRange();
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000233 }
234
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000235 // C++ [dcl.ref]p1:
236 // Cv-qualified references are ill-formed except when the
237 // cv-qualifiers are introduced through the use of a typedef
238 // (7.1.3) or of a template type argument (14.3), in which
239 // case the cv-qualifiers are ignored.
240 if (DS.getTypeSpecType() == DeclSpec::TST_typedef &&
241 TypeQuals && Result->isReferenceType()) {
242 TypeQuals &= ~QualType::Const;
243 TypeQuals &= ~QualType::Volatile;
244 }
245
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000246 Result = Result.getQualifiedType(TypeQuals);
247 }
Chris Lattner9e982502008-02-21 01:07:18 +0000248 return Result;
249}
250
Chris Lattner4b009652007-07-25 00:24:17 +0000251/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000252/// instances. Skip the outermost Skip type objects.
253QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000254 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000255 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000256 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
257 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
258
Chris Lattner726c5452008-02-20 23:53:49 +0000259 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000260
Chris Lattner4b009652007-07-25 00:24:17 +0000261 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
262 // are ordered from the identifier out, which is opposite of what we want :).
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000263 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
264 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Chris Lattner4b009652007-07-25 00:24:17 +0000265 switch (DeclType.Kind) {
266 default: assert(0 && "Unknown decltype!");
Steve Naroff7aa54752008-08-27 16:04:49 +0000267 case DeclaratorChunk::BlockPointer:
268 if (DeclType.Cls.TypeQuals)
269 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
270 if (!T.getTypePtr()->isFunctionType())
271 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
272 else
273 T = Context.getBlockPointerType(T);
274 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000275 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000276 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000277 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000278 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
279 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000280 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000281 T = Context.IntTy;
282 }
283
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000284 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
285 // object or incomplete types shall not be restrict-qualified."
286 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000287 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000288 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000289 << T;
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000290 DeclType.Ptr.TypeQuals &= QualType::Restrict;
291 }
292
Chris Lattner4b009652007-07-25 00:24:17 +0000293 // Apply the pointer typequals to the pointer object.
294 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
295 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000296 case DeclaratorChunk::Reference: {
297 // Whether we should suppress the creation of the reference.
298 bool SuppressReference = false;
299 if (T->isReferenceType()) {
300 // C++ [dcl.ref]p4: There shall be no references to references.
301 //
302 // According to C++ DR 106, references to references are only
303 // diagnosed when they are written directly (e.g., "int & &"),
304 // but not when they happen via a typedef:
305 //
306 // typedef int& intref;
307 // typedef intref& intref2;
308 //
309 // Parser::ParserDeclaratorInternal diagnoses the case where
310 // references are written directly; here, we handle the
311 // collapsing of references-to-references as described in C++
312 // DR 106 and amended by C++ DR 540.
313 SuppressReference = true;
314 }
315
316 // C++ [dcl.ref]p1:
317 // A declarator that specifies the type “reference to cv void”
318 // is ill-formed.
319 if (T->isVoidType()) {
320 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroff91b03f72007-08-28 03:03:08 +0000321 D.setInvalidType(true);
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000322 T = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000323 }
324
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000325 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
326 // object or incomplete types shall not be restrict-qualified."
327 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000328 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000329 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000330 << T;
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000331 DeclType.Ref.HasRestrict = false;
332 }
333
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000334 if (!SuppressReference)
335 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000336
337 // Handle restrict on references.
338 if (DeclType.Ref.HasRestrict)
339 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000340 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000341 }
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000343 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000344 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000345 ArrayType::ArraySizeModifier ASM;
346 if (ATI.isStar)
347 ASM = ArrayType::Star;
348 else if (ATI.hasStatic)
349 ASM = ArrayType::Static;
350 else
351 ASM = ArrayType::Normal;
352
353 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
354 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000355 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
356 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroff91b03f72007-08-28 03:03:08 +0000357 T = Context.IntTy;
358 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000359 } else if (T->isFunctionType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000360 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
361 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000362 T = Context.getPointerType(T);
363 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000364 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000365 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000366 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
367 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000368 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000369 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000370 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000371 // If the element type is a struct or union that contains a variadic
372 // array, reject it: C99 6.7.2.1p2.
373 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000374 Diag(DeclType.Loc, diag::err_flexible_array_in_array) << T;
Steve Naroff91b03f72007-08-28 03:03:08 +0000375 T = Context.IntTy;
376 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000377 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000378 } else if (T->isObjCInterfaceType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000379 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Chris Lattner4b009652007-07-25 00:24:17 +0000380 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000381
Steve Naroff63b6a642007-08-30 22:35:45 +0000382 // C99 6.7.5.2p1: The size expression shall have integer type.
383 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000384 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000385 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000386 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000387 delete ArraySize;
388 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000389 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000390 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000391 if (!ArraySize) {
392 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000393 } else if (ArraySize->isValueDependent()) {
394 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000395 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000396 !T->isConstantSizeType()) {
Eli Friedman2ff28d12008-05-14 00:40:18 +0000397 // Per C99, a variable array is an array with either a non-constant
398 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000399 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000400 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000401 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
402 // have a value greater than zero.
403 if (ConstVal.isSigned()) {
404 if (ConstVal.isNegative()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000405 Diag(ArraySize->getLocStart(),
406 diag::err_typecheck_negative_array_size)
407 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000408 D.setInvalidType(true);
409 } else if (ConstVal == 0) {
410 // GCC accepts zero sized static arrays.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000411 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
412 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000413 }
414 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000415 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000416 }
Chris Lattner47958f62007-08-28 16:54:00 +0000417 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattner306d4df2008-12-18 06:50:14 +0000418 if (!getLangOptions().C99) {
419 if (ArraySize && !ArraySize->isValueDependent() &&
420 !ArraySize->isIntegerConstantExpr(Context))
421 Diag(D.getIdentifierLoc(), diag::ext_vla);
422 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
423 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
424 }
Chris Lattner4b009652007-07-25 00:24:17 +0000425 break;
426 }
427 case DeclaratorChunk::Function:
428 // If the function declarator has a prototype (i.e. it is not () and
429 // does not have a K&R-style identifier list), then the arguments are part
430 // of the type, otherwise the argument list is ().
431 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000432
Chris Lattner6ad7e882007-12-19 05:31:29 +0000433 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000434 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000435 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattner6ad7e882007-12-19 05:31:29 +0000436 T = Context.IntTy;
437 D.setInvalidType(true);
438 }
439
Eli Friedman769e7302008-08-25 21:31:01 +0000440 if (FTI.NumArgs == 0) {
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000441 if (getLangOptions().CPlusPlus) {
442 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
443 // function takes no arguments.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000444 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000445 } else {
446 // Simple void foo(), where the incoming T is the result type.
447 T = Context.getFunctionTypeNoProto(T);
448 }
Eli Friedman769e7302008-08-25 21:31:01 +0000449 } else if (FTI.ArgInfo[0].Param == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000450 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedman769e7302008-08-25 21:31:01 +0000451 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Chris Lattner4b009652007-07-25 00:24:17 +0000452 } else {
453 // Otherwise, we have a function with an argument list that is
454 // potentially variadic.
455 llvm::SmallVector<QualType, 16> ArgTys;
456
457 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000458 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
459 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000460 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000461 //
462 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
463 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000464 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000465 // argument type in the function prototype *will not* match the
466 // type in ParmVarDecl (which makes the code generator unhappy).
467 //
468 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000469 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000470 // it should be driving off the type being created here.
471 //
472 // FIXME: If a source translation tool needs to see the original type,
473 // then we need to consider storing both types somewhere...
474 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000475 if (ArgTy->isArrayType()) {
476 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000477 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000478 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000479
Chris Lattner4b009652007-07-25 00:24:17 +0000480 // Look for 'void'. void is allowed only as a single argument to a
481 // function with no other parameters (C99 6.7.5.3p10). We record
482 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000483 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000484 // If this is something like 'float(int, void)', reject it. 'void'
485 // is an incomplete type (C99 6.2.5p19) and function decls cannot
486 // have arguments of incomplete type.
487 if (FTI.NumArgs != 1 || FTI.isVariadic) {
488 Diag(DeclType.Loc, diag::err_void_only_param);
489 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000490 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000491 } else if (FTI.ArgInfo[i].Ident) {
492 // Reject, but continue to parse 'int(void abc)'.
493 Diag(FTI.ArgInfo[i].IdentLoc,
494 diag::err_param_with_void_type);
495 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000496 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 } else {
498 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000499 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000500 Diag(DeclType.Loc, diag::err_void_param_qualified);
501
502 // Do not add 'void' to the ArgTys list.
503 break;
504 }
Eli Friedman769e7302008-08-25 21:31:01 +0000505 } else if (!FTI.hasPrototype) {
506 if (ArgTy->isPromotableIntegerType()) {
507 ArgTy = Context.IntTy;
508 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
509 if (BTy->getKind() == BuiltinType::Float)
510 ArgTy = Context.DoubleTy;
511 }
Chris Lattner4b009652007-07-25 00:24:17 +0000512 }
513
514 ArgTys.push_back(ArgTy);
515 }
516 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000517 FTI.isVariadic, FTI.TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
519 break;
520 }
Chris Lattner65a57042008-06-29 00:50:08 +0000521
522 // See if there are any attributes on this declarator chunk.
523 if (const AttributeList *AL = DeclType.getAttrs())
524 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000525 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000526
527 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
528 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
529 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
530
531 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
532 // for a nonstatic member function, the function type to which a pointer
533 // to member refers, or the top-level function type of a function typedef
534 // declaration.
535 if (FnTy->getTypeQuals() != 0 &&
536 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregorc5cbc242008-12-15 23:53:10 +0000537 ((D.getContext() != Declarator::MemberContext &&
538 (!D.getCXXScopeSpec().isSet() ||
539 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregor723d3332009-01-07 00:43:41 +0000540 ->isRecord())) ||
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000541 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000542 if (D.isFunctionDeclarator())
543 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
544 else
545 Diag(D.getIdentifierLoc(),
546 diag::err_invalid_qualified_typedef_function_type_use);
547
548 // Strip the cv-quals from the type.
549 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000550 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000551 }
552 }
Chris Lattner4b009652007-07-25 00:24:17 +0000553
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000554 // If there were any type attributes applied to the decl itself (not the
555 // type, apply the type attribute to the type!)
556 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000557 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000558
Chris Lattner4b009652007-07-25 00:24:17 +0000559 return T;
560}
561
Ted Kremenek42730c52008-01-07 19:49:32 +0000562/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000563/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000564QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
565 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000566 QualType T = MDecl->getResultType();
567 llvm::SmallVector<QualType, 16> ArgTys;
568
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000569 // Add the first two invisible argument types for self and _cmd.
Douglas Gregor5d764842009-01-09 17:18:27 +0000570 if (MDecl->isInstanceMethod()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000571 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000572 selfTy = Context.getPointerType(selfTy);
573 ArgTys.push_back(selfTy);
574 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000575 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000576 ArgTys.push_back(Context.getObjCIdType());
577 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000578
Chris Lattner685d7922008-03-16 01:07:14 +0000579 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000580 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
581 QualType ArgTy = PDecl->getType();
582 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000583 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
584 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000585 // Sema::ActOnParamDeclarator().
586 if (ArgTy->isArrayType())
587 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000588 else if (ArgTy->isFunctionType())
589 ArgTy = Context.getPointerType(ArgTy);
590 ArgTys.push_back(ArgTy);
591 }
592 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000593 MDecl->isVariadic(), 0);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000594 return T;
595}
596
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000597/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types (FIXME:
598/// or pointer-to-member types) that may be similar (C++ 4.4),
599/// replaces T1 and T2 with the type that they point to and return
600/// true. If T1 and T2 aren't pointer types or pointer-to-member
601/// types, or if they are not similar at this level, returns false and
602/// leaves T1 and T2 unchanged. Top-level qualifiers on T1 and T2 are
Douglas Gregorf8e92702008-10-24 15:36:09 +0000603/// ignored. This function will typically be called in a loop that
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000604/// successively "unwraps" pointer and pointer-to-member types to
605/// compare them at each level.
606bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2)
607{
608 const PointerType *T1PtrType = T1->getAsPointerType(),
609 *T2PtrType = T2->getAsPointerType();
610 if (T1PtrType && T2PtrType) {
611 T1 = T1PtrType->getPointeeType();
612 T2 = T2PtrType->getPointeeType();
613 return true;
614 }
615
616 // FIXME: pointer-to-member types
617 return false;
618}
619
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000620Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000621 // C99 6.7.6: Type names have no identifier. This is already validated by
622 // the parser.
623 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
624
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000625 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000626
627 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000628
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000629 // Check that there are no default arguments (C++ only).
630 if (getLangOptions().CPlusPlus)
631 CheckExtraCXXDefaultArguments(D);
632
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000633 // In this context, we *do not* check D.getInvalidType(). If the declarator
634 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
635 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000636 return T.getAsOpaquePtr();
637}
638
Chris Lattner65a57042008-06-29 00:50:08 +0000639
640
641//===----------------------------------------------------------------------===//
642// Type Attribute Processing
643//===----------------------------------------------------------------------===//
644
645/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
646/// specified type. The attribute contains 1 argument, the id of the address
647/// space for the type.
648static void HandleAddressSpaceTypeAttribute(QualType &Type,
649 const AttributeList &Attr, Sema &S){
650 // If this type is already address space qualified, reject it.
651 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
652 // for two or more different address spaces."
653 if (Type.getAddressSpace()) {
654 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
655 return;
656 }
657
658 // Check the attribute arguments.
659 if (Attr.getNumArgs() != 1) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner65a57042008-06-29 00:50:08 +0000661 return;
662 }
663 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
664 llvm::APSInt addrSpace(32);
665 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
667 << ASArgExpr->getSourceRange();
Chris Lattner65a57042008-06-29 00:50:08 +0000668 return;
669 }
670
671 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
672 Type = S.Context.getASQualType(Type, ASIdx);
673}
674
675void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000676 // Scan through and apply attributes to this type where it makes sense. Some
677 // attributes (such as __address_space__, __vector_size__, etc) apply to the
678 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000679 // apply to the decl. Here we apply type attributes and ignore the rest.
680 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000681 // If this is an attribute we can handle, do so now, otherwise, add it to
682 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000683 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000684 default: break;
685 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000686 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
687 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000688 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000689 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000690}
691
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000692/// @brief If the type T is incomplete and cannot be completed,
693/// produce a suitable diagnostic.
694///
695/// This routine checks whether the type @p T is complete in any
696/// context where a complete type is required. If @p T is a complete
697/// type, returns false. If @p T is incomplete, issues the diagnostic
698/// @p diag (giving it the type @p T) and returns true.
699///
700/// @param Loc The location in the source that the incomplete type
701/// diagnostic should refer to.
702///
703/// @param T The type that this routine is examining for completeness.
704///
705/// @param diag The diagnostic value (e.g.,
706/// @c diag::err_typecheck_decl_incomplete_type) that will be used
707/// for the error message if @p T is incomplete.
708///
709/// @param Range1 An optional range in the source code that will be a
710/// part of the "incomplete type" error message.
711///
712/// @param Range2 An optional range in the source code that will be a
713/// part of the "incomplete type" error message.
714///
715/// @param PrintType If non-NULL, the type that should be printed
716/// instead of @p T. This parameter should be used when the type that
717/// we're checking for incompleteness isn't the type that should be
718/// displayed to the user, e.g., when T is a type and PrintType is a
719/// pointer to T.
720///
721/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
722/// @c false otherwise.
723///
724/// @todo When Clang gets proper support for C++ templates, this
725/// routine will also be able perform template instantiation when @p T
726/// is a class template specialization.
727bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
728 SourceRange Range1, SourceRange Range2,
729 QualType PrintType) {
730 // If we have a complete type, we're done.
731 if (!T->isIncompleteType())
732 return false;
Eli Friedman86ad5222008-05-27 03:33:27 +0000733
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000734 if (PrintType.isNull())
735 PrintType = T;
736
737 // We have an incomplete type. Produce a diagnostic.
738 Diag(Loc, diag) << PrintType << Range1 << Range2;
739
740 // If the type was a forward declaration of a class/struct/union
741 // type, produce
742 const TagType *Tag = 0;
743 if (const RecordType *Record = T->getAsRecordType())
744 Tag = Record;
745 else if (const EnumType *Enum = T->getAsEnumType())
746 Tag = Enum;
747
748 if (Tag && !Tag->getDecl()->isInvalidDecl())
749 Diag(Tag->getDecl()->getLocation(),
750 Tag->isBeingDefined() ? diag::note_type_being_defined
751 : diag::note_forward_declaration)
752 << QualType(Tag, 0);
753
754 return true;
755}