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