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