Steve Naroff | 0cca749 | 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 | // |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | 8879e3b | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8879e3b | 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 | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 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 | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 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 | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | ce6c9b7 | 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 | fef8b34 | 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 | 79e079d | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 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 | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 95 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 96 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 97 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 98 | ConstVal, |
| 99 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 100 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 103 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 105 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 106 | // the size may be smaller or larger than the string we are initializing. |
| 107 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 108 | if (S.getLangOpts().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 109 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 110 | // For Pascal strings it's OK to strip off the terminating null character, |
| 111 | // so the example below is valid: |
| 112 | // |
| 113 | // unsigned char a[2] = "\pa"; |
| 114 | if (SL->isPascal()) |
| 115 | StrLength--; |
| 116 | } |
| 117 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 118 | // [dcl.init.string]p2 |
| 119 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 120 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 121 | diag::err_initializer_string_for_char_array_too_long) |
| 122 | << Str->getSourceRange(); |
| 123 | } else { |
| 124 | // C99 6.7.8p14. |
| 125 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 126 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 127 | diag::warn_initializer_string_for_char_array_too_long) |
| 128 | << Str->getSourceRange(); |
| 129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 131 | // Set the type to the actual size that we are initializing. If we have |
| 132 | // something like: |
| 133 | // char x[1] = "foo"; |
| 134 | // then this will set the string literal's type to char[1]. |
| 135 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
| 139 | // Semantic checking for initializer lists. |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 142 | /// @brief Semantic checking for initializer lists. |
| 143 | /// |
| 144 | /// The InitListChecker class contains a set of routines that each |
| 145 | /// handle the initialization of a certain kind of entity, e.g., |
| 146 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 147 | /// InitListChecker itself performs a recursive walk of the subobject |
| 148 | /// structure of the type to be initialized, while stepping through |
| 149 | /// the initializer list one element at a time. The IList and Index |
| 150 | /// parameters to each of the Check* routines contain the active |
| 151 | /// (syntactic) initializer list and the index into that initializer |
| 152 | /// list that represents the current initializer. Each routine is |
| 153 | /// responsible for moving that Index forward as it consumes elements. |
| 154 | /// |
| 155 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 156 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 157 | /// initializer list and the index into that initializer list where we |
| 158 | /// are copying initializers as we map them over to the semantic |
| 159 | /// list. Once we have completed our recursive walk of the subobject |
| 160 | /// structure, we will have constructed a full semantic initializer |
| 161 | /// list. |
| 162 | /// |
| 163 | /// C99 designators cause changes in the initializer list traversal, |
| 164 | /// because they make the initialization "jump" into a specific |
| 165 | /// subobject and then continue the initialization from that |
| 166 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 167 | /// designated subobject and manages backing out the recursion to |
| 168 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 169 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 170 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 171 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 172 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 173 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 174 | bool AllowBraceElision; |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 175 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 176 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 178 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 179 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 180 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 181 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 182 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 183 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 184 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 185 | unsigned &StructuredIndex, |
| 186 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 187 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 188 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 190 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 191 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 192 | unsigned &StructuredIndex, |
| 193 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 194 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 195 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 196 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 197 | InitListExpr *StructuredList, |
| 198 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 199 | void CheckComplexType(const InitializedEntity &Entity, |
| 200 | InitListExpr *IList, QualType DeclType, |
| 201 | unsigned &Index, |
| 202 | InitListExpr *StructuredList, |
| 203 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 204 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 205 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 206 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | InitListExpr *StructuredList, |
| 208 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 209 | void CheckReferenceType(const InitializedEntity &Entity, |
| 210 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 211 | unsigned &Index, |
| 212 | InitListExpr *StructuredList, |
| 213 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 214 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 215 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 216 | InitListExpr *StructuredList, |
| 217 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 218 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 219 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 221 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 223 | unsigned &StructuredIndex, |
| 224 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 225 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 226 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 228 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 229 | InitListExpr *StructuredList, |
| 230 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 231 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 232 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 233 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | RecordDecl::field_iterator *NextField, |
| 236 | llvm::APSInt *NextElementIndex, |
| 237 | unsigned &Index, |
| 238 | InitListExpr *StructuredList, |
| 239 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 240 | bool FinishSubobjectInit, |
| 241 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 242 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 243 | QualType CurrentObjectType, |
| 244 | InitListExpr *StructuredList, |
| 245 | unsigned StructuredIndex, |
| 246 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 247 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 248 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 249 | Expr *expr); |
| 250 | int numArrayElements(QualType DeclType); |
| 251 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 253 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 254 | const InitializedEntity &ParentEntity, |
| 255 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 256 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 257 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 258 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 259 | Expr *InitExpr, FieldDecl *Field, |
| 260 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 261 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 262 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 263 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 264 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 265 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 266 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 267 | bool HadError() { return hadError; } |
| 268 | |
| 269 | // @brief Retrieves the fully-structured initializer list used for |
| 270 | // semantic analysis and code generation. |
| 271 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 272 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 273 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 274 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 275 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 276 | assert(VerifyOnly && |
| 277 | "CheckValueInitializable is only inteded for verification mode."); |
| 278 | |
| 279 | SourceLocation Loc; |
| 280 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 281 | true); |
| 282 | InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0); |
| 283 | if (InitSeq.Failed()) |
| 284 | hadError = true; |
| 285 | } |
| 286 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 287 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 288 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 289 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 290 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 291 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 292 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 293 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 294 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 295 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 296 | // FIXME: We probably don't need to handle references |
| 297 | // specially here, since value-initialization of references is |
| 298 | // handled in InitializationSequence. |
| 299 | if (Field->getType()->isReferenceType()) { |
| 300 | // C++ [dcl.init.aggr]p9: |
| 301 | // If an incomplete or empty initializer-list leaves a |
| 302 | // member of reference type uninitialized, the program is |
| 303 | // ill-formed. |
| 304 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 305 | << Field->getType() |
| 306 | << ILE->getSyntacticForm()->getSourceRange(); |
| 307 | SemaRef.Diag(Field->getLocation(), |
| 308 | diag::note_uninit_reference_member); |
| 309 | hadError = true; |
| 310 | return; |
| 311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 313 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 314 | true); |
| 315 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 316 | if (!InitSeq) { |
| 317 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 318 | hadError = true; |
| 319 | return; |
| 320 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 321 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 322 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 323 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 324 | if (MemberInit.isInvalid()) { |
| 325 | hadError = true; |
| 326 | return; |
| 327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 329 | if (hadError) { |
| 330 | // Do nothing |
| 331 | } else if (Init < NumInits) { |
| 332 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 333 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 334 | // Value-initialization requires a constructor call, so |
| 335 | // extend the initializer list to include the constructor |
| 336 | // call and make a note that we'll need to take another pass |
| 337 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 338 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 339 | RequiresSecondPass = true; |
| 340 | } |
| 341 | } else if (InitListExpr *InnerILE |
| 342 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | FillInValueInitializations(MemberEntity, InnerILE, |
| 344 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 347 | /// Recursively replaces NULL values within the given initializer list |
| 348 | /// with expressions that perform value-initialization of the |
| 349 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 350 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 351 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 352 | InitListExpr *ILE, |
| 353 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 355 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 356 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 357 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 358 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 360 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 361 | if (RType->getDecl()->isUnion() && |
| 362 | ILE->getInitializedFieldInUnion()) |
| 363 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 364 | Entity, ILE, RequiresSecondPass); |
| 365 | else { |
| 366 | unsigned Init = 0; |
| 367 | for (RecordDecl::field_iterator |
| 368 | Field = RType->getDecl()->field_begin(), |
| 369 | FieldEnd = RType->getDecl()->field_end(); |
| 370 | Field != FieldEnd; ++Field) { |
| 371 | if (Field->isUnnamedBitfield()) |
| 372 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 374 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 375 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 376 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 377 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 378 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 379 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 381 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 383 | // Only look at the first initialization of a union. |
| 384 | if (RType->getDecl()->isUnion()) |
| 385 | break; |
| 386 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 391 | |
| 392 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 394 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 395 | unsigned NumInits = ILE->getNumInits(); |
| 396 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 397 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 398 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 399 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 400 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 401 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 402 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 403 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 404 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 405 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 406 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 407 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 409 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 412 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 413 | if (hadError) |
| 414 | return; |
| 415 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 416 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 417 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 418 | ElementEntity.setElementIndex(Init); |
| 419 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 420 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 421 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 422 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 423 | true); |
| 424 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 425 | if (!InitSeq) { |
| 426 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 427 | hadError = true; |
| 428 | return; |
| 429 | } |
| 430 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 431 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 432 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 433 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 434 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | |
| 438 | if (hadError) { |
| 439 | // Do nothing |
| 440 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 441 | // For arrays, just set the expression used for value-initialization |
| 442 | // of the "holes" in the array. |
| 443 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 444 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 445 | else |
| 446 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 447 | } else { |
| 448 | // For arrays, just set the expression used for value-initialization |
| 449 | // of the rest of elements and exit. |
| 450 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 451 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 452 | return; |
| 453 | } |
| 454 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 455 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 456 | // Value-initialization requires a constructor call, so |
| 457 | // extend the initializer list to include the constructor |
| 458 | // call and make a note that we'll need to take another pass |
| 459 | // through the initializer list. |
| 460 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 461 | RequiresSecondPass = true; |
| 462 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 463 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 464 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 465 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 466 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 471 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 472 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 473 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 474 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 475 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 476 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 477 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 478 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 480 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 481 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 482 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 483 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 484 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 485 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 486 | bool RequiresSecondPass = false; |
| 487 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 488 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 489 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 490 | RequiresSecondPass); |
| 491 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 495 | // FIXME: use a proper constant |
| 496 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 497 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 498 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 499 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 500 | } |
| 501 | return maxElements; |
| 502 | } |
| 503 | |
| 504 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 505 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 506 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 508 | Field = structDecl->field_begin(), |
| 509 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 510 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 511 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 512 | ++InitializableMembers; |
| 513 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 514 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 515 | return std::min(InitializableMembers, 1); |
| 516 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 519 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 520 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 521 | QualType T, unsigned &Index, |
| 522 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 523 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 524 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 526 | if (T->isArrayType()) |
| 527 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 528 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 529 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 530 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 531 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 532 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 533 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 534 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 535 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 536 | if (!VerifyOnly) |
| 537 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 538 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 539 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 540 | hadError = true; |
| 541 | return; |
| 542 | } |
| 543 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 544 | // Build a structured initializer list corresponding to this subobject. |
| 545 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 547 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 548 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 549 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 550 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 551 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 552 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 553 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 555 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 557 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 558 | |
| 559 | if (VerifyOnly) { |
| 560 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 561 | hadError = true; |
| 562 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 563 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 564 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 565 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 566 | // Update the structured sub-object initializer so that it's ending |
| 567 | // range corresponds with the end of the last initializer it used. |
| 568 | if (EndIndex < ParentIList->getNumInits()) { |
| 569 | SourceLocation EndLoc |
| 570 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 571 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 572 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 573 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 574 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 575 | if (T->isArrayType() || T->isRecordType()) { |
| 576 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 577 | AllowBraceElision ? diag::warn_missing_braces : |
| 578 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 579 | << StructuredSubobjectInitList->getSourceRange() |
| 580 | << FixItHint::CreateInsertion( |
| 581 | StructuredSubobjectInitList->getLocStart(), "{") |
| 582 | << FixItHint::CreateInsertion( |
| 583 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 584 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 585 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 586 | if (!AllowBraceElision) |
| 587 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 588 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 589 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 592 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 593 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 594 | unsigned &Index, |
| 595 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 596 | unsigned &StructuredIndex, |
| 597 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 598 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 599 | if (!VerifyOnly) { |
| 600 | SyntacticToSemantic[IList] = StructuredList; |
| 601 | StructuredList->setSyntacticForm(IList); |
| 602 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 603 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 604 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 605 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 606 | QualType ExprTy = T; |
| 607 | if (!ExprTy->isArrayType()) |
| 608 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 609 | IList->setType(ExprTy); |
| 610 | StructuredList->setType(ExprTy); |
| 611 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 612 | if (hadError) |
| 613 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 614 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 615 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 616 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 617 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 618 | if (SemaRef.getLangOpts().CPlusPlus || |
| 619 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 620 | IList->getType()->isVectorType())) { |
| 621 | hadError = true; |
| 622 | } |
| 623 | return; |
| 624 | } |
| 625 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 626 | if (StructuredIndex == 1 && |
| 627 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 628 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 629 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 630 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 631 | hadError = true; |
| 632 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 633 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 634 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 635 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 636 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 637 | // Don't complain for incomplete types, since we'll get an error |
| 638 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 639 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 641 | CurrentObjectType->isArrayType()? 0 : |
| 642 | CurrentObjectType->isVectorType()? 1 : |
| 643 | CurrentObjectType->isScalarType()? 2 : |
| 644 | CurrentObjectType->isUnionType()? 3 : |
| 645 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 646 | |
| 647 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 648 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 649 | DK = diag::err_excess_initializers; |
| 650 | hadError = true; |
| 651 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 652 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 653 | DK = diag::err_excess_initializers; |
| 654 | hadError = true; |
| 655 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 657 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 658 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 661 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 662 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 663 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 664 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 665 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 666 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 667 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 670 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 671 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 673 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 674 | unsigned &Index, |
| 675 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 676 | unsigned &StructuredIndex, |
| 677 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 678 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 679 | // Explicitly braced initializer for complex type can be real+imaginary |
| 680 | // parts. |
| 681 | CheckComplexType(Entity, IList, DeclType, Index, |
| 682 | StructuredList, StructuredIndex); |
| 683 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 684 | CheckScalarType(Entity, IList, DeclType, Index, |
| 685 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 686 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 688 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 689 | } else if (DeclType->isRecordType()) { |
| 690 | assert(DeclType->isAggregateType() && |
| 691 | "non-aggregate records should be handed in CheckSubElementType"); |
| 692 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 693 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 694 | SubobjectIsDesignatorContext, Index, |
| 695 | StructuredList, StructuredIndex, |
| 696 | TopLevelObject); |
| 697 | } else if (DeclType->isArrayType()) { |
| 698 | llvm::APSInt Zero( |
| 699 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 700 | false); |
| 701 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 702 | SubobjectIsDesignatorContext, Index, |
| 703 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 704 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 705 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 706 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 707 | if (!VerifyOnly) |
| 708 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 709 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 710 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 711 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 712 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 713 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 714 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 715 | if (!VerifyOnly) |
| 716 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 717 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 718 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 719 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 720 | if (!VerifyOnly) |
| 721 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 722 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 723 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 727 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 728 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 730 | unsigned &Index, |
| 731 | InitListExpr *StructuredList, |
| 732 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 733 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 734 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 735 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 736 | unsigned newIndex = 0; |
| 737 | unsigned newStructuredIndex = 0; |
| 738 | InitListExpr *newStructuredList |
| 739 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 740 | StructuredList, StructuredIndex, |
| 741 | SubInitList->getSourceRange()); |
| 742 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 743 | newStructuredList, newStructuredIndex); |
| 744 | ++StructuredIndex; |
| 745 | ++Index; |
| 746 | return; |
| 747 | } |
| 748 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 749 | "non-aggregate records are only possible in C++"); |
| 750 | // C++ initialization is handled later. |
| 751 | } |
| 752 | |
| 753 | if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 754 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 755 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 756 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 757 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 758 | StructuredList, StructuredIndex); |
| 759 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 760 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 761 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 762 | // arrayType can be incomplete if we're initializing a flexible |
| 763 | // array member. There's nothing we can do with the completed |
| 764 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 766 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 767 | if (!VerifyOnly) { |
| 768 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 769 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 770 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 771 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 772 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 773 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 774 | |
| 775 | // Fall through for subaggregate initialization. |
| 776 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 777 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 778 | // C++ [dcl.init.aggr]p12: |
| 779 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 780 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 781 | // an initializer-list. If the initializer can initialize a |
| 782 | // member, the member is initialized. [...] |
| 783 | |
| 784 | // FIXME: Better EqualLoc? |
| 785 | InitializationKind Kind = |
| 786 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 787 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 788 | |
| 789 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 790 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 791 | ExprResult Result = |
| 792 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 793 | if (Result.isInvalid()) |
| 794 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 795 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 796 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 797 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 798 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 799 | ++Index; |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | // Fall through for subaggregate initialization |
| 804 | } else { |
| 805 | // C99 6.7.8p13: |
| 806 | // |
| 807 | // The initializer for a structure or union object that has |
| 808 | // automatic storage duration shall be either an initializer |
| 809 | // list as described below, or a single expression that has |
| 810 | // compatible structure or union type. In the latter case, the |
| 811 | // initial value of the object, including unnamed members, is |
| 812 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 813 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 814 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 815 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 816 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 817 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 818 | if (ExprRes.isInvalid()) |
| 819 | hadError = true; |
| 820 | else { |
| 821 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 822 | if (ExprRes.isInvalid()) |
| 823 | hadError = true; |
| 824 | } |
| 825 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 826 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 827 | ++Index; |
| 828 | return; |
| 829 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 830 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 831 | // Fall through for subaggregate initialization |
| 832 | } |
| 833 | |
| 834 | // C++ [dcl.init.aggr]p12: |
| 835 | // |
| 836 | // [...] Otherwise, if the member is itself a non-empty |
| 837 | // subaggregate, brace elision is assumed and the initializer is |
| 838 | // considered for the initialization of the first member of |
| 839 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 840 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 841 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 842 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 843 | StructuredIndex); |
| 844 | ++StructuredIndex; |
| 845 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 846 | if (!VerifyOnly) { |
| 847 | // We cannot initialize this element, so let |
| 848 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 849 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 850 | SemaRef.Owned(expr), |
| 851 | /*TopLevelOfInitList=*/true); |
| 852 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 853 | hadError = true; |
| 854 | ++Index; |
| 855 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 859 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 860 | InitListExpr *IList, QualType DeclType, |
| 861 | unsigned &Index, |
| 862 | InitListExpr *StructuredList, |
| 863 | unsigned &StructuredIndex) { |
| 864 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 865 | |
| 866 | // As an extension, clang supports complex initializers, which initialize |
| 867 | // a complex number component-wise. When an explicit initializer list for |
| 868 | // a complex number contains two two initializers, this extension kicks in: |
| 869 | // it exepcts the initializer list to contain two elements convertible to |
| 870 | // the element type of the complex type. The first element initializes |
| 871 | // the real part, and the second element intitializes the imaginary part. |
| 872 | |
| 873 | if (IList->getNumInits() != 2) |
| 874 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 875 | StructuredIndex); |
| 876 | |
| 877 | // This is an extension in C. (The builtin _Complex type does not exist |
| 878 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 879 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 880 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 881 | << IList->getSourceRange(); |
| 882 | |
| 883 | // Initialize the complex number. |
| 884 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 885 | InitializedEntity ElementEntity = |
| 886 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 887 | |
| 888 | for (unsigned i = 0; i < 2; ++i) { |
| 889 | ElementEntity.setElementIndex(Index); |
| 890 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 891 | StructuredList, StructuredIndex); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 896 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 897 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 898 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 899 | InitListExpr *StructuredList, |
| 900 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 901 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 902 | if (!VerifyOnly) |
| 903 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 904 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 905 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 906 | diag::err_empty_scalar_initializer) |
| 907 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 908 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 909 | ++Index; |
| 910 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 911 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 912 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 913 | |
| 914 | Expr *expr = IList->getInit(Index); |
| 915 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 916 | if (!VerifyOnly) |
| 917 | SemaRef.Diag(SubIList->getLocStart(), |
| 918 | diag::warn_many_braces_around_scalar_init) |
| 919 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 920 | |
| 921 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 922 | StructuredIndex); |
| 923 | return; |
| 924 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 925 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 926 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 927 | diag::err_designator_for_scalar_init) |
| 928 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 929 | hadError = true; |
| 930 | ++Index; |
| 931 | ++StructuredIndex; |
| 932 | return; |
| 933 | } |
| 934 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 935 | if (VerifyOnly) { |
| 936 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 937 | hadError = true; |
| 938 | ++Index; |
| 939 | return; |
| 940 | } |
| 941 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 942 | ExprResult Result = |
| 943 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 944 | SemaRef.Owned(expr), |
| 945 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 946 | |
| 947 | Expr *ResultExpr = 0; |
| 948 | |
| 949 | if (Result.isInvalid()) |
| 950 | hadError = true; // types weren't compatible. |
| 951 | else { |
| 952 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 954 | if (ResultExpr != expr) { |
| 955 | // The type was promoted, update initializer list. |
| 956 | IList->setInit(Index, ResultExpr); |
| 957 | } |
| 958 | } |
| 959 | if (hadError) |
| 960 | ++StructuredIndex; |
| 961 | else |
| 962 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 963 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 966 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 967 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 968 | unsigned &Index, |
| 969 | InitListExpr *StructuredList, |
| 970 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 971 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 972 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 973 | // general, it would be useful to pass location information down the stack, |
| 974 | // so that we know the location (or decl) of the "current object" being |
| 975 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 976 | if (!VerifyOnly) |
| 977 | SemaRef.Diag(IList->getLocStart(), |
| 978 | diag::err_init_reference_member_uninitialized) |
| 979 | << DeclType |
| 980 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 981 | hadError = true; |
| 982 | ++Index; |
| 983 | ++StructuredIndex; |
| 984 | return; |
| 985 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 986 | |
| 987 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 988 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 989 | if (!VerifyOnly) |
| 990 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 991 | << DeclType << IList->getSourceRange(); |
| 992 | hadError = true; |
| 993 | ++Index; |
| 994 | ++StructuredIndex; |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | if (VerifyOnly) { |
| 999 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1000 | hadError = true; |
| 1001 | ++Index; |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | ExprResult Result = |
| 1006 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1007 | SemaRef.Owned(expr), |
| 1008 | /*TopLevelOfInitList=*/true); |
| 1009 | |
| 1010 | if (Result.isInvalid()) |
| 1011 | hadError = true; |
| 1012 | |
| 1013 | expr = Result.takeAs<Expr>(); |
| 1014 | IList->setInit(Index, expr); |
| 1015 | |
| 1016 | if (hadError) |
| 1017 | ++StructuredIndex; |
| 1018 | else |
| 1019 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1020 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1023 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1024 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1025 | unsigned &Index, |
| 1026 | InitListExpr *StructuredList, |
| 1027 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1028 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1029 | unsigned maxElements = VT->getNumElements(); |
| 1030 | unsigned numEltsInit = 0; |
| 1031 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1032 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1033 | if (Index >= IList->getNumInits()) { |
| 1034 | // Make sure the element type can be value-initialized. |
| 1035 | if (VerifyOnly) |
| 1036 | CheckValueInitializable( |
| 1037 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1041 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1042 | // If the initializing element is a vector, try to copy-initialize |
| 1043 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1044 | Expr *Init = IList->getInit(Index); |
| 1045 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1046 | if (VerifyOnly) { |
| 1047 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1048 | hadError = true; |
| 1049 | ++Index; |
| 1050 | return; |
| 1051 | } |
| 1052 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1053 | ExprResult Result = |
| 1054 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1055 | SemaRef.Owned(Init), |
| 1056 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1057 | |
| 1058 | Expr *ResultExpr = 0; |
| 1059 | if (Result.isInvalid()) |
| 1060 | hadError = true; // types weren't compatible. |
| 1061 | else { |
| 1062 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1063 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1064 | if (ResultExpr != Init) { |
| 1065 | // The type was promoted, update initializer list. |
| 1066 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1069 | if (hadError) |
| 1070 | ++StructuredIndex; |
| 1071 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1072 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1073 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1074 | ++Index; |
| 1075 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1078 | InitializedEntity ElementEntity = |
| 1079 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1080 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1081 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1082 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1083 | if (Index >= IList->getNumInits()) { |
| 1084 | if (VerifyOnly) |
| 1085 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1086 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1087 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1089 | ElementEntity.setElementIndex(Index); |
| 1090 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1091 | StructuredList, StructuredIndex); |
| 1092 | } |
| 1093 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1094 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1095 | |
| 1096 | InitializedEntity ElementEntity = |
| 1097 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1099 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1100 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1101 | // Don't attempt to go past the end of the init list |
| 1102 | if (Index >= IList->getNumInits()) |
| 1103 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1104 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1105 | ElementEntity.setElementIndex(Index); |
| 1106 | |
| 1107 | QualType IType = IList->getInit(Index)->getType(); |
| 1108 | if (!IType->isVectorType()) { |
| 1109 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1110 | StructuredList, StructuredIndex); |
| 1111 | ++numEltsInit; |
| 1112 | } else { |
| 1113 | QualType VecType; |
| 1114 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1115 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1117 | if (IType->isExtVectorType()) |
| 1118 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1119 | else |
| 1120 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1121 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1122 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1123 | StructuredList, StructuredIndex); |
| 1124 | numEltsInit += numIElts; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1129 | if (numEltsInit != maxElements) { |
| 1130 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1131 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1132 | diag::err_vector_incorrect_num_initializers) |
| 1133 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1134 | hadError = true; |
| 1135 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1138 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1139 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1140 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1142 | unsigned &Index, |
| 1143 | InitListExpr *StructuredList, |
| 1144 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1145 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1146 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1147 | // Check for the special-case of initializing an array with a string. |
| 1148 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1149 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1150 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1151 | // We place the string literal directly into the resulting |
| 1152 | // initializer list. This is the only place where the structure |
| 1153 | // of the structured initializer list doesn't match exactly, |
| 1154 | // because doing so would involve allocating one character |
| 1155 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1156 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1157 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1158 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1159 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1160 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1161 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
| 1164 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1165 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1166 | // Check for VLAs; in standard C it would be possible to check this |
| 1167 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1168 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1169 | if (!VerifyOnly) |
| 1170 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1171 | diag::err_variable_object_no_init) |
| 1172 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1173 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1174 | ++Index; |
| 1175 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1176 | return; |
| 1177 | } |
| 1178 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1179 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1180 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1181 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1182 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1183 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1184 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1185 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1186 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | maxElementsKnown = true; |
| 1188 | } |
| 1189 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1190 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1191 | while (Index < IList->getNumInits()) { |
| 1192 | Expr *Init = IList->getInit(Index); |
| 1193 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1194 | // If we're not the subobject that matches up with the '{' for |
| 1195 | // the designator, we shouldn't be handling the |
| 1196 | // designator. Return immediately. |
| 1197 | if (!SubobjectIsDesignatorContext) |
| 1198 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1200 | // Handle this designated initializer. elementIndex will be |
| 1201 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1202 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1203 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1204 | StructuredList, StructuredIndex, true, |
| 1205 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1206 | hadError = true; |
| 1207 | continue; |
| 1208 | } |
| 1209 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1210 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1211 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1212 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1213 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1214 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1216 | // If the array is of incomplete type, keep track of the number of |
| 1217 | // elements in the initializer. |
| 1218 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1219 | maxElements = elementIndex; |
| 1220 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | } |
| 1223 | |
| 1224 | // If we know the maximum number of elements, and we've already |
| 1225 | // hit it, stop consuming elements in the initializer list. |
| 1226 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1227 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1228 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1229 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1230 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1231 | Entity); |
| 1232 | // Check this element. |
| 1233 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1234 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1235 | ++elementIndex; |
| 1236 | |
| 1237 | // If the array is of incomplete type, keep track of the number of |
| 1238 | // elements in the initializer. |
| 1239 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1240 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1241 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1242 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1243 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1244 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1245 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1246 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1247 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1248 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1249 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1250 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1251 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1252 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1254 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1255 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1256 | if (!hadError && VerifyOnly) { |
| 1257 | // Check if there are any members of the array that get value-initialized. |
| 1258 | // If so, check if doing that is possible. |
| 1259 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1260 | if (maxElementsKnown && elementIndex < maxElements) |
| 1261 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1262 | SemaRef.Context, 0, Entity)); |
| 1263 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1266 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1267 | Expr *InitExpr, |
| 1268 | FieldDecl *Field, |
| 1269 | bool TopLevelObject) { |
| 1270 | // Handle GNU flexible array initializers. |
| 1271 | unsigned FlexArrayDiag; |
| 1272 | if (isa<InitListExpr>(InitExpr) && |
| 1273 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1274 | // Empty flexible array init always allowed as an extension |
| 1275 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1276 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1277 | // Disallow flexible array init in C++; it is not required for gcc |
| 1278 | // compatibility, and it needs work to IRGen correctly in general. |
| 1279 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1280 | } else if (!TopLevelObject) { |
| 1281 | // Disallow flexible array init on non-top-level object |
| 1282 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1283 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1284 | // Disallow flexible array init on anything which is not a variable. |
| 1285 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1286 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1287 | // Disallow flexible array init on local variables. |
| 1288 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1289 | } else { |
| 1290 | // Allow other cases. |
| 1291 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1292 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1293 | |
| 1294 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1295 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1296 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1297 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1298 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1299 | << Field; |
| 1300 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1301 | |
| 1302 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1303 | } |
| 1304 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1305 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1306 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1308 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1310 | unsigned &Index, |
| 1311 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1312 | unsigned &StructuredIndex, |
| 1313 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1314 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1316 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1317 | // confusion, we forgo checking the intializer for the entire record. |
| 1318 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1319 | // Assume it was supposed to consume a single initializer. |
| 1320 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1321 | hadError = true; |
| 1322 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1324 | |
| 1325 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1326 | // Value-initialize the first named member of the union. |
| 1327 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1328 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1329 | Field != FieldEnd; ++Field) { |
| 1330 | if (Field->getDeclName()) { |
| 1331 | if (VerifyOnly) |
| 1332 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1333 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1334 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1335 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1336 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | return; |
| 1340 | } |
| 1341 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1342 | // If structDecl is a forward declaration, this loop won't do |
| 1343 | // anything except look at designated initializers; That's okay, |
| 1344 | // because an error should get printed out elsewhere. It might be |
| 1345 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1346 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1347 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1348 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1349 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1350 | while (Index < IList->getNumInits()) { |
| 1351 | Expr *Init = IList->getInit(Index); |
| 1352 | |
| 1353 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1354 | // If we're not the subobject that matches up with the '{' for |
| 1355 | // the designator, we shouldn't be handling the |
| 1356 | // designator. Return immediately. |
| 1357 | if (!SubobjectIsDesignatorContext) |
| 1358 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1360 | // Handle this designated initializer. Field will be updated to |
| 1361 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1362 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1363 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1364 | StructuredList, StructuredIndex, |
| 1365 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1366 | hadError = true; |
| 1367 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1368 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Disable check for missing fields when designators are used. |
| 1371 | // This matches gcc behaviour. |
| 1372 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1373 | continue; |
| 1374 | } |
| 1375 | |
| 1376 | if (Field == FieldEnd) { |
| 1377 | // We've run out of fields. We're done. |
| 1378 | break; |
| 1379 | } |
| 1380 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1381 | // We've already initialized a member of a union. We're done. |
| 1382 | if (InitializedSomething && DeclType->isUnionType()) |
| 1383 | break; |
| 1384 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1385 | // If we've hit the flexible array member at the end, we're done. |
| 1386 | if (Field->getType()->isIncompleteArrayType()) |
| 1387 | break; |
| 1388 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1389 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1390 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1391 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1392 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1393 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1394 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1395 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1396 | bool InvalidUse; |
| 1397 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1398 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1399 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1400 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1401 | IList->getInit(Index)->getLocStart()); |
| 1402 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1403 | ++Index; |
| 1404 | ++Field; |
| 1405 | hadError = true; |
| 1406 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1407 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1408 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1409 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1410 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1411 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1412 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1413 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1414 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1415 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1416 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1417 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1418 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1419 | |
| 1420 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1421 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1422 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1423 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1424 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1425 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1426 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1427 | // It is possible we have one or more unnamed bitfields remaining. |
| 1428 | // Find first (if any) named field and emit warning. |
| 1429 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1430 | it != end; ++it) { |
| 1431 | if (!it->isUnnamedBitfield()) { |
| 1432 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1433 | diag::warn_missing_field_initializers) << it->getName(); |
| 1434 | break; |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1439 | // Check that any remaining fields can be value-initialized. |
| 1440 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1441 | !Field->getType()->isIncompleteArrayType()) { |
| 1442 | // FIXME: Should check for holes left by designated initializers too. |
| 1443 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1444 | if (!Field->isUnnamedBitfield()) |
| 1445 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1446 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1451 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1452 | return; |
| 1453 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1454 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1455 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1456 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1457 | ++Index; |
| 1458 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1461 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1462 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1463 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1464 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1465 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1466 | StructuredList, StructuredIndex); |
| 1467 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1468 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1469 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1470 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1472 | /// \brief Expand a field designator that refers to a member of an |
| 1473 | /// anonymous struct or union into a series of field designators that |
| 1474 | /// refers to the field within the appropriate subobject. |
| 1475 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1476 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | DesignatedInitExpr *DIE, |
| 1478 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1479 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1480 | typedef DesignatedInitExpr::Designator Designator; |
| 1481 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1482 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1483 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1484 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1485 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1486 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1488 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1489 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1490 | else |
| 1491 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1492 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1493 | assert(isa<FieldDecl>(*PI)); |
| 1494 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // Expand the current designator into the set of replacement |
| 1498 | // designators, so we have a full subobject path down to where the |
| 1499 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1500 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1501 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1504 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1505 | /// corresponds to FieldName. |
| 1506 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1507 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1508 | if (!FieldName) |
| 1509 | return 0; |
| 1510 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1511 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1512 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1513 | while (IndirectFieldDecl *IF = |
| 1514 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1515 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1516 | return IF; |
| 1517 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1518 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1519 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1522 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1523 | DesignatedInitExpr *DIE) { |
| 1524 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1525 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1526 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1527 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1528 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1529 | DIE->size(), IndexExprs, |
| 1530 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1531 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1532 | } |
| 1533 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1534 | namespace { |
| 1535 | |
| 1536 | // Callback to only accept typo corrections that are for field members of |
| 1537 | // the given struct or union. |
| 1538 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1539 | public: |
| 1540 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1541 | : Record(RD) {} |
| 1542 | |
| 1543 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1544 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1545 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1546 | } |
| 1547 | |
| 1548 | private: |
| 1549 | RecordDecl *Record; |
| 1550 | }; |
| 1551 | |
| 1552 | } |
| 1553 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1554 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1555 | /// |
| 1556 | /// Determines whether the designated initializer @p DIE, which |
| 1557 | /// resides at the given @p Index within the initializer list @p |
| 1558 | /// IList, is well-formed for a current object of type @p DeclType |
| 1559 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1561 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1562 | /// |
| 1563 | /// @param IList The initializer list in which this designated |
| 1564 | /// initializer occurs. |
| 1565 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1566 | /// @param DIE The designated initializer expression. |
| 1567 | /// |
| 1568 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1569 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1570 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1571 | /// into which the designation in @p DIE should refer. |
| 1572 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1573 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1574 | /// a field, this will be set to the field declaration corresponding |
| 1575 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1576 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1577 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1578 | /// DIE is an array designator or GNU array-range designator, this |
| 1579 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1580 | /// |
| 1581 | /// @param Index Index into @p IList where the designated initializer |
| 1582 | /// @p DIE occurs. |
| 1583 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1584 | /// @param StructuredList The initializer list expression that |
| 1585 | /// describes all of the subobject initializers in the order they'll |
| 1586 | /// actually be initialized. |
| 1587 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1588 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1590 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1591 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1592 | DesignatedInitExpr *DIE, |
| 1593 | unsigned DesigIdx, |
| 1594 | QualType &CurrentObjectType, |
| 1595 | RecordDecl::field_iterator *NextField, |
| 1596 | llvm::APSInt *NextElementIndex, |
| 1597 | unsigned &Index, |
| 1598 | InitListExpr *StructuredList, |
| 1599 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1600 | bool FinishSubobjectInit, |
| 1601 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1602 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1603 | // Check the actual initialization for the designated object type. |
| 1604 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Temporarily remove the designator expression from the |
| 1607 | // initializer list that the child calls see, so that we don't try |
| 1608 | // to re-process the designator. |
| 1609 | unsigned OldIndex = Index; |
| 1610 | IList->setInit(OldIndex, DIE->getInit()); |
| 1611 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1612 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1613 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1614 | |
| 1615 | // Restore the designated initializer expression in the syntactic |
| 1616 | // form of the initializer list. |
| 1617 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1618 | DIE->setInit(IList->getInit(OldIndex)); |
| 1619 | IList->setInit(OldIndex, DIE); |
| 1620 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1621 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1624 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1625 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1626 | if (!VerifyOnly) { |
| 1627 | assert((IsFirstDesignator || StructuredList) && |
| 1628 | "Need a non-designated initializer list to start from"); |
| 1629 | |
| 1630 | // Determine the structural initializer list that corresponds to the |
| 1631 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1632 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1633 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1634 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1635 | SourceRange(D->getLocStart(), |
| 1636 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1637 | assert(StructuredList && "Expected a structured initializer list"); |
| 1638 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1640 | if (D->isFieldDesignator()) { |
| 1641 | // C99 6.7.8p7: |
| 1642 | // |
| 1643 | // If a designator has the form |
| 1644 | // |
| 1645 | // . identifier |
| 1646 | // |
| 1647 | // then the current object (defined below) shall have |
| 1648 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1650 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1651 | if (!RT) { |
| 1652 | SourceLocation Loc = D->getDotLoc(); |
| 1653 | if (Loc.isInvalid()) |
| 1654 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1655 | if (!VerifyOnly) |
| 1656 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1657 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1658 | ++Index; |
| 1659 | return true; |
| 1660 | } |
| 1661 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1662 | // Note: we perform a linear search of the fields here, despite |
| 1663 | // the fact that we have a faster lookup method, because we always |
| 1664 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1665 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1666 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1667 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1669 | Field = RT->getDecl()->field_begin(), |
| 1670 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1671 | for (; Field != FieldEnd; ++Field) { |
| 1672 | if (Field->isUnnamedBitfield()) |
| 1673 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1675 | // If we find a field representing an anonymous field, look in the |
| 1676 | // IndirectFieldDecl that follow for the designated initializer. |
| 1677 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1678 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1679 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1680 | // In verify mode, don't modify the original. |
| 1681 | if (VerifyOnly) |
| 1682 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1683 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1684 | D = DIE->getDesignator(DesigIdx); |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1688 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1689 | break; |
| 1690 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1691 | break; |
| 1692 | |
| 1693 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1696 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1697 | if (VerifyOnly) { |
| 1698 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1699 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1700 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1702 | // There was no normal field in the struct with the designated |
| 1703 | // name. Perform another lookup for this name, which may find |
| 1704 | // something that we can't designate (e.g., a member function), |
| 1705 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1707 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1708 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1709 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1710 | // Name lookup didn't find anything. Determine whether this |
| 1711 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1712 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1713 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1714 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1715 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1716 | RT->getDecl()); |
| 1717 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1718 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1719 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1720 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1721 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1722 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1723 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1724 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1725 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1726 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1727 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1728 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1729 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1730 | } else { |
| 1731 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1732 | << FieldName << CurrentObjectType; |
| 1733 | ++Index; |
| 1734 | return true; |
| 1735 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1736 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1738 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1739 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1740 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1741 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1742 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1743 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1744 | ++Index; |
| 1745 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1746 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1747 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1748 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1749 | // The replacement field comes from typo correction; find it |
| 1750 | // in the list of fields. |
| 1751 | FieldIndex = 0; |
| 1752 | Field = RT->getDecl()->field_begin(); |
| 1753 | for (; Field != FieldEnd; ++Field) { |
| 1754 | if (Field->isUnnamedBitfield()) |
| 1755 | continue; |
| 1756 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1757 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1758 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1759 | break; |
| 1760 | |
| 1761 | ++FieldIndex; |
| 1762 | } |
| 1763 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1764 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | |
| 1766 | // All of the fields of a union are located at the same place in |
| 1767 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1768 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1769 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1770 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1771 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1772 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1774 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1775 | bool InvalidUse; |
| 1776 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1777 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1778 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1779 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1780 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1781 | ++Index; |
| 1782 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1783 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1784 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1785 | if (!VerifyOnly) { |
| 1786 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1787 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1789 | // Make sure that our non-designated initializer list has space |
| 1790 | // for a subobject corresponding to this field. |
| 1791 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1792 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1793 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1795 | // This designator names a flexible array member. |
| 1796 | if (Field->getType()->isIncompleteArrayType()) { |
| 1797 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1798 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1799 | // We can't designate an object within the flexible array |
| 1800 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1801 | if (!VerifyOnly) { |
| 1802 | DesignatedInitExpr::Designator *NextD |
| 1803 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1804 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1805 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1806 | << SourceRange(NextD->getLocStart(), |
| 1807 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1808 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1809 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1810 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1811 | Invalid = true; |
| 1812 | } |
| 1813 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1814 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1815 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1816 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1817 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1818 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1819 | diag::err_flexible_array_init_needs_braces) |
| 1820 | << DIE->getInit()->getSourceRange(); |
| 1821 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1822 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1823 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1824 | Invalid = true; |
| 1825 | } |
| 1826 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1827 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1828 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1829 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1830 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1831 | |
| 1832 | if (Invalid) { |
| 1833 | ++Index; |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | // Initialize the array. |
| 1838 | bool prevHadError = hadError; |
| 1839 | unsigned newStructuredIndex = FieldIndex; |
| 1840 | unsigned OldIndex = Index; |
| 1841 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1842 | |
| 1843 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1844 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1845 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1846 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1848 | IList->setInit(OldIndex, DIE); |
| 1849 | if (hadError && !prevHadError) { |
| 1850 | ++Field; |
| 1851 | ++FieldIndex; |
| 1852 | if (NextField) |
| 1853 | *NextField = Field; |
| 1854 | StructuredIndex = FieldIndex; |
| 1855 | return true; |
| 1856 | } |
| 1857 | } else { |
| 1858 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1859 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1860 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1861 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1862 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1863 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1864 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1865 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1866 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1867 | true, false)) |
| 1868 | return true; |
| 1869 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1870 | |
| 1871 | // Find the position of the next field to be initialized in this |
| 1872 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1873 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1874 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1875 | |
| 1876 | // If this the first designator, our caller will continue checking |
| 1877 | // the rest of this struct/class/union subobject. |
| 1878 | if (IsFirstDesignator) { |
| 1879 | if (NextField) |
| 1880 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1881 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1885 | if (!FinishSubobjectInit) |
| 1886 | return false; |
| 1887 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1888 | // We've already initialized something in the union; we're done. |
| 1889 | if (RT->getDecl()->isUnion()) |
| 1890 | return hadError; |
| 1891 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1892 | // Check the remaining fields within this class/struct/union subobject. |
| 1893 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1895 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1896 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1897 | return hadError && !prevHadError; |
| 1898 | } |
| 1899 | |
| 1900 | // C99 6.7.8p6: |
| 1901 | // |
| 1902 | // If a designator has the form |
| 1903 | // |
| 1904 | // [ constant-expression ] |
| 1905 | // |
| 1906 | // then the current object (defined below) shall have array |
| 1907 | // type and the expression shall be an integer constant |
| 1908 | // expression. If the array is of unknown size, any |
| 1909 | // nonnegative value is valid. |
| 1910 | // |
| 1911 | // Additionally, cope with the GNU extension that permits |
| 1912 | // designators of the form |
| 1913 | // |
| 1914 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1915 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1916 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1917 | if (!VerifyOnly) |
| 1918 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1919 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1920 | ++Index; |
| 1921 | return true; |
| 1922 | } |
| 1923 | |
| 1924 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1925 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1926 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1927 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1928 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1929 | DesignatedEndIndex = DesignatedStartIndex; |
| 1930 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1931 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1932 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1934 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1936 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1937 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1939 | // Codegen can't handle evaluating array range designators that have side |
| 1940 | // effects, because we replicate the AST value for each initialized element. |
| 1941 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1942 | // elements with something that has a side effect, so codegen can emit an |
| 1943 | // "error unsupported" error instead of miscompiling the app. |
| 1944 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1945 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1946 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1949 | if (isa<ConstantArrayType>(AT)) { |
| 1950 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1951 | DesignatedStartIndex |
| 1952 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1953 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1954 | DesignatedEndIndex |
| 1955 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1956 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1957 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1958 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1959 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1960 | diag::err_array_designator_too_large) |
| 1961 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1962 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1963 | ++Index; |
| 1964 | return true; |
| 1965 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1966 | } else { |
| 1967 | // Make sure the bit-widths and signedness match. |
| 1968 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1969 | DesignatedEndIndex |
| 1970 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1971 | else if (DesignatedStartIndex.getBitWidth() < |
| 1972 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1973 | DesignatedStartIndex |
| 1974 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1975 | DesignatedStartIndex.setIsUnsigned(true); |
| 1976 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1977 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1979 | // Make sure that our non-designated initializer list has space |
| 1980 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1981 | if (!VerifyOnly && |
| 1982 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1984 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1986 | // Repeatedly perform subobject initializations in the range |
| 1987 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1989 | // Move to the next designator |
| 1990 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1991 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1992 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1993 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1994 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1996 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1997 | // Recurse to check later designated subobjects. |
| 1998 | QualType ElementType = AT->getElementType(); |
| 1999 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2000 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2001 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2002 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2003 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2004 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2005 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2006 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2007 | return true; |
| 2008 | |
| 2009 | // Move to the next index in the array that we'll be initializing. |
| 2010 | ++DesignatedStartIndex; |
| 2011 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2012 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2013 | |
| 2014 | // If this the first designator, our caller will continue checking |
| 2015 | // the rest of this array subobject. |
| 2016 | if (IsFirstDesignator) { |
| 2017 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2018 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2019 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2020 | return false; |
| 2021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2023 | if (!FinishSubobjectInit) |
| 2024 | return false; |
| 2025 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2026 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2027 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2028 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2029 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2030 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2034 | // Get the structured initializer list for a subobject of type |
| 2035 | // @p CurrentObjectType. |
| 2036 | InitListExpr * |
| 2037 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2038 | QualType CurrentObjectType, |
| 2039 | InitListExpr *StructuredList, |
| 2040 | unsigned StructuredIndex, |
| 2041 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2042 | if (VerifyOnly) |
| 2043 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2044 | Expr *ExistingInit = 0; |
| 2045 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2046 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2047 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2048 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2050 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2051 | return Result; |
| 2052 | |
| 2053 | if (ExistingInit) { |
| 2054 | // We are creating an initializer list that initializes the |
| 2055 | // subobjects of the current object, but there was already an |
| 2056 | // initialization that completely initialized the current |
| 2057 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2059 | // struct X { int a, b; }; |
| 2060 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2062 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2063 | // designated initializer re-initializes the whole |
| 2064 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2066 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2067 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2068 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2069 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2070 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2071 | << ExistingInit->getSourceRange(); |
| 2072 | } |
| 2073 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2075 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2076 | InitRange.getBegin(), MultiExprArg(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2077 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2078 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2079 | QualType ResultType = CurrentObjectType; |
| 2080 | if (!ResultType->isArrayType()) |
| 2081 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2082 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2084 | // Pre-allocate storage for the structured initializer list. |
| 2085 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2086 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2087 | bool GotNumInits = false; |
| 2088 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2089 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2090 | GotNumInits = true; |
| 2091 | } else if (Index < IList->getNumInits()) { |
| 2092 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2093 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2094 | GotNumInits = true; |
| 2095 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2099 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2100 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2101 | NumElements = CAType->getSize().getZExtValue(); |
| 2102 | // Simple heuristic so that we don't allocate a very large |
| 2103 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2104 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2105 | NumElements = 0; |
| 2106 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2107 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2108 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2109 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2110 | RecordDecl *RDecl = RType->getDecl(); |
| 2111 | if (RDecl->isUnion()) |
| 2112 | NumElements = 1; |
| 2113 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2115 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2118 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2120 | // Link this new initializer list into the structured initializer |
| 2121 | // lists. |
| 2122 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2123 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2124 | else { |
| 2125 | Result->setSyntacticForm(IList); |
| 2126 | SyntacticToSemantic[IList] = Result; |
| 2127 | } |
| 2128 | |
| 2129 | return Result; |
| 2130 | } |
| 2131 | |
| 2132 | /// Update the initializer at index @p StructuredIndex within the |
| 2133 | /// structured initializer list to the value @p expr. |
| 2134 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2135 | unsigned &StructuredIndex, |
| 2136 | Expr *expr) { |
| 2137 | // No structured initializer list to update |
| 2138 | if (!StructuredList) |
| 2139 | return; |
| 2140 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2141 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2142 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2143 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2144 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2145 | diag::warn_initializer_overrides) |
| 2146 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2147 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2148 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2149 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2150 | << PrevInit->getSourceRange(); |
| 2151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2153 | ++StructuredIndex; |
| 2154 | } |
| 2155 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2156 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2157 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2158 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2159 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2160 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2161 | /// added, on success. If everything went okay, Value will receive the |
| 2162 | /// value of the constant expression. |
| 2163 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2164 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2165 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2168 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2169 | if (Result.isInvalid()) |
| 2170 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2172 | if (Value.isSigned() && Value.isNegative()) |
| 2173 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2174 | << Value.toString(10) << Index->getSourceRange(); |
| 2175 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2176 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2177 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2180 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2181 | SourceLocation Loc, |
| 2182 | bool GNUSyntax, |
| 2183 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2184 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2185 | |
| 2186 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2187 | SmallVector<ASTDesignator, 32> Designators; |
| 2188 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2189 | |
| 2190 | // Build designators and check array designator expressions. |
| 2191 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2192 | const Designator &D = Desig.getDesignator(Idx); |
| 2193 | switch (D.getKind()) { |
| 2194 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2196 | D.getFieldLoc())); |
| 2197 | break; |
| 2198 | |
| 2199 | case Designator::ArrayDesignator: { |
| 2200 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2201 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2202 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2203 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2204 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2205 | Invalid = true; |
| 2206 | else { |
| 2207 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2209 | D.getRBracketLoc())); |
| 2210 | InitExpressions.push_back(Index); |
| 2211 | } |
| 2212 | break; |
| 2213 | } |
| 2214 | |
| 2215 | case Designator::ArrayRangeDesignator: { |
| 2216 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2217 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2218 | llvm::APSInt StartValue; |
| 2219 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2220 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2221 | StartIndex->isValueDependent(); |
| 2222 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2223 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2224 | if (!StartDependent) |
| 2225 | StartIndex = |
| 2226 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2227 | if (!EndDependent) |
| 2228 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2229 | |
| 2230 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2231 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2232 | else { |
| 2233 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2234 | if (StartDependent || EndDependent) { |
| 2235 | // Nothing to compute. |
| 2236 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2237 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2238 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2239 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2241 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2242 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2244 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2245 | Invalid = true; |
| 2246 | } else { |
| 2247 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2249 | D.getEllipsisLoc(), |
| 2250 | D.getRBracketLoc())); |
| 2251 | InitExpressions.push_back(StartIndex); |
| 2252 | InitExpressions.push_back(EndIndex); |
| 2253 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2254 | } |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | if (Invalid || Init.isInvalid()) |
| 2261 | return ExprError(); |
| 2262 | |
| 2263 | // Clear out the expressions within the designation. |
| 2264 | Desig.ClearExprs(*this); |
| 2265 | |
| 2266 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2267 | = DesignatedInitExpr::Create(Context, |
| 2268 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2269 | InitExpressions, Loc, GNUSyntax, |
| 2270 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2271 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2272 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2273 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2274 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2276 | return Owned(DIE); |
| 2277 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2279 | //===----------------------------------------------------------------------===// |
| 2280 | // Initialization entity |
| 2281 | //===----------------------------------------------------------------------===// |
| 2282 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2284 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2285 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2286 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2287 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2288 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2289 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2290 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2291 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2292 | Type = VT->getElementType(); |
| 2293 | } else { |
| 2294 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2295 | assert(CT && "Unexpected type"); |
| 2296 | Kind = EK_ComplexElement; |
| 2297 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2298 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2301 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2302 | CXXBaseSpecifier *Base, |
| 2303 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2304 | { |
| 2305 | InitializedEntity Result; |
| 2306 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2307 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2308 | if (IsInheritedVirtualBase) |
| 2309 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2310 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2311 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2312 | return Result; |
| 2313 | } |
| 2314 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2315 | DeclarationName InitializedEntity::getName() const { |
| 2316 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2317 | case EK_Parameter: { |
| 2318 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2319 | return (D ? D->getDeclName() : DeclarationName()); |
| 2320 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2321 | |
| 2322 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2323 | case EK_Member: |
| 2324 | return VariableOrMember->getDeclName(); |
| 2325 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2326 | case EK_LambdaCapture: |
| 2327 | return Capture.Var->getDeclName(); |
| 2328 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2329 | case EK_Result: |
| 2330 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2331 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2332 | case EK_Temporary: |
| 2333 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2334 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2335 | case EK_ArrayElement: |
| 2336 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2337 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2338 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2339 | return DeclarationName(); |
| 2340 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2342 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2345 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2346 | switch (getKind()) { |
| 2347 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2348 | case EK_Member: |
| 2349 | return VariableOrMember; |
| 2350 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2351 | case EK_Parameter: |
| 2352 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2353 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2354 | case EK_Result: |
| 2355 | case EK_Exception: |
| 2356 | case EK_New: |
| 2357 | case EK_Temporary: |
| 2358 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2359 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2360 | case EK_ArrayElement: |
| 2361 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2362 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2363 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2364 | case EK_LambdaCapture: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2365 | return 0; |
| 2366 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2367 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2368 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2371 | bool InitializedEntity::allowsNRVO() const { |
| 2372 | switch (getKind()) { |
| 2373 | case EK_Result: |
| 2374 | case EK_Exception: |
| 2375 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2376 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2377 | case EK_Variable: |
| 2378 | case EK_Parameter: |
| 2379 | case EK_Member: |
| 2380 | case EK_New: |
| 2381 | case EK_Temporary: |
| 2382 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2383 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2384 | case EK_ArrayElement: |
| 2385 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2386 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2387 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2388 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | } |
| 2391 | |
| 2392 | return false; |
| 2393 | } |
| 2394 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2395 | //===----------------------------------------------------------------------===// |
| 2396 | // Initialization sequence |
| 2397 | //===----------------------------------------------------------------------===// |
| 2398 | |
| 2399 | void InitializationSequence::Step::Destroy() { |
| 2400 | switch (Kind) { |
| 2401 | case SK_ResolveAddressOfOverloadedFunction: |
| 2402 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2403 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2404 | case SK_CastDerivedToBaseLValue: |
| 2405 | case SK_BindReference: |
| 2406 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2407 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2408 | case SK_UserConversion: |
| 2409 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2410 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2411 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2412 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2413 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2414 | case SK_UnwrapInitList: |
| 2415 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2416 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2417 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2418 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2419 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2420 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2421 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2422 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2423 | case SK_PassByIndirectCopyRestore: |
| 2424 | case SK_PassByIndirectRestore: |
| 2425 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2426 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 2427 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2428 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2429 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2430 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2431 | case SK_ConversionSequence: |
| 2432 | delete ICS; |
| 2433 | } |
| 2434 | } |
| 2435 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2436 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2437 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2441 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2442 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2443 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2444 | switch (getFailureKind()) { |
| 2445 | case FK_TooManyInitsForReference: |
| 2446 | case FK_ArrayNeedsInitList: |
| 2447 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2448 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2449 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2450 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2451 | case FK_RValueReferenceBindingToLValue: |
| 2452 | case FK_ReferenceInitDropsQualifiers: |
| 2453 | case FK_ReferenceInitFailed: |
| 2454 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2455 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2456 | case FK_TooManyInitsForScalar: |
| 2457 | case FK_ReferenceBindingToInitList: |
| 2458 | case FK_InitListBadDestinationType: |
| 2459 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2460 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2461 | case FK_ArrayTypeMismatch: |
| 2462 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2463 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2464 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2465 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2466 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2467 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2468 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2470 | case FK_ReferenceInitOverloadFailed: |
| 2471 | case FK_UserConversionOverloadFailed: |
| 2472 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2473 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2474 | return FailedOverloadResult == OR_Ambiguous; |
| 2475 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2477 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2480 | bool InitializationSequence::isConstructorInitialization() const { |
| 2481 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2482 | } |
| 2483 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2484 | void |
| 2485 | InitializationSequence |
| 2486 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2487 | DeclAccessPair Found, |
| 2488 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2489 | Step S; |
| 2490 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2491 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2492 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2493 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2494 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2495 | Steps.push_back(S); |
| 2496 | } |
| 2497 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2498 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2499 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2500 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2501 | switch (VK) { |
| 2502 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2503 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2504 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2505 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2506 | S.Type = BaseType; |
| 2507 | Steps.push_back(S); |
| 2508 | } |
| 2509 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | bool BindingTemporary) { |
| 2512 | Step S; |
| 2513 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2514 | S.Type = T; |
| 2515 | Steps.push_back(S); |
| 2516 | } |
| 2517 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2518 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2519 | Step S; |
| 2520 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2521 | S.Type = T; |
| 2522 | Steps.push_back(S); |
| 2523 | } |
| 2524 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2525 | void |
| 2526 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2527 | DeclAccessPair FoundDecl, |
| 2528 | QualType T, |
| 2529 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2530 | Step S; |
| 2531 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2532 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2533 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2534 | S.Function.Function = Function; |
| 2535 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2536 | Steps.push_back(S); |
| 2537 | } |
| 2538 | |
| 2539 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2540 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2541 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2542 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2543 | switch (VK) { |
| 2544 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2545 | S.Kind = SK_QualificationConversionRValue; |
| 2546 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2547 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2548 | S.Kind = SK_QualificationConversionXValue; |
| 2549 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2550 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2551 | S.Kind = SK_QualificationConversionLValue; |
| 2552 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2553 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2554 | S.Type = Ty; |
| 2555 | Steps.push_back(S); |
| 2556 | } |
| 2557 | |
| 2558 | void InitializationSequence::AddConversionSequenceStep( |
| 2559 | const ImplicitConversionSequence &ICS, |
| 2560 | QualType T) { |
| 2561 | Step S; |
| 2562 | S.Kind = SK_ConversionSequence; |
| 2563 | S.Type = T; |
| 2564 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2565 | Steps.push_back(S); |
| 2566 | } |
| 2567 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2568 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2569 | Step S; |
| 2570 | S.Kind = SK_ListInitialization; |
| 2571 | S.Type = T; |
| 2572 | Steps.push_back(S); |
| 2573 | } |
| 2574 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2575 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2576 | InitializationSequence |
| 2577 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2578 | AccessSpecifier Access, |
| 2579 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2580 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2581 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2582 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2583 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2584 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2585 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2586 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2587 | S.Function.Function = Constructor; |
| 2588 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2589 | Steps.push_back(S); |
| 2590 | } |
| 2591 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2592 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2593 | Step S; |
| 2594 | S.Kind = SK_ZeroInitialization; |
| 2595 | S.Type = T; |
| 2596 | Steps.push_back(S); |
| 2597 | } |
| 2598 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2599 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2600 | Step S; |
| 2601 | S.Kind = SK_CAssignment; |
| 2602 | S.Type = T; |
| 2603 | Steps.push_back(S); |
| 2604 | } |
| 2605 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2606 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2607 | Step S; |
| 2608 | S.Kind = SK_StringInit; |
| 2609 | S.Type = T; |
| 2610 | Steps.push_back(S); |
| 2611 | } |
| 2612 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2613 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2614 | Step S; |
| 2615 | S.Kind = SK_ObjCObjectConversion; |
| 2616 | S.Type = T; |
| 2617 | Steps.push_back(S); |
| 2618 | } |
| 2619 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2620 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2621 | Step S; |
| 2622 | S.Kind = SK_ArrayInit; |
| 2623 | S.Type = T; |
| 2624 | Steps.push_back(S); |
| 2625 | } |
| 2626 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2627 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2628 | Step S; |
| 2629 | S.Kind = SK_ParenthesizedArrayInit; |
| 2630 | S.Type = T; |
| 2631 | Steps.push_back(S); |
| 2632 | } |
| 2633 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2634 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2635 | bool shouldCopy) { |
| 2636 | Step s; |
| 2637 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2638 | : SK_PassByIndirectRestore); |
| 2639 | s.Type = type; |
| 2640 | Steps.push_back(s); |
| 2641 | } |
| 2642 | |
| 2643 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2644 | Step S; |
| 2645 | S.Kind = SK_ProduceObjCObject; |
| 2646 | S.Type = T; |
| 2647 | Steps.push_back(S); |
| 2648 | } |
| 2649 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2650 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2651 | Step S; |
| 2652 | S.Kind = SK_StdInitializerList; |
| 2653 | S.Type = T; |
| 2654 | Steps.push_back(S); |
| 2655 | } |
| 2656 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 2657 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2658 | Step S; |
| 2659 | S.Kind = SK_OCLSamplerInit; |
| 2660 | S.Type = T; |
| 2661 | Steps.push_back(S); |
| 2662 | } |
| 2663 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2664 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2665 | Step S; |
| 2666 | S.Kind = SK_OCLZeroEvent; |
| 2667 | S.Type = T; |
| 2668 | Steps.push_back(S); |
| 2669 | } |
| 2670 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2671 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2672 | InitListExpr *Syntactic) { |
| 2673 | assert(Syntactic->getNumInits() == 1 && |
| 2674 | "Can only rewrap trivial init lists."); |
| 2675 | Step S; |
| 2676 | S.Kind = SK_UnwrapInitList; |
| 2677 | S.Type = Syntactic->getInit(0)->getType(); |
| 2678 | Steps.insert(Steps.begin(), S); |
| 2679 | |
| 2680 | S.Kind = SK_RewrapInitList; |
| 2681 | S.Type = T; |
| 2682 | S.WrappingSyntacticList = Syntactic; |
| 2683 | Steps.push_back(S); |
| 2684 | } |
| 2685 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2687 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2688 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2689 | this->Failure = Failure; |
| 2690 | this->FailedOverloadResult = Result; |
| 2691 | } |
| 2692 | |
| 2693 | //===----------------------------------------------------------------------===// |
| 2694 | // Attempt initialization |
| 2695 | //===----------------------------------------------------------------------===// |
| 2696 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2697 | static void MaybeProduceObjCObject(Sema &S, |
| 2698 | InitializationSequence &Sequence, |
| 2699 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2700 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2701 | |
| 2702 | /// When initializing a parameter, produce the value if it's marked |
| 2703 | /// __attribute__((ns_consumed)). |
| 2704 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2705 | if (!Entity.isParameterConsumed()) |
| 2706 | return; |
| 2707 | |
| 2708 | assert(Entity.getType()->isObjCRetainableType() && |
| 2709 | "consuming an object of unretainable type?"); |
| 2710 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2711 | |
| 2712 | /// When initializing a return value, if the return type is a |
| 2713 | /// retainable type, then returns need to immediately retain the |
| 2714 | /// object. If an autorelease is required, it will be done at the |
| 2715 | /// last instant. |
| 2716 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2717 | if (!Entity.getType()->isObjCRetainableType()) |
| 2718 | return; |
| 2719 | |
| 2720 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2721 | } |
| 2722 | } |
| 2723 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2724 | /// \brief When initializing from init list via constructor, handle |
| 2725 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2726 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2727 | /// \return true if we have handled initialization of an object of type |
| 2728 | /// std::initializer_list<T>, false otherwise. |
| 2729 | static bool TryInitializerListConstruction(Sema &S, |
| 2730 | InitListExpr *List, |
| 2731 | QualType DestType, |
| 2732 | InitializationSequence &Sequence) { |
| 2733 | QualType E; |
| 2734 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2735 | return false; |
| 2736 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2737 | // Check that each individual element can be copy-constructed. But since we |
| 2738 | // have no place to store further information, we'll recalculate everything |
| 2739 | // later. |
| 2740 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2741 | S.Context.getConstantArrayType(E, |
| 2742 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2743 | List->getNumInits()), |
| 2744 | ArrayType::Normal, 0)); |
| 2745 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2746 | 0, HiddenArray); |
| 2747 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2748 | Element.setElementIndex(i); |
| 2749 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2750 | Sequence.SetFailed( |
| 2751 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2752 | return true; |
| 2753 | } |
| 2754 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2755 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2756 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2759 | static OverloadingResult |
| 2760 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
| 2761 | Expr **Args, unsigned NumArgs, |
| 2762 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2763 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2764 | OverloadCandidateSet::iterator &Best, |
| 2765 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2766 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2767 | CandidateSet.clear(); |
| 2768 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2769 | for (ArrayRef<NamedDecl *>::iterator |
| 2770 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2771 | NamedDecl *D = *Con; |
| 2772 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2773 | bool SuppressUserConversions = false; |
| 2774 | |
| 2775 | // Find the constructor (which may be a template). |
| 2776 | CXXConstructorDecl *Constructor = 0; |
| 2777 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2778 | if (ConstructorTmpl) |
| 2779 | Constructor = cast<CXXConstructorDecl>( |
| 2780 | ConstructorTmpl->getTemplatedDecl()); |
| 2781 | else { |
| 2782 | Constructor = cast<CXXConstructorDecl>(D); |
| 2783 | |
| 2784 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2785 | // suppress user-defined conversions on the arguments. We do the same for |
| 2786 | // move constructors. |
| 2787 | if ((CopyInitializing || (InitListSyntax && NumArgs == 1)) && |
| 2788 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2789 | SuppressUserConversions = true; |
| 2790 | } |
| 2791 | |
| 2792 | if (!Constructor->isInvalidDecl() && |
| 2793 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2794 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2795 | if (ConstructorTmpl) |
| 2796 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2797 | /*ExplicitArgs*/ 0, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2798 | llvm::makeArrayRef(Args, NumArgs), |
| 2799 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2800 | else { |
| 2801 | // C++ [over.match.copy]p1: |
| 2802 | // - When initializing a temporary to be bound to the first parameter |
| 2803 | // of a constructor that takes a reference to possibly cv-qualified |
| 2804 | // T as its first argument, called with a single argument in the |
| 2805 | // context of direct-initialization, explicit conversion functions |
| 2806 | // are also considered. |
| 2807 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 2808 | NumArgs == 1 && |
| 2809 | Constructor->isCopyOrMoveConstructor(); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2810 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2811 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2812 | SuppressUserConversions, |
| 2813 | /*PartialOverloading=*/false, |
| 2814 | /*AllowExplicit=*/AllowExplicitConv); |
| 2815 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | // Perform overload resolution and return the result. |
| 2820 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2821 | } |
| 2822 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2823 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2824 | /// enumerates the constructors of the initialized entity and performs overload |
| 2825 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2826 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2827 | /// class type. |
| 2828 | static void TryConstructorInitialization(Sema &S, |
| 2829 | const InitializedEntity &Entity, |
| 2830 | const InitializationKind &Kind, |
| 2831 | Expr **Args, unsigned NumArgs, |
| 2832 | QualType DestType, |
| 2833 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2834 | bool InitListSyntax = false) { |
| 2835 | assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) && |
| 2836 | "InitListSyntax must come with a single initializer list argument."); |
| 2837 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2838 | // The type we're constructing needs to be complete. |
| 2839 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2840 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2841 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2845 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2846 | CXXRecordDecl *DestRecordDecl |
| 2847 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2848 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2849 | // Build the candidate set directly in the initialization sequence |
| 2850 | // structure, so that it will persist if we fail. |
| 2851 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2852 | |
| 2853 | // Determine whether we are allowed to call explicit constructors or |
| 2854 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2855 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2856 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2857 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2858 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2859 | // applicable constructors are enumerated, and the best one is chosen |
| 2860 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2861 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2862 | // The container holding the constructors can under certain conditions |
| 2863 | // be changed while iterating (e.g. because of deserialization). |
| 2864 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2865 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2866 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2867 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2868 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2869 | bool AsInitializerList = false; |
| 2870 | |
| 2871 | // C++11 [over.match.list]p1: |
| 2872 | // When objects of non-aggregate type T are list-initialized, overload |
| 2873 | // resolution selects the constructor in two phases: |
| 2874 | // - Initially, the candidate functions are the initializer-list |
| 2875 | // constructors of the class T and the argument list consists of the |
| 2876 | // initializer list as a single argument. |
| 2877 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2878 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2879 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2880 | |
| 2881 | // If the initializer list has no elements and T has a default constructor, |
| 2882 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 2883 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2884 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2885 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2886 | CopyInitialization, AllowExplicit, |
| 2887 | /*OnlyListConstructor=*/true, |
| 2888 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2889 | |
| 2890 | // Time to unwrap the init list. |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2891 | Args = ILE->getInits(); |
| 2892 | NumArgs = ILE->getNumInits(); |
| 2893 | } |
| 2894 | |
| 2895 | // C++11 [over.match.list]p1: |
| 2896 | // - If no viable initializer-list constructor is found, overload resolution |
| 2897 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2898 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2899 | // elements of the initializer list. |
| 2900 | if (Result == OR_No_Viable_Function) { |
| 2901 | AsInitializerList = false; |
| 2902 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2903 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2904 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2905 | /*OnlyListConstructors=*/false, |
| 2906 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2907 | } |
| 2908 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2909 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2910 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2911 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2912 | Result); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2916 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2917 | // If a program calls for the default initialization of an object |
| 2918 | // of a const-qualified type T, T shall be a class type with a |
| 2919 | // user-provided default constructor. |
| 2920 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2921 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 2922 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2923 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2924 | return; |
| 2925 | } |
| 2926 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2927 | // C++11 [over.match.list]p1: |
| 2928 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 2929 | // initializer is ill-formed. |
| 2930 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2931 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 2932 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 2933 | return; |
| 2934 | } |
| 2935 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2936 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2937 | // subsumed by the initialization. |
| 2938 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2939 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 2940 | Best->FoundDecl.getAccess(), |
| 2941 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2942 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2945 | static bool |
| 2946 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 2947 | Expr *Initializer, |
| 2948 | QualType &SourceType, |
| 2949 | QualType &UnqualifiedSourceType, |
| 2950 | QualType UnqualifiedTargetType, |
| 2951 | InitializationSequence &Sequence) { |
| 2952 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 2953 | S.Context.OverloadTy) { |
| 2954 | DeclAccessPair Found; |
| 2955 | bool HadMultipleCandidates = false; |
| 2956 | if (FunctionDecl *Fn |
| 2957 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2958 | UnqualifiedTargetType, |
| 2959 | false, Found, |
| 2960 | &HadMultipleCandidates)) { |
| 2961 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 2962 | HadMultipleCandidates); |
| 2963 | SourceType = Fn->getType(); |
| 2964 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 2965 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 2966 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2967 | return true; |
| 2968 | } |
| 2969 | } |
| 2970 | return false; |
| 2971 | } |
| 2972 | |
| 2973 | static void TryReferenceInitializationCore(Sema &S, |
| 2974 | const InitializedEntity &Entity, |
| 2975 | const InitializationKind &Kind, |
| 2976 | Expr *Initializer, |
| 2977 | QualType cv1T1, QualType T1, |
| 2978 | Qualifiers T1Quals, |
| 2979 | QualType cv2T2, QualType T2, |
| 2980 | Qualifiers T2Quals, |
| 2981 | InitializationSequence &Sequence); |
| 2982 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2983 | static void TryValueInitialization(Sema &S, |
| 2984 | const InitializedEntity &Entity, |
| 2985 | const InitializationKind &Kind, |
| 2986 | InitializationSequence &Sequence, |
| 2987 | InitListExpr *InitList = 0); |
| 2988 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2989 | static void TryListInitialization(Sema &S, |
| 2990 | const InitializedEntity &Entity, |
| 2991 | const InitializationKind &Kind, |
| 2992 | InitListExpr *InitList, |
| 2993 | InitializationSequence &Sequence); |
| 2994 | |
| 2995 | /// \brief Attempt list initialization of a reference. |
| 2996 | static void TryReferenceListInitialization(Sema &S, |
| 2997 | const InitializedEntity &Entity, |
| 2998 | const InitializationKind &Kind, |
| 2999 | InitListExpr *InitList, |
| 3000 | InitializationSequence &Sequence) |
| 3001 | { |
| 3002 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3003 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3004 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3005 | return; |
| 3006 | } |
| 3007 | |
| 3008 | QualType DestType = Entity.getType(); |
| 3009 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3010 | Qualifiers T1Quals; |
| 3011 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3012 | |
| 3013 | // Reference initialization via an initializer list works thus: |
| 3014 | // If the initializer list consists of a single element that is |
| 3015 | // reference-related to the referenced type, bind directly to that element |
| 3016 | // (possibly creating temporaries). |
| 3017 | // Otherwise, initialize a temporary with the initializer list and |
| 3018 | // bind to that. |
| 3019 | if (InitList->getNumInits() == 1) { |
| 3020 | Expr *Initializer = InitList->getInit(0); |
| 3021 | QualType cv2T2 = Initializer->getType(); |
| 3022 | Qualifiers T2Quals; |
| 3023 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3024 | |
| 3025 | // If this fails, creating a temporary wouldn't work either. |
| 3026 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3027 | T1, Sequence)) |
| 3028 | return; |
| 3029 | |
| 3030 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3031 | bool dummy1, dummy2, dummy3; |
| 3032 | Sema::ReferenceCompareResult RefRelationship |
| 3033 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3034 | dummy2, dummy3); |
| 3035 | if (RefRelationship >= Sema::Ref_Related) { |
| 3036 | // Try to bind the reference here. |
| 3037 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3038 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3039 | if (Sequence) |
| 3040 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3041 | return; |
| 3042 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Update the initializer if we've resolved an overloaded function. |
| 3045 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3046 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | // Not reference-related. Create a temporary and bind to that. |
| 3050 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3051 | |
| 3052 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3053 | if (Sequence) { |
| 3054 | if (DestType->isRValueReferenceType() || |
| 3055 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3056 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3057 | else |
| 3058 | Sequence.SetFailed( |
| 3059 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3060 | } |
| 3061 | } |
| 3062 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3063 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3064 | static void TryListInitialization(Sema &S, |
| 3065 | const InitializedEntity &Entity, |
| 3066 | const InitializationKind &Kind, |
| 3067 | InitListExpr *InitList, |
| 3068 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3069 | QualType DestType = Entity.getType(); |
| 3070 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3071 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3072 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3073 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3074 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3075 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3076 | return; |
| 3077 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3078 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3079 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3080 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3081 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3082 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3083 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3084 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3085 | return; |
| 3086 | } |
| 3087 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3088 | // C++11 [dcl.init.list]p3: |
| 3089 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3090 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3091 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3092 | // - Otherwise, if the initializer list has no elements and T is a |
| 3093 | // class type with a default constructor, the object is |
| 3094 | // value-initialized. |
| 3095 | if (InitList->getNumInits() == 0) { |
| 3096 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3097 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3098 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3099 | return; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3104 | // an initializer_list object constructed [...] |
| 3105 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3106 | return; |
| 3107 | |
| 3108 | // - Otherwise, if T is a class type, constructors are considered. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3109 | Expr *Arg = InitList; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3110 | TryConstructorInitialization(S, Entity, Kind, &Arg, 1, DestType, |
| 3111 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3112 | } else |
| 3113 | Sequence.SetFailed( |
| 3114 | InitializationSequence::FK_InitListBadDestinationType); |
| 3115 | return; |
| 3116 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3119 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3120 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3121 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3122 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3123 | if (CheckInitList.HadError()) { |
| 3124 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3125 | return; |
| 3126 | } |
| 3127 | |
| 3128 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3129 | Sequence.AddListInitializationStep(DestType); |
| 3130 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3131 | |
| 3132 | /// \brief Try a reference initialization that involves calling a conversion |
| 3133 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3134 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3135 | const InitializedEntity &Entity, |
| 3136 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3137 | Expr *Initializer, |
| 3138 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3139 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3140 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3141 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3142 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3143 | QualType cv2T2 = Initializer->getType(); |
| 3144 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3145 | |
| 3146 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3147 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3148 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3150 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3151 | ObjCConversion, |
| 3152 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3153 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3154 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3155 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3156 | (void)ObjCLifetimeConversion; |
| 3157 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3158 | // Build the candidate set directly in the initialization sequence |
| 3159 | // structure, so that it will persist if we fail. |
| 3160 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3161 | CandidateSet.clear(); |
| 3162 | |
| 3163 | // Determine whether we are allowed to call explicit constructors or |
| 3164 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3165 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3166 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3167 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3168 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3169 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3170 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3171 | // The type we're converting to is a class type. Enumerate its constructors |
| 3172 | // to see if there is a suitable conversion. |
| 3173 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3174 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3175 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3176 | // The container holding the constructors can under certain conditions |
| 3177 | // be changed while iterating (e.g. because of deserialization). |
| 3178 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3179 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3180 | for (SmallVector<NamedDecl*, 16>::iterator |
| 3181 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3182 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3183 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3184 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3185 | // Find the constructor (which may be a template). |
| 3186 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3187 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3188 | if (ConstructorTmpl) |
| 3189 | Constructor = cast<CXXConstructorDecl>( |
| 3190 | ConstructorTmpl->getTemplatedDecl()); |
| 3191 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3192 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3194 | if (!Constructor->isInvalidDecl() && |
| 3195 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3196 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3197 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3198 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3199 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3200 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3201 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3202 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3203 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3204 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3205 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3206 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3207 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3208 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3209 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3210 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3211 | const RecordType *T2RecordType = 0; |
| 3212 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3213 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3214 | // The type we're converting from is a class type, enumerate its conversion |
| 3215 | // functions. |
| 3216 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3217 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3218 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3219 | CXXRecordDecl::conversion_iterator> |
| 3220 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3221 | for (CXXRecordDecl::conversion_iterator |
| 3222 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3223 | NamedDecl *D = *I; |
| 3224 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3225 | if (isa<UsingShadowDecl>(D)) |
| 3226 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3228 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3229 | CXXConversionDecl *Conv; |
| 3230 | if (ConvTemplate) |
| 3231 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3232 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3233 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3234 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3235 | // If the conversion function doesn't return a reference type, |
| 3236 | // it can't be considered for this conversion unless we're allowed to |
| 3237 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | // FIXME: Do we need to make sure that we only consider conversion |
| 3239 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3240 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3241 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3242 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3243 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3244 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3245 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3246 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3247 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3248 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3249 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3253 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3254 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3255 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3256 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3257 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3259 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3261 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3262 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3264 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3265 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3266 | // This is the overload that will actually be used for the initialization, so |
| 3267 | // mark it as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3268 | S.MarkFunctionReferenced(DeclLoc, Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3269 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3270 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3271 | if (isa<CXXConversionDecl>(Function)) |
| 3272 | T2 = Function->getResultType(); |
| 3273 | else |
| 3274 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3275 | |
| 3276 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3277 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3278 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3279 | T2.getNonLValueExprType(S.Context), |
| 3280 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3281 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3283 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3284 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3285 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3286 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3287 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3288 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3290 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3291 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3292 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3293 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3294 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3295 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3296 | NewDerivedToBase, NewObjCConversion, |
| 3297 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3298 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3299 | // If the type we've converted to is not reference-related to the |
| 3300 | // type we're looking for, then there is another conversion step |
| 3301 | // we need to perform to produce a temporary of the right type |
| 3302 | // that we'll be binding to. |
| 3303 | ImplicitConversionSequence ICS; |
| 3304 | ICS.setStandard(); |
| 3305 | ICS.Standard = Best->FinalConversion; |
| 3306 | T2 = ICS.Standard.getToType(2); |
| 3307 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3308 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3309 | Sequence.AddDerivedToBaseCastStep( |
| 3310 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3311 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3312 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3313 | else if (NewObjCConversion) |
| 3314 | Sequence.AddObjCObjectConversionStep( |
| 3315 | S.Context.getQualifiedType(T1, |
| 3316 | T2.getNonReferenceType().getQualifiers())); |
| 3317 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3319 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3321 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3322 | return OR_Success; |
| 3323 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3325 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3326 | const InitializedEntity &Entity, |
| 3327 | Expr *CurInitExpr); |
| 3328 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3329 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3330 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3331 | const InitializedEntity &Entity, |
| 3332 | const InitializationKind &Kind, |
| 3333 | Expr *Initializer, |
| 3334 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3335 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3336 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3337 | Qualifiers T1Quals; |
| 3338 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3339 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3340 | Qualifiers T2Quals; |
| 3341 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3343 | // If the initializer is the address of an overloaded function, try |
| 3344 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3345 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3346 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3347 | T1, Sequence)) |
| 3348 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3349 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3350 | // Delegate everything else to a subfunction. |
| 3351 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3352 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3353 | } |
| 3354 | |
| 3355 | /// \brief Reference initialization without resolving overloaded functions. |
| 3356 | static void TryReferenceInitializationCore(Sema &S, |
| 3357 | const InitializedEntity &Entity, |
| 3358 | const InitializationKind &Kind, |
| 3359 | Expr *Initializer, |
| 3360 | QualType cv1T1, QualType T1, |
| 3361 | Qualifiers T1Quals, |
| 3362 | QualType cv2T2, QualType T2, |
| 3363 | Qualifiers T2Quals, |
| 3364 | InitializationSequence &Sequence) { |
| 3365 | QualType DestType = Entity.getType(); |
| 3366 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3367 | // Compute some basic properties of the types and the initializer. |
| 3368 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3369 | bool isRValueRef = !isLValueRef; |
| 3370 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3371 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3372 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3373 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3374 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3375 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3376 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3377 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3378 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3379 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3380 | // "cv2 T2" as follows: |
| 3381 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3382 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3383 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3384 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3385 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3386 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3387 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3388 | bool T1Function = T1->isFunctionType(); |
| 3389 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3391 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3393 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | // reference-compatible with "cv2 T2," or |
| 3396 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3397 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3398 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3399 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3400 | // to decide whether we're actually binding to a temporary created from |
| 3401 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | if (DerivedToBase) |
| 3403 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3405 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3406 | else if (ObjCConversion) |
| 3407 | Sequence.AddObjCObjectConversionStep( |
| 3408 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3409 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3410 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3411 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3412 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3413 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3414 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3415 | return; |
| 3416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3417 | |
| 3418 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3419 | // reference-related to T2, and can be implicitly converted to an |
| 3420 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3421 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3422 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3423 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3424 | // If we have an rvalue ref to function type here, the rhs must be |
| 3425 | // an rvalue. |
| 3426 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3427 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3428 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3429 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3430 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3431 | Sequence); |
| 3432 | if (ConvOvlResult == OR_Success) |
| 3433 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3434 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3435 | Sequence.SetOverloadFailure( |
| 3436 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3437 | ConvOvlResult); |
| 3438 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3439 | } |
| 3440 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3441 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3443 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 3444 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3445 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3446 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3447 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3448 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3449 | Sequence.SetOverloadFailure( |
| 3450 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3451 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3452 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3453 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3454 | ? (RefRelationship == Sema::Ref_Related |
| 3455 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3456 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3457 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3459 | return; |
| 3460 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3462 | // - If the initializer expression |
| 3463 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3464 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3465 | // Note: functions are handled below. |
| 3466 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3467 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3469 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3470 | (InitCategory.isXValue() || |
| 3471 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3472 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3473 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3474 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3475 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3476 | // compiler the freedom to perform a copy here or bind to the |
| 3477 | // object, while C++0x requires that we bind directly to the |
| 3478 | // object. Hence, we always bind to the object without making an |
| 3479 | // extra copy. However, in C++03 requires that we check for the |
| 3480 | // presence of a suitable copy constructor: |
| 3481 | // |
| 3482 | // The constructor that would be used to make the copy shall |
| 3483 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3484 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3485 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3486 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3487 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3488 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3490 | if (DerivedToBase) |
| 3491 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3492 | ValueKind); |
| 3493 | else if (ObjCConversion) |
| 3494 | Sequence.AddObjCObjectConversionStep( |
| 3495 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3497 | if (T1Quals != T2Quals) |
| 3498 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3499 | Sequence.AddReferenceBindingStep(cv1T1, |
Peter Collingbourne | 65bfd68 | 2011-11-13 00:51:30 +0000 | [diff] [blame] | 3500 | /*bindingTemporary=*/InitCategory.isPRValue()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | |
| 3504 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3505 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3506 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3507 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3508 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3509 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3510 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3511 | Kind, Initializer, |
| 3512 | /*AllowRValues=*/true, |
| 3513 | Sequence); |
| 3514 | if (ConvOvlResult) |
| 3515 | Sequence.SetOverloadFailure( |
| 3516 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3517 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3519 | return; |
| 3520 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3522 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3523 | return; |
| 3524 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3525 | |
| 3526 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3527 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3528 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3531 | // Determine whether we are allowed to call explicit constructors or |
| 3532 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3533 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3534 | |
| 3535 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3536 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3537 | ImplicitConversionSequence ICS |
| 3538 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3539 | /*SuppressUserConversions*/ false, |
| 3540 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3541 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3542 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3543 | /*AllowObjCWritebackConversion=*/false); |
| 3544 | |
| 3545 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3546 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3547 | // this into an overloading ambiguity diagnostic. However, we need |
| 3548 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3549 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3550 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3551 | Sequence.SetOverloadFailure( |
| 3552 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3553 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3554 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3555 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3556 | else |
| 3557 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3558 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3559 | } else { |
| 3560 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3564 | // same cv-qualification as, or greater cv-qualification |
| 3565 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3566 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3567 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3568 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3569 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3570 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3571 | return; |
| 3572 | } |
| 3573 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3574 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3575 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3576 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3577 | InitCategory.isLValue()) { |
| 3578 | Sequence.SetFailed( |
| 3579 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3580 | return; |
| 3581 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3583 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3584 | return; |
| 3585 | } |
| 3586 | |
| 3587 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3589 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3590 | const InitializedEntity &Entity, |
| 3591 | const InitializationKind &Kind, |
| 3592 | Expr *Initializer, |
| 3593 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3594 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3597 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3598 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3599 | const InitializedEntity &Entity, |
| 3600 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3601 | InitializationSequence &Sequence, |
| 3602 | InitListExpr *InitList) { |
| 3603 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3604 | "Shouldn't use value-init for non-empty init lists"); |
| 3605 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3606 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3607 | // |
| 3608 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3609 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3610 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3611 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3612 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3614 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3615 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3616 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3617 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3618 | // C++98: |
| 3619 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3620 | // (12.1), then the default constructor for T is called (and the |
| 3621 | // initialization is ill-formed if T has no accessible default |
| 3622 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3623 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3624 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3625 | } else { |
| 3626 | // C++11: |
| 3627 | // -- if T is a class type (clause 9) with either no default constructor |
| 3628 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3629 | // or deleted, then the object is default-initialized; |
| 3630 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3631 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3632 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3633 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3634 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3635 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3636 | // user-provided or deleted default constructor, then the object is |
| 3637 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3638 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3639 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3640 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3641 | if (NeedZeroInitialization) |
| 3642 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3643 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3644 | // C++03: |
| 3645 | // -- if T is a non-union class type without a user-declared constructor, |
| 3646 | // then every non-static data member and base class component of T is |
| 3647 | // value-initialized; |
| 3648 | // [...] A program that calls for [...] value-initialization of an |
| 3649 | // entity of reference type is ill-formed. |
| 3650 | // |
| 3651 | // C++11 doesn't need this handling, because value-initialization does not |
| 3652 | // occur recursively there, and the implicit default constructor is |
| 3653 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3654 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3655 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3656 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3657 | return; |
| 3658 | } |
| 3659 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3660 | // If this is list-value-initialization, pass the empty init list on when |
| 3661 | // building the constructor call. This affects the semantics of a few |
| 3662 | // things (such as whether an explicit default constructor can be called). |
| 3663 | Expr *InitListAsExpr = InitList; |
| 3664 | Expr **Args = InitList ? &InitListAsExpr : 0; |
| 3665 | unsigned NumArgs = InitList ? 1 : 0; |
| 3666 | bool InitListSyntax = InitList; |
| 3667 | |
| 3668 | return TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, T, |
| 3669 | Sequence, InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3673 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3676 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3677 | static void TryDefaultInitialization(Sema &S, |
| 3678 | const InitializedEntity &Entity, |
| 3679 | const InitializationKind &Kind, |
| 3680 | InitializationSequence &Sequence) { |
| 3681 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3682 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3683 | // C++ [dcl.init]p6: |
| 3684 | // To default-initialize an object of type T means: |
| 3685 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3686 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3687 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3688 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3689 | // constructor for T is called (and the initialization is ill-formed if |
| 3690 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3691 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3692 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3693 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3694 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3696 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3698 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3700 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3701 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3702 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3703 | return; |
| 3704 | } |
| 3705 | |
| 3706 | // If the destination type has a lifetime property, zero-initialize it. |
| 3707 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3708 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3709 | return; |
| 3710 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3713 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3714 | /// which enumerates all conversion functions and performs overload resolution |
| 3715 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3717 | const InitializedEntity &Entity, |
| 3718 | const InitializationKind &Kind, |
| 3719 | Expr *Initializer, |
| 3720 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3721 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3722 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3723 | QualType SourceType = Initializer->getType(); |
| 3724 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3725 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3726 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3727 | // Build the candidate set directly in the initialization sequence |
| 3728 | // structure, so that it will persist if we fail. |
| 3729 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3730 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3731 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3732 | // Determine whether we are allowed to call explicit constructors or |
| 3733 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3734 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3735 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3736 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3737 | // The type we're converting to is a class type. Enumerate its constructors |
| 3738 | // to see if there is a suitable conversion. |
| 3739 | CXXRecordDecl *DestRecordDecl |
| 3740 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3741 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3742 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3743 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3744 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3745 | // The container holding the constructors can under certain conditions |
| 3746 | // be changed while iterating. To be safe we copy the lookup results |
| 3747 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3748 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3749 | for (SmallVector<NamedDecl*, 8>::iterator |
| 3750 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3751 | Con != ConEnd; ++Con) { |
| 3752 | NamedDecl *D = *Con; |
| 3753 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3755 | // Find the constructor (which may be a template). |
| 3756 | CXXConstructorDecl *Constructor = 0; |
| 3757 | FunctionTemplateDecl *ConstructorTmpl |
| 3758 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3759 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3760 | Constructor = cast<CXXConstructorDecl>( |
| 3761 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3762 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3763 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3765 | if (!Constructor->isInvalidDecl() && |
| 3766 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3767 | if (ConstructorTmpl) |
| 3768 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3769 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3770 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3771 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3772 | else |
| 3773 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3774 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3775 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3776 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3778 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3779 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3780 | |
| 3781 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3782 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3783 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3784 | // The type we're converting from is a class type, enumerate its conversion |
| 3785 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3786 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3787 | // We can only enumerate the conversion functions for a complete type; if |
| 3788 | // the type isn't complete, simply skip this step. |
| 3789 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3790 | CXXRecordDecl *SourceRecordDecl |
| 3791 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3793 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3794 | CXXRecordDecl::conversion_iterator> |
| 3795 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 3796 | for (CXXRecordDecl::conversion_iterator |
| 3797 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3798 | NamedDecl *D = *I; |
| 3799 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3800 | if (isa<UsingShadowDecl>(D)) |
| 3801 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3803 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3804 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3805 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3806 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3807 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3808 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3810 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3811 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3812 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3813 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3814 | CandidateSet); |
| 3815 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3816 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3817 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3818 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3822 | |
| 3823 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3824 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3825 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3826 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3827 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3828 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3829 | Result); |
| 3830 | return; |
| 3831 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3833 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3834 | S.MarkFunctionReferenced(DeclLoc, Function); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3835 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3837 | if (isa<CXXConstructorDecl>(Function)) { |
| 3838 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3839 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 3840 | // cv-unqualified type of the destination. |
| 3841 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 3842 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3843 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3844 | return; |
| 3845 | } |
| 3846 | |
| 3847 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3848 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3849 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3850 | // If we're converting to a class type, there may be an copy of |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3851 | // the resulting temporary object (possible to create an object of |
| 3852 | // a base class type). That copy is not a separate conversion, so |
| 3853 | // we just make a note of the actual destination type (possibly a |
| 3854 | // base class of the type returned by the conversion function) and |
| 3855 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3856 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3857 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3858 | return; |
| 3859 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3860 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3861 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3862 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3864 | // If the conversion following the call to the conversion function |
| 3865 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3866 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3867 | Best->FinalConversion.Third) { |
| 3868 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3869 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3870 | ICS.Standard = Best->FinalConversion; |
| 3871 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3872 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3875 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3876 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3877 | |
| 3878 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3879 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3880 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3881 | // Skip parens. |
| 3882 | e = e->IgnoreParens(); |
| 3883 | |
| 3884 | // Skip address-of nodes. |
| 3885 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3886 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3887 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 3888 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3889 | |
| 3890 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3891 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3892 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3893 | case CK_Dependent: |
| 3894 | case CK_BitCast: |
| 3895 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3896 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3897 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3898 | |
| 3899 | case CK_ArrayToPointerDecay: |
| 3900 | return IIK_nonscalar; |
| 3901 | |
| 3902 | case CK_NullToPointer: |
| 3903 | return IIK_okay; |
| 3904 | |
| 3905 | default: |
| 3906 | break; |
| 3907 | } |
| 3908 | |
| 3909 | // If we have a declaration reference, it had better be a local variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3910 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3911 | // set isWeakAccess to true, to mean that there will be an implicit |
| 3912 | // load which requires a cleanup. |
| 3913 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 3914 | isWeakAccess = true; |
| 3915 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3916 | if (!isAddressOf) return IIK_nonlocal; |
| 3917 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3918 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3919 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3920 | |
| 3921 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3922 | |
| 3923 | // If we have a conditional operator, check both sides. |
| 3924 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3925 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 3926 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3927 | return iik; |
| 3928 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3929 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3930 | |
| 3931 | // These are never scalar. |
| 3932 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3933 | return IIK_nonscalar; |
| 3934 | |
| 3935 | // Otherwise, it needs to be a null pointer constant. |
| 3936 | } else { |
| 3937 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3938 | ? IIK_okay : IIK_nonlocal); |
| 3939 | } |
| 3940 | |
| 3941 | return IIK_nonlocal; |
| 3942 | } |
| 3943 | |
| 3944 | /// Check whether the given expression is a valid operand for an |
| 3945 | /// indirect copy/restore. |
| 3946 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3947 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3948 | bool isWeakAccess = false; |
| 3949 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 3950 | // If isWeakAccess to true, there will be an implicit |
| 3951 | // load which requires a cleanup. |
| 3952 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 3953 | S.ExprNeedsCleanups = true; |
| 3954 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3955 | if (iik == IIK_okay) return; |
| 3956 | |
| 3957 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3958 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3959 | << src->getSourceRange(); |
| 3960 | } |
| 3961 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3962 | /// \brief Determine whether we have compatible array types for the |
| 3963 | /// purposes of GNU by-copy array initialization. |
| 3964 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3965 | const ArrayType *Dest, |
| 3966 | const ArrayType *Source) { |
| 3967 | // If the source and destination array types are equivalent, we're |
| 3968 | // done. |
| 3969 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3970 | return true; |
| 3971 | |
| 3972 | // Make sure that the element types are the same. |
| 3973 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3974 | return false; |
| 3975 | |
| 3976 | // The only mismatch we allow is when the destination is an |
| 3977 | // incomplete array type and the source is a constant array type. |
| 3978 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3979 | } |
| 3980 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3981 | static bool tryObjCWritebackConversion(Sema &S, |
| 3982 | InitializationSequence &Sequence, |
| 3983 | const InitializedEntity &Entity, |
| 3984 | Expr *Initializer) { |
| 3985 | bool ArrayDecay = false; |
| 3986 | QualType ArgType = Initializer->getType(); |
| 3987 | QualType ArgPointee; |
| 3988 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3989 | ArrayDecay = true; |
| 3990 | ArgPointee = ArgArrayType->getElementType(); |
| 3991 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3992 | } |
| 3993 | |
| 3994 | // Handle write-back conversion. |
| 3995 | QualType ConvertedArgType; |
| 3996 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3997 | ConvertedArgType)) |
| 3998 | return false; |
| 3999 | |
| 4000 | // We should copy unless we're passing to an argument explicitly |
| 4001 | // marked 'out'. |
| 4002 | bool ShouldCopy = true; |
| 4003 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4004 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4005 | |
| 4006 | // Do we need an lvalue conversion? |
| 4007 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4008 | ImplicitConversionSequence ICS; |
| 4009 | ICS.setStandard(); |
| 4010 | ICS.Standard.setAsIdentityConversion(); |
| 4011 | |
| 4012 | QualType ResultType; |
| 4013 | if (ArrayDecay) { |
| 4014 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4015 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4016 | } else { |
| 4017 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4018 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4019 | } |
| 4020 | |
| 4021 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4022 | } |
| 4023 | |
| 4024 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4025 | return true; |
| 4026 | } |
| 4027 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 4028 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4029 | InitializationSequence &Sequence, |
| 4030 | QualType DestType, |
| 4031 | Expr *Initializer) { |
| 4032 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4033 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4034 | return false; |
| 4035 | |
| 4036 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4037 | return true; |
| 4038 | } |
| 4039 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4040 | // |
| 4041 | // OpenCL 1.2 spec, s6.12.10 |
| 4042 | // |
| 4043 | // The event argument can also be used to associate the |
| 4044 | // async_work_group_copy with a previous async copy allowing |
| 4045 | // an event to be shared by multiple async copies; otherwise |
| 4046 | // event should be zero. |
| 4047 | // |
| 4048 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4049 | InitializationSequence &Sequence, |
| 4050 | QualType DestType, |
| 4051 | Expr *Initializer) { |
| 4052 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4053 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4054 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4055 | return false; |
| 4056 | |
| 4057 | Sequence.AddOCLZeroEventStep(DestType); |
| 4058 | return true; |
| 4059 | } |
| 4060 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4061 | InitializationSequence::InitializationSequence(Sema &S, |
| 4062 | const InitializedEntity &Entity, |
| 4063 | const InitializationKind &Kind, |
| 4064 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4065 | unsigned NumArgs) |
| 4066 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4067 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4068 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4069 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | // The semantics of initializers are as follows. The destination type is |
| 4071 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4072 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4073 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4074 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4075 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4076 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4077 | if (DestType->isDependentType() || |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4078 | Expr::hasAnyTypeDependentArguments(llvm::makeArrayRef(Args, NumArgs))) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4079 | SequenceKind = DependentSequence; |
| 4080 | return; |
| 4081 | } |
| 4082 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4083 | // Almost everything is a normal sequence. |
| 4084 | setSequenceKind(NormalSequence); |
| 4085 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4086 | for (unsigned I = 0; I != NumArgs; ++I) |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4087 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4088 | // FIXME: should we be doing this here? |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4089 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4090 | if (result.isInvalid()) { |
| 4091 | SetFailed(FK_PlaceholderType); |
| 4092 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4093 | } |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4094 | Args[I] = result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4095 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4096 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4098 | QualType SourceType; |
| 4099 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4100 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4101 | Initializer = Args[0]; |
| 4102 | if (!isa<InitListExpr>(Initializer)) |
| 4103 | SourceType = Initializer->getType(); |
| 4104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4105 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4106 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4107 | // object is list-initialized (8.5.4). |
| 4108 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4109 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4110 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4111 | return; |
| 4112 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4113 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4115 | // - If the destination type is a reference type, see 8.5.3. |
| 4116 | if (DestType->isReferenceType()) { |
| 4117 | // C++0x [dcl.init.ref]p1: |
| 4118 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4119 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4120 | // by an object that can be converted into a T. |
| 4121 | // (Therefore, multiple arguments are not permitted.) |
| 4122 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4123 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4124 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4125 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4126 | return; |
| 4127 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4128 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4129 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4130 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 4131 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4132 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4133 | return; |
| 4134 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4136 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4137 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4138 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4139 | return; |
| 4140 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4141 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4142 | // - If the destination type is an array of characters, an array of |
| 4143 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4144 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4146 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4147 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4148 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4149 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4150 | return; |
| 4151 | } |
| 4152 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4153 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4154 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4155 | return; |
| 4156 | } |
| 4157 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4158 | // Note: as an GNU C extension, we allow initialization of an |
| 4159 | // array from a compound literal that creates an array of the same |
| 4160 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4161 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4162 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4163 | Initializer->getType()->isArrayType()) { |
| 4164 | const ArrayType *SourceAT |
| 4165 | = Context.getAsArrayType(Initializer->getType()); |
| 4166 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4167 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4168 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4169 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4170 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4171 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4172 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4173 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4174 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4175 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4176 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4177 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4178 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4179 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4180 | *this); |
| 4181 | AddParenthesizedArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4182 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4183 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4185 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4187 | return; |
| 4188 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4189 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4190 | // Determine whether we should consider writeback conversions for |
| 4191 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4192 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4193 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4194 | |
| 4195 | // We're at the end of the line for C: it's either a write-back conversion |
| 4196 | // or it's a C assignment. There's no need to check anything else. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4197 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4198 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4199 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4200 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 4203 | |
| 4204 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4205 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4206 | |
| 4207 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4208 | return; |
| 4209 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4210 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4211 | AddCAssignmentStep(DestType); |
| 4212 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4213 | return; |
| 4214 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4216 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4217 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4218 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4219 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | // - If the initialization is direct-initialization, or if it is |
| 4221 | // copy-initialization where the cv-unqualified version of the |
| 4222 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4223 | // class of the destination, constructors are considered. [...] |
| 4224 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4225 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4226 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4227 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4228 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4229 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4231 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4232 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4233 | // used) to a derived class thereof are enumerated as described in |
| 4234 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4235 | // (13.3). |
| 4236 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4237 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4238 | return; |
| 4239 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4241 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4242 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4243 | return; |
| 4244 | } |
| 4245 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | |
| 4247 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4248 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4249 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4250 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4251 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4252 | return; |
| 4253 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4254 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4255 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4256 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4257 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4259 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4260 | |
| 4261 | ImplicitConversionSequence ICS |
| 4262 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4263 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4264 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4265 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4266 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4267 | allowObjCWritebackConversion); |
| 4268 | |
| 4269 | if (ICS.isStandard() && |
| 4270 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4271 | // Objective-C ARC writeback conversion. |
| 4272 | |
| 4273 | // We should copy unless we're passing to an argument explicitly |
| 4274 | // marked 'out'. |
| 4275 | bool ShouldCopy = true; |
| 4276 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4277 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4278 | |
| 4279 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4280 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4281 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4282 | ImplicitConversionSequence LvalueICS; |
| 4283 | LvalueICS.setStandard(); |
| 4284 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4285 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4286 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4287 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4288 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4289 | |
| 4290 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4291 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4292 | DeclAccessPair dap; |
| 4293 | if (Initializer->getType() == Context.OverloadTy && |
| 4294 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4295 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4296 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4297 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4298 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4299 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4300 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4301 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4302 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4303 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4307 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4308 | StepEnd = Steps.end(); |
| 4309 | Step != StepEnd; ++Step) |
| 4310 | Step->Destroy(); |
| 4311 | } |
| 4312 | |
| 4313 | //===----------------------------------------------------------------------===// |
| 4314 | // Perform initialization |
| 4315 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4317 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4318 | switch(Entity.getKind()) { |
| 4319 | case InitializedEntity::EK_Variable: |
| 4320 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4321 | case InitializedEntity::EK_Exception: |
| 4322 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4323 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4324 | return Sema::AA_Initializing; |
| 4325 | |
| 4326 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4327 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4328 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4329 | return Sema::AA_Sending; |
| 4330 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4331 | return Sema::AA_Passing; |
| 4332 | |
| 4333 | case InitializedEntity::EK_Result: |
| 4334 | return Sema::AA_Returning; |
| 4335 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4336 | case InitializedEntity::EK_Temporary: |
| 4337 | // FIXME: Can we tell apart casting vs. converting? |
| 4338 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4340 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4341 | case InitializedEntity::EK_ArrayElement: |
| 4342 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4343 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4344 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4345 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4346 | return Sema::AA_Initializing; |
| 4347 | } |
| 4348 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4349 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4352 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4353 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4354 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4355 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4356 | case InitializedEntity::EK_ArrayElement: |
| 4357 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4358 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4359 | case InitializedEntity::EK_New: |
| 4360 | case InitializedEntity::EK_Variable: |
| 4361 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4362 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4363 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4364 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4365 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4366 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4367 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4368 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4369 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4370 | case InitializedEntity::EK_Parameter: |
| 4371 | case InitializedEntity::EK_Temporary: |
| 4372 | return true; |
| 4373 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4374 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4375 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4376 | } |
| 4377 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4378 | /// \brief Whether the given entity, when initialized with an object |
| 4379 | /// created for that initialization, requires destruction. |
| 4380 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4381 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4382 | case InitializedEntity::EK_Result: |
| 4383 | case InitializedEntity::EK_New: |
| 4384 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4385 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4386 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4387 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4388 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4389 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4390 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4391 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4392 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4393 | case InitializedEntity::EK_Variable: |
| 4394 | case InitializedEntity::EK_Parameter: |
| 4395 | case InitializedEntity::EK_Temporary: |
| 4396 | case InitializedEntity::EK_ArrayElement: |
| 4397 | case InitializedEntity::EK_Exception: |
| 4398 | return true; |
| 4399 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4400 | |
| 4401 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4404 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4405 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4406 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4407 | OverloadCandidateSet &CandidateSet, |
| 4408 | CXXRecordDecl *Class, |
| 4409 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4410 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4411 | // The container holding the constructors can under certain conditions |
| 4412 | // be changed while iterating (e.g. because of deserialization). |
| 4413 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4414 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4415 | for (SmallVector<NamedDecl*, 16>::iterator |
| 4416 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4417 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4418 | CXXConstructorDecl *Constructor = 0; |
| 4419 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4420 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4421 | // Handle copy/moveconstructors, only. |
| 4422 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4423 | !Constructor->isCopyOrMoveConstructor() || |
| 4424 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4425 | continue; |
| 4426 | |
| 4427 | DeclAccessPair FoundDecl |
| 4428 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4429 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4430 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4431 | continue; |
| 4432 | } |
| 4433 | |
| 4434 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4435 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4436 | if (ConstructorTmpl->isInvalidDecl()) |
| 4437 | continue; |
| 4438 | |
| 4439 | Constructor = cast<CXXConstructorDecl>( |
| 4440 | ConstructorTmpl->getTemplatedDecl()); |
| 4441 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4442 | continue; |
| 4443 | |
| 4444 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4445 | // candidates? |
| 4446 | DeclAccessPair FoundDecl |
| 4447 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4448 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4449 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4454 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4455 | Expr *Initializer) { |
| 4456 | switch (Entity.getKind()) { |
| 4457 | case InitializedEntity::EK_Result: |
| 4458 | return Entity.getReturnLoc(); |
| 4459 | |
| 4460 | case InitializedEntity::EK_Exception: |
| 4461 | return Entity.getThrowLoc(); |
| 4462 | |
| 4463 | case InitializedEntity::EK_Variable: |
| 4464 | return Entity.getDecl()->getLocation(); |
| 4465 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4466 | case InitializedEntity::EK_LambdaCapture: |
| 4467 | return Entity.getCaptureLoc(); |
| 4468 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4469 | case InitializedEntity::EK_ArrayElement: |
| 4470 | case InitializedEntity::EK_Member: |
| 4471 | case InitializedEntity::EK_Parameter: |
| 4472 | case InitializedEntity::EK_Temporary: |
| 4473 | case InitializedEntity::EK_New: |
| 4474 | case InitializedEntity::EK_Base: |
| 4475 | case InitializedEntity::EK_Delegating: |
| 4476 | case InitializedEntity::EK_VectorElement: |
| 4477 | case InitializedEntity::EK_ComplexElement: |
| 4478 | case InitializedEntity::EK_BlockElement: |
| 4479 | return Initializer->getLocStart(); |
| 4480 | } |
| 4481 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4482 | } |
| 4483 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4484 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4485 | /// provided by the given initializer by calling the appropriate copy |
| 4486 | /// constructor. |
| 4487 | /// |
| 4488 | /// \param S The Sema object used for type-checking. |
| 4489 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4490 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4491 | /// the type of the initializer expression or a superclass thereof. |
| 4492 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4493 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4494 | /// |
| 4495 | /// \param CurInit The initializer expression. |
| 4496 | /// |
| 4497 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4498 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4499 | /// an rvalue. |
| 4500 | /// |
| 4501 | /// \returns An expression that copies the initializer expression into |
| 4502 | /// a temporary object, or an error expression if a copy could not be |
| 4503 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4504 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4505 | QualType T, |
| 4506 | const InitializedEntity &Entity, |
| 4507 | ExprResult CurInit, |
| 4508 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4509 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4510 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4511 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4512 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4513 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4514 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4515 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4516 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4517 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4518 | // When certain criteria are met, an implementation is allowed to |
| 4519 | // omit the copy/move construction of a class object, even if the |
| 4520 | // copy/move constructor and/or destructor for the object have |
| 4521 | // side effects. [...] |
| 4522 | // - when a temporary class object that has not been bound to a |
| 4523 | // reference (12.2) would be copied/moved to a class object |
| 4524 | // with the same cv-unqualified type, the copy/move operation |
| 4525 | // can be omitted by constructing the temporary object |
| 4526 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4528 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4529 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4531 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4532 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4533 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4534 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4535 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4536 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4537 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4539 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4540 | // Only consider constructors and constructor templates. Per |
| 4541 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4542 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4543 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4544 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4546 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4547 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4548 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4549 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4550 | case OR_Success: |
| 4551 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4553 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4554 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4555 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4556 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4557 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4558 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4559 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4560 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4561 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4562 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4564 | case OR_Ambiguous: |
| 4565 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4566 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4567 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4568 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4569 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4571 | case OR_Deleted: |
| 4572 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4573 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4574 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4575 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4576 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4579 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4580 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4581 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4582 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4583 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4584 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4585 | |
| 4586 | if (IsExtraneousCopy) { |
| 4587 | // If this is a totally extraneous copy for C++03 reference |
| 4588 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4589 | // expression. We don't generate an (elided) copy operation here |
| 4590 | // because doing so would require us to pass down a flag to avoid |
| 4591 | // infinite recursion, where each step adds another extraneous, |
| 4592 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4594 | // Instantiate the default arguments of any extra parameters in |
| 4595 | // the selected copy constructor, as if we were going to create a |
| 4596 | // proper call to the copy constructor. |
| 4597 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4598 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4599 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4600 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | |
| 4603 | // Build the default argument expression; we don't actually care |
| 4604 | // if this succeeds or not, because this routine will complain |
| 4605 | // if there was a problem. |
| 4606 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4607 | } |
| 4608 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4609 | return S.Owned(CurInitExpr); |
| 4610 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4611 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4612 | S.MarkFunctionReferenced(Loc, Constructor); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4614 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4615 | // constructor call (we might have derived-to-base conversions, or |
| 4616 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4617 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4618 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4619 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4620 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4621 | // Actually perform the constructor call. |
| 4622 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4623 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4624 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4625 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4626 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4627 | CXXConstructExpr::CK_Complete, |
| 4628 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4629 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4630 | // If we're supposed to bind temporaries, do so. |
| 4631 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4632 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4633 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4634 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4635 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4636 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4637 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4638 | /// -Wc++98-compat. |
| 4639 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4640 | const InitializedEntity &Entity, |
| 4641 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4642 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4643 | |
| 4644 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4645 | if (!Record) |
| 4646 | return; |
| 4647 | |
| 4648 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4649 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4650 | == DiagnosticsEngine::Ignored) |
| 4651 | return; |
| 4652 | |
| 4653 | // Find constructors which would have been considered. |
| 4654 | OverloadCandidateSet CandidateSet(Loc); |
| 4655 | LookupCopyAndMoveConstructors( |
| 4656 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4657 | |
| 4658 | // Perform overload resolution. |
| 4659 | OverloadCandidateSet::iterator Best; |
| 4660 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4661 | |
| 4662 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4663 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4664 | << CurInitExpr->getSourceRange(); |
| 4665 | |
| 4666 | switch (OR) { |
| 4667 | case OR_Success: |
| 4668 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4669 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4670 | // FIXME: Check default arguments as far as that's possible. |
| 4671 | break; |
| 4672 | |
| 4673 | case OR_No_Viable_Function: |
| 4674 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4675 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | |
| 4678 | case OR_Ambiguous: |
| 4679 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4680 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4681 | break; |
| 4682 | |
| 4683 | case OR_Deleted: |
| 4684 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4685 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4686 | break; |
| 4687 | } |
| 4688 | } |
| 4689 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4690 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4691 | const InitializedEntity &Entity) { |
| 4692 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4693 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4694 | return; |
| 4695 | |
| 4696 | if (Entity.getDecl()->getDeclName()) |
| 4697 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4698 | << Entity.getDecl()->getDeclName(); |
| 4699 | else |
| 4700 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4701 | } |
| 4702 | } |
| 4703 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4704 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4705 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4706 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4707 | } |
| 4708 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4709 | static ExprResult |
| 4710 | PerformConstructorInitialization(Sema &S, |
| 4711 | const InitializedEntity &Entity, |
| 4712 | const InitializationKind &Kind, |
| 4713 | MultiExprArg Args, |
| 4714 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4715 | bool &ConstructorInitRequiresZeroInit, |
| 4716 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4717 | unsigned NumArgs = Args.size(); |
| 4718 | CXXConstructorDecl *Constructor |
| 4719 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4720 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4721 | |
| 4722 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4723 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4724 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4725 | ? Kind.getEqualLoc() |
| 4726 | : Kind.getLocation(); |
| 4727 | |
| 4728 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4729 | // Force even a trivial, implicit default constructor to be |
| 4730 | // semantically checked. We do this explicitly because we don't build |
| 4731 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4732 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4733 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4734 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4735 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4736 | } |
| 4737 | |
| 4738 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4739 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4740 | // C++ [over.match.copy]p1: |
| 4741 | // - When initializing a temporary to be bound to the first parameter |
| 4742 | // of a constructor that takes a reference to possibly cv-qualified |
| 4743 | // T as its first argument, called with a single argument in the |
| 4744 | // context of direct-initialization, explicit conversion functions |
| 4745 | // are also considered. |
| 4746 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4747 | Args.size() == 1 && |
| 4748 | Constructor->isCopyOrMoveConstructor(); |
| 4749 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4750 | // Determine the arguments required to actually perform the constructor |
| 4751 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4752 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4753 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 4754 | AllowExplicitConv, |
| 4755 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4756 | return ExprError(); |
| 4757 | |
| 4758 | |
| 4759 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4760 | (Kind.getKind() == InitializationKind::IK_DirectList || |
| 4761 | (NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4762 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4763 | Kind.getKind() == InitializationKind::IK_Value)))) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4764 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4765 | S.MarkFunctionReferenced(Loc, Constructor); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4766 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 4767 | |
| 4768 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4769 | if (!TSInfo) |
| 4770 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4771 | SourceRange ParenRange; |
| 4772 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4773 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4774 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4775 | CurInit = S.Owned( |
| 4776 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 4777 | TSInfo, ConstructorArgs, |
| 4778 | ParenRange, IsListInitialization, |
| 4779 | HadMultipleCandidates, |
| 4780 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4781 | } else { |
| 4782 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4783 | CXXConstructExpr::CK_Complete; |
| 4784 | |
| 4785 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4786 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4787 | CXXConstructExpr::CK_VirtualBase : |
| 4788 | CXXConstructExpr::CK_NonVirtualBase; |
| 4789 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4790 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4791 | } |
| 4792 | |
| 4793 | // Only get the parenthesis range if it is a direct construction. |
| 4794 | SourceRange parenRange = |
| 4795 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4796 | Kind.getParenRange() : SourceRange(); |
| 4797 | |
| 4798 | // If the entity allows NRVO, mark the construction as elidable |
| 4799 | // unconditionally. |
| 4800 | if (Entity.allowsNRVO()) |
| 4801 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4802 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4803 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4804 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4805 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4806 | ConstructorInitRequiresZeroInit, |
| 4807 | ConstructKind, |
| 4808 | parenRange); |
| 4809 | else |
| 4810 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4811 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4812 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4813 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4814 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4815 | ConstructorInitRequiresZeroInit, |
| 4816 | ConstructKind, |
| 4817 | parenRange); |
| 4818 | } |
| 4819 | if (CurInit.isInvalid()) |
| 4820 | return ExprError(); |
| 4821 | |
| 4822 | // Only check access if all of that succeeded. |
| 4823 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4824 | Step.Function.FoundDecl.getAccess()); |
| 4825 | S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc); |
| 4826 | |
| 4827 | if (shouldBindAsTemporary(Entity)) |
| 4828 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4829 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4830 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4833 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 4834 | /// longer than the current full-expression. Conservatively returns false if |
| 4835 | /// it's unclear. |
| 4836 | static bool |
| 4837 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 4838 | const InitializedEntity *Top = &Entity; |
| 4839 | while (Top->getParent()) |
| 4840 | Top = Top->getParent(); |
| 4841 | |
| 4842 | switch (Top->getKind()) { |
| 4843 | case InitializedEntity::EK_Variable: |
| 4844 | case InitializedEntity::EK_Result: |
| 4845 | case InitializedEntity::EK_Exception: |
| 4846 | case InitializedEntity::EK_Member: |
| 4847 | case InitializedEntity::EK_New: |
| 4848 | case InitializedEntity::EK_Base: |
| 4849 | case InitializedEntity::EK_Delegating: |
| 4850 | return true; |
| 4851 | |
| 4852 | case InitializedEntity::EK_ArrayElement: |
| 4853 | case InitializedEntity::EK_VectorElement: |
| 4854 | case InitializedEntity::EK_BlockElement: |
| 4855 | case InitializedEntity::EK_ComplexElement: |
| 4856 | // Could not determine what the full initialization is. Assume it might not |
| 4857 | // outlive the full-expression. |
| 4858 | return false; |
| 4859 | |
| 4860 | case InitializedEntity::EK_Parameter: |
| 4861 | case InitializedEntity::EK_Temporary: |
| 4862 | case InitializedEntity::EK_LambdaCapture: |
| 4863 | // The entity being initialized might not outlive the full-expression. |
| 4864 | return false; |
| 4865 | } |
| 4866 | |
| 4867 | llvm_unreachable("unknown entity kind"); |
| 4868 | } |
| 4869 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4870 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4871 | InitializationSequence::Perform(Sema &S, |
| 4872 | const InitializedEntity &Entity, |
| 4873 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4874 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4875 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4876 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4877 | unsigned NumArgs = Args.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4878 | Diagnose(S, Entity, Kind, Args.data(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4879 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4880 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4881 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4882 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4883 | // If the declaration is a non-dependent, incomplete array type |
| 4884 | // that has an initializer, then its type will be completed once |
| 4885 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4886 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4887 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4888 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4889 | if (const IncompleteArrayType *ArrayT |
| 4890 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4891 | // FIXME: We don't currently have the ability to accurately |
| 4892 | // compute the length of an initializer list without |
| 4893 | // performing full type-checking of the initializer list |
| 4894 | // (since we have to determine where braces are implicitly |
| 4895 | // introduced and such). So, we fall back to making the array |
| 4896 | // type a dependently-sized array type with no specified |
| 4897 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4898 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4899 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4900 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4901 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4902 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4903 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4904 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4905 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4906 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4907 | Brackets = ArrayLoc->getBracketsRange(); |
| 4908 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | *ResultType |
| 4912 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4913 | /*NumElts=*/0, |
| 4914 | ArrayT->getSizeModifier(), |
| 4915 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4916 | Brackets); |
| 4917 | } |
| 4918 | |
| 4919 | } |
| 4920 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4921 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 4922 | !Kind.isExplicitCast()) { |
| 4923 | // Rebuild the ParenListExpr. |
| 4924 | SourceRange ParenRange = Kind.getParenRange(); |
| 4925 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4926 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4927 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4928 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 4929 | Kind.isExplicitCast() || |
| 4930 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4931 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4934 | // No steps means no initialization. |
| 4935 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4936 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4938 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4939 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 4940 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 4941 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 4942 | // from an initializer list. For parameters, we produce a better warning |
| 4943 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4944 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 4945 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 4946 | << Init->getSourceRange(); |
| 4947 | } |
| 4948 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4949 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 4950 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4951 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4952 | Entity.getType()->isPointerType() && |
| 4953 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4954 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4955 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 4956 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 4957 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 4958 | << Init->getSourceRange(); |
| 4959 | } |
| 4960 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4961 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4962 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4963 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4964 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4965 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4966 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4967 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4968 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4969 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4970 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4971 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4972 | // grab the only argument out the Args and place it into the "current" |
| 4973 | // initializer. |
| 4974 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4975 | case SK_ResolveAddressOfOverloadedFunction: |
| 4976 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4977 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4978 | case SK_CastDerivedToBaseLValue: |
| 4979 | case SK_BindReference: |
| 4980 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4981 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4982 | case SK_UserConversion: |
| 4983 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4984 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4985 | case SK_QualificationConversionRValue: |
| 4986 | case SK_ConversionSequence: |
| 4987 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4988 | case SK_UnwrapInitList: |
| 4989 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4990 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4991 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4992 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4993 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4994 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4995 | case SK_PassByIndirectCopyRestore: |
| 4996 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 4997 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4998 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 4999 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5000 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5001 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5002 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5003 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5004 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5005 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5007 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5008 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5009 | case SK_ZeroInitialization: |
| 5010 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5011 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
| 5013 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5014 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5015 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5016 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5017 | Step != StepEnd; ++Step) { |
| 5018 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5019 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5020 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5021 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5023 | switch (Step->Kind) { |
| 5024 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5026 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5027 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5028 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5029 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5030 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5031 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5032 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5034 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5035 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5036 | case SK_CastDerivedToBaseLValue: { |
| 5037 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5038 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5040 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5041 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5042 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5043 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5044 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5045 | CurInit.get()->getLocStart(), |
| 5046 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5047 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5048 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5050 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5051 | QualType T = SourceType; |
| 5052 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5053 | T = Pointer->getPointeeType(); |
| 5054 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5055 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5056 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5057 | } |
| 5058 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5059 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5060 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5061 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5062 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5063 | VK_XValue : |
| 5064 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5065 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5066 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5067 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5068 | CurInit.get(), |
| 5069 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5070 | break; |
| 5071 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5072 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5073 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5074 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5075 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 5076 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5077 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5078 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5079 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5080 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5081 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5082 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5083 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5084 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5085 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5086 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5087 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5088 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5089 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5090 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5091 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5093 | // Reference binding does not have any corresponding ASTs. |
| 5094 | |
| 5095 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5096 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5097 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5099 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5101 | case SK_BindReferenceToTemporary: |
| 5102 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5103 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5104 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5106 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5107 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5108 | Entity.getType().getNonReferenceType(), |
| 5109 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5110 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5111 | |
| 5112 | // If we're binding to an Objective-C object that has lifetime, we |
| 5113 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5114 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5115 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5116 | S.ExprNeedsCleanups = true; |
| 5117 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5118 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5119 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5120 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5121 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5122 | /*IsExtraneousCopy=*/true); |
| 5123 | break; |
| 5124 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5125 | case SK_UserConversion: { |
| 5126 | // We have a user-defined conversion that invokes either a constructor |
| 5127 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5128 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5129 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5130 | FunctionDecl *Fn = Step->Function.Function; |
| 5131 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5132 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5133 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5134 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5135 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5136 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5137 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5138 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5140 | // Determine the arguments required to actually perform the constructor |
| 5141 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5142 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5143 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5144 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5145 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5146 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5148 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5150 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5151 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5152 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5153 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5154 | CXXConstructExpr::CK_Complete, |
| 5155 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5156 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5157 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5158 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5159 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5160 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5161 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5162 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5163 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5164 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5165 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5166 | S.IsDerivedFrom(SourceType, Class)) |
| 5167 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5169 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5170 | } else { |
| 5171 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5172 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5173 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5174 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5175 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5176 | |
| 5177 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5178 | // derived-to-base conversion? I believe the answer is "no", because |
| 5179 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5180 | ExprResult CurInitExprRes = |
| 5181 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5182 | FoundFn, Conversion); |
| 5183 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5184 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5185 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5186 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5187 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5188 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5189 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5190 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5191 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5193 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5195 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5197 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5198 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5199 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5200 | |
| 5201 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5202 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5203 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5204 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5205 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5206 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5207 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5208 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5209 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5210 | } |
| 5211 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5213 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5214 | CurInit.get()->getType(), |
| 5215 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5216 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5217 | if (MaybeBindToTemp) |
| 5218 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5219 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5220 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5221 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5222 | break; |
| 5223 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5225 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5226 | case SK_QualificationConversionXValue: |
| 5227 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5228 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5229 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5230 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5231 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5232 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5233 | VK_XValue : |
| 5234 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5235 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5236 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5239 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5240 | Sema::CheckedConversionKind CCK |
| 5241 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5242 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5243 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5244 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5245 | ExprResult CurInitExprRes = |
| 5246 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5247 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5248 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5249 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5250 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5251 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5252 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5253 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5254 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5255 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5256 | // Hack: We must pass *ResultType if available in order to set the type |
| 5257 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5258 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5259 | // temporary, not a reference, so we should pass Ty. |
| 5260 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5261 | // Since this step is never used for a reference directly, we explicitly |
| 5262 | // unwrap references here and rewrap them afterwards. |
| 5263 | // We also need to create a InitializeTemporary entity for this. |
| 5264 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5265 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5266 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5267 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5268 | InitListChecker PerformInitList(S, InitEntity, |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5269 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5270 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5271 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5272 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5273 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5274 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5275 | if (ResultType) { |
| 5276 | if ((*ResultType)->isRValueReferenceType()) |
| 5277 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5278 | else if ((*ResultType)->isLValueReferenceType()) |
| 5279 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5280 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5281 | *ResultType = Ty; |
| 5282 | } |
| 5283 | |
| 5284 | InitListExpr *StructuredInitList = |
| 5285 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5286 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5287 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5288 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5289 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5290 | break; |
| 5291 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5292 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5293 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5294 | // When an initializer list is passed for a parameter of type "reference |
| 5295 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5296 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5297 | // FIXME: This is a hack. What we really should do is create a user |
| 5298 | // conversion step for this case, but this makes it considerably more |
| 5299 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5300 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5301 | Entity.getType().getNonReferenceType()); |
| 5302 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5303 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5304 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5305 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5306 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5307 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5308 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5309 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5310 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5311 | ConstructorInitRequiresZeroInit, |
| 5312 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5313 | break; |
| 5314 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5315 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5316 | case SK_UnwrapInitList: |
| 5317 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5318 | break; |
| 5319 | |
| 5320 | case SK_RewrapInitList: { |
| 5321 | Expr *E = CurInit.take(); |
| 5322 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5323 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5324 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5325 | ILE->setSyntacticForm(Syntactic); |
| 5326 | ILE->setType(E->getType()); |
| 5327 | ILE->setValueKind(E->getValueKind()); |
| 5328 | CurInit = S.Owned(ILE); |
| 5329 | break; |
| 5330 | } |
| 5331 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5332 | case SK_ConstructorInitialization: { |
| 5333 | // When an initializer list is passed for a parameter of type "reference |
| 5334 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5335 | // EK_Parameter entity with reference type. |
| 5336 | // FIXME: This is a hack. What we really should do is create a user |
| 5337 | // conversion step for this case, but this makes it considerably more |
| 5338 | // complicated. For now, this will do. |
| 5339 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5340 | Entity.getType().getNonReferenceType()); |
| 5341 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5342 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5343 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5344 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5345 | ConstructorInitRequiresZeroInit, |
| 5346 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5347 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5348 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5350 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5351 | step_iterator NextStep = Step; |
| 5352 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5353 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5354 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5355 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5356 | // The need for zero-initialization is recorded directly into |
| 5357 | // the call to the object's constructor within the next step. |
| 5358 | ConstructorInitRequiresZeroInit = true; |
| 5359 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5360 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5361 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5362 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5363 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5364 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5365 | Kind.getRange().getBegin()); |
| 5366 | |
| 5367 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5368 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5369 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5370 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5371 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5372 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5373 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5374 | break; |
| 5375 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5376 | |
| 5377 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5378 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5379 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5380 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5381 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5382 | if (Result.isInvalid()) |
| 5383 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5384 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5385 | |
| 5386 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5387 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5388 | if (ConvTy != Sema::Compatible && |
| 5389 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5390 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5391 | == Sema::Compatible) |
| 5392 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5393 | if (CurInitExprRes.isInvalid()) |
| 5394 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5395 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5396 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5397 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5398 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5399 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5400 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5401 | getAssignmentAction(Entity), |
| 5402 | &Complained)) { |
| 5403 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5404 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5405 | } else if (Complained) |
| 5406 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5407 | break; |
| 5408 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5409 | |
| 5410 | case SK_StringInit: { |
| 5411 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5412 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5413 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5414 | break; |
| 5415 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5416 | |
| 5417 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5418 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5419 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5420 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5421 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5422 | |
| 5423 | case SK_ArrayInit: |
| 5424 | // Okay: we checked everything before creating this step. Note that |
| 5425 | // this is a GNU extension. |
| 5426 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5427 | << Step->Type << CurInit.get()->getType() |
| 5428 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5429 | |
| 5430 | // If the destination type is an incomplete array type, update the |
| 5431 | // type accordingly. |
| 5432 | if (ResultType) { |
| 5433 | if (const IncompleteArrayType *IncompleteDest |
| 5434 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5435 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5436 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5437 | *ResultType = S.Context.getConstantArrayType( |
| 5438 | IncompleteDest->getElementType(), |
| 5439 | ConstantSource->getSize(), |
| 5440 | ArrayType::Normal, 0); |
| 5441 | } |
| 5442 | } |
| 5443 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5444 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5445 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5446 | case SK_ParenthesizedArrayInit: |
| 5447 | // Okay: we checked everything before creating this step. Note that |
| 5448 | // this is a GNU extension. |
| 5449 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5450 | << CurInit.get()->getSourceRange(); |
| 5451 | break; |
| 5452 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5453 | case SK_PassByIndirectCopyRestore: |
| 5454 | case SK_PassByIndirectRestore: |
| 5455 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5456 | CurInit = S.Owned(new (S.Context) |
| 5457 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5458 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5459 | break; |
| 5460 | |
| 5461 | case SK_ProduceObjCObject: |
| 5462 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5463 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5464 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5465 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5466 | |
| 5467 | case SK_StdInitializerList: { |
| 5468 | QualType Dest = Step->Type; |
| 5469 | QualType E; |
| 5470 | bool Success = S.isStdInitializerList(Dest, &E); |
| 5471 | (void)Success; |
| 5472 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5473 | |
| 5474 | // If the element type has a destructor, check it. |
| 5475 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5476 | if (!RD->hasIrrelevantDestructor()) { |
| 5477 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5478 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5479 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5480 | S.PDiag(diag::err_access_dtor_temp) << E); |
| 5481 | S.DiagnoseUseOfDecl(Destructor, Kind.getLocation()); |
| 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5486 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5487 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5488 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5489 | unsigned NumInits = ILE->getNumInits(); |
| 5490 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5491 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5492 | S.Context.getConstantArrayType(E, |
| 5493 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5494 | NumInits), |
| 5495 | ArrayType::Normal, 0)); |
| 5496 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5497 | 0, HiddenArray); |
| 5498 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5499 | Element.setElementIndex(i); |
| 5500 | ExprResult Init = S.Owned(ILE->getInit(i)); |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5501 | ExprResult Res = S.PerformCopyInitialization( |
| 5502 | Element, Init.get()->getExprLoc(), Init, |
| 5503 | /*TopLevelOfInitList=*/ true); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5504 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5505 | Converted[i] = Res.take(); |
| 5506 | } |
| 5507 | InitListExpr *Semantic = new (S.Context) |
| 5508 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5509 | Converted, ILE->getRBraceLoc()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5510 | Semantic->setSyntacticForm(ILE); |
| 5511 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5512 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5513 | CurInit = S.Owned(Semantic); |
| 5514 | break; |
| 5515 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 5516 | case SK_OCLSamplerInit: { |
| 5517 | assert(Step->Type->isSamplerT() && |
| 5518 | "Sampler initialization on non sampler type."); |
| 5519 | |
| 5520 | QualType SourceType = CurInit.get()->getType(); |
| 5521 | InitializedEntity::EntityKind EntityKind = Entity.getKind(); |
| 5522 | |
| 5523 | if (EntityKind == InitializedEntity::EK_Parameter) { |
| 5524 | if (!SourceType->isSamplerT()) |
| 5525 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 5526 | << SourceType; |
| 5527 | } else if (EntityKind != InitializedEntity::EK_Variable) { |
| 5528 | llvm_unreachable("Invalid EntityKind!"); |
| 5529 | } |
| 5530 | |
| 5531 | break; |
| 5532 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5533 | case SK_OCLZeroEvent: { |
| 5534 | assert(Step->Type->isEventT() && |
| 5535 | "Event initialization on non event type."); |
| 5536 | |
| 5537 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 5538 | CK_ZeroToOCLEvent, |
| 5539 | CurInit.get()->getValueKind()); |
| 5540 | break; |
| 5541 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5542 | } |
| 5543 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5544 | |
| 5545 | // Diagnose non-fatal problems with the completed initialization. |
| 5546 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5547 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5548 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5549 | cast<FieldDecl>(Entity.getDecl()), |
| 5550 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5551 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5552 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5555 | /// Somewhere within T there is an uninitialized reference subobject. |
| 5556 | /// Dig it out and diagnose it. |
| 5557 | bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, QualType T) { |
| 5558 | if (T->isReferenceType()) { |
| 5559 | S.Diag(Loc, diag::err_reference_without_init) |
| 5560 | << T.getNonReferenceType(); |
| 5561 | return true; |
| 5562 | } |
| 5563 | |
| 5564 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 5565 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 5566 | return false; |
| 5567 | |
| 5568 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5569 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5570 | if (FI->isUnnamedBitfield()) |
| 5571 | continue; |
| 5572 | |
| 5573 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 5574 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5575 | return true; |
| 5576 | } |
| 5577 | } |
| 5578 | |
| 5579 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5580 | BE = RD->bases_end(); |
| 5581 | BI != BE; ++BI) { |
| 5582 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 5583 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5584 | return true; |
| 5585 | } |
| 5586 | } |
| 5587 | |
| 5588 | return false; |
| 5589 | } |
| 5590 | |
| 5591 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5592 | //===----------------------------------------------------------------------===// |
| 5593 | // Diagnose initialization failures |
| 5594 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5596 | const InitializedEntity &Entity, |
| 5597 | const InitializationKind &Kind, |
| 5598 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5599 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5600 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5601 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5602 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5603 | switch (Failure) { |
| 5604 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5605 | // FIXME: Customize for the initialized entity? |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5606 | if (NumArgs == 0) { |
| 5607 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 5608 | // If this is value-initialization, this could be nested some way within |
| 5609 | // the target type. |
| 5610 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 5611 | DestType->isReferenceType()); |
| 5612 | bool Diagnosed = |
| 5613 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 5614 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 5615 | (void)Diagnosed; |
| 5616 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5617 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5618 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5619 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5621 | case FK_ArrayNeedsInitList: |
| 5622 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5623 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5624 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5625 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5627 | case FK_ArrayTypeMismatch: |
| 5628 | case FK_NonConstantArrayInit: |
| 5629 | S.Diag(Kind.getLocation(), |
| 5630 | (Failure == FK_ArrayTypeMismatch |
| 5631 | ? diag::err_array_init_different_type |
| 5632 | : diag::err_array_init_non_constant_array)) |
| 5633 | << DestType.getNonReferenceType() |
| 5634 | << Args[0]->getType() |
| 5635 | << Args[0]->getSourceRange(); |
| 5636 | break; |
| 5637 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5638 | case FK_VariableLengthArrayHasInitializer: |
| 5639 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5640 | << Args[0]->getSourceRange(); |
| 5641 | break; |
| 5642 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5643 | case FK_AddressOfOverloadFailed: { |
| 5644 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5645 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5646 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5647 | true, |
| 5648 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5649 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5652 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5653 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5654 | switch (FailedOverloadResult) { |
| 5655 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5656 | if (Failure == FK_UserConversionOverloadFailed) |
| 5657 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5658 | << Args[0]->getType() << DestType |
| 5659 | << Args[0]->getSourceRange(); |
| 5660 | else |
| 5661 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5662 | << DestType << Args[0]->getType() |
| 5663 | << Args[0]->getSourceRange(); |
| 5664 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5665 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5666 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5667 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5668 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5669 | case OR_No_Viable_Function: |
| 5670 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5671 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5672 | << Args[0]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5673 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5674 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5675 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5677 | case OR_Deleted: { |
| 5678 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5679 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5680 | << Args[0]->getSourceRange(); |
| 5681 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5682 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5683 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5684 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5685 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5686 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5687 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5688 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5689 | } |
| 5690 | break; |
| 5691 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5693 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5694 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5695 | } |
| 5696 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5697 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5698 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5699 | if (isa<InitListExpr>(Args[0])) { |
| 5700 | S.Diag(Kind.getLocation(), |
| 5701 | diag::err_lvalue_reference_bind_to_initlist) |
| 5702 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5703 | << DestType.getNonReferenceType() |
| 5704 | << Args[0]->getSourceRange(); |
| 5705 | break; |
| 5706 | } |
| 5707 | // Intentional fallthrough |
| 5708 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5709 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5711 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5712 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5713 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5714 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5715 | << DestType.getNonReferenceType() |
| 5716 | << Args[0]->getType() |
| 5717 | << Args[0]->getSourceRange(); |
| 5718 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5719 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5720 | case FK_RValueReferenceBindingToLValue: |
| 5721 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5722 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5723 | << Args[0]->getSourceRange(); |
| 5724 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5726 | case FK_ReferenceInitDropsQualifiers: |
| 5727 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5728 | << DestType.getNonReferenceType() |
| 5729 | << Args[0]->getType() |
| 5730 | << Args[0]->getSourceRange(); |
| 5731 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5733 | case FK_ReferenceInitFailed: |
| 5734 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5735 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5736 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5737 | << Args[0]->getType() |
| 5738 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5739 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5740 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5741 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5742 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5744 | case FK_ConversionFailed: { |
| 5745 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5746 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5747 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5748 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5749 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5750 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5751 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5752 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5753 | S.Diag(Kind.getLocation(), PDiag); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5754 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5755 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5756 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5757 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5758 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5759 | |
| 5760 | case FK_ConversionFromPropertyFailed: |
| 5761 | // No-op. This error has already been reported. |
| 5762 | break; |
| 5763 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5764 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5765 | SourceRange R; |
| 5766 | |
| 5767 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5768 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5769 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5770 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5771 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5772 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5773 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5774 | if (Kind.isCStyleOrFunctionalCast()) |
| 5775 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5776 | << R; |
| 5777 | else |
| 5778 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5779 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5780 | break; |
| 5781 | } |
| 5782 | |
| 5783 | case FK_ReferenceBindingToInitList: |
| 5784 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5785 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5786 | break; |
| 5787 | |
| 5788 | case FK_InitListBadDestinationType: |
| 5789 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5790 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5791 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5792 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5793 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5794 | case FK_ConstructorOverloadFailed: { |
| 5795 | SourceRange ArgsRange; |
| 5796 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5798 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5800 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 5801 | assert(NumArgs == 1 && "List construction from other than 1 argument."); |
| 5802 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 5803 | Args = InitList->getInits(); |
| 5804 | NumArgs = InitList->getNumInits(); |
| 5805 | } |
| 5806 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5807 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5808 | // bad. |
| 5809 | switch (FailedOverloadResult) { |
| 5810 | case OR_Ambiguous: |
| 5811 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5812 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5813 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5814 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5815 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5817 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5818 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5819 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5820 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5821 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5822 | // This is implicit default initialization of a member or |
| 5823 | // base within a constructor. If no viable function was |
| 5824 | // found, notify the user that she needs to explicitly |
| 5825 | // initialize this base/member. |
| 5826 | CXXConstructorDecl *Constructor |
| 5827 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5828 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5829 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5830 | << Constructor->isImplicit() |
| 5831 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5832 | << /*base=*/0 |
| 5833 | << Entity.getType(); |
| 5834 | |
| 5835 | RecordDecl *BaseDecl |
| 5836 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5837 | ->getDecl(); |
| 5838 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5839 | << S.Context.getTagDeclType(BaseDecl); |
| 5840 | } else { |
| 5841 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5842 | << Constructor->isImplicit() |
| 5843 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5844 | << /*member=*/1 |
| 5845 | << Entity.getName(); |
| 5846 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5847 | |
| 5848 | if (const RecordType *Record |
| 5849 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5850 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5851 | diag::note_previous_decl) |
| 5852 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5853 | } |
| 5854 | break; |
| 5855 | } |
| 5856 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5857 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5858 | << DestType << ArgsRange; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5859 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5860 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5861 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5863 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5864 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5865 | OverloadingResult Ovl |
| 5866 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5867 | if (Ovl != OR_Deleted) { |
| 5868 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5869 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5870 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5871 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5872 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5873 | |
| 5874 | // If this is a defaulted or implicitly-declared function, then |
| 5875 | // it was implicitly deleted. Make it clear that the deletion was |
| 5876 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5877 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5878 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5879 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5880 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5881 | else |
| 5882 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5883 | << true << DestType << ArgsRange; |
| 5884 | |
| 5885 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5886 | break; |
| 5887 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5889 | case OR_Success: |
| 5890 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5891 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5892 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 5893 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5895 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5896 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5897 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5898 | // This is implicit default-initialization of a const member in |
| 5899 | // a constructor. Complain that it needs to be explicitly |
| 5900 | // initialized. |
| 5901 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5902 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5903 | << Constructor->isImplicit() |
| 5904 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5905 | << /*const=*/1 |
| 5906 | << Entity.getName(); |
| 5907 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5908 | << Entity.getName(); |
| 5909 | } else { |
| 5910 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5911 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5912 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5913 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5915 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 5916 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5917 | diag::err_init_incomplete_type); |
| 5918 | break; |
| 5919 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5920 | case FK_ListInitializationFailed: { |
| 5921 | // Run the init list checker again to emit diagnostics. |
| 5922 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5923 | QualType DestType = Entity.getType(); |
| 5924 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5925 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5926 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5927 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5928 | assert(DiagnoseInitList.HadError() && |
| 5929 | "Inconsistent init list check result."); |
| 5930 | break; |
| 5931 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5932 | |
| 5933 | case FK_PlaceholderType: { |
| 5934 | // FIXME: Already diagnosed! |
| 5935 | break; |
| 5936 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5937 | |
| 5938 | case FK_InitListElementCopyFailure: { |
| 5939 | // Try to perform all copies again. |
| 5940 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5941 | unsigned NumInits = InitList->getNumInits(); |
| 5942 | QualType DestType = Entity.getType(); |
| 5943 | QualType E; |
| 5944 | bool Success = S.isStdInitializerList(DestType, &E); |
| 5945 | (void)Success; |
| 5946 | assert(Success && "Where did the std::initializer_list go?"); |
| 5947 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5948 | S.Context.getConstantArrayType(E, |
| 5949 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5950 | NumInits), |
| 5951 | ArrayType::Normal, 0)); |
| 5952 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 5953 | 0, HiddenArray); |
| 5954 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 5955 | // where the init list type is wrong, e.g. |
| 5956 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 5957 | // FIXME: Emit a note if we hit the limit? |
| 5958 | int ErrorCount = 0; |
| 5959 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 5960 | Element.setElementIndex(i); |
| 5961 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 5962 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 5963 | .isInvalid()) |
| 5964 | ++ErrorCount; |
| 5965 | } |
| 5966 | break; |
| 5967 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5968 | |
| 5969 | case FK_ExplicitConstructor: { |
| 5970 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 5971 | << Args[0]->getSourceRange(); |
| 5972 | OverloadCandidateSet::iterator Best; |
| 5973 | OverloadingResult Ovl |
| 5974 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 5975 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5976 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 5977 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 5978 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 5979 | break; |
| 5980 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5981 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5982 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5983 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5984 | return true; |
| 5985 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5986 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5987 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5988 | switch (SequenceKind) { |
| 5989 | case FailedSequence: { |
| 5990 | OS << "Failed sequence: "; |
| 5991 | switch (Failure) { |
| 5992 | case FK_TooManyInitsForReference: |
| 5993 | OS << "too many initializers for reference"; |
| 5994 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5995 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5996 | case FK_ArrayNeedsInitList: |
| 5997 | OS << "array requires initializer list"; |
| 5998 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6000 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6001 | OS << "array requires initializer list or string literal"; |
| 6002 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6003 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6004 | case FK_ArrayTypeMismatch: |
| 6005 | OS << "array type mismatch"; |
| 6006 | break; |
| 6007 | |
| 6008 | case FK_NonConstantArrayInit: |
| 6009 | OS << "non-constant array initializer"; |
| 6010 | break; |
| 6011 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6012 | case FK_AddressOfOverloadFailed: |
| 6013 | OS << "address of overloaded function failed"; |
| 6014 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6016 | case FK_ReferenceInitOverloadFailed: |
| 6017 | OS << "overload resolution for reference initialization failed"; |
| 6018 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6019 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6020 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6021 | OS << "non-const lvalue reference bound to temporary"; |
| 6022 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6024 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6025 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6026 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6028 | case FK_RValueReferenceBindingToLValue: |
| 6029 | OS << "rvalue reference bound to an lvalue"; |
| 6030 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6032 | case FK_ReferenceInitDropsQualifiers: |
| 6033 | OS << "reference initialization drops qualifiers"; |
| 6034 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6035 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6036 | case FK_ReferenceInitFailed: |
| 6037 | OS << "reference initialization failed"; |
| 6038 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6040 | case FK_ConversionFailed: |
| 6041 | OS << "conversion failed"; |
| 6042 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6043 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6044 | case FK_ConversionFromPropertyFailed: |
| 6045 | OS << "conversion from property failed"; |
| 6046 | break; |
| 6047 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6048 | case FK_TooManyInitsForScalar: |
| 6049 | OS << "too many initializers for scalar"; |
| 6050 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6052 | case FK_ReferenceBindingToInitList: |
| 6053 | OS << "referencing binding to initializer list"; |
| 6054 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6055 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6056 | case FK_InitListBadDestinationType: |
| 6057 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6058 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6059 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6060 | case FK_UserConversionOverloadFailed: |
| 6061 | OS << "overloading failed for user-defined conversion"; |
| 6062 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6063 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6064 | case FK_ConstructorOverloadFailed: |
| 6065 | OS << "constructor overloading failed"; |
| 6066 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6067 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6068 | case FK_DefaultInitOfConst: |
| 6069 | OS << "default initialization of a const variable"; |
| 6070 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6072 | case FK_Incomplete: |
| 6073 | OS << "initialization of incomplete type"; |
| 6074 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6075 | |
| 6076 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6077 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6078 | break; |
| 6079 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6080 | case FK_VariableLengthArrayHasInitializer: |
| 6081 | OS << "variable length array has an initializer"; |
| 6082 | break; |
| 6083 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6084 | case FK_PlaceholderType: |
| 6085 | OS << "initializer expression isn't contextually valid"; |
| 6086 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6087 | |
| 6088 | case FK_ListConstructorOverloadFailed: |
| 6089 | OS << "list constructor overloading failed"; |
| 6090 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6091 | |
| 6092 | case FK_InitListElementCopyFailure: |
| 6093 | OS << "copy construction of initializer list element failed"; |
| 6094 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6095 | |
| 6096 | case FK_ExplicitConstructor: |
| 6097 | OS << "list copy initialization chose explicit constructor"; |
| 6098 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6100 | OS << '\n'; |
| 6101 | return; |
| 6102 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6103 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6104 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6105 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6106 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6107 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6108 | case NormalSequence: |
| 6109 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6110 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6112 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6113 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6114 | if (S != step_begin()) { |
| 6115 | OS << " -> "; |
| 6116 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6117 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6118 | switch (S->Kind) { |
| 6119 | case SK_ResolveAddressOfOverloadedFunction: |
| 6120 | OS << "resolve address of overloaded function"; |
| 6121 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6122 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6123 | case SK_CastDerivedToBaseRValue: |
| 6124 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6125 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6126 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6127 | case SK_CastDerivedToBaseXValue: |
| 6128 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6129 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6130 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6131 | case SK_CastDerivedToBaseLValue: |
| 6132 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6133 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6134 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6135 | case SK_BindReference: |
| 6136 | OS << "bind reference to lvalue"; |
| 6137 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6138 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6139 | case SK_BindReferenceToTemporary: |
| 6140 | OS << "bind reference to a temporary"; |
| 6141 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6143 | case SK_ExtraneousCopyToTemporary: |
| 6144 | OS << "extraneous C++03 copy to temporary"; |
| 6145 | break; |
| 6146 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6147 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6148 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6149 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6150 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6151 | case SK_QualificationConversionRValue: |
| 6152 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6153 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6154 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6155 | case SK_QualificationConversionXValue: |
| 6156 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6157 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6158 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6159 | case SK_QualificationConversionLValue: |
| 6160 | OS << "qualification conversion (lvalue)"; |
| 6161 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6162 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6163 | case SK_ConversionSequence: |
| 6164 | OS << "implicit conversion sequence ("; |
| 6165 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6166 | OS << ")"; |
| 6167 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6169 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6170 | OS << "list aggregate initialization"; |
| 6171 | break; |
| 6172 | |
| 6173 | case SK_ListConstructorCall: |
| 6174 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6175 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6176 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6177 | case SK_UnwrapInitList: |
| 6178 | OS << "unwrap reference initializer list"; |
| 6179 | break; |
| 6180 | |
| 6181 | case SK_RewrapInitList: |
| 6182 | OS << "rewrap reference initializer list"; |
| 6183 | break; |
| 6184 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6185 | case SK_ConstructorInitialization: |
| 6186 | OS << "constructor initialization"; |
| 6187 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6189 | case SK_ZeroInitialization: |
| 6190 | OS << "zero initialization"; |
| 6191 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6193 | case SK_CAssignment: |
| 6194 | OS << "C assignment"; |
| 6195 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6197 | case SK_StringInit: |
| 6198 | OS << "string initialization"; |
| 6199 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6200 | |
| 6201 | case SK_ObjCObjectConversion: |
| 6202 | OS << "Objective-C object conversion"; |
| 6203 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6204 | |
| 6205 | case SK_ArrayInit: |
| 6206 | OS << "array initialization"; |
| 6207 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6208 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6209 | case SK_ParenthesizedArrayInit: |
| 6210 | OS << "parenthesized array initialization"; |
| 6211 | break; |
| 6212 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6213 | case SK_PassByIndirectCopyRestore: |
| 6214 | OS << "pass by indirect copy and restore"; |
| 6215 | break; |
| 6216 | |
| 6217 | case SK_PassByIndirectRestore: |
| 6218 | OS << "pass by indirect restore"; |
| 6219 | break; |
| 6220 | |
| 6221 | case SK_ProduceObjCObject: |
| 6222 | OS << "Objective-C object retension"; |
| 6223 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6224 | |
| 6225 | case SK_StdInitializerList: |
| 6226 | OS << "std::initializer_list from initializer list"; |
| 6227 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6228 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame^] | 6229 | case SK_OCLSamplerInit: |
| 6230 | OS << "OpenCL sampler_t from integer constant"; |
| 6231 | break; |
| 6232 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6233 | case SK_OCLZeroEvent: |
| 6234 | OS << "OpenCL event_t from zero"; |
| 6235 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6236 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6237 | |
| 6238 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6239 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6240 | |
| 6241 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
| 6244 | void InitializationSequence::dump() const { |
| 6245 | dump(llvm::errs()); |
| 6246 | } |
| 6247 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6248 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6249 | QualType EntityType, |
| 6250 | const Expr *PreInit, |
| 6251 | const Expr *PostInit) { |
| 6252 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6253 | return; |
| 6254 | |
| 6255 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6256 | // an initialization sequence. |
| 6257 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6258 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6259 | return; |
| 6260 | |
| 6261 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6262 | const StandardConversionSequence *SCS = 0; |
| 6263 | switch (ICS.getKind()) { |
| 6264 | case ImplicitConversionSequence::StandardConversion: |
| 6265 | SCS = &ICS.Standard; |
| 6266 | break; |
| 6267 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6268 | SCS = &ICS.UserDefined.After; |
| 6269 | break; |
| 6270 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6271 | case ImplicitConversionSequence::EllipsisConversion: |
| 6272 | case ImplicitConversionSequence::BadConversion: |
| 6273 | return; |
| 6274 | } |
| 6275 | |
| 6276 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6277 | // operator was used, this may be different from both the type of the entity |
| 6278 | // and of the pre-initialization expression. |
| 6279 | QualType PreNarrowingType = PreInit->getType(); |
| 6280 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6281 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6282 | |
| 6283 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6284 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6285 | QualType ConstantType; |
| 6286 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6287 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6288 | case NK_Not_Narrowing: |
| 6289 | // No narrowing occurred. |
| 6290 | return; |
| 6291 | |
| 6292 | case NK_Type_Narrowing: |
| 6293 | // This was a floating-to-integer conversion, which is always considered a |
| 6294 | // narrowing conversion even if the value is a constant and can be |
| 6295 | // represented exactly as an integer. |
| 6296 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6297 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6298 | diag::warn_init_list_type_narrowing |
| 6299 | : S.isSFINAEContext()? |
| 6300 | diag::err_init_list_type_narrowing_sfinae |
| 6301 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6302 | << PostInit->getSourceRange() |
| 6303 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6304 | << EntityType.getLocalUnqualifiedType(); |
| 6305 | break; |
| 6306 | |
| 6307 | case NK_Constant_Narrowing: |
| 6308 | // A constant value was narrowed. |
| 6309 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6310 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6311 | diag::warn_init_list_constant_narrowing |
| 6312 | : S.isSFINAEContext()? |
| 6313 | diag::err_init_list_constant_narrowing_sfinae |
| 6314 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6315 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6316 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6317 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6318 | break; |
| 6319 | |
| 6320 | case NK_Variable_Narrowing: |
| 6321 | // A variable's value may have been narrowed. |
| 6322 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6323 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6324 | diag::warn_init_list_variable_narrowing |
| 6325 | : S.isSFINAEContext()? |
| 6326 | diag::err_init_list_variable_narrowing_sfinae |
| 6327 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6328 | << PostInit->getSourceRange() |
| 6329 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6330 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6331 | break; |
| 6332 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6333 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6334 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6335 | llvm::raw_svector_ostream OS(StaticCast); |
| 6336 | OS << "static_cast<"; |
| 6337 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6338 | // It's important to use the typedef's name if there is one so that the |
| 6339 | // fixit doesn't break code using types like int64_t. |
| 6340 | // |
| 6341 | // FIXME: This will break if the typedef requires qualification. But |
| 6342 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6343 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6344 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6345 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6346 | else { |
| 6347 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6348 | // with a broken cast. |
| 6349 | return; |
| 6350 | } |
| 6351 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6352 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6353 | << PostInit->getSourceRange() |
| 6354 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6355 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6356 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6359 | //===----------------------------------------------------------------------===// |
| 6360 | // Initialization helper functions |
| 6361 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6362 | bool |
| 6363 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6364 | ExprResult Init) { |
| 6365 | if (Init.isInvalid()) |
| 6366 | return false; |
| 6367 | |
| 6368 | Expr *InitE = Init.get(); |
| 6369 | assert(InitE && "No initialization expression"); |
| 6370 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6371 | InitializationKind Kind |
| 6372 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6373 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6374 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6377 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6378 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6379 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6380 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6381 | bool TopLevelOfInitList, |
| 6382 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6383 | if (Init.isInvalid()) |
| 6384 | return ExprError(); |
| 6385 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6386 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6387 | assert(InitE && "No initialization expression?"); |
| 6388 | |
| 6389 | if (EqualLoc.isInvalid()) |
| 6390 | EqualLoc = InitE->getLocStart(); |
| 6391 | |
| 6392 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6393 | EqualLoc, |
| 6394 | AllowExplicit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6395 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 6396 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6397 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6398 | ExprResult Result = Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
| 6399 | |
| 6400 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6401 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6402 | InitE, Result.get()); |
| 6403 | |
| 6404 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6405 | } |