Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | cc7b160 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | cc7b160 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 24 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 26 | // checking. |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 27 | QualType Result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | |
| 29 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 30 | default: assert(0 && "Unknown TypeSpecType!"); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 31 | case DeclSpec::TST_void: |
| 32 | Result = Context.VoidTy; |
| 33 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | case DeclSpec::TST_char: |
| 35 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 36 | Result = Context.CharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 38 | Result = Context.SignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 39 | else { |
| 40 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 41 | "Unknown TSS value"); |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 42 | Result = Context.UnsignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 44 | break; |
Argiris Kirtzidis | 1ed03e7 | 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) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 49 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 50 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 51 | Result = Context.getSignedWCharType(); |
| 52 | } else { |
| 53 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 54 | "Unknown TSS value"); |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 55 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 56 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argiris Kirtzidis | 1ed03e7 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 57 | Result = Context.getUnsignedWCharType(); |
| 58 | } |
| 59 | break; |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 60 | case DeclSpec::TST_unspecified: |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 61 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | cce4224 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 62 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 63 | Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 64 | DS.getNumProtocolQualifiers()); |
| 65 | break; |
| 66 | } |
| 67 | |
Chris Lattner | 6ab935b | 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 | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 90 | case DeclSpec::TST_int: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 91 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 92 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 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; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 97 | } |
| 98 | } else { |
| 99 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 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; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 106 | break; |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 108 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 109 | case DeclSpec::TST_double: |
| 110 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 111 | Result = Context.LongDoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 112 | else |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 113 | Result = Context.DoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 114 | break; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 115 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 120 | case DeclSpec::TST_class: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 2e78db3 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 125 | assert(D && "Didn't get a decl for a class/enum/union/struct?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 130 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 131 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 139 | DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers(); |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 140 | |
Steve Naroff | 81f1bba | 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 | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 143 | if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 144 | if (PQ == 0) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 145 | Result = Context.getObjCInterfaceType(ObjCIntDecl); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 146 | break; |
| 147 | } |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 149 | Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, |
Chris Lattner | ada6379 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 150 | (ObjCProtocolDecl**)PQ, |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 151 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 152 | break; |
Chris Lattner | 4a68fe0 | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 153 | } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | ada6379 | 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 | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 158 | break; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | // TypeQuals handled by caller. |
Douglas Gregor | 1d66155 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 162 | Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 163 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 06fb867 | 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 | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 168 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 169 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 170 | break; |
Steve Naroff | 7cbb146 | 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 | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 175 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 176 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 179 | |
| 180 | // Handle complex types. |
| 181 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 182 | Result = Context.getComplexType(Result); |
Chris Lattner | 06fb867 | 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 | d496fb9 | 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 | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 189 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 190 | ProcessTypeAttributeList(Result, AL); |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 5fcb38b | 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 | cfac88d | 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(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 206 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 207 | << EltTy << DS.getSourceRange(); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 208 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
| 209 | } |
| 210 | } else { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 211 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 212 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 213 | << Result << DS.getSourceRange(); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 214 | TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 5fcb38b | 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 | } |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 231 | Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 232 | << Result << DS.getSourceRange(); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Douglas Gregor | b7b28a2 | 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 | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 246 | Result = Result.getQualifiedType(TypeQuals); |
| 247 | } |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 248 | return Result; |
| 249 | } |
| 250 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 251 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 252 | /// instances. Skip the outermost Skip type objects. |
| 253 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) { |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 254 | // long long is a C99 feature. |
Chris Lattner | 1a7d991 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 255 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | 11f20f9 | 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 | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 259 | QualType T = ConvertDeclSpecToType(D.getDeclSpec()); |
Sebastian Redl | 19fec9d | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 :). |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 263 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 264 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | switch (DeclType.Kind) { |
| 266 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 7aa5475 | 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; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 275 | case DeclaratorChunk::Pointer: |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 276 | if (T->isReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 277 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 278 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) |
| 279 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 280 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 281 | T = Context.IntTy; |
| 282 | } |
| 283 | |
Chris Lattner | 5fcb38b | 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 | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 287 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 288 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 289 | << T; |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 290 | DeclType.Ptr.TypeQuals &= QualType::Restrict; |
| 291 | } |
| 292 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 293 | // Apply the pointer typequals to the pointer object. |
| 294 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
| 295 | break; |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 296 | case DeclaratorChunk::Reference: { |
| 297 | // Whether we should suppress the creation of the reference. |
| 298 | bool SuppressReference = false; |
| 299 | if (T->isReferenceType()) { |
| 300 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 301 | // |
| 302 | // According to C++ DR 106, references to references are only |
| 303 | // diagnosed when they are written directly (e.g., "int & &"), |
| 304 | // but not when they happen via a typedef: |
| 305 | // |
| 306 | // typedef int& intref; |
| 307 | // typedef intref& intref2; |
| 308 | // |
| 309 | // Parser::ParserDeclaratorInternal diagnoses the case where |
| 310 | // references are written directly; here, we handle the |
| 311 | // collapsing of references-to-references as described in C++ |
| 312 | // DR 106 and amended by C++ DR 540. |
| 313 | SuppressReference = true; |
| 314 | } |
| 315 | |
| 316 | // C++ [dcl.ref]p1: |
| 317 | // A declarator that specifies the type “reference to cv void” |
| 318 | // is ill-formed. |
| 319 | if (T->isVoidType()) { |
| 320 | Diag(DeclType.Loc, diag::err_reference_to_void); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 321 | D.setInvalidType(true); |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 322 | T = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 325 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 326 | // object or incomplete types shall not be restrict-qualified." |
| 327 | if (DeclType.Ref.HasRestrict && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 328 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 329 | Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 330 | << T; |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 331 | DeclType.Ref.HasRestrict = false; |
| 332 | } |
| 333 | |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 334 | if (!SuppressReference) |
| 335 | T = Context.getReferenceType(T); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 336 | |
| 337 | // Handle restrict on references. |
| 338 | if (DeclType.Ref.HasRestrict) |
| 339 | T.addRestrict(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 340 | break; |
Douglas Gregor | b7b28a2 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 341 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | case DeclaratorChunk::Array: { |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 343 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 344 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 345 | ArrayType::ArraySizeModifier ASM; |
| 346 | if (ATI.isStar) |
| 347 | ASM = ArrayType::Star; |
| 348 | else if (ATI.hasStatic) |
| 349 | ASM = ArrayType::Static; |
| 350 | else |
| 351 | ASM = ArrayType::Normal; |
| 352 | |
| 353 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 354 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 355 | if (T->isIncompleteType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 356 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 357 | << T; |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 358 | T = Context.IntTy; |
| 359 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 360 | } else if (T->isFunctionType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 361 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions) |
| 362 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 363 | T = Context.getPointerType(T); |
| 364 | D.setInvalidType(true); |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 365 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 366 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 367 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references) |
| 368 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Chris Lattner | cfac88d | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 369 | T = RT->getPointeeType(); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 370 | D.setInvalidType(true); |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 371 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 372 | // If the element type is a struct or union that contains a variadic |
| 373 | // array, reject it: C99 6.7.2.1p2. |
| 374 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 375 | Diag(DeclType.Loc, diag::err_flexible_array_in_array) << T; |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 376 | T = Context.IntTy; |
| 377 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | 31fccaf | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 379 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 380 | Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | 31fccaf | 2008-08-18 22:49:54 +0000 | [diff] [blame] | 382 | |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 383 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 384 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 385 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 386 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 387 | D.setInvalidType(true); |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 388 | delete ArraySize; |
| 389 | ATI.NumElts = ArraySize = 0; |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 390 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 391 | llvm::APSInt ConstVal(32); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 392 | if (!ArraySize) { |
| 393 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame^] | 394 | } else if (ArraySize->isValueDependent()) { |
| 395 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | 2ff28d1 | 2008-05-14 00:40:18 +0000 | [diff] [blame] | 396 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 397 | !T->isConstantSizeType()) { |
Eli Friedman | 2ff28d1 | 2008-05-14 00:40:18 +0000 | [diff] [blame] | 398 | // Per C99, a variable array is an array with either a non-constant |
| 399 | // size or an element type that has a non-constant-size |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 400 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 401 | } else { |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 402 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 403 | // have a value greater than zero. |
| 404 | if (ConstVal.isSigned()) { |
| 405 | if (ConstVal.isNegative()) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 406 | Diag(ArraySize->getLocStart(), |
| 407 | diag::err_typecheck_negative_array_size) |
| 408 | << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 409 | D.setInvalidType(true); |
| 410 | } else if (ConstVal == 0) { |
| 411 | // GCC accepts zero sized static arrays. |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 412 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 413 | << ArraySize->getSourceRange(); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 416 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 418 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 419 | if (!getLangOptions().C99 && |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 420 | (ASM != ArrayType::Normal || |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame^] | 421 | (ArraySize && !ArraySize->isValueDependent() && |
| 422 | !ArraySize->isIntegerConstantExpr(Context)))) |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 423 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | case DeclaratorChunk::Function: |
| 427 | // If the function declarator has a prototype (i.e. it is not () and |
| 428 | // does not have a K&R-style identifier list), then the arguments are part |
| 429 | // of the type, otherwise the argument list is (). |
| 430 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 432 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 433 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 434 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 435 | T = Context.IntTy; |
| 436 | D.setInvalidType(true); |
| 437 | } |
| 438 | |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 439 | if (FTI.NumArgs == 0) { |
Argiris Kirtzidis | 1c51d47 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 440 | if (getLangOptions().CPlusPlus) { |
| 441 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 442 | // function takes no arguments. |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 443 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals); |
Argiris Kirtzidis | 1c51d47 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 444 | } else { |
| 445 | // Simple void foo(), where the incoming T is the result type. |
| 446 | T = Context.getFunctionTypeNoProto(T); |
| 447 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 448 | } else if (FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 449 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 450 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 451 | } else { |
| 452 | // Otherwise, we have a function with an argument list that is |
| 453 | // potentially variadic. |
| 454 | llvm::SmallVector<QualType, 16> ArgTys; |
| 455 | |
| 456 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 457 | ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; |
| 458 | QualType ArgTy = Param->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 459 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 460 | // |
| 461 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 462 | // This matches the conversion that is done in |
Nate Begeman | 2240f54 | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 463 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 464 | // argument type in the function prototype *will not* match the |
| 465 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 466 | // |
| 467 | // FIXME: We still apparently need the conversion in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 468 | // Sema::ActOnParamDeclarator(). This doesn't make any sense, since |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 469 | // it should be driving off the type being created here. |
| 470 | // |
| 471 | // FIXME: If a source translation tool needs to see the original type, |
| 472 | // then we need to consider storing both types somewhere... |
| 473 | // |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 474 | if (ArgTy->isArrayType()) { |
| 475 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 476 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 477 | ArgTy = Context.getPointerType(ArgTy); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | // Look for 'void'. void is allowed only as a single argument to a |
| 480 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 481 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 482 | else if (ArgTy->isVoidType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 483 | // If this is something like 'float(int, void)', reject it. 'void' |
| 484 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 485 | // have arguments of incomplete type. |
| 486 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 487 | Diag(DeclType.Loc, diag::err_void_only_param); |
| 488 | ArgTy = Context.IntTy; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 489 | Param->setType(ArgTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 490 | } else if (FTI.ArgInfo[i].Ident) { |
| 491 | // Reject, but continue to parse 'int(void abc)'. |
| 492 | Diag(FTI.ArgInfo[i].IdentLoc, |
| 493 | diag::err_param_with_void_type); |
| 494 | ArgTy = Context.IntTy; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 495 | Param->setType(ArgTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 496 | } else { |
| 497 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 498 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 499 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 500 | |
| 501 | // Do not add 'void' to the ArgTys list. |
| 502 | break; |
| 503 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 504 | } else if (!FTI.hasPrototype) { |
| 505 | if (ArgTy->isPromotableIntegerType()) { |
| 506 | ArgTy = Context.IntTy; |
| 507 | } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { |
| 508 | if (BTy->getKind() == BuiltinType::Float) |
| 509 | ArgTy = Context.DoubleTy; |
| 510 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | ArgTys.push_back(ArgTy); |
| 514 | } |
| 515 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 516 | FTI.isVariadic, FTI.TypeQuals); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 517 | } |
| 518 | break; |
| 519 | } |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 520 | |
| 521 | // See if there are any attributes on this declarator chunk. |
| 522 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 523 | ProcessTypeAttributeList(T, AL); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 524 | } |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 525 | |
| 526 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
| 527 | const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto(); |
| 528 | assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?"); |
| 529 | |
| 530 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 531 | // for a nonstatic member function, the function type to which a pointer |
| 532 | // to member refers, or the top-level function type of a function typedef |
| 533 | // declaration. |
| 534 | if (FnTy->getTypeQuals() != 0 && |
| 535 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 536 | (D.getContext() != Declarator::MemberContext || |
| 537 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
| 538 | |
| 539 | if (D.isFunctionDeclarator()) |
| 540 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 541 | else |
| 542 | Diag(D.getIdentifierLoc(), |
| 543 | diag::err_invalid_qualified_typedef_function_type_use); |
| 544 | |
| 545 | // Strip the cv-quals from the type. |
| 546 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 547 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argiris Kirtzidis | 4b269b4 | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 550 | |
Chris Lattner | f9e90cc | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 551 | // If there were any type attributes applied to the decl itself (not the |
| 552 | // type, apply the type attribute to the type!) |
| 553 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 554 | ProcessTypeAttributeList(T, Attrs); |
Chris Lattner | f9e90cc | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 556 | return T; |
| 557 | } |
| 558 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 559 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | ff746bc | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 560 | /// declarator |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 561 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
| 562 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 563 | QualType T = MDecl->getResultType(); |
| 564 | llvm::SmallVector<QualType, 16> ArgTys; |
| 565 | |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 566 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 567 | if (MDecl->isInstance()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 568 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 569 | selfTy = Context.getPointerType(selfTy); |
| 570 | ArgTys.push_back(selfTy); |
| 571 | } |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 572 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 573 | ArgTys.push_back(Context.getObjCIdType()); |
| 574 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 685d792 | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 576 | for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 577 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 578 | QualType ArgTy = PDecl->getType(); |
| 579 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 580 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 581 | // This matches the conversion that is done in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 582 | // Sema::ActOnParamDeclarator(). |
| 583 | if (ArgTy->isArrayType()) |
| 584 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 585 | else if (ArgTy->isFunctionType()) |
| 586 | ArgTy = Context.getPointerType(ArgTy); |
| 587 | ArgTys.push_back(ArgTy); |
| 588 | } |
| 589 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 590 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 591 | return T; |
| 592 | } |
| 593 | |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 594 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types (FIXME: |
| 595 | /// or pointer-to-member types) that may be similar (C++ 4.4), |
| 596 | /// replaces T1 and T2 with the type that they point to and return |
| 597 | /// true. If T1 and T2 aren't pointer types or pointer-to-member |
| 598 | /// types, or if they are not similar at this level, returns false and |
| 599 | /// leaves T1 and T2 unchanged. Top-level qualifiers on T1 and T2 are |
Douglas Gregor | f8e9270 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 600 | /// ignored. This function will typically be called in a loop that |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 601 | /// successively "unwraps" pointer and pointer-to-member types to |
| 602 | /// compare them at each level. |
| 603 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) |
| 604 | { |
| 605 | const PointerType *T1PtrType = T1->getAsPointerType(), |
| 606 | *T2PtrType = T2->getAsPointerType(); |
| 607 | if (T1PtrType && T2PtrType) { |
| 608 | T1 = T1PtrType->getPointeeType(); |
| 609 | T2 = T2PtrType->getPointeeType(); |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | // FIXME: pointer-to-member types |
| 614 | return false; |
| 615 | } |
| 616 | |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 617 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 618 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 619 | // the parser. |
| 620 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 621 | |
Sebastian Redl | 66df3ef | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 622 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 623 | |
| 624 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | 2b9422f | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 626 | // Check that there are no default arguments (C++ only). |
| 627 | if (getLangOptions().CPlusPlus) |
| 628 | CheckExtraCXXDefaultArguments(D); |
| 629 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 630 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 631 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 632 | // though it will not reflect the user specified type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 633 | return T.getAsOpaquePtr(); |
| 634 | } |
| 635 | |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 636 | |
| 637 | |
| 638 | //===----------------------------------------------------------------------===// |
| 639 | // Type Attribute Processing |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | |
| 642 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 643 | /// specified type. The attribute contains 1 argument, the id of the address |
| 644 | /// space for the type. |
| 645 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
| 646 | const AttributeList &Attr, Sema &S){ |
| 647 | // If this type is already address space qualified, reject it. |
| 648 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 649 | // for two or more different address spaces." |
| 650 | if (Type.getAddressSpace()) { |
| 651 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | // Check the attribute arguments. |
| 656 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 657 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 661 | llvm::APSInt addrSpace(32); |
| 662 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 663 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 664 | << ASArgExpr->getSourceRange(); |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
| 669 | Type = S.Context.getASQualType(Type, ASIdx); |
| 670 | } |
| 671 | |
| 672 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 673 | // Scan through and apply attributes to this type where it makes sense. Some |
| 674 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 675 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 676 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 677 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 678 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 679 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | 99dbc96 | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 680 | switch (AL->getKind()) { |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 681 | default: break; |
| 682 | case AttributeList::AT_address_space: |
Chris Lattner | 65a5704 | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 683 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 684 | break; |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 685 | } |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 686 | } |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Eli Friedman | 86ad522 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 689 | |