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