blob: a1dd1f7cda5018b2305cde820ce0b07d3ee0ae22 [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"
19using namespace clang;
20
21/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
22/// type object. This returns null on error.
23static QualType ConvertDeclSpecToType(const DeclSpec &DS, ASTContext &Ctx) {
24 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
25 // checking.
26
27 switch (DS.getTypeSpecType()) {
28 default: return QualType(); // FIXME: Handle unimp cases!
29 case DeclSpec::TST_void: return Ctx.VoidTy;
30 case DeclSpec::TST_char:
31 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
32 return Ctx.CharTy;
33 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
34 return Ctx.SignedCharTy;
35 else {
36 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
37 "Unknown TSS value");
38 return Ctx.UnsignedCharTy;
39 }
40 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
41 case DeclSpec::TST_int:
42 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
43 switch (DS.getTypeSpecWidth()) {
44 case DeclSpec::TSW_unspecified: return Ctx.IntTy;
45 case DeclSpec::TSW_short: return Ctx.ShortTy;
46 case DeclSpec::TSW_long: return Ctx.LongTy;
47 case DeclSpec::TSW_longlong: return Ctx.LongLongTy;
48 }
49 } else {
50 switch (DS.getTypeSpecWidth()) {
51 case DeclSpec::TSW_unspecified: return Ctx.UnsignedIntTy;
52 case DeclSpec::TSW_short: return Ctx.UnsignedShortTy;
53 case DeclSpec::TSW_long: return Ctx.UnsignedLongTy;
54 case DeclSpec::TSW_longlong: return Ctx.UnsignedLongLongTy;
55 }
56 }
57 case DeclSpec::TST_float:
58 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
59 return Ctx.FloatTy;
60 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
61 "FIXME: imaginary types not supported yet!");
62 return Ctx.FloatComplexTy;
63
64 case DeclSpec::TST_double: {
65 bool isLong = DS.getTypeSpecWidth() == DeclSpec::TSW_long;
66 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
67 return isLong ? Ctx.LongDoubleTy : Ctx.DoubleTy;
68 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
69 "FIXME: imaginary types not supported yet!");
70 return isLong ? Ctx.LongDoubleComplexTy : Ctx.DoubleComplexTy;
71 }
72 case DeclSpec::TST_bool: // _Bool or bool
73 return Ctx.BoolTy;
74 case DeclSpec::TST_decimal32: // _Decimal32
75 case DeclSpec::TST_decimal64: // _Decimal64
76 case DeclSpec::TST_decimal128: // _Decimal128
77 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
78 case DeclSpec::TST_enum:
79 case DeclSpec::TST_union:
80 case DeclSpec::TST_struct: {
81 Decl *D = static_cast<Decl *>(DS.getTypeRep());
82 assert(D && "Didn't get a decl for a enum/union/struct?");
83 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
84 DS.getTypeSpecSign() == 0 &&
85 "Can't handle qualifiers on typedef names yet!");
86 // TypeQuals handled by caller.
87 return Ctx.getTagDeclType(cast<TagDecl>(D));
88 }
89 case DeclSpec::TST_typedef: {
90 Decl *D = static_cast<Decl *>(DS.getTypeRep());
91 assert(D && "Didn't get a decl for a typedef?");
92 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
93 DS.getTypeSpecSign() == 0 &&
94 "Can't handle qualifiers on typedef names yet!");
95 // TypeQuals handled by caller.
96 return Ctx.getTypedefType(cast<TypedefDecl>(D));
97 }
Steve Naroff7cbb1462007-07-31 12:34:36 +000098 case DeclSpec::TST_typeofType: {
99 QualType T = QualType::getFromOpaquePtr(DS.getTypeRep());
100 assert(!T.isNull() && "Didn't get a type for typeof?");
101 // TypeQuals handled by caller.
102 return Ctx.getTypeOfType(T);
103 }
104 case DeclSpec::TST_typeofExpr: {
105 Expr *E = static_cast<Expr *>(DS.getTypeRep());
106 assert(E && "Didn't get an expression for typeof?");
107 // TypeQuals handled by caller.
108 return Ctx.getTypeOfType(E);
109 }
Chris Lattner4b009652007-07-25 00:24:17 +0000110 }
111}
112
113/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
114/// instances.
115QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
116 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
117
118 // Apply const/volatile/restrict qualifiers to T.
119 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
120
121 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
122 // are ordered from the identifier out, which is opposite of what we want :).
123 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
124 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
125 switch (DeclType.Kind) {
126 default: assert(0 && "Unknown decltype!");
127 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000128 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000129 // C++ 8.3.2p4: There shall be no ... pointers to references ...
130 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
131 D.getIdentifier()->getName());
132 T = Context.IntTy;
133 }
134
135 // Apply the pointer typequals to the pointer object.
136 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
137 break;
138 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000139 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 // C++ 8.3.2p4: There shall be no references to references ...
141 Diag(D.getIdentifierLoc(),
142 diag::err_illegal_decl_reference_to_reference,
143 D.getIdentifier()->getName());
144 T = RT->getReferenceeType();
145 }
146
147 T = Context.getReferenceType(T);
148 break;
149 case DeclaratorChunk::Array: {
150 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
151 ArrayType::ArraySizeModifier ASM;
152 if (ATI.isStar)
153 ASM = ArrayType::Star;
154 else if (ATI.hasStatic)
155 ASM = ArrayType::Static;
156 else
157 ASM = ArrayType::Normal;
158
159 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
160 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
161 if (T->isIncompleteType()) {
162 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
163 T.getAsString());
164 T = Context.IntTy;
165 } else if (T->isFunctionType()) {
166 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
167 D.getIdentifier()->getName());
168 T = Context.getPointerType(T);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000169 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000170 // C++ 8.3.2p4: There shall be no ... arrays of references ...
171 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
172 D.getIdentifier()->getName());
173 T = RT->getReferenceeType();
Chris Lattner36be3d82007-07-31 21:33:24 +0000174 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 // If the element type is a struct or union that contains a variadic
176 // array, reject it: C99 6.7.2.1p2.
177 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
178 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
179 T.getAsString());
180 T = Context.IntTy;
181 }
182 }
183 T = Context.getArrayType(T, ASM, ATI.TypeQuals,
184 static_cast<Expr *>(ATI.NumElts));
185 break;
186 }
187 case DeclaratorChunk::Function:
188 // If the function declarator has a prototype (i.e. it is not () and
189 // does not have a K&R-style identifier list), then the arguments are part
190 // of the type, otherwise the argument list is ().
191 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
192 if (!FTI.hasPrototype) {
193 // Simple void foo(), where the incoming T is the result type.
194 T = Context.getFunctionTypeNoProto(T);
195
196 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
197 if (FTI.NumArgs != 0)
198 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
199
200 } else {
201 // Otherwise, we have a function with an argument list that is
202 // potentially variadic.
203 llvm::SmallVector<QualType, 16> ArgTys;
204
205 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
206 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
207 assert(!ArgTy.isNull() && "Couldn't parse type?");
208
209 // Look for 'void'. void is allowed only as a single argument to a
210 // function with no other parameters (C99 6.7.5.3p10). We record
211 // int(void) as a FunctionTypeProto with an empty argument list.
212 if (ArgTy->isVoidType()) {
213 // If this is something like 'float(int, void)', reject it. 'void'
214 // is an incomplete type (C99 6.2.5p19) and function decls cannot
215 // have arguments of incomplete type.
216 if (FTI.NumArgs != 1 || FTI.isVariadic) {
217 Diag(DeclType.Loc, diag::err_void_only_param);
218 ArgTy = Context.IntTy;
219 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
220 } else if (FTI.ArgInfo[i].Ident) {
221 // Reject, but continue to parse 'int(void abc)'.
222 Diag(FTI.ArgInfo[i].IdentLoc,
223 diag::err_param_with_void_type);
224 ArgTy = Context.IntTy;
225 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
226 } else {
227 // Reject, but continue to parse 'float(const void)'.
228 if (ArgTy.getQualifiers())
229 Diag(DeclType.Loc, diag::err_void_param_qualified);
230
231 // Do not add 'void' to the ArgTys list.
232 break;
233 }
234 }
235
236 ArgTys.push_back(ArgTy);
237 }
238 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
239 FTI.isVariadic);
240 }
241 break;
242 }
243 }
244
245 return T;
246}
247
248Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
249 // C99 6.7.6: Type names have no identifier. This is already validated by
250 // the parser.
251 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
252
253 QualType T = GetTypeForDeclarator(D, S);
254
255 // If the type of the declarator was invalid, this is an invalid typename.
256 if (T.isNull())
257 return true;
258
259 return T.getAsOpaquePtr();
260}
261
262Sema::TypeResult Sema::ParseParamDeclaratorType(Scope *S, Declarator &D) {
263 // Note: parameters have identifiers, but we don't care about them here, we
264 // just want the type converted.
265 QualType T = GetTypeForDeclarator(D, S);
266
267 // If the type of the declarator was invalid, this is an invalid typename.
268 if (T.isNull())
269 return true;
270
271 return T.getAsOpaquePtr();
272}