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