Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | /// ConvertDeclSpecToType - Convert the specified declspec to the appropriate |
| 23 | /// type object. This returns null on error. |
| 24 | static 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 | } |
Chris Lattner | 7a543ad | 2007-07-13 21:02:29 +0000 | [diff] [blame] | 41 | case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 42 | case DeclSpec::TST_int: { |
| 43 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 45 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 46 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | } |
| 51 | } else { |
| 52 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 53 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 59 | // 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | 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 Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 71 | return Ctx.getComplexType(Ctx.FloatTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | |
| 73 | case DeclSpec::TST_double: { |
| 74 | bool isLong = DS.getTypeSpecWidth() == DeclSpec::TSW_long; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 75 | QualType T = isLong ? Ctx.LongDoubleTy : Ctx.DoubleTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified) |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 77 | return T; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex && |
| 79 | "FIXME: imaginary types not supported yet!"); |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 80 | return Ctx.getComplexType(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
| 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!"); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 105 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so |
| 106 | // we have this "hack" for now... |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 107 | if (ObjcInterfaceDecl *ObjcIntDecl = dyn_cast<ObjcInterfaceDecl>(D)) { |
| 108 | if (DS.getProtocolQualifiers() == 0) |
| 109 | return Ctx.getObjcInterfaceType(ObjcIntDecl); |
| 110 | |
| 111 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
| 112 | return Ctx.getObjcQualifiedInterfaceType(ObjcIntDecl, |
| 113 | reinterpret_cast<ObjcProtocolDecl**>(PPDecl), |
| 114 | DS.NumProtocolQualifiers()); |
| 115 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | // TypeQuals handled by caller. |
| 117 | return Ctx.getTypedefType(cast<TypedefDecl>(D)); |
| 118 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 119 | case DeclSpec::TST_typeofType: { |
| 120 | QualType T = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 121 | assert(!T.isNull() && "Didn't get a type for typeof?"); |
| 122 | // TypeQuals handled by caller. |
| 123 | return Ctx.getTypeOfType(T); |
| 124 | } |
| 125 | case DeclSpec::TST_typeofExpr: { |
| 126 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 127 | assert(E && "Didn't get an expression for typeof?"); |
| 128 | // TypeQuals handled by caller. |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 129 | return Ctx.getTypeOfExpr(E); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
| 135 | /// instances. |
| 136 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 137 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 138 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 139 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 140 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
| 141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | // Apply const/volatile/restrict qualifiers to T. |
| 145 | T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers()); |
| 146 | |
| 147 | // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos |
| 148 | // are ordered from the identifier out, which is opposite of what we want :). |
| 149 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 150 | const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
| 151 | switch (DeclType.Kind) { |
| 152 | default: assert(0 && "Unknown decltype!"); |
| 153 | case DeclaratorChunk::Pointer: |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 154 | if (T->isReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 156 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference, |
| 157 | D.getIdentifier()->getName()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 158 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 159 | T = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Apply the pointer typequals to the pointer object. |
| 163 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
| 164 | break; |
| 165 | case DeclaratorChunk::Reference: |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 166 | if (const ReferenceType *RT = T->getAsReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | // C++ 8.3.2p4: There shall be no references to references ... |
| 168 | Diag(D.getIdentifierLoc(), |
| 169 | diag::err_illegal_decl_reference_to_reference, |
| 170 | D.getIdentifier()->getName()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 171 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 172 | T = RT->getReferenceeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | T = Context.getReferenceType(T); |
| 176 | break; |
| 177 | case DeclaratorChunk::Array: { |
| 178 | const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 179 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | ArrayType::ArraySizeModifier ASM; |
| 181 | if (ATI.isStar) |
| 182 | ASM = ArrayType::Star; |
| 183 | else if (ATI.hasStatic) |
| 184 | ASM = ArrayType::Static; |
| 185 | else |
| 186 | ASM = ArrayType::Normal; |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 189 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 190 | if (T->isIncompleteType()) { |
| 191 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, |
| 192 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 193 | T = Context.IntTy; |
| 194 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 195 | } else if (T->isFunctionType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, |
| 197 | D.getIdentifier()->getName()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 198 | T = Context.getPointerType(T); |
| 199 | D.setInvalidType(true); |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 200 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 202 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, |
| 203 | D.getIdentifier()->getName()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 204 | T = RT->getReferenceeType(); |
| 205 | D.setInvalidType(true); |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 206 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | // If the element type is a struct or union that contains a variadic |
| 208 | // array, reject it: C99 6.7.2.1p2. |
| 209 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
| 210 | Diag(DeclType.Loc, diag::err_flexible_array_in_array, |
| 211 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 212 | T = Context.IntTy; |
| 213 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 216 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 217 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
| 218 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, |
| 219 | ArraySize->getType().getAsString(), ArraySize->getSourceRange()); |
| 220 | D.setInvalidType(true); |
| 221 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 222 | llvm::APSInt ConstVal(32); |
| 223 | // If no expression was provided, we consider it a VLA. |
| 224 | if (!ArraySize || !ArraySize->isIntegerConstantExpr(ConstVal, Context)) |
| 225 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 226 | else { |
| 227 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 228 | // have a value greater than zero. |
| 229 | if (ConstVal.isSigned()) { |
| 230 | if (ConstVal.isNegative()) { |
| 231 | Diag(ArraySize->getLocStart(), |
| 232 | diag::err_typecheck_negative_array_size, |
| 233 | ArraySize->getSourceRange()); |
| 234 | D.setInvalidType(true); |
| 235 | } else if (ConstVal == 0) { |
| 236 | // GCC accepts zero sized static arrays. |
| 237 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, |
| 238 | ArraySize->getSourceRange()); |
| 239 | } |
| 240 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 241 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 243 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 244 | if (!getLangOptions().C99 && |
| 245 | (ASM != ArrayType::Normal || |
| 246 | (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) |
| 247 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | case DeclaratorChunk::Function: |
| 251 | // If the function declarator has a prototype (i.e. it is not () and |
| 252 | // does not have a K&R-style identifier list), then the arguments are part |
| 253 | // of the type, otherwise the argument list is (). |
| 254 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 255 | if (!FTI.hasPrototype) { |
| 256 | // Simple void foo(), where the incoming T is the result type. |
| 257 | T = Context.getFunctionTypeNoProto(T); |
| 258 | |
| 259 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
| 260 | if (FTI.NumArgs != 0) |
| 261 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 262 | |
| 263 | } else { |
| 264 | // Otherwise, we have a function with an argument list that is |
| 265 | // potentially variadic. |
| 266 | llvm::SmallVector<QualType, 16> ArgTys; |
| 267 | |
| 268 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 269 | QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 270 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 271 | // |
| 272 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 273 | // This matches the conversion that is done in |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame^] | 274 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 275 | // argument type in the function prototype *will not* match the |
| 276 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 277 | // |
| 278 | // FIXME: We still apparently need the conversion in |
| 279 | // Sema::ParseParamDeclarator(). This doesn't make any sense, since |
| 280 | // it should be driving off the type being created here. |
| 281 | // |
| 282 | // FIXME: If a source translation tool needs to see the original type, |
| 283 | // then we need to consider storing both types somewhere... |
| 284 | // |
| 285 | if (const ArrayType *AT = ArgTy->getAsArrayType()) |
| 286 | ArgTy = Context.getPointerType(AT->getElementType()); |
| 287 | else if (ArgTy->isFunctionType()) |
| 288 | ArgTy = Context.getPointerType(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | // Look for 'void'. void is allowed only as a single argument to a |
| 290 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 291 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 292 | else if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | // If this is something like 'float(int, void)', reject it. 'void' |
| 294 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 295 | // have arguments of incomplete type. |
| 296 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 297 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 298 | ArgTy = Context.IntTy; |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 299 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 300 | } else if (FTI.ArgInfo[i].Ident) { |
| 301 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 303 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 304 | ArgTy = Context.IntTy; |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 305 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 306 | } else { |
| 307 | // Reject, but continue to parse 'float(const void)'. |
| 308 | if (ArgTy.getQualifiers()) |
| 309 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 310 | |
| 311 | // Do not add 'void' to the ArgTys list. |
| 312 | break; |
| 313 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | ArgTys.push_back(ArgTy); |
| 317 | } |
| 318 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
| 319 | FTI.isVariadic); |
| 320 | } |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return T; |
| 326 | } |
| 327 | |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 328 | /// ObjcGetTypeForMethodDefinition - Builds the type for a method definition |
| 329 | /// declarator |
| 330 | QualType Sema::ObjcGetTypeForMethodDefinition(DeclTy *D, Scope *S) { |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 331 | ObjcMethodDecl *MDecl = dyn_cast<ObjcMethodDecl>(static_cast<Decl *>(D)); |
| 332 | QualType T = MDecl->getResultType(); |
| 333 | llvm::SmallVector<QualType, 16> ArgTys; |
| 334 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 335 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 336 | if (MDecl->isInstance()) { |
| 337 | QualType selfTy = Context.getObjcInterfaceType(MDecl->getClassInterface()); |
| 338 | selfTy = Context.getPointerType(selfTy); |
| 339 | ArgTys.push_back(selfTy); |
| 340 | } |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 341 | else |
| 342 | ArgTys.push_back(Context.getObjcIdType()); |
| 343 | ArgTys.push_back(Context.getObjcSelType()); |
| 344 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 345 | for (int i = 0; i < MDecl->getNumParams(); i++) { |
| 346 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 347 | QualType ArgTy = PDecl->getType(); |
| 348 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 349 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 350 | // This matches the conversion that is done in |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 351 | // Sema::ParseParamDeclarator(). |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 352 | if (const ArrayType *AT = ArgTy->getAsArrayType()) |
| 353 | ArgTy = Context.getPointerType(AT->getElementType()); |
| 354 | else if (ArgTy->isFunctionType()) |
| 355 | ArgTy = Context.getPointerType(ArgTy); |
| 356 | ArgTys.push_back(ArgTy); |
| 357 | } |
| 358 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
| 359 | false); |
| 360 | return T; |
| 361 | } |
| 362 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 363 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 365 | // the parser. |
| 366 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 367 | |
| 368 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 369 | |
| 370 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 372 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 373 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 374 | // though it will not reflect the user specified type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | return T.getAsOpaquePtr(); |
| 376 | } |
| 377 | |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 378 | // Called from Parser::ParseParenDeclarator(). |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 379 | Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | // Note: parameters have identifiers, but we don't care about them here, we |
| 381 | // just want the type converted. |
| 382 | QualType T = GetTypeForDeclarator(D, S); |
| 383 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 384 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 385 | |
| 386 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 387 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 388 | // though it will not reflect the user specified type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | return T.getAsOpaquePtr(); |
| 390 | } |