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" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 22 | #include "clang/Parse/DeclSpec.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 26 | /// \brief Perform adjustment on the parameter type of a function. |
| 27 | /// |
| 28 | /// This routine adjusts the given parameter type @p T to the actual |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 30 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 31 | QualType Sema::adjustParameterType(QualType T) { |
| 32 | // C99 6.7.5.3p7: |
| 33 | if (T->isArrayType()) { |
| 34 | // C99 6.7.5.3p7: |
| 35 | // A declaration of a parameter as "array of type" shall be |
| 36 | // adjusted to "qualified pointer to type", where the type |
| 37 | // qualifiers (if any) are those specified within the [ and ] of |
| 38 | // the array type derivation. |
| 39 | return Context.getArrayDecayedType(T); |
| 40 | } else if (T->isFunctionType()) |
| 41 | // C99 6.7.5.3p8: |
| 42 | // A declaration of a parameter as "function returning type" |
| 43 | // shall be adjusted to "pointer to function returning type", as |
| 44 | // in 6.3.2.1. |
| 45 | return Context.getPointerType(T); |
| 46 | |
| 47 | return T; |
| 48 | } |
| 49 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 50 | /// \brief Convert the specified declspec to the appropriate type |
| 51 | /// object. |
| 52 | /// \param DS the declaration specifiers |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 53 | /// \param DeclLoc The location of the declarator identifier or invalid if none. |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 54 | /// \param SourceTy QualType representing the type as written in source form. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 55 | /// \returns The type described by the declaration specifiers. This function |
| 56 | /// never returns null. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 57 | QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS, |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 58 | SourceLocation DeclLoc, |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 59 | bool &isInvalid, QualType &SourceTy) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 61 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 62 | QualType Result; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 63 | SourceTy = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 66 | case DeclSpec::TST_void: |
| 67 | Result = Context.VoidTy; |
| 68 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | case DeclSpec::TST_char: |
| 70 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 71 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 73 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | else { |
| 75 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 76 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 77 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 79 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 80 | case DeclSpec::TST_wchar: |
| 81 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 82 | Result = Context.WCharTy; |
| 83 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 84 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 85 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 86 | Result = Context.getSignedWCharType(); |
| 87 | } else { |
| 88 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 89 | "Unknown TSS value"); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 90 | Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
| 91 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 92 | Result = Context.getUnsignedWCharType(); |
| 93 | } |
| 94 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 95 | case DeclSpec::TST_char16: |
| 96 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 97 | "Unknown TSS value"); |
| 98 | Result = Context.Char16Ty; |
| 99 | break; |
| 100 | case DeclSpec::TST_char32: |
| 101 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 102 | "Unknown TSS value"); |
| 103 | Result = Context.Char32Ty; |
| 104 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 105 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 106 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 107 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 108 | SourceTy = Context.getObjCProtocolListType(QualType(), |
| 109 | (ObjCProtocolDecl**)PQ, |
| 110 | DS.getNumProtocolQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 112 | (ObjCProtocolDecl**)PQ, |
Steve Naroff | 683087f | 2009-06-29 16:22:52 +0000 | [diff] [blame] | 113 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 117 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 118 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 119 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 120 | // Note that the one exception to this is function definitions, which are |
| 121 | // allowed to be completely missing a declspec. This is handled in the |
| 122 | // parser already though by it pretending to have seen an 'int' in this |
| 123 | // case. |
| 124 | if (getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 125 | // In C89 mode, we only warn if there is a completely missing declspec |
| 126 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 127 | if (DS.isEmpty()) { |
| 128 | if (DeclLoc.isInvalid()) |
| 129 | DeclLoc = DS.getSourceRange().getBegin(); |
Eli Friedman | fcff577 | 2009-06-03 12:22:01 +0000 | [diff] [blame] | 130 | Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 131 | << DS.getSourceRange() |
Chris Lattner | 173144a | 2009-02-27 22:31:56 +0000 | [diff] [blame] | 132 | << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), |
| 133 | "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 134 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 135 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 136 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 137 | // "At least one type specifier shall be given in the declaration |
| 138 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 139 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 140 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 141 | if (DeclLoc.isInvalid()) |
| 142 | DeclLoc = DS.getSourceRange().getBegin(); |
| 143 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 144 | if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 145 | Diag(DeclLoc, diag::err_missing_type_specifier) |
| 146 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 148 | // When this occurs in C++ code, often something is very broken with the |
| 149 | // value being declared, poison it as invalid so we don't get chains of |
| 150 | // errors. |
| 151 | isInvalid = true; |
| 152 | } else { |
Eli Friedman | fcff577 | 2009-06-03 12:22:01 +0000 | [diff] [blame] | 153 | Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 154 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 155 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
| 158 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 159 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 161 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 162 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 163 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 164 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 165 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | } |
| 167 | } else { |
| 168 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 169 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 170 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 171 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 172 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 175 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 177 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 178 | case DeclSpec::TST_double: |
| 179 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 180 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 181 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 182 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 183 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 184 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 186 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 187 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 188 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
| 189 | Result = Context.IntTy; |
| 190 | isInvalid = true; |
| 191 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 192 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | case DeclSpec::TST_enum: |
| 194 | case DeclSpec::TST_union: |
| 195 | case DeclSpec::TST_struct: { |
| 196 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 197 | if (!D) { |
| 198 | // This can happen in C++ with ambiguous lookups. |
| 199 | Result = Context.IntTy; |
| 200 | isInvalid = true; |
| 201 | break; |
| 202 | } |
| 203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 205 | DS.getTypeSpecSign() == 0 && |
| 206 | "Can't handle qualifiers on typedef names yet!"); |
| 207 | // TypeQuals handled by caller. |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 208 | Result = Context.getTypeDeclType(cast<TypeDecl>(D)); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 209 | |
| 210 | // In C++, make an ElaboratedType. |
| 211 | if (getLangOptions().CPlusPlus) { |
| 212 | TagDecl::TagKind Tag |
| 213 | = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType()); |
| 214 | Result = Context.getElaboratedType(Result, Tag); |
| 215 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 217 | if (D->isInvalidDecl()) |
| 218 | isInvalid = true; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 219 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 221 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 223 | DS.getTypeSpecSign() == 0 && |
| 224 | "Can't handle qualifiers on typedef names yet!"); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 225 | Result = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 227 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 228 | SourceTy = Context.getObjCProtocolListType(Result, |
| 229 | (ObjCProtocolDecl**)PQ, |
| 230 | DS.getNumProtocolQualifiers()); |
| 231 | if (const ObjCInterfaceType * |
| 232 | Interface = Result->getAs<ObjCInterfaceType>()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 233 | // It would be nice if protocol qualifiers were only stored with the |
| 234 | // ObjCObjectPointerType. Unfortunately, this isn't possible due |
| 235 | // to the following typedef idiom (which is uncommon, but allowed): |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | // |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 237 | // typedef Foo<P> T; |
| 238 | // static void func() { |
| 239 | // Foo<P> *yy; |
| 240 | // T *zz; |
| 241 | // } |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 242 | Result = Context.getObjCInterfaceType(Interface->getDecl(), |
| 243 | (ObjCProtocolDecl**)PQ, |
| 244 | DS.getNumProtocolQualifiers()); |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 245 | } else if (Result->isObjCIdType()) |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 246 | // id<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 248 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
| 249 | else if (Result->isObjCClassType()) { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 250 | if (DeclLoc.isInvalid()) |
| 251 | DeclLoc = DS.getSourceRange().getBegin(); |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 252 | // Class<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy, |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 254 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 255 | } else { |
| 256 | if (DeclLoc.isInvalid()) |
| 257 | DeclLoc = DS.getSourceRange().getBegin(); |
| 258 | Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
| 259 | << DS.getSourceRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 260 | isInvalid = true; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 261 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 264 | // If this is a reference to an invalid typedef, propagate the invalidity. |
| 265 | if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) |
| 266 | if (TDT->getDecl()->isInvalidDecl()) |
| 267 | isInvalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 270 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 272 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 273 | // FIXME: Preserve type source info. |
| 274 | Result = GetTypeFromParser(DS.getTypeRep()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 275 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 276 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 277 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 278 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 279 | case DeclSpec::TST_typeofExpr: { |
| 280 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 281 | assert(E && "Didn't get an expression for typeof?"); |
| 282 | // TypeQuals handled by caller. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 283 | Result = Context.getTypeOfExprType(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 284 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 285 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 286 | case DeclSpec::TST_decltype: { |
| 287 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 288 | assert(E && "Didn't get an expression for decltype?"); |
| 289 | // TypeQuals handled by caller. |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 290 | Result = BuildDecltypeType(E); |
| 291 | if (Result.isNull()) { |
| 292 | Result = Context.IntTy; |
| 293 | isInvalid = true; |
| 294 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 297 | case DeclSpec::TST_auto: { |
| 298 | // TypeQuals handled by caller. |
| 299 | Result = Context.UndeducedAutoTy; |
| 300 | break; |
| 301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 303 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 304 | Result = Context.IntTy; |
| 305 | isInvalid = true; |
| 306 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 309 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 310 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
| 311 | if (getLangOptions().Freestanding) |
| 312 | Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 313 | Result = Context.getComplexType(Result); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 314 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 316 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 317 | "FIXME: imaginary types not supported yet!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 319 | // See if there are any attributes on the declspec that apply to the type (as |
| 320 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 321 | if (const AttributeList *AL = DS.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 322 | ProcessTypeAttributeList(Result, AL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 324 | // Apply const/volatile/restrict qualifiers to T. |
| 325 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 326 | |
| 327 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 328 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 329 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 330 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 331 | if (Result->isPointerType() || Result->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | QualType EltTy = Result->isPointerType() ? |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 333 | Result->getAs<PointerType>()->getPointeeType() : |
| 334 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 336 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 337 | // incomplete type. |
| 338 | if (!EltTy->isIncompleteOrObjectType()) { |
| 339 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 340 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 341 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 342 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 343 | } |
| 344 | } else { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 345 | Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 346 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 347 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 348 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 350 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 352 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 353 | // of a function type includes any type qualifiers, the behavior is |
| 354 | // undefined." |
| 355 | if (Result->isFunctionType() && TypeQuals) { |
| 356 | // Get some location to point at, either the C or V location. |
| 357 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 358 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 359 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 360 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 361 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 362 | else { |
| 363 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 364 | "Has CVR quals but not C, V, or R?"); |
| 365 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 367 | Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 368 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 371 | // C++ [dcl.ref]p1: |
| 372 | // Cv-qualified references are ill-formed except when the |
| 373 | // cv-qualifiers are introduced through the use of a typedef |
| 374 | // (7.1.3) or of a template type argument (14.3), in which |
| 375 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 376 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 377 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 378 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 379 | TypeQuals &= ~DeclSpec::TQ_const; |
| 380 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | } |
| 382 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 383 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 384 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 385 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 386 | |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 387 | if (SourceTy.isNull()) |
| 388 | SourceTy = Result; |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 389 | return Result; |
| 390 | } |
| 391 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 392 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 393 | if (Entity) |
| 394 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 396 | return "type name"; |
| 397 | } |
| 398 | |
| 399 | /// \brief Build a pointer type. |
| 400 | /// |
| 401 | /// \param T The type to which we'll be building a pointer. |
| 402 | /// |
| 403 | /// \param Quals The cvr-qualifiers to be applied to the pointer type. |
| 404 | /// |
| 405 | /// \param Loc The location of the entity whose type involves this |
| 406 | /// pointer type or, if there is no such entity, the location of the |
| 407 | /// type that will have pointer type. |
| 408 | /// |
| 409 | /// \param Entity The name of the entity that involves the pointer |
| 410 | /// type, if known. |
| 411 | /// |
| 412 | /// \returns A suitable pointer type, if there are no |
| 413 | /// errors. Otherwise, returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | QualType Sema::BuildPointerType(QualType T, unsigned Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 415 | SourceLocation Loc, DeclarationName Entity) { |
| 416 | if (T->isReferenceType()) { |
| 417 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 418 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
| 419 | << getPrintableNameForEntity(Entity); |
| 420 | return QualType(); |
| 421 | } |
| 422 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 423 | Qualifiers Qs = Qualifiers::fromCVRMask(Quals); |
| 424 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 425 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 426 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 427 | if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 428 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 429 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 430 | Qs.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Build the pointer type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 434 | return Context.getQualifiedType(Context.getPointerType(T), Qs); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /// \brief Build a reference type. |
| 438 | /// |
| 439 | /// \param T The type to which we'll be building a reference. |
| 440 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 441 | /// \param CVR The cvr-qualifiers to be applied to the reference type. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 442 | /// |
| 443 | /// \param Loc The location of the entity whose type involves this |
| 444 | /// reference type or, if there is no such entity, the location of the |
| 445 | /// type that will have reference type. |
| 446 | /// |
| 447 | /// \param Entity The name of the entity that involves the reference |
| 448 | /// type, if known. |
| 449 | /// |
| 450 | /// \returns A suitable reference type, if there are no |
| 451 | /// errors. Otherwise, returns a NULL type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 452 | QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned CVR, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 453 | SourceLocation Loc, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 454 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 455 | if (LValueRef) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 456 | if (const RValueReferenceType *R = T->getAs<RValueReferenceType>()) { |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 457 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 458 | // reference to a type T, and attempt to create the type "lvalue |
| 459 | // reference to cv TD" creates the type "lvalue reference to T". |
| 460 | // We use the qualifiers (restrict or none) of the original reference, |
| 461 | // not the new ones. This is consistent with GCC. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 462 | QualType LVRT = Context.getLValueReferenceType(R->getPointeeType()); |
| 463 | return Context.getQualifiedType(LVRT, T.getQualifiers()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 466 | if (T->isReferenceType()) { |
| 467 | // C++ [dcl.ref]p4: There shall be no references to references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | // |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 469 | // According to C++ DR 106, references to references are only |
| 470 | // diagnosed when they are written directly (e.g., "int & &"), |
| 471 | // but not when they happen via a typedef: |
| 472 | // |
| 473 | // typedef int& intref; |
| 474 | // typedef intref& intref2; |
| 475 | // |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 476 | // Parser::ParseDeclaratorInternal diagnoses the case where |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 477 | // references are written directly; here, we handle the |
| 478 | // collapsing of references-to-references as described in C++ |
| 479 | // DR 106 and amended by C++ DR 540. |
| 480 | return T; |
| 481 | } |
| 482 | |
| 483 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 484 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 485 | // is ill-formed. |
| 486 | if (T->isVoidType()) { |
| 487 | Diag(Loc, diag::err_reference_to_void); |
| 488 | return QualType(); |
| 489 | } |
| 490 | |
| 491 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 492 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 493 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 494 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 495 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 496 | Quals.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // C++ [dcl.ref]p1: |
| 500 | // [...] Cv-qualified references are ill-formed except when the |
| 501 | // cv-qualifiers are introduced through the use of a typedef |
| 502 | // (7.1.3) or of a template type argument (14.3), in which case |
| 503 | // the cv-qualifiers are ignored. |
| 504 | // |
| 505 | // We diagnose extraneous cv-qualifiers for the non-typedef, |
| 506 | // non-template type argument case within the parser. Here, we just |
| 507 | // ignore any extraneous cv-qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 508 | Quals.removeConst(); |
| 509 | Quals.removeVolatile(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 510 | |
| 511 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 512 | if (LValueRef) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 513 | return Context.getQualifiedType(Context.getLValueReferenceType(T), Quals); |
| 514 | return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /// \brief Build an array type. |
| 518 | /// |
| 519 | /// \param T The type of each element in the array. |
| 520 | /// |
| 521 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | /// |
| 523 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 524 | /// |
| 525 | /// \param Quals The cvr-qualifiers to be applied to the array's |
| 526 | /// element type. |
| 527 | /// |
| 528 | /// \param Loc The location of the entity whose type involves this |
| 529 | /// array type or, if there is no such entity, the location of the |
| 530 | /// type that will have array type. |
| 531 | /// |
| 532 | /// \param Entity The name of the entity that involves the array |
| 533 | /// type, if known. |
| 534 | /// |
| 535 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 536 | /// returns a NULL type. |
| 537 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 538 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 539 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 541 | SourceLocation Loc = Brackets.getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 543 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | if (RequireCompleteType(Loc, T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 545 | diag::err_illegal_decl_array_incomplete_type)) |
| 546 | return QualType(); |
| 547 | |
| 548 | if (T->isFunctionType()) { |
| 549 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
| 550 | << getPrintableNameForEntity(Entity); |
| 551 | return QualType(); |
| 552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 554 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 555 | if (T->isReferenceType()) { |
| 556 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 557 | << getPrintableNameForEntity(Entity); |
| 558 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 560 | |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 561 | if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 563 | << getPrintableNameForEntity(Entity); |
| 564 | return QualType(); |
| 565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 567 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 568 | // If the element type is a struct or union that contains a variadic |
| 569 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 570 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 571 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
| 572 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 573 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 574 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 577 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 578 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 579 | !ArraySize->getType()->isIntegerType()) { |
| 580 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 581 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 582 | ArraySize->Destroy(Context); |
| 583 | return QualType(); |
| 584 | } |
| 585 | llvm::APSInt ConstVal(32); |
| 586 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 587 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 588 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 589 | else |
| 590 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 591 | } else if (ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 592 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 593 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
| 594 | (!T->isDependentType() && !T->isConstantSizeType())) { |
| 595 | // Per C99, a variable array is an array with either a non-constant |
| 596 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 597 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 598 | } else { |
| 599 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 600 | // have a value greater than zero. |
| 601 | if (ConstVal.isSigned()) { |
| 602 | if (ConstVal.isNegative()) { |
| 603 | Diag(ArraySize->getLocStart(), |
| 604 | diag::err_typecheck_negative_array_size) |
| 605 | << ArraySize->getSourceRange(); |
| 606 | return QualType(); |
| 607 | } else if (ConstVal == 0) { |
| 608 | // GCC accepts zero sized static arrays. |
| 609 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 610 | << ArraySize->getSourceRange(); |
| 611 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | } |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame^] | 613 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 614 | } |
| 615 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 616 | if (!getLangOptions().C99) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 618 | !ArraySize->isValueDependent() && |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 619 | !ArraySize->isIntegerConstantExpr(Context)) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 620 | Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 621 | else if (ASM != ArrayType::Normal || Quals != 0) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 622 | Diag(Loc, |
| 623 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
| 624 | : diag::ext_c99_array_usage); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return T; |
| 628 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 629 | |
| 630 | /// \brief Build an ext-vector type. |
| 631 | /// |
| 632 | /// Run the required checks for the extended vector type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 634 | SourceLocation AttrLoc) { |
| 635 | |
| 636 | Expr *Arg = (Expr *)ArraySize.get(); |
| 637 | |
| 638 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 639 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 641 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 642 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 643 | return QualType(); |
| 644 | } |
| 645 | |
| 646 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 647 | llvm::APSInt vecSize(32); |
| 648 | if (!Arg->isIntegerConstantExpr(vecSize, Context)) { |
| 649 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 650 | << "ext_vector_type" << Arg->getSourceRange(); |
| 651 | return QualType(); |
| 652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
| 654 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 655 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 657 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 658 | if (vectorSize == 0) { |
| 659 | Diag(AttrLoc, diag::err_attribute_zero_size) |
| 660 | << Arg->getSourceRange(); |
| 661 | return QualType(); |
| 662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 664 | if (!T->isDependentType()) |
| 665 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 669 | AttrLoc); |
| 670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 672 | /// \brief Build a function type. |
| 673 | /// |
| 674 | /// This routine checks the function type according to C++ rules and |
| 675 | /// under the assumption that the result type and parameter types have |
| 676 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 677 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 678 | /// simpler form that is only suitable for this narrow use case. |
| 679 | /// |
| 680 | /// \param T The return type of the function. |
| 681 | /// |
| 682 | /// \param ParamTypes The parameter types of the function. This array |
| 683 | /// will be modified to account for adjustments to the types of the |
| 684 | /// function parameters. |
| 685 | /// |
| 686 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 687 | /// |
| 688 | /// \param Variadic Whether this is a variadic function type. |
| 689 | /// |
| 690 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 691 | /// |
| 692 | /// \param Loc The location of the entity whose type involves this |
| 693 | /// function type or, if there is no such entity, the location of the |
| 694 | /// type that will have function type. |
| 695 | /// |
| 696 | /// \param Entity The name of the entity that involves the function |
| 697 | /// type, if known. |
| 698 | /// |
| 699 | /// \returns A suitable function type, if there are no |
| 700 | /// errors. Otherwise, returns a NULL type. |
| 701 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 703 | unsigned NumParamTypes, |
| 704 | bool Variadic, unsigned Quals, |
| 705 | SourceLocation Loc, DeclarationName Entity) { |
| 706 | if (T->isArrayType() || T->isFunctionType()) { |
| 707 | Diag(Loc, diag::err_func_returning_array_function) << T; |
| 708 | return QualType(); |
| 709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 711 | bool Invalid = false; |
| 712 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 713 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 714 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 715 | Diag(Loc, diag::err_param_with_void_type); |
| 716 | Invalid = true; |
| 717 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 718 | |
Anders Carlsson | 83913e3 | 2009-09-16 23:47:08 +0000 | [diff] [blame] | 719 | ParamTypes[Idx] = adjustFunctionParamType(ParamType); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | if (Invalid) |
| 723 | return QualType(); |
| 724 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 726 | Quals); |
| 727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 729 | /// \brief Build a member pointer type \c T Class::*. |
| 730 | /// |
| 731 | /// \param T the type to which the member pointer refers. |
| 732 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 733 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 734 | /// \param Loc the location where this type begins |
| 735 | /// \param Entity the name of the entity that will have this member pointer type |
| 736 | /// |
| 737 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 738 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 740 | unsigned CVR, SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 741 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 742 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 743 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 744 | // Verify that we're not building a pointer to pointer to function with |
| 745 | // exception specification. |
| 746 | if (CheckDistantExceptionSpec(T)) { |
| 747 | Diag(Loc, diag::err_distant_exception_spec); |
| 748 | |
| 749 | // FIXME: If we're doing this as part of template instantiation, |
| 750 | // we should return immediately. |
| 751 | |
| 752 | // Build the type anyway, but use the canonical type so that the |
| 753 | // exception specifiers are stripped off. |
| 754 | T = Context.getCanonicalType(T); |
| 755 | } |
| 756 | |
| 757 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 758 | // with reference type, or "cv void." |
| 759 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 760 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 761 | << (Entity? Entity.getAsString() : "type name"); |
| 762 | return QualType(); |
| 763 | } |
| 764 | |
| 765 | if (T->isVoidType()) { |
| 766 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 767 | << (Entity? Entity.getAsString() : "type name"); |
| 768 | return QualType(); |
| 769 | } |
| 770 | |
| 771 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 772 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 773 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 774 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 775 | << T; |
| 776 | |
| 777 | // FIXME: If we're doing this as part of template instantiation, |
| 778 | // we should return immediately. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 779 | Quals.removeRestrict(); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 783 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 784 | return QualType(); |
| 785 | } |
| 786 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 787 | return Context.getQualifiedType( |
| 788 | Context.getMemberPointerType(T, Class.getTypePtr()), Quals); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 791 | /// \brief Build a block pointer type. |
| 792 | /// |
| 793 | /// \param T The type to which we'll be building a block pointer. |
| 794 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 795 | /// \param CVR The cvr-qualifiers to be applied to the block pointer type. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 796 | /// |
| 797 | /// \param Loc The location of the entity whose type involves this |
| 798 | /// block pointer type or, if there is no such entity, the location of the |
| 799 | /// type that will have block pointer type. |
| 800 | /// |
| 801 | /// \param Entity The name of the entity that involves the block pointer |
| 802 | /// type, if known. |
| 803 | /// |
| 804 | /// \returns A suitable block pointer type, if there are no |
| 805 | /// errors. Otherwise, returns a NULL type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 806 | QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 808 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 809 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 810 | Diag(Loc, diag::err_nonfunction_block_type); |
| 811 | return QualType(); |
| 812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 814 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 815 | return Context.getQualifiedType(Context.getBlockPointerType(T), Quals); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 818 | QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) { |
| 819 | QualType QT = QualType::getFromOpaquePtr(Ty); |
| 820 | DeclaratorInfo *DI = 0; |
| 821 | if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
| 822 | QT = LIT->getType(); |
| 823 | DI = LIT->getDeclaratorInfo(); |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 826 | if (DInfo) *DInfo = DI; |
| 827 | return QT; |
| 828 | } |
| 829 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 830 | /// GetTypeForDeclarator - Convert the type for the specified |
| 831 | /// declarator to Type instances. Skip the outermost Skip type |
| 832 | /// objects. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 833 | /// |
| 834 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 835 | /// owns the declaration of a type (e.g., the definition of a struct |
| 836 | /// type), then *OwnedDecl will receive the owned declaration. |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 837 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
| 838 | DeclaratorInfo **DInfo, unsigned Skip, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 839 | TagDecl **OwnedDecl) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 840 | bool OmittedReturnType = false; |
| 841 | |
| 842 | if (D.getContext() == Declarator::BlockLiteralContext |
| 843 | && Skip == 0 |
| 844 | && !D.getDeclSpec().hasTypeSpecifier() |
| 845 | && (D.getNumTypeObjects() == 0 |
| 846 | || (D.getNumTypeObjects() == 1 |
| 847 | && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) |
| 848 | OmittedReturnType = true; |
| 849 | |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 850 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 851 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 852 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 853 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 854 | |
| 855 | // Determine the type of the declarator. Not all forms of declarator |
| 856 | // have a type. |
| 857 | QualType T; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 858 | // The QualType referring to the type as written in source code. We can't use |
| 859 | // T because it can change due to semantic analysis. |
| 860 | QualType SourceTy; |
| 861 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 862 | switch (D.getKind()) { |
| 863 | case Declarator::DK_Abstract: |
| 864 | case Declarator::DK_Normal: |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 865 | case Declarator::DK_Operator: |
| 866 | case Declarator::DK_TemplateId: { |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 867 | const DeclSpec &DS = D.getDeclSpec(); |
| 868 | if (OmittedReturnType) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 869 | // We default to a dependent type initially. Can be modified by |
| 870 | // the first return statement. |
| 871 | T = Context.DependentTy; |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 872 | } else { |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 873 | bool isInvalid = false; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 874 | T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid, SourceTy); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 875 | if (isInvalid) |
| 876 | D.setInvalidType(true); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 877 | else if (OwnedDecl && DS.isTypeSpecOwned()) |
| 878 | *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep()); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 879 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 880 | break; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 881 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 882 | |
| 883 | case Declarator::DK_Constructor: |
| 884 | case Declarator::DK_Destructor: |
| 885 | case Declarator::DK_Conversion: |
| 886 | // Constructors and destructors don't have return types. Use |
| 887 | // "void" instead. Conversion operators will check their return |
| 888 | // types separately. |
| 889 | T = Context.VoidTy; |
| 890 | break; |
| 891 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 892 | |
| 893 | if (SourceTy.isNull()) |
| 894 | SourceTy = T; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 895 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 896 | if (T == Context.UndeducedAutoTy) { |
| 897 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 899 | switch (D.getContext()) { |
| 900 | case Declarator::KNRTypeListContext: |
| 901 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 902 | break; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 903 | case Declarator::PrototypeContext: |
| 904 | Error = 0; // Function prototype |
| 905 | break; |
| 906 | case Declarator::MemberContext: |
| 907 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
| 908 | case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break; |
| 909 | case TagDecl::TK_struct: Error = 1; /* Struct member */ break; |
| 910 | case TagDecl::TK_union: Error = 2; /* Union member */ break; |
| 911 | case TagDecl::TK_class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 913 | break; |
| 914 | case Declarator::CXXCatchContext: |
| 915 | Error = 4; // Exception declaration |
| 916 | break; |
| 917 | case Declarator::TemplateParamContext: |
| 918 | Error = 5; // Template parameter |
| 919 | break; |
| 920 | case Declarator::BlockLiteralContext: |
| 921 | Error = 6; // Block literal |
| 922 | break; |
| 923 | case Declarator::FileContext: |
| 924 | case Declarator::BlockContext: |
| 925 | case Declarator::ForContext: |
| 926 | case Declarator::ConditionContext: |
| 927 | case Declarator::TypeNameContext: |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | if (Error != -1) { |
| 932 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 933 | << Error; |
| 934 | T = Context.IntTy; |
| 935 | D.setInvalidType(true); |
| 936 | } |
| 937 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 939 | // The name we're declaring, if any. |
| 940 | DeclarationName Name; |
| 941 | if (D.getIdentifier()) |
| 942 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 944 | bool ShouldBuildInfo = DInfo != 0; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 945 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 946 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 947 | // DeclTypeInfos are ordered from the identifier out, which is |
| 948 | // opposite of what we want :). |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 949 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 950 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | switch (DeclType.Kind) { |
| 952 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 953 | case DeclaratorChunk::BlockPointer: |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 954 | if (ShouldBuildInfo) { |
| 955 | if (SourceTy->isFunctionType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 956 | SourceTy |
| 957 | = Context.getQualifiedType(Context.getBlockPointerType(SourceTy), |
| 958 | Qualifiers::fromCVRMask(DeclType.Cls.TypeQuals)); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 959 | else |
| 960 | // If not function type Context::getBlockPointerType asserts, |
| 961 | // so just give up. |
| 962 | ShouldBuildInfo = false; |
| 963 | } |
| 964 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 965 | // If blocks are disabled, emit an error. |
| 966 | if (!LangOpts.Blocks) |
| 967 | Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | |
| 969 | T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 970 | Name); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 971 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | case DeclaratorChunk::Pointer: |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 973 | //FIXME: Use ObjCObjectPointer for info when appropriate. |
| 974 | if (ShouldBuildInfo) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 975 | SourceTy = Context.getQualifiedType(Context.getPointerType(SourceTy), |
| 976 | Qualifiers::fromCVRMask(DeclType.Ptr.TypeQuals)); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 977 | // Verify that we're not building a pointer to pointer to function with |
| 978 | // exception specification. |
| 979 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 980 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 981 | D.setInvalidType(true); |
| 982 | // Build the type anyway. |
| 983 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 984 | if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 985 | const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 986 | T = Context.getObjCObjectPointerType(T, |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 987 | (ObjCProtocolDecl **)OIT->qual_begin(), |
| 988 | OIT->getNumProtocols()); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 989 | break; |
| 990 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 991 | T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 993 | case DeclaratorChunk::Reference: { |
| 994 | Qualifiers Quals; |
| 995 | if (DeclType.Ref.HasRestrict) Quals.addRestrict(); |
| 996 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 997 | if (ShouldBuildInfo) { |
| 998 | if (DeclType.Ref.LValueRef) |
| 999 | SourceTy = Context.getLValueReferenceType(SourceTy); |
| 1000 | else |
| 1001 | SourceTy = Context.getRValueReferenceType(SourceTy); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1002 | SourceTy = Context.getQualifiedType(SourceTy, Quals); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1005 | // Verify that we're not building a reference to pointer to function with |
| 1006 | // exception specification. |
| 1007 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1008 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1009 | D.setInvalidType(true); |
| 1010 | // Build the type anyway. |
| 1011 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1012 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1013 | DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1015 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | case DeclaratorChunk::Array: { |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1017 | if (ShouldBuildInfo) |
| 1018 | // We just need to get an array type, the exact type doesn't matter. |
| 1019 | SourceTy = Context.getIncompleteArrayType(SourceTy, ArrayType::Normal, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1020 | DeclType.Arr.TypeQuals); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1021 | |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1022 | // Verify that we're not building an array of pointers to function with |
| 1023 | // exception specification. |
| 1024 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1025 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1026 | D.setInvalidType(true); |
| 1027 | // Build the type anyway. |
| 1028 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1029 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 1030 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | ArrayType::ArraySizeModifier ASM; |
| 1032 | if (ATI.isStar) |
| 1033 | ASM = ArrayType::Star; |
| 1034 | else if (ATI.hasStatic) |
| 1035 | ASM = ArrayType::Static; |
| 1036 | else |
| 1037 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1038 | if (ASM == ArrayType::Star && |
| 1039 | D.getContext() != Declarator::PrototypeContext) { |
| 1040 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 1041 | // for function definitions, and disallows some edge cases detailed |
| 1042 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 1043 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 1044 | ASM = ArrayType::Normal; |
| 1045 | D.setInvalidType(true); |
| 1046 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1047 | T = BuildArrayType(T, ASM, ArraySize, |
| 1048 | Qualifiers::fromCVRMask(ATI.TypeQuals), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1049 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1050 | break; |
| 1051 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1052 | case DeclaratorChunk::Function: { |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1053 | if (ShouldBuildInfo) { |
| 1054 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 1055 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1057 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 1058 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
Anders Carlsson | 83913e3 | 2009-09-16 23:47:08 +0000 | [diff] [blame] | 1059 | if (Param) { |
| 1060 | QualType ArgTy = adjustFunctionParamType(Param->getType()); |
| 1061 | |
| 1062 | ArgTys.push_back(ArgTy); |
| 1063 | } |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1064 | } |
| 1065 | SourceTy = Context.getFunctionType(SourceTy, ArgTys.data(), |
| 1066 | ArgTys.size(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1067 | FTI.isVariadic, |
| 1068 | FTI.TypeQuals); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // If the function declarator has a prototype (i.e. it is not () and |
| 1072 | // does not have a K&R-style identifier list), then the arguments are part |
| 1073 | // of the type, otherwise the argument list is (). |
| 1074 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1076 | // 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] | 1077 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1078 | Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1079 | T = Context.IntTy; |
| 1080 | D.setInvalidType(true); |
| 1081 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1083 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 1084 | // C++ [dcl.fct]p6: |
| 1085 | // Types shall not be defined in return or parameter types. |
| 1086 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 1087 | if (Tag->isDefinition()) |
| 1088 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 1089 | << Context.getTypeDeclType(Tag); |
| 1090 | } |
| 1091 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1092 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 1093 | // anyway. |
| 1094 | if (FTI.hasExceptionSpec && |
| 1095 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1096 | Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); |
| 1097 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1098 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1099 | if (getLangOptions().CPlusPlus) { |
| 1100 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 1101 | // function takes no arguments. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1102 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1103 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1105 | // FIXME: Preserve type source info. |
| 1106 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1107 | // Check that the type is valid for an exception spec, and drop it |
| 1108 | // if not. |
| 1109 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1110 | Exceptions.push_back(ET); |
| 1111 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1112 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, |
| 1113 | FTI.hasExceptionSpec, |
| 1114 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1115 | Exceptions.size(), Exceptions.data()); |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1116 | } else if (FTI.isVariadic) { |
| 1117 | // We allow a zero-parameter variadic function in C if the |
| 1118 | // function is marked with the "overloadable" |
| 1119 | // attribute. Scan for this attribute now. |
| 1120 | bool Overloadable = false; |
| 1121 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1122 | Attrs; Attrs = Attrs->getNext()) { |
| 1123 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1124 | Overloadable = true; |
| 1125 | break; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | if (!Overloadable) |
| 1130 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
| 1131 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1132 | } else { |
| 1133 | // Simple void foo(), where the incoming T is the result type. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1134 | T = Context.getFunctionNoProtoType(T); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1135 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1136 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1139 | } else { |
| 1140 | // Otherwise, we have a function with an argument list that is |
| 1141 | // potentially variadic. |
| 1142 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1145 | ParmVarDecl *Param = |
| 1146 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1147 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1148 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1149 | |
| 1150 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1151 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | // Look for 'void'. void is allowed only as a single argument to a |
| 1154 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1155 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1156 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1157 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1158 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1159 | // have arguments of incomplete type. |
| 1160 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1161 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1162 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1163 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1164 | } else if (FTI.ArgInfo[i].Ident) { |
| 1165 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1166 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1167 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1168 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1169 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1172 | if (ArgTy.hasQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1173 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1175 | // Do not add 'void' to the ArgTys list. |
| 1176 | break; |
| 1177 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1178 | } else if (!FTI.hasPrototype) { |
| 1179 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 1180 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1181 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1182 | if (BTy->getKind() == BuiltinType::Float) |
| 1183 | ArgTy = Context.DoubleTy; |
| 1184 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Anders Carlsson | 83913e3 | 2009-09-16 23:47:08 +0000 | [diff] [blame] | 1187 | ArgTys.push_back(adjustFunctionParamType(ArgTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1189 | |
| 1190 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1191 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1193 | // FIXME: Preserve type source info. |
| 1194 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1195 | // Check that the type is valid for an exception spec, and drop it if |
| 1196 | // not. |
| 1197 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1198 | Exceptions.push_back(ET); |
| 1199 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1200 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1201 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1202 | FTI.isVariadic, FTI.TypeQuals, |
| 1203 | FTI.hasExceptionSpec, |
| 1204 | FTI.hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1205 | Exceptions.size(), Exceptions.data()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | } |
| 1207 | break; |
| 1208 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1209 | case DeclaratorChunk::MemberPointer: |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 1210 | // Verify that we're not building a pointer to pointer to function with |
| 1211 | // exception specification. |
| 1212 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1213 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1214 | D.setInvalidType(true); |
| 1215 | // Build the type anyway. |
| 1216 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1217 | // The scope spec must refer to a class, or be dependent. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1218 | QualType ClsType; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1219 | if (isDependentScopeSpecifier(DeclType.Mem.Scope())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | NestedNameSpecifier *NNS |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1221 | = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); |
| 1222 | assert(NNS->getAsType() && "Nested-name-specifier must name a type"); |
| 1223 | ClsType = QualType(NNS->getAsType(), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | } else if (CXXRecordDecl *RD |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1225 | = dyn_cast_or_null<CXXRecordDecl>( |
| 1226 | computeDeclContext(DeclType.Mem.Scope()))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1227 | ClsType = Context.getTagDeclType(RD); |
| 1228 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1229 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1230 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1231 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1232 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1233 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1236 | if (ShouldBuildInfo) { |
| 1237 | QualType cls = !ClsType.isNull() ? ClsType : Context.IntTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1238 | SourceTy = Context.getQualifiedType( |
| 1239 | Context.getMemberPointerType(SourceTy, cls.getTypePtr()), |
| 1240 | Qualifiers::fromCVRMask(DeclType.Mem.TypeQuals)); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1243 | if (!ClsType.isNull()) |
| 1244 | T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, |
| 1245 | DeclType.Loc, D.getIdentifier()); |
| 1246 | if (T.isNull()) { |
| 1247 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1248 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1249 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1253 | if (T.isNull()) { |
| 1254 | D.setInvalidType(true); |
| 1255 | T = Context.IntTy; |
| 1256 | } |
| 1257 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1258 | // See if there are any attributes on this declarator chunk. |
| 1259 | if (const AttributeList *AL = DeclType.getAttrs()) |
| 1260 | ProcessTypeAttributeList(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1264 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1265 | assert(FnTy && "Why oh why is there not a FunctionProtoType here ?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1266 | |
| 1267 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 1268 | // for a nonstatic member function, the function type to which a pointer |
| 1269 | // to member refers, or the top-level function type of a function typedef |
| 1270 | // declaration. |
| 1271 | if (FnTy->getTypeQuals() != 0 && |
| 1272 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1273 | ((D.getContext() != Declarator::MemberContext && |
| 1274 | (!D.getCXXScopeSpec().isSet() || |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1275 | !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true) |
| 1276 | ->isRecord())) || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1277 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1278 | if (D.isFunctionDeclarator()) |
| 1279 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 1280 | else |
| 1281 | Diag(D.getIdentifierLoc(), |
| 1282 | diag::err_invalid_qualified_typedef_function_type_use); |
| 1283 | |
| 1284 | // Strip the cv-quals from the type. |
| 1285 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1286 | FnTy->getNumArgs(), FnTy->isVariadic(), 0); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 0bf29ad | 2008-06-29 00:19:33 +0000 | [diff] [blame] | 1290 | // If there were any type attributes applied to the decl itself (not the |
| 1291 | // type, apply the type attribute to the type!) |
| 1292 | if (const AttributeList *Attrs = D.getAttributes()) |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1293 | ProcessTypeAttributeList(T, Attrs); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (ShouldBuildInfo) |
| 1296 | *DInfo = GetDeclaratorInfoForDeclarator(D, SourceTy, Skip); |
| 1297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | return T; |
| 1299 | } |
| 1300 | |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1301 | static void FillTypeSpecLoc(TypeLoc TSL, const DeclSpec &DS) { |
| 1302 | if (TSL.isNull()) return; |
| 1303 | |
| 1304 | if (TypedefLoc *TL = dyn_cast<TypedefLoc>(&TSL)) { |
| 1305 | TL->setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1306 | |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 1307 | } else if (ObjCInterfaceLoc *TL = dyn_cast<ObjCInterfaceLoc>(&TSL)) { |
| 1308 | TL->setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1309 | |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1310 | } else if (ObjCProtocolListLoc *PLL = dyn_cast<ObjCProtocolListLoc>(&TSL)) { |
| 1311 | assert(PLL->getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1312 | PLL->setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1313 | PLL->setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1314 | for (unsigned i = 0; i != DS.getNumProtocolQualifiers(); ++i) |
| 1315 | PLL->setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1316 | FillTypeSpecLoc(PLL->getBaseTypeLoc(), DS); |
| 1317 | |
| 1318 | } else { |
| 1319 | //FIXME: Other typespecs. |
| 1320 | DefaultTypeSpecLoc &DTL = cast<DefaultTypeSpecLoc>(TSL); |
| 1321 | DTL.setStartLoc(DS.getSourceRange().getBegin()); |
| 1322 | } |
| 1323 | } |
| 1324 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1325 | /// \brief Create and instantiate a DeclaratorInfo with type source information. |
| 1326 | /// |
| 1327 | /// \param T QualType referring to the type as written in source code. |
| 1328 | DeclaratorInfo * |
| 1329 | Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) { |
| 1330 | DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T); |
| 1331 | TypeLoc CurrTL = DInfo->getTypeLoc(); |
| 1332 | |
| 1333 | for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { |
| 1334 | assert(!CurrTL.isNull()); |
John McCall | 34a0447 | 2009-10-15 03:50:32 +0000 | [diff] [blame] | 1335 | |
| 1336 | // Don't bother recording source locations for qualifiers. |
| 1337 | CurrTL = CurrTL.getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1338 | |
| 1339 | DeclaratorChunk &DeclType = D.getTypeObject(i); |
| 1340 | switch (DeclType.Kind) { |
| 1341 | default: assert(0 && "Unknown decltype!"); |
| 1342 | case DeclaratorChunk::BlockPointer: { |
| 1343 | BlockPointerLoc &BPL = cast<BlockPointerLoc>(CurrTL); |
| 1344 | BPL.setCaretLoc(DeclType.Loc); |
| 1345 | break; |
| 1346 | } |
| 1347 | case DeclaratorChunk::Pointer: { |
| 1348 | //FIXME: ObjCObject pointers. |
| 1349 | PointerLoc &PL = cast<PointerLoc>(CurrTL); |
| 1350 | PL.setStarLoc(DeclType.Loc); |
| 1351 | break; |
| 1352 | } |
| 1353 | case DeclaratorChunk::Reference: { |
| 1354 | ReferenceLoc &RL = cast<ReferenceLoc>(CurrTL); |
| 1355 | RL.setAmpLoc(DeclType.Loc); |
| 1356 | break; |
| 1357 | } |
| 1358 | case DeclaratorChunk::Array: { |
| 1359 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
| 1360 | ArrayLoc &AL = cast<ArrayLoc>(CurrTL); |
| 1361 | AL.setLBracketLoc(DeclType.Loc); |
| 1362 | AL.setRBracketLoc(DeclType.EndLoc); |
| 1363 | AL.setSizeExpr(static_cast<Expr*>(ATI.NumElts)); |
| 1364 | //FIXME: Star location for [*]. |
| 1365 | break; |
| 1366 | } |
| 1367 | case DeclaratorChunk::Function: { |
| 1368 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 1369 | FunctionLoc &FL = cast<FunctionLoc>(CurrTL); |
| 1370 | FL.setLParenLoc(DeclType.Loc); |
| 1371 | FL.setRParenLoc(DeclType.EndLoc); |
| 1372 | for (unsigned i = 0, e = FTI.NumArgs, tpi = 0; i != e; ++i) { |
| 1373 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
| 1374 | if (Param) { |
| 1375 | assert(tpi < FL.getNumArgs()); |
| 1376 | FL.setArg(tpi++, Param); |
| 1377 | } |
| 1378 | } |
| 1379 | break; |
| 1380 | //FIXME: Exception specs. |
| 1381 | } |
| 1382 | case DeclaratorChunk::MemberPointer: { |
| 1383 | MemberPointerLoc &MPL = cast<MemberPointerLoc>(CurrTL); |
| 1384 | MPL.setStarLoc(DeclType.Loc); |
| 1385 | //FIXME: Class location. |
| 1386 | break; |
| 1387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | CurrTL = CurrTL.getNextTypeLoc(); |
| 1392 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1393 | |
| 1394 | FillTypeSpecLoc(CurrTL, D.getDeclSpec()); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1395 | |
| 1396 | return DInfo; |
| 1397 | } |
| 1398 | |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1399 | /// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo. |
| 1400 | QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) { |
| 1401 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 1402 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 1403 | // it's appropriate, instead of allocating them and keeping them around. |
| 1404 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); |
| 1405 | new (LocT) LocInfoType(T, DInfo); |
| 1406 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 1407 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
| 1408 | return QualType(LocT, 0); |
| 1409 | } |
| 1410 | |
| 1411 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 1412 | const PrintingPolicy &Policy) const { |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 1413 | assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" |
| 1414 | " was used directly instead of getting the QualType through" |
| 1415 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1418 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 1419 | /// declarator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1420 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { |
| 1421 | ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1422 | QualType T = MDecl->getResultType(); |
| 1423 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 1425 | // Add the first two invisible argument types for self and _cmd. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1426 | if (MDecl->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1427 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 1428 | selfTy = Context.getPointerType(selfTy); |
| 1429 | ArgTys.push_back(selfTy); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1430 | } else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1431 | ArgTys.push_back(Context.getObjCIdType()); |
| 1432 | ArgTys.push_back(Context.getObjCSelType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1434 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 1435 | E = MDecl->param_end(); PI != E; ++PI) { |
| 1436 | QualType ArgTy = (*PI)->getType(); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1437 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1438 | ArgTy = adjustParameterType(ArgTy); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1439 | ArgTys.push_back(ArgTy); |
| 1440 | } |
| 1441 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1442 | MDecl->isVariadic(), 0); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 1443 | return T; |
| 1444 | } |
| 1445 | |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 1446 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 1447 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 1448 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 1449 | /// or pointer-to-member types, or if they are not similar at this |
| 1450 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 1451 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 1452 | /// be called in a loop that successively "unwraps" pointer and |
| 1453 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | ecb81f2 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 1454 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1455 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 1456 | *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1457 | if (T1PtrType && T2PtrType) { |
| 1458 | T1 = T1PtrType->getPointeeType(); |
| 1459 | T2 = T2PtrType->getPointeeType(); |
| 1460 | return true; |
| 1461 | } |
| 1462 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1463 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 1464 | *T2MPType = T2->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1465 | if (T1MPType && T2MPType && |
| 1466 | Context.getCanonicalType(T1MPType->getClass()) == |
| 1467 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1468 | T1 = T1MPType->getPointeeType(); |
| 1469 | T2 = T2MPType->getPointeeType(); |
| 1470 | return true; |
| 1471 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
| 1474 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1475 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1476 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 1477 | // the parser. |
| 1478 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1480 | DeclaratorInfo *DInfo = 0; |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1481 | TagDecl *OwnedTag = 0; |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1482 | QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1483 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1484 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1486 | if (getLangOptions().CPlusPlus) { |
| 1487 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1488 | CheckExtraCXXDefaultArguments(D); |
| 1489 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1490 | // C++0x [dcl.type]p3: |
| 1491 | // A type-specifier-seq shall not define a class or enumeration |
| 1492 | // unless it appears in the type-id of an alias-declaration |
| 1493 | // (7.1.3). |
| 1494 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1495 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1496 | << Context.getTypeDeclType(OwnedTag); |
| 1497 | } |
| 1498 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1499 | if (DInfo) |
| 1500 | T = CreateLocInfoType(T, DInfo); |
| 1501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | return T.getAsOpaquePtr(); |
| 1503 | } |
| 1504 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1505 | |
| 1506 | |
| 1507 | //===----------------------------------------------------------------------===// |
| 1508 | // Type Attribute Processing |
| 1509 | //===----------------------------------------------------------------------===// |
| 1510 | |
| 1511 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1512 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1513 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1515 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1517 | // If this type is already address space qualified, reject it. |
| 1518 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1519 | // for two or more different address spaces." |
| 1520 | if (Type.getAddressSpace()) { |
| 1521 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 1522 | return; |
| 1523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1525 | // Check the attribute arguments. |
| 1526 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1527 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1528 | return; |
| 1529 | } |
| 1530 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1531 | llvm::APSInt addrSpace(32); |
| 1532 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1533 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1534 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1535 | return; |
| 1536 | } |
| 1537 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1538 | // Bounds checking. |
| 1539 | if (addrSpace.isSigned()) { |
| 1540 | if (addrSpace.isNegative()) { |
| 1541 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 1542 | << ASArgExpr->getSourceRange(); |
| 1543 | return; |
| 1544 | } |
| 1545 | addrSpace.setIsSigned(false); |
| 1546 | } |
| 1547 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1548 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1549 | if (addrSpace > max) { |
| 1550 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1551 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1556 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1559 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1560 | /// specified type. The attribute contains 1 argument, weak or strong. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1562 | const AttributeList &Attr, Sema &S) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1563 | if (Type.getObjCGCAttr() != Qualifiers::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1564 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1568 | // Check the attribute arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | if (!Attr.getParameterName()) { |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1570 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1571 | << "objc_gc" << 1; |
| 1572 | return; |
| 1573 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1574 | Qualifiers::GC GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1575 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1576 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1577 | return; |
| 1578 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | if (Attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1580 | GCAttr = Qualifiers::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1581 | else if (Attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1582 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1583 | else { |
| 1584 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1585 | << "objc_gc" << Attr.getParameterName(); |
| 1586 | return; |
| 1587 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1589 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1592 | /// HandleNoReturnTypeAttribute - Process the noreturn attribute on the |
| 1593 | /// specified type. The attribute contains 0 arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | static void HandleNoReturnTypeAttribute(QualType &Type, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1595 | const AttributeList &Attr, Sema &S) { |
| 1596 | if (Attr.getNumArgs() != 0) |
| 1597 | return; |
| 1598 | |
| 1599 | // We only apply this to a pointer to function or a pointer to block. |
| 1600 | if (!Type->isFunctionPointerType() |
| 1601 | && !Type->isBlockPointerType() |
| 1602 | && !Type->isFunctionType()) |
| 1603 | return; |
| 1604 | |
| 1605 | Type = S.Context.getNoReturnType(Type); |
| 1606 | } |
| 1607 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1608 | void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1609 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1610 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1611 | // 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] | 1612 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1613 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1614 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 1615 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1616 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1617 | default: break; |
| 1618 | case AttributeList::AT_address_space: |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1619 | HandleAddressSpaceTypeAttribute(Result, *AL, *this); |
| 1620 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1621 | case AttributeList::AT_objc_gc: |
| 1622 | HandleObjCGCTypeAttribute(Result, *AL, *this); |
| 1623 | break; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1624 | case AttributeList::AT_noreturn: |
| 1625 | HandleNoReturnTypeAttribute(Result, *AL, *this); |
| 1626 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1627 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1628 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1632 | /// |
| 1633 | /// This routine checks whether the type @p T is complete in any |
| 1634 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1635 | /// type, returns false. If @p T is a class template specialization, |
| 1636 | /// this routine then attempts to perform class template |
| 1637 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 1638 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 1639 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1640 | /// |
| 1641 | /// @param Loc The location in the source that the incomplete type |
| 1642 | /// diagnostic should refer to. |
| 1643 | /// |
| 1644 | /// @param T The type that this routine is examining for completeness. |
| 1645 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | /// @param PD The partial diagnostic that will be printed out if T is not a |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1647 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1648 | /// |
| 1649 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 1650 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1651 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1652 | const PartialDiagnostic &PD, |
| 1653 | std::pair<SourceLocation, |
| 1654 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1655 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1657 | // FIXME: Add this assertion to help us flush out problems with |
| 1658 | // checking for dependent types and type-dependent expressions. |
| 1659 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1661 | // "Can't ask whether a dependent type is complete"); |
| 1662 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1663 | // If we have a complete type, we're done. |
| 1664 | if (!T->isIncompleteType()) |
| 1665 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1667 | // If we have a class template specialization or a class member of a |
| 1668 | // class template specialization, try to instantiate it. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1669 | if (const RecordType *Record = T->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1670 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1671 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1672 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1673 | if (Loc.isValid()) |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1674 | ClassTemplateSpec->setPointOfInstantiation(Loc); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1675 | return InstantiateClassTemplateSpecialization(ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1676 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1677 | /*Complain=*/diag != 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1678 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1680 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 1681 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1682 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 1683 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1684 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1685 | if (MSInfo->getTemplateSpecializationKind() |
| 1686 | != TSK_ExplicitSpecialization) { |
| 1687 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1688 | return InstantiateClass(Loc, Rec, Pattern, |
| 1689 | getTemplateInstantiationArgs(Rec), |
| 1690 | TSK_ImplicitInstantiation, |
| 1691 | /*Complain=*/diag != 0); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1692 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1697 | if (diag == 0) |
| 1698 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1700 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1701 | Diag(Loc, PD) << T; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1702 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1703 | // If we have a note, produce it. |
| 1704 | if (!Note.first.isInvalid()) |
| 1705 | Diag(Note.first, Note.second); |
| 1706 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1707 | // If the type was a forward declaration of a class/struct/union |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | // type, produce |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1709 | const TagType *Tag = 0; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1710 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1711 | Tag = Record; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1712 | else if (const EnumType *Enum = T->getAs<EnumType>()) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1713 | Tag = Enum; |
| 1714 | |
| 1715 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1717 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 1718 | : diag::note_forward_declaration) |
| 1719 | << QualType(Tag, 0); |
| 1720 | |
| 1721 | return true; |
| 1722 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1723 | |
| 1724 | /// \brief Retrieve a version of the type 'T' that is qualified by the |
| 1725 | /// nested-name-specifier contained in SS. |
| 1726 | QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { |
| 1727 | if (!SS.isSet() || SS.isInvalid() || T.isNull()) |
| 1728 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1730 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 1731 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1732 | return Context.getQualifiedNameType(NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1733 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1734 | |
| 1735 | QualType Sema::BuildTypeofExprType(Expr *E) { |
| 1736 | return Context.getTypeOfExprType(E); |
| 1737 | } |
| 1738 | |
| 1739 | QualType Sema::BuildDecltypeType(Expr *E) { |
| 1740 | if (E->getType() == Context.OverloadTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | Diag(E->getLocStart(), |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1742 | diag::err_cannot_determine_declared_type_of_overloaded_function); |
| 1743 | return QualType(); |
| 1744 | } |
| 1745 | return Context.getDecltypeType(E); |
| 1746 | } |