blob: 3bc0e760ba9fb59e13223fdd24fe2aa28fa0257b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Lex/IdentifierTable.h"
Chris Lattner11f20f92007-08-28 16:40:32 +000019#include "clang/Basic/LangOptions.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.
24static QualType ConvertDeclSpecToType(const DeclSpec &DS, ASTContext &Ctx) {
25 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
27
28 switch (DS.getTypeSpecType()) {
29 default: return QualType(); // FIXME: Handle unimp cases!
30 case DeclSpec::TST_void: return Ctx.VoidTy;
31 case DeclSpec::TST_char:
32 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
33 return Ctx.CharTy;
34 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
35 return Ctx.SignedCharTy;
36 else {
37 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
38 "Unknown TSS value");
39 return Ctx.UnsignedCharTy;
40 }
41 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner5328f312007-08-21 17:02:28 +000042 case DeclSpec::TST_int: {
43 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000044 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
45 switch (DS.getTypeSpecWidth()) {
Chris Lattner5328f312007-08-21 17:02:28 +000046 case DeclSpec::TSW_unspecified: Result = Ctx.IntTy; break;
47 case DeclSpec::TSW_short: Result = Ctx.ShortTy; break;
48 case DeclSpec::TSW_long: Result = Ctx.LongTy; break;
49 case DeclSpec::TSW_longlong: Result = Ctx.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000050 }
51 } else {
52 switch (DS.getTypeSpecWidth()) {
Chris Lattner5328f312007-08-21 17:02:28 +000053 case DeclSpec::TSW_unspecified: Result = Ctx.UnsignedIntTy; break;
54 case DeclSpec::TSW_short: Result = Ctx.UnsignedShortTy; break;
55 case DeclSpec::TSW_long: Result = Ctx.UnsignedLongTy; break;
56 case DeclSpec::TSW_longlong: Result = Ctx.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000057 }
58 }
Chris Lattner5328f312007-08-21 17:02:28 +000059 // Handle complex integer types.
60 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
61 return Result;
62 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
63 "FIXME: imaginary types not supported yet!");
64 return Ctx.getComplexType(Result);
65 }
Chris Lattner4b009652007-07-25 00:24:17 +000066 case DeclSpec::TST_float:
67 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
68 return Ctx.FloatTy;
69 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
70 "FIXME: imaginary types not supported yet!");
Chris Lattner5328f312007-08-21 17:02:28 +000071 return Ctx.getComplexType(Ctx.FloatTy);
Chris Lattner4b009652007-07-25 00:24:17 +000072
73 case DeclSpec::TST_double: {
74 bool isLong = DS.getTypeSpecWidth() == DeclSpec::TSW_long;
Chris Lattner5328f312007-08-21 17:02:28 +000075 QualType T = isLong ? Ctx.LongDoubleTy : Ctx.DoubleTy;
Chris Lattner4b009652007-07-25 00:24:17 +000076 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
Chris Lattner5328f312007-08-21 17:02:28 +000077 return T;
Chris Lattner4b009652007-07-25 00:24:17 +000078 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
79 "FIXME: imaginary types not supported yet!");
Chris Lattner5328f312007-08-21 17:02:28 +000080 return Ctx.getComplexType(T);
Chris Lattner4b009652007-07-25 00:24:17 +000081 }
82 case DeclSpec::TST_bool: // _Bool or bool
83 return Ctx.BoolTy;
84 case DeclSpec::TST_decimal32: // _Decimal32
85 case DeclSpec::TST_decimal64: // _Decimal64
86 case DeclSpec::TST_decimal128: // _Decimal128
87 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
88 case DeclSpec::TST_enum:
89 case DeclSpec::TST_union:
90 case DeclSpec::TST_struct: {
91 Decl *D = static_cast<Decl *>(DS.getTypeRep());
92 assert(D && "Didn't get a decl for a enum/union/struct?");
93 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
94 DS.getTypeSpecSign() == 0 &&
95 "Can't handle qualifiers on typedef names yet!");
96 // TypeQuals handled by caller.
97 return Ctx.getTagDeclType(cast<TagDecl>(D));
98 }
99 case DeclSpec::TST_typedef: {
100 Decl *D = static_cast<Decl *>(DS.getTypeRep());
101 assert(D && "Didn't get a decl for a typedef?");
102 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
103 DS.getTypeSpecSign() == 0 &&
104 "Can't handle qualifiers on typedef names yet!");
105 // TypeQuals handled by caller.
106 return Ctx.getTypedefType(cast<TypedefDecl>(D));
107 }
Steve Naroff7cbb1462007-07-31 12:34:36 +0000108 case DeclSpec::TST_typeofType: {
109 QualType T = QualType::getFromOpaquePtr(DS.getTypeRep());
110 assert(!T.isNull() && "Didn't get a type for typeof?");
111 // TypeQuals handled by caller.
112 return Ctx.getTypeOfType(T);
113 }
114 case DeclSpec::TST_typeofExpr: {
115 Expr *E = static_cast<Expr *>(DS.getTypeRep());
116 assert(E && "Didn't get an expression for typeof?");
117 // TypeQuals handled by caller.
Steve Naroff11b649c2007-08-01 17:20:42 +0000118 return Ctx.getTypeOfExpr(E);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000119 }
Chris Lattner4b009652007-07-25 00:24:17 +0000120 }
121}
122
123/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
124/// instances.
125QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000126 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000127 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000128 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
129 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
130
Chris Lattner4b009652007-07-25 00:24:17 +0000131 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
Steve Naroff91b03f72007-08-28 03:03:08 +0000132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 // Apply const/volatile/restrict qualifiers to T.
134 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
135
136 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
137 // are ordered from the identifier out, which is opposite of what we want :).
138 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
139 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
140 switch (DeclType.Kind) {
141 default: assert(0 && "Unknown decltype!");
142 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000143 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 // C++ 8.3.2p4: There shall be no ... pointers to references ...
145 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
146 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000147 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 T = Context.IntTy;
149 }
150
151 // Apply the pointer typequals to the pointer object.
152 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
153 break;
154 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000155 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000156 // C++ 8.3.2p4: There shall be no references to references ...
157 Diag(D.getIdentifierLoc(),
158 diag::err_illegal_decl_reference_to_reference,
159 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000160 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 T = RT->getReferenceeType();
162 }
163
164 T = Context.getReferenceType(T);
165 break;
166 case DeclaratorChunk::Array: {
167 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000168 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000169 ArrayType::ArraySizeModifier ASM;
170 if (ATI.isStar)
171 ASM = ArrayType::Star;
172 else if (ATI.hasStatic)
173 ASM = ArrayType::Static;
174 else
175 ASM = ArrayType::Normal;
176
177 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
178 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
179 if (T->isIncompleteType()) {
180 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
181 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000182 T = Context.IntTy;
183 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000184 } else if (T->isFunctionType()) {
185 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
186 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000187 T = Context.getPointerType(T);
188 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000189 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000190 // C++ 8.3.2p4: There shall be no ... arrays of references ...
191 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
192 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000193 T = RT->getReferenceeType();
194 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000195 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000196 // If the element type is a struct or union that contains a variadic
197 // array, reject it: C99 6.7.2.1p2.
198 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
199 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
200 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000201 T = Context.IntTy;
202 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000203 }
204 }
Chris Lattner47958f62007-08-28 16:54:00 +0000205 T = Context.getArrayType(T, ASM, ATI.TypeQuals, ArraySize);
206
207 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
208 if (!getLangOptions().C99 &&
209 (ASM != ArrayType::Normal ||
210 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
211 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000212 break;
213 }
214 case DeclaratorChunk::Function:
215 // If the function declarator has a prototype (i.e. it is not () and
216 // does not have a K&R-style identifier list), then the arguments are part
217 // of the type, otherwise the argument list is ().
218 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
219 if (!FTI.hasPrototype) {
220 // Simple void foo(), where the incoming T is the result type.
221 T = Context.getFunctionTypeNoProto(T);
222
223 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
224 if (FTI.NumArgs != 0)
225 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
226
227 } else {
228 // Otherwise, we have a function with an argument list that is
229 // potentially variadic.
230 llvm::SmallVector<QualType, 16> ArgTys;
231
232 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
233 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
234 assert(!ArgTy.isNull() && "Couldn't parse type?");
235
236 // Look for 'void'. void is allowed only as a single argument to a
237 // function with no other parameters (C99 6.7.5.3p10). We record
238 // int(void) as a FunctionTypeProto with an empty argument list.
239 if (ArgTy->isVoidType()) {
240 // If this is something like 'float(int, void)', reject it. 'void'
241 // is an incomplete type (C99 6.2.5p19) and function decls cannot
242 // have arguments of incomplete type.
243 if (FTI.NumArgs != 1 || FTI.isVariadic) {
244 Diag(DeclType.Loc, diag::err_void_only_param);
245 ArgTy = Context.IntTy;
246 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
247 } else if (FTI.ArgInfo[i].Ident) {
248 // Reject, but continue to parse 'int(void abc)'.
249 Diag(FTI.ArgInfo[i].IdentLoc,
250 diag::err_param_with_void_type);
251 ArgTy = Context.IntTy;
252 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
253 } else {
254 // Reject, but continue to parse 'float(const void)'.
255 if (ArgTy.getQualifiers())
256 Diag(DeclType.Loc, diag::err_void_param_qualified);
257
258 // Do not add 'void' to the ArgTys list.
259 break;
260 }
261 }
262
263 ArgTys.push_back(ArgTy);
264 }
265 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
266 FTI.isVariadic);
267 }
268 break;
269 }
270 }
271
272 return T;
273}
274
275Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
276 // C99 6.7.6: Type names have no identifier. This is already validated by
277 // the parser.
278 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
279
280 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000281
282 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000283
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000284 // In this context, we *do not* check D.getInvalidType(). If the declarator
285 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
286 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000287 return T.getAsOpaquePtr();
288}
289
Steve Naroff91b03f72007-08-28 03:03:08 +0000290// Called from Parser::ParseParenDeclarator().
Chris Lattner4b009652007-07-25 00:24:17 +0000291Sema::TypeResult Sema::ParseParamDeclaratorType(Scope *S, Declarator &D) {
292 // Note: parameters have identifiers, but we don't care about them here, we
293 // just want the type converted.
294 QualType T = GetTypeForDeclarator(D, S);
295
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000296 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
297
298 // In this context, we *do not* check D.getInvalidType(). If the declarator
299 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
300 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000301 return T.getAsOpaquePtr();
302}