Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 13 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Designator.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | |
| 53 | switch (SL->getKind()) { |
| 54 | case StringLiteral::Ascii: |
| 55 | case StringLiteral::UTF8: |
| 56 | // char array can be initialized with a narrow string. |
| 57 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 59 | case StringLiteral::UTF16: |
| 60 | return ElemTy->isChar16Type() ? Init : 0; |
| 61 | case StringLiteral::UTF32: |
| 62 | return ElemTy->isChar32Type() ? Init : 0; |
| 63 | case StringLiteral::Wide: |
| 64 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 65 | // correction from DR343): "An array with element type compatible with a |
| 66 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 67 | // string literal, optionally enclosed in braces." |
| 68 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 69 | ElemTy.getUnqualifiedType())) |
| 70 | return Init; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 78 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 79 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 80 | if (!arrayType) return 0; |
| 81 | |
| 82 | return IsStringInit(init, arrayType, Context); |
| 83 | } |
| 84 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 85 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 86 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | // Get the length of the string as parsed. |
| 88 | uint64_t StrLength = |
| 89 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 90 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | // being initialized to a string literal. |
| 95 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 96 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 97 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 98 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 99 | ConstVal, |
| 100 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 101 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 104 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 107 | // the size may be smaller or larger than the string we are initializing. |
| 108 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 109 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 111 | // For Pascal strings it's OK to strip off the terminating null character, |
| 112 | // so the example below is valid: |
| 113 | // |
| 114 | // unsigned char a[2] = "\pa"; |
| 115 | if (SL->isPascal()) |
| 116 | StrLength--; |
| 117 | } |
| 118 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 119 | // [dcl.init.string]p2 |
| 120 | if (StrLength > CAT->getSize().getZExtValue()) |
| 121 | S.Diag(Str->getSourceRange().getBegin(), |
| 122 | diag::err_initializer_string_for_char_array_too_long) |
| 123 | << Str->getSourceRange(); |
| 124 | } else { |
| 125 | // C99 6.7.8p14. |
| 126 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 127 | S.Diag(Str->getSourceRange().getBegin(), |
| 128 | diag::warn_initializer_string_for_char_array_too_long) |
| 129 | << Str->getSourceRange(); |
| 130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 132 | // Set the type to the actual size that we are initializing. If we have |
| 133 | // something like: |
| 134 | // char x[1] = "foo"; |
| 135 | // then this will set the string literal's type to char[1]. |
| 136 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Semantic checking for initializer lists. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 143 | /// @brief Semantic checking for initializer lists. |
| 144 | /// |
| 145 | /// The InitListChecker class contains a set of routines that each |
| 146 | /// handle the initialization of a certain kind of entity, e.g., |
| 147 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 148 | /// InitListChecker itself performs a recursive walk of the subobject |
| 149 | /// structure of the type to be initialized, while stepping through |
| 150 | /// the initializer list one element at a time. The IList and Index |
| 151 | /// parameters to each of the Check* routines contain the active |
| 152 | /// (syntactic) initializer list and the index into that initializer |
| 153 | /// list that represents the current initializer. Each routine is |
| 154 | /// responsible for moving that Index forward as it consumes elements. |
| 155 | /// |
| 156 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 157 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 158 | /// initializer list and the index into that initializer list where we |
| 159 | /// are copying initializers as we map them over to the semantic |
| 160 | /// list. Once we have completed our recursive walk of the subobject |
| 161 | /// structure, we will have constructed a full semantic initializer |
| 162 | /// list. |
| 163 | /// |
| 164 | /// C99 designators cause changes in the initializer list traversal, |
| 165 | /// because they make the initialization "jump" into a specific |
| 166 | /// subobject and then continue the initialization from that |
| 167 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 168 | /// designated subobject and manages backing out the recursion to |
| 169 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 170 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 172 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | bool hadError; |
| 174 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 175 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 177 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 178 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 179 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 180 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 181 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 182 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 184 | unsigned &StructuredIndex, |
| 185 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 191 | unsigned &StructuredIndex, |
| 192 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 194 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 195 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 198 | void CheckComplexType(const InitializedEntity &Entity, |
| 199 | InitListExpr *IList, QualType DeclType, |
| 200 | unsigned &Index, |
| 201 | InitListExpr *StructuredList, |
| 202 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 203 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 204 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 205 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | InitListExpr *StructuredList, |
| 207 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 208 | void CheckReferenceType(const InitializedEntity &Entity, |
| 209 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 210 | unsigned &Index, |
| 211 | InitListExpr *StructuredList, |
| 212 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 213 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 214 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 215 | InitListExpr *StructuredList, |
| 216 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 217 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 218 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 220 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 221 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 222 | unsigned &StructuredIndex, |
| 223 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 224 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 225 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 227 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 228 | InitListExpr *StructuredList, |
| 229 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 230 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 231 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 232 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 234 | RecordDecl::field_iterator *NextField, |
| 235 | llvm::APSInt *NextElementIndex, |
| 236 | unsigned &Index, |
| 237 | InitListExpr *StructuredList, |
| 238 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 239 | bool FinishSubobjectInit, |
| 240 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 241 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 242 | QualType CurrentObjectType, |
| 243 | InitListExpr *StructuredList, |
| 244 | unsigned StructuredIndex, |
| 245 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 246 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 247 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 248 | Expr *expr); |
| 249 | int numArrayElements(QualType DeclType); |
| 250 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 252 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 253 | const InitializedEntity &ParentEntity, |
| 254 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 255 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 256 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 257 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 258 | Expr *InitExpr, FieldDecl *Field, |
| 259 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 260 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 261 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 262 | InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 263 | bool HadError() { return hadError; } |
| 264 | |
| 265 | // @brief Retrieves the fully-structured initializer list used for |
| 266 | // semantic analysis and code generation. |
| 267 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 268 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 269 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 270 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 271 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 272 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 273 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 274 | bool &RequiresSecondPass) { |
| 275 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 276 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 277 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 278 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 279 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 280 | // FIXME: We probably don't need to handle references |
| 281 | // specially here, since value-initialization of references is |
| 282 | // handled in InitializationSequence. |
| 283 | if (Field->getType()->isReferenceType()) { |
| 284 | // C++ [dcl.init.aggr]p9: |
| 285 | // If an incomplete or empty initializer-list leaves a |
| 286 | // member of reference type uninitialized, the program is |
| 287 | // ill-formed. |
| 288 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 289 | << Field->getType() |
| 290 | << ILE->getSyntacticForm()->getSourceRange(); |
| 291 | SemaRef.Diag(Field->getLocation(), |
| 292 | diag::note_uninit_reference_member); |
| 293 | hadError = true; |
| 294 | return; |
| 295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 297 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 298 | true); |
| 299 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 300 | if (!InitSeq) { |
| 301 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 302 | hadError = true; |
| 303 | return; |
| 304 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 305 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 306 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 307 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 308 | if (MemberInit.isInvalid()) { |
| 309 | hadError = true; |
| 310 | return; |
| 311 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 313 | if (hadError) { |
| 314 | // Do nothing |
| 315 | } else if (Init < NumInits) { |
| 316 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 317 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 318 | // Value-initialization requires a constructor call, so |
| 319 | // extend the initializer list to include the constructor |
| 320 | // call and make a note that we'll need to take another pass |
| 321 | // through the initializer list. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 322 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 323 | RequiresSecondPass = true; |
| 324 | } |
| 325 | } else if (InitListExpr *InnerILE |
| 326 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 327 | FillInValueInitializations(MemberEntity, InnerILE, |
| 328 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 331 | /// Recursively replaces NULL values within the given initializer list |
| 332 | /// with expressions that perform value-initialization of the |
| 333 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 334 | void |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 335 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 336 | InitListExpr *ILE, |
| 337 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 339 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 340 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 341 | if (ILE->getSyntacticForm()) |
| 342 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 344 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 345 | if (RType->getDecl()->isUnion() && |
| 346 | ILE->getInitializedFieldInUnion()) |
| 347 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 348 | Entity, ILE, RequiresSecondPass); |
| 349 | else { |
| 350 | unsigned Init = 0; |
| 351 | for (RecordDecl::field_iterator |
| 352 | Field = RType->getDecl()->field_begin(), |
| 353 | FieldEnd = RType->getDecl()->field_end(); |
| 354 | Field != FieldEnd; ++Field) { |
| 355 | if (Field->isUnnamedBitfield()) |
| 356 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 358 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 359 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 360 | |
| 361 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 362 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 363 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 365 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 367 | // Only look at the first initialization of a union. |
| 368 | if (RType->getDecl()->isUnion()) |
| 369 | break; |
| 370 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 375 | |
| 376 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 378 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 379 | unsigned NumInits = ILE->getNumInits(); |
| 380 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 381 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 382 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 383 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 384 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 386 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 387 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 388 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 389 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 391 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 393 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 396 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 397 | if (hadError) |
| 398 | return; |
| 399 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 400 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 401 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 402 | ElementEntity.setElementIndex(Init); |
| 403 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 404 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 405 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 406 | true); |
| 407 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 408 | if (!InitSeq) { |
| 409 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 410 | hadError = true; |
| 411 | return; |
| 412 | } |
| 413 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 414 | ExprResult ElementInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 415 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 416 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 417 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (hadError) { |
| 422 | // Do nothing |
| 423 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 424 | // For arrays, just set the expression used for value-initialization |
| 425 | // of the "holes" in the array. |
| 426 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 427 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 428 | else |
| 429 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 430 | } else { |
| 431 | // For arrays, just set the expression used for value-initialization |
| 432 | // of the rest of elements and exit. |
| 433 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 434 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 435 | return; |
| 436 | } |
| 437 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 438 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 439 | // Value-initialization requires a constructor call, so |
| 440 | // extend the initializer list to include the constructor |
| 441 | // call and make a note that we'll need to take another pass |
| 442 | // through the initializer list. |
| 443 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 444 | RequiresSecondPass = true; |
| 445 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 446 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 447 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 448 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 449 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 453 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 454 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 455 | InitListExpr *IL, QualType &T) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 456 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 457 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 458 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 459 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 460 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 462 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 464 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 465 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 467 | if (!hadError) { |
| 468 | bool RequiresSecondPass = false; |
| 469 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 470 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 472 | RequiresSecondPass); |
| 473 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 477 | // FIXME: use a proper constant |
| 478 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 479 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 480 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 481 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 482 | } |
| 483 | return maxElements; |
| 484 | } |
| 485 | |
| 486 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 487 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 488 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 490 | Field = structDecl->field_begin(), |
| 491 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | Field != FieldEnd; ++Field) { |
| 493 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 494 | ++InitializableMembers; |
| 495 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 496 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 497 | return std::min(InitializableMembers, 1); |
| 498 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 501 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 502 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 503 | QualType T, unsigned &Index, |
| 504 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 505 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 506 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 508 | if (T->isArrayType()) |
| 509 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 510 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 511 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 512 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 513 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 514 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 515 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 516 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 517 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 518 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 519 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 520 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 521 | hadError = true; |
| 522 | return; |
| 523 | } |
| 524 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 525 | // Build a structured initializer list corresponding to this subobject. |
| 526 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 528 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 529 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 530 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 531 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 533 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 534 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 535 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 536 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 538 | StructuredSubobjectInitIndex); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 539 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 540 | StructuredSubobjectInitList->setType(T); |
| 541 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 542 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 543 | // range corresponds with the end of the last initializer it used. |
| 544 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 546 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 547 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 548 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 549 | |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 550 | // Warn about missing braces. |
| 551 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 5cbff48 | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 552 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 553 | diag::warn_missing_braces) |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 554 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 556 | "{") |
| 557 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 559 | "}"); |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 560 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 563 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 564 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 565 | unsigned &Index, |
| 566 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 567 | unsigned &StructuredIndex, |
| 568 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 569 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 570 | SyntacticToSemantic[IList] = StructuredList; |
| 571 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 572 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 573 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 574 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 575 | IList->setType(ExprTy); |
| 576 | StructuredList->setType(ExprTy); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 577 | if (hadError) |
| 578 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 579 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 580 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 581 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 582 | if (StructuredIndex == 1 && |
| 583 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 584 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 585 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 586 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 587 | hadError = true; |
| 588 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 589 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 590 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 591 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 592 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 593 | // Don't complain for incomplete types, since we'll get an error |
| 594 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 595 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 597 | CurrentObjectType->isArrayType()? 0 : |
| 598 | CurrentObjectType->isVectorType()? 1 : |
| 599 | CurrentObjectType->isScalarType()? 2 : |
| 600 | CurrentObjectType->isUnionType()? 3 : |
| 601 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 602 | |
| 603 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 604 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 605 | DK = diag::err_excess_initializers; |
| 606 | hadError = true; |
| 607 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 608 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 609 | DK = diag::err_excess_initializers; |
| 610 | hadError = true; |
| 611 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 612 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 613 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 614 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 617 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 618 | if (T->isScalarType() && IList->getNumInits() == 1 && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 619 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 620 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 621 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 622 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 625 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 626 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 628 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 629 | unsigned &Index, |
| 630 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 631 | unsigned &StructuredIndex, |
| 632 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 633 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 634 | // Explicitly braced initializer for complex type can be real+imaginary |
| 635 | // parts. |
| 636 | CheckComplexType(Entity, IList, DeclType, Index, |
| 637 | StructuredList, StructuredIndex); |
| 638 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 639 | CheckScalarType(Entity, IList, DeclType, Index, |
| 640 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 641 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 643 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 644 | } else if (DeclType->isAggregateType()) { |
| 645 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 646 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 647 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 648 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 649 | StructuredList, StructuredIndex, |
| 650 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 651 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 652 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 653 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 654 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 655 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 656 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 657 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 658 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame^] | 659 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 660 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 661 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 662 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 663 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 664 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 665 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 666 | } else if (DeclType->isRecordType()) { |
| 667 | // C++ [dcl.init]p14: |
| 668 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 669 | // is a brace-enclosed list, see 8.5.1. |
| 670 | // |
| 671 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 672 | // we have an initializer list and a destination type that is not |
| 673 | // an aggregate. |
| 674 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 675 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 676 | << DeclType << IList->getSourceRange(); |
| 677 | hadError = true; |
| 678 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 679 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 680 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 681 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 682 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 683 | << DeclType; |
| 684 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 685 | } else { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 686 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 687 | << DeclType; |
| 688 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 692 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 693 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 695 | unsigned &Index, |
| 696 | InitListExpr *StructuredList, |
| 697 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 698 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 699 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 700 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 701 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 703 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 704 | StructuredList, StructuredIndex, |
| 705 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 706 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 707 | newStructuredList, newStructuredIndex); |
| 708 | ++StructuredIndex; |
| 709 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 710 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 711 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 712 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 713 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 714 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 715 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 716 | StructuredList, StructuredIndex); |
| 717 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 718 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 719 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 720 | // arrayType can be incomplete if we're initializing a flexible |
| 721 | // array member. There's nothing we can do with the completed |
| 722 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 724 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 725 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 726 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 727 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 728 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 729 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 730 | |
| 731 | // Fall through for subaggregate initialization. |
| 732 | |
| 733 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 734 | // C++ [dcl.init.aggr]p12: |
| 735 | // All implicit type conversions (clause 4) are considered when |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 736 | // initializing the aggregate member with an ini- tializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 737 | // an initializer-list. If the initializer can initialize a |
| 738 | // member, the member is initialized. [...] |
| 739 | |
| 740 | // FIXME: Better EqualLoc? |
| 741 | InitializationKind Kind = |
| 742 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 743 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 744 | |
| 745 | if (Seq) { |
| 746 | ExprResult Result = |
| 747 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 748 | if (Result.isInvalid()) |
| 749 | hadError = true; |
| 750 | |
| 751 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 752 | Result.takeAs<Expr>()); |
| 753 | ++Index; |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | // Fall through for subaggregate initialization |
| 758 | } else { |
| 759 | // C99 6.7.8p13: |
| 760 | // |
| 761 | // The initializer for a structure or union object that has |
| 762 | // automatic storage duration shall be either an initializer |
| 763 | // list as described below, or a single expression that has |
| 764 | // compatible structure or union type. In the latter case, the |
| 765 | // initial value of the object, including unnamed members, is |
| 766 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 767 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 768 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 769 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 770 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 771 | if (ExprRes.isInvalid()) |
| 772 | hadError = true; |
| 773 | else { |
| 774 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 775 | if (ExprRes.isInvalid()) |
| 776 | hadError = true; |
| 777 | } |
| 778 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 779 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 780 | ++Index; |
| 781 | return; |
| 782 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 783 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 784 | // Fall through for subaggregate initialization |
| 785 | } |
| 786 | |
| 787 | // C++ [dcl.init.aggr]p12: |
| 788 | // |
| 789 | // [...] Otherwise, if the member is itself a non-empty |
| 790 | // subaggregate, brace elision is assumed and the initializer is |
| 791 | // considered for the initialization of the first member of |
| 792 | // the subaggregate. |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 793 | if (!SemaRef.getLangOptions().OpenCL && |
| 794 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 795 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 796 | StructuredIndex); |
| 797 | ++StructuredIndex; |
| 798 | } else { |
| 799 | // We cannot initialize this element, so let |
| 800 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 801 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 802 | SemaRef.Owned(expr), |
| 803 | /*TopLevelOfInitList=*/true); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 804 | hadError = true; |
| 805 | ++Index; |
| 806 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 807 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 810 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 811 | InitListExpr *IList, QualType DeclType, |
| 812 | unsigned &Index, |
| 813 | InitListExpr *StructuredList, |
| 814 | unsigned &StructuredIndex) { |
| 815 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 816 | |
| 817 | // As an extension, clang supports complex initializers, which initialize |
| 818 | // a complex number component-wise. When an explicit initializer list for |
| 819 | // a complex number contains two two initializers, this extension kicks in: |
| 820 | // it exepcts the initializer list to contain two elements convertible to |
| 821 | // the element type of the complex type. The first element initializes |
| 822 | // the real part, and the second element intitializes the imaginary part. |
| 823 | |
| 824 | if (IList->getNumInits() != 2) |
| 825 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 826 | StructuredIndex); |
| 827 | |
| 828 | // This is an extension in C. (The builtin _Complex type does not exist |
| 829 | // in the C++ standard.) |
| 830 | if (!SemaRef.getLangOptions().CPlusPlus) |
| 831 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 832 | << IList->getSourceRange(); |
| 833 | |
| 834 | // Initialize the complex number. |
| 835 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 836 | InitializedEntity ElementEntity = |
| 837 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 838 | |
| 839 | for (unsigned i = 0; i < 2; ++i) { |
| 840 | ElementEntity.setElementIndex(Index); |
| 841 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 842 | StructuredList, StructuredIndex); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 847 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 848 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 849 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 850 | InitListExpr *StructuredList, |
| 851 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 852 | if (Index >= IList->getNumInits()) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 853 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 854 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 855 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 856 | ++Index; |
| 857 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 858 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 859 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 860 | |
| 861 | Expr *expr = IList->getInit(Index); |
| 862 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 863 | SemaRef.Diag(SubIList->getLocStart(), |
| 864 | diag::warn_many_braces_around_scalar_init) |
| 865 | << SubIList->getSourceRange(); |
| 866 | |
| 867 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 868 | StructuredIndex); |
| 869 | return; |
| 870 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 871 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 872 | diag::err_designator_for_scalar_init) |
| 873 | << DeclType << expr->getSourceRange(); |
| 874 | hadError = true; |
| 875 | ++Index; |
| 876 | ++StructuredIndex; |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | ExprResult Result = |
| 881 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 882 | SemaRef.Owned(expr), |
| 883 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 884 | |
| 885 | Expr *ResultExpr = 0; |
| 886 | |
| 887 | if (Result.isInvalid()) |
| 888 | hadError = true; // types weren't compatible. |
| 889 | else { |
| 890 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 891 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 892 | if (ResultExpr != expr) { |
| 893 | // The type was promoted, update initializer list. |
| 894 | IList->setInit(Index, ResultExpr); |
| 895 | } |
| 896 | } |
| 897 | if (hadError) |
| 898 | ++StructuredIndex; |
| 899 | else |
| 900 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 901 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 904 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 905 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 906 | unsigned &Index, |
| 907 | InitListExpr *StructuredList, |
| 908 | unsigned &StructuredIndex) { |
| 909 | if (Index < IList->getNumInits()) { |
| 910 | Expr *expr = IList->getInit(Index); |
| 911 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 912 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 913 | << DeclType << IList->getSourceRange(); |
| 914 | hadError = true; |
| 915 | ++Index; |
| 916 | ++StructuredIndex; |
| 917 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 919 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 920 | ExprResult Result = |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 921 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 922 | SemaRef.Owned(expr), |
| 923 | /*TopLevelOfInitList=*/true); |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 924 | |
| 925 | if (Result.isInvalid()) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 926 | hadError = true; |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 927 | |
| 928 | expr = Result.takeAs<Expr>(); |
| 929 | IList->setInit(Index, expr); |
| 930 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 931 | if (hadError) |
| 932 | ++StructuredIndex; |
| 933 | else |
| 934 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 935 | ++Index; |
| 936 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 937 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 938 | // general, it would be useful to pass location information down the stack, |
| 939 | // so that we know the location (or decl) of the "current object" being |
| 940 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 942 | diag::err_init_reference_member_uninitialized) |
| 943 | << DeclType |
| 944 | << IList->getSourceRange(); |
| 945 | hadError = true; |
| 946 | ++Index; |
| 947 | ++StructuredIndex; |
| 948 | return; |
| 949 | } |
| 950 | } |
| 951 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 952 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 953 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 954 | unsigned &Index, |
| 955 | InitListExpr *StructuredList, |
| 956 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 957 | if (Index >= IList->getNumInits()) |
| 958 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 960 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 961 | unsigned maxElements = VT->getNumElements(); |
| 962 | unsigned numEltsInit = 0; |
| 963 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 964 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 965 | if (!SemaRef.getLangOptions().OpenCL) { |
| 966 | // If the initializing element is a vector, try to copy-initialize |
| 967 | // instead of breaking it apart (which is doomed to failure anyway). |
| 968 | Expr *Init = IList->getInit(Index); |
| 969 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 970 | ExprResult Result = |
| 971 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 972 | SemaRef.Owned(Init), |
| 973 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 974 | |
| 975 | Expr *ResultExpr = 0; |
| 976 | if (Result.isInvalid()) |
| 977 | hadError = true; // types weren't compatible. |
| 978 | else { |
| 979 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 980 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 981 | if (ResultExpr != Init) { |
| 982 | // The type was promoted, update initializer list. |
| 983 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 986 | if (hadError) |
| 987 | ++StructuredIndex; |
| 988 | else |
| 989 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 990 | ++Index; |
| 991 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 992 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 994 | InitializedEntity ElementEntity = |
| 995 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 997 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 998 | // Don't attempt to go past the end of the init list |
| 999 | if (Index >= IList->getNumInits()) |
| 1000 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1001 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1002 | ElementEntity.setElementIndex(Index); |
| 1003 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1004 | StructuredList, StructuredIndex); |
| 1005 | } |
| 1006 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1007 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1008 | |
| 1009 | InitializedEntity ElementEntity = |
| 1010 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1012 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1013 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1014 | // Don't attempt to go past the end of the init list |
| 1015 | if (Index >= IList->getNumInits()) |
| 1016 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1017 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1018 | ElementEntity.setElementIndex(Index); |
| 1019 | |
| 1020 | QualType IType = IList->getInit(Index)->getType(); |
| 1021 | if (!IType->isVectorType()) { |
| 1022 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1023 | StructuredList, StructuredIndex); |
| 1024 | ++numEltsInit; |
| 1025 | } else { |
| 1026 | QualType VecType; |
| 1027 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1028 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1030 | if (IType->isExtVectorType()) |
| 1031 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1032 | else |
| 1033 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1034 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1035 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1036 | StructuredList, StructuredIndex); |
| 1037 | numEltsInit += numIElts; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // OpenCL requires all elements to be initialized. |
| 1042 | if (numEltsInit != maxElements) |
| 1043 | if (SemaRef.getLangOptions().OpenCL) |
| 1044 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1045 | diag::err_vector_incorrect_num_initializers) |
| 1046 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1049 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1050 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1051 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1053 | unsigned &Index, |
| 1054 | InitListExpr *StructuredList, |
| 1055 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1056 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1057 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1058 | // Check for the special-case of initializing an array with a string. |
| 1059 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1060 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1061 | SemaRef.Context)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1062 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1063 | // We place the string literal directly into the resulting |
| 1064 | // initializer list. This is the only place where the structure |
| 1065 | // of the structured initializer list doesn't match exactly, |
| 1066 | // because doing so would involve allocating one character |
| 1067 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1068 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1069 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1070 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1074 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1075 | // Check for VLAs; in standard C it would be possible to check this |
| 1076 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1077 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1078 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1079 | diag::err_variable_object_no_init) |
| 1080 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1081 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1082 | ++Index; |
| 1083 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1087 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1088 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1089 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1090 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1091 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1092 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1093 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1094 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1095 | maxElementsKnown = true; |
| 1096 | } |
| 1097 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1098 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1099 | while (Index < IList->getNumInits()) { |
| 1100 | Expr *Init = IList->getInit(Index); |
| 1101 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1102 | // If we're not the subobject that matches up with the '{' for |
| 1103 | // the designator, we shouldn't be handling the |
| 1104 | // designator. Return immediately. |
| 1105 | if (!SubobjectIsDesignatorContext) |
| 1106 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1108 | // Handle this designated initializer. elementIndex will be |
| 1109 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1110 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1111 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1112 | StructuredList, StructuredIndex, true, |
| 1113 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1114 | hadError = true; |
| 1115 | continue; |
| 1116 | } |
| 1117 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1118 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1119 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1120 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1121 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1122 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1124 | // If the array is of incomplete type, keep track of the number of |
| 1125 | // elements in the initializer. |
| 1126 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1127 | maxElements = elementIndex; |
| 1128 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1129 | continue; |
| 1130 | } |
| 1131 | |
| 1132 | // If we know the maximum number of elements, and we've already |
| 1133 | // hit it, stop consuming elements in the initializer list. |
| 1134 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1135 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1136 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1137 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1138 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1139 | Entity); |
| 1140 | // Check this element. |
| 1141 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1142 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1143 | ++elementIndex; |
| 1144 | |
| 1145 | // If the array is of incomplete type, keep track of the number of |
| 1146 | // elements in the initializer. |
| 1147 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1148 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1149 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1150 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1151 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1152 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1153 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1154 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1155 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1156 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1157 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1158 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1159 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1160 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1162 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1166 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1167 | Expr *InitExpr, |
| 1168 | FieldDecl *Field, |
| 1169 | bool TopLevelObject) { |
| 1170 | // Handle GNU flexible array initializers. |
| 1171 | unsigned FlexArrayDiag; |
| 1172 | if (isa<InitListExpr>(InitExpr) && |
| 1173 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1174 | // Empty flexible array init always allowed as an extension |
| 1175 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1176 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1177 | // Disallow flexible array init in C++; it is not required for gcc |
| 1178 | // compatibility, and it needs work to IRGen correctly in general. |
| 1179 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1180 | } else if (!TopLevelObject) { |
| 1181 | // Disallow flexible array init on non-top-level object |
| 1182 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1183 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1184 | // Disallow flexible array init on anything which is not a variable. |
| 1185 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1186 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1187 | // Disallow flexible array init on local variables. |
| 1188 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1189 | } else { |
| 1190 | // Allow other cases. |
| 1191 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1192 | } |
| 1193 | |
| 1194 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1195 | FlexArrayDiag) |
| 1196 | << InitExpr->getSourceRange().getBegin(); |
| 1197 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1198 | << Field; |
| 1199 | |
| 1200 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1201 | } |
| 1202 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1203 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1204 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1206 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1208 | unsigned &Index, |
| 1209 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1210 | unsigned &StructuredIndex, |
| 1211 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1212 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1214 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1215 | // confusion, we forgo checking the intializer for the entire record. |
| 1216 | if (structDecl->isInvalidDecl()) { |
| 1217 | hadError = true; |
| 1218 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1220 | |
| 1221 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1222 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1223 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1224 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1225 | Field != FieldEnd; ++Field) { |
| 1226 | if (Field->getDeclName()) { |
| 1227 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1228 | break; |
| 1229 | } |
| 1230 | } |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1234 | // If structDecl is a forward declaration, this loop won't do |
| 1235 | // anything except look at designated initializers; That's okay, |
| 1236 | // because an error should get printed out elsewhere. It might be |
| 1237 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1238 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1239 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1240 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1241 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1242 | while (Index < IList->getNumInits()) { |
| 1243 | Expr *Init = IList->getInit(Index); |
| 1244 | |
| 1245 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1246 | // If we're not the subobject that matches up with the '{' for |
| 1247 | // the designator, we shouldn't be handling the |
| 1248 | // designator. Return immediately. |
| 1249 | if (!SubobjectIsDesignatorContext) |
| 1250 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1252 | // Handle this designated initializer. Field will be updated to |
| 1253 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1254 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1255 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1256 | StructuredList, StructuredIndex, |
| 1257 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1258 | hadError = true; |
| 1259 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1260 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Disable check for missing fields when designators are used. |
| 1263 | // This matches gcc behaviour. |
| 1264 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1265 | continue; |
| 1266 | } |
| 1267 | |
| 1268 | if (Field == FieldEnd) { |
| 1269 | // We've run out of fields. We're done. |
| 1270 | break; |
| 1271 | } |
| 1272 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1273 | // We've already initialized a member of a union. We're done. |
| 1274 | if (InitializedSomething && DeclType->isUnionType()) |
| 1275 | break; |
| 1276 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1277 | // If we've hit the flexible array member at the end, we're done. |
| 1278 | if (Field->getType()->isIncompleteArrayType()) |
| 1279 | break; |
| 1280 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1281 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1282 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1283 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1284 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1285 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1287 | // Make sure we can use this declaration. |
| 1288 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1289 | IList->getInit(Index)->getLocStart())) { |
| 1290 | ++Index; |
| 1291 | ++Field; |
| 1292 | hadError = true; |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1296 | InitializedEntity MemberEntity = |
| 1297 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1298 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1299 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1300 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (DeclType->isUnionType()) { |
| 1303 | // Initialize the first field within the union. |
| 1304 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1305 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1306 | |
| 1307 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1308 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1309 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1310 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1311 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1312 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1313 | // It is possible we have one or more unnamed bitfields remaining. |
| 1314 | // Find first (if any) named field and emit warning. |
| 1315 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1316 | it != end; ++it) { |
| 1317 | if (!it->isUnnamedBitfield()) { |
| 1318 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1319 | diag::warn_missing_field_initializers) << it->getName(); |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1326 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1327 | return; |
| 1328 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1329 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1330 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1331 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1332 | ++Index; |
| 1333 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1336 | InitializedEntity MemberEntity = |
| 1337 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1338 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1339 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1340 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1341 | StructuredList, StructuredIndex); |
| 1342 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1343 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1344 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1345 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1347 | /// \brief Expand a field designator that refers to a member of an |
| 1348 | /// anonymous struct or union into a series of field designators that |
| 1349 | /// refers to the field within the appropriate subobject. |
| 1350 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1351 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | DesignatedInitExpr *DIE, |
| 1353 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1354 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1355 | typedef DesignatedInitExpr::Designator Designator; |
| 1356 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1357 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1358 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1359 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1360 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1361 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1363 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1364 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1365 | else |
| 1366 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1367 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1368 | assert(isa<FieldDecl>(*PI)); |
| 1369 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | // Expand the current designator into the set of replacement |
| 1373 | // designators, so we have a full subobject path down to where the |
| 1374 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1375 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1376 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1377 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1379 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1380 | /// corresponds to FieldName. |
| 1381 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1382 | IdentifierInfo *FieldName) { |
| 1383 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1384 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1385 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1386 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1387 | return IF; |
| 1388 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1389 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1390 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1393 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1394 | /// |
| 1395 | /// Determines whether the designated initializer @p DIE, which |
| 1396 | /// resides at the given @p Index within the initializer list @p |
| 1397 | /// IList, is well-formed for a current object of type @p DeclType |
| 1398 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1400 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1401 | /// |
| 1402 | /// @param IList The initializer list in which this designated |
| 1403 | /// initializer occurs. |
| 1404 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1405 | /// @param DIE The designated initializer expression. |
| 1406 | /// |
| 1407 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1408 | /// |
| 1409 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1410 | /// into which the designation in @p DIE should refer. |
| 1411 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1412 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1413 | /// a field, this will be set to the field declaration corresponding |
| 1414 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1415 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1416 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1417 | /// DIE is an array designator or GNU array-range designator, this |
| 1418 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1419 | /// |
| 1420 | /// @param Index Index into @p IList where the designated initializer |
| 1421 | /// @p DIE occurs. |
| 1422 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1423 | /// @param StructuredList The initializer list expression that |
| 1424 | /// describes all of the subobject initializers in the order they'll |
| 1425 | /// actually be initialized. |
| 1426 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1427 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1429 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1430 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1432 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1433 | QualType &CurrentObjectType, |
| 1434 | RecordDecl::field_iterator *NextField, |
| 1435 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1436 | unsigned &Index, |
| 1437 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1438 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1439 | bool FinishSubobjectInit, |
| 1440 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1441 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1442 | // Check the actual initialization for the designated object type. |
| 1443 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Temporarily remove the designator expression from the |
| 1446 | // initializer list that the child calls see, so that we don't try |
| 1447 | // to re-process the designator. |
| 1448 | unsigned OldIndex = Index; |
| 1449 | IList->setInit(OldIndex, DIE->getInit()); |
| 1450 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1451 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1452 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Restore the designated initializer expression in the syntactic |
| 1455 | // form of the initializer list. |
| 1456 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1457 | DIE->setInit(IList->getInit(OldIndex)); |
| 1458 | IList->setInit(OldIndex, DIE); |
| 1459 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1460 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1463 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1465 | "Need a non-designated initializer list to start from"); |
| 1466 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1467 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1468 | // Determine the structural initializer list that corresponds to the |
| 1469 | // current subobject. |
| 1470 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1472 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1473 | SourceRange(D->getStartLocation(), |
| 1474 | DIE->getSourceRange().getEnd())); |
| 1475 | assert(StructuredList && "Expected a structured initializer list"); |
| 1476 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1477 | if (D->isFieldDesignator()) { |
| 1478 | // C99 6.7.8p7: |
| 1479 | // |
| 1480 | // If a designator has the form |
| 1481 | // |
| 1482 | // . identifier |
| 1483 | // |
| 1484 | // then the current object (defined below) shall have |
| 1485 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1487 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1488 | if (!RT) { |
| 1489 | SourceLocation Loc = D->getDotLoc(); |
| 1490 | if (Loc.isInvalid()) |
| 1491 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1492 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1493 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1494 | ++Index; |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1498 | // Note: we perform a linear search of the fields here, despite |
| 1499 | // the fact that we have a faster lookup method, because we always |
| 1500 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1501 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1502 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1503 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1505 | Field = RT->getDecl()->field_begin(), |
| 1506 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1507 | for (; Field != FieldEnd; ++Field) { |
| 1508 | if (Field->isUnnamedBitfield()) |
| 1509 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1510 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1511 | // If we find a field representing an anonymous field, look in the |
| 1512 | // IndirectFieldDecl that follow for the designated initializer. |
| 1513 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1514 | if (IndirectFieldDecl *IF = |
| 1515 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1516 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1517 | D = DIE->getDesignator(DesigIdx); |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1521 | if (KnownField && KnownField == *Field) |
| 1522 | break; |
| 1523 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | |
| 1526 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1529 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1530 | // There was no normal field in the struct with the designated |
| 1531 | // name. Perform another lookup for this name, which may find |
| 1532 | // something that we can't designate (e.g., a member function), |
| 1533 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1535 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1536 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1537 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1538 | // Name lookup didn't find anything. Determine whether this |
| 1539 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1540 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1541 | Sema::LookupMemberName); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1542 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1543 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1544 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1545 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1546 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1547 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1548 | ->Equals(RT->getDecl())) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1549 | std::string CorrectedStr( |
| 1550 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1551 | std::string CorrectedQuotedStr( |
| 1552 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1553 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1554 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1555 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1556 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1557 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1558 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1559 | } else { |
| 1560 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1561 | << FieldName << CurrentObjectType; |
| 1562 | ++Index; |
| 1563 | return true; |
| 1564 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1565 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1567 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1568 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1569 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1570 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1572 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1573 | ++Index; |
| 1574 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1575 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1576 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1577 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1578 | // The replacement field comes from typo correction; find it |
| 1579 | // in the list of fields. |
| 1580 | FieldIndex = 0; |
| 1581 | Field = RT->getDecl()->field_begin(); |
| 1582 | for (; Field != FieldEnd; ++Field) { |
| 1583 | if (Field->isUnnamedBitfield()) |
| 1584 | continue; |
| 1585 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1586 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1587 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1588 | break; |
| 1589 | |
| 1590 | ++FieldIndex; |
| 1591 | } |
| 1592 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1593 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1594 | |
| 1595 | // All of the fields of a union are located at the same place in |
| 1596 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1597 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1598 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1599 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1600 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1602 | // Make sure we can use this declaration. |
| 1603 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1604 | ++Index; |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1608 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1609 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1611 | // Make sure that our non-designated initializer list has space |
| 1612 | // for a subobject corresponding to this field. |
| 1613 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1614 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1615 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1616 | // This designator names a flexible array member. |
| 1617 | if (Field->getType()->isIncompleteArrayType()) { |
| 1618 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1619 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1620 | // We can't designate an object within the flexible array |
| 1621 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1623 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1625 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1627 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1628 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1629 | << *Field; |
| 1630 | Invalid = true; |
| 1631 | } |
| 1632 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1633 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1634 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1635 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1636 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1637 | diag::err_flexible_array_init_needs_braces) |
| 1638 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1639 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1640 | << *Field; |
| 1641 | Invalid = true; |
| 1642 | } |
| 1643 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1644 | // Check GNU flexible array initializer. |
| 1645 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1646 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1647 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1648 | |
| 1649 | if (Invalid) { |
| 1650 | ++Index; |
| 1651 | return true; |
| 1652 | } |
| 1653 | |
| 1654 | // Initialize the array. |
| 1655 | bool prevHadError = hadError; |
| 1656 | unsigned newStructuredIndex = FieldIndex; |
| 1657 | unsigned OldIndex = Index; |
| 1658 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1659 | |
| 1660 | InitializedEntity MemberEntity = |
| 1661 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1662 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1663 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1664 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1665 | IList->setInit(OldIndex, DIE); |
| 1666 | if (hadError && !prevHadError) { |
| 1667 | ++Field; |
| 1668 | ++FieldIndex; |
| 1669 | if (NextField) |
| 1670 | *NextField = Field; |
| 1671 | StructuredIndex = FieldIndex; |
| 1672 | return true; |
| 1673 | } |
| 1674 | } else { |
| 1675 | // Recurse to check later designated subobjects. |
| 1676 | QualType FieldType = (*Field)->getType(); |
| 1677 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1678 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1679 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1680 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1681 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1682 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1683 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1684 | true, false)) |
| 1685 | return true; |
| 1686 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Find the position of the next field to be initialized in this |
| 1689 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1690 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1691 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1692 | |
| 1693 | // If this the first designator, our caller will continue checking |
| 1694 | // the rest of this struct/class/union subobject. |
| 1695 | if (IsFirstDesignator) { |
| 1696 | if (NextField) |
| 1697 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1698 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1699 | return false; |
| 1700 | } |
| 1701 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1702 | if (!FinishSubobjectInit) |
| 1703 | return false; |
| 1704 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1705 | // We've already initialized something in the union; we're done. |
| 1706 | if (RT->getDecl()->isUnion()) |
| 1707 | return hadError; |
| 1708 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1709 | // Check the remaining fields within this class/struct/union subobject. |
| 1710 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1711 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1712 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1713 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1714 | return hadError && !prevHadError; |
| 1715 | } |
| 1716 | |
| 1717 | // C99 6.7.8p6: |
| 1718 | // |
| 1719 | // If a designator has the form |
| 1720 | // |
| 1721 | // [ constant-expression ] |
| 1722 | // |
| 1723 | // then the current object (defined below) shall have array |
| 1724 | // type and the expression shall be an integer constant |
| 1725 | // expression. If the array is of unknown size, any |
| 1726 | // nonnegative value is valid. |
| 1727 | // |
| 1728 | // Additionally, cope with the GNU extension that permits |
| 1729 | // designators of the form |
| 1730 | // |
| 1731 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1732 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1733 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1734 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1735 | << CurrentObjectType; |
| 1736 | ++Index; |
| 1737 | return true; |
| 1738 | } |
| 1739 | |
| 1740 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1741 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1742 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1743 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1744 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1745 | DesignatedEndIndex = DesignatedStartIndex; |
| 1746 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1747 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1748 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1750 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1752 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1753 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1754 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1755 | // Codegen can't handle evaluating array range designators that have side |
| 1756 | // effects, because we replicate the AST value for each initialized element. |
| 1757 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1758 | // elements with something that has a side effect, so codegen can emit an |
| 1759 | // "error unsupported" error instead of miscompiling the app. |
| 1760 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1761 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1762 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1765 | if (isa<ConstantArrayType>(AT)) { |
| 1766 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1767 | DesignatedStartIndex |
| 1768 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1769 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1770 | DesignatedEndIndex |
| 1771 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1772 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1773 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1774 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1775 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1776 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1777 | << IndexExpr->getSourceRange(); |
| 1778 | ++Index; |
| 1779 | return true; |
| 1780 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1781 | } else { |
| 1782 | // Make sure the bit-widths and signedness match. |
| 1783 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1784 | DesignatedEndIndex |
| 1785 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1786 | else if (DesignatedStartIndex.getBitWidth() < |
| 1787 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1788 | DesignatedStartIndex |
| 1789 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1790 | DesignatedStartIndex.setIsUnsigned(true); |
| 1791 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | // Make sure that our non-designated initializer list has space |
| 1795 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1796 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1798 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1800 | // Repeatedly perform subobject initializations in the range |
| 1801 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1803 | // Move to the next designator |
| 1804 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1805 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1806 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1807 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1808 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1810 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1811 | // Recurse to check later designated subobjects. |
| 1812 | QualType ElementType = AT->getElementType(); |
| 1813 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1814 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1815 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1816 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1817 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1818 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1819 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1820 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1821 | return true; |
| 1822 | |
| 1823 | // Move to the next index in the array that we'll be initializing. |
| 1824 | ++DesignatedStartIndex; |
| 1825 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1826 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1827 | |
| 1828 | // If this the first designator, our caller will continue checking |
| 1829 | // the rest of this array subobject. |
| 1830 | if (IsFirstDesignator) { |
| 1831 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1832 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1833 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1834 | return false; |
| 1835 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1837 | if (!FinishSubobjectInit) |
| 1838 | return false; |
| 1839 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1840 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1841 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1842 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1843 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1844 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1848 | // Get the structured initializer list for a subobject of type |
| 1849 | // @p CurrentObjectType. |
| 1850 | InitListExpr * |
| 1851 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1852 | QualType CurrentObjectType, |
| 1853 | InitListExpr *StructuredList, |
| 1854 | unsigned StructuredIndex, |
| 1855 | SourceRange InitRange) { |
| 1856 | Expr *ExistingInit = 0; |
| 1857 | if (!StructuredList) |
| 1858 | ExistingInit = SyntacticToSemantic[IList]; |
| 1859 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1860 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1862 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1863 | return Result; |
| 1864 | |
| 1865 | if (ExistingInit) { |
| 1866 | // We are creating an initializer list that initializes the |
| 1867 | // subobjects of the current object, but there was already an |
| 1868 | // initialization that completely initialized the current |
| 1869 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1871 | // struct X { int a, b; }; |
| 1872 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1874 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1875 | // designated initializer re-initializes the whole |
| 1876 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1878 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1879 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1881 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1882 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1883 | << ExistingInit->getSourceRange(); |
| 1884 | } |
| 1885 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1887 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1888 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1889 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1891 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1893 | // Pre-allocate storage for the structured initializer list. |
| 1894 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1895 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1896 | bool GotNumInits = false; |
| 1897 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1898 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1899 | GotNumInits = true; |
| 1900 | } else if (Index < IList->getNumInits()) { |
| 1901 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1902 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1903 | GotNumInits = true; |
| 1904 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1908 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1909 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1910 | NumElements = CAType->getSize().getZExtValue(); |
| 1911 | // Simple heuristic so that we don't allocate a very large |
| 1912 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1913 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1914 | NumElements = 0; |
| 1915 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1916 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1917 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1918 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1919 | RecordDecl *RDecl = RType->getDecl(); |
| 1920 | if (RDecl->isUnion()) |
| 1921 | NumElements = 1; |
| 1922 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1924 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1927 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1928 | NumElements = IList->getNumInits(); |
| 1929 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1930 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1932 | // Link this new initializer list into the structured initializer |
| 1933 | // lists. |
| 1934 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1935 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1936 | else { |
| 1937 | Result->setSyntacticForm(IList); |
| 1938 | SyntacticToSemantic[IList] = Result; |
| 1939 | } |
| 1940 | |
| 1941 | return Result; |
| 1942 | } |
| 1943 | |
| 1944 | /// Update the initializer at index @p StructuredIndex within the |
| 1945 | /// structured initializer list to the value @p expr. |
| 1946 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1947 | unsigned &StructuredIndex, |
| 1948 | Expr *expr) { |
| 1949 | // No structured initializer list to update |
| 1950 | if (!StructuredList) |
| 1951 | return; |
| 1952 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1953 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1954 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1955 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1957 | diag::warn_initializer_overrides) |
| 1958 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1960 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1961 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1962 | << PrevInit->getSourceRange(); |
| 1963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1965 | ++StructuredIndex; |
| 1966 | } |
| 1967 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1968 | /// Check that the given Index expression is a valid array designator |
| 1969 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1970 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1971 | /// and produces a reasonable diagnostic if there is a |
| 1972 | /// failure. Returns true if there was an error, false otherwise. If |
| 1973 | /// everything went okay, Value will receive the value of the constant |
| 1974 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1976 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1977 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1978 | |
| 1979 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1980 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1981 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1982 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1983 | if (Value.isSigned() && Value.isNegative()) |
| 1984 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1985 | << Value.toString(10) << Index->getSourceRange(); |
| 1986 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1987 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1988 | return false; |
| 1989 | } |
| 1990 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1991 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1992 | SourceLocation Loc, |
| 1993 | bool GNUSyntax, |
| 1994 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1995 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1996 | |
| 1997 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1998 | SmallVector<ASTDesignator, 32> Designators; |
| 1999 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2000 | |
| 2001 | // Build designators and check array designator expressions. |
| 2002 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2003 | const Designator &D = Desig.getDesignator(Idx); |
| 2004 | switch (D.getKind()) { |
| 2005 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2007 | D.getFieldLoc())); |
| 2008 | break; |
| 2009 | |
| 2010 | case Designator::ArrayDesignator: { |
| 2011 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2012 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2013 | if (!Index->isTypeDependent() && |
| 2014 | !Index->isValueDependent() && |
| 2015 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2016 | Invalid = true; |
| 2017 | else { |
| 2018 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2020 | D.getRBracketLoc())); |
| 2021 | InitExpressions.push_back(Index); |
| 2022 | } |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | case Designator::ArrayRangeDesignator: { |
| 2027 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2028 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2029 | llvm::APSInt StartValue; |
| 2030 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2031 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2032 | StartIndex->isValueDependent(); |
| 2033 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2034 | EndIndex->isValueDependent(); |
| 2035 | if ((!StartDependent && |
| 2036 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2037 | (!EndDependent && |
| 2038 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2039 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2040 | else { |
| 2041 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2042 | if (StartDependent || EndDependent) { |
| 2043 | // Nothing to compute. |
| 2044 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2045 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2046 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2047 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2049 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2050 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2052 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2053 | Invalid = true; |
| 2054 | } else { |
| 2055 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2057 | D.getEllipsisLoc(), |
| 2058 | D.getRBracketLoc())); |
| 2059 | InitExpressions.push_back(StartIndex); |
| 2060 | InitExpressions.push_back(EndIndex); |
| 2061 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2062 | } |
| 2063 | break; |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | if (Invalid || Init.isInvalid()) |
| 2069 | return ExprError(); |
| 2070 | |
| 2071 | // Clear out the expressions within the designation. |
| 2072 | Desig.ClearExprs(*this); |
| 2073 | |
| 2074 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2075 | = DesignatedInitExpr::Create(Context, |
| 2076 | Designators.data(), Designators.size(), |
| 2077 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2078 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2080 | if (getLangOptions().CPlusPlus) |
Eli Friedman | ea7b85b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2081 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2082 | << DIE->getSourceRange(); |
| 2083 | else if (!getLangOptions().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2084 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2085 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2087 | return Owned(DIE); |
| 2088 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2090 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2091 | InitListExpr *&InitList, QualType &DeclType) { |
| 2092 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2093 | if (!CheckInitList.HadError()) |
| 2094 | InitList = CheckInitList.getFullyStructuredList(); |
| 2095 | |
| 2096 | return CheckInitList.HadError(); |
| 2097 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2098 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2099 | //===----------------------------------------------------------------------===// |
| 2100 | // Initialization entity |
| 2101 | //===----------------------------------------------------------------------===// |
| 2102 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2103 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2104 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2105 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2106 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2107 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2108 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2109 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2110 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2111 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2112 | Type = VT->getElementType(); |
| 2113 | } else { |
| 2114 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2115 | assert(CT && "Unexpected type"); |
| 2116 | Kind = EK_ComplexElement; |
| 2117 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2118 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2121 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2122 | CXXBaseSpecifier *Base, |
| 2123 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2124 | { |
| 2125 | InitializedEntity Result; |
| 2126 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2127 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2128 | if (IsInheritedVirtualBase) |
| 2129 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2130 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2131 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2132 | return Result; |
| 2133 | } |
| 2134 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2135 | DeclarationName InitializedEntity::getName() const { |
| 2136 | switch (getKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2137 | case EK_Parameter: { |
| 2138 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2139 | return (D ? D->getDeclName() : DeclarationName()); |
| 2140 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2141 | |
| 2142 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2143 | case EK_Member: |
| 2144 | return VariableOrMember->getDeclName(); |
| 2145 | |
| 2146 | case EK_Result: |
| 2147 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2148 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2149 | case EK_Temporary: |
| 2150 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2151 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2152 | case EK_ArrayElement: |
| 2153 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2154 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2155 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2156 | return DeclarationName(); |
| 2157 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2159 | // Silence GCC warning |
| 2160 | return DeclarationName(); |
| 2161 | } |
| 2162 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2163 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2164 | switch (getKind()) { |
| 2165 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2166 | case EK_Member: |
| 2167 | return VariableOrMember; |
| 2168 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2169 | case EK_Parameter: |
| 2170 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2171 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2172 | case EK_Result: |
| 2173 | case EK_Exception: |
| 2174 | case EK_New: |
| 2175 | case EK_Temporary: |
| 2176 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2177 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2178 | case EK_ArrayElement: |
| 2179 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2180 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2181 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2182 | return 0; |
| 2183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2185 | // Silence GCC warning |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2189 | bool InitializedEntity::allowsNRVO() const { |
| 2190 | switch (getKind()) { |
| 2191 | case EK_Result: |
| 2192 | case EK_Exception: |
| 2193 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2195 | case EK_Variable: |
| 2196 | case EK_Parameter: |
| 2197 | case EK_Member: |
| 2198 | case EK_New: |
| 2199 | case EK_Temporary: |
| 2200 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2201 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2202 | case EK_ArrayElement: |
| 2203 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2204 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2205 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | } |
| 2208 | |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2212 | //===----------------------------------------------------------------------===// |
| 2213 | // Initialization sequence |
| 2214 | //===----------------------------------------------------------------------===// |
| 2215 | |
| 2216 | void InitializationSequence::Step::Destroy() { |
| 2217 | switch (Kind) { |
| 2218 | case SK_ResolveAddressOfOverloadedFunction: |
| 2219 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2220 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2221 | case SK_CastDerivedToBaseLValue: |
| 2222 | case SK_BindReference: |
| 2223 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2224 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2225 | case SK_UserConversion: |
| 2226 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2227 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2228 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2229 | case SK_ListInitialization: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2230 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2231 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2232 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2233 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2234 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2235 | case SK_ArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2236 | case SK_PassByIndirectCopyRestore: |
| 2237 | case SK_PassByIndirectRestore: |
| 2238 | case SK_ProduceObjCObject: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2239 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2241 | case SK_ConversionSequence: |
| 2242 | delete ICS; |
| 2243 | } |
| 2244 | } |
| 2245 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2246 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2247 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2251 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2252 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2254 | switch (getFailureKind()) { |
| 2255 | case FK_TooManyInitsForReference: |
| 2256 | case FK_ArrayNeedsInitList: |
| 2257 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2258 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2259 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2260 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2261 | case FK_RValueReferenceBindingToLValue: |
| 2262 | case FK_ReferenceInitDropsQualifiers: |
| 2263 | case FK_ReferenceInitFailed: |
| 2264 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2265 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2266 | case FK_TooManyInitsForScalar: |
| 2267 | case FK_ReferenceBindingToInitList: |
| 2268 | case FK_InitListBadDestinationType: |
| 2269 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2270 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2271 | case FK_ArrayTypeMismatch: |
| 2272 | case FK_NonConstantArrayInit: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2273 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2275 | case FK_ReferenceInitOverloadFailed: |
| 2276 | case FK_UserConversionOverloadFailed: |
| 2277 | case FK_ConstructorOverloadFailed: |
| 2278 | return FailedOverloadResult == OR_Ambiguous; |
| 2279 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2281 | return false; |
| 2282 | } |
| 2283 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2284 | bool InitializationSequence::isConstructorInitialization() const { |
| 2285 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2286 | } |
| 2287 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2288 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2289 | const Expr *Initializer, |
| 2290 | bool *isInitializerConstant, |
| 2291 | APValue *ConstantValue) const { |
| 2292 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2293 | return false; |
| 2294 | |
| 2295 | const Step &LastStep = Steps.back(); |
| 2296 | if (LastStep.Kind != SK_ConversionSequence) |
| 2297 | return false; |
| 2298 | |
| 2299 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2300 | const StandardConversionSequence *SCS = NULL; |
| 2301 | switch (ICS.getKind()) { |
| 2302 | case ImplicitConversionSequence::StandardConversion: |
| 2303 | SCS = &ICS.Standard; |
| 2304 | break; |
| 2305 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2306 | SCS = &ICS.UserDefined.After; |
| 2307 | break; |
| 2308 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2309 | case ImplicitConversionSequence::EllipsisConversion: |
| 2310 | case ImplicitConversionSequence::BadConversion: |
| 2311 | return false; |
| 2312 | } |
| 2313 | |
| 2314 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2315 | // [dcl.init.list]p7: |
| 2316 | // |
| 2317 | // A narrowing conversion is an implicit conversion ... |
| 2318 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2319 | QualType FromType = SCS->getToType(0); |
| 2320 | QualType ToType = SCS->getToType(1); |
| 2321 | switch (PossibleNarrowing) { |
| 2322 | // * from a floating-point type to an integer type, or |
| 2323 | // |
| 2324 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2325 | // type, except where the source is a constant expression and the actual |
| 2326 | // value after conversion will fit into the target type and will produce |
| 2327 | // the original value when converted back to the original type, or |
| 2328 | case ICK_Floating_Integral: |
| 2329 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2330 | *isInitializerConstant = false; |
| 2331 | return true; |
| 2332 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2333 | llvm::APSInt IntConstantValue; |
| 2334 | if (Initializer && |
| 2335 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2336 | // Convert the integer to the floating type. |
| 2337 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2338 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2339 | llvm::APFloat::rmNearestTiesToEven); |
| 2340 | // And back. |
| 2341 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2342 | bool ignored; |
| 2343 | Result.convertToInteger(ConvertedValue, |
| 2344 | llvm::APFloat::rmTowardZero, &ignored); |
| 2345 | // If the resulting value is different, this was a narrowing conversion. |
| 2346 | if (IntConstantValue != ConvertedValue) { |
| 2347 | *isInitializerConstant = true; |
| 2348 | *ConstantValue = APValue(IntConstantValue); |
| 2349 | return true; |
| 2350 | } |
| 2351 | } else { |
| 2352 | // Variables are always narrowings. |
| 2353 | *isInitializerConstant = false; |
| 2354 | return true; |
| 2355 | } |
| 2356 | } |
| 2357 | return false; |
| 2358 | |
| 2359 | // * from long double to double or float, or from double to float, except |
| 2360 | // where the source is a constant expression and the actual value after |
| 2361 | // conversion is within the range of values that can be represented (even |
| 2362 | // if it cannot be represented exactly), or |
| 2363 | case ICK_Floating_Conversion: |
| 2364 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2365 | // FromType is larger than ToType. |
| 2366 | Expr::EvalResult InitializerValue; |
| 2367 | // FIXME: Check whether Initializer is a constant expression according |
| 2368 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2369 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2370 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2371 | // Constant! (Except for FIXME above.) |
| 2372 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2373 | // Convert the source value into the target type. |
| 2374 | bool ignored; |
| 2375 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2376 | Ctx.getFloatTypeSemantics(ToType), |
| 2377 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2378 | // If there was no overflow, the source value is within the range of |
| 2379 | // values that can be represented. |
| 2380 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2381 | *isInitializerConstant = true; |
| 2382 | *ConstantValue = InitializerValue.Val; |
| 2383 | return true; |
| 2384 | } |
| 2385 | } else { |
| 2386 | *isInitializerConstant = false; |
| 2387 | return true; |
| 2388 | } |
| 2389 | } |
| 2390 | return false; |
| 2391 | |
| 2392 | // * from an integer type or unscoped enumeration type to an integer type |
| 2393 | // that cannot represent all the values of the original type, except where |
| 2394 | // the source is a constant expression and the actual value after |
| 2395 | // conversion will fit into the target type and will produce the original |
| 2396 | // value when converted back to the original type. |
Jeffrey Yasskin | 94f8c77 | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2397 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | 9242558 | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2398 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2399 | // Boolean conversions can be from pointers and pointers to members |
| 2400 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2401 | return false; |
| 2402 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2403 | case ICK_Integral_Conversion: { |
| 2404 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2405 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2406 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2407 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2408 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2409 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2410 | |
| 2411 | if (FromWidth > ToWidth || |
| 2412 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2413 | // Not all values of FromType can be represented in ToType. |
| 2414 | llvm::APSInt InitializerValue; |
| 2415 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2416 | *isInitializerConstant = true; |
| 2417 | *ConstantValue = APValue(InitializerValue); |
| 2418 | |
| 2419 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2420 | // signed vs. unsigned comparisons. |
| 2421 | InitializerValue = InitializerValue.extend( |
| 2422 | InitializerValue.getBitWidth() + 1); |
| 2423 | // Convert the initializer to and from the target width and signed-ness. |
| 2424 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2425 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2426 | ConvertedValue.setIsSigned(ToSigned); |
| 2427 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2428 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2429 | // If the result is different, this was a narrowing conversion. |
| 2430 | return ConvertedValue != InitializerValue; |
| 2431 | } else { |
| 2432 | // Variables are always narrowings. |
| 2433 | *isInitializerConstant = false; |
| 2434 | return true; |
| 2435 | } |
| 2436 | } |
| 2437 | return false; |
| 2438 | } |
| 2439 | |
| 2440 | default: |
| 2441 | // Other kinds of conversions are not narrowings. |
| 2442 | return false; |
| 2443 | } |
| 2444 | } |
| 2445 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2446 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2447 | FunctionDecl *Function, |
| 2448 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2449 | Step S; |
| 2450 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2451 | S.Type = Function->getType(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2452 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2453 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2454 | Steps.push_back(S); |
| 2455 | } |
| 2456 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2458 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2459 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2460 | switch (VK) { |
| 2461 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2462 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2463 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2464 | default: llvm_unreachable("No such category"); |
| 2465 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2466 | S.Type = BaseType; |
| 2467 | Steps.push_back(S); |
| 2468 | } |
| 2469 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2471 | bool BindingTemporary) { |
| 2472 | Step S; |
| 2473 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2474 | S.Type = T; |
| 2475 | Steps.push_back(S); |
| 2476 | } |
| 2477 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2478 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2479 | Step S; |
| 2480 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2481 | S.Type = T; |
| 2482 | Steps.push_back(S); |
| 2483 | } |
| 2484 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2485 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2486 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2487 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2488 | Step S; |
| 2489 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2490 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2491 | S.Function.Function = Function; |
| 2492 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2493 | Steps.push_back(S); |
| 2494 | } |
| 2495 | |
| 2496 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2497 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2498 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2499 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2500 | switch (VK) { |
| 2501 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2502 | S.Kind = SK_QualificationConversionRValue; |
| 2503 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2504 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2505 | S.Kind = SK_QualificationConversionXValue; |
| 2506 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2507 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2508 | S.Kind = SK_QualificationConversionLValue; |
| 2509 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2510 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | S.Type = Ty; |
| 2512 | Steps.push_back(S); |
| 2513 | } |
| 2514 | |
| 2515 | void InitializationSequence::AddConversionSequenceStep( |
| 2516 | const ImplicitConversionSequence &ICS, |
| 2517 | QualType T) { |
| 2518 | Step S; |
| 2519 | S.Kind = SK_ConversionSequence; |
| 2520 | S.Type = T; |
| 2521 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2522 | Steps.push_back(S); |
| 2523 | } |
| 2524 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2525 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2526 | Step S; |
| 2527 | S.Kind = SK_ListInitialization; |
| 2528 | S.Type = T; |
| 2529 | Steps.push_back(S); |
| 2530 | } |
| 2531 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2532 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2533 | InitializationSequence::AddConstructorInitializationStep( |
| 2534 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2535 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2536 | QualType T) { |
| 2537 | Step S; |
| 2538 | S.Kind = SK_ConstructorInitialization; |
| 2539 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2540 | S.Function.Function = Constructor; |
| 2541 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2542 | Steps.push_back(S); |
| 2543 | } |
| 2544 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2545 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2546 | Step S; |
| 2547 | S.Kind = SK_ZeroInitialization; |
| 2548 | S.Type = T; |
| 2549 | Steps.push_back(S); |
| 2550 | } |
| 2551 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2552 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2553 | Step S; |
| 2554 | S.Kind = SK_CAssignment; |
| 2555 | S.Type = T; |
| 2556 | Steps.push_back(S); |
| 2557 | } |
| 2558 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2559 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2560 | Step S; |
| 2561 | S.Kind = SK_StringInit; |
| 2562 | S.Type = T; |
| 2563 | Steps.push_back(S); |
| 2564 | } |
| 2565 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2566 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2567 | Step S; |
| 2568 | S.Kind = SK_ObjCObjectConversion; |
| 2569 | S.Type = T; |
| 2570 | Steps.push_back(S); |
| 2571 | } |
| 2572 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2573 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2574 | Step S; |
| 2575 | S.Kind = SK_ArrayInit; |
| 2576 | S.Type = T; |
| 2577 | Steps.push_back(S); |
| 2578 | } |
| 2579 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2580 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2581 | bool shouldCopy) { |
| 2582 | Step s; |
| 2583 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2584 | : SK_PassByIndirectRestore); |
| 2585 | s.Type = type; |
| 2586 | Steps.push_back(s); |
| 2587 | } |
| 2588 | |
| 2589 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2590 | Step S; |
| 2591 | S.Kind = SK_ProduceObjCObject; |
| 2592 | S.Type = T; |
| 2593 | Steps.push_back(S); |
| 2594 | } |
| 2595 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2596 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2597 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2598 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2599 | this->Failure = Failure; |
| 2600 | this->FailedOverloadResult = Result; |
| 2601 | } |
| 2602 | |
| 2603 | //===----------------------------------------------------------------------===// |
| 2604 | // Attempt initialization |
| 2605 | //===----------------------------------------------------------------------===// |
| 2606 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2607 | static void MaybeProduceObjCObject(Sema &S, |
| 2608 | InitializationSequence &Sequence, |
| 2609 | const InitializedEntity &Entity) { |
| 2610 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2611 | |
| 2612 | /// When initializing a parameter, produce the value if it's marked |
| 2613 | /// __attribute__((ns_consumed)). |
| 2614 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2615 | if (!Entity.isParameterConsumed()) |
| 2616 | return; |
| 2617 | |
| 2618 | assert(Entity.getType()->isObjCRetainableType() && |
| 2619 | "consuming an object of unretainable type?"); |
| 2620 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2621 | |
| 2622 | /// When initializing a return value, if the return type is a |
| 2623 | /// retainable type, then returns need to immediately retain the |
| 2624 | /// object. If an autorelease is required, it will be done at the |
| 2625 | /// last instant. |
| 2626 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2627 | if (!Entity.getType()->isObjCRetainableType()) |
| 2628 | return; |
| 2629 | |
| 2630 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2634 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2635 | static void TryListInitialization(Sema &S, |
| 2636 | const InitializedEntity &Entity, |
| 2637 | const InitializationKind &Kind, |
| 2638 | InitListExpr *InitList, |
| 2639 | InitializationSequence &Sequence) { |
| 2640 | // FIXME: We only perform rudimentary checking of list |
| 2641 | // initializations at this point, then assume that any list |
| 2642 | // initialization of an array, aggregate, or scalar will be |
| 2643 | // well-formed. When we actually "perform" list initialization, we'll |
| 2644 | // do all of the necessary checking. C++0x initializer lists will |
| 2645 | // force us to perform more checking here. |
| 2646 | |
| 2647 | QualType DestType = Entity.getType(); |
| 2648 | |
| 2649 | // C++ [dcl.init]p13: |
| 2650 | // If T is a scalar type, then a declaration of the form |
| 2651 | // |
| 2652 | // T x = { a }; |
| 2653 | // |
| 2654 | // is equivalent to |
| 2655 | // |
| 2656 | // T x = a; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2657 | if (DestType->isAnyComplexType()) { |
| 2658 | // We allow more than 1 init for complex types in some cases, even though |
| 2659 | // they are scalar. |
| 2660 | } else if (DestType->isScalarType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2661 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2662 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2663 | return; |
| 2664 | } |
| 2665 | |
| 2666 | // Assume scalar initialization from a single value works. |
| 2667 | } else if (DestType->isAggregateType()) { |
| 2668 | // Assume aggregate initialization works. |
| 2669 | } else if (DestType->isVectorType()) { |
| 2670 | // Assume vector initialization works. |
| 2671 | } else if (DestType->isReferenceType()) { |
| 2672 | // FIXME: C++0x defines behavior for this. |
| 2673 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2674 | return; |
| 2675 | } else if (DestType->isRecordType()) { |
| 2676 | // FIXME: C++0x defines behavior for this |
| 2677 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2678 | } |
| 2679 | |
| 2680 | // Add a general "list initialization" step. |
| 2681 | Sequence.AddListInitializationStep(DestType); |
| 2682 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2683 | |
| 2684 | /// \brief Try a reference initialization that involves calling a conversion |
| 2685 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2686 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2687 | const InitializedEntity &Entity, |
| 2688 | const InitializationKind &Kind, |
| 2689 | Expr *Initializer, |
| 2690 | bool AllowRValues, |
| 2691 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2692 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2693 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2694 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2695 | QualType cv2T2 = Initializer->getType(); |
| 2696 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2697 | |
| 2698 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2699 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2700 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2701 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2702 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2703 | ObjCConversion, |
| 2704 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2705 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2706 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2707 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2708 | (void)ObjCLifetimeConversion; |
| 2709 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2710 | // Build the candidate set directly in the initialization sequence |
| 2711 | // structure, so that it will persist if we fail. |
| 2712 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2713 | CandidateSet.clear(); |
| 2714 | |
| 2715 | // Determine whether we are allowed to call explicit constructors or |
| 2716 | // explicit conversion operators. |
| 2717 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2719 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2720 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2721 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2722 | // The type we're converting to is a class type. Enumerate its constructors |
| 2723 | // to see if there is a suitable conversion. |
| 2724 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2726 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2727 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2728 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2729 | NamedDecl *D = *Con; |
| 2730 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2731 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2732 | // Find the constructor (which may be a template). |
| 2733 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2734 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2735 | if (ConstructorTmpl) |
| 2736 | Constructor = cast<CXXConstructorDecl>( |
| 2737 | ConstructorTmpl->getTemplatedDecl()); |
| 2738 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2739 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2741 | if (!Constructor->isInvalidDecl() && |
| 2742 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2743 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2744 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2745 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2746 | &Initializer, 1, CandidateSet, |
| 2747 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2748 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2749 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2750 | &Initializer, 1, CandidateSet, |
| 2751 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2753 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2754 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2755 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2756 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2758 | const RecordType *T2RecordType = 0; |
| 2759 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2760 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2761 | // The type we're converting from is a class type, enumerate its conversion |
| 2762 | // functions. |
| 2763 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2764 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2765 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2766 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2767 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2768 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2769 | NamedDecl *D = *I; |
| 2770 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2771 | if (isa<UsingShadowDecl>(D)) |
| 2772 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2774 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2775 | CXXConversionDecl *Conv; |
| 2776 | if (ConvTemplate) |
| 2777 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2778 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2779 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2781 | // If the conversion function doesn't return a reference type, |
| 2782 | // it can't be considered for this conversion unless we're allowed to |
| 2783 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | // FIXME: Do we need to make sure that we only consider conversion |
| 2785 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2786 | // break recursion. |
| 2787 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2788 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2789 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2790 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2791 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2792 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2793 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2794 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2795 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2799 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2800 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2802 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2803 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2805 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2806 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2807 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2808 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2810 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2811 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2812 | // This is the overload that will actually be used for the initialization, so |
| 2813 | // mark it as used. |
| 2814 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2815 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2816 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2817 | if (isa<CXXConversionDecl>(Function)) |
| 2818 | T2 = Function->getResultType(); |
| 2819 | else |
| 2820 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2821 | |
| 2822 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2823 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2824 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2825 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2827 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2828 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2829 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2830 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2831 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2832 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2833 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2835 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2836 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2837 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2839 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2840 | NewDerivedToBase, NewObjCConversion, |
| 2841 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2842 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2843 | // If the type we've converted to is not reference-related to the |
| 2844 | // type we're looking for, then there is another conversion step |
| 2845 | // we need to perform to produce a temporary of the right type |
| 2846 | // that we'll be binding to. |
| 2847 | ImplicitConversionSequence ICS; |
| 2848 | ICS.setStandard(); |
| 2849 | ICS.Standard = Best->FinalConversion; |
| 2850 | T2 = ICS.Standard.getToType(2); |
| 2851 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2852 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2853 | Sequence.AddDerivedToBaseCastStep( |
| 2854 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2856 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2857 | else if (NewObjCConversion) |
| 2858 | Sequence.AddObjCObjectConversionStep( |
| 2859 | S.Context.getQualifiedType(T1, |
| 2860 | T2.getNonReferenceType().getQualifiers())); |
| 2861 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2862 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2863 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2865 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2866 | return OR_Success; |
| 2867 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2868 | |
| 2869 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2870 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2871 | const InitializedEntity &Entity, |
| 2872 | const InitializationKind &Kind, |
| 2873 | Expr *Initializer, |
| 2874 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2875 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2876 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2877 | Qualifiers T1Quals; |
| 2878 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2879 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2880 | Qualifiers T2Quals; |
| 2881 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2882 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2884 | // If the initializer is the address of an overloaded function, try |
| 2885 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2886 | // type of the resulting function. |
| 2887 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2888 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2889 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2890 | T1, |
| 2891 | false, |
| 2892 | Found)) { |
| 2893 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2894 | cv2T2 = Fn->getType(); |
| 2895 | T2 = cv2T2.getUnqualifiedType(); |
| 2896 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2897 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2898 | return; |
| 2899 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2900 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2902 | // Compute some basic properties of the types and the initializer. |
| 2903 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2904 | bool isRValueRef = !isLValueRef; |
| 2905 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2906 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2907 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2908 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2909 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2910 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2911 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2913 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2915 | // "cv2 T2" as follows: |
| 2916 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2917 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2918 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2919 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2920 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2921 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2922 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2923 | bool T1Function = T1->isFunctionType(); |
| 2924 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2925 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2926 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2928 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2930 | // reference-compatible with "cv2 T2," or |
| 2931 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2933 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2934 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2935 | // to decide whether we're actually binding to a temporary created from |
| 2936 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2937 | if (DerivedToBase) |
| 2938 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2940 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2941 | else if (ObjCConversion) |
| 2942 | Sequence.AddObjCObjectConversionStep( |
| 2943 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2944 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2945 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2946 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2947 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2948 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2949 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2950 | return; |
| 2951 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | |
| 2953 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2954 | // reference-related to T2, and can be implicitly converted to an |
| 2955 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2956 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2957 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2958 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2959 | // If we have an rvalue ref to function type here, the rhs must be |
| 2960 | // an rvalue. |
| 2961 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2962 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2963 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2964 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2965 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2966 | Sequence); |
| 2967 | if (ConvOvlResult == OR_Success) |
| 2968 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2969 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2970 | Sequence.SetOverloadFailure( |
| 2971 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2972 | ConvOvlResult); |
| 2973 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2974 | } |
| 2975 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2976 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2978 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 7a2a116 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 2979 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2980 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2981 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2982 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2983 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2984 | Sequence.SetOverloadFailure( |
| 2985 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2986 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2987 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2988 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2989 | ? (RefRelationship == Sema::Ref_Related |
| 2990 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2991 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2992 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2993 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2994 | return; |
| 2995 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2996 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2997 | // - If the initializer expression |
| 2998 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2999 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3000 | // Note: functions are handled below. |
| 3001 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3002 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3004 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3005 | (InitCategory.isXValue() || |
| 3006 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3007 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3008 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3009 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3010 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3011 | // compiler the freedom to perform a copy here or bind to the |
| 3012 | // object, while C++0x requires that we bind directly to the |
| 3013 | // object. Hence, we always bind to the object without making an |
| 3014 | // extra copy. However, in C++03 requires that we check for the |
| 3015 | // presence of a suitable copy constructor: |
| 3016 | // |
| 3017 | // The constructor that would be used to make the copy shall |
| 3018 | // be callable whether or not the copy is actually done. |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3019 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3020 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3022 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3023 | if (DerivedToBase) |
| 3024 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3025 | ValueKind); |
| 3026 | else if (ObjCConversion) |
| 3027 | Sequence.AddObjCObjectConversionStep( |
| 3028 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3030 | if (T1Quals != T2Quals) |
| 3031 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3032 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3033 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3034 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3035 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3036 | |
| 3037 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3038 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3039 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3040 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3041 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3042 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3043 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3044 | Kind, Initializer, |
| 3045 | /*AllowRValues=*/true, |
| 3046 | Sequence); |
| 3047 | if (ConvOvlResult) |
| 3048 | Sequence.SetOverloadFailure( |
| 3049 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3050 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3052 | return; |
| 3053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3055 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3056 | return; |
| 3057 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3058 | |
| 3059 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3060 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3062 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3064 | // Determine whether we are allowed to call explicit constructors or |
| 3065 | // explicit conversion operators. |
| 3066 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3067 | |
| 3068 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3069 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3070 | ImplicitConversionSequence ICS |
| 3071 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3072 | /*SuppressUserConversions*/ false, |
| 3073 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3074 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3075 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3076 | /*AllowObjCWritebackConversion=*/false); |
| 3077 | |
| 3078 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3079 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3080 | // this into an overloading ambiguity diagnostic. However, we need |
| 3081 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3082 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3083 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3084 | Sequence.SetOverloadFailure( |
| 3085 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3086 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3087 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3088 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3089 | else |
| 3090 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3091 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3092 | } else { |
| 3093 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3097 | // same cv-qualification as, or greater cv-qualification |
| 3098 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3099 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3100 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3101 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3102 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3103 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3104 | return; |
| 3105 | } |
| 3106 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3108 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3109 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3110 | InitCategory.isLValue()) { |
| 3111 | Sequence.SetFailed( |
| 3112 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3113 | return; |
| 3114 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3116 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3117 | return; |
| 3118 | } |
| 3119 | |
| 3120 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3122 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3123 | const InitializedEntity &Entity, |
| 3124 | const InitializationKind &Kind, |
| 3125 | Expr *Initializer, |
| 3126 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3127 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3130 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3131 | /// enumerates the constructors of the initialized entity and performs overload |
| 3132 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3133 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3134 | const InitializedEntity &Entity, |
| 3135 | const InitializationKind &Kind, |
| 3136 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3137 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3138 | InitializationSequence &Sequence) { |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3139 | // Check constructor arguments for self reference. |
| 3140 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3141 | // Parameters arguments are occassionially constructed with itself, |
| 3142 | // for instance, in recursive functions. Skip them. |
| 3143 | if (!isa<ParmVarDecl>(DD)) |
| 3144 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3145 | S.CheckSelfReference(DD, Args[i]); |
| 3146 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3147 | // Build the candidate set directly in the initialization sequence |
| 3148 | // structure, so that it will persist if we fail. |
| 3149 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3150 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3152 | // Determine whether we are allowed to call explicit constructors or |
| 3153 | // explicit conversion operators. |
| 3154 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3155 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3156 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3157 | |
| 3158 | // The type we're constructing needs to be complete. |
| 3159 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3160 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3161 | return; |
| 3162 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3163 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3164 | // The type we're converting to is a class type. Enumerate its constructors |
| 3165 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3166 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3168 | CXXRecordDecl *DestRecordDecl |
| 3169 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3170 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3171 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3172 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3173 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3174 | NamedDecl *D = *Con; |
| 3175 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3176 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3178 | // Find the constructor (which may be a template). |
| 3179 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3180 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3181 | if (ConstructorTmpl) |
| 3182 | Constructor = cast<CXXConstructorDecl>( |
| 3183 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3184 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3185 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3186 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3187 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3188 | // suppress user-defined conversions on the arguments. |
| 3189 | // FIXME: Move constructors? |
| 3190 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3191 | Constructor->isCopyConstructor()) |
| 3192 | SuppressUserConversions = true; |
| 3193 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3194 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3195 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3196 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3197 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3198 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3199 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3200 | Args, NumArgs, CandidateSet, |
| 3201 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3202 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3203 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3204 | Args, NumArgs, CandidateSet, |
| 3205 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3206 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3209 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3212 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3214 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3215 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3216 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3217 | Result); |
| 3218 | return; |
| 3219 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3220 | |
| 3221 | // C++0x [dcl.init]p6: |
| 3222 | // If a program calls for the default initialization of an object |
| 3223 | // of a const-qualified type T, T shall be a class type with a |
| 3224 | // user-provided default constructor. |
| 3225 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3226 | Entity.getType().isConstQualified() && |
| 3227 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3228 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3229 | return; |
| 3230 | } |
| 3231 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3232 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3233 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3234 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3235 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3236 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3237 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3240 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3242 | const InitializedEntity &Entity, |
| 3243 | const InitializationKind &Kind, |
| 3244 | InitializationSequence &Sequence) { |
| 3245 | // C++ [dcl.init]p5: |
| 3246 | // |
| 3247 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3248 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3249 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3250 | // -- if T is an array type, then each element is value-initialized; |
| 3251 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3252 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3253 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3254 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3255 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3256 | // -- if T is a class type (clause 9) with a user-declared |
| 3257 | // constructor (12.1), then the default constructor for T is |
| 3258 | // called (and the initialization is ill-formed if T has no |
| 3259 | // accessible default constructor); |
| 3260 | // |
| 3261 | // FIXME: we really want to refer to a single subobject of the array, |
| 3262 | // but Entity doesn't have a way to capture that (yet). |
| 3263 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3264 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3266 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3267 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3268 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3269 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3270 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3271 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3272 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3274 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3278 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3281 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3282 | static void TryDefaultInitialization(Sema &S, |
| 3283 | const InitializedEntity &Entity, |
| 3284 | const InitializationKind &Kind, |
| 3285 | InitializationSequence &Sequence) { |
| 3286 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3288 | // C++ [dcl.init]p6: |
| 3289 | // To default-initialize an object of type T means: |
| 3290 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3291 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3292 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3293 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3294 | // constructor for T is called (and the initialization is ill-formed if |
| 3295 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3296 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3297 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3298 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3299 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3301 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3303 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3304 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3305 | // default constructor. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3306 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3307 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3308 | return; |
| 3309 | } |
| 3310 | |
| 3311 | // If the destination type has a lifetime property, zero-initialize it. |
| 3312 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3313 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3314 | return; |
| 3315 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3319 | /// which enumerates all conversion functions and performs overload resolution |
| 3320 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3321 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3322 | const InitializedEntity &Entity, |
| 3323 | const InitializationKind &Kind, |
| 3324 | Expr *Initializer, |
| 3325 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3326 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3327 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3328 | QualType SourceType = Initializer->getType(); |
| 3329 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3330 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3332 | // Build the candidate set directly in the initialization sequence |
| 3333 | // structure, so that it will persist if we fail. |
| 3334 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3335 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3337 | // Determine whether we are allowed to call explicit constructors or |
| 3338 | // explicit conversion operators. |
| 3339 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3341 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3342 | // The type we're converting to is a class type. Enumerate its constructors |
| 3343 | // to see if there is a suitable conversion. |
| 3344 | CXXRecordDecl *DestRecordDecl |
| 3345 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3346 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3347 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3348 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3349 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3350 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3351 | Con != ConEnd; ++Con) { |
| 3352 | NamedDecl *D = *Con; |
| 3353 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3354 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3355 | // Find the constructor (which may be a template). |
| 3356 | CXXConstructorDecl *Constructor = 0; |
| 3357 | FunctionTemplateDecl *ConstructorTmpl |
| 3358 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3359 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3360 | Constructor = cast<CXXConstructorDecl>( |
| 3361 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3362 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3363 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3365 | if (!Constructor->isInvalidDecl() && |
| 3366 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3367 | if (ConstructorTmpl) |
| 3368 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3369 | /*ExplicitArgs*/ 0, |
| 3370 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3371 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3372 | else |
| 3373 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3374 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3375 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3376 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3377 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3378 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3379 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3380 | |
| 3381 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3382 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3383 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3384 | // The type we're converting from is a class type, enumerate its conversion |
| 3385 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3386 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3387 | // We can only enumerate the conversion functions for a complete type; if |
| 3388 | // the type isn't complete, simply skip this step. |
| 3389 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3390 | CXXRecordDecl *SourceRecordDecl |
| 3391 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3393 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3394 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3395 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3397 | I != E; ++I) { |
| 3398 | NamedDecl *D = *I; |
| 3399 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3400 | if (isa<UsingShadowDecl>(D)) |
| 3401 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3403 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3404 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3405 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3406 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3407 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3408 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3409 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3410 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3411 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3412 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3413 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3414 | CandidateSet); |
| 3415 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3416 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3417 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3418 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3419 | } |
| 3420 | } |
| 3421 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | |
| 3423 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3424 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3425 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3426 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3427 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3428 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3429 | Result); |
| 3430 | return; |
| 3431 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3433 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3434 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3436 | if (isa<CXXConstructorDecl>(Function)) { |
| 3437 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3438 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3439 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3440 | return; |
| 3441 | } |
| 3442 | |
| 3443 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3444 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3445 | if (ConvType->getAs<RecordType>()) { |
| 3446 | // If we're converting to a class type, there may be an copy if |
| 3447 | // the resulting temporary object (possible to create an object of |
| 3448 | // a base class type). That copy is not a separate conversion, so |
| 3449 | // we just make a note of the actual destination type (possibly a |
| 3450 | // base class of the type returned by the conversion function) and |
| 3451 | // let the user-defined conversion step handle the conversion. |
| 3452 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3453 | return; |
| 3454 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3456 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3458 | // If the conversion following the call to the conversion function |
| 3459 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3460 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3461 | Best->FinalConversion.Third) { |
| 3462 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3463 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3464 | ICS.Standard = Best->FinalConversion; |
| 3465 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3466 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3469 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3470 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3471 | |
| 3472 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3473 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3474 | bool isAddressOf) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3475 | // Skip parens. |
| 3476 | e = e->IgnoreParens(); |
| 3477 | |
| 3478 | // Skip address-of nodes. |
| 3479 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3480 | if (op->getOpcode() == UO_AddrOf) |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3481 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3482 | |
| 3483 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3484 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3485 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3486 | case CK_Dependent: |
| 3487 | case CK_BitCast: |
| 3488 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3489 | case CK_NoOp: |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3490 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3491 | |
| 3492 | case CK_ArrayToPointerDecay: |
| 3493 | return IIK_nonscalar; |
| 3494 | |
| 3495 | case CK_NullToPointer: |
| 3496 | return IIK_okay; |
| 3497 | |
| 3498 | default: |
| 3499 | break; |
| 3500 | } |
| 3501 | |
| 3502 | // If we have a declaration reference, it had better be a local variable. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3503 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3504 | if (!isAddressOf) return IIK_nonlocal; |
| 3505 | |
| 3506 | VarDecl *var; |
| 3507 | if (isa<DeclRefExpr>(e)) { |
| 3508 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3509 | if (!var) return IIK_nonlocal; |
| 3510 | } else { |
| 3511 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3512 | } |
| 3513 | |
| 3514 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3515 | |
| 3516 | // If we have a conditional operator, check both sides. |
| 3517 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3518 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3519 | return iik; |
| 3520 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3521 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3522 | |
| 3523 | // These are never scalar. |
| 3524 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3525 | return IIK_nonscalar; |
| 3526 | |
| 3527 | // Otherwise, it needs to be a null pointer constant. |
| 3528 | } else { |
| 3529 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3530 | ? IIK_okay : IIK_nonlocal); |
| 3531 | } |
| 3532 | |
| 3533 | return IIK_nonlocal; |
| 3534 | } |
| 3535 | |
| 3536 | /// Check whether the given expression is a valid operand for an |
| 3537 | /// indirect copy/restore. |
| 3538 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3539 | assert(src->isRValue()); |
| 3540 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3541 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3542 | if (iik == IIK_okay) return; |
| 3543 | |
| 3544 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3545 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3546 | << src->getSourceRange(); |
| 3547 | } |
| 3548 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3549 | /// \brief Determine whether we have compatible array types for the |
| 3550 | /// purposes of GNU by-copy array initialization. |
| 3551 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3552 | const ArrayType *Dest, |
| 3553 | const ArrayType *Source) { |
| 3554 | // If the source and destination array types are equivalent, we're |
| 3555 | // done. |
| 3556 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3557 | return true; |
| 3558 | |
| 3559 | // Make sure that the element types are the same. |
| 3560 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3561 | return false; |
| 3562 | |
| 3563 | // The only mismatch we allow is when the destination is an |
| 3564 | // incomplete array type and the source is a constant array type. |
| 3565 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3566 | } |
| 3567 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3568 | static bool tryObjCWritebackConversion(Sema &S, |
| 3569 | InitializationSequence &Sequence, |
| 3570 | const InitializedEntity &Entity, |
| 3571 | Expr *Initializer) { |
| 3572 | bool ArrayDecay = false; |
| 3573 | QualType ArgType = Initializer->getType(); |
| 3574 | QualType ArgPointee; |
| 3575 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3576 | ArrayDecay = true; |
| 3577 | ArgPointee = ArgArrayType->getElementType(); |
| 3578 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3579 | } |
| 3580 | |
| 3581 | // Handle write-back conversion. |
| 3582 | QualType ConvertedArgType; |
| 3583 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3584 | ConvertedArgType)) |
| 3585 | return false; |
| 3586 | |
| 3587 | // We should copy unless we're passing to an argument explicitly |
| 3588 | // marked 'out'. |
| 3589 | bool ShouldCopy = true; |
| 3590 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3591 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3592 | |
| 3593 | // Do we need an lvalue conversion? |
| 3594 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3595 | ImplicitConversionSequence ICS; |
| 3596 | ICS.setStandard(); |
| 3597 | ICS.Standard.setAsIdentityConversion(); |
| 3598 | |
| 3599 | QualType ResultType; |
| 3600 | if (ArrayDecay) { |
| 3601 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3602 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3603 | } else { |
| 3604 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3605 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3606 | } |
| 3607 | |
| 3608 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3609 | } |
| 3610 | |
| 3611 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3612 | return true; |
| 3613 | } |
| 3614 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3615 | InitializationSequence::InitializationSequence(Sema &S, |
| 3616 | const InitializedEntity &Entity, |
| 3617 | const InitializationKind &Kind, |
| 3618 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3619 | unsigned NumArgs) |
| 3620 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3621 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3623 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3624 | // The semantics of initializers are as follows. The destination type is |
| 3625 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3626 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3627 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3628 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3629 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3630 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3631 | if (DestType->isDependentType() || |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3632 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3633 | SequenceKind = DependentSequence; |
| 3634 | return; |
| 3635 | } |
| 3636 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3637 | // Almost everything is a normal sequence. |
| 3638 | setSequenceKind(NormalSequence); |
| 3639 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3640 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3641 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3642 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3643 | if (Result.isInvalid()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3644 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3645 | return; |
| 3646 | } |
| 3647 | Args[I] = Result.take(); |
| 3648 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3650 | QualType SourceType; |
| 3651 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3652 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | Initializer = Args[0]; |
| 3654 | if (!isa<InitListExpr>(Initializer)) |
| 3655 | SourceType = Initializer->getType(); |
| 3656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | |
| 3658 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3659 | // list-initialized (8.5.4). |
| 3660 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3661 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3662 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3663 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3665 | // - If the destination type is a reference type, see 8.5.3. |
| 3666 | if (DestType->isReferenceType()) { |
| 3667 | // C++0x [dcl.init.ref]p1: |
| 3668 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3669 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3670 | // by an object that can be converted into a T. |
| 3671 | // (Therefore, multiple arguments are not permitted.) |
| 3672 | if (NumArgs != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3673 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3674 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3675 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | return; |
| 3677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3679 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3680 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3681 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3682 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3683 | return; |
| 3684 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3686 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3687 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3688 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3689 | return; |
| 3690 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3691 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3692 | // - If the destination type is an array of characters, an array of |
| 3693 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3694 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3696 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3697 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3698 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3699 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3700 | return; |
| 3701 | } |
| 3702 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3703 | // Note: as an GNU C extension, we allow initialization of an |
| 3704 | // array from a compound literal that creates an array of the same |
| 3705 | // type, so long as the initializer has no side effects. |
| 3706 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3707 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3708 | Initializer->getType()->isArrayType()) { |
| 3709 | const ArrayType *SourceAT |
| 3710 | = Context.getAsArrayType(Initializer->getType()); |
| 3711 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3712 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3713 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3714 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3715 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3716 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3717 | } |
| 3718 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3719 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3720 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3721 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3723 | return; |
| 3724 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3725 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3726 | // Determine whether we should consider writeback conversions for |
| 3727 | // Objective-C ARC. |
| 3728 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3729 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3730 | |
| 3731 | // We're at the end of the line for C: it's either a write-back conversion |
| 3732 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3733 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3734 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3735 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3736 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3737 | return; |
| 3738 | } |
| 3739 | |
| 3740 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3741 | AddCAssignmentStep(DestType); |
| 3742 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3743 | return; |
| 3744 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3746 | assert(S.getLangOptions().CPlusPlus); |
| 3747 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3748 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3749 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3750 | // - If the initialization is direct-initialization, or if it is |
| 3751 | // copy-initialization where the cv-unqualified version of the |
| 3752 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3753 | // class of the destination, constructors are considered. [...] |
| 3754 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3755 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3756 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3757 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3759 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3761 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3762 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3763 | // used) to a derived class thereof are enumerated as described in |
| 3764 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3765 | // (13.3). |
| 3766 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3767 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3768 | return; |
| 3769 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3771 | if (NumArgs > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3772 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3773 | return; |
| 3774 | } |
| 3775 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
| 3777 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3778 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3779 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3780 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3781 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3782 | return; |
| 3783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3785 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3786 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3787 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3789 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3790 | |
| 3791 | ImplicitConversionSequence ICS |
| 3792 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3793 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3794 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3795 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3796 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3797 | allowObjCWritebackConversion); |
| 3798 | |
| 3799 | if (ICS.isStandard() && |
| 3800 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3801 | // Objective-C ARC writeback conversion. |
| 3802 | |
| 3803 | // We should copy unless we're passing to an argument explicitly |
| 3804 | // marked 'out'. |
| 3805 | bool ShouldCopy = true; |
| 3806 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3807 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3808 | |
| 3809 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3810 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3811 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3812 | ImplicitConversionSequence LvalueICS; |
| 3813 | LvalueICS.setStandard(); |
| 3814 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3815 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3816 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3817 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3818 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3819 | |
| 3820 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3821 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3822 | DeclAccessPair dap; |
| 3823 | if (Initializer->getType() == Context.OverloadTy && |
| 3824 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3825 | , DestType, false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3826 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3827 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3828 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3829 | } else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3830 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3831 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3832 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3833 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3837 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3838 | StepEnd = Steps.end(); |
| 3839 | Step != StepEnd; ++Step) |
| 3840 | Step->Destroy(); |
| 3841 | } |
| 3842 | |
| 3843 | //===----------------------------------------------------------------------===// |
| 3844 | // Perform initialization |
| 3845 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3846 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3847 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3848 | switch(Entity.getKind()) { |
| 3849 | case InitializedEntity::EK_Variable: |
| 3850 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3851 | case InitializedEntity::EK_Exception: |
| 3852 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3853 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3854 | return Sema::AA_Initializing; |
| 3855 | |
| 3856 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3857 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3858 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3859 | return Sema::AA_Sending; |
| 3860 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3861 | return Sema::AA_Passing; |
| 3862 | |
| 3863 | case InitializedEntity::EK_Result: |
| 3864 | return Sema::AA_Returning; |
| 3865 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3866 | case InitializedEntity::EK_Temporary: |
| 3867 | // FIXME: Can we tell apart casting vs. converting? |
| 3868 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3870 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3871 | case InitializedEntity::EK_ArrayElement: |
| 3872 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3873 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3874 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3875 | return Sema::AA_Initializing; |
| 3876 | } |
| 3877 | |
| 3878 | return Sema::AA_Converting; |
| 3879 | } |
| 3880 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3881 | /// \brief Whether we should binding a created object as a temporary when |
| 3882 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3883 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3884 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3885 | case InitializedEntity::EK_ArrayElement: |
| 3886 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3887 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3888 | case InitializedEntity::EK_New: |
| 3889 | case InitializedEntity::EK_Variable: |
| 3890 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3891 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3892 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3893 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3894 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3895 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3896 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3898 | case InitializedEntity::EK_Parameter: |
| 3899 | case InitializedEntity::EK_Temporary: |
| 3900 | return true; |
| 3901 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3903 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3904 | } |
| 3905 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3906 | /// \brief Whether the given entity, when initialized with an object |
| 3907 | /// created for that initialization, requires destruction. |
| 3908 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3909 | switch (Entity.getKind()) { |
| 3910 | case InitializedEntity::EK_Member: |
| 3911 | case InitializedEntity::EK_Result: |
| 3912 | case InitializedEntity::EK_New: |
| 3913 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3914 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3915 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3916 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3917 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3918 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3920 | case InitializedEntity::EK_Variable: |
| 3921 | case InitializedEntity::EK_Parameter: |
| 3922 | case InitializedEntity::EK_Temporary: |
| 3923 | case InitializedEntity::EK_ArrayElement: |
| 3924 | case InitializedEntity::EK_Exception: |
| 3925 | return true; |
| 3926 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | |
| 3928 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3931 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3932 | /// provided by the given initializer by calling the appropriate copy |
| 3933 | /// constructor. |
| 3934 | /// |
| 3935 | /// \param S The Sema object used for type-checking. |
| 3936 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3937 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3938 | /// the type of the initializer expression or a superclass thereof. |
| 3939 | /// |
| 3940 | /// \param Enter The entity being initialized. |
| 3941 | /// |
| 3942 | /// \param CurInit The initializer expression. |
| 3943 | /// |
| 3944 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3945 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3946 | /// an rvalue. |
| 3947 | /// |
| 3948 | /// \returns An expression that copies the initializer expression into |
| 3949 | /// a temporary object, or an error expression if a copy could not be |
| 3950 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3951 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3952 | QualType T, |
| 3953 | const InitializedEntity &Entity, |
| 3954 | ExprResult CurInit, |
| 3955 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3956 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3957 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3958 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3959 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3960 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3961 | if (!Class) |
| 3962 | return move(CurInit); |
| 3963 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3964 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3965 | // When certain criteria are met, an implementation is allowed to |
| 3966 | // omit the copy/move construction of a class object, even if the |
| 3967 | // copy/move constructor and/or destructor for the object have |
| 3968 | // side effects. [...] |
| 3969 | // - when a temporary class object that has not been bound to a |
| 3970 | // reference (12.2) would be copied/moved to a class object |
| 3971 | // with the same cv-unqualified type, the copy/move operation |
| 3972 | // can be omitted by constructing the temporary object |
| 3973 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3975 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3976 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3978 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3979 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3980 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3981 | switch (Entity.getKind()) { |
| 3982 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3983 | Loc = Entity.getReturnLoc(); |
| 3984 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3986 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3987 | Loc = Entity.getThrowLoc(); |
| 3988 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3989 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3990 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3991 | Loc = Entity.getDecl()->getLocation(); |
| 3992 | break; |
| 3993 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3994 | case InitializedEntity::EK_ArrayElement: |
| 3995 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3996 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3997 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3998 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3999 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4000 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4001 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4002 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4003 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4004 | Loc = CurInitExpr->getLocStart(); |
| 4005 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4006 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4007 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4008 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4009 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4010 | return move(CurInit); |
| 4011 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4012 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4013 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4014 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 4015 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4016 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4017 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4018 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 4019 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4020 | CXXConstructorDecl *Constructor = 0; |
| 4021 | |
| 4022 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4023 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4024 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4025 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4026 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4027 | continue; |
| 4028 | |
| 4029 | DeclAccessPair FoundDecl |
| 4030 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4031 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4032 | &CurInitExpr, 1, CandidateSet); |
| 4033 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4035 | |
| 4036 | // Handle constructor templates. |
| 4037 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4038 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4039 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4040 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4041 | Constructor = cast<CXXConstructorDecl>( |
| 4042 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4043 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4044 | continue; |
| 4045 | |
| 4046 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4047 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4048 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4049 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4050 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4051 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4052 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4053 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4054 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4055 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4056 | case OR_Success: |
| 4057 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4058 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4059 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4060 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4061 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4062 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4063 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4064 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4065 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4066 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4067 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4068 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4070 | case OR_Ambiguous: |
| 4071 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4072 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4073 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4074 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4075 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4077 | case OR_Deleted: |
| 4078 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4079 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4080 | << CurInitExpr->getSourceRange(); |
| 4081 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4082 | << 1 << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4083 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4086 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4087 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4088 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4089 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4090 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4091 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4092 | |
| 4093 | if (IsExtraneousCopy) { |
| 4094 | // If this is a totally extraneous copy for C++03 reference |
| 4095 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4096 | // expression. We don't generate an (elided) copy operation here |
| 4097 | // because doing so would require us to pass down a flag to avoid |
| 4098 | // infinite recursion, where each step adds another extraneous, |
| 4099 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4100 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4101 | // Instantiate the default arguments of any extra parameters in |
| 4102 | // the selected copy constructor, as if we were going to create a |
| 4103 | // proper call to the copy constructor. |
| 4104 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4105 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4106 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4107 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4108 | break; |
| 4109 | |
| 4110 | // Build the default argument expression; we don't actually care |
| 4111 | // if this succeeds or not, because this routine will complain |
| 4112 | // if there was a problem. |
| 4113 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4114 | } |
| 4115 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4116 | return S.Owned(CurInitExpr); |
| 4117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4118 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4119 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4120 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4121 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4122 | // constructor call (we might have derived-to-base conversions, or |
| 4123 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4124 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4125 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4126 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4127 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4128 | // Actually perform the constructor call. |
| 4129 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4130 | move_arg(ConstructorArgs), |
| 4131 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4132 | CXXConstructExpr::CK_Complete, |
| 4133 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4135 | // If we're supposed to bind temporaries, do so. |
| 4136 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4137 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4138 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4139 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4140 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4141 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4142 | const InitializedEntity &Entity) { |
| 4143 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4144 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4145 | return; |
| 4146 | |
| 4147 | if (Entity.getDecl()->getDeclName()) |
| 4148 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4149 | << Entity.getDecl()->getDeclName(); |
| 4150 | else |
| 4151 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4152 | } |
| 4153 | } |
| 4154 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4155 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4156 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4157 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4158 | } |
| 4159 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4160 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4161 | InitializationSequence::Perform(Sema &S, |
| 4162 | const InitializedEntity &Entity, |
| 4163 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4164 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4165 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4166 | if (Failed()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4167 | unsigned NumArgs = Args.size(); |
| 4168 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4169 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4170 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4172 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4173 | // If the declaration is a non-dependent, incomplete array type |
| 4174 | // that has an initializer, then its type will be completed once |
| 4175 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4176 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4177 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4178 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4179 | if (const IncompleteArrayType *ArrayT |
| 4180 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4181 | // FIXME: We don't currently have the ability to accurately |
| 4182 | // compute the length of an initializer list without |
| 4183 | // performing full type-checking of the initializer list |
| 4184 | // (since we have to determine where braces are implicitly |
| 4185 | // introduced and such). So, we fall back to making the array |
| 4186 | // type a dependently-sized array type with no specified |
| 4187 | // bound. |
| 4188 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4189 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4191 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4192 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4193 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4194 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4195 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4196 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4197 | Brackets = ArrayLoc->getBracketsRange(); |
| 4198 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | *ResultType |
| 4202 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4203 | /*NumElts=*/0, |
| 4204 | ArrayT->getSizeModifier(), |
| 4205 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4206 | Brackets); |
| 4207 | } |
| 4208 | |
| 4209 | } |
| 4210 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4211 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4212 | Kind.isExplicitCast()); |
| 4213 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4216 | // No steps means no initialization. |
| 4217 | if (Steps.empty()) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4218 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4219 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4220 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4221 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4222 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4223 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4224 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4225 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4226 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4227 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4228 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4231 | // grab the only argument out the Args and place it into the "current" |
| 4232 | // initializer. |
| 4233 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4234 | case SK_ResolveAddressOfOverloadedFunction: |
| 4235 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4236 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4237 | case SK_CastDerivedToBaseLValue: |
| 4238 | case SK_BindReference: |
| 4239 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4240 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4241 | case SK_UserConversion: |
| 4242 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4243 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4244 | case SK_QualificationConversionRValue: |
| 4245 | case SK_ConversionSequence: |
| 4246 | case SK_ListInitialization: |
| 4247 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4248 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4249 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4250 | case SK_ArrayInit: |
| 4251 | case SK_PassByIndirectCopyRestore: |
| 4252 | case SK_PassByIndirectRestore: |
| 4253 | case SK_ProduceObjCObject: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4254 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4255 | CurInit = Args.get()[0]; |
| 4256 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4257 | |
| 4258 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4259 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4260 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4261 | if (CurInit.isInvalid()) |
| 4262 | return ExprError(); |
| 4263 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4264 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4267 | case SK_ConstructorInitialization: |
| 4268 | case SK_ZeroInitialization: |
| 4269 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4270 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | |
| 4272 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4273 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4274 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4275 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4276 | Step != StepEnd; ++Step) { |
| 4277 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4278 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4279 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4280 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4282 | switch (Step->Kind) { |
| 4283 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4285 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4286 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4287 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4288 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4289 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4290 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4291 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4293 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4294 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4295 | case SK_CastDerivedToBaseLValue: { |
| 4296 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4297 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4299 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4300 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4301 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4302 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4303 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4304 | CurInit.get()->getLocStart(), |
| 4305 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4306 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4307 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4309 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4310 | QualType T = SourceType; |
| 4311 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4312 | T = Pointer->getPointeeType(); |
| 4313 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4314 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4315 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4316 | } |
| 4317 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4318 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4319 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4320 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4321 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4322 | VK_XValue : |
| 4323 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4324 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4325 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4326 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4327 | CurInit.get(), |
| 4328 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4329 | break; |
| 4330 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4332 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4333 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4334 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4335 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4336 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4337 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4338 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4339 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4340 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4341 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4342 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4343 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4344 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4345 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4346 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4347 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4348 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4349 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4350 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4351 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4352 | // Reference binding does not have any corresponding ASTs. |
| 4353 | |
| 4354 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4355 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4356 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4358 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4360 | case SK_BindReferenceToTemporary: |
| 4361 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4362 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4363 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4364 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4365 | // Materialize the temporary into memory. |
Douglas Gregor | 2fa40a3 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4366 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4367 | Entity.getType().getNonReferenceType(), |
| 4368 | CurInit.get(), |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4369 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4370 | |
| 4371 | // If we're binding to an Objective-C object that has lifetime, we |
| 4372 | // need cleanups. |
| 4373 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4374 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4375 | S.ExprNeedsCleanups = true; |
| 4376 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4377 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4379 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4381 | /*IsExtraneousCopy=*/true); |
| 4382 | break; |
| 4383 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4384 | case SK_UserConversion: { |
| 4385 | // We have a user-defined conversion that invokes either a constructor |
| 4386 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4387 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4388 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4389 | FunctionDecl *Fn = Step->Function.Function; |
| 4390 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4391 | bool CreatedObject = false; |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4392 | bool IsLvalue = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4393 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4394 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4395 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4396 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4397 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4399 | // Determine the arguments required to actually perform the constructor |
| 4400 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4401 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4402 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4403 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4404 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4405 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4407 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4409 | move_arg(ConstructorArgs), |
| 4410 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4411 | CXXConstructExpr::CK_Complete, |
| 4412 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4413 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4414 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4415 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4416 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4417 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4418 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4420 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4421 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4422 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4423 | S.IsDerivedFrom(SourceType, Class)) |
| 4424 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4426 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4427 | } else { |
| 4428 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4429 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4430 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4431 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4432 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4433 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | |
| 4435 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4436 | // derived-to-base conversion? I believe the answer is "no", because |
| 4437 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4438 | ExprResult CurInitExprRes = |
| 4439 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4440 | FoundFn, Conversion); |
| 4441 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4442 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4443 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4445 | // Build the actual call to the conversion function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4446 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4447 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4448 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4450 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4451 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4452 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4453 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4455 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4456 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4457 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4458 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4459 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4460 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4461 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4462 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4463 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4464 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4465 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4466 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4467 | } |
| 4468 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4470 | // FIXME: xvalues |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4471 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4472 | CurInit.get()->getType(), |
| 4473 | CastKind, CurInit.get(), 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4474 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4476 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4477 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4478 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4479 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4480 | break; |
| 4481 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4483 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4484 | case SK_QualificationConversionXValue: |
| 4485 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4486 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4487 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4488 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4489 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4490 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4491 | VK_XValue : |
| 4492 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4493 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4494 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4497 | case SK_ConversionSequence: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4498 | Sema::CheckedConversionKind CCK |
| 4499 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4500 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4501 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4502 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4503 | ExprResult CurInitExprRes = |
| 4504 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4505 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4506 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4507 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4508 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4509 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4510 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4512 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4513 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4514 | QualType Ty = Step->Type; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4515 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4516 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4517 | |
| 4518 | CurInit.release(); |
| 4519 | CurInit = S.Owned(InitList); |
| 4520 | break; |
| 4521 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4522 | |
| 4523 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4524 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4525 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4526 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4528 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4529 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4530 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4531 | ? Kind.getEqualLoc() |
| 4532 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4533 | |
| 4534 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4535 | // Force even a trivial, implicit default constructor to be |
| 4536 | // semantically checked. We do this explicitly because we don't build |
| 4537 | // the definition for completely trivial constructors. |
| 4538 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4539 | assert(ClassDecl && "No parent class for constructor."); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4540 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4541 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4542 | !Constructor->isUsed(false)) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4543 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4544 | } |
| 4545 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4546 | // Determine the arguments required to actually perform the constructor |
| 4547 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4549 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4550 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | |
| 4552 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4553 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4554 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4555 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4556 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4557 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4558 | unsigned NumExprs = ConstructorArgs.size(); |
| 4559 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4560 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4561 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4562 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4563 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4564 | if (!TSInfo) |
| 4565 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4566 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4567 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4568 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4569 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4571 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4572 | Kind.getParenRange(), |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4573 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4574 | } else { |
| 4575 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4576 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4578 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4579 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4580 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4581 | CXXConstructExpr::CK_NonVirtualBase; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4582 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4583 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4584 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4586 | // Only get the parenthesis range if it is a direct construction. |
| 4587 | SourceRange parenRange = |
| 4588 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4589 | Kind.getParenRange() : SourceRange(); |
| 4590 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4591 | // If the entity allows NRVO, mark the construction as elidable |
| 4592 | // unconditionally. |
| 4593 | if (Entity.allowsNRVO()) |
| 4594 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4595 | Constructor, /*Elidable=*/true, |
| 4596 | move_arg(ConstructorArgs), |
| 4597 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4598 | ConstructKind, |
| 4599 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4600 | else |
| 4601 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4603 | move_arg(ConstructorArgs), |
| 4604 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4605 | ConstructKind, |
| 4606 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4607 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4608 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4609 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4610 | |
| 4611 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4612 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4613 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4614 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4615 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4616 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4617 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4619 | break; |
| 4620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4622 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4623 | step_iterator NextStep = Step; |
| 4624 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4625 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4626 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4627 | // The need for zero-initialization is recorded directly into |
| 4628 | // the call to the object's constructor within the next step. |
| 4629 | ConstructorInitRequiresZeroInit = true; |
| 4630 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4631 | S.getLangOptions().CPlusPlus && |
| 4632 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4633 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4634 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4636 | Kind.getRange().getBegin()); |
| 4637 | |
| 4638 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4639 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4640 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4641 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4642 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4643 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4644 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4645 | break; |
| 4646 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4647 | |
| 4648 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4649 | QualType SourceType = CurInit.get()->getType(); |
| 4650 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4651 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4652 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4653 | if (Result.isInvalid()) |
| 4654 | return ExprError(); |
| 4655 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4656 | |
| 4657 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4658 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4659 | if (ConvTy != Sema::Compatible && |
| 4660 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4661 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4662 | == Sema::Compatible) |
| 4663 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4664 | if (CurInitExprRes.isInvalid()) |
| 4665 | return ExprError(); |
| 4666 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4667 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4668 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4669 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4670 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4671 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4672 | getAssignmentAction(Entity), |
| 4673 | &Complained)) { |
| 4674 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4675 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4676 | } else if (Complained) |
| 4677 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4678 | break; |
| 4679 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4680 | |
| 4681 | case SK_StringInit: { |
| 4682 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4683 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4684 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4685 | break; |
| 4686 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4687 | |
| 4688 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4689 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4690 | CK_ObjCObjectLValueCast, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4691 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4692 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4693 | |
| 4694 | case SK_ArrayInit: |
| 4695 | // Okay: we checked everything before creating this step. Note that |
| 4696 | // this is a GNU extension. |
| 4697 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4698 | << Step->Type << CurInit.get()->getType() |
| 4699 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4700 | |
| 4701 | // If the destination type is an incomplete array type, update the |
| 4702 | // type accordingly. |
| 4703 | if (ResultType) { |
| 4704 | if (const IncompleteArrayType *IncompleteDest |
| 4705 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4706 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4707 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4708 | *ResultType = S.Context.getConstantArrayType( |
| 4709 | IncompleteDest->getElementType(), |
| 4710 | ConstantSource->getSize(), |
| 4711 | ArrayType::Normal, 0); |
| 4712 | } |
| 4713 | } |
| 4714 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4715 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4716 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4717 | case SK_PassByIndirectCopyRestore: |
| 4718 | case SK_PassByIndirectRestore: |
| 4719 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4720 | CurInit = S.Owned(new (S.Context) |
| 4721 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4722 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4723 | break; |
| 4724 | |
| 4725 | case SK_ProduceObjCObject: |
| 4726 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4727 | CK_ARCProduceObject, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4728 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4729 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4730 | } |
| 4731 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4732 | |
| 4733 | // Diagnose non-fatal problems with the completed initialization. |
| 4734 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4735 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4736 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4737 | cast<FieldDecl>(Entity.getDecl()), |
| 4738 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4740 | return move(CurInit); |
| 4741 | } |
| 4742 | |
| 4743 | //===----------------------------------------------------------------------===// |
| 4744 | // Diagnose initialization failures |
| 4745 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4747 | const InitializedEntity &Entity, |
| 4748 | const InitializationKind &Kind, |
| 4749 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4750 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4751 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4753 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4754 | switch (Failure) { |
| 4755 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4756 | // FIXME: Customize for the initialized entity? |
| 4757 | if (NumArgs == 0) |
| 4758 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4759 | << DestType.getNonReferenceType(); |
| 4760 | else // FIXME: diagnostic below could be better! |
| 4761 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4762 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | case FK_ArrayNeedsInitList: |
| 4766 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4767 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4768 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4769 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4770 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4771 | case FK_ArrayTypeMismatch: |
| 4772 | case FK_NonConstantArrayInit: |
| 4773 | S.Diag(Kind.getLocation(), |
| 4774 | (Failure == FK_ArrayTypeMismatch |
| 4775 | ? diag::err_array_init_different_type |
| 4776 | : diag::err_array_init_non_constant_array)) |
| 4777 | << DestType.getNonReferenceType() |
| 4778 | << Args[0]->getType() |
| 4779 | << Args[0]->getSourceRange(); |
| 4780 | break; |
| 4781 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4782 | case FK_AddressOfOverloadFailed: { |
| 4783 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4784 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4785 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4786 | true, |
| 4787 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4788 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4789 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4791 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4792 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4793 | switch (FailedOverloadResult) { |
| 4794 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4795 | if (Failure == FK_UserConversionOverloadFailed) |
| 4796 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4797 | << Args[0]->getType() << DestType |
| 4798 | << Args[0]->getSourceRange(); |
| 4799 | else |
| 4800 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4801 | << DestType << Args[0]->getType() |
| 4802 | << Args[0]->getSourceRange(); |
| 4803 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4804 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4805 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4807 | case OR_No_Viable_Function: |
| 4808 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4809 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4810 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4811 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4812 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4814 | case OR_Deleted: { |
| 4815 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4816 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4817 | << Args[0]->getSourceRange(); |
| 4818 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4819 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4820 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4821 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4822 | if (Ovl == OR_Deleted) { |
| 4823 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4824 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4825 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4826 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4827 | } |
| 4828 | break; |
| 4829 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4830 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4831 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4832 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4833 | break; |
| 4834 | } |
| 4835 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4837 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4838 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4840 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4841 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4842 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4843 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4844 | << DestType.getNonReferenceType() |
| 4845 | << Args[0]->getType() |
| 4846 | << Args[0]->getSourceRange(); |
| 4847 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4848 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4849 | case FK_RValueReferenceBindingToLValue: |
| 4850 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4851 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4852 | << Args[0]->getSourceRange(); |
| 4853 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4855 | case FK_ReferenceInitDropsQualifiers: |
| 4856 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4857 | << DestType.getNonReferenceType() |
| 4858 | << Args[0]->getType() |
| 4859 | << Args[0]->getSourceRange(); |
| 4860 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4862 | case FK_ReferenceInitFailed: |
| 4863 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4864 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4865 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4866 | << Args[0]->getType() |
| 4867 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4868 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4869 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4870 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4871 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4873 | case FK_ConversionFailed: { |
| 4874 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4875 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4876 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4877 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4878 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4879 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4880 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4881 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4882 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4883 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4884 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4885 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4886 | |
| 4887 | case FK_ConversionFromPropertyFailed: |
| 4888 | // No-op. This error has already been reported. |
| 4889 | break; |
| 4890 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4891 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4892 | SourceRange R; |
| 4893 | |
| 4894 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4895 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4896 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4897 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4898 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4900 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4901 | if (Kind.isCStyleOrFunctionalCast()) |
| 4902 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4903 | << R; |
| 4904 | else |
| 4905 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4906 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4907 | break; |
| 4908 | } |
| 4909 | |
| 4910 | case FK_ReferenceBindingToInitList: |
| 4911 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4912 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4913 | break; |
| 4914 | |
| 4915 | case FK_InitListBadDestinationType: |
| 4916 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4917 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4918 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4920 | case FK_ConstructorOverloadFailed: { |
| 4921 | SourceRange ArgsRange; |
| 4922 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4923 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4924 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4926 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4927 | // bad. |
| 4928 | switch (FailedOverloadResult) { |
| 4929 | case OR_Ambiguous: |
| 4930 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4931 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4932 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4933 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4934 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4936 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4937 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4938 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4939 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4940 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4941 | // This is implicit default initialization of a member or |
| 4942 | // base within a constructor. If no viable function was |
| 4943 | // found, notify the user that she needs to explicitly |
| 4944 | // initialize this base/member. |
| 4945 | CXXConstructorDecl *Constructor |
| 4946 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4947 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4948 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4949 | << Constructor->isImplicit() |
| 4950 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4951 | << /*base=*/0 |
| 4952 | << Entity.getType(); |
| 4953 | |
| 4954 | RecordDecl *BaseDecl |
| 4955 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4956 | ->getDecl(); |
| 4957 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4958 | << S.Context.getTagDeclType(BaseDecl); |
| 4959 | } else { |
| 4960 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4961 | << Constructor->isImplicit() |
| 4962 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4963 | << /*member=*/1 |
| 4964 | << Entity.getName(); |
| 4965 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4966 | |
| 4967 | if (const RecordType *Record |
| 4968 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4970 | diag::note_previous_decl) |
| 4971 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4972 | } |
| 4973 | break; |
| 4974 | } |
| 4975 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4976 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4977 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4978 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4979 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4981 | case OR_Deleted: { |
| 4982 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4983 | << true << DestType << ArgsRange; |
| 4984 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4985 | OverloadingResult Ovl |
| 4986 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4987 | if (Ovl == OR_Deleted) { |
| 4988 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4989 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4990 | } else { |
| 4991 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4992 | } |
| 4993 | break; |
| 4994 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4996 | case OR_Success: |
| 4997 | llvm_unreachable("Conversion did not fail!"); |
| 4998 | break; |
| 4999 | } |
| 5000 | break; |
| 5001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5003 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5004 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5005 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5006 | // This is implicit default-initialization of a const member in |
| 5007 | // a constructor. Complain that it needs to be explicitly |
| 5008 | // initialized. |
| 5009 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5010 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5011 | << Constructor->isImplicit() |
| 5012 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5013 | << /*const=*/1 |
| 5014 | << Entity.getName(); |
| 5015 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5016 | << Entity.getName(); |
| 5017 | } else { |
| 5018 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5019 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5020 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5021 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5023 | case FK_Incomplete: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5024 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5025 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5026 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5027 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5029 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5030 | return true; |
| 5031 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5032 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5033 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5034 | switch (SequenceKind) { |
| 5035 | case FailedSequence: { |
| 5036 | OS << "Failed sequence: "; |
| 5037 | switch (Failure) { |
| 5038 | case FK_TooManyInitsForReference: |
| 5039 | OS << "too many initializers for reference"; |
| 5040 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5042 | case FK_ArrayNeedsInitList: |
| 5043 | OS << "array requires initializer list"; |
| 5044 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5045 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5046 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5047 | OS << "array requires initializer list or string literal"; |
| 5048 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5050 | case FK_ArrayTypeMismatch: |
| 5051 | OS << "array type mismatch"; |
| 5052 | break; |
| 5053 | |
| 5054 | case FK_NonConstantArrayInit: |
| 5055 | OS << "non-constant array initializer"; |
| 5056 | break; |
| 5057 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5058 | case FK_AddressOfOverloadFailed: |
| 5059 | OS << "address of overloaded function failed"; |
| 5060 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5061 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5062 | case FK_ReferenceInitOverloadFailed: |
| 5063 | OS << "overload resolution for reference initialization failed"; |
| 5064 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5065 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5066 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5067 | OS << "non-const lvalue reference bound to temporary"; |
| 5068 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5069 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5070 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5071 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5072 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5074 | case FK_RValueReferenceBindingToLValue: |
| 5075 | OS << "rvalue reference bound to an lvalue"; |
| 5076 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5077 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5078 | case FK_ReferenceInitDropsQualifiers: |
| 5079 | OS << "reference initialization drops qualifiers"; |
| 5080 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5081 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5082 | case FK_ReferenceInitFailed: |
| 5083 | OS << "reference initialization failed"; |
| 5084 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5085 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5086 | case FK_ConversionFailed: |
| 5087 | OS << "conversion failed"; |
| 5088 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5090 | case FK_ConversionFromPropertyFailed: |
| 5091 | OS << "conversion from property failed"; |
| 5092 | break; |
| 5093 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5094 | case FK_TooManyInitsForScalar: |
| 5095 | OS << "too many initializers for scalar"; |
| 5096 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5097 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5098 | case FK_ReferenceBindingToInitList: |
| 5099 | OS << "referencing binding to initializer list"; |
| 5100 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5102 | case FK_InitListBadDestinationType: |
| 5103 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5104 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5106 | case FK_UserConversionOverloadFailed: |
| 5107 | OS << "overloading failed for user-defined conversion"; |
| 5108 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5109 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5110 | case FK_ConstructorOverloadFailed: |
| 5111 | OS << "constructor overloading failed"; |
| 5112 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5113 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5114 | case FK_DefaultInitOfConst: |
| 5115 | OS << "default initialization of a const variable"; |
| 5116 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5117 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5118 | case FK_Incomplete: |
| 5119 | OS << "initialization of incomplete type"; |
| 5120 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5122 | OS << '\n'; |
| 5123 | return; |
| 5124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5125 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5126 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5127 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5128 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5129 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5130 | case NormalSequence: |
| 5131 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5132 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5133 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5135 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5136 | if (S != step_begin()) { |
| 5137 | OS << " -> "; |
| 5138 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5140 | switch (S->Kind) { |
| 5141 | case SK_ResolveAddressOfOverloadedFunction: |
| 5142 | OS << "resolve address of overloaded function"; |
| 5143 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5144 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5145 | case SK_CastDerivedToBaseRValue: |
| 5146 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5147 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5148 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5149 | case SK_CastDerivedToBaseXValue: |
| 5150 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5151 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5153 | case SK_CastDerivedToBaseLValue: |
| 5154 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5155 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5156 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5157 | case SK_BindReference: |
| 5158 | OS << "bind reference to lvalue"; |
| 5159 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5160 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5161 | case SK_BindReferenceToTemporary: |
| 5162 | OS << "bind reference to a temporary"; |
| 5163 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5165 | case SK_ExtraneousCopyToTemporary: |
| 5166 | OS << "extraneous C++03 copy to temporary"; |
| 5167 | break; |
| 5168 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5169 | case SK_UserConversion: |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5170 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5171 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5172 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5173 | case SK_QualificationConversionRValue: |
| 5174 | OS << "qualification conversion (rvalue)"; |
| 5175 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5176 | case SK_QualificationConversionXValue: |
| 5177 | OS << "qualification conversion (xvalue)"; |
| 5178 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5179 | case SK_QualificationConversionLValue: |
| 5180 | OS << "qualification conversion (lvalue)"; |
| 5181 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5183 | case SK_ConversionSequence: |
| 5184 | OS << "implicit conversion sequence ("; |
| 5185 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5186 | OS << ")"; |
| 5187 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5189 | case SK_ListInitialization: |
| 5190 | OS << "list initialization"; |
| 5191 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5193 | case SK_ConstructorInitialization: |
| 5194 | OS << "constructor initialization"; |
| 5195 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5196 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5197 | case SK_ZeroInitialization: |
| 5198 | OS << "zero initialization"; |
| 5199 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5200 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5201 | case SK_CAssignment: |
| 5202 | OS << "C assignment"; |
| 5203 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5204 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5205 | case SK_StringInit: |
| 5206 | OS << "string initialization"; |
| 5207 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5208 | |
| 5209 | case SK_ObjCObjectConversion: |
| 5210 | OS << "Objective-C object conversion"; |
| 5211 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5212 | |
| 5213 | case SK_ArrayInit: |
| 5214 | OS << "array initialization"; |
| 5215 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5216 | |
| 5217 | case SK_PassByIndirectCopyRestore: |
| 5218 | OS << "pass by indirect copy and restore"; |
| 5219 | break; |
| 5220 | |
| 5221 | case SK_PassByIndirectRestore: |
| 5222 | OS << "pass by indirect restore"; |
| 5223 | break; |
| 5224 | |
| 5225 | case SK_ProduceObjCObject: |
| 5226 | OS << "Objective-C object retension"; |
| 5227 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5228 | } |
| 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | void InitializationSequence::dump() const { |
| 5233 | dump(llvm::errs()); |
| 5234 | } |
| 5235 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5236 | static void DiagnoseNarrowingInInitList( |
| 5237 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5238 | bool Constant, const APValue &ConstantValue) { |
| 5239 | if (Constant) { |
| 5240 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5241 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5242 | ? diag::err_init_list_constant_narrowing |
| 5243 | : diag::warn_init_list_constant_narrowing) |
| 5244 | << InitE->getSourceRange() |
| 5245 | << ConstantValue |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5246 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5247 | } else |
| 5248 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5249 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5250 | ? diag::err_init_list_variable_narrowing |
| 5251 | : diag::warn_init_list_variable_narrowing) |
| 5252 | << InitE->getSourceRange() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5253 | << InitE->getType().getLocalUnqualifiedType() |
| 5254 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5255 | |
| 5256 | llvm::SmallString<128> StaticCast; |
| 5257 | llvm::raw_svector_ostream OS(StaticCast); |
| 5258 | OS << "static_cast<"; |
| 5259 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5260 | // It's important to use the typedef's name if there is one so that the |
| 5261 | // fixit doesn't break code using types like int64_t. |
| 5262 | // |
| 5263 | // FIXME: This will break if the typedef requires qualification. But |
| 5264 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5265 | OS << TT->getDecl(); |
| 5266 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5267 | OS << BT->getName(S.getLangOptions()); |
| 5268 | else { |
| 5269 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5270 | // with a broken cast. |
| 5271 | return; |
| 5272 | } |
| 5273 | OS << ">("; |
| 5274 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5275 | << InitE->getSourceRange() |
| 5276 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5277 | << FixItHint::CreateInsertion( |
| 5278 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5279 | } |
| 5280 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5281 | //===----------------------------------------------------------------------===// |
| 5282 | // Initialization helper functions |
| 5283 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5284 | bool |
| 5285 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5286 | ExprResult Init) { |
| 5287 | if (Init.isInvalid()) |
| 5288 | return false; |
| 5289 | |
| 5290 | Expr *InitE = Init.get(); |
| 5291 | assert(InitE && "No initialization expression"); |
| 5292 | |
| 5293 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5294 | SourceLocation()); |
| 5295 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5296 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5299 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5300 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5301 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5302 | ExprResult Init, |
| 5303 | bool TopLevelOfInitList) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5304 | if (Init.isInvalid()) |
| 5305 | return ExprError(); |
| 5306 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5307 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5308 | assert(InitE && "No initialization expression?"); |
| 5309 | |
| 5310 | if (EqualLoc.isInvalid()) |
| 5311 | EqualLoc = InitE->getLocStart(); |
| 5312 | |
| 5313 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5314 | EqualLoc); |
| 5315 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5316 | Init.release(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5317 | |
| 5318 | bool Constant = false; |
| 5319 | APValue Result; |
| 5320 | if (TopLevelOfInitList && |
| 5321 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5322 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5323 | Constant, Result); |
| 5324 | } |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5325 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5326 | } |