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 | // |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 13 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Designator.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | |
| 53 | switch (SL->getKind()) { |
| 54 | case StringLiteral::Ascii: |
| 55 | case StringLiteral::UTF8: |
| 56 | // char array can be initialized with a narrow string. |
| 57 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 59 | case StringLiteral::UTF16: |
| 60 | return ElemTy->isChar16Type() ? Init : 0; |
| 61 | case StringLiteral::UTF32: |
| 62 | return ElemTy->isChar32Type() ? Init : 0; |
| 63 | case StringLiteral::Wide: |
| 64 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 65 | // correction from DR343): "An array with element type compatible with a |
| 66 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 67 | // string literal, optionally enclosed in braces." |
| 68 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 69 | ElemTy.getUnqualifiedType())) |
| 70 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 78 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 79 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 80 | if (!arrayType) return 0; |
| 81 | |
| 82 | return IsStringInit(init, arrayType, Context); |
| 83 | } |
| 84 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 85 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 86 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | // Get the length of the string as parsed. |
| 88 | uint64_t StrLength = |
| 89 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 90 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | // being initialized to a string literal. |
| 95 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 96 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 97 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 98 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 99 | ConstVal, |
| 100 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 101 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 104 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 107 | // the size may be smaller or larger than the string we are initializing. |
| 108 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 109 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 111 | // For Pascal strings it's OK to strip off the terminating null character, |
| 112 | // so the example below is valid: |
| 113 | // |
| 114 | // unsigned char a[2] = "\pa"; |
| 115 | if (SL->isPascal()) |
| 116 | StrLength--; |
| 117 | } |
| 118 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 119 | // [dcl.init.string]p2 |
| 120 | if (StrLength > CAT->getSize().getZExtValue()) |
| 121 | S.Diag(Str->getSourceRange().getBegin(), |
| 122 | diag::err_initializer_string_for_char_array_too_long) |
| 123 | << Str->getSourceRange(); |
| 124 | } else { |
| 125 | // C99 6.7.8p14. |
| 126 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 127 | S.Diag(Str->getSourceRange().getBegin(), |
| 128 | diag::warn_initializer_string_for_char_array_too_long) |
| 129 | << Str->getSourceRange(); |
| 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 132 | // Set the type to the actual size that we are initializing. If we have |
| 133 | // something like: |
| 134 | // char x[1] = "foo"; |
| 135 | // then this will set the string literal's type to char[1]. |
| 136 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Semantic checking for initializer lists. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 143 | /// @brief Semantic checking for initializer lists. |
| 144 | /// |
| 145 | /// The InitListChecker class contains a set of routines that each |
| 146 | /// handle the initialization of a certain kind of entity, e.g., |
| 147 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 148 | /// InitListChecker itself performs a recursive walk of the subobject |
| 149 | /// structure of the type to be initialized, while stepping through |
| 150 | /// the initializer list one element at a time. The IList and Index |
| 151 | /// parameters to each of the Check* routines contain the active |
| 152 | /// (syntactic) initializer list and the index into that initializer |
| 153 | /// list that represents the current initializer. Each routine is |
| 154 | /// responsible for moving that Index forward as it consumes elements. |
| 155 | /// |
| 156 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 157 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 158 | /// initializer list and the index into that initializer list where we |
| 159 | /// are copying initializers as we map them over to the semantic |
| 160 | /// list. Once we have completed our recursive walk of the subobject |
| 161 | /// structure, we will have constructed a full semantic initializer |
| 162 | /// list. |
| 163 | /// |
| 164 | /// C99 designators cause changes in the initializer list traversal, |
| 165 | /// because they make the initialization "jump" into a specific |
| 166 | /// subobject and then continue the initialization from that |
| 167 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 168 | /// designated subobject and manages backing out the recursion to |
| 169 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 170 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 172 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | bool hadError; |
| 174 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 175 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 177 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 178 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 179 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 180 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 181 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 182 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 184 | unsigned &StructuredIndex, |
| 185 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 191 | unsigned &StructuredIndex, |
| 192 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 194 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 195 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 198 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 199 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 200 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 201 | InitListExpr *StructuredList, |
| 202 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 203 | void CheckReferenceType(const InitializedEntity &Entity, |
| 204 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 205 | unsigned &Index, |
| 206 | InitListExpr *StructuredList, |
| 207 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 208 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 209 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 210 | InitListExpr *StructuredList, |
| 211 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 212 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 213 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 215 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 216 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 217 | unsigned &StructuredIndex, |
| 218 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 219 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 220 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 223 | InitListExpr *StructuredList, |
| 224 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 225 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 226 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 227 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 229 | RecordDecl::field_iterator *NextField, |
| 230 | llvm::APSInt *NextElementIndex, |
| 231 | unsigned &Index, |
| 232 | InitListExpr *StructuredList, |
| 233 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 234 | bool FinishSubobjectInit, |
| 235 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 237 | QualType CurrentObjectType, |
| 238 | InitListExpr *StructuredList, |
| 239 | unsigned StructuredIndex, |
| 240 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 241 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 242 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | Expr *expr); |
| 244 | int numArrayElements(QualType DeclType); |
| 245 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 246 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 247 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 248 | const InitializedEntity &ParentEntity, |
| 249 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 250 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 251 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 252 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 253 | Expr *InitExpr, FieldDecl *Field, |
| 254 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 255 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 256 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 257 | InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 258 | bool HadError() { return hadError; } |
| 259 | |
| 260 | // @brief Retrieves the fully-structured initializer list used for |
| 261 | // semantic analysis and code generation. |
| 262 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 263 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 264 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 266 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 267 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 268 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 269 | bool &RequiresSecondPass) { |
| 270 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 271 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 273 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 274 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 275 | // FIXME: We probably don't need to handle references |
| 276 | // specially here, since value-initialization of references is |
| 277 | // handled in InitializationSequence. |
| 278 | if (Field->getType()->isReferenceType()) { |
| 279 | // C++ [dcl.init.aggr]p9: |
| 280 | // If an incomplete or empty initializer-list leaves a |
| 281 | // member of reference type uninitialized, the program is |
| 282 | // ill-formed. |
| 283 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 284 | << Field->getType() |
| 285 | << ILE->getSyntacticForm()->getSourceRange(); |
| 286 | SemaRef.Diag(Field->getLocation(), |
| 287 | diag::note_uninit_reference_member); |
| 288 | hadError = true; |
| 289 | return; |
| 290 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 292 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 293 | true); |
| 294 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 295 | if (!InitSeq) { |
| 296 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 297 | hadError = true; |
| 298 | return; |
| 299 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 300 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 301 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 302 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 303 | if (MemberInit.isInvalid()) { |
| 304 | hadError = true; |
| 305 | return; |
| 306 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 308 | if (hadError) { |
| 309 | // Do nothing |
| 310 | } else if (Init < NumInits) { |
| 311 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 312 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 313 | // Value-initialization requires a constructor call, so |
| 314 | // extend the initializer list to include the constructor |
| 315 | // call and make a note that we'll need to take another pass |
| 316 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 317 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 318 | RequiresSecondPass = true; |
| 319 | } |
| 320 | } else if (InitListExpr *InnerILE |
| 321 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 322 | FillInValueInitializations(MemberEntity, InnerILE, |
| 323 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 326 | /// Recursively replaces NULL values within the given initializer list |
| 327 | /// with expressions that perform value-initialization of the |
| 328 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 329 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 330 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 331 | InitListExpr *ILE, |
| 332 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 334 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 335 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 336 | if (ILE->getSyntacticForm()) |
| 337 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 339 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 340 | if (RType->getDecl()->isUnion() && |
| 341 | ILE->getInitializedFieldInUnion()) |
| 342 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 343 | Entity, ILE, RequiresSecondPass); |
| 344 | else { |
| 345 | unsigned Init = 0; |
| 346 | for (RecordDecl::field_iterator |
| 347 | Field = RType->getDecl()->field_begin(), |
| 348 | FieldEnd = RType->getDecl()->field_end(); |
| 349 | Field != FieldEnd; ++Field) { |
| 350 | if (Field->isUnnamedBitfield()) |
| 351 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 353 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 354 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 355 | |
| 356 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 357 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 358 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 360 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 362 | // Only look at the first initialization of a union. |
| 363 | if (RType->getDecl()->isUnion()) |
| 364 | break; |
| 365 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 370 | |
| 371 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 373 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 374 | unsigned NumInits = ILE->getNumInits(); |
| 375 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 376 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 377 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 378 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 379 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 380 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 381 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 382 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 383 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 384 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 386 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 388 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 391 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 392 | if (hadError) |
| 393 | return; |
| 394 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 395 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 396 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 397 | ElementEntity.setElementIndex(Init); |
| 398 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 399 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 400 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 401 | true); |
| 402 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 403 | if (!InitSeq) { |
| 404 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 405 | hadError = true; |
| 406 | return; |
| 407 | } |
| 408 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 409 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 410 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 411 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 412 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | if (hadError) { |
| 417 | // Do nothing |
| 418 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 419 | // For arrays, just set the expression used for value-initialization |
| 420 | // of the "holes" in the array. |
| 421 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 422 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 423 | else |
| 424 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 425 | } else { |
| 426 | // For arrays, just set the expression used for value-initialization |
| 427 | // of the rest of elements and exit. |
| 428 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 429 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 430 | return; |
| 431 | } |
| 432 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 433 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 434 | // Value-initialization requires a constructor call, so |
| 435 | // extend the initializer list to include the constructor |
| 436 | // call and make a note that we'll need to take another pass |
| 437 | // through the initializer list. |
| 438 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 439 | RequiresSecondPass = true; |
| 440 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 441 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 442 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 443 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 444 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 449 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 450 | InitListExpr *IL, QualType &T) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 451 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 452 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 453 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 454 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 455 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 457 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 459 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 460 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 462 | if (!hadError) { |
| 463 | bool RequiresSecondPass = false; |
| 464 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 465 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 467 | RequiresSecondPass); |
| 468 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 472 | // FIXME: use a proper constant |
| 473 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 474 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 475 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 476 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 477 | } |
| 478 | return maxElements; |
| 479 | } |
| 480 | |
| 481 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 482 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 483 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 485 | Field = structDecl->field_begin(), |
| 486 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 487 | Field != FieldEnd; ++Field) { |
| 488 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 489 | ++InitializableMembers; |
| 490 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 491 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 492 | return std::min(InitializableMembers, 1); |
| 493 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 496 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 497 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 498 | QualType T, unsigned &Index, |
| 499 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 500 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 501 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 503 | if (T->isArrayType()) |
| 504 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 505 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 506 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 507 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 508 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 509 | else |
| 510 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 511 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 512 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 513 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 514 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 515 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 516 | hadError = true; |
| 517 | return; |
| 518 | } |
| 519 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 520 | // Build a structured initializer list corresponding to this subobject. |
| 521 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 523 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 524 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 525 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 526 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 528 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 529 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 530 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 531 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 533 | StructuredSubobjectInitIndex); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 534 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 535 | StructuredSubobjectInitList->setType(T); |
| 536 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 537 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 538 | // range corresponds with the end of the last initializer it used. |
| 539 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 541 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 542 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 543 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 544 | |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 545 | // Warn about missing braces. |
| 546 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 47f164e | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 547 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 548 | diag::warn_missing_braces) |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 549 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 550 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 551 | "{") |
| 552 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 553 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 554 | "}"); |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 555 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 558 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 559 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 560 | unsigned &Index, |
| 561 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 562 | unsigned &StructuredIndex, |
| 563 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 564 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 565 | SyntacticToSemantic[IList] = StructuredList; |
| 566 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 567 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 568 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 569 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 570 | IList->setType(ExprTy); |
| 571 | StructuredList->setType(ExprTy); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 572 | if (hadError) |
| 573 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 574 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 575 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 576 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 577 | if (StructuredIndex == 1 && |
| 578 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 579 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 580 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 581 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 582 | hadError = true; |
| 583 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 584 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 585 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 586 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 587 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 588 | // Don't complain for incomplete types, since we'll get an error |
| 589 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 590 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 592 | CurrentObjectType->isArrayType()? 0 : |
| 593 | CurrentObjectType->isVectorType()? 1 : |
| 594 | CurrentObjectType->isScalarType()? 2 : |
| 595 | CurrentObjectType->isUnionType()? 3 : |
| 596 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 597 | |
| 598 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 599 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 600 | DK = diag::err_excess_initializers; |
| 601 | hadError = true; |
| 602 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 603 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 604 | DK = diag::err_excess_initializers; |
| 605 | hadError = true; |
| 606 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 608 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 609 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 612 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 613 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 614 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 615 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 616 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 617 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 620 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 621 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 623 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 624 | unsigned &Index, |
| 625 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 626 | unsigned &StructuredIndex, |
| 627 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 628 | if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 629 | CheckScalarType(Entity, IList, DeclType, Index, |
| 630 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 631 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 633 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 634 | } else if (DeclType->isAggregateType()) { |
| 635 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 636 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 637 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 638 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 639 | StructuredList, StructuredIndex, |
| 640 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 641 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 642 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 643 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 644 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 645 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 646 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 647 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 648 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 649 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 650 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 651 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 652 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 653 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 654 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 655 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 656 | } else if (DeclType->isRecordType()) { |
| 657 | // C++ [dcl.init]p14: |
| 658 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 659 | // is a brace-enclosed list, see 8.5.1. |
| 660 | // |
| 661 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 662 | // we have an initializer list and a destination type that is not |
| 663 | // an aggregate. |
| 664 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 665 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 666 | << DeclType << IList->getSourceRange(); |
| 667 | hadError = true; |
| 668 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 669 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 670 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 671 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 672 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 673 | << DeclType; |
| 674 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 675 | } else { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 676 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 677 | << DeclType; |
| 678 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 682 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 683 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 685 | unsigned &Index, |
| 686 | InitListExpr *StructuredList, |
| 687 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 688 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 689 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 690 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 691 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 693 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 694 | StructuredList, StructuredIndex, |
| 695 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 696 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 697 | newStructuredList, newStructuredIndex); |
| 698 | ++StructuredIndex; |
| 699 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 700 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 701 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 702 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 703 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 704 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 705 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 706 | StructuredList, StructuredIndex); |
| 707 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 708 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 709 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 710 | // arrayType can be incomplete if we're initializing a flexible |
| 711 | // array member. There's nothing we can do with the completed |
| 712 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 713 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 714 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 715 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 716 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 717 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 718 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 719 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 720 | |
| 721 | // Fall through for subaggregate initialization. |
| 722 | |
| 723 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 724 | // C++ [dcl.init.aggr]p12: |
| 725 | // All implicit type conversions (clause 4) are considered when |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 726 | // initializing the aggregate member with an ini- tializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 727 | // an initializer-list. If the initializer can initialize a |
| 728 | // member, the member is initialized. [...] |
| 729 | |
| 730 | // FIXME: Better EqualLoc? |
| 731 | InitializationKind Kind = |
| 732 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 733 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 734 | |
| 735 | if (Seq) { |
| 736 | ExprResult Result = |
| 737 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 738 | if (Result.isInvalid()) |
| 739 | hadError = true; |
| 740 | |
| 741 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 742 | Result.takeAs<Expr>()); |
| 743 | ++Index; |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | // Fall through for subaggregate initialization |
| 748 | } else { |
| 749 | // C99 6.7.8p13: |
| 750 | // |
| 751 | // The initializer for a structure or union object that has |
| 752 | // automatic storage duration shall be either an initializer |
| 753 | // list as described below, or a single expression that has |
| 754 | // compatible structure or union type. In the latter case, the |
| 755 | // initial value of the object, including unnamed members, is |
| 756 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 757 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 758 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 759 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 760 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 761 | if (ExprRes.isInvalid()) |
| 762 | hadError = true; |
| 763 | else { |
| 764 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 765 | if (ExprRes.isInvalid()) |
| 766 | hadError = true; |
| 767 | } |
| 768 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 769 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 770 | ++Index; |
| 771 | return; |
| 772 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 773 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 774 | // Fall through for subaggregate initialization |
| 775 | } |
| 776 | |
| 777 | // C++ [dcl.init.aggr]p12: |
| 778 | // |
| 779 | // [...] Otherwise, if the member is itself a non-empty |
| 780 | // subaggregate, brace elision is assumed and the initializer is |
| 781 | // considered for the initialization of the first member of |
| 782 | // the subaggregate. |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 783 | if (!SemaRef.getLangOptions().OpenCL && |
| 784 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 785 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 786 | StructuredIndex); |
| 787 | ++StructuredIndex; |
| 788 | } else { |
| 789 | // We cannot initialize this element, so let |
| 790 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 791 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 792 | SemaRef.Owned(expr), |
| 793 | /*TopLevelOfInitList=*/true); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 794 | hadError = true; |
| 795 | ++Index; |
| 796 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 797 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 800 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 801 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 802 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 803 | InitListExpr *StructuredList, |
| 804 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 805 | if (Index >= IList->getNumInits()) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 806 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 807 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 808 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 809 | ++Index; |
| 810 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 811 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 812 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 813 | |
| 814 | Expr *expr = IList->getInit(Index); |
| 815 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 816 | SemaRef.Diag(SubIList->getLocStart(), |
| 817 | diag::warn_many_braces_around_scalar_init) |
| 818 | << SubIList->getSourceRange(); |
| 819 | |
| 820 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 821 | StructuredIndex); |
| 822 | return; |
| 823 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 824 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 825 | diag::err_designator_for_scalar_init) |
| 826 | << DeclType << expr->getSourceRange(); |
| 827 | hadError = true; |
| 828 | ++Index; |
| 829 | ++StructuredIndex; |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | ExprResult Result = |
| 834 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 835 | SemaRef.Owned(expr), |
| 836 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 837 | |
| 838 | Expr *ResultExpr = 0; |
| 839 | |
| 840 | if (Result.isInvalid()) |
| 841 | hadError = true; // types weren't compatible. |
| 842 | else { |
| 843 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 845 | if (ResultExpr != expr) { |
| 846 | // The type was promoted, update initializer list. |
| 847 | IList->setInit(Index, ResultExpr); |
| 848 | } |
| 849 | } |
| 850 | if (hadError) |
| 851 | ++StructuredIndex; |
| 852 | else |
| 853 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 854 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 857 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 858 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 859 | unsigned &Index, |
| 860 | InitListExpr *StructuredList, |
| 861 | unsigned &StructuredIndex) { |
| 862 | if (Index < IList->getNumInits()) { |
| 863 | Expr *expr = IList->getInit(Index); |
| 864 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 865 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 866 | << DeclType << IList->getSourceRange(); |
| 867 | hadError = true; |
| 868 | ++Index; |
| 869 | ++StructuredIndex; |
| 870 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 872 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 873 | ExprResult Result = |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 874 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 875 | SemaRef.Owned(expr), |
| 876 | /*TopLevelOfInitList=*/true); |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 877 | |
| 878 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 879 | hadError = true; |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 880 | |
| 881 | expr = Result.takeAs<Expr>(); |
| 882 | IList->setInit(Index, expr); |
| 883 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 884 | if (hadError) |
| 885 | ++StructuredIndex; |
| 886 | else |
| 887 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 888 | ++Index; |
| 889 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 890 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 891 | // general, it would be useful to pass location information down the stack, |
| 892 | // so that we know the location (or decl) of the "current object" being |
| 893 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 895 | diag::err_init_reference_member_uninitialized) |
| 896 | << DeclType |
| 897 | << IList->getSourceRange(); |
| 898 | hadError = true; |
| 899 | ++Index; |
| 900 | ++StructuredIndex; |
| 901 | return; |
| 902 | } |
| 903 | } |
| 904 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 905 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 906 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 907 | unsigned &Index, |
| 908 | InitListExpr *StructuredList, |
| 909 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 910 | if (Index >= IList->getNumInits()) |
| 911 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 913 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 914 | unsigned maxElements = VT->getNumElements(); |
| 915 | unsigned numEltsInit = 0; |
| 916 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 917 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 918 | if (!SemaRef.getLangOptions().OpenCL) { |
| 919 | // If the initializing element is a vector, try to copy-initialize |
| 920 | // instead of breaking it apart (which is doomed to failure anyway). |
| 921 | Expr *Init = IList->getInit(Index); |
| 922 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 923 | ExprResult Result = |
| 924 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 925 | SemaRef.Owned(Init), |
| 926 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 927 | |
| 928 | Expr *ResultExpr = 0; |
| 929 | if (Result.isInvalid()) |
| 930 | hadError = true; // types weren't compatible. |
| 931 | else { |
| 932 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 933 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 934 | if (ResultExpr != Init) { |
| 935 | // The type was promoted, update initializer list. |
| 936 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 939 | if (hadError) |
| 940 | ++StructuredIndex; |
| 941 | else |
| 942 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 943 | ++Index; |
| 944 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 945 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 947 | InitializedEntity ElementEntity = |
| 948 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 949 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 950 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 951 | // Don't attempt to go past the end of the init list |
| 952 | if (Index >= IList->getNumInits()) |
| 953 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 955 | ElementEntity.setElementIndex(Index); |
| 956 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 957 | StructuredList, StructuredIndex); |
| 958 | } |
| 959 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 960 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 961 | |
| 962 | InitializedEntity ElementEntity = |
| 963 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 964 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 965 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 966 | for (unsigned i = 0; i < maxElements; ++i) { |
| 967 | // Don't attempt to go past the end of the init list |
| 968 | if (Index >= IList->getNumInits()) |
| 969 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 970 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 971 | ElementEntity.setElementIndex(Index); |
| 972 | |
| 973 | QualType IType = IList->getInit(Index)->getType(); |
| 974 | if (!IType->isVectorType()) { |
| 975 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 976 | StructuredList, StructuredIndex); |
| 977 | ++numEltsInit; |
| 978 | } else { |
| 979 | QualType VecType; |
| 980 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 981 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 982 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 983 | if (IType->isExtVectorType()) |
| 984 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 985 | else |
| 986 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 987 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 988 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 989 | StructuredList, StructuredIndex); |
| 990 | numEltsInit += numIElts; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | // OpenCL requires all elements to be initialized. |
| 995 | if (numEltsInit != maxElements) |
| 996 | if (SemaRef.getLangOptions().OpenCL) |
| 997 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 998 | diag::err_vector_incorrect_num_initializers) |
| 999 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1002 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1003 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1004 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1006 | unsigned &Index, |
| 1007 | InitListExpr *StructuredList, |
| 1008 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1009 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1010 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1011 | // Check for the special-case of initializing an array with a string. |
| 1012 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1013 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1014 | SemaRef.Context)) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1015 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1016 | // We place the string literal directly into the resulting |
| 1017 | // initializer list. This is the only place where the structure |
| 1018 | // of the structured initializer list doesn't match exactly, |
| 1019 | // because doing so would involve allocating one character |
| 1020 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1021 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1022 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1023 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1027 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1028 | // Check for VLAs; in standard C it would be possible to check this |
| 1029 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1030 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1031 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1032 | diag::err_variable_object_no_init) |
| 1033 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1034 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1035 | ++Index; |
| 1036 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1037 | return; |
| 1038 | } |
| 1039 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1040 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1041 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1042 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1043 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1044 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1045 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1046 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1047 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1048 | maxElementsKnown = true; |
| 1049 | } |
| 1050 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1051 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1052 | while (Index < IList->getNumInits()) { |
| 1053 | Expr *Init = IList->getInit(Index); |
| 1054 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1055 | // If we're not the subobject that matches up with the '{' for |
| 1056 | // the designator, we shouldn't be handling the |
| 1057 | // designator. Return immediately. |
| 1058 | if (!SubobjectIsDesignatorContext) |
| 1059 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1061 | // Handle this designated initializer. elementIndex will be |
| 1062 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1063 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1064 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1065 | StructuredList, StructuredIndex, true, |
| 1066 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1067 | hadError = true; |
| 1068 | continue; |
| 1069 | } |
| 1070 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1071 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1072 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1073 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1074 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1075 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1077 | // If the array is of incomplete type, keep track of the number of |
| 1078 | // elements in the initializer. |
| 1079 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1080 | maxElements = elementIndex; |
| 1081 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1082 | continue; |
| 1083 | } |
| 1084 | |
| 1085 | // If we know the maximum number of elements, and we've already |
| 1086 | // hit it, stop consuming elements in the initializer list. |
| 1087 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1088 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1089 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1090 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1091 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1092 | Entity); |
| 1093 | // Check this element. |
| 1094 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1095 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1096 | ++elementIndex; |
| 1097 | |
| 1098 | // If the array is of incomplete type, keep track of the number of |
| 1099 | // elements in the initializer. |
| 1100 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1101 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1102 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1103 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1104 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1105 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1106 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1107 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1108 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1109 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1110 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1111 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1112 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1115 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1119 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1120 | Expr *InitExpr, |
| 1121 | FieldDecl *Field, |
| 1122 | bool TopLevelObject) { |
| 1123 | // Handle GNU flexible array initializers. |
| 1124 | unsigned FlexArrayDiag; |
| 1125 | if (isa<InitListExpr>(InitExpr) && |
| 1126 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1127 | // Empty flexible array init always allowed as an extension |
| 1128 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1129 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1130 | // Disallow flexible array init in C++; it is not required for gcc |
| 1131 | // compatibility, and it needs work to IRGen correctly in general. |
| 1132 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1133 | } else if (!TopLevelObject) { |
| 1134 | // Disallow flexible array init on non-top-level object |
| 1135 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1136 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1137 | // Disallow flexible array init on anything which is not a variable. |
| 1138 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1139 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1140 | // Disallow flexible array init on local variables. |
| 1141 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1142 | } else { |
| 1143 | // Allow other cases. |
| 1144 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1145 | } |
| 1146 | |
| 1147 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1148 | FlexArrayDiag) |
| 1149 | << InitExpr->getSourceRange().getBegin(); |
| 1150 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1151 | << Field; |
| 1152 | |
| 1153 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1154 | } |
| 1155 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1156 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1157 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1159 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1161 | unsigned &Index, |
| 1162 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1163 | unsigned &StructuredIndex, |
| 1164 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1165 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1167 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1168 | // confusion, we forgo checking the intializer for the entire record. |
| 1169 | if (structDecl->isInvalidDecl()) { |
| 1170 | hadError = true; |
| 1171 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1175 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1176 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1177 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1178 | Field != FieldEnd; ++Field) { |
| 1179 | if (Field->getDeclName()) { |
| 1180 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | return; |
| 1185 | } |
| 1186 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | // If structDecl is a forward declaration, this loop won't do |
| 1188 | // anything except look at designated initializers; That's okay, |
| 1189 | // because an error should get printed out elsewhere. It might be |
| 1190 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1191 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1192 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1193 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1194 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1195 | while (Index < IList->getNumInits()) { |
| 1196 | Expr *Init = IList->getInit(Index); |
| 1197 | |
| 1198 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1199 | // If we're not the subobject that matches up with the '{' for |
| 1200 | // the designator, we shouldn't be handling the |
| 1201 | // designator. Return immediately. |
| 1202 | if (!SubobjectIsDesignatorContext) |
| 1203 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1205 | // Handle this designated initializer. Field will be updated to |
| 1206 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1207 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1208 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1209 | StructuredList, StructuredIndex, |
| 1210 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1211 | hadError = true; |
| 1212 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1213 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1214 | |
| 1215 | // Disable check for missing fields when designators are used. |
| 1216 | // This matches gcc behaviour. |
| 1217 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1218 | continue; |
| 1219 | } |
| 1220 | |
| 1221 | if (Field == FieldEnd) { |
| 1222 | // We've run out of fields. We're done. |
| 1223 | break; |
| 1224 | } |
| 1225 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1226 | // We've already initialized a member of a union. We're done. |
| 1227 | if (InitializedSomething && DeclType->isUnionType()) |
| 1228 | break; |
| 1229 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1230 | // If we've hit the flexible array member at the end, we're done. |
| 1231 | if (Field->getType()->isIncompleteArrayType()) |
| 1232 | break; |
| 1233 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1234 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1235 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1236 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1237 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1238 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1240 | // Make sure we can use this declaration. |
| 1241 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1242 | IList->getInit(Index)->getLocStart())) { |
| 1243 | ++Index; |
| 1244 | ++Field; |
| 1245 | hadError = true; |
| 1246 | continue; |
| 1247 | } |
| 1248 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1249 | InitializedEntity MemberEntity = |
| 1250 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1251 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1252 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1253 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (DeclType->isUnionType()) { |
| 1256 | // Initialize the first field within the union. |
| 1257 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1258 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1259 | |
| 1260 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1261 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1262 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1263 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1264 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1265 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1266 | // It is possible we have one or more unnamed bitfields remaining. |
| 1267 | // Find first (if any) named field and emit warning. |
| 1268 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1269 | it != end; ++it) { |
| 1270 | if (!it->isUnnamedBitfield()) { |
| 1271 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1272 | diag::warn_missing_field_initializers) << it->getName(); |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1279 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1280 | return; |
| 1281 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1282 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1283 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1284 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1285 | ++Index; |
| 1286 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1289 | InitializedEntity MemberEntity = |
| 1290 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1291 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1292 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1293 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1294 | StructuredList, StructuredIndex); |
| 1295 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1296 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1297 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1298 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1300 | /// \brief Expand a field designator that refers to a member of an |
| 1301 | /// anonymous struct or union into a series of field designators that |
| 1302 | /// refers to the field within the appropriate subobject. |
| 1303 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1304 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | DesignatedInitExpr *DIE, |
| 1306 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1307 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1308 | typedef DesignatedInitExpr::Designator Designator; |
| 1309 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1310 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1311 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1312 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1313 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1314 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1316 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1317 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1318 | else |
| 1319 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1320 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1321 | assert(isa<FieldDecl>(*PI)); |
| 1322 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | // Expand the current designator into the set of replacement |
| 1326 | // designators, so we have a full subobject path down to where the |
| 1327 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1328 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1329 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1332 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1333 | /// corresponds to FieldName. |
| 1334 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1335 | IdentifierInfo *FieldName) { |
| 1336 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1337 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1338 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1339 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1340 | return IF; |
| 1341 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1342 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1343 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1346 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1347 | /// |
| 1348 | /// Determines whether the designated initializer @p DIE, which |
| 1349 | /// resides at the given @p Index within the initializer list @p |
| 1350 | /// IList, is well-formed for a current object of type @p DeclType |
| 1351 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1353 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1354 | /// |
| 1355 | /// @param IList The initializer list in which this designated |
| 1356 | /// initializer occurs. |
| 1357 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1358 | /// @param DIE The designated initializer expression. |
| 1359 | /// |
| 1360 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1361 | /// |
| 1362 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1363 | /// into which the designation in @p DIE should refer. |
| 1364 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1365 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1366 | /// a field, this will be set to the field declaration corresponding |
| 1367 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1368 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1369 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1370 | /// DIE is an array designator or GNU array-range designator, this |
| 1371 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1372 | /// |
| 1373 | /// @param Index Index into @p IList where the designated initializer |
| 1374 | /// @p DIE occurs. |
| 1375 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1376 | /// @param StructuredList The initializer list expression that |
| 1377 | /// describes all of the subobject initializers in the order they'll |
| 1378 | /// actually be initialized. |
| 1379 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1380 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1382 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1383 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1385 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1386 | QualType &CurrentObjectType, |
| 1387 | RecordDecl::field_iterator *NextField, |
| 1388 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1389 | unsigned &Index, |
| 1390 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1391 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1392 | bool FinishSubobjectInit, |
| 1393 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1394 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1395 | // Check the actual initialization for the designated object type. |
| 1396 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Temporarily remove the designator expression from the |
| 1399 | // initializer list that the child calls see, so that we don't try |
| 1400 | // to re-process the designator. |
| 1401 | unsigned OldIndex = Index; |
| 1402 | IList->setInit(OldIndex, DIE->getInit()); |
| 1403 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1404 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1405 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Restore the designated initializer expression in the syntactic |
| 1408 | // form of the initializer list. |
| 1409 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1410 | DIE->setInit(IList->getInit(OldIndex)); |
| 1411 | IList->setInit(OldIndex, DIE); |
| 1412 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1416 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1418 | "Need a non-designated initializer list to start from"); |
| 1419 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1420 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1421 | // Determine the structural initializer list that corresponds to the |
| 1422 | // current subobject. |
| 1423 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1425 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1426 | SourceRange(D->getStartLocation(), |
| 1427 | DIE->getSourceRange().getEnd())); |
| 1428 | assert(StructuredList && "Expected a structured initializer list"); |
| 1429 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1430 | if (D->isFieldDesignator()) { |
| 1431 | // C99 6.7.8p7: |
| 1432 | // |
| 1433 | // If a designator has the form |
| 1434 | // |
| 1435 | // . identifier |
| 1436 | // |
| 1437 | // then the current object (defined below) shall have |
| 1438 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1440 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1441 | if (!RT) { |
| 1442 | SourceLocation Loc = D->getDotLoc(); |
| 1443 | if (Loc.isInvalid()) |
| 1444 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1445 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1446 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1447 | ++Index; |
| 1448 | return true; |
| 1449 | } |
| 1450 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1451 | // Note: we perform a linear search of the fields here, despite |
| 1452 | // the fact that we have a faster lookup method, because we always |
| 1453 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1454 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1455 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1456 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1458 | Field = RT->getDecl()->field_begin(), |
| 1459 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1460 | for (; Field != FieldEnd; ++Field) { |
| 1461 | if (Field->isUnnamedBitfield()) |
| 1462 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1463 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1464 | // If we find a field representing an anonymous field, look in the |
| 1465 | // IndirectFieldDecl that follow for the designated initializer. |
| 1466 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1467 | if (IndirectFieldDecl *IF = |
| 1468 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1469 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1470 | D = DIE->getDesignator(DesigIdx); |
| 1471 | break; |
| 1472 | } |
| 1473 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1474 | if (KnownField && KnownField == *Field) |
| 1475 | break; |
| 1476 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | |
| 1479 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1482 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1483 | // There was no normal field in the struct with the designated |
| 1484 | // name. Perform another lookup for this name, which may find |
| 1485 | // something that we can't designate (e.g., a member function), |
| 1486 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1488 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1489 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1490 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1491 | // Name lookup didn't find anything. Determine whether this |
| 1492 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1493 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1494 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1495 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1496 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1497 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1498 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1499 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1500 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1501 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1502 | std::string CorrectedStr( |
| 1503 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1504 | std::string CorrectedQuotedStr( |
| 1505 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1506 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1507 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1508 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1509 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1510 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1511 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1512 | } else { |
| 1513 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1514 | << FieldName << CurrentObjectType; |
| 1515 | ++Index; |
| 1516 | return true; |
| 1517 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1518 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1520 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1521 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1522 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1523 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1525 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1526 | ++Index; |
| 1527 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1528 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1529 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1530 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1531 | // The replacement field comes from typo correction; find it |
| 1532 | // in the list of fields. |
| 1533 | FieldIndex = 0; |
| 1534 | Field = RT->getDecl()->field_begin(); |
| 1535 | for (; Field != FieldEnd; ++Field) { |
| 1536 | if (Field->isUnnamedBitfield()) |
| 1537 | continue; |
| 1538 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1539 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1540 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1541 | break; |
| 1542 | |
| 1543 | ++FieldIndex; |
| 1544 | } |
| 1545 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1546 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1547 | |
| 1548 | // All of the fields of a union are located at the same place in |
| 1549 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1550 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1551 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1552 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1553 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1555 | // Make sure we can use this declaration. |
| 1556 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1557 | ++Index; |
| 1558 | return true; |
| 1559 | } |
| 1560 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1561 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1562 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1564 | // Make sure that our non-designated initializer list has space |
| 1565 | // for a subobject corresponding to this field. |
| 1566 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1567 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1568 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1569 | // This designator names a flexible array member. |
| 1570 | if (Field->getType()->isIncompleteArrayType()) { |
| 1571 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1572 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1573 | // We can't designate an object within the flexible array |
| 1574 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1576 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1578 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1580 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1581 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1582 | << *Field; |
| 1583 | Invalid = true; |
| 1584 | } |
| 1585 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1586 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1587 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1588 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1589 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1590 | diag::err_flexible_array_init_needs_braces) |
| 1591 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1592 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1593 | << *Field; |
| 1594 | Invalid = true; |
| 1595 | } |
| 1596 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1597 | // Check GNU flexible array initializer. |
| 1598 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1599 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1600 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1601 | |
| 1602 | if (Invalid) { |
| 1603 | ++Index; |
| 1604 | return true; |
| 1605 | } |
| 1606 | |
| 1607 | // Initialize the array. |
| 1608 | bool prevHadError = hadError; |
| 1609 | unsigned newStructuredIndex = FieldIndex; |
| 1610 | unsigned OldIndex = Index; |
| 1611 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1612 | |
| 1613 | InitializedEntity MemberEntity = |
| 1614 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1615 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1616 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1618 | IList->setInit(OldIndex, DIE); |
| 1619 | if (hadError && !prevHadError) { |
| 1620 | ++Field; |
| 1621 | ++FieldIndex; |
| 1622 | if (NextField) |
| 1623 | *NextField = Field; |
| 1624 | StructuredIndex = FieldIndex; |
| 1625 | return true; |
| 1626 | } |
| 1627 | } else { |
| 1628 | // Recurse to check later designated subobjects. |
| 1629 | QualType FieldType = (*Field)->getType(); |
| 1630 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1631 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1632 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1633 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1634 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1635 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1636 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1637 | true, false)) |
| 1638 | return true; |
| 1639 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1640 | |
| 1641 | // Find the position of the next field to be initialized in this |
| 1642 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1643 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1644 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1645 | |
| 1646 | // If this the first designator, our caller will continue checking |
| 1647 | // the rest of this struct/class/union subobject. |
| 1648 | if (IsFirstDesignator) { |
| 1649 | if (NextField) |
| 1650 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1651 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1655 | if (!FinishSubobjectInit) |
| 1656 | return false; |
| 1657 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1658 | // We've already initialized something in the union; we're done. |
| 1659 | if (RT->getDecl()->isUnion()) |
| 1660 | return hadError; |
| 1661 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1662 | // Check the remaining fields within this class/struct/union subobject. |
| 1663 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1664 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1665 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1666 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1667 | return hadError && !prevHadError; |
| 1668 | } |
| 1669 | |
| 1670 | // C99 6.7.8p6: |
| 1671 | // |
| 1672 | // If a designator has the form |
| 1673 | // |
| 1674 | // [ constant-expression ] |
| 1675 | // |
| 1676 | // then the current object (defined below) shall have array |
| 1677 | // type and the expression shall be an integer constant |
| 1678 | // expression. If the array is of unknown size, any |
| 1679 | // nonnegative value is valid. |
| 1680 | // |
| 1681 | // Additionally, cope with the GNU extension that permits |
| 1682 | // designators of the form |
| 1683 | // |
| 1684 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1685 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1686 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1687 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1688 | << CurrentObjectType; |
| 1689 | ++Index; |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
| 1693 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1694 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1695 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1696 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1697 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1698 | DesignatedEndIndex = DesignatedStartIndex; |
| 1699 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1700 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1701 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1703 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1705 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1706 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1708 | // Codegen can't handle evaluating array range designators that have side |
| 1709 | // effects, because we replicate the AST value for each initialized element. |
| 1710 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1711 | // elements with something that has a side effect, so codegen can emit an |
| 1712 | // "error unsupported" error instead of miscompiling the app. |
| 1713 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1714 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1715 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1718 | if (isa<ConstantArrayType>(AT)) { |
| 1719 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1720 | DesignatedStartIndex |
| 1721 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1722 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1723 | DesignatedEndIndex |
| 1724 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1725 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1726 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1727 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1728 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1729 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1730 | << IndexExpr->getSourceRange(); |
| 1731 | ++Index; |
| 1732 | return true; |
| 1733 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1734 | } else { |
| 1735 | // Make sure the bit-widths and signedness match. |
| 1736 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1737 | DesignatedEndIndex |
| 1738 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1739 | else if (DesignatedStartIndex.getBitWidth() < |
| 1740 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1741 | DesignatedStartIndex |
| 1742 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1743 | DesignatedStartIndex.setIsUnsigned(true); |
| 1744 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1745 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1747 | // Make sure that our non-designated initializer list has space |
| 1748 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1749 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1751 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1753 | // Repeatedly perform subobject initializations in the range |
| 1754 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1755 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1756 | // Move to the next designator |
| 1757 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1758 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1759 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1760 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1761 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1763 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1764 | // Recurse to check later designated subobjects. |
| 1765 | QualType ElementType = AT->getElementType(); |
| 1766 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1768 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1769 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1770 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1771 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1772 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1773 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1774 | return true; |
| 1775 | |
| 1776 | // Move to the next index in the array that we'll be initializing. |
| 1777 | ++DesignatedStartIndex; |
| 1778 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1779 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1780 | |
| 1781 | // If this the first designator, our caller will continue checking |
| 1782 | // the rest of this array subobject. |
| 1783 | if (IsFirstDesignator) { |
| 1784 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1785 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1786 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1787 | return false; |
| 1788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1790 | if (!FinishSubobjectInit) |
| 1791 | return false; |
| 1792 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1793 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1794 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1795 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1796 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1797 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1801 | // Get the structured initializer list for a subobject of type |
| 1802 | // @p CurrentObjectType. |
| 1803 | InitListExpr * |
| 1804 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1805 | QualType CurrentObjectType, |
| 1806 | InitListExpr *StructuredList, |
| 1807 | unsigned StructuredIndex, |
| 1808 | SourceRange InitRange) { |
| 1809 | Expr *ExistingInit = 0; |
| 1810 | if (!StructuredList) |
| 1811 | ExistingInit = SyntacticToSemantic[IList]; |
| 1812 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1813 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1815 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1816 | return Result; |
| 1817 | |
| 1818 | if (ExistingInit) { |
| 1819 | // We are creating an initializer list that initializes the |
| 1820 | // subobjects of the current object, but there was already an |
| 1821 | // initialization that completely initialized the current |
| 1822 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1824 | // struct X { int a, b; }; |
| 1825 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1827 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1828 | // designated initializer re-initializes the whole |
| 1829 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1831 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1832 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1834 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1835 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1836 | << ExistingInit->getSourceRange(); |
| 1837 | } |
| 1838 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1840 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1841 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1842 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1844 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1846 | // Pre-allocate storage for the structured initializer list. |
| 1847 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1848 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1849 | bool GotNumInits = false; |
| 1850 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1851 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1852 | GotNumInits = true; |
| 1853 | } else if (Index < IList->getNumInits()) { |
| 1854 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1855 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1856 | GotNumInits = true; |
| 1857 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1861 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1862 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1863 | NumElements = CAType->getSize().getZExtValue(); |
| 1864 | // Simple heuristic so that we don't allocate a very large |
| 1865 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1866 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1867 | NumElements = 0; |
| 1868 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1869 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1870 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1871 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1872 | RecordDecl *RDecl = RType->getDecl(); |
| 1873 | if (RDecl->isUnion()) |
| 1874 | NumElements = 1; |
| 1875 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1877 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1880 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1881 | NumElements = IList->getNumInits(); |
| 1882 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1883 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1885 | // Link this new initializer list into the structured initializer |
| 1886 | // lists. |
| 1887 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1888 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1889 | else { |
| 1890 | Result->setSyntacticForm(IList); |
| 1891 | SyntacticToSemantic[IList] = Result; |
| 1892 | } |
| 1893 | |
| 1894 | return Result; |
| 1895 | } |
| 1896 | |
| 1897 | /// Update the initializer at index @p StructuredIndex within the |
| 1898 | /// structured initializer list to the value @p expr. |
| 1899 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1900 | unsigned &StructuredIndex, |
| 1901 | Expr *expr) { |
| 1902 | // No structured initializer list to update |
| 1903 | if (!StructuredList) |
| 1904 | return; |
| 1905 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1906 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1907 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1908 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1910 | diag::warn_initializer_overrides) |
| 1911 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1913 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1914 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1915 | << PrevInit->getSourceRange(); |
| 1916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1918 | ++StructuredIndex; |
| 1919 | } |
| 1920 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1921 | /// Check that the given Index expression is a valid array designator |
| 1922 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1923 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1924 | /// and produces a reasonable diagnostic if there is a |
| 1925 | /// failure. Returns true if there was an error, false otherwise. If |
| 1926 | /// everything went okay, Value will receive the value of the constant |
| 1927 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1929 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1930 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1931 | |
| 1932 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1933 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1934 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1936 | if (Value.isSigned() && Value.isNegative()) |
| 1937 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1938 | << Value.toString(10) << Index->getSourceRange(); |
| 1939 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1940 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1941 | return false; |
| 1942 | } |
| 1943 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1944 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1945 | SourceLocation Loc, |
| 1946 | bool GNUSyntax, |
| 1947 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1948 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1949 | |
| 1950 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1951 | SmallVector<ASTDesignator, 32> Designators; |
| 1952 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Build designators and check array designator expressions. |
| 1955 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1956 | const Designator &D = Desig.getDesignator(Idx); |
| 1957 | switch (D.getKind()) { |
| 1958 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1960 | D.getFieldLoc())); |
| 1961 | break; |
| 1962 | |
| 1963 | case Designator::ArrayDesignator: { |
| 1964 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1965 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1966 | if (!Index->isTypeDependent() && |
| 1967 | !Index->isValueDependent() && |
| 1968 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1969 | Invalid = true; |
| 1970 | else { |
| 1971 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1973 | D.getRBracketLoc())); |
| 1974 | InitExpressions.push_back(Index); |
| 1975 | } |
| 1976 | break; |
| 1977 | } |
| 1978 | |
| 1979 | case Designator::ArrayRangeDesignator: { |
| 1980 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1981 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1982 | llvm::APSInt StartValue; |
| 1983 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1984 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1985 | StartIndex->isValueDependent(); |
| 1986 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1987 | EndIndex->isValueDependent(); |
| 1988 | if ((!StartDependent && |
| 1989 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1990 | (!EndDependent && |
| 1991 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1992 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1993 | else { |
| 1994 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1995 | if (StartDependent || EndDependent) { |
| 1996 | // Nothing to compute. |
| 1997 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1998 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1999 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2000 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2002 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2003 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2005 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2006 | Invalid = true; |
| 2007 | } else { |
| 2008 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2010 | D.getEllipsisLoc(), |
| 2011 | D.getRBracketLoc())); |
| 2012 | InitExpressions.push_back(StartIndex); |
| 2013 | InitExpressions.push_back(EndIndex); |
| 2014 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2015 | } |
| 2016 | break; |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | if (Invalid || Init.isInvalid()) |
| 2022 | return ExprError(); |
| 2023 | |
| 2024 | // Clear out the expressions within the designation. |
| 2025 | Desig.ClearExprs(*this); |
| 2026 | |
| 2027 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2028 | = DesignatedInitExpr::Create(Context, |
| 2029 | Designators.data(), Designators.size(), |
| 2030 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2031 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2033 | if (getLangOptions().CPlusPlus) |
Eli Friedman | a47317b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2034 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2035 | << DIE->getSourceRange(); |
| 2036 | else if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2037 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2038 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2040 | return Owned(DIE); |
| 2041 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2043 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2044 | InitListExpr *&InitList, QualType &DeclType) { |
| 2045 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2046 | if (!CheckInitList.HadError()) |
| 2047 | InitList = CheckInitList.getFullyStructuredList(); |
| 2048 | |
| 2049 | return CheckInitList.HadError(); |
| 2050 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2052 | //===----------------------------------------------------------------------===// |
| 2053 | // Initialization entity |
| 2054 | //===----------------------------------------------------------------------===// |
| 2055 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2056 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2057 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2058 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2059 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2060 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2061 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2062 | Type = AT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2063 | } else { |
| 2064 | Kind = EK_VectorElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2065 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2066 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2069 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2070 | CXXBaseSpecifier *Base, |
| 2071 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2072 | { |
| 2073 | InitializedEntity Result; |
| 2074 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2075 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2076 | if (IsInheritedVirtualBase) |
| 2077 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2079 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2080 | return Result; |
| 2081 | } |
| 2082 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2083 | DeclarationName InitializedEntity::getName() const { |
| 2084 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2085 | case EK_Parameter: { |
| 2086 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2087 | return (D ? D->getDeclName() : DeclarationName()); |
| 2088 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2089 | |
| 2090 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2091 | case EK_Member: |
| 2092 | return VariableOrMember->getDeclName(); |
| 2093 | |
| 2094 | case EK_Result: |
| 2095 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2096 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2097 | case EK_Temporary: |
| 2098 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2099 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2100 | case EK_ArrayElement: |
| 2101 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2102 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2103 | return DeclarationName(); |
| 2104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2106 | // Silence GCC warning |
| 2107 | return DeclarationName(); |
| 2108 | } |
| 2109 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2110 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2111 | switch (getKind()) { |
| 2112 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2113 | case EK_Member: |
| 2114 | return VariableOrMember; |
| 2115 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2116 | case EK_Parameter: |
| 2117 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2118 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2119 | case EK_Result: |
| 2120 | case EK_Exception: |
| 2121 | case EK_New: |
| 2122 | case EK_Temporary: |
| 2123 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2124 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2125 | case EK_ArrayElement: |
| 2126 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2127 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2128 | return 0; |
| 2129 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2130 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2131 | // Silence GCC warning |
| 2132 | return 0; |
| 2133 | } |
| 2134 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2135 | bool InitializedEntity::allowsNRVO() const { |
| 2136 | switch (getKind()) { |
| 2137 | case EK_Result: |
| 2138 | case EK_Exception: |
| 2139 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2141 | case EK_Variable: |
| 2142 | case EK_Parameter: |
| 2143 | case EK_Member: |
| 2144 | case EK_New: |
| 2145 | case EK_Temporary: |
| 2146 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2147 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2148 | case EK_ArrayElement: |
| 2149 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2150 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2151 | break; |
| 2152 | } |
| 2153 | |
| 2154 | return false; |
| 2155 | } |
| 2156 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2157 | //===----------------------------------------------------------------------===// |
| 2158 | // Initialization sequence |
| 2159 | //===----------------------------------------------------------------------===// |
| 2160 | |
| 2161 | void InitializationSequence::Step::Destroy() { |
| 2162 | switch (Kind) { |
| 2163 | case SK_ResolveAddressOfOverloadedFunction: |
| 2164 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2165 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2166 | case SK_CastDerivedToBaseLValue: |
| 2167 | case SK_BindReference: |
| 2168 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2169 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2170 | case SK_UserConversion: |
| 2171 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2172 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2173 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2174 | case SK_ListInitialization: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2175 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2176 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2177 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2178 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2179 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2180 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2181 | case SK_PassByIndirectCopyRestore: |
| 2182 | case SK_PassByIndirectRestore: |
| 2183 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2184 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2186 | case SK_ConversionSequence: |
| 2187 | delete ICS; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2191 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2192 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2196 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2197 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2199 | switch (getFailureKind()) { |
| 2200 | case FK_TooManyInitsForReference: |
| 2201 | case FK_ArrayNeedsInitList: |
| 2202 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2203 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2204 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2205 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2206 | case FK_RValueReferenceBindingToLValue: |
| 2207 | case FK_ReferenceInitDropsQualifiers: |
| 2208 | case FK_ReferenceInitFailed: |
| 2209 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2210 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2211 | case FK_TooManyInitsForScalar: |
| 2212 | case FK_ReferenceBindingToInitList: |
| 2213 | case FK_InitListBadDestinationType: |
| 2214 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2215 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2216 | case FK_ArrayTypeMismatch: |
| 2217 | case FK_NonConstantArrayInit: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2218 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2220 | case FK_ReferenceInitOverloadFailed: |
| 2221 | case FK_UserConversionOverloadFailed: |
| 2222 | case FK_ConstructorOverloadFailed: |
| 2223 | return FailedOverloadResult == OR_Ambiguous; |
| 2224 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2225 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2226 | return false; |
| 2227 | } |
| 2228 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2229 | bool InitializationSequence::isConstructorInitialization() const { |
| 2230 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2231 | } |
| 2232 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2233 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2234 | const Expr *Initializer, |
| 2235 | bool *isInitializerConstant, |
| 2236 | APValue *ConstantValue) const { |
| 2237 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2238 | return false; |
| 2239 | |
| 2240 | const Step &LastStep = Steps.back(); |
| 2241 | if (LastStep.Kind != SK_ConversionSequence) |
| 2242 | return false; |
| 2243 | |
| 2244 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2245 | const StandardConversionSequence *SCS = NULL; |
| 2246 | switch (ICS.getKind()) { |
| 2247 | case ImplicitConversionSequence::StandardConversion: |
| 2248 | SCS = &ICS.Standard; |
| 2249 | break; |
| 2250 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2251 | SCS = &ICS.UserDefined.After; |
| 2252 | break; |
| 2253 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2254 | case ImplicitConversionSequence::EllipsisConversion: |
| 2255 | case ImplicitConversionSequence::BadConversion: |
| 2256 | return false; |
| 2257 | } |
| 2258 | |
| 2259 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2260 | // [dcl.init.list]p7: |
| 2261 | // |
| 2262 | // A narrowing conversion is an implicit conversion ... |
| 2263 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2264 | QualType FromType = SCS->getToType(0); |
| 2265 | QualType ToType = SCS->getToType(1); |
| 2266 | switch (PossibleNarrowing) { |
| 2267 | // * from a floating-point type to an integer type, or |
| 2268 | // |
| 2269 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2270 | // type, except where the source is a constant expression and the actual |
| 2271 | // value after conversion will fit into the target type and will produce |
| 2272 | // the original value when converted back to the original type, or |
| 2273 | case ICK_Floating_Integral: |
| 2274 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2275 | *isInitializerConstant = false; |
| 2276 | return true; |
| 2277 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2278 | llvm::APSInt IntConstantValue; |
| 2279 | if (Initializer && |
| 2280 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2281 | // Convert the integer to the floating type. |
| 2282 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2283 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2284 | llvm::APFloat::rmNearestTiesToEven); |
| 2285 | // And back. |
| 2286 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2287 | bool ignored; |
| 2288 | Result.convertToInteger(ConvertedValue, |
| 2289 | llvm::APFloat::rmTowardZero, &ignored); |
| 2290 | // If the resulting value is different, this was a narrowing conversion. |
| 2291 | if (IntConstantValue != ConvertedValue) { |
| 2292 | *isInitializerConstant = true; |
| 2293 | *ConstantValue = APValue(IntConstantValue); |
| 2294 | return true; |
| 2295 | } |
| 2296 | } else { |
| 2297 | // Variables are always narrowings. |
| 2298 | *isInitializerConstant = false; |
| 2299 | return true; |
| 2300 | } |
| 2301 | } |
| 2302 | return false; |
| 2303 | |
| 2304 | // * from long double to double or float, or from double to float, except |
| 2305 | // where the source is a constant expression and the actual value after |
| 2306 | // conversion is within the range of values that can be represented (even |
| 2307 | // if it cannot be represented exactly), or |
| 2308 | case ICK_Floating_Conversion: |
| 2309 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2310 | // FromType is larger than ToType. |
| 2311 | Expr::EvalResult InitializerValue; |
| 2312 | // FIXME: Check whether Initializer is a constant expression according |
| 2313 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2314 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2315 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2316 | // Constant! (Except for FIXME above.) |
| 2317 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2318 | // Convert the source value into the target type. |
| 2319 | bool ignored; |
| 2320 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2321 | Ctx.getFloatTypeSemantics(ToType), |
| 2322 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2323 | // If there was no overflow, the source value is within the range of |
| 2324 | // values that can be represented. |
| 2325 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2326 | *isInitializerConstant = true; |
| 2327 | *ConstantValue = InitializerValue.Val; |
| 2328 | return true; |
| 2329 | } |
| 2330 | } else { |
| 2331 | *isInitializerConstant = false; |
| 2332 | return true; |
| 2333 | } |
| 2334 | } |
| 2335 | return false; |
| 2336 | |
| 2337 | // * from an integer type or unscoped enumeration type to an integer type |
| 2338 | // that cannot represent all the values of the original type, except where |
| 2339 | // the source is a constant expression and the actual value after |
| 2340 | // conversion will fit into the target type and will produce the original |
| 2341 | // value when converted back to the original type. |
Jeffrey Yasskin | 6d0ee8d | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2342 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | b89d5ed | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2343 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2344 | // Boolean conversions can be from pointers and pointers to members |
| 2345 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2346 | return false; |
| 2347 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2348 | case ICK_Integral_Conversion: { |
| 2349 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2350 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2351 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2352 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2353 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2354 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2355 | |
| 2356 | if (FromWidth > ToWidth || |
| 2357 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2358 | // Not all values of FromType can be represented in ToType. |
| 2359 | llvm::APSInt InitializerValue; |
| 2360 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2361 | *isInitializerConstant = true; |
| 2362 | *ConstantValue = APValue(InitializerValue); |
| 2363 | |
| 2364 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2365 | // signed vs. unsigned comparisons. |
| 2366 | InitializerValue = InitializerValue.extend( |
| 2367 | InitializerValue.getBitWidth() + 1); |
| 2368 | // Convert the initializer to and from the target width and signed-ness. |
| 2369 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2370 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2371 | ConvertedValue.setIsSigned(ToSigned); |
| 2372 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2373 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2374 | // If the result is different, this was a narrowing conversion. |
| 2375 | return ConvertedValue != InitializerValue; |
| 2376 | } else { |
| 2377 | // Variables are always narrowings. |
| 2378 | *isInitializerConstant = false; |
| 2379 | return true; |
| 2380 | } |
| 2381 | } |
| 2382 | return false; |
| 2383 | } |
| 2384 | |
| 2385 | default: |
| 2386 | // Other kinds of conversions are not narrowings. |
| 2387 | return false; |
| 2388 | } |
| 2389 | } |
| 2390 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2391 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2392 | FunctionDecl *Function, |
| 2393 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2394 | Step S; |
| 2395 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2396 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2397 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2398 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | Steps.push_back(S); |
| 2400 | } |
| 2401 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2403 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2404 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2405 | switch (VK) { |
| 2406 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2407 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2408 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2409 | default: llvm_unreachable("No such category"); |
| 2410 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2411 | S.Type = BaseType; |
| 2412 | Steps.push_back(S); |
| 2413 | } |
| 2414 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2415 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2416 | bool BindingTemporary) { |
| 2417 | Step S; |
| 2418 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2419 | S.Type = T; |
| 2420 | Steps.push_back(S); |
| 2421 | } |
| 2422 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2423 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2424 | Step S; |
| 2425 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2426 | S.Type = T; |
| 2427 | Steps.push_back(S); |
| 2428 | } |
| 2429 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2430 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2431 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2432 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2433 | Step S; |
| 2434 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2435 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2436 | S.Function.Function = Function; |
| 2437 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2438 | Steps.push_back(S); |
| 2439 | } |
| 2440 | |
| 2441 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2442 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2443 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2444 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2445 | switch (VK) { |
| 2446 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2447 | S.Kind = SK_QualificationConversionRValue; |
| 2448 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2449 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2450 | S.Kind = SK_QualificationConversionXValue; |
| 2451 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2452 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2453 | S.Kind = SK_QualificationConversionLValue; |
| 2454 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2455 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2456 | S.Type = Ty; |
| 2457 | Steps.push_back(S); |
| 2458 | } |
| 2459 | |
| 2460 | void InitializationSequence::AddConversionSequenceStep( |
| 2461 | const ImplicitConversionSequence &ICS, |
| 2462 | QualType T) { |
| 2463 | Step S; |
| 2464 | S.Kind = SK_ConversionSequence; |
| 2465 | S.Type = T; |
| 2466 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2467 | Steps.push_back(S); |
| 2468 | } |
| 2469 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2470 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2471 | Step S; |
| 2472 | S.Kind = SK_ListInitialization; |
| 2473 | S.Type = T; |
| 2474 | Steps.push_back(S); |
| 2475 | } |
| 2476 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2477 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2478 | InitializationSequence::AddConstructorInitializationStep( |
| 2479 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2480 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2481 | QualType T) { |
| 2482 | Step S; |
| 2483 | S.Kind = SK_ConstructorInitialization; |
| 2484 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2485 | S.Function.Function = Constructor; |
| 2486 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2487 | Steps.push_back(S); |
| 2488 | } |
| 2489 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2490 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2491 | Step S; |
| 2492 | S.Kind = SK_ZeroInitialization; |
| 2493 | S.Type = T; |
| 2494 | Steps.push_back(S); |
| 2495 | } |
| 2496 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2497 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2498 | Step S; |
| 2499 | S.Kind = SK_CAssignment; |
| 2500 | S.Type = T; |
| 2501 | Steps.push_back(S); |
| 2502 | } |
| 2503 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2504 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2505 | Step S; |
| 2506 | S.Kind = SK_StringInit; |
| 2507 | S.Type = T; |
| 2508 | Steps.push_back(S); |
| 2509 | } |
| 2510 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2511 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2512 | Step S; |
| 2513 | S.Kind = SK_ObjCObjectConversion; |
| 2514 | S.Type = T; |
| 2515 | Steps.push_back(S); |
| 2516 | } |
| 2517 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2518 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2519 | Step S; |
| 2520 | S.Kind = SK_ArrayInit; |
| 2521 | S.Type = T; |
| 2522 | Steps.push_back(S); |
| 2523 | } |
| 2524 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2525 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2526 | bool shouldCopy) { |
| 2527 | Step s; |
| 2528 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2529 | : SK_PassByIndirectRestore); |
| 2530 | s.Type = type; |
| 2531 | Steps.push_back(s); |
| 2532 | } |
| 2533 | |
| 2534 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2535 | Step S; |
| 2536 | S.Kind = SK_ProduceObjCObject; |
| 2537 | S.Type = T; |
| 2538 | Steps.push_back(S); |
| 2539 | } |
| 2540 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2542 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2543 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2544 | this->Failure = Failure; |
| 2545 | this->FailedOverloadResult = Result; |
| 2546 | } |
| 2547 | |
| 2548 | //===----------------------------------------------------------------------===// |
| 2549 | // Attempt initialization |
| 2550 | //===----------------------------------------------------------------------===// |
| 2551 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2552 | static void MaybeProduceObjCObject(Sema &S, |
| 2553 | InitializationSequence &Sequence, |
| 2554 | const InitializedEntity &Entity) { |
| 2555 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2556 | |
| 2557 | /// When initializing a parameter, produce the value if it's marked |
| 2558 | /// __attribute__((ns_consumed)). |
| 2559 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2560 | if (!Entity.isParameterConsumed()) |
| 2561 | return; |
| 2562 | |
| 2563 | assert(Entity.getType()->isObjCRetainableType() && |
| 2564 | "consuming an object of unretainable type?"); |
| 2565 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2566 | |
| 2567 | /// When initializing a return value, if the return type is a |
| 2568 | /// retainable type, then returns need to immediately retain the |
| 2569 | /// object. If an autorelease is required, it will be done at the |
| 2570 | /// last instant. |
| 2571 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2572 | if (!Entity.getType()->isObjCRetainableType()) |
| 2573 | return; |
| 2574 | |
| 2575 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2576 | } |
| 2577 | } |
| 2578 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2579 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2580 | static void TryListInitialization(Sema &S, |
| 2581 | const InitializedEntity &Entity, |
| 2582 | const InitializationKind &Kind, |
| 2583 | InitListExpr *InitList, |
| 2584 | InitializationSequence &Sequence) { |
| 2585 | // FIXME: We only perform rudimentary checking of list |
| 2586 | // initializations at this point, then assume that any list |
| 2587 | // initialization of an array, aggregate, or scalar will be |
| 2588 | // well-formed. When we actually "perform" list initialization, we'll |
| 2589 | // do all of the necessary checking. C++0x initializer lists will |
| 2590 | // force us to perform more checking here. |
| 2591 | |
| 2592 | QualType DestType = Entity.getType(); |
| 2593 | |
| 2594 | // C++ [dcl.init]p13: |
| 2595 | // If T is a scalar type, then a declaration of the form |
| 2596 | // |
| 2597 | // T x = { a }; |
| 2598 | // |
| 2599 | // is equivalent to |
| 2600 | // |
| 2601 | // T x = a; |
| 2602 | if (DestType->isScalarType()) { |
| 2603 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2604 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | // Assume scalar initialization from a single value works. |
| 2609 | } else if (DestType->isAggregateType()) { |
| 2610 | // Assume aggregate initialization works. |
| 2611 | } else if (DestType->isVectorType()) { |
| 2612 | // Assume vector initialization works. |
| 2613 | } else if (DestType->isReferenceType()) { |
| 2614 | // FIXME: C++0x defines behavior for this. |
| 2615 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2616 | return; |
| 2617 | } else if (DestType->isRecordType()) { |
| 2618 | // FIXME: C++0x defines behavior for this |
| 2619 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2620 | } |
| 2621 | |
| 2622 | // Add a general "list initialization" step. |
| 2623 | Sequence.AddListInitializationStep(DestType); |
| 2624 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2625 | |
| 2626 | /// \brief Try a reference initialization that involves calling a conversion |
| 2627 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2628 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2629 | const InitializedEntity &Entity, |
| 2630 | const InitializationKind &Kind, |
| 2631 | Expr *Initializer, |
| 2632 | bool AllowRValues, |
| 2633 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2634 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2635 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2636 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2637 | QualType cv2T2 = Initializer->getType(); |
| 2638 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2639 | |
| 2640 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2641 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2642 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2644 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2645 | ObjCConversion, |
| 2646 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2647 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2648 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2649 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2650 | (void)ObjCLifetimeConversion; |
| 2651 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2652 | // Build the candidate set directly in the initialization sequence |
| 2653 | // structure, so that it will persist if we fail. |
| 2654 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2655 | CandidateSet.clear(); |
| 2656 | |
| 2657 | // Determine whether we are allowed to call explicit constructors or |
| 2658 | // explicit conversion operators. |
| 2659 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2661 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2662 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2663 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2664 | // The type we're converting to is a class type. Enumerate its constructors |
| 2665 | // to see if there is a suitable conversion. |
| 2666 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2667 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2668 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2669 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2670 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2671 | NamedDecl *D = *Con; |
| 2672 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2673 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2674 | // Find the constructor (which may be a template). |
| 2675 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2676 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2677 | if (ConstructorTmpl) |
| 2678 | Constructor = cast<CXXConstructorDecl>( |
| 2679 | ConstructorTmpl->getTemplatedDecl()); |
| 2680 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2681 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2683 | if (!Constructor->isInvalidDecl() && |
| 2684 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2685 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2686 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2687 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2688 | &Initializer, 1, CandidateSet, |
| 2689 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2690 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2691 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2692 | &Initializer, 1, CandidateSet, |
| 2693 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2694 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2695 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2696 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2697 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2698 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2700 | const RecordType *T2RecordType = 0; |
| 2701 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2702 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2703 | // The type we're converting from is a class type, enumerate its conversion |
| 2704 | // functions. |
| 2705 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2706 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2707 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2708 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2709 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2710 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2711 | NamedDecl *D = *I; |
| 2712 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2713 | if (isa<UsingShadowDecl>(D)) |
| 2714 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2715 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2716 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2717 | CXXConversionDecl *Conv; |
| 2718 | if (ConvTemplate) |
| 2719 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2720 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2721 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2723 | // If the conversion function doesn't return a reference type, |
| 2724 | // it can't be considered for this conversion unless we're allowed to |
| 2725 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2726 | // FIXME: Do we need to make sure that we only consider conversion |
| 2727 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2728 | // break recursion. |
| 2729 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2730 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2731 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2732 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2733 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2734 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2735 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2736 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2737 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2741 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2742 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2744 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2745 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2747 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2748 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2749 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2750 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2751 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2752 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2753 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2754 | // This is the overload that will actually be used for the initialization, so |
| 2755 | // mark it as used. |
| 2756 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2757 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2758 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2759 | if (isa<CXXConversionDecl>(Function)) |
| 2760 | T2 = Function->getResultType(); |
| 2761 | else |
| 2762 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2763 | |
| 2764 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2765 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2766 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2767 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2769 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2770 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2771 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2772 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2773 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2774 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2776 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2777 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2778 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2779 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2780 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2781 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2782 | NewDerivedToBase, NewObjCConversion, |
| 2783 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2784 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2785 | // If the type we've converted to is not reference-related to the |
| 2786 | // type we're looking for, then there is another conversion step |
| 2787 | // we need to perform to produce a temporary of the right type |
| 2788 | // that we'll be binding to. |
| 2789 | ImplicitConversionSequence ICS; |
| 2790 | ICS.setStandard(); |
| 2791 | ICS.Standard = Best->FinalConversion; |
| 2792 | T2 = ICS.Standard.getToType(2); |
| 2793 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2794 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2795 | Sequence.AddDerivedToBaseCastStep( |
| 2796 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2798 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2799 | else if (NewObjCConversion) |
| 2800 | Sequence.AddObjCObjectConversionStep( |
| 2801 | S.Context.getQualifiedType(T1, |
| 2802 | T2.getNonReferenceType().getQualifiers())); |
| 2803 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2804 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2805 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2807 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2808 | return OR_Success; |
| 2809 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2810 | |
| 2811 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2812 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2813 | const InitializedEntity &Entity, |
| 2814 | const InitializationKind &Kind, |
| 2815 | Expr *Initializer, |
| 2816 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2817 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2818 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2819 | Qualifiers T1Quals; |
| 2820 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2821 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2822 | Qualifiers T2Quals; |
| 2823 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2824 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2826 | // If the initializer is the address of an overloaded function, try |
| 2827 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2828 | // type of the resulting function. |
| 2829 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2830 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2831 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2832 | T1, |
| 2833 | false, |
| 2834 | Found)) { |
| 2835 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2836 | cv2T2 = Fn->getType(); |
| 2837 | T2 = cv2T2.getUnqualifiedType(); |
| 2838 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2839 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2840 | return; |
| 2841 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2842 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2843 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | // Compute some basic properties of the types and the initializer. |
| 2845 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2846 | bool isRValueRef = !isLValueRef; |
| 2847 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2848 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2849 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2850 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2851 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2852 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2853 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2855 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2856 | // 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] | 2857 | // "cv2 T2" as follows: |
| 2858 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2860 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2861 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2862 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2863 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2864 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2865 | bool T1Function = T1->isFunctionType(); |
| 2866 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2868 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2869 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2870 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2871 | // - 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] | 2872 | // reference-compatible with "cv2 T2," or |
| 2873 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2874 | // 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] | 2875 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2876 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2877 | // to decide whether we're actually binding to a temporary created from |
| 2878 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2879 | if (DerivedToBase) |
| 2880 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2881 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2882 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2883 | else if (ObjCConversion) |
| 2884 | Sequence.AddObjCObjectConversionStep( |
| 2885 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2886 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2887 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2888 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2889 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2890 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2891 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | |
| 2895 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2896 | // reference-related to T2, and can be implicitly converted to an |
| 2897 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2898 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2899 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2900 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2901 | // If we have an rvalue ref to function type here, the rhs must be |
| 2902 | // an rvalue. |
| 2903 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2904 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2905 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2906 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2907 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2908 | Sequence); |
| 2909 | if (ConvOvlResult == OR_Success) |
| 2910 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2911 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2912 | Sequence.SetOverloadFailure( |
| 2913 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2914 | ConvOvlResult); |
| 2915 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2918 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2919 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2920 | // 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] | 2921 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2922 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2923 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2924 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2925 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2926 | Sequence.SetOverloadFailure( |
| 2927 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2928 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2929 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2930 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2931 | ? (RefRelationship == Sema::Ref_Related |
| 2932 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2933 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2934 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2936 | return; |
| 2937 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2939 | // - If the initializer expression |
| 2940 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2941 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2942 | // Note: functions are handled below. |
| 2943 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2944 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2945 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2946 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2947 | (InitCategory.isXValue() || |
| 2948 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2949 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2950 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2951 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2952 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2953 | // compiler the freedom to perform a copy here or bind to the |
| 2954 | // object, while C++0x requires that we bind directly to the |
| 2955 | // object. Hence, we always bind to the object without making an |
| 2956 | // extra copy. However, in C++03 requires that we check for the |
| 2957 | // presence of a suitable copy constructor: |
| 2958 | // |
| 2959 | // The constructor that would be used to make the copy shall |
| 2960 | // be callable whether or not the copy is actually done. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2961 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2962 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2963 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2965 | if (DerivedToBase) |
| 2966 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2967 | ValueKind); |
| 2968 | else if (ObjCConversion) |
| 2969 | Sequence.AddObjCObjectConversionStep( |
| 2970 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2972 | if (T1Quals != T2Quals) |
| 2973 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2975 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2977 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2978 | |
| 2979 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2980 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2981 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2982 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2983 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2984 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2985 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2986 | Kind, Initializer, |
| 2987 | /*AllowRValues=*/true, |
| 2988 | Sequence); |
| 2989 | if (ConvOvlResult) |
| 2990 | Sequence.SetOverloadFailure( |
| 2991 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2992 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2993 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2994 | return; |
| 2995 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2996 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2997 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2998 | return; |
| 2999 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3000 | |
| 3001 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3002 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3004 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3006 | // Determine whether we are allowed to call explicit constructors or |
| 3007 | // explicit conversion operators. |
| 3008 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3009 | |
| 3010 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3011 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3012 | ImplicitConversionSequence ICS |
| 3013 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3014 | /*SuppressUserConversions*/ false, |
| 3015 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3016 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3017 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3018 | /*AllowObjCWritebackConversion=*/false); |
| 3019 | |
| 3020 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3021 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3022 | // this into an overloading ambiguity diagnostic. However, we need |
| 3023 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3024 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3025 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3026 | Sequence.SetOverloadFailure( |
| 3027 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3028 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3029 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3030 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3031 | else |
| 3032 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3033 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3034 | } else { |
| 3035 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3039 | // same cv-qualification as, or greater cv-qualification |
| 3040 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3041 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3042 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3043 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3044 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3045 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3046 | return; |
| 3047 | } |
| 3048 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | // [...] 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] | 3050 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3052 | InitCategory.isLValue()) { |
| 3053 | Sequence.SetFailed( |
| 3054 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3055 | return; |
| 3056 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3058 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3059 | return; |
| 3060 | } |
| 3061 | |
| 3062 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3064 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3065 | const InitializedEntity &Entity, |
| 3066 | const InitializationKind &Kind, |
| 3067 | Expr *Initializer, |
| 3068 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3069 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3072 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3073 | /// enumerates the constructors of the initialized entity and performs overload |
| 3074 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3076 | const InitializedEntity &Entity, |
| 3077 | const InitializationKind &Kind, |
| 3078 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3079 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3080 | InitializationSequence &Sequence) { |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3081 | // Check constructor arguments for self reference. |
| 3082 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3083 | // Parameters arguments are occassionially constructed with itself, |
| 3084 | // for instance, in recursive functions. Skip them. |
| 3085 | if (!isa<ParmVarDecl>(DD)) |
| 3086 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3087 | S.CheckSelfReference(DD, Args[i]); |
| 3088 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3089 | // Build the candidate set directly in the initialization sequence |
| 3090 | // structure, so that it will persist if we fail. |
| 3091 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3092 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3094 | // Determine whether we are allowed to call explicit constructors or |
| 3095 | // explicit conversion operators. |
| 3096 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3097 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3098 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3099 | |
| 3100 | // The type we're constructing needs to be complete. |
| 3101 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3102 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3103 | return; |
| 3104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3106 | // The type we're converting to is a class type. Enumerate its constructors |
| 3107 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3108 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3109 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3110 | CXXRecordDecl *DestRecordDecl |
| 3111 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3113 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3114 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3115 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3116 | NamedDecl *D = *Con; |
| 3117 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3118 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3120 | // Find the constructor (which may be a template). |
| 3121 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3122 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3123 | if (ConstructorTmpl) |
| 3124 | Constructor = cast<CXXConstructorDecl>( |
| 3125 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3126 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3127 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3128 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3129 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3130 | // suppress user-defined conversions on the arguments. |
| 3131 | // FIXME: Move constructors? |
| 3132 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3133 | Constructor->isCopyConstructor()) |
| 3134 | SuppressUserConversions = true; |
| 3135 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3137 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3138 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3139 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3140 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3141 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3142 | Args, NumArgs, CandidateSet, |
| 3143 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3144 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3145 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3146 | Args, NumArgs, CandidateSet, |
| 3147 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3148 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3151 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
| 3153 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3154 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3156 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3157 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3158 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3159 | Result); |
| 3160 | return; |
| 3161 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3162 | |
| 3163 | // C++0x [dcl.init]p6: |
| 3164 | // If a program calls for the default initialization of an object |
| 3165 | // of a const-qualified type T, T shall be a class type with a |
| 3166 | // user-provided default constructor. |
| 3167 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3168 | Entity.getType().isConstQualified() && |
| 3169 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3170 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3171 | return; |
| 3172 | } |
| 3173 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3174 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3175 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3176 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3178 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3179 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3182 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3183 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3184 | const InitializedEntity &Entity, |
| 3185 | const InitializationKind &Kind, |
| 3186 | InitializationSequence &Sequence) { |
| 3187 | // C++ [dcl.init]p5: |
| 3188 | // |
| 3189 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3190 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3192 | // -- if T is an array type, then each element is value-initialized; |
| 3193 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3194 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3196 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3197 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3198 | // -- if T is a class type (clause 9) with a user-declared |
| 3199 | // constructor (12.1), then the default constructor for T is |
| 3200 | // called (and the initialization is ill-formed if T has no |
| 3201 | // accessible default constructor); |
| 3202 | // |
| 3203 | // FIXME: we really want to refer to a single subobject of the array, |
| 3204 | // but Entity doesn't have a way to capture that (yet). |
| 3205 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3206 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3208 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3209 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3210 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3211 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3212 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3213 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3214 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3215 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3216 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3220 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3223 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3224 | static void TryDefaultInitialization(Sema &S, |
| 3225 | const InitializedEntity &Entity, |
| 3226 | const InitializationKind &Kind, |
| 3227 | InitializationSequence &Sequence) { |
| 3228 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3229 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3230 | // C++ [dcl.init]p6: |
| 3231 | // To default-initialize an object of type T means: |
| 3232 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3233 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3234 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3235 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3236 | // constructor for T is called (and the initialization is ill-formed if |
| 3237 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3238 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3239 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3240 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3242 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3243 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3245 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | // 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] | 3247 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3248 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3249 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3250 | return; |
| 3251 | } |
| 3252 | |
| 3253 | // If the destination type has a lifetime property, zero-initialize it. |
| 3254 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3255 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3256 | return; |
| 3257 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3260 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3261 | /// which enumerates all conversion functions and performs overload resolution |
| 3262 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3264 | const InitializedEntity &Entity, |
| 3265 | const InitializationKind &Kind, |
| 3266 | Expr *Initializer, |
| 3267 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3268 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3269 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3270 | QualType SourceType = Initializer->getType(); |
| 3271 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3272 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3274 | // Build the candidate set directly in the initialization sequence |
| 3275 | // structure, so that it will persist if we fail. |
| 3276 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3277 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3278 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3279 | // Determine whether we are allowed to call explicit constructors or |
| 3280 | // explicit conversion operators. |
| 3281 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3283 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3284 | // The type we're converting to is a class type. Enumerate its constructors |
| 3285 | // to see if there is a suitable conversion. |
| 3286 | CXXRecordDecl *DestRecordDecl |
| 3287 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3289 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3291 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3292 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3293 | Con != ConEnd; ++Con) { |
| 3294 | NamedDecl *D = *Con; |
| 3295 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3297 | // Find the constructor (which may be a template). |
| 3298 | CXXConstructorDecl *Constructor = 0; |
| 3299 | FunctionTemplateDecl *ConstructorTmpl |
| 3300 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3301 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3302 | Constructor = cast<CXXConstructorDecl>( |
| 3303 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3304 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3305 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3306 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3307 | if (!Constructor->isInvalidDecl() && |
| 3308 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3309 | if (ConstructorTmpl) |
| 3310 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3311 | /*ExplicitArgs*/ 0, |
| 3312 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3313 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3314 | else |
| 3315 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3316 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3317 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3319 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3320 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3321 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3322 | |
| 3323 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3324 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3325 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3326 | // The type we're converting from is a class type, enumerate its conversion |
| 3327 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3328 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3329 | // We can only enumerate the conversion functions for a complete type; if |
| 3330 | // the type isn't complete, simply skip this step. |
| 3331 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3332 | CXXRecordDecl *SourceRecordDecl |
| 3333 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3334 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3335 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3336 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3337 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3338 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3339 | I != E; ++I) { |
| 3340 | NamedDecl *D = *I; |
| 3341 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3342 | if (isa<UsingShadowDecl>(D)) |
| 3343 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3345 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3346 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3347 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3348 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3349 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3350 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3352 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3353 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3354 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3355 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3356 | CandidateSet); |
| 3357 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3358 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3359 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3360 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | |
| 3365 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3366 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3367 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3368 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3369 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3370 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3371 | Result); |
| 3372 | return; |
| 3373 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3375 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3376 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3377 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3378 | if (isa<CXXConstructorDecl>(Function)) { |
| 3379 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3380 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3381 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3386 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3387 | if (ConvType->getAs<RecordType>()) { |
| 3388 | // If we're converting to a class type, there may be an copy if |
| 3389 | // the resulting temporary object (possible to create an object of |
| 3390 | // a base class type). That copy is not a separate conversion, so |
| 3391 | // we just make a note of the actual destination type (possibly a |
| 3392 | // base class of the type returned by the conversion function) and |
| 3393 | // let the user-defined conversion step handle the conversion. |
| 3394 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3395 | return; |
| 3396 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3398 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3399 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3400 | // If the conversion following the call to the conversion function |
| 3401 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3402 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3403 | Best->FinalConversion.Third) { |
| 3404 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3405 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3406 | ICS.Standard = Best->FinalConversion; |
| 3407 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3408 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3411 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3412 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3413 | |
| 3414 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3415 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3416 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3417 | // Skip parens. |
| 3418 | e = e->IgnoreParens(); |
| 3419 | |
| 3420 | // Skip address-of nodes. |
| 3421 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3422 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3423 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3424 | |
| 3425 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3426 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3427 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3428 | case CK_Dependent: |
| 3429 | case CK_BitCast: |
| 3430 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3431 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3432 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3433 | |
| 3434 | case CK_ArrayToPointerDecay: |
| 3435 | return IIK_nonscalar; |
| 3436 | |
| 3437 | case CK_NullToPointer: |
| 3438 | return IIK_okay; |
| 3439 | |
| 3440 | default: |
| 3441 | break; |
| 3442 | } |
| 3443 | |
| 3444 | // 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] | 3445 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3446 | if (!isAddressOf) return IIK_nonlocal; |
| 3447 | |
| 3448 | VarDecl *var; |
| 3449 | if (isa<DeclRefExpr>(e)) { |
| 3450 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3451 | if (!var) return IIK_nonlocal; |
| 3452 | } else { |
| 3453 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3454 | } |
| 3455 | |
| 3456 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3457 | |
| 3458 | // If we have a conditional operator, check both sides. |
| 3459 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3460 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3461 | return iik; |
| 3462 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3463 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3464 | |
| 3465 | // These are never scalar. |
| 3466 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3467 | return IIK_nonscalar; |
| 3468 | |
| 3469 | // Otherwise, it needs to be a null pointer constant. |
| 3470 | } else { |
| 3471 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3472 | ? IIK_okay : IIK_nonlocal); |
| 3473 | } |
| 3474 | |
| 3475 | return IIK_nonlocal; |
| 3476 | } |
| 3477 | |
| 3478 | /// Check whether the given expression is a valid operand for an |
| 3479 | /// indirect copy/restore. |
| 3480 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3481 | assert(src->isRValue()); |
| 3482 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3483 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3484 | if (iik == IIK_okay) return; |
| 3485 | |
| 3486 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3487 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3488 | << src->getSourceRange(); |
| 3489 | } |
| 3490 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3491 | /// \brief Determine whether we have compatible array types for the |
| 3492 | /// purposes of GNU by-copy array initialization. |
| 3493 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3494 | const ArrayType *Dest, |
| 3495 | const ArrayType *Source) { |
| 3496 | // If the source and destination array types are equivalent, we're |
| 3497 | // done. |
| 3498 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3499 | return true; |
| 3500 | |
| 3501 | // Make sure that the element types are the same. |
| 3502 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3503 | return false; |
| 3504 | |
| 3505 | // The only mismatch we allow is when the destination is an |
| 3506 | // incomplete array type and the source is a constant array type. |
| 3507 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3508 | } |
| 3509 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3510 | static bool tryObjCWritebackConversion(Sema &S, |
| 3511 | InitializationSequence &Sequence, |
| 3512 | const InitializedEntity &Entity, |
| 3513 | Expr *Initializer) { |
| 3514 | bool ArrayDecay = false; |
| 3515 | QualType ArgType = Initializer->getType(); |
| 3516 | QualType ArgPointee; |
| 3517 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3518 | ArrayDecay = true; |
| 3519 | ArgPointee = ArgArrayType->getElementType(); |
| 3520 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3521 | } |
| 3522 | |
| 3523 | // Handle write-back conversion. |
| 3524 | QualType ConvertedArgType; |
| 3525 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3526 | ConvertedArgType)) |
| 3527 | return false; |
| 3528 | |
| 3529 | // We should copy unless we're passing to an argument explicitly |
| 3530 | // marked 'out'. |
| 3531 | bool ShouldCopy = true; |
| 3532 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3533 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3534 | |
| 3535 | // Do we need an lvalue conversion? |
| 3536 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3537 | ImplicitConversionSequence ICS; |
| 3538 | ICS.setStandard(); |
| 3539 | ICS.Standard.setAsIdentityConversion(); |
| 3540 | |
| 3541 | QualType ResultType; |
| 3542 | if (ArrayDecay) { |
| 3543 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3544 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3545 | } else { |
| 3546 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3547 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3548 | } |
| 3549 | |
| 3550 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3551 | } |
| 3552 | |
| 3553 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3554 | return true; |
| 3555 | } |
| 3556 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3557 | InitializationSequence::InitializationSequence(Sema &S, |
| 3558 | const InitializedEntity &Entity, |
| 3559 | const InitializationKind &Kind, |
| 3560 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3561 | unsigned NumArgs) |
| 3562 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3563 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | // The semantics of initializers are as follows. The destination type is |
| 3567 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3568 | // 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] | 3569 | // 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] | 3570 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3571 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3572 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3573 | if (DestType->isDependentType() || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3574 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3575 | SequenceKind = DependentSequence; |
| 3576 | return; |
| 3577 | } |
| 3578 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3579 | // Almost everything is a normal sequence. |
| 3580 | setSequenceKind(NormalSequence); |
| 3581 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3582 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3583 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3584 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3585 | if (Result.isInvalid()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3586 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3587 | return; |
| 3588 | } |
| 3589 | Args[I] = Result.take(); |
| 3590 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3591 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3592 | QualType SourceType; |
| 3593 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3594 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3595 | Initializer = Args[0]; |
| 3596 | if (!isa<InitListExpr>(Initializer)) |
| 3597 | SourceType = Initializer->getType(); |
| 3598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | |
| 3600 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3601 | // list-initialized (8.5.4). |
| 3602 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3603 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3604 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3605 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3606 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | // - If the destination type is a reference type, see 8.5.3. |
| 3608 | if (DestType->isReferenceType()) { |
| 3609 | // C++0x [dcl.init.ref]p1: |
| 3610 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3611 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3612 | // by an object that can be converted into a T. |
| 3613 | // (Therefore, multiple arguments are not permitted.) |
| 3614 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3615 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3616 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3617 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3618 | return; |
| 3619 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3620 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3621 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3622 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3623 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3624 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3625 | return; |
| 3626 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3627 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3628 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3629 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3630 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3633 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3634 | // - If the destination type is an array of characters, an array of |
| 3635 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3636 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3638 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3639 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3640 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3641 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3642 | return; |
| 3643 | } |
| 3644 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3645 | // Note: as an GNU C extension, we allow initialization of an |
| 3646 | // array from a compound literal that creates an array of the same |
| 3647 | // type, so long as the initializer has no side effects. |
| 3648 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3649 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3650 | Initializer->getType()->isArrayType()) { |
| 3651 | const ArrayType *SourceAT |
| 3652 | = Context.getAsArrayType(Initializer->getType()); |
| 3653 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3654 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3655 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3656 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3657 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3658 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3661 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3662 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3663 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3665 | return; |
| 3666 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3667 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3668 | // Determine whether we should consider writeback conversions for |
| 3669 | // Objective-C ARC. |
| 3670 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3671 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3672 | |
| 3673 | // We're at the end of the line for C: it's either a write-back conversion |
| 3674 | // 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] | 3675 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3676 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3677 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3678 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3683 | AddCAssignmentStep(DestType); |
| 3684 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3685 | return; |
| 3686 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3687 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3688 | assert(S.getLangOptions().CPlusPlus); |
| 3689 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3690 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3691 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | // - If the initialization is direct-initialization, or if it is |
| 3693 | // copy-initialization where the cv-unqualified version of the |
| 3694 | // 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] | 3695 | // class of the destination, constructors are considered. [...] |
| 3696 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3697 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3698 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3699 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3701 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3702 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3703 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3704 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3705 | // used) to a derived class thereof are enumerated as described in |
| 3706 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3707 | // (13.3). |
| 3708 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3709 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3710 | return; |
| 3711 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3713 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3714 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3715 | return; |
| 3716 | } |
| 3717 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3718 | |
| 3719 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3720 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3721 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3722 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3723 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3724 | return; |
| 3725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3726 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3727 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3728 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3729 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3730 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3731 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3732 | |
| 3733 | ImplicitConversionSequence ICS |
| 3734 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3735 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3736 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3737 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3738 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3739 | allowObjCWritebackConversion); |
| 3740 | |
| 3741 | if (ICS.isStandard() && |
| 3742 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3743 | // Objective-C ARC writeback conversion. |
| 3744 | |
| 3745 | // We should copy unless we're passing to an argument explicitly |
| 3746 | // marked 'out'. |
| 3747 | bool ShouldCopy = true; |
| 3748 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3749 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3750 | |
| 3751 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3752 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3753 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3754 | ImplicitConversionSequence LvalueICS; |
| 3755 | LvalueICS.setStandard(); |
| 3756 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3757 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3758 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3759 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3760 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3761 | |
| 3762 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3763 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3764 | DeclAccessPair dap; |
| 3765 | if (Initializer->getType() == Context.OverloadTy && |
| 3766 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3767 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3768 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3769 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3770 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3771 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3772 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3773 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3774 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3775 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3779 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3780 | StepEnd = Steps.end(); |
| 3781 | Step != StepEnd; ++Step) |
| 3782 | Step->Destroy(); |
| 3783 | } |
| 3784 | |
| 3785 | //===----------------------------------------------------------------------===// |
| 3786 | // Perform initialization |
| 3787 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3789 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3790 | switch(Entity.getKind()) { |
| 3791 | case InitializedEntity::EK_Variable: |
| 3792 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3793 | case InitializedEntity::EK_Exception: |
| 3794 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3795 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3796 | return Sema::AA_Initializing; |
| 3797 | |
| 3798 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3799 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3800 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3801 | return Sema::AA_Sending; |
| 3802 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3803 | return Sema::AA_Passing; |
| 3804 | |
| 3805 | case InitializedEntity::EK_Result: |
| 3806 | return Sema::AA_Returning; |
| 3807 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3808 | case InitializedEntity::EK_Temporary: |
| 3809 | // FIXME: Can we tell apart casting vs. converting? |
| 3810 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3811 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3812 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3813 | case InitializedEntity::EK_ArrayElement: |
| 3814 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3815 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3816 | return Sema::AA_Initializing; |
| 3817 | } |
| 3818 | |
| 3819 | return Sema::AA_Converting; |
| 3820 | } |
| 3821 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3822 | /// \brief Whether we should binding a created object as a temporary when |
| 3823 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3824 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3825 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3826 | case InitializedEntity::EK_ArrayElement: |
| 3827 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3828 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3829 | case InitializedEntity::EK_New: |
| 3830 | case InitializedEntity::EK_Variable: |
| 3831 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3832 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3833 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3834 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3835 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3836 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3838 | case InitializedEntity::EK_Parameter: |
| 3839 | case InitializedEntity::EK_Temporary: |
| 3840 | return true; |
| 3841 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3843 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3844 | } |
| 3845 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3846 | /// \brief Whether the given entity, when initialized with an object |
| 3847 | /// created for that initialization, requires destruction. |
| 3848 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3849 | switch (Entity.getKind()) { |
| 3850 | case InitializedEntity::EK_Member: |
| 3851 | case InitializedEntity::EK_Result: |
| 3852 | case InitializedEntity::EK_New: |
| 3853 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3854 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3855 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3856 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3857 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3858 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3859 | case InitializedEntity::EK_Variable: |
| 3860 | case InitializedEntity::EK_Parameter: |
| 3861 | case InitializedEntity::EK_Temporary: |
| 3862 | case InitializedEntity::EK_ArrayElement: |
| 3863 | case InitializedEntity::EK_Exception: |
| 3864 | return true; |
| 3865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | |
| 3867 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3870 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3871 | /// provided by the given initializer by calling the appropriate copy |
| 3872 | /// constructor. |
| 3873 | /// |
| 3874 | /// \param S The Sema object used for type-checking. |
| 3875 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3876 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3877 | /// the type of the initializer expression or a superclass thereof. |
| 3878 | /// |
| 3879 | /// \param Enter The entity being initialized. |
| 3880 | /// |
| 3881 | /// \param CurInit The initializer expression. |
| 3882 | /// |
| 3883 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3884 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3885 | /// an rvalue. |
| 3886 | /// |
| 3887 | /// \returns An expression that copies the initializer expression into |
| 3888 | /// a temporary object, or an error expression if a copy could not be |
| 3889 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3890 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3891 | QualType T, |
| 3892 | const InitializedEntity &Entity, |
| 3893 | ExprResult CurInit, |
| 3894 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3895 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3896 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3898 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3899 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3900 | if (!Class) |
| 3901 | return move(CurInit); |
| 3902 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3903 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3904 | // When certain criteria are met, an implementation is allowed to |
| 3905 | // omit the copy/move construction of a class object, even if the |
| 3906 | // copy/move constructor and/or destructor for the object have |
| 3907 | // side effects. [...] |
| 3908 | // - when a temporary class object that has not been bound to a |
| 3909 | // reference (12.2) would be copied/moved to a class object |
| 3910 | // with the same cv-unqualified type, the copy/move operation |
| 3911 | // can be omitted by constructing the temporary object |
| 3912 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3914 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3915 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3917 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3918 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3919 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3920 | switch (Entity.getKind()) { |
| 3921 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3922 | Loc = Entity.getReturnLoc(); |
| 3923 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3925 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3926 | Loc = Entity.getThrowLoc(); |
| 3927 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3928 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3929 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3930 | Loc = Entity.getDecl()->getLocation(); |
| 3931 | break; |
| 3932 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3933 | case InitializedEntity::EK_ArrayElement: |
| 3934 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3935 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3936 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3937 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3938 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3939 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3940 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3941 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3942 | Loc = CurInitExpr->getLocStart(); |
| 3943 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3944 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3945 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3946 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3947 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3948 | return move(CurInit); |
| 3949 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3950 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3951 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3952 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3953 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3954 | Con != ConEnd; ++Con) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3955 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3956 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3957 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3958 | CXXConstructorDecl *Constructor = 0; |
| 3959 | |
| 3960 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3961 | // Handle copy/moveconstructors, only. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3962 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3963 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3964 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3965 | continue; |
| 3966 | |
| 3967 | DeclAccessPair FoundDecl |
| 3968 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3969 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3970 | &CurInitExpr, 1, CandidateSet); |
| 3971 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3972 | } |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3973 | |
| 3974 | // Handle constructor templates. |
| 3975 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3976 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3977 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3978 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3979 | Constructor = cast<CXXConstructorDecl>( |
| 3980 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3981 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3982 | continue; |
| 3983 | |
| 3984 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3985 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3986 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3987 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3988 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3989 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3990 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3992 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3993 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3994 | case OR_Success: |
| 3995 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3997 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3998 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3999 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4000 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4001 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4002 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4003 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4004 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4005 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4006 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4007 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4008 | case OR_Ambiguous: |
| 4009 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4010 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4011 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4012 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4013 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4015 | case OR_Deleted: |
| 4016 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4017 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4018 | << CurInitExpr->getSourceRange(); |
| 4019 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4020 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4021 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4024 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4025 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4026 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4027 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4028 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4029 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (IsExtraneousCopy) { |
| 4032 | // If this is a totally extraneous copy for C++03 reference |
| 4033 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4034 | // expression. We don't generate an (elided) copy operation here |
| 4035 | // because doing so would require us to pass down a flag to avoid |
| 4036 | // infinite recursion, where each step adds another extraneous, |
| 4037 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4039 | // Instantiate the default arguments of any extra parameters in |
| 4040 | // the selected copy constructor, as if we were going to create a |
| 4041 | // proper call to the copy constructor. |
| 4042 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4043 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4044 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4045 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4046 | break; |
| 4047 | |
| 4048 | // Build the default argument expression; we don't actually care |
| 4049 | // if this succeeds or not, because this routine will complain |
| 4050 | // if there was a problem. |
| 4051 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4052 | } |
| 4053 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4054 | return S.Owned(CurInitExpr); |
| 4055 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4056 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4057 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4058 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4059 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4060 | // constructor call (we might have derived-to-base conversions, or |
| 4061 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4062 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4063 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4064 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4066 | // Actually perform the constructor call. |
| 4067 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4068 | move_arg(ConstructorArgs), |
| 4069 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4070 | CXXConstructExpr::CK_Complete, |
| 4071 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4073 | // If we're supposed to bind temporaries, do so. |
| 4074 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4075 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4076 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4077 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4078 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4079 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4080 | const InitializedEntity &Entity) { |
| 4081 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4082 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4083 | return; |
| 4084 | |
| 4085 | if (Entity.getDecl()->getDeclName()) |
| 4086 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4087 | << Entity.getDecl()->getDeclName(); |
| 4088 | else |
| 4089 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4090 | } |
| 4091 | } |
| 4092 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4093 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4094 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4095 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4096 | } |
| 4097 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4098 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4099 | InitializationSequence::Perform(Sema &S, |
| 4100 | const InitializedEntity &Entity, |
| 4101 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4102 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4103 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4104 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4105 | unsigned NumArgs = Args.size(); |
| 4106 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4107 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4108 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4110 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4111 | // If the declaration is a non-dependent, incomplete array type |
| 4112 | // that has an initializer, then its type will be completed once |
| 4113 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4114 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4115 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4116 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4117 | if (const IncompleteArrayType *ArrayT |
| 4118 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4119 | // FIXME: We don't currently have the ability to accurately |
| 4120 | // compute the length of an initializer list without |
| 4121 | // performing full type-checking of the initializer list |
| 4122 | // (since we have to determine where braces are implicitly |
| 4123 | // introduced and such). So, we fall back to making the array |
| 4124 | // type a dependently-sized array type with no specified |
| 4125 | // bound. |
| 4126 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4127 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4128 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4129 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4130 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4131 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4132 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4133 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4134 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4135 | Brackets = ArrayLoc->getBracketsRange(); |
| 4136 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | *ResultType |
| 4140 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4141 | /*NumElts=*/0, |
| 4142 | ArrayT->getSizeModifier(), |
| 4143 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4144 | Brackets); |
| 4145 | } |
| 4146 | |
| 4147 | } |
| 4148 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4149 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4150 | Kind.isExplicitCast()); |
| 4151 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4154 | // No steps means no initialization. |
| 4155 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4156 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4157 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4158 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4159 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4160 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4161 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4162 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4163 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4164 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4165 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4166 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4168 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4169 | // grab the only argument out the Args and place it into the "current" |
| 4170 | // initializer. |
| 4171 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4172 | case SK_ResolveAddressOfOverloadedFunction: |
| 4173 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4174 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4175 | case SK_CastDerivedToBaseLValue: |
| 4176 | case SK_BindReference: |
| 4177 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4178 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4179 | case SK_UserConversion: |
| 4180 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4181 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4182 | case SK_QualificationConversionRValue: |
| 4183 | case SK_ConversionSequence: |
| 4184 | case SK_ListInitialization: |
| 4185 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4186 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4187 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4188 | case SK_ArrayInit: |
| 4189 | case SK_PassByIndirectCopyRestore: |
| 4190 | case SK_PassByIndirectRestore: |
| 4191 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4192 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4193 | CurInit = Args.get()[0]; |
| 4194 | if (!CurInit.get()) return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4195 | |
| 4196 | // Read from a property when initializing something with it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4197 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4198 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4199 | if (CurInit.isInvalid()) |
| 4200 | return ExprError(); |
| 4201 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4202 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4203 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4205 | case SK_ConstructorInitialization: |
| 4206 | case SK_ZeroInitialization: |
| 4207 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4208 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | |
| 4210 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4211 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4212 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4213 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4214 | Step != StepEnd; ++Step) { |
| 4215 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4216 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4217 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4218 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4219 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4220 | switch (Step->Kind) { |
| 4221 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4222 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4223 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4224 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4225 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4226 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4227 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4228 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4229 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4231 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4232 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4233 | case SK_CastDerivedToBaseLValue: { |
| 4234 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4235 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4236 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4237 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4239 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4240 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4241 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4242 | CurInit.get()->getLocStart(), |
| 4243 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4244 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4245 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4247 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4248 | QualType T = SourceType; |
| 4249 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4250 | T = Pointer->getPointeeType(); |
| 4251 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4252 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4253 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4254 | } |
| 4255 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4256 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4257 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4258 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4259 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4260 | VK_XValue : |
| 4261 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4262 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4263 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4264 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4265 | CurInit.get(), |
| 4266 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4267 | break; |
| 4268 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4270 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4271 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4272 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4273 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4274 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4275 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4276 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4277 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4278 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4279 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4280 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4281 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4282 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4283 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4284 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4285 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4286 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4287 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4288 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4290 | // Reference binding does not have any corresponding ASTs. |
| 4291 | |
| 4292 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4293 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4294 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4296 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4297 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4298 | case SK_BindReferenceToTemporary: |
| 4299 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4300 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4301 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4303 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4304 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4305 | Entity.getType().getNonReferenceType(), |
| 4306 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4307 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4308 | |
| 4309 | // If we're binding to an Objective-C object that has lifetime, we |
| 4310 | // need cleanups. |
| 4311 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4312 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4313 | S.ExprNeedsCleanups = true; |
| 4314 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4315 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4317 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4319 | /*IsExtraneousCopy=*/true); |
| 4320 | break; |
| 4321 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4322 | case SK_UserConversion: { |
| 4323 | // We have a user-defined conversion that invokes either a constructor |
| 4324 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4325 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4326 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4327 | FunctionDecl *Fn = Step->Function.Function; |
| 4328 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4329 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4330 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4331 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4332 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4333 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4334 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4335 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4336 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4337 | // Determine the arguments required to actually perform the constructor |
| 4338 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4339 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4340 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4341 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4342 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4343 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4345 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4347 | move_arg(ConstructorArgs), |
| 4348 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4349 | CXXConstructExpr::CK_Complete, |
| 4350 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4351 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4352 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4353 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4354 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4355 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4356 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4358 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4359 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4360 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4361 | S.IsDerivedFrom(SourceType, Class)) |
| 4362 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4364 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4365 | } else { |
| 4366 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4367 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4368 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4369 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4370 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4371 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
| 4373 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4374 | // derived-to-base conversion? I believe the answer is "no", because |
| 4375 | // 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] | 4376 | ExprResult CurInitExprRes = |
| 4377 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4378 | FoundFn, Conversion); |
| 4379 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4380 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4381 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4383 | // Build the actual call to the conversion function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4384 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4385 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4386 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4387 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4388 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4390 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4393 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4394 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4395 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4396 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4397 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4398 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4400 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4401 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4402 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4403 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4404 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4405 | } |
| 4406 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4408 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4409 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4410 | CurInit.get()->getType(), |
| 4411 | CastKind, CurInit.get(), 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4412 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4414 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4415 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4416 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4418 | break; |
| 4419 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4421 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4422 | case SK_QualificationConversionXValue: |
| 4423 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4424 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4425 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4426 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4427 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4428 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4429 | VK_XValue : |
| 4430 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4431 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4435 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4436 | Sema::CheckedConversionKind CCK |
| 4437 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4438 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4439 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4440 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4441 | ExprResult CurInitExprRes = |
| 4442 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4443 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4444 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4445 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4446 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4447 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4450 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4451 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4452 | QualType Ty = Step->Type; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4453 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4454 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4455 | |
| 4456 | CurInit.release(); |
| 4457 | CurInit = S.Owned(InitList); |
| 4458 | break; |
| 4459 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4460 | |
| 4461 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4462 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4463 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4464 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4466 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4467 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4468 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4469 | ? Kind.getEqualLoc() |
| 4470 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4471 | |
| 4472 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4473 | // Force even a trivial, implicit default constructor to be |
| 4474 | // semantically checked. We do this explicitly because we don't build |
| 4475 | // the definition for completely trivial constructors. |
| 4476 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4477 | assert(ClassDecl && "No parent class for constructor."); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4478 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4479 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4480 | !Constructor->isUsed(false)) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4481 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4482 | } |
| 4483 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4484 | // Determine the arguments required to actually perform the constructor |
| 4485 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4486 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4487 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4488 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4489 | |
| 4490 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4491 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4492 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4493 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4494 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4495 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4496 | unsigned NumExprs = ConstructorArgs.size(); |
| 4497 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4498 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4499 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4500 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4501 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4502 | if (!TSInfo) |
| 4503 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4505 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4506 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4507 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4509 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4510 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4511 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4512 | } else { |
| 4513 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4514 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4515 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4516 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4517 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4518 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4519 | CXXConstructExpr::CK_NonVirtualBase; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4520 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4521 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4522 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4524 | // Only get the parenthesis range if it is a direct construction. |
| 4525 | SourceRange parenRange = |
| 4526 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4527 | Kind.getParenRange() : SourceRange(); |
| 4528 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4529 | // If the entity allows NRVO, mark the construction as elidable |
| 4530 | // unconditionally. |
| 4531 | if (Entity.allowsNRVO()) |
| 4532 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4533 | Constructor, /*Elidable=*/true, |
| 4534 | move_arg(ConstructorArgs), |
| 4535 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4536 | ConstructKind, |
| 4537 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4538 | else |
| 4539 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4540 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4541 | move_arg(ConstructorArgs), |
| 4542 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4543 | ConstructKind, |
| 4544 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4545 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4546 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4547 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4548 | |
| 4549 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4550 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4551 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4552 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4554 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4555 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4556 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4557 | break; |
| 4558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4559 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4560 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4561 | step_iterator NextStep = Step; |
| 4562 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4564 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4565 | // The need for zero-initialization is recorded directly into |
| 4566 | // the call to the object's constructor within the next step. |
| 4567 | ConstructorInitRequiresZeroInit = true; |
| 4568 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4569 | S.getLangOptions().CPlusPlus && |
| 4570 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4571 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4572 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4573 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4574 | Kind.getRange().getBegin()); |
| 4575 | |
| 4576 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4577 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4578 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4579 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4580 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4581 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4582 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4583 | break; |
| 4584 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4585 | |
| 4586 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4587 | QualType SourceType = CurInit.get()->getType(); |
| 4588 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4589 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4590 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4591 | if (Result.isInvalid()) |
| 4592 | return ExprError(); |
| 4593 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4594 | |
| 4595 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4596 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4597 | if (ConvTy != Sema::Compatible && |
| 4598 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4599 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4600 | == Sema::Compatible) |
| 4601 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4602 | if (CurInitExprRes.isInvalid()) |
| 4603 | return ExprError(); |
| 4604 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4605 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4606 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4607 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4608 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4609 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4610 | getAssignmentAction(Entity), |
| 4611 | &Complained)) { |
| 4612 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4613 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4614 | } else if (Complained) |
| 4615 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4616 | break; |
| 4617 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4618 | |
| 4619 | case SK_StringInit: { |
| 4620 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4621 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4622 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4623 | break; |
| 4624 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4625 | |
| 4626 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4627 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4628 | CK_ObjCObjectLValueCast, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4629 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4630 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4631 | |
| 4632 | case SK_ArrayInit: |
| 4633 | // Okay: we checked everything before creating this step. Note that |
| 4634 | // this is a GNU extension. |
| 4635 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4636 | << Step->Type << CurInit.get()->getType() |
| 4637 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4638 | |
| 4639 | // If the destination type is an incomplete array type, update the |
| 4640 | // type accordingly. |
| 4641 | if (ResultType) { |
| 4642 | if (const IncompleteArrayType *IncompleteDest |
| 4643 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4644 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4645 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4646 | *ResultType = S.Context.getConstantArrayType( |
| 4647 | IncompleteDest->getElementType(), |
| 4648 | ConstantSource->getSize(), |
| 4649 | ArrayType::Normal, 0); |
| 4650 | } |
| 4651 | } |
| 4652 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4653 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4654 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4655 | case SK_PassByIndirectCopyRestore: |
| 4656 | case SK_PassByIndirectRestore: |
| 4657 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4658 | CurInit = S.Owned(new (S.Context) |
| 4659 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4660 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4661 | break; |
| 4662 | |
| 4663 | case SK_ProduceObjCObject: |
| 4664 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4665 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4666 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4667 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4668 | } |
| 4669 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4670 | |
| 4671 | // Diagnose non-fatal problems with the completed initialization. |
| 4672 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4673 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4674 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4675 | cast<FieldDecl>(Entity.getDecl()), |
| 4676 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4678 | return move(CurInit); |
| 4679 | } |
| 4680 | |
| 4681 | //===----------------------------------------------------------------------===// |
| 4682 | // Diagnose initialization failures |
| 4683 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4684 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4685 | const InitializedEntity &Entity, |
| 4686 | const InitializationKind &Kind, |
| 4687 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4688 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4689 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4691 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4692 | switch (Failure) { |
| 4693 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4694 | // FIXME: Customize for the initialized entity? |
| 4695 | if (NumArgs == 0) |
| 4696 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4697 | << DestType.getNonReferenceType(); |
| 4698 | else // FIXME: diagnostic below could be better! |
| 4699 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4700 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4701 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4703 | case FK_ArrayNeedsInitList: |
| 4704 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4705 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4706 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4707 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4709 | case FK_ArrayTypeMismatch: |
| 4710 | case FK_NonConstantArrayInit: |
| 4711 | S.Diag(Kind.getLocation(), |
| 4712 | (Failure == FK_ArrayTypeMismatch |
| 4713 | ? diag::err_array_init_different_type |
| 4714 | : diag::err_array_init_non_constant_array)) |
| 4715 | << DestType.getNonReferenceType() |
| 4716 | << Args[0]->getType() |
| 4717 | << Args[0]->getSourceRange(); |
| 4718 | break; |
| 4719 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4720 | case FK_AddressOfOverloadFailed: { |
| 4721 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4723 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4724 | true, |
| 4725 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4726 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4727 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4729 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4730 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4731 | switch (FailedOverloadResult) { |
| 4732 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4733 | if (Failure == FK_UserConversionOverloadFailed) |
| 4734 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4735 | << Args[0]->getType() << DestType |
| 4736 | << Args[0]->getSourceRange(); |
| 4737 | else |
| 4738 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4739 | << DestType << Args[0]->getType() |
| 4740 | << Args[0]->getSourceRange(); |
| 4741 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4742 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4743 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4744 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4745 | case OR_No_Viable_Function: |
| 4746 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4747 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4748 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4749 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4750 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4752 | case OR_Deleted: { |
| 4753 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4754 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4755 | << Args[0]->getSourceRange(); |
| 4756 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4757 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4758 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4759 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4760 | if (Ovl == OR_Deleted) { |
| 4761 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4762 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4764 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | } |
| 4766 | break; |
| 4767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4769 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4770 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4771 | break; |
| 4772 | } |
| 4773 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4775 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4776 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4778 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4779 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4780 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4781 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4782 | << DestType.getNonReferenceType() |
| 4783 | << Args[0]->getType() |
| 4784 | << Args[0]->getSourceRange(); |
| 4785 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4786 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4787 | case FK_RValueReferenceBindingToLValue: |
| 4788 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4789 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4790 | << Args[0]->getSourceRange(); |
| 4791 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4793 | case FK_ReferenceInitDropsQualifiers: |
| 4794 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4795 | << DestType.getNonReferenceType() |
| 4796 | << Args[0]->getType() |
| 4797 | << Args[0]->getSourceRange(); |
| 4798 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4800 | case FK_ReferenceInitFailed: |
| 4801 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4802 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4803 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4804 | << Args[0]->getType() |
| 4805 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4806 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4807 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4808 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4809 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4811 | case FK_ConversionFailed: { |
| 4812 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4813 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4814 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4815 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4816 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4817 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4818 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4819 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4820 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4821 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4822 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4823 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4824 | |
| 4825 | case FK_ConversionFromPropertyFailed: |
| 4826 | // No-op. This error has already been reported. |
| 4827 | break; |
| 4828 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4829 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4830 | SourceRange R; |
| 4831 | |
| 4832 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4833 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4834 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4835 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4836 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4838 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4839 | if (Kind.isCStyleOrFunctionalCast()) |
| 4840 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4841 | << R; |
| 4842 | else |
| 4843 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4844 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4845 | break; |
| 4846 | } |
| 4847 | |
| 4848 | case FK_ReferenceBindingToInitList: |
| 4849 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4850 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4851 | break; |
| 4852 | |
| 4853 | case FK_InitListBadDestinationType: |
| 4854 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4855 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4856 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4858 | case FK_ConstructorOverloadFailed: { |
| 4859 | SourceRange ArgsRange; |
| 4860 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4862 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4864 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4865 | // bad. |
| 4866 | switch (FailedOverloadResult) { |
| 4867 | case OR_Ambiguous: |
| 4868 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4869 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4870 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4871 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4872 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4874 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4875 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4876 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4877 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4878 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4879 | // This is implicit default initialization of a member or |
| 4880 | // base within a constructor. If no viable function was |
| 4881 | // found, notify the user that she needs to explicitly |
| 4882 | // initialize this base/member. |
| 4883 | CXXConstructorDecl *Constructor |
| 4884 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4885 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4886 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4887 | << Constructor->isImplicit() |
| 4888 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4889 | << /*base=*/0 |
| 4890 | << Entity.getType(); |
| 4891 | |
| 4892 | RecordDecl *BaseDecl |
| 4893 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4894 | ->getDecl(); |
| 4895 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4896 | << S.Context.getTagDeclType(BaseDecl); |
| 4897 | } else { |
| 4898 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4899 | << Constructor->isImplicit() |
| 4900 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4901 | << /*member=*/1 |
| 4902 | << Entity.getName(); |
| 4903 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4904 | |
| 4905 | if (const RecordType *Record |
| 4906 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4907 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4908 | diag::note_previous_decl) |
| 4909 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4910 | } |
| 4911 | break; |
| 4912 | } |
| 4913 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4914 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4915 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4916 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4917 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4919 | case OR_Deleted: { |
| 4920 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4921 | << true << DestType << ArgsRange; |
| 4922 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4923 | OverloadingResult Ovl |
| 4924 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4925 | if (Ovl == OR_Deleted) { |
| 4926 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4927 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4928 | } else { |
| 4929 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4930 | } |
| 4931 | break; |
| 4932 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4933 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4934 | case OR_Success: |
| 4935 | llvm_unreachable("Conversion did not fail!"); |
| 4936 | break; |
| 4937 | } |
| 4938 | break; |
| 4939 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4941 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4942 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4943 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4944 | // This is implicit default-initialization of a const member in |
| 4945 | // a constructor. Complain that it needs to be explicitly |
| 4946 | // initialized. |
| 4947 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4948 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4949 | << Constructor->isImplicit() |
| 4950 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4951 | << /*const=*/1 |
| 4952 | << Entity.getName(); |
| 4953 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4954 | << Entity.getName(); |
| 4955 | } else { |
| 4956 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4957 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4958 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4959 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4961 | case FK_Incomplete: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4962 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4963 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4964 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4965 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4966 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4967 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4968 | return true; |
| 4969 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4970 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4971 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4972 | switch (SequenceKind) { |
| 4973 | case FailedSequence: { |
| 4974 | OS << "Failed sequence: "; |
| 4975 | switch (Failure) { |
| 4976 | case FK_TooManyInitsForReference: |
| 4977 | OS << "too many initializers for reference"; |
| 4978 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4979 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4980 | case FK_ArrayNeedsInitList: |
| 4981 | OS << "array requires initializer list"; |
| 4982 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4983 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4984 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4985 | OS << "array requires initializer list or string literal"; |
| 4986 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4988 | case FK_ArrayTypeMismatch: |
| 4989 | OS << "array type mismatch"; |
| 4990 | break; |
| 4991 | |
| 4992 | case FK_NonConstantArrayInit: |
| 4993 | OS << "non-constant array initializer"; |
| 4994 | break; |
| 4995 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4996 | case FK_AddressOfOverloadFailed: |
| 4997 | OS << "address of overloaded function failed"; |
| 4998 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4999 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5000 | case FK_ReferenceInitOverloadFailed: |
| 5001 | OS << "overload resolution for reference initialization failed"; |
| 5002 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5004 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5005 | OS << "non-const lvalue reference bound to temporary"; |
| 5006 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5007 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5008 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5009 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5010 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5012 | case FK_RValueReferenceBindingToLValue: |
| 5013 | OS << "rvalue reference bound to an lvalue"; |
| 5014 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5016 | case FK_ReferenceInitDropsQualifiers: |
| 5017 | OS << "reference initialization drops qualifiers"; |
| 5018 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5020 | case FK_ReferenceInitFailed: |
| 5021 | OS << "reference initialization failed"; |
| 5022 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5024 | case FK_ConversionFailed: |
| 5025 | OS << "conversion failed"; |
| 5026 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5027 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5028 | case FK_ConversionFromPropertyFailed: |
| 5029 | OS << "conversion from property failed"; |
| 5030 | break; |
| 5031 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5032 | case FK_TooManyInitsForScalar: |
| 5033 | OS << "too many initializers for scalar"; |
| 5034 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5036 | case FK_ReferenceBindingToInitList: |
| 5037 | OS << "referencing binding to initializer list"; |
| 5038 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5040 | case FK_InitListBadDestinationType: |
| 5041 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5042 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5043 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5044 | case FK_UserConversionOverloadFailed: |
| 5045 | OS << "overloading failed for user-defined conversion"; |
| 5046 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5047 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5048 | case FK_ConstructorOverloadFailed: |
| 5049 | OS << "constructor overloading failed"; |
| 5050 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5051 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5052 | case FK_DefaultInitOfConst: |
| 5053 | OS << "default initialization of a const variable"; |
| 5054 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5056 | case FK_Incomplete: |
| 5057 | OS << "initialization of incomplete type"; |
| 5058 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5059 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5060 | OS << '\n'; |
| 5061 | return; |
| 5062 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5064 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5065 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5066 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5067 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5068 | case NormalSequence: |
| 5069 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5070 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5071 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5072 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5073 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5074 | if (S != step_begin()) { |
| 5075 | OS << " -> "; |
| 5076 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5077 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5078 | switch (S->Kind) { |
| 5079 | case SK_ResolveAddressOfOverloadedFunction: |
| 5080 | OS << "resolve address of overloaded function"; |
| 5081 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5083 | case SK_CastDerivedToBaseRValue: |
| 5084 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5085 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5087 | case SK_CastDerivedToBaseXValue: |
| 5088 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5089 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5090 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5091 | case SK_CastDerivedToBaseLValue: |
| 5092 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5093 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5095 | case SK_BindReference: |
| 5096 | OS << "bind reference to lvalue"; |
| 5097 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5099 | case SK_BindReferenceToTemporary: |
| 5100 | OS << "bind reference to a temporary"; |
| 5101 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5103 | case SK_ExtraneousCopyToTemporary: |
| 5104 | OS << "extraneous C++03 copy to temporary"; |
| 5105 | break; |
| 5106 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5107 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5108 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5109 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5110 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5111 | case SK_QualificationConversionRValue: |
| 5112 | OS << "qualification conversion (rvalue)"; |
| 5113 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5114 | case SK_QualificationConversionXValue: |
| 5115 | OS << "qualification conversion (xvalue)"; |
| 5116 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5117 | case SK_QualificationConversionLValue: |
| 5118 | OS << "qualification conversion (lvalue)"; |
| 5119 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5121 | case SK_ConversionSequence: |
| 5122 | OS << "implicit conversion sequence ("; |
| 5123 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5124 | OS << ")"; |
| 5125 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5127 | case SK_ListInitialization: |
| 5128 | OS << "list initialization"; |
| 5129 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5131 | case SK_ConstructorInitialization: |
| 5132 | OS << "constructor initialization"; |
| 5133 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5135 | case SK_ZeroInitialization: |
| 5136 | OS << "zero initialization"; |
| 5137 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5139 | case SK_CAssignment: |
| 5140 | OS << "C assignment"; |
| 5141 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5142 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5143 | case SK_StringInit: |
| 5144 | OS << "string initialization"; |
| 5145 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5146 | |
| 5147 | case SK_ObjCObjectConversion: |
| 5148 | OS << "Objective-C object conversion"; |
| 5149 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5150 | |
| 5151 | case SK_ArrayInit: |
| 5152 | OS << "array initialization"; |
| 5153 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5154 | |
| 5155 | case SK_PassByIndirectCopyRestore: |
| 5156 | OS << "pass by indirect copy and restore"; |
| 5157 | break; |
| 5158 | |
| 5159 | case SK_PassByIndirectRestore: |
| 5160 | OS << "pass by indirect restore"; |
| 5161 | break; |
| 5162 | |
| 5163 | case SK_ProduceObjCObject: |
| 5164 | OS << "Objective-C object retension"; |
| 5165 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | void InitializationSequence::dump() const { |
| 5171 | dump(llvm::errs()); |
| 5172 | } |
| 5173 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5174 | static void DiagnoseNarrowingInInitList( |
| 5175 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5176 | bool Constant, const APValue &ConstantValue) { |
| 5177 | if (Constant) { |
| 5178 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5179 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5180 | ? diag::err_init_list_constant_narrowing |
| 5181 | : diag::warn_init_list_constant_narrowing) |
| 5182 | << InitE->getSourceRange() |
| 5183 | << ConstantValue |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5184 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5185 | } else |
| 5186 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5187 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5188 | ? diag::err_init_list_variable_narrowing |
| 5189 | : diag::warn_init_list_variable_narrowing) |
| 5190 | << InitE->getSourceRange() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5191 | << InitE->getType().getLocalUnqualifiedType() |
| 5192 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5193 | |
| 5194 | llvm::SmallString<128> StaticCast; |
| 5195 | llvm::raw_svector_ostream OS(StaticCast); |
| 5196 | OS << "static_cast<"; |
| 5197 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5198 | // It's important to use the typedef's name if there is one so that the |
| 5199 | // fixit doesn't break code using types like int64_t. |
| 5200 | // |
| 5201 | // FIXME: This will break if the typedef requires qualification. But |
| 5202 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5203 | OS << TT->getDecl(); |
| 5204 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5205 | OS << BT->getName(S.getLangOptions()); |
| 5206 | else { |
| 5207 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5208 | // with a broken cast. |
| 5209 | return; |
| 5210 | } |
| 5211 | OS << ">("; |
| 5212 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5213 | << InitE->getSourceRange() |
| 5214 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5215 | << FixItHint::CreateInsertion( |
| 5216 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5217 | } |
| 5218 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5219 | //===----------------------------------------------------------------------===// |
| 5220 | // Initialization helper functions |
| 5221 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5222 | bool |
| 5223 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5224 | ExprResult Init) { |
| 5225 | if (Init.isInvalid()) |
| 5226 | return false; |
| 5227 | |
| 5228 | Expr *InitE = Init.get(); |
| 5229 | assert(InitE && "No initialization expression"); |
| 5230 | |
| 5231 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5232 | SourceLocation()); |
| 5233 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5234 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5237 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5238 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5239 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5240 | ExprResult Init, |
| 5241 | bool TopLevelOfInitList) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5242 | if (Init.isInvalid()) |
| 5243 | return ExprError(); |
| 5244 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5245 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5246 | assert(InitE && "No initialization expression?"); |
| 5247 | |
| 5248 | if (EqualLoc.isInvalid()) |
| 5249 | EqualLoc = InitE->getLocStart(); |
| 5250 | |
| 5251 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5252 | EqualLoc); |
| 5253 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5254 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5255 | |
| 5256 | bool Constant = false; |
| 5257 | APValue Result; |
| 5258 | if (TopLevelOfInitList && |
| 5259 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5260 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5261 | Constant, Result); |
| 5262 | } |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5263 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5264 | } |