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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements type-related semantic analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.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. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 24 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 26 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 27 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | |
| 29 | switch (DS.getTypeSpecType()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 30 | default: assert(0 && "Unknown TypeSpecType!"); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 31 | case DeclSpec::TST_void: |
| 32 | Result = Context.VoidTy; |
| 33 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | case DeclSpec::TST_char: |
| 35 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 36 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 38 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | else { |
| 40 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 41 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 42 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 44 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 45 | case DeclSpec::TST_wchar: |
| 46 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 47 | Result = Context.WCharTy; |
| 48 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
| 49 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec, |
| 50 | DS.getSpecifierName(DS.getTypeSpecType())); |
| 51 | Result = Context.getSignedWCharType(); |
| 52 | } else { |
| 53 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 54 | "Unknown TSS value"); |
| 55 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec, |
| 56 | DS.getSpecifierName(DS.getTypeSpecType())); |
| 57 | Result = Context.getUnsignedWCharType(); |
| 58 | } |
| 59 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 60 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 61 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 62 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 63 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 64 | DS.getNumProtocolQualifiers()); |
| 65 | break; |
| 66 | } |
| 67 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 68 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 69 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 70 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 71 | // Note that the one exception to this is function definitions, which are |
| 72 | // allowed to be completely missing a declspec. This is handled in the |
| 73 | // parser already though by it pretending to have seen an 'int' in this |
| 74 | // case. |
| 75 | if (getLangOptions().ImplicitInt) { |
| 76 | if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier | |
| 77 | DeclSpec::PQ_TypeSpecifier | |
| 78 | DeclSpec::PQ_TypeQualifier)) == 0) |
| 79 | Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec); |
| 80 | } else { |
| 81 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 82 | // "At least one type specifier shall be given in the declaration |
| 83 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 84 | // each struct declaration and type name." |
| 85 | if (!DS.hasTypeSpecifier()) |
| 86 | Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier); |
| 87 | } |
| 88 | |
| 89 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 90 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 92 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 93 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 94 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 95 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 96 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | } |
| 98 | } else { |
| 99 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 100 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 101 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 102 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 103 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 106 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 108 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 109 | case DeclSpec::TST_double: |
| 110 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 111 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 112 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 113 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 114 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 115 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 117 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 118 | case DeclSpec::TST_decimal128: // _Decimal128 |
| 119 | assert(0 && "FIXME: GNU decimal extensions not supported yet!"); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 120 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | case DeclSpec::TST_enum: |
| 122 | case DeclSpec::TST_union: |
| 123 | case DeclSpec::TST_struct: { |
| 124 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 125 | assert(D && "Didn't get a decl for a class/enum/union/struct?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 127 | DS.getTypeSpecSign() == 0 && |
| 128 | "Can't handle qualifiers on typedef names yet!"); |
| 129 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 130 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 131 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
| 133 | case DeclSpec::TST_typedef: { |
| 134 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
| 135 | assert(D && "Didn't get a decl for a typedef?"); |
| 136 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 137 | DS.getTypeSpecSign() == 0 && |
| 138 | "Can't handle qualifiers on typedef names yet!"); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 139 | DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers(); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 140 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 141 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so |
| 142 | // we have this "hack" for now... |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 143 | if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 144 | if (PQ == 0) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 145 | Result = Context.getObjCInterfaceType(ObjCIntDecl); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 146 | break; |
| 147 | } |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 148 | |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 149 | Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 150 | (ObjCProtocolDecl**)PQ, |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 151 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 152 | break; |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 153 | } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 154 | if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) { |
| 155 | // id<protocol-list> |
| 156 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
| 157 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 158 | break; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 162 | Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D)); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 163 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 165 | case DeclSpec::TST_typeofType: |
| 166 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 167 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 168 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 169 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 170 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 171 | case DeclSpec::TST_typeofExpr: { |
| 172 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 173 | assert(E && "Didn't get an expression for typeof?"); |
| 174 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 175 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 176 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 177 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 179 | |
| 180 | // Handle complex types. |
| 181 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 182 | Result = Context.getComplexType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 183 | |
| 184 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 185 | "FIXME: imaginary types not supported yet!"); |
| 186 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 187 | // See if there are any attributes on the declspec that apply to the type (as |
| 188 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 189 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 190 | ProcessTypeAttributeList(Result, AL); |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 192 | // Apply const/volatile/restrict qualifiers to T. |
| 193 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 194 | |
| 195 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 196 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 197 | // restrict-qualified references. |
| 198 | if (TypeQuals & QualType::Restrict) { |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 199 | if (const PointerLikeType *PT = Result->getAsPointerLikeType()) { |
| 200 | QualType EltTy = PT->getPointeeType(); |
| 201 | |
| 202 | // If we have a pointer or reference, the pointee must have an object or |
| 203 | // incomplete type. |
| 204 | if (!EltTy->isIncompleteOrObjectType()) { |
| 205 | Diag(DS.getRestrictSpecLoc(), |
| 206 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 207 | EltTy.getAsString(), DS.getSourceRange()); |
| 208 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
| 209 | } |
| 210 | } else { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 211 | Diag(DS.getRestrictSpecLoc(), |
| 212 | diag::err_typecheck_invalid_restrict_not_pointer, |
| 213 | Result.getAsString(), DS.getSourceRange()); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 214 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 219 | // of a function type includes any type qualifiers, the behavior is |
| 220 | // undefined." |
| 221 | if (Result->isFunctionType() && TypeQuals) { |
| 222 | // Get some location to point at, either the C or V location. |
| 223 | SourceLocation Loc; |
| 224 | if (TypeQuals & QualType::Const) |
| 225 | Loc = DS.getConstSpecLoc(); |
| 226 | else { |
| 227 | assert((TypeQuals & QualType::Volatile) && |
| 228 | "Has CV quals but not C or V?"); |
| 229 | Loc = DS.getVolatileSpecLoc(); |
| 230 | } |
| 231 | Diag(Loc, diag::warn_typecheck_function_qualifiers, |
| 232 | Result.getAsString(), DS.getSourceRange()); |
| 233 | } |
| 234 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 235 | // C++ [dcl.ref]p1: |
| 236 | // Cv-qualified references are ill-formed except when the |
| 237 | // cv-qualifiers are introduced through the use of a typedef |
| 238 | // (7.1.3) or of a template type argument (14.3), in which |
| 239 | // case the cv-qualifiers are ignored. |
| 240 | if (DS.getTypeSpecType() == DeclSpec::TST_typedef && |
| 241 | TypeQuals && Result->isReferenceType()) { |
| 242 | TypeQuals &= ~QualType::Const; |
| 243 | TypeQuals &= ~QualType::Volatile; |
| 244 | } |
| 245 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 246 | Result = Result.getQualifiedType(TypeQuals); |
| 247 | } |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 248 | return Result; |
| 249 | } |
| 250 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
| 252 | /// instances. |
| 253 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 254 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 255 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 256 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 257 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
| 258 | |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 259 | QualType T = ConvertDeclSpecToType(D.getDeclSpec()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos |
| 262 | // are ordered from the identifier out, which is opposite of what we want :). |
| 263 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 264 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | switch (DeclType.Kind) { |
| 266 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 267 | case DeclaratorChunk::BlockPointer: |
| 268 | if (DeclType.Cls.TypeQuals) |
| 269 | Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type); |
| 270 | if (!T.getTypePtr()->isFunctionType()) |
| 271 | Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type); |
| 272 | else |
| 273 | T = Context.getBlockPointerType(T); |
| 274 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | case DeclaratorChunk::Pointer: |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 276 | if (T->isReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 278 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 279 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 280 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 281 | T = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 284 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 285 | // object or incomplete types shall not be restrict-qualified." |
| 286 | if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 287 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 288 | Diag(DeclType.Loc, |
| 289 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 290 | T.getAsString()); |
| 291 | DeclType.Ptr.TypeQuals &= QualType::Restrict; |
| 292 | } |
| 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | // Apply the pointer typequals to the pointer object. |
| 295 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
| 296 | break; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 297 | case DeclaratorChunk::Reference: { |
| 298 | // Whether we should suppress the creation of the reference. |
| 299 | bool SuppressReference = false; |
| 300 | if (T->isReferenceType()) { |
| 301 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 302 | // |
| 303 | // According to C++ DR 106, references to references are only |
| 304 | // diagnosed when they are written directly (e.g., "int & &"), |
| 305 | // but not when they happen via a typedef: |
| 306 | // |
| 307 | // typedef int& intref; |
| 308 | // typedef intref& intref2; |
| 309 | // |
| 310 | // Parser::ParserDeclaratorInternal diagnoses the case where |
| 311 | // references are written directly; here, we handle the |
| 312 | // collapsing of references-to-references as described in C++ |
| 313 | // DR 106 and amended by C++ DR 540. |
| 314 | SuppressReference = true; |
| 315 | } |
| 316 | |
| 317 | // C++ [dcl.ref]p1: |
| 318 | // A declarator that specifies the type “reference to cv void” |
| 319 | // is ill-formed. |
| 320 | if (T->isVoidType()) { |
| 321 | Diag(DeclType.Loc, diag::err_reference_to_void); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 322 | D.setInvalidType(true); |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 323 | T = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 326 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 327 | // object or incomplete types shall not be restrict-qualified." |
| 328 | if (DeclType.Ref.HasRestrict && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 329 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 330 | Diag(DeclType.Loc, |
| 331 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 332 | T.getAsString()); |
| 333 | DeclType.Ref.HasRestrict = false; |
| 334 | } |
| 335 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 336 | if (!SuppressReference) |
| 337 | T = Context.getReferenceType(T); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 338 | |
| 339 | // Handle restrict on references. |
| 340 | if (DeclType.Ref.HasRestrict) |
| 341 | T.addRestrict(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | break; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 343 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | case DeclaratorChunk::Array: { |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 345 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 346 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | ArrayType::ArraySizeModifier ASM; |
| 348 | if (ATI.isStar) |
| 349 | ASM = ArrayType::Star; |
| 350 | else if (ATI.hasStatic) |
| 351 | ASM = ArrayType::Static; |
| 352 | else |
| 353 | ASM = ArrayType::Normal; |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 354 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 356 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 357 | if (T->isIncompleteType()) { |
| 358 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, |
| 359 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 360 | T = Context.IntTy; |
| 361 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 362 | } else if (T->isFunctionType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 364 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 365 | T = Context.getPointerType(T); |
| 366 | D.setInvalidType(true); |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 367 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 369 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 370 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 371 | T = RT->getPointeeType(); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 372 | D.setInvalidType(true); |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 373 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | // If the element type is a struct or union that contains a variadic |
| 375 | // array, reject it: C99 6.7.2.1p2. |
| 376 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
| 377 | Diag(DeclType.Loc, diag::err_flexible_array_in_array, |
| 378 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 379 | T = Context.IntTy; |
| 380 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | 43477ca | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 382 | } else if (T->isObjCInterfaceType()) { |
| 383 | Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces, |
| 384 | T.getAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | } |
Chris Lattner | 43477ca | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 386 | |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 387 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 388 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
| 389 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, |
| 390 | ArraySize->getType().getAsString(), ArraySize->getSourceRange()); |
| 391 | D.setInvalidType(true); |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 392 | delete ArraySize; |
| 393 | ATI.NumElts = ArraySize = 0; |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 394 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 395 | llvm::APSInt ConstVal(32); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 396 | if (!ArraySize) { |
| 397 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
Eli Friedman | 37148aa | 2008-05-14 00:40:18 +0000 | [diff] [blame] | 398 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
| 399 | !T->isConstantSizeType()) { |
| 400 | // Per C99, a variable array is an array with either a non-constant |
| 401 | // size or an element type that has a non-constant-size |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 402 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 403 | } else { |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 404 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 405 | // have a value greater than zero. |
| 406 | if (ConstVal.isSigned()) { |
| 407 | if (ConstVal.isNegative()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 408 | Diag(ArraySize->getLocStart(), |
| 409 | diag::err_typecheck_negative_array_size) |
| 410 | << ArraySize->getSourceRange(); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 411 | D.setInvalidType(true); |
| 412 | } else if (ConstVal == 0) { |
| 413 | // GCC accepts zero sized static arrays. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 414 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 415 | << ArraySize->getSourceRange(); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 418 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 420 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 421 | if (!getLangOptions().C99 && |
| 422 | (ASM != ArrayType::Normal || |
| 423 | (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) |
| 424 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | case DeclaratorChunk::Function: |
| 428 | // If the function declarator has a prototype (i.e. it is not () and |
| 429 | // does not have a K&R-style identifier list), then the arguments are part |
| 430 | // of the type, otherwise the argument list is (). |
| 431 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 432 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 433 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 434 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 435 | Diag(DeclType.Loc, diag::err_func_returning_array_function, |
| 436 | T.getAsString()); |
| 437 | T = Context.IntTy; |
| 438 | D.setInvalidType(true); |
| 439 | } |
| 440 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 441 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 442 | if (getLangOptions().CPlusPlus) { |
| 443 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 444 | // function takes no arguments. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 445 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 446 | } else { |
| 447 | // Simple void foo(), where the incoming T is the result type. |
| 448 | T = Context.getFunctionTypeNoProto(T); |
| 449 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 450 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 451 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 452 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | } else { |
| 454 | // Otherwise, we have a function with an argument list that is |
| 455 | // potentially variadic. |
| 456 | llvm::SmallVector<QualType, 16> ArgTys; |
| 457 | |
| 458 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 459 | ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; |
| 460 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 461 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 462 | // |
| 463 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 464 | // This matches the conversion that is done in |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 465 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 466 | // argument type in the function prototype *will not* match the |
| 467 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 468 | // |
| 469 | // FIXME: We still apparently need the conversion in |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 470 | // Sema::ActOnParamDeclarator(). This doesn't make any sense, since |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 471 | // it should be driving off the type being created here. |
| 472 | // |
| 473 | // FIXME: If a source translation tool needs to see the original type, |
| 474 | // then we need to consider storing both types somewhere... |
| 475 | // |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 476 | if (ArgTy->isArrayType()) { |
| 477 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 478 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 479 | ArgTy = Context.getPointerType(ArgTy); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 480 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | // Look for 'void'. void is allowed only as a single argument to a |
| 482 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 483 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 484 | else if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | // If this is something like 'float(int, void)', reject it. 'void' |
| 486 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 487 | // have arguments of incomplete type. |
| 488 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 489 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 490 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 491 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 492 | } else if (FTI.ArgInfo[i].Ident) { |
| 493 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 495 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 496 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 497 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 498 | } else { |
| 499 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 500 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 501 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 502 | |
| 503 | // Do not add 'void' to the ArgTys list. |
| 504 | break; |
| 505 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 506 | } else if (!FTI.hasPrototype) { |
| 507 | if (ArgTy->isPromotableIntegerType()) { |
| 508 | ArgTy = Context.IntTy; |
| 509 | } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { |
| 510 | if (BTy->getKind() == BuiltinType::Float) |
| 511 | ArgTy = Context.DoubleTy; |
| 512 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | ArgTys.push_back(ArgTy); |
| 516 | } |
| 517 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 518 | FTI.isVariadic, FTI.TypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | } |
| 520 | break; |
| 521 | } |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 522 | |
| 523 | // See if there are any attributes on this declarator chunk. |
| 524 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 525 | ProcessTypeAttributeList(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 527 | |
| 528 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
| 529 | const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto(); |
| 530 | assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?"); |
| 531 | |
| 532 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 533 | // for a nonstatic member function, the function type to which a pointer |
| 534 | // to member refers, or the top-level function type of a function typedef |
| 535 | // declaration. |
| 536 | if (FnTy->getTypeQuals() != 0 && |
| 537 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 538 | (D.getContext() != Declarator::MemberContext || |
| 539 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
| 540 | |
| 541 | if (D.isFunctionDeclarator()) |
| 542 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 543 | else |
| 544 | Diag(D.getIdentifierLoc(), |
| 545 | diag::err_invalid_qualified_typedef_function_type_use); |
| 546 | |
| 547 | // Strip the cv-quals from the type. |
| 548 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 549 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 553 | // If there were any type attributes applied to the decl itself (not the |
| 554 | // type, apply the type attribute to the type!) |
| 555 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 556 | ProcessTypeAttributeList(T, Attrs); |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | return T; |
| 559 | } |
| 560 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 561 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 562 | /// declarator |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 563 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
| 564 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 565 | QualType T = MDecl->getResultType(); |
| 566 | llvm::SmallVector<QualType, 16> ArgTys; |
| 567 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 568 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 569 | if (MDecl->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 570 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 571 | selfTy = Context.getPointerType(selfTy); |
| 572 | ArgTys.push_back(selfTy); |
| 573 | } |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 574 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 575 | ArgTys.push_back(Context.getObjCIdType()); |
| 576 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 578 | for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 579 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 580 | QualType ArgTy = PDecl->getType(); |
| 581 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 582 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 583 | // This matches the conversion that is done in |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 584 | // Sema::ActOnParamDeclarator(). |
| 585 | if (ArgTy->isArrayType()) |
| 586 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 587 | else if (ArgTy->isFunctionType()) |
| 588 | ArgTy = Context.getPointerType(ArgTy); |
| 589 | ArgTys.push_back(ArgTy); |
| 590 | } |
| 591 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 592 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 593 | return T; |
| 594 | } |
| 595 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 596 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types (FIXME: |
| 597 | /// or pointer-to-member types) that may be similar (C++ 4.4), |
| 598 | /// replaces T1 and T2 with the type that they point to and return |
| 599 | /// true. If T1 and T2 aren't pointer types or pointer-to-member |
| 600 | /// types, or if they are not similar at this level, returns false and |
| 601 | /// leaves T1 and T2 unchanged. Top-level qualifiers on T1 and T2 are |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 602 | /// ignored. This function will typically be called in a loop that |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 603 | /// successively "unwraps" pointer and pointer-to-member types to |
| 604 | /// compare them at each level. |
| 605 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) |
| 606 | { |
| 607 | const PointerType *T1PtrType = T1->getAsPointerType(), |
| 608 | *T2PtrType = T2->getAsPointerType(); |
| 609 | if (T1PtrType && T2PtrType) { |
| 610 | T1 = T1PtrType->getPointeeType(); |
| 611 | T2 = T2PtrType->getPointeeType(); |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | // FIXME: pointer-to-member types |
| 616 | return false; |
| 617 | } |
| 618 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 619 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 620 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 621 | // the parser. |
| 622 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 623 | |
| 624 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 625 | |
| 626 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 628 | // Check that there are no default arguments (C++ only). |
| 629 | if (getLangOptions().CPlusPlus) |
| 630 | CheckExtraCXXDefaultArguments(D); |
| 631 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 632 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 633 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 634 | // though it will not reflect the user specified type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 635 | return T.getAsOpaquePtr(); |
| 636 | } |
| 637 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 638 | |
| 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // Type Attribute Processing |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
| 644 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 645 | /// specified type. The attribute contains 1 argument, the id of the address |
| 646 | /// space for the type. |
| 647 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
| 648 | const AttributeList &Attr, Sema &S){ |
| 649 | // If this type is already address space qualified, reject it. |
| 650 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 651 | // for two or more different address spaces." |
| 652 | if (Type.getAddressSpace()) { |
| 653 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // Check the attribute arguments. |
| 658 | if (Attr.getNumArgs() != 1) { |
| 659 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 660 | std::string("1")); |
| 661 | return; |
| 662 | } |
| 663 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 664 | llvm::APSInt addrSpace(32); |
| 665 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 666 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 667 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | |
| 671 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
| 672 | Type = S.Context.getASQualType(Type, ASIdx); |
| 673 | } |
| 674 | |
| 675 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 676 | // Scan through and apply attributes to this type where it makes sense. Some |
| 677 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 678 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 679 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 680 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 681 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 682 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 683 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 684 | default: break; |
| 685 | case AttributeList::AT_address_space: |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 686 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 687 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 692 | |