blob: 30af618c26ea206e429301a596caf1fa4432ed64 [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"
16#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner11f20f92007-08-28 16:40:32 +000019#include "clang/Basic/LangOptions.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
23/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
24/// type object. This returns null on error.
Chris Lattner99dbc962008-06-26 06:27:57 +000025QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000026 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
27 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000028 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000029
30 switch (DS.getTypeSpecType()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000031 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner5fcb38b2008-04-02 06:50:17 +000032 case DeclSpec::TST_void:
33 Result = Context.VoidTy;
34 break;
Chris Lattner4b009652007-07-25 00:24:17 +000035 case DeclSpec::TST_char:
36 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000037 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000038 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000039 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000040 else {
41 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
42 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000043 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000044 }
Chris Lattner06fb8672008-02-20 21:40:32 +000045 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000046 case DeclSpec::TST_wchar:
47 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
48 Result = Context.WCharTy;
49 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
50 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
51 DS.getSpecifierName(DS.getTypeSpecType()));
52 Result = Context.getSignedWCharType();
53 } else {
54 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
55 "Unknown TSS value");
56 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
57 DS.getSpecifierName(DS.getTypeSpecType()));
58 Result = Context.getUnsignedWCharType();
59 }
60 break;
Chris Lattner6ab935b2008-04-05 06:32:51 +000061 case DeclSpec::TST_unspecified:
Chris Lattner4a68fe02008-07-26 00:46:50 +000062 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnerada63792008-07-26 01:53:50 +000063 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
64 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner4a68fe02008-07-26 00:46:50 +000065 DS.getNumProtocolQualifiers());
66 break;
67 }
68
Chris Lattner6ab935b2008-04-05 06:32:51 +000069 // Unspecified typespec defaults to int in C90. However, the C90 grammar
70 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
71 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
72 // Note that the one exception to this is function definitions, which are
73 // allowed to be completely missing a declspec. This is handled in the
74 // parser already though by it pretending to have seen an 'int' in this
75 // case.
76 if (getLangOptions().ImplicitInt) {
77 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
78 DeclSpec::PQ_TypeSpecifier |
79 DeclSpec::PQ_TypeQualifier)) == 0)
80 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
81 } else {
82 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
83 // "At least one type specifier shall be given in the declaration
84 // specifiers in each declaration, and in the specifier-qualifier list in
85 // each struct declaration and type name."
86 if (!DS.hasTypeSpecifier())
87 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
88 }
89
90 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000091 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000092 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
93 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000094 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
95 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
96 case DeclSpec::TSW_long: Result = Context.LongTy; break;
97 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000098 }
99 } else {
100 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +0000101 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
102 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
103 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
104 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000105 }
106 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000107 break;
Chris Lattner5328f312007-08-21 17:02:28 +0000108 }
Chris Lattner726c5452008-02-20 23:53:49 +0000109 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +0000110 case DeclSpec::TST_double:
111 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +0000112 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000113 else
Chris Lattner726c5452008-02-20 23:53:49 +0000114 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000115 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000116 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +0000117 case DeclSpec::TST_decimal32: // _Decimal32
118 case DeclSpec::TST_decimal64: // _Decimal64
119 case DeclSpec::TST_decimal128: // _Decimal128
120 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +0000121 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000122 case DeclSpec::TST_enum:
123 case DeclSpec::TST_union:
124 case DeclSpec::TST_struct: {
125 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000126 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000127 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
128 DS.getTypeSpecSign() == 0 &&
129 "Can't handle qualifiers on typedef names yet!");
130 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000131 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000132 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000133 }
134 case DeclSpec::TST_typedef: {
135 Decl *D = static_cast<Decl *>(DS.getTypeRep());
136 assert(D && "Didn't get a decl for a typedef?");
137 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
138 DS.getTypeSpecSign() == 0 &&
139 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerada63792008-07-26 01:53:50 +0000140 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor1d661552008-04-13 21:07:44 +0000141
Steve Naroff81f1bba2007-09-06 21:24:23 +0000142 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
143 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000144 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000145 if (PQ == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000146 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000147 break;
148 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000149
Chris Lattner726c5452008-02-20 23:53:49 +0000150 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerada63792008-07-26 01:53:50 +0000151 (ObjCProtocolDecl**)PQ,
Chris Lattner69f01932008-02-21 01:32:26 +0000152 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000153 break;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000154 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000155 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
156 // id<protocol-list>
157 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
158 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000159 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000160 }
161 }
Chris Lattner4b009652007-07-25 00:24:17 +0000162 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000163 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000164 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000165 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000166 case DeclSpec::TST_typeofType:
167 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
168 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000169 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000170 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000171 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000172 case DeclSpec::TST_typeofExpr: {
173 Expr *E = static_cast<Expr *>(DS.getTypeRep());
174 assert(E && "Didn't get an expression for typeof?");
175 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000176 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000177 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000178 }
Chris Lattner4b009652007-07-25 00:24:17 +0000179 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000180
181 // Handle complex types.
182 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000183 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000184
185 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
186 "FIXME: imaginary types not supported yet!");
187
Chris Lattnerd496fb92008-02-20 22:04:11 +0000188 // See if there are any attributes on the declspec that apply to the type (as
189 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000190 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000191 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000192
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000193 // Apply const/volatile/restrict qualifiers to T.
194 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
195
196 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
197 // or incomplete types shall not be restrict-qualified." C++ also allows
198 // restrict-qualified references.
199 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000200 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
201 QualType EltTy = PT->getPointeeType();
202
203 // If we have a pointer or reference, the pointee must have an object or
204 // incomplete type.
205 if (!EltTy->isIncompleteOrObjectType()) {
206 Diag(DS.getRestrictSpecLoc(),
207 diag::err_typecheck_invalid_restrict_invalid_pointee,
208 EltTy.getAsString(), DS.getSourceRange());
209 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
210 }
211 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000212 Diag(DS.getRestrictSpecLoc(),
213 diag::err_typecheck_invalid_restrict_not_pointer,
214 Result.getAsString(), DS.getSourceRange());
Chris Lattnercfac88d2008-04-02 17:35:06 +0000215 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000216 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000217 }
218
219 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
220 // of a function type includes any type qualifiers, the behavior is
221 // undefined."
222 if (Result->isFunctionType() && TypeQuals) {
223 // Get some location to point at, either the C or V location.
224 SourceLocation Loc;
225 if (TypeQuals & QualType::Const)
226 Loc = DS.getConstSpecLoc();
227 else {
228 assert((TypeQuals & QualType::Volatile) &&
229 "Has CV quals but not C or V?");
230 Loc = DS.getVolatileSpecLoc();
231 }
232 Diag(Loc, diag::warn_typecheck_function_qualifiers,
233 Result.getAsString(), DS.getSourceRange());
234 }
235
236 Result = Result.getQualifiedType(TypeQuals);
237 }
Chris Lattner9e982502008-02-21 01:07:18 +0000238 return Result;
239}
240
Chris Lattner4b009652007-07-25 00:24:17 +0000241/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
242/// instances.
243QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000244 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000245 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000246 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
247 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
248
Chris Lattner726c5452008-02-20 23:53:49 +0000249 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000250
Chris Lattner4b009652007-07-25 00:24:17 +0000251 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
252 // are ordered from the identifier out, which is opposite of what we want :).
253 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner69f01932008-02-21 01:32:26 +0000254 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000255 switch (DeclType.Kind) {
256 default: assert(0 && "Unknown decltype!");
257 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000258 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000259 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000260 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000261 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000262 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000263 T = Context.IntTy;
264 }
265
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000266 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
267 // object or incomplete types shall not be restrict-qualified."
268 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000269 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000270 Diag(DeclType.Loc,
271 diag::err_typecheck_invalid_restrict_invalid_pointee,
272 T.getAsString());
273 DeclType.Ptr.TypeQuals &= QualType::Restrict;
274 }
275
Chris Lattner4b009652007-07-25 00:24:17 +0000276 // Apply the pointer typequals to the pointer object.
277 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
278 break;
279 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000280 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000281 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000282 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000283 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000284 D.setInvalidType(true);
Chris Lattnercfac88d2008-04-02 17:35:06 +0000285 T = RT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000286 }
287
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000288 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
289 // object or incomplete types shall not be restrict-qualified."
290 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000291 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000292 Diag(DeclType.Loc,
293 diag::err_typecheck_invalid_restrict_invalid_pointee,
294 T.getAsString());
295 DeclType.Ref.HasRestrict = false;
296 }
297
Chris Lattner4b009652007-07-25 00:24:17 +0000298 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000299
300 // Handle restrict on references.
301 if (DeclType.Ref.HasRestrict)
302 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000303 break;
304 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000305 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000306 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000307 ArrayType::ArraySizeModifier ASM;
308 if (ATI.isStar)
309 ASM = ArrayType::Star;
310 else if (ATI.hasStatic)
311 ASM = ArrayType::Static;
312 else
313 ASM = ArrayType::Normal;
314
315 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
316 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
317 if (T->isIncompleteType()) {
318 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
319 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000320 T = Context.IntTy;
321 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000322 } else if (T->isFunctionType()) {
323 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000324 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000325 T = Context.getPointerType(T);
326 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000327 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000328 // C++ 8.3.2p4: There shall be no ... arrays of references ...
329 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000330 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000331 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000332 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000333 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000334 // If the element type is a struct or union that contains a variadic
335 // array, reject it: C99 6.7.2.1p2.
336 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
337 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
338 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000339 T = Context.IntTy;
340 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000341 }
342 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000343 // C99 6.7.5.2p1: The size expression shall have integer type.
344 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
345 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
346 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
347 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000348 delete ArraySize;
349 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000350 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000351 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000352 if (!ArraySize) {
353 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000354 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
355 !T->isConstantSizeType()) {
356 // Per C99, a variable array is an array with either a non-constant
357 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000358 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000359 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000360 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
361 // have a value greater than zero.
362 if (ConstVal.isSigned()) {
363 if (ConstVal.isNegative()) {
364 Diag(ArraySize->getLocStart(),
365 diag::err_typecheck_negative_array_size,
366 ArraySize->getSourceRange());
367 D.setInvalidType(true);
368 } else if (ConstVal == 0) {
369 // GCC accepts zero sized static arrays.
370 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
371 ArraySize->getSourceRange());
372 }
373 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000374 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000375 }
Chris Lattner47958f62007-08-28 16:54:00 +0000376 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
377 if (!getLangOptions().C99 &&
378 (ASM != ArrayType::Normal ||
379 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
380 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000381 break;
382 }
383 case DeclaratorChunk::Function:
384 // If the function declarator has a prototype (i.e. it is not () and
385 // does not have a K&R-style identifier list), then the arguments are part
386 // of the type, otherwise the argument list is ().
387 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000388
Chris Lattner6ad7e882007-12-19 05:31:29 +0000389 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000390 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000391 Diag(DeclType.Loc, diag::err_func_returning_array_function,
392 T.getAsString());
393 T = Context.IntTy;
394 D.setInvalidType(true);
395 }
396
Chris Lattner4b009652007-07-25 00:24:17 +0000397 if (!FTI.hasPrototype) {
398 // Simple void foo(), where the incoming T is the result type.
399 T = Context.getFunctionTypeNoProto(T);
400
401 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
402 if (FTI.NumArgs != 0)
403 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
404
405 } else {
406 // Otherwise, we have a function with an argument list that is
407 // potentially variadic.
408 llvm::SmallVector<QualType, 16> ArgTys;
409
410 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000411 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
412 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000413 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000414 //
415 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
416 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000417 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000418 // argument type in the function prototype *will not* match the
419 // type in ParmVarDecl (which makes the code generator unhappy).
420 //
421 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000422 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000423 // it should be driving off the type being created here.
424 //
425 // FIXME: If a source translation tool needs to see the original type,
426 // then we need to consider storing both types somewhere...
427 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000428 if (ArgTy->isArrayType()) {
429 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000430 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000431 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000432
Chris Lattner4b009652007-07-25 00:24:17 +0000433 // Look for 'void'. void is allowed only as a single argument to a
434 // function with no other parameters (C99 6.7.5.3p10). We record
435 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000436 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000437 // If this is something like 'float(int, void)', reject it. 'void'
438 // is an incomplete type (C99 6.2.5p19) and function decls cannot
439 // have arguments of incomplete type.
440 if (FTI.NumArgs != 1 || FTI.isVariadic) {
441 Diag(DeclType.Loc, diag::err_void_only_param);
442 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000443 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000444 } else if (FTI.ArgInfo[i].Ident) {
445 // Reject, but continue to parse 'int(void abc)'.
446 Diag(FTI.ArgInfo[i].IdentLoc,
447 diag::err_param_with_void_type);
448 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000449 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000450 } else {
451 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000452 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000453 Diag(DeclType.Loc, diag::err_void_param_qualified);
454
455 // Do not add 'void' to the ArgTys list.
456 break;
457 }
458 }
459
460 ArgTys.push_back(ArgTy);
461 }
462 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
463 FTI.isVariadic);
464 }
465 break;
466 }
Chris Lattner65a57042008-06-29 00:50:08 +0000467
468 // See if there are any attributes on this declarator chunk.
469 if (const AttributeList *AL = DeclType.getAttrs())
470 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000471 }
472
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000473 // If there were any type attributes applied to the decl itself (not the
474 // type, apply the type attribute to the type!)
475 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000476 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000477
Chris Lattner4b009652007-07-25 00:24:17 +0000478 return T;
479}
480
Ted Kremenek42730c52008-01-07 19:49:32 +0000481/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000482/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000483QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
484 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000485 QualType T = MDecl->getResultType();
486 llvm::SmallVector<QualType, 16> ArgTys;
487
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000488 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000489 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000491 selfTy = Context.getPointerType(selfTy);
492 ArgTys.push_back(selfTy);
493 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000494 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000495 ArgTys.push_back(Context.getObjCIdType());
496 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000497
Chris Lattner685d7922008-03-16 01:07:14 +0000498 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000499 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
500 QualType ArgTy = PDecl->getType();
501 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000502 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
503 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000504 // Sema::ActOnParamDeclarator().
505 if (ArgTy->isArrayType())
506 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000507 else if (ArgTy->isFunctionType())
508 ArgTy = Context.getPointerType(ArgTy);
509 ArgTys.push_back(ArgTy);
510 }
511 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000512 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000513 return T;
514}
515
Steve Naroff0acc9c92007-09-15 18:49:24 +0000516Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000517 // C99 6.7.6: Type names have no identifier. This is already validated by
518 // the parser.
519 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
520
521 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000522
523 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000524
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000525 // Check that there are no default arguments (C++ only).
526 if (getLangOptions().CPlusPlus)
527 CheckExtraCXXDefaultArguments(D);
528
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000529 // In this context, we *do not* check D.getInvalidType(). If the declarator
530 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
531 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000532 return T.getAsOpaquePtr();
533}
534
Chris Lattner65a57042008-06-29 00:50:08 +0000535
536
537//===----------------------------------------------------------------------===//
538// Type Attribute Processing
539//===----------------------------------------------------------------------===//
540
541/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
542/// specified type. The attribute contains 1 argument, the id of the address
543/// space for the type.
544static void HandleAddressSpaceTypeAttribute(QualType &Type,
545 const AttributeList &Attr, Sema &S){
546 // If this type is already address space qualified, reject it.
547 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
548 // for two or more different address spaces."
549 if (Type.getAddressSpace()) {
550 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
551 return;
552 }
553
554 // Check the attribute arguments.
555 if (Attr.getNumArgs() != 1) {
556 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
557 std::string("1"));
558 return;
559 }
560 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
561 llvm::APSInt addrSpace(32);
562 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
563 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
564 ASArgExpr->getSourceRange());
565 return;
566 }
567
568 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
569 Type = S.Context.getASQualType(Type, ASIdx);
570}
571
572void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000573 // Scan through and apply attributes to this type where it makes sense. Some
574 // attributes (such as __address_space__, __vector_size__, etc) apply to the
575 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000576 // apply to the decl. Here we apply type attributes and ignore the rest.
577 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000578 // If this is an attribute we can handle, do so now, otherwise, add it to
579 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000580 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000581 default: break;
582 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000583 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
584 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000585 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000586 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000587}
588
Eli Friedman86ad5222008-05-27 03:33:27 +0000589