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 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Designator.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 26 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 27 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 28 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Sema Initialization Checking |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 33 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 34 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 35 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 36 | return 0; |
| 37 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 38 | // See if this is a string literal or @encode. |
| 39 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // Handle @encode, which is a narrow string. |
| 42 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 43 | return Init; |
| 44 | |
| 45 | // Otherwise we can only handle string literals. |
| 46 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 47 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 48 | |
| 49 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 50 | |
| 51 | switch (SL->getKind()) { |
| 52 | case StringLiteral::Ascii: |
| 53 | case StringLiteral::UTF8: |
| 54 | // char array can be initialized with a narrow string. |
| 55 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 57 | case StringLiteral::UTF16: |
| 58 | return ElemTy->isChar16Type() ? Init : 0; |
| 59 | case StringLiteral::UTF32: |
| 60 | return ElemTy->isChar32Type() ? Init : 0; |
| 61 | case StringLiteral::Wide: |
| 62 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 63 | // correction from DR343): "An array with element type compatible with a |
| 64 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 65 | // string literal, optionally enclosed in braces." |
| 66 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 67 | ElemTy.getUnqualifiedType())) |
| 68 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 73 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 76 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 77 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 78 | if (!arrayType) return 0; |
| 79 | |
| 80 | return IsStringInit(init, arrayType, Context); |
| 81 | } |
| 82 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 83 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 84 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 85 | // Get the length of the string as parsed. |
| 86 | uint64_t StrLength = |
| 87 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 88 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 90 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | // 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] | 92 | // being initialized to a string literal. |
| 93 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 94 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 95 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 96 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 97 | ConstVal, |
| 98 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 99 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 102 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 104 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 105 | // the size may be smaller or larger than the string we are initializing. |
| 106 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 107 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 108 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 109 | // For Pascal strings it's OK to strip off the terminating null character, |
| 110 | // so the example below is valid: |
| 111 | // |
| 112 | // unsigned char a[2] = "\pa"; |
| 113 | if (SL->isPascal()) |
| 114 | StrLength--; |
| 115 | } |
| 116 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 117 | // [dcl.init.string]p2 |
| 118 | if (StrLength > CAT->getSize().getZExtValue()) |
| 119 | S.Diag(Str->getSourceRange().getBegin(), |
| 120 | diag::err_initializer_string_for_char_array_too_long) |
| 121 | << Str->getSourceRange(); |
| 122 | } else { |
| 123 | // C99 6.7.8p14. |
| 124 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 125 | S.Diag(Str->getSourceRange().getBegin(), |
| 126 | diag::warn_initializer_string_for_char_array_too_long) |
| 127 | << Str->getSourceRange(); |
| 128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 130 | // Set the type to the actual size that we are initializing. If we have |
| 131 | // something like: |
| 132 | // char x[1] = "foo"; |
| 133 | // then this will set the string literal's type to char[1]. |
| 134 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // Semantic checking for initializer lists. |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 141 | /// @brief Semantic checking for initializer lists. |
| 142 | /// |
| 143 | /// The InitListChecker class contains a set of routines that each |
| 144 | /// handle the initialization of a certain kind of entity, e.g., |
| 145 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 146 | /// InitListChecker itself performs a recursive walk of the subobject |
| 147 | /// structure of the type to be initialized, while stepping through |
| 148 | /// the initializer list one element at a time. The IList and Index |
| 149 | /// parameters to each of the Check* routines contain the active |
| 150 | /// (syntactic) initializer list and the index into that initializer |
| 151 | /// list that represents the current initializer. Each routine is |
| 152 | /// responsible for moving that Index forward as it consumes elements. |
| 153 | /// |
| 154 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 155 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 156 | /// initializer list and the index into that initializer list where we |
| 157 | /// are copying initializers as we map them over to the semantic |
| 158 | /// list. Once we have completed our recursive walk of the subobject |
| 159 | /// structure, we will have constructed a full semantic initializer |
| 160 | /// list. |
| 161 | /// |
| 162 | /// C99 designators cause changes in the initializer list traversal, |
| 163 | /// because they make the initialization "jump" into a specific |
| 164 | /// subobject and then continue the initialization from that |
| 165 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 166 | /// designated subobject and manages backing out the recursion to |
| 167 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 168 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 169 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 170 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 172 | bool VerifyOnly; // no diagnostics, no structure building |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 174 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 176 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 177 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 178 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 179 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 180 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 181 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 182 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 183 | unsigned &StructuredIndex, |
| 184 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 185 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 186 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 188 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 189 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 190 | unsigned &StructuredIndex, |
| 191 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 192 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 193 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 194 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 195 | InitListExpr *StructuredList, |
| 196 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 197 | void CheckComplexType(const InitializedEntity &Entity, |
| 198 | InitListExpr *IList, QualType DeclType, |
| 199 | unsigned &Index, |
| 200 | InitListExpr *StructuredList, |
| 201 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 202 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 203 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 204 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 205 | InitListExpr *StructuredList, |
| 206 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 207 | void CheckReferenceType(const InitializedEntity &Entity, |
| 208 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 209 | unsigned &Index, |
| 210 | InitListExpr *StructuredList, |
| 211 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 212 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 213 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 214 | InitListExpr *StructuredList, |
| 215 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 216 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 217 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 220 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 221 | unsigned &StructuredIndex, |
| 222 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 223 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 224 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 226 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 227 | InitListExpr *StructuredList, |
| 228 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 229 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 230 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 231 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 233 | RecordDecl::field_iterator *NextField, |
| 234 | llvm::APSInt *NextElementIndex, |
| 235 | unsigned &Index, |
| 236 | InitListExpr *StructuredList, |
| 237 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 238 | bool FinishSubobjectInit, |
| 239 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 241 | QualType CurrentObjectType, |
| 242 | InitListExpr *StructuredList, |
| 243 | unsigned StructuredIndex, |
| 244 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 245 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 246 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 247 | Expr *expr); |
| 248 | int numArrayElements(QualType DeclType); |
| 249 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 251 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 252 | const InitializedEntity &ParentEntity, |
| 253 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 254 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 255 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 256 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 257 | Expr *InitExpr, FieldDecl *Field, |
| 258 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 260 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 261 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 262 | bool HadError() { return hadError; } |
| 263 | |
| 264 | // @brief Retrieves the fully-structured initializer list used for |
| 265 | // semantic analysis and code generation. |
| 266 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 267 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 268 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 270 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 271 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 273 | bool &RequiresSecondPass) { |
| 274 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 275 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 277 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 278 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 279 | // FIXME: We probably don't need to handle references |
| 280 | // specially here, since value-initialization of references is |
| 281 | // handled in InitializationSequence. |
| 282 | if (Field->getType()->isReferenceType()) { |
| 283 | // C++ [dcl.init.aggr]p9: |
| 284 | // If an incomplete or empty initializer-list leaves a |
| 285 | // member of reference type uninitialized, the program is |
| 286 | // ill-formed. |
| 287 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 288 | << Field->getType() |
| 289 | << ILE->getSyntacticForm()->getSourceRange(); |
| 290 | SemaRef.Diag(Field->getLocation(), |
| 291 | diag::note_uninit_reference_member); |
| 292 | hadError = true; |
| 293 | return; |
| 294 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 296 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 297 | true); |
| 298 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 299 | if (!InitSeq) { |
| 300 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 301 | hadError = true; |
| 302 | return; |
| 303 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 304 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 305 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 306 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 307 | if (MemberInit.isInvalid()) { |
| 308 | hadError = true; |
| 309 | return; |
| 310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 312 | if (hadError) { |
| 313 | // Do nothing |
| 314 | } else if (Init < NumInits) { |
| 315 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 316 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 317 | // Value-initialization requires a constructor call, so |
| 318 | // extend the initializer list to include the constructor |
| 319 | // call and make a note that we'll need to take another pass |
| 320 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 321 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 322 | RequiresSecondPass = true; |
| 323 | } |
| 324 | } else if (InitListExpr *InnerILE |
| 325 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 326 | FillInValueInitializations(MemberEntity, InnerILE, |
| 327 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 330 | /// Recursively replaces NULL values within the given initializer list |
| 331 | /// with expressions that perform value-initialization of the |
| 332 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 333 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 334 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 335 | InitListExpr *ILE, |
| 336 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 338 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 339 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 340 | if (ILE->getSyntacticForm()) |
| 341 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 343 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 344 | if (RType->getDecl()->isUnion() && |
| 345 | ILE->getInitializedFieldInUnion()) |
| 346 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 347 | Entity, ILE, RequiresSecondPass); |
| 348 | else { |
| 349 | unsigned Init = 0; |
| 350 | for (RecordDecl::field_iterator |
| 351 | Field = RType->getDecl()->field_begin(), |
| 352 | FieldEnd = RType->getDecl()->field_end(); |
| 353 | Field != FieldEnd; ++Field) { |
| 354 | if (Field->isUnnamedBitfield()) |
| 355 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 357 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 358 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 359 | |
| 360 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 361 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 362 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 363 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 364 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 365 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 366 | // Only look at the first initialization of a union. |
| 367 | if (RType->getDecl()->isUnion()) |
| 368 | break; |
| 369 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 374 | |
| 375 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 377 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 378 | unsigned NumInits = ILE->getNumInits(); |
| 379 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 380 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 381 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 382 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 383 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 385 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 386 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 387 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 388 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 390 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 392 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 395 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 396 | if (hadError) |
| 397 | return; |
| 398 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 399 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 400 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 401 | ElementEntity.setElementIndex(Init); |
| 402 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 403 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 404 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 405 | true); |
| 406 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 407 | if (!InitSeq) { |
| 408 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 409 | hadError = true; |
| 410 | return; |
| 411 | } |
| 412 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 413 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 414 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 415 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 416 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| 420 | if (hadError) { |
| 421 | // Do nothing |
| 422 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 423 | // For arrays, just set the expression used for value-initialization |
| 424 | // of the "holes" in the array. |
| 425 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 426 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 427 | else |
| 428 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 429 | } else { |
| 430 | // For arrays, just set the expression used for value-initialization |
| 431 | // of the rest of elements and exit. |
| 432 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 433 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 434 | return; |
| 435 | } |
| 436 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 437 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 438 | // Value-initialization requires a constructor call, so |
| 439 | // extend the initializer list to include the constructor |
| 440 | // call and make a note that we'll need to take another pass |
| 441 | // through the initializer list. |
| 442 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 443 | RequiresSecondPass = true; |
| 444 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 445 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 446 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 447 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 448 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 453 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 454 | InitListExpr *IL, QualType &T, |
| 455 | bool VerifyOnly) |
| 456 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 457 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 458 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 459 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 460 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 462 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 464 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 465 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 466 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 467 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 468 | bool RequiresSecondPass = false; |
| 469 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 470 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 472 | RequiresSecondPass); |
| 473 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 477 | // FIXME: use a proper constant |
| 478 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 479 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 480 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 481 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 482 | } |
| 483 | return maxElements; |
| 484 | } |
| 485 | |
| 486 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 487 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 488 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 490 | Field = structDecl->field_begin(), |
| 491 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | Field != FieldEnd; ++Field) { |
| 493 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 494 | ++InitializableMembers; |
| 495 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 496 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 497 | return std::min(InitializableMembers, 1); |
| 498 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 501 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 502 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 503 | QualType T, unsigned &Index, |
| 504 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 505 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 506 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 508 | if (T->isArrayType()) |
| 509 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 510 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 511 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 512 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 513 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 514 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 515 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 516 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 517 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 518 | if (!VerifyOnly) |
| 519 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 520 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 521 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 522 | hadError = true; |
| 523 | return; |
| 524 | } |
| 525 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 526 | // Build a structured initializer list corresponding to this subobject. |
| 527 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 529 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 530 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 531 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 532 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 534 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 535 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 536 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 537 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 539 | StructuredSubobjectInitIndex); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 540 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 541 | if (!VerifyOnly) { |
| 542 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 543 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 544 | // Update the structured sub-object initializer so that it's ending |
| 545 | // range corresponds with the end of the last initializer it used. |
| 546 | if (EndIndex < ParentIList->getNumInits()) { |
| 547 | SourceLocation EndLoc |
| 548 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 549 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 550 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 552 | // Warn about missing braces. |
| 553 | if (T->isArrayType() || T->isRecordType()) { |
| 554 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 555 | diag::warn_missing_braces) |
| 556 | << StructuredSubobjectInitList->getSourceRange() |
| 557 | << FixItHint::CreateInsertion( |
| 558 | StructuredSubobjectInitList->getLocStart(), "{") |
| 559 | << FixItHint::CreateInsertion( |
| 560 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 561 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 562 | "}"); |
| 563 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 564 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 567 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 568 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 569 | unsigned &Index, |
| 570 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 571 | unsigned &StructuredIndex, |
| 572 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 573 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 574 | if (!VerifyOnly) { |
| 575 | SyntacticToSemantic[IList] = StructuredList; |
| 576 | StructuredList->setSyntacticForm(IList); |
| 577 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 579 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 580 | if (!VerifyOnly) { |
| 581 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 582 | IList->setType(ExprTy); |
| 583 | StructuredList->setType(ExprTy); |
| 584 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 585 | if (hadError) |
| 586 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 587 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 588 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 589 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 590 | if (VerifyOnly) { |
| 591 | if (SemaRef.getLangOptions().CPlusPlus || |
| 592 | (SemaRef.getLangOptions().OpenCL && |
| 593 | IList->getType()->isVectorType())) { |
| 594 | hadError = true; |
| 595 | } |
| 596 | return; |
| 597 | } |
| 598 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 599 | if (StructuredIndex == 1 && |
| 600 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 601 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 602 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 603 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 604 | hadError = true; |
| 605 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 606 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 607 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 608 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 609 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 610 | // Don't complain for incomplete types, since we'll get an error |
| 611 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 612 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 614 | CurrentObjectType->isArrayType()? 0 : |
| 615 | CurrentObjectType->isVectorType()? 1 : |
| 616 | CurrentObjectType->isScalarType()? 2 : |
| 617 | CurrentObjectType->isUnionType()? 3 : |
| 618 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 619 | |
| 620 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 621 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 622 | DK = diag::err_excess_initializers; |
| 623 | hadError = true; |
| 624 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 625 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 626 | DK = diag::err_excess_initializers; |
| 627 | hadError = true; |
| 628 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 630 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 631 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 634 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 635 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 636 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 637 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 638 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 639 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 640 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 643 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 644 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 646 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 647 | unsigned &Index, |
| 648 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 649 | unsigned &StructuredIndex, |
| 650 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 651 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 652 | // Explicitly braced initializer for complex type can be real+imaginary |
| 653 | // parts. |
| 654 | CheckComplexType(Entity, IList, DeclType, Index, |
| 655 | StructuredList, StructuredIndex); |
| 656 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 657 | CheckScalarType(Entity, IList, DeclType, Index, |
| 658 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 659 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 661 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 662 | } else if (DeclType->isAggregateType()) { |
| 663 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 664 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 665 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 666 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 667 | StructuredList, StructuredIndex, |
| 668 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 669 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 670 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 671 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 672 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 673 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 674 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 676 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 677 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 678 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 679 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 680 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 681 | if (!VerifyOnly) |
| 682 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 683 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 684 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 685 | } else if (DeclType->isRecordType()) { |
| 686 | // C++ [dcl.init]p14: |
| 687 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 688 | // is a brace-enclosed list, see 8.5.1. |
| 689 | // |
| 690 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 691 | // we have an initializer list and a destination type that is not |
| 692 | // an aggregate. |
| 693 | // FIXME: In C++0x, this is yet another form of initialization. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 694 | if (!VerifyOnly) |
| 695 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 696 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 697 | hadError = true; |
| 698 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 699 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 700 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 701 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 702 | if (!VerifyOnly) |
| 703 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 704 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 705 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 706 | } else { |
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; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 710 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 714 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 715 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 717 | unsigned &Index, |
| 718 | InitListExpr *StructuredList, |
| 719 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 720 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 721 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 722 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 723 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 725 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 726 | StructuredList, StructuredIndex, |
| 727 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 728 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 729 | newStructuredList, newStructuredIndex); |
| 730 | ++StructuredIndex; |
| 731 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 732 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 733 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 734 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 735 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 736 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 737 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 738 | StructuredList, StructuredIndex); |
| 739 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 740 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 741 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 742 | // arrayType can be incomplete if we're initializing a flexible |
| 743 | // array member. There's nothing we can do with the completed |
| 744 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 745 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 746 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 747 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 748 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 749 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 750 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 751 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 752 | |
| 753 | // Fall through for subaggregate initialization. |
| 754 | |
| 755 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 756 | // C++ [dcl.init.aggr]p12: |
| 757 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 758 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 759 | // an initializer-list. If the initializer can initialize a |
| 760 | // member, the member is initialized. [...] |
| 761 | |
| 762 | // FIXME: Better EqualLoc? |
| 763 | InitializationKind Kind = |
| 764 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 765 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 766 | |
| 767 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 768 | if (!VerifyOnly) { |
| 769 | ExprResult Result = |
| 770 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 771 | if (Result.isInvalid()) |
| 772 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 773 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 774 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 775 | Result.takeAs<Expr>()); |
| 776 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 777 | ++Index; |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | // Fall through for subaggregate initialization |
| 782 | } else { |
| 783 | // C99 6.7.8p13: |
| 784 | // |
| 785 | // The initializer for a structure or union object that has |
| 786 | // automatic storage duration shall be either an initializer |
| 787 | // list as described below, or a single expression that has |
| 788 | // compatible structure or union type. In the latter case, the |
| 789 | // initial value of the object, including unnamed members, is |
| 790 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 791 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 792 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 793 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 794 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 795 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 796 | if (ExprRes.isInvalid()) |
| 797 | hadError = true; |
| 798 | else { |
| 799 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 800 | if (ExprRes.isInvalid()) |
| 801 | hadError = true; |
| 802 | } |
| 803 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 804 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 805 | ++Index; |
| 806 | return; |
| 807 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 808 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 809 | // Fall through for subaggregate initialization |
| 810 | } |
| 811 | |
| 812 | // C++ [dcl.init.aggr]p12: |
| 813 | // |
| 814 | // [...] Otherwise, if the member is itself a non-empty |
| 815 | // subaggregate, brace elision is assumed and the initializer is |
| 816 | // considered for the initialization of the first member of |
| 817 | // the subaggregate. |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 818 | if (!SemaRef.getLangOptions().OpenCL && |
| 819 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 820 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 821 | StructuredIndex); |
| 822 | ++StructuredIndex; |
| 823 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 824 | if (!VerifyOnly) { |
| 825 | // We cannot initialize this element, so let |
| 826 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 827 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 828 | SemaRef.Owned(expr), |
| 829 | /*TopLevelOfInitList=*/true); |
| 830 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 831 | hadError = true; |
| 832 | ++Index; |
| 833 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 834 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 837 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 838 | InitListExpr *IList, QualType DeclType, |
| 839 | unsigned &Index, |
| 840 | InitListExpr *StructuredList, |
| 841 | unsigned &StructuredIndex) { |
| 842 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 843 | |
| 844 | // As an extension, clang supports complex initializers, which initialize |
| 845 | // a complex number component-wise. When an explicit initializer list for |
| 846 | // a complex number contains two two initializers, this extension kicks in: |
| 847 | // it exepcts the initializer list to contain two elements convertible to |
| 848 | // the element type of the complex type. The first element initializes |
| 849 | // the real part, and the second element intitializes the imaginary part. |
| 850 | |
| 851 | if (IList->getNumInits() != 2) |
| 852 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 853 | StructuredIndex); |
| 854 | |
| 855 | // This is an extension in C. (The builtin _Complex type does not exist |
| 856 | // in the C++ standard.) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 857 | if (!SemaRef.getLangOptions().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 858 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 859 | << IList->getSourceRange(); |
| 860 | |
| 861 | // Initialize the complex number. |
| 862 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 863 | InitializedEntity ElementEntity = |
| 864 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 865 | |
| 866 | for (unsigned i = 0; i < 2; ++i) { |
| 867 | ElementEntity.setElementIndex(Index); |
| 868 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 869 | StructuredList, StructuredIndex); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 874 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 875 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 876 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 877 | InitListExpr *StructuredList, |
| 878 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 879 | if (Index >= IList->getNumInits()) { |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame^] | 880 | if (!SemaRef.getLangOptions().CPlusPlus0x) { |
| 881 | if (!VerifyOnly) |
| 882 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 883 | << IList->getSourceRange(); |
| 884 | hadError = true; |
| 885 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 886 | ++Index; |
| 887 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 888 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 889 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 890 | |
| 891 | Expr *expr = IList->getInit(Index); |
| 892 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 893 | if (!VerifyOnly) |
| 894 | SemaRef.Diag(SubIList->getLocStart(), |
| 895 | diag::warn_many_braces_around_scalar_init) |
| 896 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 897 | |
| 898 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 899 | StructuredIndex); |
| 900 | return; |
| 901 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 902 | if (!VerifyOnly) |
| 903 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 904 | diag::err_designator_for_scalar_init) |
| 905 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 906 | hadError = true; |
| 907 | ++Index; |
| 908 | ++StructuredIndex; |
| 909 | return; |
| 910 | } |
| 911 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 912 | if (VerifyOnly) { |
| 913 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 914 | hadError = true; |
| 915 | ++Index; |
| 916 | return; |
| 917 | } |
| 918 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 919 | ExprResult Result = |
| 920 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 921 | SemaRef.Owned(expr), |
| 922 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 923 | |
| 924 | Expr *ResultExpr = 0; |
| 925 | |
| 926 | if (Result.isInvalid()) |
| 927 | hadError = true; // types weren't compatible. |
| 928 | else { |
| 929 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 930 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 931 | if (ResultExpr != expr) { |
| 932 | // The type was promoted, update initializer list. |
| 933 | IList->setInit(Index, ResultExpr); |
| 934 | } |
| 935 | } |
| 936 | if (hadError) |
| 937 | ++StructuredIndex; |
| 938 | else |
| 939 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 940 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 943 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 944 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 945 | unsigned &Index, |
| 946 | InitListExpr *StructuredList, |
| 947 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 948 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 949 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 950 | // general, it would be useful to pass location information down the stack, |
| 951 | // so that we know the location (or decl) of the "current object" being |
| 952 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 953 | if (!VerifyOnly) |
| 954 | SemaRef.Diag(IList->getLocStart(), |
| 955 | diag::err_init_reference_member_uninitialized) |
| 956 | << DeclType |
| 957 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 958 | hadError = true; |
| 959 | ++Index; |
| 960 | ++StructuredIndex; |
| 961 | return; |
| 962 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 963 | |
| 964 | Expr *expr = IList->getInit(Index); |
| 965 | if (isa<InitListExpr>(expr)) { |
| 966 | // FIXME: Allowed in C++11. |
| 967 | if (!VerifyOnly) |
| 968 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 969 | << DeclType << IList->getSourceRange(); |
| 970 | hadError = true; |
| 971 | ++Index; |
| 972 | ++StructuredIndex; |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (VerifyOnly) { |
| 977 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 978 | hadError = true; |
| 979 | ++Index; |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | ExprResult Result = |
| 984 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 985 | SemaRef.Owned(expr), |
| 986 | /*TopLevelOfInitList=*/true); |
| 987 | |
| 988 | if (Result.isInvalid()) |
| 989 | hadError = true; |
| 990 | |
| 991 | expr = Result.takeAs<Expr>(); |
| 992 | IList->setInit(Index, expr); |
| 993 | |
| 994 | if (hadError) |
| 995 | ++StructuredIndex; |
| 996 | else |
| 997 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 998 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1001 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1002 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1003 | unsigned &Index, |
| 1004 | InitListExpr *StructuredList, |
| 1005 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1006 | if (Index >= IList->getNumInits()) |
| 1007 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1009 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1010 | unsigned maxElements = VT->getNumElements(); |
| 1011 | unsigned numEltsInit = 0; |
| 1012 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1013 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1014 | if (!SemaRef.getLangOptions().OpenCL) { |
| 1015 | // If the initializing element is a vector, try to copy-initialize |
| 1016 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1017 | Expr *Init = IList->getInit(Index); |
| 1018 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1019 | if (VerifyOnly) { |
| 1020 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1021 | hadError = true; |
| 1022 | ++Index; |
| 1023 | return; |
| 1024 | } |
| 1025 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1026 | ExprResult Result = |
| 1027 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1028 | SemaRef.Owned(Init), |
| 1029 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1030 | |
| 1031 | Expr *ResultExpr = 0; |
| 1032 | if (Result.isInvalid()) |
| 1033 | hadError = true; // types weren't compatible. |
| 1034 | else { |
| 1035 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1037 | if (ResultExpr != Init) { |
| 1038 | // The type was promoted, update initializer list. |
| 1039 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1042 | if (hadError) |
| 1043 | ++StructuredIndex; |
| 1044 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1045 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1046 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1047 | ++Index; |
| 1048 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1051 | InitializedEntity ElementEntity = |
| 1052 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1054 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1055 | // Don't attempt to go past the end of the init list |
| 1056 | if (Index >= IList->getNumInits()) |
| 1057 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1059 | ElementEntity.setElementIndex(Index); |
| 1060 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1061 | StructuredList, StructuredIndex); |
| 1062 | } |
| 1063 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1064 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1065 | |
| 1066 | InitializedEntity ElementEntity = |
| 1067 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1069 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1070 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1071 | // Don't attempt to go past the end of the init list |
| 1072 | if (Index >= IList->getNumInits()) |
| 1073 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1075 | ElementEntity.setElementIndex(Index); |
| 1076 | |
| 1077 | QualType IType = IList->getInit(Index)->getType(); |
| 1078 | if (!IType->isVectorType()) { |
| 1079 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1080 | StructuredList, StructuredIndex); |
| 1081 | ++numEltsInit; |
| 1082 | } else { |
| 1083 | QualType VecType; |
| 1084 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1085 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1087 | if (IType->isExtVectorType()) |
| 1088 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1089 | else |
| 1090 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1091 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1092 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1093 | StructuredList, StructuredIndex); |
| 1094 | numEltsInit += numIElts; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1099 | // FIXME: Shouldn't this set hadError to true then? |
| 1100 | if (numEltsInit != maxElements && !VerifyOnly) |
| 1101 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1102 | diag::err_vector_incorrect_num_initializers) |
| 1103 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1106 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1107 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1108 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1110 | unsigned &Index, |
| 1111 | InitListExpr *StructuredList, |
| 1112 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1113 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1114 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1115 | // Check for the special-case of initializing an array with a string. |
| 1116 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1117 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1118 | SemaRef.Context)) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1119 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1120 | // We place the string literal directly into the resulting |
| 1121 | // initializer list. This is the only place where the structure |
| 1122 | // of the structured initializer list doesn't match exactly, |
| 1123 | // because doing so would involve allocating one character |
| 1124 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1125 | if (!VerifyOnly) { |
| 1126 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1127 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1128 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1129 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1133 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1134 | // Check for VLAs; in standard C it would be possible to check this |
| 1135 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1136 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1137 | if (!VerifyOnly) |
| 1138 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1139 | diag::err_variable_object_no_init) |
| 1140 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1141 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1142 | ++Index; |
| 1143 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1147 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1148 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1149 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1150 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1151 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1152 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1153 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1154 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1155 | maxElementsKnown = true; |
| 1156 | } |
| 1157 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1158 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1159 | while (Index < IList->getNumInits()) { |
| 1160 | Expr *Init = IList->getInit(Index); |
| 1161 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1162 | // If we're not the subobject that matches up with the '{' for |
| 1163 | // the designator, we shouldn't be handling the |
| 1164 | // designator. Return immediately. |
| 1165 | if (!SubobjectIsDesignatorContext) |
| 1166 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1168 | // Handle this designated initializer. elementIndex will be |
| 1169 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1170 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1171 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1172 | StructuredList, StructuredIndex, true, |
| 1173 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1174 | hadError = true; |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1178 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1179 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1180 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1181 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1182 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1183 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1184 | // If the array is of incomplete type, keep track of the number of |
| 1185 | // elements in the initializer. |
| 1186 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1187 | maxElements = elementIndex; |
| 1188 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | // If we know the maximum number of elements, and we've already |
| 1193 | // hit it, stop consuming elements in the initializer list. |
| 1194 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1195 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1196 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1197 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1198 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1199 | Entity); |
| 1200 | // Check this element. |
| 1201 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1202 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1203 | ++elementIndex; |
| 1204 | |
| 1205 | // If the array is of incomplete type, keep track of the number of |
| 1206 | // elements in the initializer. |
| 1207 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1208 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1209 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1210 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1211 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1212 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1213 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1214 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1215 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1216 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1217 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1218 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1219 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1222 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1226 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1227 | Expr *InitExpr, |
| 1228 | FieldDecl *Field, |
| 1229 | bool TopLevelObject) { |
| 1230 | // Handle GNU flexible array initializers. |
| 1231 | unsigned FlexArrayDiag; |
| 1232 | if (isa<InitListExpr>(InitExpr) && |
| 1233 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1234 | // Empty flexible array init always allowed as an extension |
| 1235 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1236 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1237 | // Disallow flexible array init in C++; it is not required for gcc |
| 1238 | // compatibility, and it needs work to IRGen correctly in general. |
| 1239 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1240 | } else if (!TopLevelObject) { |
| 1241 | // Disallow flexible array init on non-top-level object |
| 1242 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1243 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1244 | // Disallow flexible array init on anything which is not a variable. |
| 1245 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1246 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1247 | // Disallow flexible array init on local variables. |
| 1248 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1249 | } else { |
| 1250 | // Allow other cases. |
| 1251 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1252 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (!VerifyOnly) { |
| 1255 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1256 | FlexArrayDiag) |
| 1257 | << InitExpr->getSourceRange().getBegin(); |
| 1258 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1259 | << Field; |
| 1260 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1261 | |
| 1262 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1263 | } |
| 1264 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1265 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1266 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1268 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1270 | unsigned &Index, |
| 1271 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1272 | unsigned &StructuredIndex, |
| 1273 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1274 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1276 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1277 | // confusion, we forgo checking the intializer for the entire record. |
| 1278 | if (structDecl->isInvalidDecl()) { |
| 1279 | hadError = true; |
| 1280 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1282 | |
| 1283 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1284 | if (!VerifyOnly) { |
| 1285 | // Value-initialize the first named member of the union. |
| 1286 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1287 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1288 | Field != FieldEnd; ++Field) { |
| 1289 | if (Field->getDeclName()) { |
| 1290 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1291 | break; |
| 1292 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | return; |
| 1296 | } |
| 1297 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1298 | // If structDecl is a forward declaration, this loop won't do |
| 1299 | // anything except look at designated initializers; That's okay, |
| 1300 | // because an error should get printed out elsewhere. It might be |
| 1301 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1302 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1303 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1304 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1305 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1306 | while (Index < IList->getNumInits()) { |
| 1307 | Expr *Init = IList->getInit(Index); |
| 1308 | |
| 1309 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1310 | // If we're not the subobject that matches up with the '{' for |
| 1311 | // the designator, we shouldn't be handling the |
| 1312 | // designator. Return immediately. |
| 1313 | if (!SubobjectIsDesignatorContext) |
| 1314 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1316 | // Handle this designated initializer. Field will be updated to |
| 1317 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1318 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1319 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1320 | StructuredList, StructuredIndex, |
| 1321 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1322 | hadError = true; |
| 1323 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1324 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Disable check for missing fields when designators are used. |
| 1327 | // This matches gcc behaviour. |
| 1328 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1329 | continue; |
| 1330 | } |
| 1331 | |
| 1332 | if (Field == FieldEnd) { |
| 1333 | // We've run out of fields. We're done. |
| 1334 | break; |
| 1335 | } |
| 1336 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1337 | // We've already initialized a member of a union. We're done. |
| 1338 | if (InitializedSomething && DeclType->isUnionType()) |
| 1339 | break; |
| 1340 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1341 | // If we've hit the flexible array member at the end, we're done. |
| 1342 | if (Field->getType()->isIncompleteArrayType()) |
| 1343 | break; |
| 1344 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1345 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1346 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1347 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1348 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1349 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1351 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1352 | bool InvalidUse; |
| 1353 | if (VerifyOnly) |
| 1354 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1355 | else |
| 1356 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
| 1357 | IList->getInit(Index)->getLocStart()); |
| 1358 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1359 | ++Index; |
| 1360 | ++Field; |
| 1361 | hadError = true; |
| 1362 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1363 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1364 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1365 | InitializedEntity MemberEntity = |
| 1366 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1367 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1368 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1369 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1370 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1371 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1372 | // Initialize the first field within the union. |
| 1373 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1374 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1375 | |
| 1376 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1377 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1378 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1379 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1380 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1381 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1382 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1383 | // It is possible we have one or more unnamed bitfields remaining. |
| 1384 | // Find first (if any) named field and emit warning. |
| 1385 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1386 | it != end; ++it) { |
| 1387 | if (!it->isUnnamedBitfield()) { |
| 1388 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1389 | diag::warn_missing_field_initializers) << it->getName(); |
| 1390 | break; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1396 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1397 | return; |
| 1398 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1399 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1400 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1401 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1402 | ++Index; |
| 1403 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1406 | InitializedEntity MemberEntity = |
| 1407 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1409 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1411 | StructuredList, StructuredIndex); |
| 1412 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1413 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1414 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1415 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1417 | /// \brief Expand a field designator that refers to a member of an |
| 1418 | /// anonymous struct or union into a series of field designators that |
| 1419 | /// refers to the field within the appropriate subobject. |
| 1420 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1421 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | DesignatedInitExpr *DIE, |
| 1423 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1424 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1425 | typedef DesignatedInitExpr::Designator Designator; |
| 1426 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1427 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1428 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1429 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1430 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1431 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1433 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1434 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1435 | else |
| 1436 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1437 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1438 | assert(isa<FieldDecl>(*PI)); |
| 1439 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | // Expand the current designator into the set of replacement |
| 1443 | // designators, so we have a full subobject path down to where the |
| 1444 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1445 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1446 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1447 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1450 | /// corresponds to FieldName. |
| 1451 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1452 | IdentifierInfo *FieldName) { |
| 1453 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1454 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1455 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1456 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1457 | return IF; |
| 1458 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1459 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1460 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1463 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1464 | DesignatedInitExpr *DIE) { |
| 1465 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1466 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1467 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1468 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1469 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1470 | DIE->size(), IndexExprs.data(), |
| 1471 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1472 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1473 | } |
| 1474 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1475 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1476 | /// |
| 1477 | /// Determines whether the designated initializer @p DIE, which |
| 1478 | /// resides at the given @p Index within the initializer list @p |
| 1479 | /// IList, is well-formed for a current object of type @p DeclType |
| 1480 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1482 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1483 | /// |
| 1484 | /// @param IList The initializer list in which this designated |
| 1485 | /// initializer occurs. |
| 1486 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1487 | /// @param DIE The designated initializer expression. |
| 1488 | /// |
| 1489 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1490 | /// |
| 1491 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1492 | /// into which the designation in @p DIE should refer. |
| 1493 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1494 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1495 | /// a field, this will be set to the field declaration corresponding |
| 1496 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1497 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1498 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1499 | /// DIE is an array designator or GNU array-range designator, this |
| 1500 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1501 | /// |
| 1502 | /// @param Index Index into @p IList where the designated initializer |
| 1503 | /// @p DIE occurs. |
| 1504 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1505 | /// @param StructuredList The initializer list expression that |
| 1506 | /// describes all of the subobject initializers in the order they'll |
| 1507 | /// actually be initialized. |
| 1508 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1509 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1511 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1512 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1513 | DesignatedInitExpr *DIE, |
| 1514 | unsigned DesigIdx, |
| 1515 | QualType &CurrentObjectType, |
| 1516 | RecordDecl::field_iterator *NextField, |
| 1517 | llvm::APSInt *NextElementIndex, |
| 1518 | unsigned &Index, |
| 1519 | InitListExpr *StructuredList, |
| 1520 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1521 | bool FinishSubobjectInit, |
| 1522 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1523 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1524 | // Check the actual initialization for the designated object type. |
| 1525 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Temporarily remove the designator expression from the |
| 1528 | // initializer list that the child calls see, so that we don't try |
| 1529 | // to re-process the designator. |
| 1530 | unsigned OldIndex = Index; |
| 1531 | IList->setInit(OldIndex, DIE->getInit()); |
| 1532 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1533 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1534 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Restore the designated initializer expression in the syntactic |
| 1537 | // form of the initializer list. |
| 1538 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1539 | DIE->setInit(IList->getInit(OldIndex)); |
| 1540 | IList->setInit(OldIndex, DIE); |
| 1541 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1542 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1545 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1546 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1547 | if (!VerifyOnly) { |
| 1548 | assert((IsFirstDesignator || StructuredList) && |
| 1549 | "Need a non-designated initializer list to start from"); |
| 1550 | |
| 1551 | // Determine the structural initializer list that corresponds to the |
| 1552 | // current subobject. |
| 1553 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 1554 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1555 | StructuredList, StructuredIndex, |
| 1556 | SourceRange(D->getStartLocation(), |
| 1557 | DIE->getSourceRange().getEnd())); |
| 1558 | assert(StructuredList && "Expected a structured initializer list"); |
| 1559 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1561 | if (D->isFieldDesignator()) { |
| 1562 | // C99 6.7.8p7: |
| 1563 | // |
| 1564 | // If a designator has the form |
| 1565 | // |
| 1566 | // . identifier |
| 1567 | // |
| 1568 | // then the current object (defined below) shall have |
| 1569 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1571 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1572 | if (!RT) { |
| 1573 | SourceLocation Loc = D->getDotLoc(); |
| 1574 | if (Loc.isInvalid()) |
| 1575 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1576 | if (!VerifyOnly) |
| 1577 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1578 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1579 | ++Index; |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1583 | // Note: we perform a linear search of the fields here, despite |
| 1584 | // the fact that we have a faster lookup method, because we always |
| 1585 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1586 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1587 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1588 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1590 | Field = RT->getDecl()->field_begin(), |
| 1591 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1592 | for (; Field != FieldEnd; ++Field) { |
| 1593 | if (Field->isUnnamedBitfield()) |
| 1594 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1595 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1596 | // If we find a field representing an anonymous field, look in the |
| 1597 | // IndirectFieldDecl that follow for the designated initializer. |
| 1598 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1599 | if (IndirectFieldDecl *IF = |
| 1600 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1601 | // In verify mode, don't modify the original. |
| 1602 | if (VerifyOnly) |
| 1603 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1604 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1605 | D = DIE->getDesignator(DesigIdx); |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1609 | if (KnownField && KnownField == *Field) |
| 1610 | break; |
| 1611 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | |
| 1614 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1617 | if (Field == FieldEnd) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1618 | if (VerifyOnly) |
| 1619 | return true; // No typo correction when just trying this out. |
| 1620 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1621 | // There was no normal field in the struct with the designated |
| 1622 | // name. Perform another lookup for this name, which may find |
| 1623 | // something that we can't designate (e.g., a member function), |
| 1624 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1626 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1627 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1628 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1629 | // Name lookup didn't find anything. Determine whether this |
| 1630 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1631 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1632 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1633 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1634 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1635 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1636 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1637 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1638 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1639 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1640 | std::string CorrectedStr( |
| 1641 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1642 | std::string CorrectedQuotedStr( |
| 1643 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1644 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1645 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1646 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1647 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1648 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1649 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1650 | } else { |
| 1651 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1652 | << FieldName << CurrentObjectType; |
| 1653 | ++Index; |
| 1654 | return true; |
| 1655 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1656 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1658 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1659 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1660 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1661 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1663 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1664 | ++Index; |
| 1665 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1666 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1667 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1668 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1669 | // The replacement field comes from typo correction; find it |
| 1670 | // in the list of fields. |
| 1671 | FieldIndex = 0; |
| 1672 | Field = RT->getDecl()->field_begin(); |
| 1673 | for (; Field != FieldEnd; ++Field) { |
| 1674 | if (Field->isUnnamedBitfield()) |
| 1675 | continue; |
| 1676 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1677 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1678 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1679 | break; |
| 1680 | |
| 1681 | ++FieldIndex; |
| 1682 | } |
| 1683 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1684 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1685 | |
| 1686 | // All of the fields of a union are located at the same place in |
| 1687 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1688 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1689 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1690 | if (!VerifyOnly) |
| 1691 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1692 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1694 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1695 | bool InvalidUse; |
| 1696 | if (VerifyOnly) |
| 1697 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1698 | else |
| 1699 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 1700 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1701 | ++Index; |
| 1702 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1703 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1704 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1705 | if (!VerifyOnly) { |
| 1706 | // Update the designator with the field declaration. |
| 1707 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1709 | // Make sure that our non-designated initializer list has space |
| 1710 | // for a subobject corresponding to this field. |
| 1711 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1712 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1713 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1715 | // This designator names a flexible array member. |
| 1716 | if (Field->getType()->isIncompleteArrayType()) { |
| 1717 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1718 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1719 | // We can't designate an object within the flexible array |
| 1720 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1721 | if (!VerifyOnly) { |
| 1722 | DesignatedInitExpr::Designator *NextD |
| 1723 | = DIE->getDesignator(DesigIdx + 1); |
| 1724 | SemaRef.Diag(NextD->getStartLocation(), |
| 1725 | diag::err_designator_into_flexible_array_member) |
| 1726 | << SourceRange(NextD->getStartLocation(), |
| 1727 | DIE->getSourceRange().getEnd()); |
| 1728 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1729 | << *Field; |
| 1730 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1731 | Invalid = true; |
| 1732 | } |
| 1733 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1734 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1735 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1736 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1737 | if (!VerifyOnly) { |
| 1738 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
| 1739 | diag::err_flexible_array_init_needs_braces) |
| 1740 | << DIE->getInit()->getSourceRange(); |
| 1741 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1742 | << *Field; |
| 1743 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1744 | Invalid = true; |
| 1745 | } |
| 1746 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1747 | // Check GNU flexible array initializer. |
| 1748 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1749 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1750 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (Invalid) { |
| 1753 | ++Index; |
| 1754 | return true; |
| 1755 | } |
| 1756 | |
| 1757 | // Initialize the array. |
| 1758 | bool prevHadError = hadError; |
| 1759 | unsigned newStructuredIndex = FieldIndex; |
| 1760 | unsigned OldIndex = Index; |
| 1761 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1762 | |
| 1763 | InitializedEntity MemberEntity = |
| 1764 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1765 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1766 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1768 | IList->setInit(OldIndex, DIE); |
| 1769 | if (hadError && !prevHadError) { |
| 1770 | ++Field; |
| 1771 | ++FieldIndex; |
| 1772 | if (NextField) |
| 1773 | *NextField = Field; |
| 1774 | StructuredIndex = FieldIndex; |
| 1775 | return true; |
| 1776 | } |
| 1777 | } else { |
| 1778 | // Recurse to check later designated subobjects. |
| 1779 | QualType FieldType = (*Field)->getType(); |
| 1780 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1782 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1783 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1785 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1786 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1787 | true, false)) |
| 1788 | return true; |
| 1789 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Find the position of the next field to be initialized in this |
| 1792 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1793 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1795 | |
| 1796 | // If this the first designator, our caller will continue checking |
| 1797 | // the rest of this struct/class/union subobject. |
| 1798 | if (IsFirstDesignator) { |
| 1799 | if (NextField) |
| 1800 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1801 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1802 | return false; |
| 1803 | } |
| 1804 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1805 | if (!FinishSubobjectInit) |
| 1806 | return false; |
| 1807 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1808 | // We've already initialized something in the union; we're done. |
| 1809 | if (RT->getDecl()->isUnion()) |
| 1810 | return hadError; |
| 1811 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1812 | // Check the remaining fields within this class/struct/union subobject. |
| 1813 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1814 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1815 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1816 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1817 | return hadError && !prevHadError; |
| 1818 | } |
| 1819 | |
| 1820 | // C99 6.7.8p6: |
| 1821 | // |
| 1822 | // If a designator has the form |
| 1823 | // |
| 1824 | // [ constant-expression ] |
| 1825 | // |
| 1826 | // then the current object (defined below) shall have array |
| 1827 | // type and the expression shall be an integer constant |
| 1828 | // expression. If the array is of unknown size, any |
| 1829 | // nonnegative value is valid. |
| 1830 | // |
| 1831 | // Additionally, cope with the GNU extension that permits |
| 1832 | // designators of the form |
| 1833 | // |
| 1834 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1835 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1836 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1837 | if (!VerifyOnly) |
| 1838 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1839 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1840 | ++Index; |
| 1841 | return true; |
| 1842 | } |
| 1843 | |
| 1844 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1845 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1846 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1847 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1848 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1849 | DesignatedEndIndex = DesignatedStartIndex; |
| 1850 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1851 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1852 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1854 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1856 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1857 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1859 | // Codegen can't handle evaluating array range designators that have side |
| 1860 | // effects, because we replicate the AST value for each initialized element. |
| 1861 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1862 | // elements with something that has a side effect, so codegen can emit an |
| 1863 | // "error unsupported" error instead of miscompiling the app. |
| 1864 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1865 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1866 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1869 | if (isa<ConstantArrayType>(AT)) { |
| 1870 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1871 | DesignatedStartIndex |
| 1872 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1873 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1874 | DesignatedEndIndex |
| 1875 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1876 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1877 | if (DesignatedEndIndex >= MaxElements) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1878 | if (VerifyOnly) |
| 1879 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
| 1880 | diag::err_array_designator_too_large) |
| 1881 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1882 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1883 | ++Index; |
| 1884 | return true; |
| 1885 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1886 | } else { |
| 1887 | // Make sure the bit-widths and signedness match. |
| 1888 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1889 | DesignatedEndIndex |
| 1890 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1891 | else if (DesignatedStartIndex.getBitWidth() < |
| 1892 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1893 | DesignatedStartIndex |
| 1894 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1895 | DesignatedStartIndex.setIsUnsigned(true); |
| 1896 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1897 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1899 | // Make sure that our non-designated initializer list has space |
| 1900 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1901 | if (!VerifyOnly && |
| 1902 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1904 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1906 | // Repeatedly perform subobject initializations in the range |
| 1907 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1909 | // Move to the next designator |
| 1910 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1911 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1912 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1913 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1914 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1916 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1917 | // Recurse to check later designated subobjects. |
| 1918 | QualType ElementType = AT->getElementType(); |
| 1919 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1921 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1922 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1923 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1924 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1925 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1926 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1927 | return true; |
| 1928 | |
| 1929 | // Move to the next index in the array that we'll be initializing. |
| 1930 | ++DesignatedStartIndex; |
| 1931 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1932 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1933 | |
| 1934 | // If this the first designator, our caller will continue checking |
| 1935 | // the rest of this array subobject. |
| 1936 | if (IsFirstDesignator) { |
| 1937 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1938 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1939 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1940 | return false; |
| 1941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1943 | if (!FinishSubobjectInit) |
| 1944 | return false; |
| 1945 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1946 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1947 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1949 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1950 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1954 | // Get the structured initializer list for a subobject of type |
| 1955 | // @p CurrentObjectType. |
| 1956 | InitListExpr * |
| 1957 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1958 | QualType CurrentObjectType, |
| 1959 | InitListExpr *StructuredList, |
| 1960 | unsigned StructuredIndex, |
| 1961 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1962 | if (VerifyOnly) |
| 1963 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1964 | Expr *ExistingInit = 0; |
| 1965 | if (!StructuredList) |
| 1966 | ExistingInit = SyntacticToSemantic[IList]; |
| 1967 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1968 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1970 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1971 | return Result; |
| 1972 | |
| 1973 | if (ExistingInit) { |
| 1974 | // We are creating an initializer list that initializes the |
| 1975 | // subobjects of the current object, but there was already an |
| 1976 | // initialization that completely initialized the current |
| 1977 | // subobject, e.g., by a compound literal: |
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 | // struct X { int a, b; }; |
| 1980 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1983 | // designated initializer re-initializes the whole |
| 1984 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1986 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1987 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1989 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1990 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1991 | << ExistingInit->getSourceRange(); |
| 1992 | } |
| 1993 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1995 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1996 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1997 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1999 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2001 | // Pre-allocate storage for the structured initializer list. |
| 2002 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2003 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2004 | bool GotNumInits = false; |
| 2005 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2006 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2007 | GotNumInits = true; |
| 2008 | } else if (Index < IList->getNumInits()) { |
| 2009 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2010 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2011 | GotNumInits = true; |
| 2012 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2016 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2017 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2018 | NumElements = CAType->getSize().getZExtValue(); |
| 2019 | // Simple heuristic so that we don't allocate a very large |
| 2020 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2021 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2022 | NumElements = 0; |
| 2023 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2024 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2025 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2026 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2027 | RecordDecl *RDecl = RType->getDecl(); |
| 2028 | if (RDecl->isUnion()) |
| 2029 | NumElements = 1; |
| 2030 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2032 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2035 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2036 | NumElements = IList->getNumInits(); |
| 2037 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2038 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2040 | // Link this new initializer list into the structured initializer |
| 2041 | // lists. |
| 2042 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2043 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2044 | else { |
| 2045 | Result->setSyntacticForm(IList); |
| 2046 | SyntacticToSemantic[IList] = Result; |
| 2047 | } |
| 2048 | |
| 2049 | return Result; |
| 2050 | } |
| 2051 | |
| 2052 | /// Update the initializer at index @p StructuredIndex within the |
| 2053 | /// structured initializer list to the value @p expr. |
| 2054 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2055 | unsigned &StructuredIndex, |
| 2056 | Expr *expr) { |
| 2057 | // No structured initializer list to update |
| 2058 | if (!StructuredList) |
| 2059 | return; |
| 2060 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2061 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2062 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2063 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2065 | diag::warn_initializer_overrides) |
| 2066 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2068 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2069 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2070 | << PrevInit->getSourceRange(); |
| 2071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2073 | ++StructuredIndex; |
| 2074 | } |
| 2075 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2076 | /// Check that the given Index expression is a valid array designator |
| 2077 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2078 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2079 | /// and produces a reasonable diagnostic if there is a |
| 2080 | /// failure. Returns true if there was an error, false otherwise. If |
| 2081 | /// everything went okay, Value will receive the value of the constant |
| 2082 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2084 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2085 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 2086 | |
| 2087 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2088 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 2089 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2090 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2091 | if (Value.isSigned() && Value.isNegative()) |
| 2092 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2093 | << Value.toString(10) << Index->getSourceRange(); |
| 2094 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2095 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2096 | return false; |
| 2097 | } |
| 2098 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2099 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2100 | SourceLocation Loc, |
| 2101 | bool GNUSyntax, |
| 2102 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2103 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2104 | |
| 2105 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2106 | SmallVector<ASTDesignator, 32> Designators; |
| 2107 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Build designators and check array designator expressions. |
| 2110 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2111 | const Designator &D = Desig.getDesignator(Idx); |
| 2112 | switch (D.getKind()) { |
| 2113 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2115 | D.getFieldLoc())); |
| 2116 | break; |
| 2117 | |
| 2118 | case Designator::ArrayDesignator: { |
| 2119 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2120 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2121 | if (!Index->isTypeDependent() && |
| 2122 | !Index->isValueDependent() && |
| 2123 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2124 | Invalid = true; |
| 2125 | else { |
| 2126 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2127 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2128 | D.getRBracketLoc())); |
| 2129 | InitExpressions.push_back(Index); |
| 2130 | } |
| 2131 | break; |
| 2132 | } |
| 2133 | |
| 2134 | case Designator::ArrayRangeDesignator: { |
| 2135 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2136 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2137 | llvm::APSInt StartValue; |
| 2138 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2139 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2140 | StartIndex->isValueDependent(); |
| 2141 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2142 | EndIndex->isValueDependent(); |
| 2143 | if ((!StartDependent && |
| 2144 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2145 | (!EndDependent && |
| 2146 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2147 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2148 | else { |
| 2149 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2150 | if (StartDependent || EndDependent) { |
| 2151 | // Nothing to compute. |
| 2152 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2153 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2154 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2155 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2157 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2158 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2160 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2161 | Invalid = true; |
| 2162 | } else { |
| 2163 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2165 | D.getEllipsisLoc(), |
| 2166 | D.getRBracketLoc())); |
| 2167 | InitExpressions.push_back(StartIndex); |
| 2168 | InitExpressions.push_back(EndIndex); |
| 2169 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2170 | } |
| 2171 | break; |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | if (Invalid || Init.isInvalid()) |
| 2177 | return ExprError(); |
| 2178 | |
| 2179 | // Clear out the expressions within the designation. |
| 2180 | Desig.ClearExprs(*this); |
| 2181 | |
| 2182 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2183 | = DesignatedInitExpr::Create(Context, |
| 2184 | Designators.data(), Designators.size(), |
| 2185 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2186 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2188 | if (getLangOptions().CPlusPlus) |
Eli Friedman | a47317b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2189 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2190 | << DIE->getSourceRange(); |
| 2191 | else if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2192 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2193 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2195 | return Owned(DIE); |
| 2196 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2198 | //===----------------------------------------------------------------------===// |
| 2199 | // Initialization entity |
| 2200 | //===----------------------------------------------------------------------===// |
| 2201 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2202 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2203 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2205 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2206 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2207 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2208 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2209 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2210 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2211 | Type = VT->getElementType(); |
| 2212 | } else { |
| 2213 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2214 | assert(CT && "Unexpected type"); |
| 2215 | Kind = EK_ComplexElement; |
| 2216 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2217 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2221 | CXXBaseSpecifier *Base, |
| 2222 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2223 | { |
| 2224 | InitializedEntity Result; |
| 2225 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2226 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2227 | if (IsInheritedVirtualBase) |
| 2228 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2230 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2231 | return Result; |
| 2232 | } |
| 2233 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2234 | DeclarationName InitializedEntity::getName() const { |
| 2235 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2236 | case EK_Parameter: { |
| 2237 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2238 | return (D ? D->getDeclName() : DeclarationName()); |
| 2239 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2240 | |
| 2241 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2242 | case EK_Member: |
| 2243 | return VariableOrMember->getDeclName(); |
| 2244 | |
| 2245 | case EK_Result: |
| 2246 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2247 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2248 | case EK_Temporary: |
| 2249 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2250 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2251 | case EK_ArrayElement: |
| 2252 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2253 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2254 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2255 | return DeclarationName(); |
| 2256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2258 | // Silence GCC warning |
| 2259 | return DeclarationName(); |
| 2260 | } |
| 2261 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2262 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2263 | switch (getKind()) { |
| 2264 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2265 | case EK_Member: |
| 2266 | return VariableOrMember; |
| 2267 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2268 | case EK_Parameter: |
| 2269 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2270 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2271 | case EK_Result: |
| 2272 | case EK_Exception: |
| 2273 | case EK_New: |
| 2274 | case EK_Temporary: |
| 2275 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2276 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2277 | case EK_ArrayElement: |
| 2278 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2279 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2280 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2284 | // Silence GCC warning |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2288 | bool InitializedEntity::allowsNRVO() const { |
| 2289 | switch (getKind()) { |
| 2290 | case EK_Result: |
| 2291 | case EK_Exception: |
| 2292 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2294 | case EK_Variable: |
| 2295 | case EK_Parameter: |
| 2296 | case EK_Member: |
| 2297 | case EK_New: |
| 2298 | case EK_Temporary: |
| 2299 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2300 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2301 | case EK_ArrayElement: |
| 2302 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2303 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2304 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | } |
| 2307 | |
| 2308 | return false; |
| 2309 | } |
| 2310 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2311 | //===----------------------------------------------------------------------===// |
| 2312 | // Initialization sequence |
| 2313 | //===----------------------------------------------------------------------===// |
| 2314 | |
| 2315 | void InitializationSequence::Step::Destroy() { |
| 2316 | switch (Kind) { |
| 2317 | case SK_ResolveAddressOfOverloadedFunction: |
| 2318 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2319 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2320 | case SK_CastDerivedToBaseLValue: |
| 2321 | case SK_BindReference: |
| 2322 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2323 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2324 | case SK_UserConversion: |
| 2325 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2326 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2327 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2328 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2329 | case SK_ListConstructorCall: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2330 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2331 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2332 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2333 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2334 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2335 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2336 | case SK_PassByIndirectCopyRestore: |
| 2337 | case SK_PassByIndirectRestore: |
| 2338 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2339 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2341 | case SK_ConversionSequence: |
| 2342 | delete ICS; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2346 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2347 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2351 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2352 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2354 | switch (getFailureKind()) { |
| 2355 | case FK_TooManyInitsForReference: |
| 2356 | case FK_ArrayNeedsInitList: |
| 2357 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2358 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2359 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2360 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2361 | case FK_RValueReferenceBindingToLValue: |
| 2362 | case FK_ReferenceInitDropsQualifiers: |
| 2363 | case FK_ReferenceInitFailed: |
| 2364 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2365 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2366 | case FK_TooManyInitsForScalar: |
| 2367 | case FK_ReferenceBindingToInitList: |
| 2368 | case FK_InitListBadDestinationType: |
| 2369 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2370 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2371 | case FK_ArrayTypeMismatch: |
| 2372 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2373 | case FK_ListInitializationFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2374 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2375 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2376 | case FK_ReferenceInitOverloadFailed: |
| 2377 | case FK_UserConversionOverloadFailed: |
| 2378 | case FK_ConstructorOverloadFailed: |
| 2379 | return FailedOverloadResult == OR_Ambiguous; |
| 2380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2382 | return false; |
| 2383 | } |
| 2384 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2385 | bool InitializationSequence::isConstructorInitialization() const { |
| 2386 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2387 | } |
| 2388 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2389 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2390 | const Expr *Initializer, |
| 2391 | bool *isInitializerConstant, |
| 2392 | APValue *ConstantValue) const { |
| 2393 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2394 | return false; |
| 2395 | |
| 2396 | const Step &LastStep = Steps.back(); |
| 2397 | if (LastStep.Kind != SK_ConversionSequence) |
| 2398 | return false; |
| 2399 | |
| 2400 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2401 | const StandardConversionSequence *SCS = NULL; |
| 2402 | switch (ICS.getKind()) { |
| 2403 | case ImplicitConversionSequence::StandardConversion: |
| 2404 | SCS = &ICS.Standard; |
| 2405 | break; |
| 2406 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2407 | SCS = &ICS.UserDefined.After; |
| 2408 | break; |
| 2409 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2410 | case ImplicitConversionSequence::EllipsisConversion: |
| 2411 | case ImplicitConversionSequence::BadConversion: |
| 2412 | return false; |
| 2413 | } |
| 2414 | |
| 2415 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2416 | // [dcl.init.list]p7: |
| 2417 | // |
| 2418 | // A narrowing conversion is an implicit conversion ... |
| 2419 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2420 | QualType FromType = SCS->getToType(0); |
| 2421 | QualType ToType = SCS->getToType(1); |
| 2422 | switch (PossibleNarrowing) { |
| 2423 | // * from a floating-point type to an integer type, or |
| 2424 | // |
| 2425 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2426 | // type, except where the source is a constant expression and the actual |
| 2427 | // value after conversion will fit into the target type and will produce |
| 2428 | // the original value when converted back to the original type, or |
| 2429 | case ICK_Floating_Integral: |
| 2430 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2431 | *isInitializerConstant = false; |
| 2432 | return true; |
| 2433 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2434 | llvm::APSInt IntConstantValue; |
| 2435 | if (Initializer && |
| 2436 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2437 | // Convert the integer to the floating type. |
| 2438 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2439 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2440 | llvm::APFloat::rmNearestTiesToEven); |
| 2441 | // And back. |
| 2442 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2443 | bool ignored; |
| 2444 | Result.convertToInteger(ConvertedValue, |
| 2445 | llvm::APFloat::rmTowardZero, &ignored); |
| 2446 | // If the resulting value is different, this was a narrowing conversion. |
| 2447 | if (IntConstantValue != ConvertedValue) { |
| 2448 | *isInitializerConstant = true; |
| 2449 | *ConstantValue = APValue(IntConstantValue); |
| 2450 | return true; |
| 2451 | } |
| 2452 | } else { |
| 2453 | // Variables are always narrowings. |
| 2454 | *isInitializerConstant = false; |
| 2455 | return true; |
| 2456 | } |
| 2457 | } |
| 2458 | return false; |
| 2459 | |
| 2460 | // * from long double to double or float, or from double to float, except |
| 2461 | // where the source is a constant expression and the actual value after |
| 2462 | // conversion is within the range of values that can be represented (even |
| 2463 | // if it cannot be represented exactly), or |
| 2464 | case ICK_Floating_Conversion: |
| 2465 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2466 | // FromType is larger than ToType. |
| 2467 | Expr::EvalResult InitializerValue; |
| 2468 | // FIXME: Check whether Initializer is a constant expression according |
| 2469 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2470 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2471 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2472 | // Constant! (Except for FIXME above.) |
| 2473 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2474 | // Convert the source value into the target type. |
| 2475 | bool ignored; |
| 2476 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2477 | Ctx.getFloatTypeSemantics(ToType), |
| 2478 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2479 | // If there was no overflow, the source value is within the range of |
| 2480 | // values that can be represented. |
| 2481 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2482 | *isInitializerConstant = true; |
| 2483 | *ConstantValue = InitializerValue.Val; |
| 2484 | return true; |
| 2485 | } |
| 2486 | } else { |
| 2487 | *isInitializerConstant = false; |
| 2488 | return true; |
| 2489 | } |
| 2490 | } |
| 2491 | return false; |
| 2492 | |
| 2493 | // * from an integer type or unscoped enumeration type to an integer type |
| 2494 | // that cannot represent all the values of the original type, except where |
| 2495 | // the source is a constant expression and the actual value after |
| 2496 | // conversion will fit into the target type and will produce the original |
| 2497 | // value when converted back to the original type. |
Jeffrey Yasskin | 6d0ee8d | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2498 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | b89d5ed | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2499 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2500 | // Boolean conversions can be from pointers and pointers to members |
| 2501 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2502 | return false; |
| 2503 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2504 | case ICK_Integral_Conversion: { |
| 2505 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2506 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2507 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2508 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2509 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2510 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2511 | |
| 2512 | if (FromWidth > ToWidth || |
| 2513 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2514 | // Not all values of FromType can be represented in ToType. |
| 2515 | llvm::APSInt InitializerValue; |
| 2516 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2517 | *isInitializerConstant = true; |
| 2518 | *ConstantValue = APValue(InitializerValue); |
| 2519 | |
| 2520 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2521 | // signed vs. unsigned comparisons. |
| 2522 | InitializerValue = InitializerValue.extend( |
| 2523 | InitializerValue.getBitWidth() + 1); |
| 2524 | // Convert the initializer to and from the target width and signed-ness. |
| 2525 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2526 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2527 | ConvertedValue.setIsSigned(ToSigned); |
| 2528 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2529 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2530 | // If the result is different, this was a narrowing conversion. |
| 2531 | return ConvertedValue != InitializerValue; |
| 2532 | } else { |
| 2533 | // Variables are always narrowings. |
| 2534 | *isInitializerConstant = false; |
| 2535 | return true; |
| 2536 | } |
| 2537 | } |
| 2538 | return false; |
| 2539 | } |
| 2540 | |
| 2541 | default: |
| 2542 | // Other kinds of conversions are not narrowings. |
| 2543 | return false; |
| 2544 | } |
| 2545 | } |
| 2546 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2547 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2548 | FunctionDecl *Function, |
| 2549 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2550 | Step S; |
| 2551 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2552 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2553 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2554 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2555 | Steps.push_back(S); |
| 2556 | } |
| 2557 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2558 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2559 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2560 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2561 | switch (VK) { |
| 2562 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2563 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2564 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2565 | default: llvm_unreachable("No such category"); |
| 2566 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2567 | S.Type = BaseType; |
| 2568 | Steps.push_back(S); |
| 2569 | } |
| 2570 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2571 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2572 | bool BindingTemporary) { |
| 2573 | Step S; |
| 2574 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2575 | S.Type = T; |
| 2576 | Steps.push_back(S); |
| 2577 | } |
| 2578 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2579 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2580 | Step S; |
| 2581 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2582 | S.Type = T; |
| 2583 | Steps.push_back(S); |
| 2584 | } |
| 2585 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2586 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2587 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2588 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2589 | Step S; |
| 2590 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2591 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2592 | S.Function.Function = Function; |
| 2593 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2594 | Steps.push_back(S); |
| 2595 | } |
| 2596 | |
| 2597 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2598 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2599 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2600 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2601 | switch (VK) { |
| 2602 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2603 | S.Kind = SK_QualificationConversionRValue; |
| 2604 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2605 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2606 | S.Kind = SK_QualificationConversionXValue; |
| 2607 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2608 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2609 | S.Kind = SK_QualificationConversionLValue; |
| 2610 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2611 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2612 | S.Type = Ty; |
| 2613 | Steps.push_back(S); |
| 2614 | } |
| 2615 | |
| 2616 | void InitializationSequence::AddConversionSequenceStep( |
| 2617 | const ImplicitConversionSequence &ICS, |
| 2618 | QualType T) { |
| 2619 | Step S; |
| 2620 | S.Kind = SK_ConversionSequence; |
| 2621 | S.Type = T; |
| 2622 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2623 | Steps.push_back(S); |
| 2624 | } |
| 2625 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2626 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2627 | Step S; |
| 2628 | S.Kind = SK_ListInitialization; |
| 2629 | S.Type = T; |
| 2630 | Steps.push_back(S); |
| 2631 | } |
| 2632 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2633 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2634 | InitializationSequence::AddConstructorInitializationStep( |
| 2635 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2636 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2637 | QualType T) { |
| 2638 | Step S; |
| 2639 | S.Kind = SK_ConstructorInitialization; |
| 2640 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2641 | S.Function.Function = Constructor; |
| 2642 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2643 | Steps.push_back(S); |
| 2644 | } |
| 2645 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2646 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2647 | Step S; |
| 2648 | S.Kind = SK_ZeroInitialization; |
| 2649 | S.Type = T; |
| 2650 | Steps.push_back(S); |
| 2651 | } |
| 2652 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2653 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2654 | Step S; |
| 2655 | S.Kind = SK_CAssignment; |
| 2656 | S.Type = T; |
| 2657 | Steps.push_back(S); |
| 2658 | } |
| 2659 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2660 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2661 | Step S; |
| 2662 | S.Kind = SK_StringInit; |
| 2663 | S.Type = T; |
| 2664 | Steps.push_back(S); |
| 2665 | } |
| 2666 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2667 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2668 | Step S; |
| 2669 | S.Kind = SK_ObjCObjectConversion; |
| 2670 | S.Type = T; |
| 2671 | Steps.push_back(S); |
| 2672 | } |
| 2673 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2674 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2675 | Step S; |
| 2676 | S.Kind = SK_ArrayInit; |
| 2677 | S.Type = T; |
| 2678 | Steps.push_back(S); |
| 2679 | } |
| 2680 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2681 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2682 | bool shouldCopy) { |
| 2683 | Step s; |
| 2684 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2685 | : SK_PassByIndirectRestore); |
| 2686 | s.Type = type; |
| 2687 | Steps.push_back(s); |
| 2688 | } |
| 2689 | |
| 2690 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2691 | Step S; |
| 2692 | S.Kind = SK_ProduceObjCObject; |
| 2693 | S.Type = T; |
| 2694 | Steps.push_back(S); |
| 2695 | } |
| 2696 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2698 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2699 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2700 | this->Failure = Failure; |
| 2701 | this->FailedOverloadResult = Result; |
| 2702 | } |
| 2703 | |
| 2704 | //===----------------------------------------------------------------------===// |
| 2705 | // Attempt initialization |
| 2706 | //===----------------------------------------------------------------------===// |
| 2707 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2708 | static void MaybeProduceObjCObject(Sema &S, |
| 2709 | InitializationSequence &Sequence, |
| 2710 | const InitializedEntity &Entity) { |
| 2711 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2712 | |
| 2713 | /// When initializing a parameter, produce the value if it's marked |
| 2714 | /// __attribute__((ns_consumed)). |
| 2715 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2716 | if (!Entity.isParameterConsumed()) |
| 2717 | return; |
| 2718 | |
| 2719 | assert(Entity.getType()->isObjCRetainableType() && |
| 2720 | "consuming an object of unretainable type?"); |
| 2721 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2722 | |
| 2723 | /// When initializing a return value, if the return type is a |
| 2724 | /// retainable type, then returns need to immediately retain the |
| 2725 | /// object. If an autorelease is required, it will be done at the |
| 2726 | /// last instant. |
| 2727 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2728 | if (!Entity.getType()->isObjCRetainableType()) |
| 2729 | return; |
| 2730 | |
| 2731 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2732 | } |
| 2733 | } |
| 2734 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2735 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2736 | static void TryListInitialization(Sema &S, |
| 2737 | const InitializedEntity &Entity, |
| 2738 | const InitializationKind &Kind, |
| 2739 | InitListExpr *InitList, |
| 2740 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2741 | QualType DestType = Entity.getType(); |
| 2742 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2743 | // C++ doesn't allow scalar initialization with more than one argument. |
| 2744 | // But C99 complex numbers are scalars and it makes sense there. |
| 2745 | if (S.getLangOptions().CPlusPlus && DestType->isScalarType() && |
| 2746 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 2747 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2748 | return; |
| 2749 | } |
| 2750 | // FIXME: C++0x defines behavior for these two cases. |
| 2751 | if (DestType->isReferenceType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2752 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2753 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2754 | } |
| 2755 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2756 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2757 | return; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2760 | InitListChecker CheckInitList(S, Entity, InitList, |
| 2761 | DestType, /*VerifyOnly=*/true); |
| 2762 | if (CheckInitList.HadError()) { |
| 2763 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 2764 | return; |
| 2765 | } |
| 2766 | |
| 2767 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2768 | Sequence.AddListInitializationStep(DestType); |
| 2769 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2770 | |
| 2771 | /// \brief Try a reference initialization that involves calling a conversion |
| 2772 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2773 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2774 | const InitializedEntity &Entity, |
| 2775 | const InitializationKind &Kind, |
| 2776 | Expr *Initializer, |
| 2777 | bool AllowRValues, |
| 2778 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2779 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2780 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2781 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2782 | QualType cv2T2 = Initializer->getType(); |
| 2783 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2784 | |
| 2785 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2786 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2787 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2788 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2789 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2790 | ObjCConversion, |
| 2791 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2792 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2793 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2794 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2795 | (void)ObjCLifetimeConversion; |
| 2796 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2797 | // Build the candidate set directly in the initialization sequence |
| 2798 | // structure, so that it will persist if we fail. |
| 2799 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2800 | CandidateSet.clear(); |
| 2801 | |
| 2802 | // Determine whether we are allowed to call explicit constructors or |
| 2803 | // explicit conversion operators. |
| 2804 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2806 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2807 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2808 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2809 | // The type we're converting to is a class type. Enumerate its constructors |
| 2810 | // to see if there is a suitable conversion. |
| 2811 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2813 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2814 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2815 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2816 | NamedDecl *D = *Con; |
| 2817 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2818 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2819 | // Find the constructor (which may be a template). |
| 2820 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2821 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2822 | if (ConstructorTmpl) |
| 2823 | Constructor = cast<CXXConstructorDecl>( |
| 2824 | ConstructorTmpl->getTemplatedDecl()); |
| 2825 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2826 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2827 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2828 | if (!Constructor->isInvalidDecl() && |
| 2829 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2830 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2831 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2832 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2833 | &Initializer, 1, CandidateSet, |
| 2834 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2835 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2836 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2837 | &Initializer, 1, CandidateSet, |
| 2838 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2839 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2841 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2842 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2843 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2845 | const RecordType *T2RecordType = 0; |
| 2846 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2847 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2848 | // The type we're converting from is a class type, enumerate its conversion |
| 2849 | // functions. |
| 2850 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2851 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2852 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2853 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2854 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2855 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2856 | NamedDecl *D = *I; |
| 2857 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2858 | if (isa<UsingShadowDecl>(D)) |
| 2859 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2861 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2862 | CXXConversionDecl *Conv; |
| 2863 | if (ConvTemplate) |
| 2864 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2865 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2866 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2868 | // If the conversion function doesn't return a reference type, |
| 2869 | // it can't be considered for this conversion unless we're allowed to |
| 2870 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2871 | // FIXME: Do we need to make sure that we only consider conversion |
| 2872 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2873 | // break recursion. |
| 2874 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2875 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2876 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2877 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2878 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2879 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2880 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2881 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2882 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2886 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2887 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2889 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2890 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2891 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2892 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2893 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2894 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2895 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2896 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2897 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2898 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2899 | // This is the overload that will actually be used for the initialization, so |
| 2900 | // mark it as used. |
| 2901 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2902 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2903 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2904 | if (isa<CXXConversionDecl>(Function)) |
| 2905 | T2 = Function->getResultType(); |
| 2906 | else |
| 2907 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2908 | |
| 2909 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2910 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2911 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2912 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2913 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2914 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2915 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2916 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2917 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2918 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2919 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2921 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2922 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2923 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2924 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2925 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2926 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2927 | NewDerivedToBase, NewObjCConversion, |
| 2928 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2929 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2930 | // If the type we've converted to is not reference-related to the |
| 2931 | // type we're looking for, then there is another conversion step |
| 2932 | // we need to perform to produce a temporary of the right type |
| 2933 | // that we'll be binding to. |
| 2934 | ImplicitConversionSequence ICS; |
| 2935 | ICS.setStandard(); |
| 2936 | ICS.Standard = Best->FinalConversion; |
| 2937 | T2 = ICS.Standard.getToType(2); |
| 2938 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2939 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2940 | Sequence.AddDerivedToBaseCastStep( |
| 2941 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2942 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2943 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2944 | else if (NewObjCConversion) |
| 2945 | Sequence.AddObjCObjectConversionStep( |
| 2946 | S.Context.getQualifiedType(T1, |
| 2947 | T2.getNonReferenceType().getQualifiers())); |
| 2948 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2949 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2950 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2951 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2952 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2953 | return OR_Success; |
| 2954 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2955 | |
| 2956 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2957 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2958 | const InitializedEntity &Entity, |
| 2959 | const InitializationKind &Kind, |
| 2960 | Expr *Initializer, |
| 2961 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2962 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2963 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2964 | Qualifiers T1Quals; |
| 2965 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2966 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2967 | Qualifiers T2Quals; |
| 2968 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2969 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2971 | // If the initializer is the address of an overloaded function, try |
| 2972 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2973 | // type of the resulting function. |
| 2974 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2975 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2977 | T1, |
| 2978 | false, |
| 2979 | Found)) { |
| 2980 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2981 | cv2T2 = Fn->getType(); |
| 2982 | T2 = cv2T2.getUnqualifiedType(); |
| 2983 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2984 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2985 | return; |
| 2986 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2987 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2988 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2989 | // Compute some basic properties of the types and the initializer. |
| 2990 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2991 | bool isRValueRef = !isLValueRef; |
| 2992 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2993 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2994 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2995 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2996 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2997 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2998 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3000 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3001 | // 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] | 3002 | // "cv2 T2" as follows: |
| 3003 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3004 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3005 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3006 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3007 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3008 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3009 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3010 | bool T1Function = T1->isFunctionType(); |
| 3011 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3012 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3013 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3014 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3015 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3016 | // - 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] | 3017 | // reference-compatible with "cv2 T2," or |
| 3018 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3019 | // 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] | 3020 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3021 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3022 | // to decide whether we're actually binding to a temporary created from |
| 3023 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3024 | if (DerivedToBase) |
| 3025 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3027 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3028 | else if (ObjCConversion) |
| 3029 | Sequence.AddObjCObjectConversionStep( |
| 3030 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3031 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3032 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3033 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3034 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3035 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3036 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3037 | return; |
| 3038 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3039 | |
| 3040 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3041 | // reference-related to T2, and can be implicitly converted to an |
| 3042 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3043 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3044 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3045 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3046 | // If we have an rvalue ref to function type here, the rhs must be |
| 3047 | // an rvalue. |
| 3048 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3049 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3051 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3052 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3053 | Sequence); |
| 3054 | if (ConvOvlResult == OR_Success) |
| 3055 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3056 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3057 | Sequence.SetOverloadFailure( |
| 3058 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3059 | ConvOvlResult); |
| 3060 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3061 | } |
| 3062 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3063 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3064 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3065 | // 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] | 3066 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3067 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3068 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3069 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3070 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3071 | Sequence.SetOverloadFailure( |
| 3072 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3073 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3074 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3075 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3076 | ? (RefRelationship == Sema::Ref_Related |
| 3077 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3078 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3079 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3080 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3081 | return; |
| 3082 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3084 | // - If the initializer expression |
| 3085 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3086 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3087 | // Note: functions are handled below. |
| 3088 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3089 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3091 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3092 | (InitCategory.isXValue() || |
| 3093 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3094 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3095 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3096 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3097 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3098 | // compiler the freedom to perform a copy here or bind to the |
| 3099 | // object, while C++0x requires that we bind directly to the |
| 3100 | // object. Hence, we always bind to the object without making an |
| 3101 | // extra copy. However, in C++03 requires that we check for the |
| 3102 | // presence of a suitable copy constructor: |
| 3103 | // |
| 3104 | // The constructor that would be used to make the copy shall |
| 3105 | // be callable whether or not the copy is actually done. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3106 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3107 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3108 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3110 | if (DerivedToBase) |
| 3111 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3112 | ValueKind); |
| 3113 | else if (ObjCConversion) |
| 3114 | Sequence.AddObjCObjectConversionStep( |
| 3115 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3117 | if (T1Quals != T2Quals) |
| 3118 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3119 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3120 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3122 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3123 | |
| 3124 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3125 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3126 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3127 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3128 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3129 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3130 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3131 | Kind, Initializer, |
| 3132 | /*AllowRValues=*/true, |
| 3133 | Sequence); |
| 3134 | if (ConvOvlResult) |
| 3135 | Sequence.SetOverloadFailure( |
| 3136 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3137 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3138 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3139 | return; |
| 3140 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3142 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3143 | return; |
| 3144 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3145 | |
| 3146 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3147 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3148 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3149 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3150 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3151 | // Determine whether we are allowed to call explicit constructors or |
| 3152 | // explicit conversion operators. |
| 3153 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3154 | |
| 3155 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3156 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3157 | ImplicitConversionSequence ICS |
| 3158 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3159 | /*SuppressUserConversions*/ false, |
| 3160 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3161 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3162 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3163 | /*AllowObjCWritebackConversion=*/false); |
| 3164 | |
| 3165 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3166 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3167 | // this into an overloading ambiguity diagnostic. However, we need |
| 3168 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3169 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3170 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3171 | Sequence.SetOverloadFailure( |
| 3172 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3173 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3174 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3175 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3176 | else |
| 3177 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3178 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3179 | } else { |
| 3180 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3184 | // same cv-qualification as, or greater cv-qualification |
| 3185 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3186 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3187 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3189 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3190 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3191 | return; |
| 3192 | } |
| 3193 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3194 | // [...] 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] | 3195 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3197 | InitCategory.isLValue()) { |
| 3198 | Sequence.SetFailed( |
| 3199 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3200 | return; |
| 3201 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3202 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3203 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3204 | return; |
| 3205 | } |
| 3206 | |
| 3207 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3209 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3210 | const InitializedEntity &Entity, |
| 3211 | const InitializationKind &Kind, |
| 3212 | Expr *Initializer, |
| 3213 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3214 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3217 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3218 | /// enumerates the constructors of the initialized entity and performs overload |
| 3219 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3220 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3221 | const InitializedEntity &Entity, |
| 3222 | const InitializationKind &Kind, |
| 3223 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3224 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3225 | InitializationSequence &Sequence) { |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3226 | // Check constructor arguments for self reference. |
| 3227 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3228 | // Parameters arguments are occassionially constructed with itself, |
| 3229 | // for instance, in recursive functions. Skip them. |
| 3230 | if (!isa<ParmVarDecl>(DD)) |
| 3231 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3232 | S.CheckSelfReference(DD, Args[i]); |
| 3233 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3234 | // Build the candidate set directly in the initialization sequence |
| 3235 | // structure, so that it will persist if we fail. |
| 3236 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3237 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3239 | // Determine whether we are allowed to call explicit constructors or |
| 3240 | // explicit conversion operators. |
| 3241 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3242 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3243 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3244 | |
| 3245 | // The type we're constructing needs to be complete. |
| 3246 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3247 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3248 | return; |
| 3249 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3251 | // The type we're converting to is a class type. Enumerate its constructors |
| 3252 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3253 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3255 | CXXRecordDecl *DestRecordDecl |
| 3256 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3258 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3259 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3260 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3261 | NamedDecl *D = *Con; |
| 3262 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3263 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3265 | // Find the constructor (which may be a template). |
| 3266 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3267 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3268 | if (ConstructorTmpl) |
| 3269 | Constructor = cast<CXXConstructorDecl>( |
| 3270 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3271 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3272 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3273 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3274 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3275 | // suppress user-defined conversions on the arguments. |
| 3276 | // FIXME: Move constructors? |
| 3277 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3278 | Constructor->isCopyConstructor()) |
| 3279 | SuppressUserConversions = true; |
| 3280 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3282 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3283 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3284 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3285 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3286 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3287 | Args, NumArgs, CandidateSet, |
| 3288 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3289 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3290 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3291 | Args, NumArgs, CandidateSet, |
| 3292 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3293 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3296 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3299 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3301 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3302 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3304 | Result); |
| 3305 | return; |
| 3306 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3307 | |
| 3308 | // C++0x [dcl.init]p6: |
| 3309 | // If a program calls for the default initialization of an object |
| 3310 | // of a const-qualified type T, T shall be a class type with a |
| 3311 | // user-provided default constructor. |
| 3312 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3313 | Entity.getType().isConstQualified() && |
| 3314 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3315 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3316 | return; |
| 3317 | } |
| 3318 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3319 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3320 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3321 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3323 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3324 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3327 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3328 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3329 | const InitializedEntity &Entity, |
| 3330 | const InitializationKind &Kind, |
| 3331 | InitializationSequence &Sequence) { |
| 3332 | // C++ [dcl.init]p5: |
| 3333 | // |
| 3334 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3335 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3337 | // -- if T is an array type, then each element is value-initialized; |
| 3338 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3339 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3341 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3342 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3343 | // -- if T is a class type (clause 9) with a user-declared |
| 3344 | // constructor (12.1), then the default constructor for T is |
| 3345 | // called (and the initialization is ill-formed if T has no |
| 3346 | // accessible default constructor); |
| 3347 | // |
| 3348 | // FIXME: we really want to refer to a single subobject of the array, |
| 3349 | // but Entity doesn't have a way to capture that (yet). |
| 3350 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3351 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3353 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3354 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3355 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3356 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3357 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3358 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3359 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3361 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3365 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3368 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3369 | static void TryDefaultInitialization(Sema &S, |
| 3370 | const InitializedEntity &Entity, |
| 3371 | const InitializationKind &Kind, |
| 3372 | InitializationSequence &Sequence) { |
| 3373 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3375 | // C++ [dcl.init]p6: |
| 3376 | // To default-initialize an object of type T means: |
| 3377 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3378 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3379 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3380 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3381 | // constructor for T is called (and the initialization is ill-formed if |
| 3382 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3383 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3384 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3385 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3386 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3388 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3390 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3391 | // 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] | 3392 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3393 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3394 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3395 | return; |
| 3396 | } |
| 3397 | |
| 3398 | // If the destination type has a lifetime property, zero-initialize it. |
| 3399 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3400 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3401 | return; |
| 3402 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3405 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3406 | /// which enumerates all conversion functions and performs overload resolution |
| 3407 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | const InitializedEntity &Entity, |
| 3410 | const InitializationKind &Kind, |
| 3411 | Expr *Initializer, |
| 3412 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3413 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3414 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3415 | QualType SourceType = Initializer->getType(); |
| 3416 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3417 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3419 | // Build the candidate set directly in the initialization sequence |
| 3420 | // structure, so that it will persist if we fail. |
| 3421 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3422 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3424 | // Determine whether we are allowed to call explicit constructors or |
| 3425 | // explicit conversion operators. |
| 3426 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3428 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3429 | // The type we're converting to is a class type. Enumerate its constructors |
| 3430 | // to see if there is a suitable conversion. |
| 3431 | CXXRecordDecl *DestRecordDecl |
| 3432 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3434 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3436 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3437 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3438 | Con != ConEnd; ++Con) { |
| 3439 | NamedDecl *D = *Con; |
| 3440 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3442 | // Find the constructor (which may be a template). |
| 3443 | CXXConstructorDecl *Constructor = 0; |
| 3444 | FunctionTemplateDecl *ConstructorTmpl |
| 3445 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3446 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3447 | Constructor = cast<CXXConstructorDecl>( |
| 3448 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3449 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3450 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3452 | if (!Constructor->isInvalidDecl() && |
| 3453 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3454 | if (ConstructorTmpl) |
| 3455 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3456 | /*ExplicitArgs*/ 0, |
| 3457 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3458 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3459 | else |
| 3460 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3461 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3462 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3463 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3465 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3466 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3467 | |
| 3468 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3469 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3470 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3471 | // The type we're converting from is a class type, enumerate its conversion |
| 3472 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3473 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3474 | // We can only enumerate the conversion functions for a complete type; if |
| 3475 | // the type isn't complete, simply skip this step. |
| 3476 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3477 | CXXRecordDecl *SourceRecordDecl |
| 3478 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3479 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3480 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3481 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3482 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3483 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3484 | I != E; ++I) { |
| 3485 | NamedDecl *D = *I; |
| 3486 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3487 | if (isa<UsingShadowDecl>(D)) |
| 3488 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3490 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3491 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3492 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3493 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3494 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3495 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3497 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3498 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3499 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3500 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3501 | CandidateSet); |
| 3502 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3503 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3504 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3505 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3509 | |
| 3510 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3511 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3512 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3513 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3514 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3516 | Result); |
| 3517 | return; |
| 3518 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3519 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3520 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3521 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3523 | if (isa<CXXConstructorDecl>(Function)) { |
| 3524 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3525 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3526 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3527 | return; |
| 3528 | } |
| 3529 | |
| 3530 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3531 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3532 | if (ConvType->getAs<RecordType>()) { |
| 3533 | // If we're converting to a class type, there may be an copy if |
| 3534 | // the resulting temporary object (possible to create an object of |
| 3535 | // a base class type). That copy is not a separate conversion, so |
| 3536 | // we just make a note of the actual destination type (possibly a |
| 3537 | // base class of the type returned by the conversion function) and |
| 3538 | // let the user-defined conversion step handle the conversion. |
| 3539 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3540 | return; |
| 3541 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3542 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3543 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3545 | // If the conversion following the call to the conversion function |
| 3546 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3547 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3548 | Best->FinalConversion.Third) { |
| 3549 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3550 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3551 | ICS.Standard = Best->FinalConversion; |
| 3552 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3553 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3556 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3557 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3558 | |
| 3559 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3560 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3561 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3562 | // Skip parens. |
| 3563 | e = e->IgnoreParens(); |
| 3564 | |
| 3565 | // Skip address-of nodes. |
| 3566 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3567 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3568 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3569 | |
| 3570 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3571 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3572 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3573 | case CK_Dependent: |
| 3574 | case CK_BitCast: |
| 3575 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3576 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3577 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3578 | |
| 3579 | case CK_ArrayToPointerDecay: |
| 3580 | return IIK_nonscalar; |
| 3581 | |
| 3582 | case CK_NullToPointer: |
| 3583 | return IIK_okay; |
| 3584 | |
| 3585 | default: |
| 3586 | break; |
| 3587 | } |
| 3588 | |
| 3589 | // If we have a declaration reference, it had better be a local variable. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3590 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3591 | if (!isAddressOf) return IIK_nonlocal; |
| 3592 | |
| 3593 | VarDecl *var; |
| 3594 | if (isa<DeclRefExpr>(e)) { |
| 3595 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3596 | if (!var) return IIK_nonlocal; |
| 3597 | } else { |
| 3598 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3599 | } |
| 3600 | |
| 3601 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3602 | |
| 3603 | // If we have a conditional operator, check both sides. |
| 3604 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3605 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3606 | return iik; |
| 3607 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3608 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3609 | |
| 3610 | // These are never scalar. |
| 3611 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3612 | return IIK_nonscalar; |
| 3613 | |
| 3614 | // Otherwise, it needs to be a null pointer constant. |
| 3615 | } else { |
| 3616 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3617 | ? IIK_okay : IIK_nonlocal); |
| 3618 | } |
| 3619 | |
| 3620 | return IIK_nonlocal; |
| 3621 | } |
| 3622 | |
| 3623 | /// Check whether the given expression is a valid operand for an |
| 3624 | /// indirect copy/restore. |
| 3625 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3626 | assert(src->isRValue()); |
| 3627 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3628 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3629 | if (iik == IIK_okay) return; |
| 3630 | |
| 3631 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3632 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3633 | << src->getSourceRange(); |
| 3634 | } |
| 3635 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3636 | /// \brief Determine whether we have compatible array types for the |
| 3637 | /// purposes of GNU by-copy array initialization. |
| 3638 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3639 | const ArrayType *Dest, |
| 3640 | const ArrayType *Source) { |
| 3641 | // If the source and destination array types are equivalent, we're |
| 3642 | // done. |
| 3643 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3644 | return true; |
| 3645 | |
| 3646 | // Make sure that the element types are the same. |
| 3647 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3648 | return false; |
| 3649 | |
| 3650 | // The only mismatch we allow is when the destination is an |
| 3651 | // incomplete array type and the source is a constant array type. |
| 3652 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3653 | } |
| 3654 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3655 | static bool tryObjCWritebackConversion(Sema &S, |
| 3656 | InitializationSequence &Sequence, |
| 3657 | const InitializedEntity &Entity, |
| 3658 | Expr *Initializer) { |
| 3659 | bool ArrayDecay = false; |
| 3660 | QualType ArgType = Initializer->getType(); |
| 3661 | QualType ArgPointee; |
| 3662 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3663 | ArrayDecay = true; |
| 3664 | ArgPointee = ArgArrayType->getElementType(); |
| 3665 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3666 | } |
| 3667 | |
| 3668 | // Handle write-back conversion. |
| 3669 | QualType ConvertedArgType; |
| 3670 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3671 | ConvertedArgType)) |
| 3672 | return false; |
| 3673 | |
| 3674 | // We should copy unless we're passing to an argument explicitly |
| 3675 | // marked 'out'. |
| 3676 | bool ShouldCopy = true; |
| 3677 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3678 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3679 | |
| 3680 | // Do we need an lvalue conversion? |
| 3681 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3682 | ImplicitConversionSequence ICS; |
| 3683 | ICS.setStandard(); |
| 3684 | ICS.Standard.setAsIdentityConversion(); |
| 3685 | |
| 3686 | QualType ResultType; |
| 3687 | if (ArrayDecay) { |
| 3688 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3689 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3690 | } else { |
| 3691 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3692 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3693 | } |
| 3694 | |
| 3695 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3696 | } |
| 3697 | |
| 3698 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3699 | return true; |
| 3700 | } |
| 3701 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3702 | InitializationSequence::InitializationSequence(Sema &S, |
| 3703 | const InitializedEntity &Entity, |
| 3704 | const InitializationKind &Kind, |
| 3705 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3706 | unsigned NumArgs) |
| 3707 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3708 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3710 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3711 | // The semantics of initializers are as follows. The destination type is |
| 3712 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3713 | // 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] | 3714 | // 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] | 3715 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3716 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3717 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3718 | if (DestType->isDependentType() || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3719 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3720 | SequenceKind = DependentSequence; |
| 3721 | return; |
| 3722 | } |
| 3723 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3724 | // Almost everything is a normal sequence. |
| 3725 | setSequenceKind(NormalSequence); |
| 3726 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3727 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3728 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3729 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3730 | if (Result.isInvalid()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3731 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3732 | return; |
| 3733 | } |
| 3734 | Args[I] = Result.take(); |
| 3735 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3737 | QualType SourceType; |
| 3738 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3739 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3740 | Initializer = Args[0]; |
| 3741 | if (!isa<InitListExpr>(Initializer)) |
| 3742 | SourceType = Initializer->getType(); |
| 3743 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | |
| 3745 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3746 | // list-initialized (8.5.4). |
| 3747 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3748 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3749 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3750 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3752 | // - If the destination type is a reference type, see 8.5.3. |
| 3753 | if (DestType->isReferenceType()) { |
| 3754 | // C++0x [dcl.init.ref]p1: |
| 3755 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3756 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3757 | // by an object that can be converted into a T. |
| 3758 | // (Therefore, multiple arguments are not permitted.) |
| 3759 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3760 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3761 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3762 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3763 | return; |
| 3764 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3766 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3767 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3768 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3769 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3770 | return; |
| 3771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3772 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3773 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3774 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3775 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3776 | return; |
| 3777 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3778 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3779 | // - If the destination type is an array of characters, an array of |
| 3780 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3781 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3783 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3784 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3785 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3786 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3787 | return; |
| 3788 | } |
| 3789 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3790 | // Note: as an GNU C extension, we allow initialization of an |
| 3791 | // array from a compound literal that creates an array of the same |
| 3792 | // type, so long as the initializer has no side effects. |
| 3793 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3794 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3795 | Initializer->getType()->isArrayType()) { |
| 3796 | const ArrayType *SourceAT |
| 3797 | = Context.getAsArrayType(Initializer->getType()); |
| 3798 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3799 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3800 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3801 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3802 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3803 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3804 | } |
| 3805 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3806 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3807 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3808 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3810 | return; |
| 3811 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3812 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3813 | // Determine whether we should consider writeback conversions for |
| 3814 | // Objective-C ARC. |
| 3815 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3816 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3817 | |
| 3818 | // We're at the end of the line for C: it's either a write-back conversion |
| 3819 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3820 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3821 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3822 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3823 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3824 | return; |
| 3825 | } |
| 3826 | |
| 3827 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3828 | AddCAssignmentStep(DestType); |
| 3829 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3830 | return; |
| 3831 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3832 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3833 | assert(S.getLangOptions().CPlusPlus); |
| 3834 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3835 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3836 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | // - If the initialization is direct-initialization, or if it is |
| 3838 | // copy-initialization where the cv-unqualified version of the |
| 3839 | // 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] | 3840 | // class of the destination, constructors are considered. [...] |
| 3841 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3842 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3843 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3844 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3845 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3846 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3847 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3848 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3850 | // used) to a derived class thereof are enumerated as described in |
| 3851 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3852 | // (13.3). |
| 3853 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3854 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3855 | return; |
| 3856 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3858 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3859 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3860 | return; |
| 3861 | } |
| 3862 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | |
| 3864 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3865 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3866 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3867 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3868 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3869 | return; |
| 3870 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3872 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3873 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3874 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3876 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3877 | |
| 3878 | ImplicitConversionSequence ICS |
| 3879 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3880 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3881 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3882 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3883 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3884 | allowObjCWritebackConversion); |
| 3885 | |
| 3886 | if (ICS.isStandard() && |
| 3887 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3888 | // Objective-C ARC writeback conversion. |
| 3889 | |
| 3890 | // We should copy unless we're passing to an argument explicitly |
| 3891 | // marked 'out'. |
| 3892 | bool ShouldCopy = true; |
| 3893 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3894 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3895 | |
| 3896 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3897 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3898 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3899 | ImplicitConversionSequence LvalueICS; |
| 3900 | LvalueICS.setStandard(); |
| 3901 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3902 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3903 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3904 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3905 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3906 | |
| 3907 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3908 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3909 | DeclAccessPair dap; |
| 3910 | if (Initializer->getType() == Context.OverloadTy && |
| 3911 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3912 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3913 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3914 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3915 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3916 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3917 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3918 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3919 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3920 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3924 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3925 | StepEnd = Steps.end(); |
| 3926 | Step != StepEnd; ++Step) |
| 3927 | Step->Destroy(); |
| 3928 | } |
| 3929 | |
| 3930 | //===----------------------------------------------------------------------===// |
| 3931 | // Perform initialization |
| 3932 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3933 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3934 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3935 | switch(Entity.getKind()) { |
| 3936 | case InitializedEntity::EK_Variable: |
| 3937 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3938 | case InitializedEntity::EK_Exception: |
| 3939 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3940 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3941 | return Sema::AA_Initializing; |
| 3942 | |
| 3943 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3945 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3946 | return Sema::AA_Sending; |
| 3947 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3948 | return Sema::AA_Passing; |
| 3949 | |
| 3950 | case InitializedEntity::EK_Result: |
| 3951 | return Sema::AA_Returning; |
| 3952 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3953 | case InitializedEntity::EK_Temporary: |
| 3954 | // FIXME: Can we tell apart casting vs. converting? |
| 3955 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3957 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3958 | case InitializedEntity::EK_ArrayElement: |
| 3959 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3960 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3961 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3962 | return Sema::AA_Initializing; |
| 3963 | } |
| 3964 | |
| 3965 | return Sema::AA_Converting; |
| 3966 | } |
| 3967 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3968 | /// \brief Whether we should binding a created object as a temporary when |
| 3969 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3970 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3971 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3972 | case InitializedEntity::EK_ArrayElement: |
| 3973 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3974 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3975 | case InitializedEntity::EK_New: |
| 3976 | case InitializedEntity::EK_Variable: |
| 3977 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3978 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3979 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3980 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3981 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3982 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3983 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3985 | case InitializedEntity::EK_Parameter: |
| 3986 | case InitializedEntity::EK_Temporary: |
| 3987 | return true; |
| 3988 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3989 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3990 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3991 | } |
| 3992 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3993 | /// \brief Whether the given entity, when initialized with an object |
| 3994 | /// created for that initialization, requires destruction. |
| 3995 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3996 | switch (Entity.getKind()) { |
| 3997 | case InitializedEntity::EK_Member: |
| 3998 | case InitializedEntity::EK_Result: |
| 3999 | case InitializedEntity::EK_New: |
| 4000 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4001 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4002 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4003 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4004 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4005 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4007 | case InitializedEntity::EK_Variable: |
| 4008 | case InitializedEntity::EK_Parameter: |
| 4009 | case InitializedEntity::EK_Temporary: |
| 4010 | case InitializedEntity::EK_ArrayElement: |
| 4011 | case InitializedEntity::EK_Exception: |
| 4012 | return true; |
| 4013 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4014 | |
| 4015 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4018 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4019 | /// provided by the given initializer by calling the appropriate copy |
| 4020 | /// constructor. |
| 4021 | /// |
| 4022 | /// \param S The Sema object used for type-checking. |
| 4023 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4024 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4025 | /// the type of the initializer expression or a superclass thereof. |
| 4026 | /// |
| 4027 | /// \param Enter The entity being initialized. |
| 4028 | /// |
| 4029 | /// \param CurInit The initializer expression. |
| 4030 | /// |
| 4031 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4032 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4033 | /// an rvalue. |
| 4034 | /// |
| 4035 | /// \returns An expression that copies the initializer expression into |
| 4036 | /// a temporary object, or an error expression if a copy could not be |
| 4037 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4038 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4039 | QualType T, |
| 4040 | const InitializedEntity &Entity, |
| 4041 | ExprResult CurInit, |
| 4042 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4043 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4044 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4045 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4046 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4047 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4048 | if (!Class) |
| 4049 | return move(CurInit); |
| 4050 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4051 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4052 | // When certain criteria are met, an implementation is allowed to |
| 4053 | // omit the copy/move construction of a class object, even if the |
| 4054 | // copy/move constructor and/or destructor for the object have |
| 4055 | // side effects. [...] |
| 4056 | // - when a temporary class object that has not been bound to a |
| 4057 | // reference (12.2) would be copied/moved to a class object |
| 4058 | // with the same cv-unqualified type, the copy/move operation |
| 4059 | // can be omitted by constructing the temporary object |
| 4060 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4061 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4062 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4063 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4065 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4066 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4067 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4068 | switch (Entity.getKind()) { |
| 4069 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4070 | Loc = Entity.getReturnLoc(); |
| 4071 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4073 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4074 | Loc = Entity.getThrowLoc(); |
| 4075 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4077 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4078 | Loc = Entity.getDecl()->getLocation(); |
| 4079 | break; |
| 4080 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4081 | case InitializedEntity::EK_ArrayElement: |
| 4082 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4083 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4084 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4085 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4086 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4087 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4088 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4089 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4090 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4091 | Loc = CurInitExpr->getLocStart(); |
| 4092 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4093 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4094 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4096 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4097 | return move(CurInit); |
| 4098 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4099 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4100 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4101 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 4102 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4103 | Con != ConEnd; ++Con) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4104 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4105 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 4106 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4107 | CXXConstructorDecl *Constructor = 0; |
| 4108 | |
| 4109 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4110 | // Handle copy/moveconstructors, only. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4111 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4112 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4113 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4114 | continue; |
| 4115 | |
| 4116 | DeclAccessPair FoundDecl |
| 4117 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4118 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4119 | &CurInitExpr, 1, CandidateSet); |
| 4120 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4121 | } |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4122 | |
| 4123 | // Handle constructor templates. |
| 4124 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4125 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4126 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4127 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4128 | Constructor = cast<CXXConstructorDecl>( |
| 4129 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4130 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4131 | continue; |
| 4132 | |
| 4133 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4134 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4135 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4136 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4137 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4138 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4139 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4140 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4141 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4142 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4143 | case OR_Success: |
| 4144 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4146 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4147 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4148 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4149 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4150 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4151 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4152 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4153 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4154 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4155 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4156 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4157 | case OR_Ambiguous: |
| 4158 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4159 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4160 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4161 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4162 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4164 | case OR_Deleted: |
| 4165 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4166 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4167 | << CurInitExpr->getSourceRange(); |
| 4168 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4169 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4170 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4173 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4174 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4175 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4176 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4177 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4178 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4179 | |
| 4180 | if (IsExtraneousCopy) { |
| 4181 | // If this is a totally extraneous copy for C++03 reference |
| 4182 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4183 | // expression. We don't generate an (elided) copy operation here |
| 4184 | // because doing so would require us to pass down a flag to avoid |
| 4185 | // infinite recursion, where each step adds another extraneous, |
| 4186 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4187 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4188 | // Instantiate the default arguments of any extra parameters in |
| 4189 | // the selected copy constructor, as if we were going to create a |
| 4190 | // proper call to the copy constructor. |
| 4191 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4192 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4193 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4194 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4195 | break; |
| 4196 | |
| 4197 | // Build the default argument expression; we don't actually care |
| 4198 | // if this succeeds or not, because this routine will complain |
| 4199 | // if there was a problem. |
| 4200 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4201 | } |
| 4202 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4203 | return S.Owned(CurInitExpr); |
| 4204 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4205 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4206 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4207 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4208 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4209 | // constructor call (we might have derived-to-base conversions, or |
| 4210 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4211 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4212 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4213 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4215 | // Actually perform the constructor call. |
| 4216 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4217 | move_arg(ConstructorArgs), |
| 4218 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4219 | CXXConstructExpr::CK_Complete, |
| 4220 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4221 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4222 | // If we're supposed to bind temporaries, do so. |
| 4223 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4224 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4225 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4226 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4228 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4229 | const InitializedEntity &Entity) { |
| 4230 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4231 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4232 | return; |
| 4233 | |
| 4234 | if (Entity.getDecl()->getDeclName()) |
| 4235 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4236 | << Entity.getDecl()->getDeclName(); |
| 4237 | else |
| 4238 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4239 | } |
| 4240 | } |
| 4241 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4242 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4243 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4244 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4245 | } |
| 4246 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4248 | InitializationSequence::Perform(Sema &S, |
| 4249 | const InitializedEntity &Entity, |
| 4250 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4251 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4252 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4253 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4254 | unsigned NumArgs = Args.size(); |
| 4255 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4256 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4257 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4259 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4260 | // If the declaration is a non-dependent, incomplete array type |
| 4261 | // that has an initializer, then its type will be completed once |
| 4262 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4263 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4264 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4265 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4266 | if (const IncompleteArrayType *ArrayT |
| 4267 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4268 | // FIXME: We don't currently have the ability to accurately |
| 4269 | // compute the length of an initializer list without |
| 4270 | // performing full type-checking of the initializer list |
| 4271 | // (since we have to determine where braces are implicitly |
| 4272 | // introduced and such). So, we fall back to making the array |
| 4273 | // type a dependently-sized array type with no specified |
| 4274 | // bound. |
| 4275 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4276 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4278 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4279 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4280 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4281 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4282 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4283 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4284 | Brackets = ArrayLoc->getBracketsRange(); |
| 4285 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | *ResultType |
| 4289 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4290 | /*NumElts=*/0, |
| 4291 | ArrayT->getSizeModifier(), |
| 4292 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4293 | Brackets); |
| 4294 | } |
| 4295 | |
| 4296 | } |
| 4297 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4298 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4299 | Kind.isExplicitCast()); |
| 4300 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4303 | // No steps means no initialization. |
| 4304 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4305 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4307 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4308 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4309 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4310 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4311 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4312 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4313 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4314 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4315 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4318 | // grab the only argument out the Args and place it into the "current" |
| 4319 | // initializer. |
| 4320 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4321 | case SK_ResolveAddressOfOverloadedFunction: |
| 4322 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4323 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4324 | case SK_CastDerivedToBaseLValue: |
| 4325 | case SK_BindReference: |
| 4326 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4327 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4328 | case SK_UserConversion: |
| 4329 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4330 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4331 | case SK_QualificationConversionRValue: |
| 4332 | case SK_ConversionSequence: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4333 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4334 | case SK_ListInitialization: |
| 4335 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4336 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4337 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4338 | case SK_ArrayInit: |
| 4339 | case SK_PassByIndirectCopyRestore: |
| 4340 | case SK_PassByIndirectRestore: |
| 4341 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4342 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4343 | CurInit = Args.get()[0]; |
| 4344 | if (!CurInit.get()) return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4345 | |
| 4346 | // Read from a property when initializing something with it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4347 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4348 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4349 | if (CurInit.isInvalid()) |
| 4350 | return ExprError(); |
| 4351 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4352 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4353 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4355 | case SK_ConstructorInitialization: |
| 4356 | case SK_ZeroInitialization: |
| 4357 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
| 4360 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4361 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4362 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4363 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4364 | Step != StepEnd; ++Step) { |
| 4365 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4366 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4368 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4369 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4370 | switch (Step->Kind) { |
| 4371 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4374 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4375 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4376 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4377 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4378 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4379 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4381 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4382 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4383 | case SK_CastDerivedToBaseLValue: { |
| 4384 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4385 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4386 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4387 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4388 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4389 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4390 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4391 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4392 | CurInit.get()->getLocStart(), |
| 4393 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4394 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4395 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4397 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4398 | QualType T = SourceType; |
| 4399 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4400 | T = Pointer->getPointeeType(); |
| 4401 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4402 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4403 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4404 | } |
| 4405 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4406 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4407 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4408 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4409 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4410 | VK_XValue : |
| 4411 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4412 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4413 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4414 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4415 | CurInit.get(), |
| 4416 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4417 | break; |
| 4418 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4420 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4421 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4422 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4423 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4424 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4425 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4426 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4427 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4428 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4429 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4430 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4431 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4432 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4433 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4434 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4435 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4436 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4437 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4438 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4440 | // Reference binding does not have any corresponding ASTs. |
| 4441 | |
| 4442 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4443 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4444 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4446 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4448 | case SK_BindReferenceToTemporary: |
| 4449 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4450 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4451 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4453 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4454 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4455 | Entity.getType().getNonReferenceType(), |
| 4456 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4457 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4458 | |
| 4459 | // If we're binding to an Objective-C object that has lifetime, we |
| 4460 | // need cleanups. |
| 4461 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4462 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4463 | S.ExprNeedsCleanups = true; |
| 4464 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4465 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4467 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4469 | /*IsExtraneousCopy=*/true); |
| 4470 | break; |
| 4471 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4472 | case SK_UserConversion: { |
| 4473 | // We have a user-defined conversion that invokes either a constructor |
| 4474 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4475 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4476 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4477 | FunctionDecl *Fn = Step->Function.Function; |
| 4478 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4479 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4480 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4481 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4482 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4483 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4484 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4485 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4486 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4487 | // Determine the arguments required to actually perform the constructor |
| 4488 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4489 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4490 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4491 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4492 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4493 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4495 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4496 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4497 | move_arg(ConstructorArgs), |
| 4498 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4499 | CXXConstructExpr::CK_Complete, |
| 4500 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4501 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4502 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4503 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4504 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4505 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4506 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4507 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4508 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4509 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4510 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4511 | S.IsDerivedFrom(SourceType, Class)) |
| 4512 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4514 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4515 | } else { |
| 4516 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4517 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4518 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4519 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4520 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4521 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | |
| 4523 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4524 | // derived-to-base conversion? I believe the answer is "no", because |
| 4525 | // 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] | 4526 | ExprResult CurInitExprRes = |
| 4527 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4528 | FoundFn, Conversion); |
| 4529 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4530 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4531 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4532 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4533 | // Build the actual call to the conversion function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4534 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4535 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4536 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4538 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4540 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4541 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4543 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4544 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4545 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4546 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4547 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4548 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4550 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4551 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4552 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4554 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4557 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4558 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4559 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4560 | CurInit.get()->getType(), |
| 4561 | CastKind, CurInit.get(), 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4562 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4564 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4565 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4566 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4572 | case SK_QualificationConversionXValue: |
| 4573 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4574 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4575 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4576 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4577 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4578 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4579 | VK_XValue : |
| 4580 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4581 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4582 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4585 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4586 | Sema::CheckedConversionKind CCK |
| 4587 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4588 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4589 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4590 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4591 | ExprResult CurInitExprRes = |
| 4592 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4593 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4594 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4595 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4596 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4597 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4600 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4601 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4602 | QualType Ty = Step->Type; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4603 | InitListChecker PerformInitList(S, Entity, InitList, |
| 4604 | ResultType ? *ResultType : Ty, /*VerifyOnly=*/false); |
| 4605 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4606 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4607 | |
| 4608 | CurInit.release(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4609 | CurInit = S.Owned(PerformInitList.getFullyStructuredList()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4610 | break; |
| 4611 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4612 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4613 | case SK_ListConstructorCall: |
| 4614 | assert(false && "List constructor calls not yet supported."); |
| 4615 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4616 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4617 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4618 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4619 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4620 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4621 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4622 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4623 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4624 | ? Kind.getEqualLoc() |
| 4625 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4626 | |
| 4627 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4628 | // Force even a trivial, implicit default constructor to be |
| 4629 | // semantically checked. We do this explicitly because we don't build |
| 4630 | // the definition for completely trivial constructors. |
| 4631 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4632 | assert(ClassDecl && "No parent class for constructor."); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4633 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4634 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4635 | !Constructor->isUsed(false)) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4636 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4637 | } |
| 4638 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4639 | // Determine the arguments required to actually perform the constructor |
| 4640 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4641 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4642 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4643 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4644 | |
| 4645 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4646 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4647 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4648 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4649 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4650 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4651 | unsigned NumExprs = ConstructorArgs.size(); |
| 4652 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4653 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4654 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4656 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4657 | if (!TSInfo) |
| 4658 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4660 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4661 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4662 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4664 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4665 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4666 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4667 | } else { |
| 4668 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4669 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4670 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4671 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4672 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4674 | CXXConstructExpr::CK_NonVirtualBase; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4675 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4676 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4677 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4679 | // Only get the parenthesis range if it is a direct construction. |
| 4680 | SourceRange parenRange = |
| 4681 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4682 | Kind.getParenRange() : SourceRange(); |
| 4683 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4684 | // If the entity allows NRVO, mark the construction as elidable |
| 4685 | // unconditionally. |
| 4686 | if (Entity.allowsNRVO()) |
| 4687 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4688 | Constructor, /*Elidable=*/true, |
| 4689 | move_arg(ConstructorArgs), |
| 4690 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4691 | ConstructKind, |
| 4692 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4693 | else |
| 4694 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4695 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4696 | move_arg(ConstructorArgs), |
| 4697 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4698 | ConstructKind, |
| 4699 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4700 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4701 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4702 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4705 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4706 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4707 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4709 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4710 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4712 | break; |
| 4713 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4715 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4716 | step_iterator NextStep = Step; |
| 4717 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4719 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4720 | // The need for zero-initialization is recorded directly into |
| 4721 | // the call to the object's constructor within the next step. |
| 4722 | ConstructorInitRequiresZeroInit = true; |
| 4723 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4724 | S.getLangOptions().CPlusPlus && |
| 4725 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4726 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4727 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4729 | Kind.getRange().getBegin()); |
| 4730 | |
| 4731 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4732 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4733 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4734 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4735 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4736 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4737 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4738 | break; |
| 4739 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4740 | |
| 4741 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4742 | QualType SourceType = CurInit.get()->getType(); |
| 4743 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4744 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4745 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4746 | if (Result.isInvalid()) |
| 4747 | return ExprError(); |
| 4748 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4749 | |
| 4750 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4751 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4752 | if (ConvTy != Sema::Compatible && |
| 4753 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4754 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4755 | == Sema::Compatible) |
| 4756 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4757 | if (CurInitExprRes.isInvalid()) |
| 4758 | return ExprError(); |
| 4759 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4760 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4761 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4762 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4763 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4764 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4765 | getAssignmentAction(Entity), |
| 4766 | &Complained)) { |
| 4767 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4768 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4769 | } else if (Complained) |
| 4770 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4771 | break; |
| 4772 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4773 | |
| 4774 | case SK_StringInit: { |
| 4775 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4776 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4777 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4778 | break; |
| 4779 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4780 | |
| 4781 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4782 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4783 | CK_ObjCObjectLValueCast, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4784 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4785 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4786 | |
| 4787 | case SK_ArrayInit: |
| 4788 | // Okay: we checked everything before creating this step. Note that |
| 4789 | // this is a GNU extension. |
| 4790 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4791 | << Step->Type << CurInit.get()->getType() |
| 4792 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4793 | |
| 4794 | // If the destination type is an incomplete array type, update the |
| 4795 | // type accordingly. |
| 4796 | if (ResultType) { |
| 4797 | if (const IncompleteArrayType *IncompleteDest |
| 4798 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4799 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4800 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4801 | *ResultType = S.Context.getConstantArrayType( |
| 4802 | IncompleteDest->getElementType(), |
| 4803 | ConstantSource->getSize(), |
| 4804 | ArrayType::Normal, 0); |
| 4805 | } |
| 4806 | } |
| 4807 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4808 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4809 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4810 | case SK_PassByIndirectCopyRestore: |
| 4811 | case SK_PassByIndirectRestore: |
| 4812 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4813 | CurInit = S.Owned(new (S.Context) |
| 4814 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4815 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4816 | break; |
| 4817 | |
| 4818 | case SK_ProduceObjCObject: |
| 4819 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4820 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4821 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4822 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4823 | } |
| 4824 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4825 | |
| 4826 | // Diagnose non-fatal problems with the completed initialization. |
| 4827 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4828 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4829 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4830 | cast<FieldDecl>(Entity.getDecl()), |
| 4831 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4832 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4833 | return move(CurInit); |
| 4834 | } |
| 4835 | |
| 4836 | //===----------------------------------------------------------------------===// |
| 4837 | // Diagnose initialization failures |
| 4838 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4840 | const InitializedEntity &Entity, |
| 4841 | const InitializationKind &Kind, |
| 4842 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4843 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4844 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4845 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4846 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4847 | switch (Failure) { |
| 4848 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4849 | // FIXME: Customize for the initialized entity? |
| 4850 | if (NumArgs == 0) |
| 4851 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4852 | << DestType.getNonReferenceType(); |
| 4853 | else // FIXME: diagnostic below could be better! |
| 4854 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4855 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4856 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4858 | case FK_ArrayNeedsInitList: |
| 4859 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4860 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4861 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4862 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4864 | case FK_ArrayTypeMismatch: |
| 4865 | case FK_NonConstantArrayInit: |
| 4866 | S.Diag(Kind.getLocation(), |
| 4867 | (Failure == FK_ArrayTypeMismatch |
| 4868 | ? diag::err_array_init_different_type |
| 4869 | : diag::err_array_init_non_constant_array)) |
| 4870 | << DestType.getNonReferenceType() |
| 4871 | << Args[0]->getType() |
| 4872 | << Args[0]->getSourceRange(); |
| 4873 | break; |
| 4874 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4875 | case FK_AddressOfOverloadFailed: { |
| 4876 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4878 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4879 | true, |
| 4880 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4881 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4883 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4884 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4885 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4886 | switch (FailedOverloadResult) { |
| 4887 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4888 | if (Failure == FK_UserConversionOverloadFailed) |
| 4889 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4890 | << Args[0]->getType() << DestType |
| 4891 | << Args[0]->getSourceRange(); |
| 4892 | else |
| 4893 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4894 | << DestType << Args[0]->getType() |
| 4895 | << Args[0]->getSourceRange(); |
| 4896 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4897 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4898 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4900 | case OR_No_Viable_Function: |
| 4901 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4902 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4903 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4904 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4905 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4907 | case OR_Deleted: { |
| 4908 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4909 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4910 | << Args[0]->getSourceRange(); |
| 4911 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4912 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4913 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4914 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4915 | if (Ovl == OR_Deleted) { |
| 4916 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4917 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4918 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4919 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4920 | } |
| 4921 | break; |
| 4922 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4923 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4924 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4925 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4926 | break; |
| 4927 | } |
| 4928 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4929 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4930 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4931 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4933 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4934 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4935 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4936 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4937 | << DestType.getNonReferenceType() |
| 4938 | << Args[0]->getType() |
| 4939 | << Args[0]->getSourceRange(); |
| 4940 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4942 | case FK_RValueReferenceBindingToLValue: |
| 4943 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4944 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4945 | << Args[0]->getSourceRange(); |
| 4946 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4948 | case FK_ReferenceInitDropsQualifiers: |
| 4949 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4950 | << DestType.getNonReferenceType() |
| 4951 | << Args[0]->getType() |
| 4952 | << Args[0]->getSourceRange(); |
| 4953 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4955 | case FK_ReferenceInitFailed: |
| 4956 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4957 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4958 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4959 | << Args[0]->getType() |
| 4960 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4961 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4962 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4963 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4964 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4966 | case FK_ConversionFailed: { |
| 4967 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4968 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4969 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4970 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4971 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4972 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4973 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4974 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4975 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4976 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4977 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4978 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4979 | |
| 4980 | case FK_ConversionFromPropertyFailed: |
| 4981 | // No-op. This error has already been reported. |
| 4982 | break; |
| 4983 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4984 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4985 | SourceRange R; |
| 4986 | |
| 4987 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4988 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4989 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4991 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4992 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4993 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4994 | if (Kind.isCStyleOrFunctionalCast()) |
| 4995 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4996 | << R; |
| 4997 | else |
| 4998 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4999 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5000 | break; |
| 5001 | } |
| 5002 | |
| 5003 | case FK_ReferenceBindingToInitList: |
| 5004 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5005 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5006 | break; |
| 5007 | |
| 5008 | case FK_InitListBadDestinationType: |
| 5009 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5010 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5011 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5013 | case FK_ConstructorOverloadFailed: { |
| 5014 | SourceRange ArgsRange; |
| 5015 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5016 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5017 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5018 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5019 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5020 | // bad. |
| 5021 | switch (FailedOverloadResult) { |
| 5022 | case OR_Ambiguous: |
| 5023 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5024 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5025 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5026 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5027 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5029 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5030 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5031 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5032 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5033 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5034 | // This is implicit default initialization of a member or |
| 5035 | // base within a constructor. If no viable function was |
| 5036 | // found, notify the user that she needs to explicitly |
| 5037 | // initialize this base/member. |
| 5038 | CXXConstructorDecl *Constructor |
| 5039 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5040 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5041 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5042 | << Constructor->isImplicit() |
| 5043 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5044 | << /*base=*/0 |
| 5045 | << Entity.getType(); |
| 5046 | |
| 5047 | RecordDecl *BaseDecl |
| 5048 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5049 | ->getDecl(); |
| 5050 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5051 | << S.Context.getTagDeclType(BaseDecl); |
| 5052 | } else { |
| 5053 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5054 | << Constructor->isImplicit() |
| 5055 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5056 | << /*member=*/1 |
| 5057 | << Entity.getName(); |
| 5058 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5059 | |
| 5060 | if (const RecordType *Record |
| 5061 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5062 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5063 | diag::note_previous_decl) |
| 5064 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5065 | } |
| 5066 | break; |
| 5067 | } |
| 5068 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5069 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5070 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5071 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5072 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5074 | case OR_Deleted: { |
| 5075 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5076 | << true << DestType << ArgsRange; |
| 5077 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5078 | OverloadingResult Ovl |
| 5079 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5080 | if (Ovl == OR_Deleted) { |
| 5081 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5082 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5083 | } else { |
| 5084 | llvm_unreachable("Inconsistent overload resolution?"); |
| 5085 | } |
| 5086 | break; |
| 5087 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5089 | case OR_Success: |
| 5090 | llvm_unreachable("Conversion did not fail!"); |
| 5091 | break; |
| 5092 | } |
| 5093 | break; |
| 5094 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5096 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5097 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5098 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5099 | // This is implicit default-initialization of a const member in |
| 5100 | // a constructor. Complain that it needs to be explicitly |
| 5101 | // initialized. |
| 5102 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5103 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5104 | << Constructor->isImplicit() |
| 5105 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5106 | << /*const=*/1 |
| 5107 | << Entity.getName(); |
| 5108 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5109 | << Entity.getName(); |
| 5110 | } else { |
| 5111 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5112 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5113 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5114 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5115 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5116 | case FK_Incomplete: |
| 5117 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 5118 | diag::err_init_incomplete_type); |
| 5119 | break; |
| 5120 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5121 | case FK_ListInitializationFailed: { |
| 5122 | // Run the init list checker again to emit diagnostics. |
| 5123 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5124 | QualType DestType = Entity.getType(); |
| 5125 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
| 5126 | DestType, /*VerifyOnly=*/false); |
| 5127 | assert(DiagnoseInitList.HadError() && |
| 5128 | "Inconsistent init list check result."); |
| 5129 | break; |
| 5130 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5131 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5133 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5134 | return true; |
| 5135 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5136 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5137 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5138 | switch (SequenceKind) { |
| 5139 | case FailedSequence: { |
| 5140 | OS << "Failed sequence: "; |
| 5141 | switch (Failure) { |
| 5142 | case FK_TooManyInitsForReference: |
| 5143 | OS << "too many initializers for reference"; |
| 5144 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5146 | case FK_ArrayNeedsInitList: |
| 5147 | OS << "array requires initializer list"; |
| 5148 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5150 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5151 | OS << "array requires initializer list or string literal"; |
| 5152 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5154 | case FK_ArrayTypeMismatch: |
| 5155 | OS << "array type mismatch"; |
| 5156 | break; |
| 5157 | |
| 5158 | case FK_NonConstantArrayInit: |
| 5159 | OS << "non-constant array initializer"; |
| 5160 | break; |
| 5161 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5162 | case FK_AddressOfOverloadFailed: |
| 5163 | OS << "address of overloaded function failed"; |
| 5164 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5165 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5166 | case FK_ReferenceInitOverloadFailed: |
| 5167 | OS << "overload resolution for reference initialization failed"; |
| 5168 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5170 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5171 | OS << "non-const lvalue reference bound to temporary"; |
| 5172 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5174 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5175 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5176 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5177 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5178 | case FK_RValueReferenceBindingToLValue: |
| 5179 | OS << "rvalue reference bound to an lvalue"; |
| 5180 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5182 | case FK_ReferenceInitDropsQualifiers: |
| 5183 | OS << "reference initialization drops qualifiers"; |
| 5184 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5185 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5186 | case FK_ReferenceInitFailed: |
| 5187 | OS << "reference initialization failed"; |
| 5188 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5190 | case FK_ConversionFailed: |
| 5191 | OS << "conversion failed"; |
| 5192 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5193 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5194 | case FK_ConversionFromPropertyFailed: |
| 5195 | OS << "conversion from property failed"; |
| 5196 | break; |
| 5197 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5198 | case FK_TooManyInitsForScalar: |
| 5199 | OS << "too many initializers for scalar"; |
| 5200 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5202 | case FK_ReferenceBindingToInitList: |
| 5203 | OS << "referencing binding to initializer list"; |
| 5204 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5206 | case FK_InitListBadDestinationType: |
| 5207 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5208 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5209 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5210 | case FK_UserConversionOverloadFailed: |
| 5211 | OS << "overloading failed for user-defined conversion"; |
| 5212 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5213 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5214 | case FK_ConstructorOverloadFailed: |
| 5215 | OS << "constructor overloading failed"; |
| 5216 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5217 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5218 | case FK_DefaultInitOfConst: |
| 5219 | OS << "default initialization of a const variable"; |
| 5220 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5221 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5222 | case FK_Incomplete: |
| 5223 | OS << "initialization of incomplete type"; |
| 5224 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5225 | |
| 5226 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5227 | OS << "list initialization checker failure"; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5228 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5229 | OS << '\n'; |
| 5230 | return; |
| 5231 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5233 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5234 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5235 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5236 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5237 | case NormalSequence: |
| 5238 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5239 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5240 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5242 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5243 | if (S != step_begin()) { |
| 5244 | OS << " -> "; |
| 5245 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5247 | switch (S->Kind) { |
| 5248 | case SK_ResolveAddressOfOverloadedFunction: |
| 5249 | OS << "resolve address of overloaded function"; |
| 5250 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5251 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5252 | case SK_CastDerivedToBaseRValue: |
| 5253 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5254 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5255 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5256 | case SK_CastDerivedToBaseXValue: |
| 5257 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5258 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5259 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5260 | case SK_CastDerivedToBaseLValue: |
| 5261 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5262 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5264 | case SK_BindReference: |
| 5265 | OS << "bind reference to lvalue"; |
| 5266 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5268 | case SK_BindReferenceToTemporary: |
| 5269 | OS << "bind reference to a temporary"; |
| 5270 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5272 | case SK_ExtraneousCopyToTemporary: |
| 5273 | OS << "extraneous C++03 copy to temporary"; |
| 5274 | break; |
| 5275 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5276 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5277 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5278 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5280 | case SK_QualificationConversionRValue: |
| 5281 | OS << "qualification conversion (rvalue)"; |
| 5282 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5283 | case SK_QualificationConversionXValue: |
| 5284 | OS << "qualification conversion (xvalue)"; |
| 5285 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5286 | case SK_QualificationConversionLValue: |
| 5287 | OS << "qualification conversion (lvalue)"; |
| 5288 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5290 | case SK_ConversionSequence: |
| 5291 | OS << "implicit conversion sequence ("; |
| 5292 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5293 | OS << ")"; |
| 5294 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5295 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5296 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5297 | OS << "list aggregate initialization"; |
| 5298 | break; |
| 5299 | |
| 5300 | case SK_ListConstructorCall: |
| 5301 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5302 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5304 | case SK_ConstructorInitialization: |
| 5305 | OS << "constructor initialization"; |
| 5306 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5308 | case SK_ZeroInitialization: |
| 5309 | OS << "zero initialization"; |
| 5310 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5312 | case SK_CAssignment: |
| 5313 | OS << "C assignment"; |
| 5314 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5316 | case SK_StringInit: |
| 5317 | OS << "string initialization"; |
| 5318 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5319 | |
| 5320 | case SK_ObjCObjectConversion: |
| 5321 | OS << "Objective-C object conversion"; |
| 5322 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5323 | |
| 5324 | case SK_ArrayInit: |
| 5325 | OS << "array initialization"; |
| 5326 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5327 | |
| 5328 | case SK_PassByIndirectCopyRestore: |
| 5329 | OS << "pass by indirect copy and restore"; |
| 5330 | break; |
| 5331 | |
| 5332 | case SK_PassByIndirectRestore: |
| 5333 | OS << "pass by indirect restore"; |
| 5334 | break; |
| 5335 | |
| 5336 | case SK_ProduceObjCObject: |
| 5337 | OS << "Objective-C object retension"; |
| 5338 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | |
| 5343 | void InitializationSequence::dump() const { |
| 5344 | dump(llvm::errs()); |
| 5345 | } |
| 5346 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5347 | static void DiagnoseNarrowingInInitList( |
| 5348 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5349 | bool Constant, const APValue &ConstantValue) { |
| 5350 | if (Constant) { |
| 5351 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5352 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5353 | ? diag::err_init_list_constant_narrowing |
| 5354 | : diag::warn_init_list_constant_narrowing) |
| 5355 | << InitE->getSourceRange() |
| 5356 | << ConstantValue |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5357 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5358 | } else |
| 5359 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5360 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5361 | ? diag::err_init_list_variable_narrowing |
| 5362 | : diag::warn_init_list_variable_narrowing) |
| 5363 | << InitE->getSourceRange() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5364 | << InitE->getType().getLocalUnqualifiedType() |
| 5365 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5366 | |
| 5367 | llvm::SmallString<128> StaticCast; |
| 5368 | llvm::raw_svector_ostream OS(StaticCast); |
| 5369 | OS << "static_cast<"; |
| 5370 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5371 | // It's important to use the typedef's name if there is one so that the |
| 5372 | // fixit doesn't break code using types like int64_t. |
| 5373 | // |
| 5374 | // FIXME: This will break if the typedef requires qualification. But |
| 5375 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5376 | OS << TT->getDecl(); |
| 5377 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5378 | OS << BT->getName(S.getLangOptions()); |
| 5379 | else { |
| 5380 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5381 | // with a broken cast. |
| 5382 | return; |
| 5383 | } |
| 5384 | OS << ">("; |
| 5385 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5386 | << InitE->getSourceRange() |
| 5387 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5388 | << FixItHint::CreateInsertion( |
| 5389 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5390 | } |
| 5391 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5392 | //===----------------------------------------------------------------------===// |
| 5393 | // Initialization helper functions |
| 5394 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5395 | bool |
| 5396 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5397 | ExprResult Init) { |
| 5398 | if (Init.isInvalid()) |
| 5399 | return false; |
| 5400 | |
| 5401 | Expr *InitE = Init.get(); |
| 5402 | assert(InitE && "No initialization expression"); |
| 5403 | |
| 5404 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5405 | SourceLocation()); |
| 5406 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5407 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5411 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5412 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5413 | ExprResult Init, |
| 5414 | bool TopLevelOfInitList) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5415 | if (Init.isInvalid()) |
| 5416 | return ExprError(); |
| 5417 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5418 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5419 | assert(InitE && "No initialization expression?"); |
| 5420 | |
| 5421 | if (EqualLoc.isInvalid()) |
| 5422 | EqualLoc = InitE->getLocStart(); |
| 5423 | |
| 5424 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5425 | EqualLoc); |
| 5426 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5427 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5428 | |
| 5429 | bool Constant = false; |
| 5430 | APValue Result; |
| 5431 | if (TopLevelOfInitList && |
| 5432 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5433 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5434 | Constant, Result); |
| 5435 | } |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5436 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5437 | } |