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 | // |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +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. |
| 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" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 27 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 28 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Sema Initialization Checking |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 34 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 35 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 36 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 37 | return 0; |
| 38 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 39 | // See if this is a string literal or @encode. |
| 40 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 42 | // Handle @encode, which is a narrow string. |
| 43 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 44 | return Init; |
| 45 | |
| 46 | // Otherwise we can only handle string literals. |
| 47 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 48 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 49 | |
| 50 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 51 | // char array can be initialized with a narrow string. |
| 52 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 53 | if (!SL->isWide()) |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 55 | |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 57 | // correction from DR343): "An array with element type compatible with a |
| 58 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 59 | // string literal, optionally enclosed in braces." |
| 60 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 61 | ElemTy.getUnqualifiedType())) |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 62 | return Init; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 67 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 68 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 69 | if (!arrayType) return 0; |
| 70 | |
| 71 | return IsStringInit(init, arrayType, Context); |
| 72 | } |
| 73 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 74 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 75 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 76 | // Get the length of the string as parsed. |
| 77 | uint64_t StrLength = |
| 78 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 79 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 81 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | // 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] | 83 | // being initialized to a string literal. |
| 84 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 85 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 86 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 87 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 88 | ConstVal, |
| 89 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 90 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 91 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 93 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 95 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 96 | // the size may be smaller or larger than the string we are initializing. |
| 97 | // FIXME: Avoid truncation for 64-bit length strings. |
| 98 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 99 | S.Diag(Str->getSourceRange().getBegin(), |
| 100 | diag::warn_initializer_string_for_char_array_too_long) |
| 101 | << Str->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 103 | // Set the type to the actual size that we are initializing. If we have |
| 104 | // something like: |
| 105 | // char x[1] = "foo"; |
| 106 | // then this will set the string literal's type to char[1]. |
| 107 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
| 111 | // Semantic checking for initializer lists. |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 114 | /// @brief Semantic checking for initializer lists. |
| 115 | /// |
| 116 | /// The InitListChecker class contains a set of routines that each |
| 117 | /// handle the initialization of a certain kind of entity, e.g., |
| 118 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 119 | /// InitListChecker itself performs a recursive walk of the subobject |
| 120 | /// structure of the type to be initialized, while stepping through |
| 121 | /// the initializer list one element at a time. The IList and Index |
| 122 | /// parameters to each of the Check* routines contain the active |
| 123 | /// (syntactic) initializer list and the index into that initializer |
| 124 | /// list that represents the current initializer. Each routine is |
| 125 | /// responsible for moving that Index forward as it consumes elements. |
| 126 | /// |
| 127 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 128 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 129 | /// initializer list and the index into that initializer list where we |
| 130 | /// are copying initializers as we map them over to the semantic |
| 131 | /// list. Once we have completed our recursive walk of the subobject |
| 132 | /// structure, we will have constructed a full semantic initializer |
| 133 | /// list. |
| 134 | /// |
| 135 | /// C99 designators cause changes in the initializer list traversal, |
| 136 | /// because they make the initialization "jump" into a specific |
| 137 | /// subobject and then continue the initialization from that |
| 138 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 139 | /// designated subobject and manages backing out the recursion to |
| 140 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 141 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 142 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 143 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 144 | bool hadError; |
| 145 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 146 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 148 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 149 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 150 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 151 | unsigned &StructuredIndex, |
| 152 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 153 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 154 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 155 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 156 | unsigned &StructuredIndex, |
| 157 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 158 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 159 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 161 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 162 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 163 | unsigned &StructuredIndex, |
| 164 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 165 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 166 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 167 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 168 | InitListExpr *StructuredList, |
| 169 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 170 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 171 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 172 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 173 | InitListExpr *StructuredList, |
| 174 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 175 | void CheckReferenceType(const InitializedEntity &Entity, |
| 176 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 177 | unsigned &Index, |
| 178 | InitListExpr *StructuredList, |
| 179 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 180 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 181 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 182 | InitListExpr *StructuredList, |
| 183 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 184 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 185 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 187 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 188 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 189 | unsigned &StructuredIndex, |
| 190 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 191 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 192 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 194 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 195 | InitListExpr *StructuredList, |
| 196 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 197 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 198 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 199 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 201 | RecordDecl::field_iterator *NextField, |
| 202 | llvm::APSInt *NextElementIndex, |
| 203 | unsigned &Index, |
| 204 | InitListExpr *StructuredList, |
| 205 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 206 | bool FinishSubobjectInit, |
| 207 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 208 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 209 | QualType CurrentObjectType, |
| 210 | InitListExpr *StructuredList, |
| 211 | unsigned StructuredIndex, |
| 212 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 213 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 214 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 215 | Expr *expr); |
| 216 | int numArrayElements(QualType DeclType); |
| 217 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 218 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 219 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 220 | const InitializedEntity &ParentEntity, |
| 221 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 222 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 223 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 224 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 225 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 226 | InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 227 | bool HadError() { return hadError; } |
| 228 | |
| 229 | // @brief Retrieves the fully-structured initializer list used for |
| 230 | // semantic analysis and code generation. |
| 231 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 232 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 233 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 234 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 235 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 236 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 238 | bool &RequiresSecondPass) { |
| 239 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 240 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 241 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 242 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 243 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 244 | // FIXME: We probably don't need to handle references |
| 245 | // specially here, since value-initialization of references is |
| 246 | // handled in InitializationSequence. |
| 247 | if (Field->getType()->isReferenceType()) { |
| 248 | // C++ [dcl.init.aggr]p9: |
| 249 | // If an incomplete or empty initializer-list leaves a |
| 250 | // member of reference type uninitialized, the program is |
| 251 | // ill-formed. |
| 252 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 253 | << Field->getType() |
| 254 | << ILE->getSyntacticForm()->getSourceRange(); |
| 255 | SemaRef.Diag(Field->getLocation(), |
| 256 | diag::note_uninit_reference_member); |
| 257 | hadError = true; |
| 258 | return; |
| 259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 261 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 262 | true); |
| 263 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 264 | if (!InitSeq) { |
| 265 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 266 | hadError = true; |
| 267 | return; |
| 268 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 269 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 270 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 271 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 272 | if (MemberInit.isInvalid()) { |
| 273 | hadError = true; |
| 274 | return; |
| 275 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 277 | if (hadError) { |
| 278 | // Do nothing |
| 279 | } else if (Init < NumInits) { |
| 280 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
| 281 | } else if (InitSeq.getKind() |
| 282 | == InitializationSequence::ConstructorInitialization) { |
| 283 | // Value-initialization requires a constructor call, so |
| 284 | // extend the initializer list to include the constructor |
| 285 | // call and make a note that we'll need to take another pass |
| 286 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 287 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 288 | RequiresSecondPass = true; |
| 289 | } |
| 290 | } else if (InitListExpr *InnerILE |
| 291 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | FillInValueInitializations(MemberEntity, InnerILE, |
| 293 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 296 | /// Recursively replaces NULL values within the given initializer list |
| 297 | /// with expressions that perform value-initialization of the |
| 298 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 299 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 300 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 301 | InitListExpr *ILE, |
| 302 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 304 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 305 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 306 | if (ILE->getSyntacticForm()) |
| 307 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 309 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 310 | if (RType->getDecl()->isUnion() && |
| 311 | ILE->getInitializedFieldInUnion()) |
| 312 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 313 | Entity, ILE, RequiresSecondPass); |
| 314 | else { |
| 315 | unsigned Init = 0; |
| 316 | for (RecordDecl::field_iterator |
| 317 | Field = RType->getDecl()->field_begin(), |
| 318 | FieldEnd = RType->getDecl()->field_end(); |
| 319 | Field != FieldEnd; ++Field) { |
| 320 | if (Field->isUnnamedBitfield()) |
| 321 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 323 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 324 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 325 | |
| 326 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 327 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 328 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 330 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 332 | // Only look at the first initialization of a union. |
| 333 | if (RType->getDecl()->isUnion()) |
| 334 | break; |
| 335 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 340 | |
| 341 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 343 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 344 | unsigned NumInits = ILE->getNumInits(); |
| 345 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 346 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 347 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 348 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 349 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 350 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 351 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 352 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 353 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 354 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 356 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 358 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 361 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 362 | if (hadError) |
| 363 | return; |
| 364 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 365 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 366 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 367 | ElementEntity.setElementIndex(Init); |
| 368 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 369 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 370 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 371 | true); |
| 372 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 373 | if (!InitSeq) { |
| 374 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 375 | hadError = true; |
| 376 | return; |
| 377 | } |
| 378 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 379 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 380 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 381 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 382 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | if (hadError) { |
| 387 | // Do nothing |
| 388 | } else if (Init < NumInits) { |
| 389 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
| 390 | } else if (InitSeq.getKind() |
| 391 | == InitializationSequence::ConstructorInitialization) { |
| 392 | // Value-initialization requires a constructor call, so |
| 393 | // extend the initializer list to include the constructor |
| 394 | // call and make a note that we'll need to take another pass |
| 395 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 396 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 397 | RequiresSecondPass = true; |
| 398 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 399 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 400 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 401 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 406 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 407 | InitListExpr *IL, QualType &T) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 408 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 409 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 410 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 411 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 412 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 414 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 415 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 416 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 417 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 419 | if (!hadError) { |
| 420 | bool RequiresSecondPass = false; |
| 421 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 422 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 423 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 424 | RequiresSecondPass); |
| 425 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 429 | // FIXME: use a proper constant |
| 430 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 431 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 432 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 433 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 434 | } |
| 435 | return maxElements; |
| 436 | } |
| 437 | |
| 438 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 439 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 440 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 442 | Field = structDecl->field_begin(), |
| 443 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 444 | Field != FieldEnd; ++Field) { |
| 445 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 446 | ++InitializableMembers; |
| 447 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 448 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 449 | return std::min(InitializableMembers, 1); |
| 450 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 453 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 454 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 455 | QualType T, unsigned &Index, |
| 456 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 457 | unsigned &StructuredIndex, |
| 458 | bool TopLevelObject) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 459 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 461 | if (T->isArrayType()) |
| 462 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 463 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 464 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 465 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 466 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 467 | else |
| 468 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 469 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 470 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 471 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 472 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 473 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 474 | hadError = true; |
| 475 | return; |
| 476 | } |
| 477 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 478 | // Build a structured initializer list corresponding to this subobject. |
| 479 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 481 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 482 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 483 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 484 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 486 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 487 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 488 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 489 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | StructuredSubobjectInitList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 491 | StructuredSubobjectInitIndex, |
| 492 | TopLevelObject); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 493 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 494 | StructuredSubobjectInitList->setType(T); |
| 495 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 496 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 497 | // range corresponds with the end of the last initializer it used. |
| 498 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 500 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 501 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 | |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 504 | // Warn about missing braces. |
| 505 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 47f164e | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 506 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 507 | diag::warn_missing_braces) |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 508 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 509 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 510 | "{") |
| 511 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 512 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 513 | "}"); |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 514 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 517 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 518 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 519 | unsigned &Index, |
| 520 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 521 | unsigned &StructuredIndex, |
| 522 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 523 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 524 | SyntacticToSemantic[IList] = StructuredList; |
| 525 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 526 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 527 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 528 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 529 | IList->setType(ExprTy); |
| 530 | StructuredList->setType(ExprTy); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 531 | if (hadError) |
| 532 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 533 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 534 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 535 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 536 | if (StructuredIndex == 1 && |
| 537 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 538 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 539 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 540 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 541 | hadError = true; |
| 542 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 543 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 544 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 545 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 546 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 547 | // Don't complain for incomplete types, since we'll get an error |
| 548 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 549 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 551 | CurrentObjectType->isArrayType()? 0 : |
| 552 | CurrentObjectType->isVectorType()? 1 : |
| 553 | CurrentObjectType->isScalarType()? 2 : |
| 554 | CurrentObjectType->isUnionType()? 3 : |
| 555 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 556 | |
| 557 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 558 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 559 | DK = diag::err_excess_initializers; |
| 560 | hadError = true; |
| 561 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 562 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 563 | DK = diag::err_excess_initializers; |
| 564 | hadError = true; |
| 565 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 567 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 568 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 571 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 572 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 573 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 574 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 575 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 576 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 579 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 580 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 582 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 583 | unsigned &Index, |
| 584 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 585 | unsigned &StructuredIndex, |
| 586 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 587 | if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 588 | CheckScalarType(Entity, IList, DeclType, Index, |
| 589 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 590 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 591 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 592 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 593 | } else if (DeclType->isAggregateType()) { |
| 594 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 595 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 596 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 597 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 598 | StructuredList, StructuredIndex, |
| 599 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 600 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 601 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 602 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 603 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 605 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 606 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 607 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 608 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 609 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 610 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 611 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 612 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 613 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 614 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 615 | } else if (DeclType->isRecordType()) { |
| 616 | // C++ [dcl.init]p14: |
| 617 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 618 | // is a brace-enclosed list, see 8.5.1. |
| 619 | // |
| 620 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 621 | // we have an initializer list and a destination type that is not |
| 622 | // an aggregate. |
| 623 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 624 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 625 | << DeclType << IList->getSourceRange(); |
| 626 | hadError = true; |
| 627 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 628 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 629 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 630 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 631 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 632 | << DeclType; |
| 633 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 634 | } else { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 635 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 636 | << DeclType; |
| 637 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 641 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 642 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 644 | unsigned &Index, |
| 645 | InitListExpr *StructuredList, |
| 646 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 647 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 648 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 649 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 650 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 652 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 653 | StructuredList, StructuredIndex, |
| 654 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 655 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 656 | newStructuredList, newStructuredIndex); |
| 657 | ++StructuredIndex; |
| 658 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 659 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 660 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 661 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 662 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 663 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 664 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 665 | StructuredList, StructuredIndex); |
| 666 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 667 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 668 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 669 | // arrayType can be incomplete if we're initializing a flexible |
| 670 | // array member. There's nothing we can do with the completed |
| 671 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 673 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 674 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 675 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 676 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 677 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 678 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 679 | |
| 680 | // Fall through for subaggregate initialization. |
| 681 | |
| 682 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 683 | // C++ [dcl.init.aggr]p12: |
| 684 | // All implicit type conversions (clause 4) are considered when |
| 685 | // initializing the aggregate member with an ini- tializer from |
| 686 | // an initializer-list. If the initializer can initialize a |
| 687 | // member, the member is initialized. [...] |
| 688 | |
| 689 | // FIXME: Better EqualLoc? |
| 690 | InitializationKind Kind = |
| 691 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 692 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 693 | |
| 694 | if (Seq) { |
| 695 | ExprResult Result = |
| 696 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 697 | if (Result.isInvalid()) |
| 698 | hadError = true; |
| 699 | |
| 700 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 701 | Result.takeAs<Expr>()); |
| 702 | ++Index; |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | // Fall through for subaggregate initialization |
| 707 | } else { |
| 708 | // C99 6.7.8p13: |
| 709 | // |
| 710 | // The initializer for a structure or union object that has |
| 711 | // automatic storage duration shall be either an initializer |
| 712 | // list as described below, or a single expression that has |
| 713 | // compatible structure or union type. In the latter case, the |
| 714 | // initial value of the object, including unnamed members, is |
| 715 | // that of the expression. |
| 716 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
| 717 | SemaRef.CheckSingleAssignmentConstraints(ElemType, expr) |
| 718 | == Sema::Compatible) { |
| 719 | SemaRef.DefaultFunctionArrayLvalueConversion(expr); |
| 720 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 721 | ++Index; |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | // Fall through for subaggregate initialization |
| 726 | } |
| 727 | |
| 728 | // C++ [dcl.init.aggr]p12: |
| 729 | // |
| 730 | // [...] Otherwise, if the member is itself a non-empty |
| 731 | // subaggregate, brace elision is assumed and the initializer is |
| 732 | // considered for the initialization of the first member of |
| 733 | // the subaggregate. |
| 734 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 735 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 736 | StructuredIndex); |
| 737 | ++StructuredIndex; |
| 738 | } else { |
| 739 | // We cannot initialize this element, so let |
| 740 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 741 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 742 | SemaRef.Owned(expr)); |
| 743 | hadError = true; |
| 744 | ++Index; |
| 745 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 746 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 749 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 750 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 751 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 752 | InitListExpr *StructuredList, |
| 753 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 754 | if (Index >= IList->getNumInits()) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 755 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 756 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 757 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 758 | ++Index; |
| 759 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 760 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 761 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 762 | |
| 763 | Expr *expr = IList->getInit(Index); |
| 764 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 765 | SemaRef.Diag(SubIList->getLocStart(), |
| 766 | diag::warn_many_braces_around_scalar_init) |
| 767 | << SubIList->getSourceRange(); |
| 768 | |
| 769 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 770 | StructuredIndex); |
| 771 | return; |
| 772 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 773 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 774 | diag::err_designator_for_scalar_init) |
| 775 | << DeclType << expr->getSourceRange(); |
| 776 | hadError = true; |
| 777 | ++Index; |
| 778 | ++StructuredIndex; |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | ExprResult Result = |
| 783 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 784 | SemaRef.Owned(expr)); |
| 785 | |
| 786 | Expr *ResultExpr = 0; |
| 787 | |
| 788 | if (Result.isInvalid()) |
| 789 | hadError = true; // types weren't compatible. |
| 790 | else { |
| 791 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 792 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 793 | if (ResultExpr != expr) { |
| 794 | // The type was promoted, update initializer list. |
| 795 | IList->setInit(Index, ResultExpr); |
| 796 | } |
| 797 | } |
| 798 | if (hadError) |
| 799 | ++StructuredIndex; |
| 800 | else |
| 801 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 802 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 805 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 806 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 807 | unsigned &Index, |
| 808 | InitListExpr *StructuredList, |
| 809 | unsigned &StructuredIndex) { |
| 810 | if (Index < IList->getNumInits()) { |
| 811 | Expr *expr = IList->getInit(Index); |
| 812 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 813 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 814 | << DeclType << IList->getSourceRange(); |
| 815 | hadError = true; |
| 816 | ++Index; |
| 817 | ++StructuredIndex; |
| 818 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 820 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 821 | ExprResult Result = |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 822 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 823 | SemaRef.Owned(expr)); |
| 824 | |
| 825 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 826 | hadError = true; |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 827 | |
| 828 | expr = Result.takeAs<Expr>(); |
| 829 | IList->setInit(Index, expr); |
| 830 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 831 | if (hadError) |
| 832 | ++StructuredIndex; |
| 833 | else |
| 834 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 835 | ++Index; |
| 836 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 837 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 838 | // general, it would be useful to pass location information down the stack, |
| 839 | // so that we know the location (or decl) of the "current object" being |
| 840 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 842 | diag::err_init_reference_member_uninitialized) |
| 843 | << DeclType |
| 844 | << IList->getSourceRange(); |
| 845 | hadError = true; |
| 846 | ++Index; |
| 847 | ++StructuredIndex; |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 852 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 853 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 854 | unsigned &Index, |
| 855 | InitListExpr *StructuredList, |
| 856 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 857 | if (Index >= IList->getNumInits()) |
| 858 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 860 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 861 | unsigned maxElements = VT->getNumElements(); |
| 862 | unsigned numEltsInit = 0; |
| 863 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 864 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 865 | if (!SemaRef.getLangOptions().OpenCL) { |
| 866 | // If the initializing element is a vector, try to copy-initialize |
| 867 | // instead of breaking it apart (which is doomed to failure anyway). |
| 868 | Expr *Init = IList->getInit(Index); |
| 869 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 870 | ExprResult Result = |
| 871 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
| 872 | SemaRef.Owned(Init)); |
| 873 | |
| 874 | Expr *ResultExpr = 0; |
| 875 | if (Result.isInvalid()) |
| 876 | hadError = true; // types weren't compatible. |
| 877 | else { |
| 878 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 880 | if (ResultExpr != Init) { |
| 881 | // The type was promoted, update initializer list. |
| 882 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 885 | if (hadError) |
| 886 | ++StructuredIndex; |
| 887 | else |
| 888 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 889 | ++Index; |
| 890 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 893 | InitializedEntity ElementEntity = |
| 894 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 896 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 897 | // Don't attempt to go past the end of the init list |
| 898 | if (Index >= IList->getNumInits()) |
| 899 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 900 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 901 | ElementEntity.setElementIndex(Index); |
| 902 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 903 | StructuredList, StructuredIndex); |
| 904 | } |
| 905 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 906 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 907 | |
| 908 | InitializedEntity ElementEntity = |
| 909 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 910 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 911 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 912 | for (unsigned i = 0; i < maxElements; ++i) { |
| 913 | // Don't attempt to go past the end of the init list |
| 914 | if (Index >= IList->getNumInits()) |
| 915 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 917 | ElementEntity.setElementIndex(Index); |
| 918 | |
| 919 | QualType IType = IList->getInit(Index)->getType(); |
| 920 | if (!IType->isVectorType()) { |
| 921 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 922 | StructuredList, StructuredIndex); |
| 923 | ++numEltsInit; |
| 924 | } else { |
| 925 | QualType VecType; |
| 926 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 927 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 928 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 929 | if (IType->isExtVectorType()) |
| 930 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 931 | else |
| 932 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 933 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 934 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 935 | StructuredList, StructuredIndex); |
| 936 | numEltsInit += numIElts; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | // OpenCL requires all elements to be initialized. |
| 941 | if (numEltsInit != maxElements) |
| 942 | if (SemaRef.getLangOptions().OpenCL) |
| 943 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 944 | diag::err_vector_incorrect_num_initializers) |
| 945 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 948 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 949 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 950 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 952 | unsigned &Index, |
| 953 | InitListExpr *StructuredList, |
| 954 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 955 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 956 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 957 | // Check for the special-case of initializing an array with a string. |
| 958 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 959 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 960 | SemaRef.Context)) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 961 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 962 | // We place the string literal directly into the resulting |
| 963 | // initializer list. This is the only place where the structure |
| 964 | // of the structured initializer list doesn't match exactly, |
| 965 | // because doing so would involve allocating one character |
| 966 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 967 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 968 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 969 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 973 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 974 | // Check for VLAs; in standard C it would be possible to check this |
| 975 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 976 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 977 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 978 | diag::err_variable_object_no_init) |
| 979 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 980 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 981 | ++Index; |
| 982 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 986 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 987 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 988 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 989 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 990 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 991 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 992 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 993 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 994 | maxElementsKnown = true; |
| 995 | } |
| 996 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 997 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 998 | while (Index < IList->getNumInits()) { |
| 999 | Expr *Init = IList->getInit(Index); |
| 1000 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1001 | // If we're not the subobject that matches up with the '{' for |
| 1002 | // the designator, we shouldn't be handling the |
| 1003 | // designator. Return immediately. |
| 1004 | if (!SubobjectIsDesignatorContext) |
| 1005 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1007 | // Handle this designated initializer. elementIndex will be |
| 1008 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1009 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1010 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1011 | StructuredList, StructuredIndex, true, |
| 1012 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1013 | hadError = true; |
| 1014 | continue; |
| 1015 | } |
| 1016 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1017 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1018 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1019 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1020 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1021 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1023 | // If the array is of incomplete type, keep track of the number of |
| 1024 | // elements in the initializer. |
| 1025 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1026 | maxElements = elementIndex; |
| 1027 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | // If we know the maximum number of elements, and we've already |
| 1032 | // hit it, stop consuming elements in the initializer list. |
| 1033 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1034 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1035 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1036 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1038 | Entity); |
| 1039 | // Check this element. |
| 1040 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1041 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1042 | ++elementIndex; |
| 1043 | |
| 1044 | // If the array is of incomplete type, keep track of the number of |
| 1045 | // elements in the initializer. |
| 1046 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1047 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1048 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1049 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1050 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1051 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1052 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1053 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1054 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1055 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1056 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1057 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1058 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1059 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1061 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1065 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1066 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1068 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1070 | unsigned &Index, |
| 1071 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1072 | unsigned &StructuredIndex, |
| 1073 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1074 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1076 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1077 | // confusion, we forgo checking the intializer for the entire record. |
| 1078 | if (structDecl->isInvalidDecl()) { |
| 1079 | hadError = true; |
| 1080 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1084 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1085 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1086 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1087 | Field != FieldEnd; ++Field) { |
| 1088 | if (Field->getDeclName()) { |
| 1089 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | return; |
| 1094 | } |
| 1095 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1096 | // If structDecl is a forward declaration, this loop won't do |
| 1097 | // anything except look at designated initializers; That's okay, |
| 1098 | // because an error should get printed out elsewhere. It might be |
| 1099 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1100 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1101 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1102 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1103 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1104 | while (Index < IList->getNumInits()) { |
| 1105 | Expr *Init = IList->getInit(Index); |
| 1106 | |
| 1107 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1108 | // If we're not the subobject that matches up with the '{' for |
| 1109 | // the designator, we shouldn't be handling the |
| 1110 | // designator. Return immediately. |
| 1111 | if (!SubobjectIsDesignatorContext) |
| 1112 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1114 | // Handle this designated initializer. Field will be updated to |
| 1115 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1116 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1117 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1118 | StructuredList, StructuredIndex, |
| 1119 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1120 | hadError = true; |
| 1121 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1122 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Disable check for missing fields when designators are used. |
| 1125 | // This matches gcc behaviour. |
| 1126 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1127 | continue; |
| 1128 | } |
| 1129 | |
| 1130 | if (Field == FieldEnd) { |
| 1131 | // We've run out of fields. We're done. |
| 1132 | break; |
| 1133 | } |
| 1134 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1135 | // We've already initialized a member of a union. We're done. |
| 1136 | if (InitializedSomething && DeclType->isUnionType()) |
| 1137 | break; |
| 1138 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1139 | // If we've hit the flexible array member at the end, we're done. |
| 1140 | if (Field->getType()->isIncompleteArrayType()) |
| 1141 | break; |
| 1142 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1143 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1144 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1145 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1146 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1147 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1148 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1149 | InitializedEntity MemberEntity = |
| 1150 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1151 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1152 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1153 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (DeclType->isUnionType()) { |
| 1156 | // Initialize the first field within the union. |
| 1157 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1158 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1159 | |
| 1160 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1161 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1162 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1163 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1164 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1165 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1166 | // It is possible we have one or more unnamed bitfields remaining. |
| 1167 | // Find first (if any) named field and emit warning. |
| 1168 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1169 | it != end; ++it) { |
| 1170 | if (!it->isUnnamedBitfield()) { |
| 1171 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1172 | diag::warn_missing_field_initializers) << it->getName(); |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1179 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | |
| 1182 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | if (!TopLevelObject && |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1184 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1185 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1187 | diag::err_flexible_array_init_nonempty) |
| 1188 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1189 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1190 | << *Field; |
| 1191 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1192 | ++Index; |
| 1193 | return; |
| 1194 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1196 | diag::ext_flexible_array_init) |
| 1197 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1198 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1199 | << *Field; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1202 | InitializedEntity MemberEntity = |
| 1203 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1204 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1205 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1206 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1207 | StructuredList, StructuredIndex); |
| 1208 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1210 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1211 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1213 | /// \brief Expand a field designator that refers to a member of an |
| 1214 | /// anonymous struct or union into a series of field designators that |
| 1215 | /// refers to the field within the appropriate subobject. |
| 1216 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1217 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | DesignatedInitExpr *DIE, |
| 1219 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1220 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1221 | typedef DesignatedInitExpr::Designator Designator; |
| 1222 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1223 | // Build the replacement designators. |
| 1224 | llvm::SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1225 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1226 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1227 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1229 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1230 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1231 | else |
| 1232 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1233 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1234 | assert(isa<FieldDecl>(*PI)); |
| 1235 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | // Expand the current designator into the set of replacement |
| 1239 | // designators, so we have a full subobject path down to where the |
| 1240 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1241 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1242 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1243 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1245 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1246 | /// corresponds to FieldName. |
| 1247 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1248 | IdentifierInfo *FieldName) { |
| 1249 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1250 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1251 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1252 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1253 | return IF; |
| 1254 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1255 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1256 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1259 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1260 | /// |
| 1261 | /// Determines whether the designated initializer @p DIE, which |
| 1262 | /// resides at the given @p Index within the initializer list @p |
| 1263 | /// IList, is well-formed for a current object of type @p DeclType |
| 1264 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1266 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1267 | /// |
| 1268 | /// @param IList The initializer list in which this designated |
| 1269 | /// initializer occurs. |
| 1270 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1271 | /// @param DIE The designated initializer expression. |
| 1272 | /// |
| 1273 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1274 | /// |
| 1275 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1276 | /// into which the designation in @p DIE should refer. |
| 1277 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1278 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1279 | /// a field, this will be set to the field declaration corresponding |
| 1280 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1281 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1282 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1283 | /// DIE is an array designator or GNU array-range designator, this |
| 1284 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1285 | /// |
| 1286 | /// @param Index Index into @p IList where the designated initializer |
| 1287 | /// @p DIE occurs. |
| 1288 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1289 | /// @param StructuredList The initializer list expression that |
| 1290 | /// describes all of the subobject initializers in the order they'll |
| 1291 | /// actually be initialized. |
| 1292 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1293 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1295 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1296 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1298 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1299 | QualType &CurrentObjectType, |
| 1300 | RecordDecl::field_iterator *NextField, |
| 1301 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1302 | unsigned &Index, |
| 1303 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1304 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1305 | bool FinishSubobjectInit, |
| 1306 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1307 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1308 | // Check the actual initialization for the designated object type. |
| 1309 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Temporarily remove the designator expression from the |
| 1312 | // initializer list that the child calls see, so that we don't try |
| 1313 | // to re-process the designator. |
| 1314 | unsigned OldIndex = Index; |
| 1315 | IList->setInit(OldIndex, DIE->getInit()); |
| 1316 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1317 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1318 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Restore the designated initializer expression in the syntactic |
| 1321 | // form of the initializer list. |
| 1322 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1323 | DIE->setInit(IList->getInit(OldIndex)); |
| 1324 | IList->setInit(OldIndex, DIE); |
| 1325 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1326 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1329 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1331 | "Need a non-designated initializer list to start from"); |
| 1332 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1333 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1334 | // Determine the structural initializer list that corresponds to the |
| 1335 | // current subobject. |
| 1336 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1338 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1339 | SourceRange(D->getStartLocation(), |
| 1340 | DIE->getSourceRange().getEnd())); |
| 1341 | assert(StructuredList && "Expected a structured initializer list"); |
| 1342 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1343 | if (D->isFieldDesignator()) { |
| 1344 | // C99 6.7.8p7: |
| 1345 | // |
| 1346 | // If a designator has the form |
| 1347 | // |
| 1348 | // . identifier |
| 1349 | // |
| 1350 | // then the current object (defined below) shall have |
| 1351 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1353 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1354 | if (!RT) { |
| 1355 | SourceLocation Loc = D->getDotLoc(); |
| 1356 | if (Loc.isInvalid()) |
| 1357 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1358 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1359 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1360 | ++Index; |
| 1361 | return true; |
| 1362 | } |
| 1363 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1364 | // Note: we perform a linear search of the fields here, despite |
| 1365 | // the fact that we have a faster lookup method, because we always |
| 1366 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1367 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1368 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1369 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1371 | Field = RT->getDecl()->field_begin(), |
| 1372 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1373 | for (; Field != FieldEnd; ++Field) { |
| 1374 | if (Field->isUnnamedBitfield()) |
| 1375 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1376 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1377 | // If we find a field representing an anonymous field, look in the |
| 1378 | // IndirectFieldDecl that follow for the designated initializer. |
| 1379 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1380 | if (IndirectFieldDecl *IF = |
| 1381 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1382 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1383 | D = DIE->getDesignator(DesigIdx); |
| 1384 | break; |
| 1385 | } |
| 1386 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1387 | if (KnownField && KnownField == *Field) |
| 1388 | break; |
| 1389 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1395 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1396 | // There was no normal field in the struct with the designated |
| 1397 | // name. Perform another lookup for this name, which may find |
| 1398 | // something that we can't designate (e.g., a member function), |
| 1399 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1401 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1402 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1403 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1404 | // Name lookup didn't find anything. Determine whether this |
| 1405 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1406 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1407 | Sema::LookupMemberName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1408 | if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1409 | Sema::CTC_NoKeywords) && |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1410 | (ReplacementField = R.getAsSingle<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1411 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1412 | ->Equals(RT->getDecl())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1413 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1414 | diag::err_field_designator_unknown_suggest) |
| 1415 | << FieldName << CurrentObjectType << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1416 | << FixItHint::CreateReplacement(D->getFieldLoc(), |
| 1417 | R.getLookupName().getAsString()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1418 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1419 | diag::note_previous_decl) |
| 1420 | << ReplacementField->getDeclName(); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1421 | } else { |
| 1422 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1423 | << FieldName << CurrentObjectType; |
| 1424 | ++Index; |
| 1425 | return true; |
| 1426 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1427 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1429 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1430 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1431 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1432 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1434 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1435 | ++Index; |
| 1436 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1437 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1438 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1439 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1440 | // The replacement field comes from typo correction; find it |
| 1441 | // in the list of fields. |
| 1442 | FieldIndex = 0; |
| 1443 | Field = RT->getDecl()->field_begin(); |
| 1444 | for (; Field != FieldEnd; ++Field) { |
| 1445 | if (Field->isUnnamedBitfield()) |
| 1446 | continue; |
| 1447 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1448 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1449 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1450 | break; |
| 1451 | |
| 1452 | ++FieldIndex; |
| 1453 | } |
| 1454 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1455 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1456 | |
| 1457 | // All of the fields of a union are located at the same place in |
| 1458 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1459 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1460 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1461 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1462 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1464 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1465 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1467 | // Make sure that our non-designated initializer list has space |
| 1468 | // for a subobject corresponding to this field. |
| 1469 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1470 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1472 | // This designator names a flexible array member. |
| 1473 | if (Field->getType()->isIncompleteArrayType()) { |
| 1474 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1475 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1476 | // We can't designate an object within the flexible array |
| 1477 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1479 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1481 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1483 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1484 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1485 | << *Field; |
| 1486 | Invalid = true; |
| 1487 | } |
| 1488 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1489 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1490 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1491 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1492 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1493 | diag::err_flexible_array_init_needs_braces) |
| 1494 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1495 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1496 | << *Field; |
| 1497 | Invalid = true; |
| 1498 | } |
| 1499 | |
| 1500 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1502 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1504 | diag::err_flexible_array_init_nonempty) |
| 1505 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1506 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1507 | << *Field; |
| 1508 | Invalid = true; |
| 1509 | } |
| 1510 | |
| 1511 | if (Invalid) { |
| 1512 | ++Index; |
| 1513 | return true; |
| 1514 | } |
| 1515 | |
| 1516 | // Initialize the array. |
| 1517 | bool prevHadError = hadError; |
| 1518 | unsigned newStructuredIndex = FieldIndex; |
| 1519 | unsigned OldIndex = Index; |
| 1520 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1521 | |
| 1522 | InitializedEntity MemberEntity = |
| 1523 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1524 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1525 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1526 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1527 | IList->setInit(OldIndex, DIE); |
| 1528 | if (hadError && !prevHadError) { |
| 1529 | ++Field; |
| 1530 | ++FieldIndex; |
| 1531 | if (NextField) |
| 1532 | *NextField = Field; |
| 1533 | StructuredIndex = FieldIndex; |
| 1534 | return true; |
| 1535 | } |
| 1536 | } else { |
| 1537 | // Recurse to check later designated subobjects. |
| 1538 | QualType FieldType = (*Field)->getType(); |
| 1539 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1541 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1542 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1543 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1544 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1545 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1546 | true, false)) |
| 1547 | return true; |
| 1548 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Find the position of the next field to be initialized in this |
| 1551 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1552 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1553 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1554 | |
| 1555 | // If this the first designator, our caller will continue checking |
| 1556 | // the rest of this struct/class/union subobject. |
| 1557 | if (IsFirstDesignator) { |
| 1558 | if (NextField) |
| 1559 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1560 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1561 | return false; |
| 1562 | } |
| 1563 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1564 | if (!FinishSubobjectInit) |
| 1565 | return false; |
| 1566 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1567 | // We've already initialized something in the union; we're done. |
| 1568 | if (RT->getDecl()->isUnion()) |
| 1569 | return hadError; |
| 1570 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1571 | // Check the remaining fields within this class/struct/union subobject. |
| 1572 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1573 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1574 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1575 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1576 | return hadError && !prevHadError; |
| 1577 | } |
| 1578 | |
| 1579 | // C99 6.7.8p6: |
| 1580 | // |
| 1581 | // If a designator has the form |
| 1582 | // |
| 1583 | // [ constant-expression ] |
| 1584 | // |
| 1585 | // then the current object (defined below) shall have array |
| 1586 | // type and the expression shall be an integer constant |
| 1587 | // expression. If the array is of unknown size, any |
| 1588 | // nonnegative value is valid. |
| 1589 | // |
| 1590 | // Additionally, cope with the GNU extension that permits |
| 1591 | // designators of the form |
| 1592 | // |
| 1593 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1594 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1595 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1596 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1597 | << CurrentObjectType; |
| 1598 | ++Index; |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
| 1602 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1603 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1604 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1605 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1606 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1607 | DesignatedEndIndex = DesignatedStartIndex; |
| 1608 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1609 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1612 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1614 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1615 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1617 | // Codegen can't handle evaluating array range designators that have side |
| 1618 | // effects, because we replicate the AST value for each initialized element. |
| 1619 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1620 | // elements with something that has a side effect, so codegen can emit an |
| 1621 | // "error unsupported" error instead of miscompiling the app. |
| 1622 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1623 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1624 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1627 | if (isa<ConstantArrayType>(AT)) { |
| 1628 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1629 | DesignatedStartIndex |
| 1630 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1631 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1632 | DesignatedEndIndex |
| 1633 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1634 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1635 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1636 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1637 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1638 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1639 | << IndexExpr->getSourceRange(); |
| 1640 | ++Index; |
| 1641 | return true; |
| 1642 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1643 | } else { |
| 1644 | // Make sure the bit-widths and signedness match. |
| 1645 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1646 | DesignatedEndIndex |
| 1647 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1648 | else if (DesignatedStartIndex.getBitWidth() < |
| 1649 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1650 | DesignatedStartIndex |
| 1651 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1652 | DesignatedStartIndex.setIsUnsigned(true); |
| 1653 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1656 | // Make sure that our non-designated initializer list has space |
| 1657 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1658 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1660 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1661 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1662 | // Repeatedly perform subobject initializations in the range |
| 1663 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1664 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1665 | // Move to the next designator |
| 1666 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1667 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1668 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1669 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1670 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1672 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1673 | // Recurse to check later designated subobjects. |
| 1674 | QualType ElementType = AT->getElementType(); |
| 1675 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1676 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1677 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1678 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1679 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1680 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1681 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1682 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1683 | return true; |
| 1684 | |
| 1685 | // Move to the next index in the array that we'll be initializing. |
| 1686 | ++DesignatedStartIndex; |
| 1687 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1688 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1689 | |
| 1690 | // If this the first designator, our caller will continue checking |
| 1691 | // the rest of this array subobject. |
| 1692 | if (IsFirstDesignator) { |
| 1693 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1694 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1695 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1696 | return false; |
| 1697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1699 | if (!FinishSubobjectInit) |
| 1700 | return false; |
| 1701 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1702 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1703 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1704 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1705 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1706 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1710 | // Get the structured initializer list for a subobject of type |
| 1711 | // @p CurrentObjectType. |
| 1712 | InitListExpr * |
| 1713 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1714 | QualType CurrentObjectType, |
| 1715 | InitListExpr *StructuredList, |
| 1716 | unsigned StructuredIndex, |
| 1717 | SourceRange InitRange) { |
| 1718 | Expr *ExistingInit = 0; |
| 1719 | if (!StructuredList) |
| 1720 | ExistingInit = SyntacticToSemantic[IList]; |
| 1721 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1722 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1724 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1725 | return Result; |
| 1726 | |
| 1727 | if (ExistingInit) { |
| 1728 | // We are creating an initializer list that initializes the |
| 1729 | // subobjects of the current object, but there was already an |
| 1730 | // initialization that completely initialized the current |
| 1731 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1733 | // struct X { int a, b; }; |
| 1734 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1736 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1737 | // designated initializer re-initializes the whole |
| 1738 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1740 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1741 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1743 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1744 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1745 | << ExistingInit->getSourceRange(); |
| 1746 | } |
| 1747 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1749 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1750 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1751 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1753 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1755 | // Pre-allocate storage for the structured initializer list. |
| 1756 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1757 | unsigned NumInits = 0; |
| 1758 | if (!StructuredList) |
| 1759 | NumInits = IList->getNumInits(); |
| 1760 | else if (Index < IList->getNumInits()) { |
| 1761 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1762 | NumInits = SubList->getNumInits(); |
| 1763 | } |
| 1764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1766 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1767 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1768 | NumElements = CAType->getSize().getZExtValue(); |
| 1769 | // Simple heuristic so that we don't allocate a very large |
| 1770 | // initializer with many empty entries at the end. |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1771 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1772 | NumElements = 0; |
| 1773 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1774 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1775 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1776 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1777 | RecordDecl *RDecl = RType->getDecl(); |
| 1778 | if (RDecl->isUnion()) |
| 1779 | NumElements = 1; |
| 1780 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1782 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1785 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1786 | NumElements = IList->getNumInits(); |
| 1787 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1788 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1790 | // Link this new initializer list into the structured initializer |
| 1791 | // lists. |
| 1792 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1793 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | else { |
| 1795 | Result->setSyntacticForm(IList); |
| 1796 | SyntacticToSemantic[IList] = Result; |
| 1797 | } |
| 1798 | |
| 1799 | return Result; |
| 1800 | } |
| 1801 | |
| 1802 | /// Update the initializer at index @p StructuredIndex within the |
| 1803 | /// structured initializer list to the value @p expr. |
| 1804 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1805 | unsigned &StructuredIndex, |
| 1806 | Expr *expr) { |
| 1807 | // No structured initializer list to update |
| 1808 | if (!StructuredList) |
| 1809 | return; |
| 1810 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1811 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1812 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1813 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1815 | diag::warn_initializer_overrides) |
| 1816 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1818 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1819 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1820 | << PrevInit->getSourceRange(); |
| 1821 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1823 | ++StructuredIndex; |
| 1824 | } |
| 1825 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1826 | /// Check that the given Index expression is a valid array designator |
| 1827 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1828 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1829 | /// and produces a reasonable diagnostic if there is a |
| 1830 | /// failure. Returns true if there was an error, false otherwise. If |
| 1831 | /// everything went okay, Value will receive the value of the constant |
| 1832 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1834 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1835 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1836 | |
| 1837 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1838 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1839 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1840 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1841 | if (Value.isSigned() && Value.isNegative()) |
| 1842 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1843 | << Value.toString(10) << Index->getSourceRange(); |
| 1844 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1845 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1846 | return false; |
| 1847 | } |
| 1848 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1849 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1850 | SourceLocation Loc, |
| 1851 | bool GNUSyntax, |
| 1852 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1853 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1854 | |
| 1855 | bool Invalid = false; |
| 1856 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1857 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1858 | |
| 1859 | // Build designators and check array designator expressions. |
| 1860 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1861 | const Designator &D = Desig.getDesignator(Idx); |
| 1862 | switch (D.getKind()) { |
| 1863 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1865 | D.getFieldLoc())); |
| 1866 | break; |
| 1867 | |
| 1868 | case Designator::ArrayDesignator: { |
| 1869 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1870 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1871 | if (!Index->isTypeDependent() && |
| 1872 | !Index->isValueDependent() && |
| 1873 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1874 | Invalid = true; |
| 1875 | else { |
| 1876 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1878 | D.getRBracketLoc())); |
| 1879 | InitExpressions.push_back(Index); |
| 1880 | } |
| 1881 | break; |
| 1882 | } |
| 1883 | |
| 1884 | case Designator::ArrayRangeDesignator: { |
| 1885 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1886 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1887 | llvm::APSInt StartValue; |
| 1888 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1889 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1890 | StartIndex->isValueDependent(); |
| 1891 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1892 | EndIndex->isValueDependent(); |
| 1893 | if ((!StartDependent && |
| 1894 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1895 | (!EndDependent && |
| 1896 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1897 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1898 | else { |
| 1899 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1900 | if (StartDependent || EndDependent) { |
| 1901 | // Nothing to compute. |
| 1902 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1903 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1904 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1905 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1906 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1907 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1908 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1910 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1911 | Invalid = true; |
| 1912 | } else { |
| 1913 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1915 | D.getEllipsisLoc(), |
| 1916 | D.getRBracketLoc())); |
| 1917 | InitExpressions.push_back(StartIndex); |
| 1918 | InitExpressions.push_back(EndIndex); |
| 1919 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1920 | } |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | if (Invalid || Init.isInvalid()) |
| 1927 | return ExprError(); |
| 1928 | |
| 1929 | // Clear out the expressions within the designation. |
| 1930 | Desig.ClearExprs(*this); |
| 1931 | |
| 1932 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1933 | = DesignatedInitExpr::Create(Context, |
| 1934 | Designators.data(), Designators.size(), |
| 1935 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1936 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 1938 | if (getLangOptions().CPlusPlus) |
| 1939 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 1940 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1942 | return Owned(DIE); |
| 1943 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1945 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 1946 | InitListExpr *&InitList, QualType &DeclType) { |
| 1947 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1948 | if (!CheckInitList.HadError()) |
| 1949 | InitList = CheckInitList.getFullyStructuredList(); |
| 1950 | |
| 1951 | return CheckInitList.HadError(); |
| 1952 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1954 | //===----------------------------------------------------------------------===// |
| 1955 | // Initialization entity |
| 1956 | //===----------------------------------------------------------------------===// |
| 1957 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1958 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1959 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1960 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1961 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1962 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 1963 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1964 | Type = AT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1965 | } else { |
| 1966 | Kind = EK_VectorElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1967 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1968 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1971 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1972 | CXXBaseSpecifier *Base, |
| 1973 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1974 | { |
| 1975 | InitializedEntity Result; |
| 1976 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1977 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 1978 | if (IsInheritedVirtualBase) |
| 1979 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1981 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1982 | return Result; |
| 1983 | } |
| 1984 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1985 | DeclarationName InitializedEntity::getName() const { |
| 1986 | switch (getKind()) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1987 | case EK_Parameter: |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 1988 | if (!VariableOrMember) |
| 1989 | return DeclarationName(); |
| 1990 | // Fall through |
| 1991 | |
| 1992 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1993 | case EK_Member: |
| 1994 | return VariableOrMember->getDeclName(); |
| 1995 | |
| 1996 | case EK_Result: |
| 1997 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1998 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1999 | case EK_Temporary: |
| 2000 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2001 | case EK_ArrayElement: |
| 2002 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2003 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2004 | return DeclarationName(); |
| 2005 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2007 | // Silence GCC warning |
| 2008 | return DeclarationName(); |
| 2009 | } |
| 2010 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2011 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2012 | switch (getKind()) { |
| 2013 | case EK_Variable: |
| 2014 | case EK_Parameter: |
| 2015 | case EK_Member: |
| 2016 | return VariableOrMember; |
| 2017 | |
| 2018 | case EK_Result: |
| 2019 | case EK_Exception: |
| 2020 | case EK_New: |
| 2021 | case EK_Temporary: |
| 2022 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2023 | case EK_ArrayElement: |
| 2024 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2025 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2026 | return 0; |
| 2027 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2028 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2029 | // Silence GCC warning |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2033 | bool InitializedEntity::allowsNRVO() const { |
| 2034 | switch (getKind()) { |
| 2035 | case EK_Result: |
| 2036 | case EK_Exception: |
| 2037 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2039 | case EK_Variable: |
| 2040 | case EK_Parameter: |
| 2041 | case EK_Member: |
| 2042 | case EK_New: |
| 2043 | case EK_Temporary: |
| 2044 | case EK_Base: |
| 2045 | case EK_ArrayElement: |
| 2046 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2047 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2048 | break; |
| 2049 | } |
| 2050 | |
| 2051 | return false; |
| 2052 | } |
| 2053 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2054 | //===----------------------------------------------------------------------===// |
| 2055 | // Initialization sequence |
| 2056 | //===----------------------------------------------------------------------===// |
| 2057 | |
| 2058 | void InitializationSequence::Step::Destroy() { |
| 2059 | switch (Kind) { |
| 2060 | case SK_ResolveAddressOfOverloadedFunction: |
| 2061 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2062 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2063 | case SK_CastDerivedToBaseLValue: |
| 2064 | case SK_BindReference: |
| 2065 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2066 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2067 | case SK_UserConversion: |
| 2068 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2069 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2070 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2071 | case SK_ListInitialization: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2072 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2073 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2074 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2075 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2076 | case SK_ObjCObjectConversion: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2077 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2079 | case SK_ConversionSequence: |
| 2080 | delete ICS; |
| 2081 | } |
| 2082 | } |
| 2083 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2084 | bool InitializationSequence::isDirectReferenceBinding() const { |
| 2085 | return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference; |
| 2086 | } |
| 2087 | |
| 2088 | bool InitializationSequence::isAmbiguous() const { |
| 2089 | if (getKind() != FailedSequence) |
| 2090 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2092 | switch (getFailureKind()) { |
| 2093 | case FK_TooManyInitsForReference: |
| 2094 | case FK_ArrayNeedsInitList: |
| 2095 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2096 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2097 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2098 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2099 | case FK_RValueReferenceBindingToLValue: |
| 2100 | case FK_ReferenceInitDropsQualifiers: |
| 2101 | case FK_ReferenceInitFailed: |
| 2102 | case FK_ConversionFailed: |
| 2103 | case FK_TooManyInitsForScalar: |
| 2104 | case FK_ReferenceBindingToInitList: |
| 2105 | case FK_InitListBadDestinationType: |
| 2106 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2107 | case FK_Incomplete: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2108 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2110 | case FK_ReferenceInitOverloadFailed: |
| 2111 | case FK_UserConversionOverloadFailed: |
| 2112 | case FK_ConstructorOverloadFailed: |
| 2113 | return FailedOverloadResult == OR_Ambiguous; |
| 2114 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2116 | return false; |
| 2117 | } |
| 2118 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2119 | bool InitializationSequence::isConstructorInitialization() const { |
| 2120 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2121 | } |
| 2122 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2123 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2124 | FunctionDecl *Function, |
| 2125 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2126 | Step S; |
| 2127 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2128 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2129 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2130 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2131 | Steps.push_back(S); |
| 2132 | } |
| 2133 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2134 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2135 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2136 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2137 | switch (VK) { |
| 2138 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2139 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2140 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2141 | default: llvm_unreachable("No such category"); |
| 2142 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2143 | S.Type = BaseType; |
| 2144 | Steps.push_back(S); |
| 2145 | } |
| 2146 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2147 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2148 | bool BindingTemporary) { |
| 2149 | Step S; |
| 2150 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2151 | S.Type = T; |
| 2152 | Steps.push_back(S); |
| 2153 | } |
| 2154 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2155 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2156 | Step S; |
| 2157 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2158 | S.Type = T; |
| 2159 | Steps.push_back(S); |
| 2160 | } |
| 2161 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2162 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2163 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2164 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2165 | Step S; |
| 2166 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2167 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2168 | S.Function.Function = Function; |
| 2169 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2170 | Steps.push_back(S); |
| 2171 | } |
| 2172 | |
| 2173 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2174 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2175 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2176 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2177 | switch (VK) { |
| 2178 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2179 | S.Kind = SK_QualificationConversionRValue; |
| 2180 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2181 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2182 | S.Kind = SK_QualificationConversionXValue; |
| 2183 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2184 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2185 | S.Kind = SK_QualificationConversionLValue; |
| 2186 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2187 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2188 | S.Type = Ty; |
| 2189 | Steps.push_back(S); |
| 2190 | } |
| 2191 | |
| 2192 | void InitializationSequence::AddConversionSequenceStep( |
| 2193 | const ImplicitConversionSequence &ICS, |
| 2194 | QualType T) { |
| 2195 | Step S; |
| 2196 | S.Kind = SK_ConversionSequence; |
| 2197 | S.Type = T; |
| 2198 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2199 | Steps.push_back(S); |
| 2200 | } |
| 2201 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2202 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2203 | Step S; |
| 2204 | S.Kind = SK_ListInitialization; |
| 2205 | S.Type = T; |
| 2206 | Steps.push_back(S); |
| 2207 | } |
| 2208 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2210 | InitializationSequence::AddConstructorInitializationStep( |
| 2211 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2212 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2213 | QualType T) { |
| 2214 | Step S; |
| 2215 | S.Kind = SK_ConstructorInitialization; |
| 2216 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2217 | S.Function.Function = Constructor; |
| 2218 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2219 | Steps.push_back(S); |
| 2220 | } |
| 2221 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2222 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2223 | Step S; |
| 2224 | S.Kind = SK_ZeroInitialization; |
| 2225 | S.Type = T; |
| 2226 | Steps.push_back(S); |
| 2227 | } |
| 2228 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2229 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2230 | Step S; |
| 2231 | S.Kind = SK_CAssignment; |
| 2232 | S.Type = T; |
| 2233 | Steps.push_back(S); |
| 2234 | } |
| 2235 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2236 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2237 | Step S; |
| 2238 | S.Kind = SK_StringInit; |
| 2239 | S.Type = T; |
| 2240 | Steps.push_back(S); |
| 2241 | } |
| 2242 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2243 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2244 | Step S; |
| 2245 | S.Kind = SK_ObjCObjectConversion; |
| 2246 | S.Type = T; |
| 2247 | Steps.push_back(S); |
| 2248 | } |
| 2249 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2250 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2251 | OverloadingResult Result) { |
| 2252 | SequenceKind = FailedSequence; |
| 2253 | this->Failure = Failure; |
| 2254 | this->FailedOverloadResult = Result; |
| 2255 | } |
| 2256 | |
| 2257 | //===----------------------------------------------------------------------===// |
| 2258 | // Attempt initialization |
| 2259 | //===----------------------------------------------------------------------===// |
| 2260 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2261 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2262 | static void TryListInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2263 | const InitializedEntity &Entity, |
| 2264 | const InitializationKind &Kind, |
| 2265 | InitListExpr *InitList, |
| 2266 | InitializationSequence &Sequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2267 | // FIXME: We only perform rudimentary checking of list |
| 2268 | // initializations at this point, then assume that any list |
| 2269 | // initialization of an array, aggregate, or scalar will be |
Sebastian Redl | 36c28db | 2010-06-30 16:41:54 +0000 | [diff] [blame] | 2270 | // well-formed. When we actually "perform" list initialization, we'll |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2271 | // do all of the necessary checking. C++0x initializer lists will |
| 2272 | // force us to perform more checking here. |
| 2273 | Sequence.setSequenceKind(InitializationSequence::ListInitialization); |
| 2274 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2275 | QualType DestType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2276 | |
| 2277 | // C++ [dcl.init]p13: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2278 | // If T is a scalar type, then a declaration of the form |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2279 | // |
| 2280 | // T x = { a }; |
| 2281 | // |
| 2282 | // is equivalent to |
| 2283 | // |
| 2284 | // T x = a; |
| 2285 | if (DestType->isScalarType()) { |
| 2286 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2287 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2288 | return; |
| 2289 | } |
| 2290 | |
| 2291 | // Assume scalar initialization from a single value works. |
| 2292 | } else if (DestType->isAggregateType()) { |
| 2293 | // Assume aggregate initialization works. |
| 2294 | } else if (DestType->isVectorType()) { |
| 2295 | // Assume vector initialization works. |
| 2296 | } else if (DestType->isReferenceType()) { |
| 2297 | // FIXME: C++0x defines behavior for this. |
| 2298 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2299 | return; |
| 2300 | } else if (DestType->isRecordType()) { |
| 2301 | // FIXME: C++0x defines behavior for this |
| 2302 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2303 | } |
| 2304 | |
| 2305 | // Add a general "list initialization" step. |
| 2306 | Sequence.AddListInitializationStep(DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /// \brief Try a reference initialization that involves calling a conversion |
| 2310 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2311 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2312 | const InitializedEntity &Entity, |
| 2313 | const InitializationKind &Kind, |
| 2314 | Expr *Initializer, |
| 2315 | bool AllowRValues, |
| 2316 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2317 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2318 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2319 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2320 | QualType cv2T2 = Initializer->getType(); |
| 2321 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2322 | |
| 2323 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2324 | bool ObjCConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2325 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2326 | T1, T2, DerivedToBase, |
| 2327 | ObjCConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2328 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2329 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2330 | (void)ObjCConversion; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Build the candidate set directly in the initialization sequence |
| 2333 | // structure, so that it will persist if we fail. |
| 2334 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2335 | CandidateSet.clear(); |
| 2336 | |
| 2337 | // Determine whether we are allowed to call explicit constructors or |
| 2338 | // explicit conversion operators. |
| 2339 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2341 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2342 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2343 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2344 | // The type we're converting to is a class type. Enumerate its constructors |
| 2345 | // to see if there is a suitable conversion. |
| 2346 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2348 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2349 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2350 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2351 | NamedDecl *D = *Con; |
| 2352 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2353 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2354 | // Find the constructor (which may be a template). |
| 2355 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2356 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2357 | if (ConstructorTmpl) |
| 2358 | Constructor = cast<CXXConstructorDecl>( |
| 2359 | ConstructorTmpl->getTemplatedDecl()); |
| 2360 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2361 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2363 | if (!Constructor->isInvalidDecl() && |
| 2364 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2365 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2366 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2367 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2368 | &Initializer, 1, CandidateSet, |
| 2369 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2370 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2371 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2372 | &Initializer, 1, CandidateSet, |
| 2373 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2374 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2375 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2376 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2377 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2378 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2380 | const RecordType *T2RecordType = 0; |
| 2381 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2382 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2383 | // The type we're converting from is a class type, enumerate its conversion |
| 2384 | // functions. |
| 2385 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2386 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2387 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2388 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2389 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2390 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2391 | NamedDecl *D = *I; |
| 2392 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2393 | if (isa<UsingShadowDecl>(D)) |
| 2394 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2396 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2397 | CXXConversionDecl *Conv; |
| 2398 | if (ConvTemplate) |
| 2399 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2400 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2401 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2403 | // If the conversion function doesn't return a reference type, |
| 2404 | // it can't be considered for this conversion unless we're allowed to |
| 2405 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2406 | // FIXME: Do we need to make sure that we only consider conversion |
| 2407 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2408 | // break recursion. |
| 2409 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2410 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2411 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2412 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2413 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2414 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2415 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2416 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2417 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2421 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2422 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2423 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2424 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2425 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2426 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2427 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2428 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2429 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2430 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2432 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2435 | if (isa<CXXConversionDecl>(Function)) |
| 2436 | T2 = Function->getResultType(); |
| 2437 | else |
| 2438 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2439 | |
| 2440 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2441 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2442 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2443 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2445 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2446 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2447 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2448 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2449 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2450 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2451 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2452 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2453 | bool NewObjCConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2454 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2455 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2456 | T2.getNonLValueExprType(S.Context), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2457 | NewDerivedToBase, NewObjCConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2458 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2459 | // If the type we've converted to is not reference-related to the |
| 2460 | // type we're looking for, then there is another conversion step |
| 2461 | // we need to perform to produce a temporary of the right type |
| 2462 | // that we'll be binding to. |
| 2463 | ImplicitConversionSequence ICS; |
| 2464 | ICS.setStandard(); |
| 2465 | ICS.Standard = Best->FinalConversion; |
| 2466 | T2 = ICS.Standard.getToType(2); |
| 2467 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2468 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2469 | Sequence.AddDerivedToBaseCastStep( |
| 2470 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2471 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2472 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2473 | else if (NewObjCConversion) |
| 2474 | Sequence.AddObjCObjectConversionStep( |
| 2475 | S.Context.getQualifiedType(T1, |
| 2476 | T2.getNonReferenceType().getQualifiers())); |
| 2477 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2478 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2479 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2480 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2481 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2482 | return OR_Success; |
| 2483 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2484 | |
| 2485 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2486 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2487 | const InitializedEntity &Entity, |
| 2488 | const InitializationKind &Kind, |
| 2489 | Expr *Initializer, |
| 2490 | InitializationSequence &Sequence) { |
| 2491 | Sequence.setSequenceKind(InitializationSequence::ReferenceBinding); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2493 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2494 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2495 | Qualifiers T1Quals; |
| 2496 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2497 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2498 | Qualifiers T2Quals; |
| 2499 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2500 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2501 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2502 | // If the initializer is the address of an overloaded function, try |
| 2503 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2504 | // type of the resulting function. |
| 2505 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2506 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2508 | T1, |
| 2509 | false, |
| 2510 | Found)) { |
| 2511 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2512 | cv2T2 = Fn->getType(); |
| 2513 | T2 = cv2T2.getUnqualifiedType(); |
| 2514 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2515 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2516 | return; |
| 2517 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2518 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2520 | // Compute some basic properties of the types and the initializer. |
| 2521 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2522 | bool isRValueRef = !isLValueRef; |
| 2523 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2524 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2525 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2526 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2527 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
| 2528 | ObjCConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2529 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2530 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2531 | // 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] | 2532 | // "cv2 T2" as follows: |
| 2533 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2534 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2535 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2536 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2537 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2538 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2539 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2540 | bool T1Function = T1->isFunctionType(); |
| 2541 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2543 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2545 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2546 | // - 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] | 2547 | // reference-compatible with "cv2 T2," or |
| 2548 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2549 | // 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] | 2550 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2551 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2552 | // to decide whether we're actually binding to a temporary created from |
| 2553 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2554 | if (DerivedToBase) |
| 2555 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2557 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2558 | else if (ObjCConversion) |
| 2559 | Sequence.AddObjCObjectConversionStep( |
| 2560 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2561 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2562 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2563 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2564 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2565 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2566 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | |
| 2570 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2571 | // reference-related to T2, and can be implicitly converted to an |
| 2572 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2573 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2574 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2575 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2576 | // If we have an rvalue ref to function type here, the rhs must be |
| 2577 | // an rvalue. |
| 2578 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2579 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2580 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2581 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2582 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2583 | Sequence); |
| 2584 | if (ConvOvlResult == OR_Success) |
| 2585 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2586 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2587 | Sequence.SetOverloadFailure( |
| 2588 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2589 | ConvOvlResult); |
| 2590 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2591 | } |
| 2592 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2593 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2594 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2595 | // 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] | 2596 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2597 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2598 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2599 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2600 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2601 | Sequence.SetOverloadFailure( |
| 2602 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2603 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2604 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2605 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2606 | ? (RefRelationship == Sema::Ref_Related |
| 2607 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2608 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2609 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2610 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2611 | return; |
| 2612 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2614 | // - If the initializer expression |
| 2615 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2616 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2617 | // Note: functions are handled below. |
| 2618 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2619 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2621 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2622 | (InitCategory.isXValue() || |
| 2623 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2624 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2625 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2626 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2627 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2628 | // compiler the freedom to perform a copy here or bind to the |
| 2629 | // object, while C++0x requires that we bind directly to the |
| 2630 | // object. Hence, we always bind to the object without making an |
| 2631 | // extra copy. However, in C++03 requires that we check for the |
| 2632 | // presence of a suitable copy constructor: |
| 2633 | // |
| 2634 | // The constructor that would be used to make the copy shall |
| 2635 | // be callable whether or not the copy is actually done. |
Francois Pichet | f57258b | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2636 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2637 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2638 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2640 | if (DerivedToBase) |
| 2641 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2642 | ValueKind); |
| 2643 | else if (ObjCConversion) |
| 2644 | Sequence.AddObjCObjectConversionStep( |
| 2645 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2646 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2647 | if (T1Quals != T2Quals) |
| 2648 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2649 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2650 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2651 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
| 2654 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2655 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2656 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2657 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2658 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2659 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2660 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2661 | Kind, Initializer, |
| 2662 | /*AllowRValues=*/true, |
| 2663 | Sequence); |
| 2664 | if (ConvOvlResult) |
| 2665 | Sequence.SetOverloadFailure( |
| 2666 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2667 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2672 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2673 | return; |
| 2674 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2675 | |
| 2676 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2677 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2678 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2679 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2681 | // Determine whether we are allowed to call explicit constructors or |
| 2682 | // explicit conversion operators. |
| 2683 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2684 | |
| 2685 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2686 | |
| 2687 | if (S.TryImplicitConversion(Sequence, TempEntity, Initializer, |
| 2688 | /*SuppressUserConversions*/ false, |
| 2689 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2690 | /*FIXME:InOverloadResolution=*/false, |
| 2691 | /*CStyle=*/Kind.isCStyleOrFunctionalCast())) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2692 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2693 | // this into an overloading ambiguity diagnostic. However, we need |
| 2694 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2695 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2696 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2697 | Sequence.SetOverloadFailure( |
| 2698 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2699 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2700 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2701 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2702 | else |
| 2703 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2708 | // same cv-qualification as, or greater cv-qualification |
| 2709 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2710 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2711 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2712 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2713 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2714 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2718 | // [...] 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] | 2719 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2720 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2721 | InitCategory.isLValue()) { |
| 2722 | Sequence.SetFailed( |
| 2723 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2724 | return; |
| 2725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2727 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2728 | return; |
| 2729 | } |
| 2730 | |
| 2731 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2732 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2733 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2734 | const InitializedEntity &Entity, |
| 2735 | const InitializationKind &Kind, |
| 2736 | Expr *Initializer, |
| 2737 | InitializationSequence &Sequence) { |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2738 | Sequence.setSequenceKind(InitializationSequence::StringInit); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2739 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2742 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2743 | /// enumerates the constructors of the initialized entity and performs overload |
| 2744 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2745 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2746 | const InitializedEntity &Entity, |
| 2747 | const InitializationKind &Kind, |
| 2748 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2749 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2750 | InitializationSequence &Sequence) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2751 | Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2752 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2753 | // Build the candidate set directly in the initialization sequence |
| 2754 | // structure, so that it will persist if we fail. |
| 2755 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2756 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2758 | // Determine whether we are allowed to call explicit constructors or |
| 2759 | // explicit conversion operators. |
| 2760 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2761 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2762 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2763 | |
| 2764 | // The type we're constructing needs to be complete. |
| 2765 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2766 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2767 | return; |
| 2768 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2770 | // The type we're converting to is a class type. Enumerate its constructors |
| 2771 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2772 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2774 | CXXRecordDecl *DestRecordDecl |
| 2775 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2777 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2778 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2779 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2780 | NamedDecl *D = *Con; |
| 2781 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2782 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2784 | // Find the constructor (which may be a template). |
| 2785 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2786 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2787 | if (ConstructorTmpl) |
| 2788 | Constructor = cast<CXXConstructorDecl>( |
| 2789 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2790 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2791 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2792 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2793 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2794 | // suppress user-defined conversions on the arguments. |
| 2795 | // FIXME: Move constructors? |
| 2796 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2797 | Constructor->isCopyConstructor()) |
| 2798 | SuppressUserConversions = true; |
| 2799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2801 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2802 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2803 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2804 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2805 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2806 | Args, NumArgs, CandidateSet, |
| 2807 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2808 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2809 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2810 | Args, NumArgs, CandidateSet, |
| 2811 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2812 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2815 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
| 2817 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2818 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2819 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2820 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2821 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2823 | Result); |
| 2824 | return; |
| 2825 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2826 | |
| 2827 | // C++0x [dcl.init]p6: |
| 2828 | // If a program calls for the default initialization of an object |
| 2829 | // of a const-qualified type T, T shall be a class type with a |
| 2830 | // user-provided default constructor. |
| 2831 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2832 | Entity.getType().isConstQualified() && |
| 2833 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2834 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2835 | return; |
| 2836 | } |
| 2837 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2838 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2839 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2840 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2841 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2842 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2843 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2846 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2848 | const InitializedEntity &Entity, |
| 2849 | const InitializationKind &Kind, |
| 2850 | InitializationSequence &Sequence) { |
| 2851 | // C++ [dcl.init]p5: |
| 2852 | // |
| 2853 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2854 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2856 | // -- if T is an array type, then each element is value-initialized; |
| 2857 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2858 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2860 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2861 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2862 | // -- if T is a class type (clause 9) with a user-declared |
| 2863 | // constructor (12.1), then the default constructor for T is |
| 2864 | // called (and the initialization is ill-formed if T has no |
| 2865 | // accessible default constructor); |
| 2866 | // |
| 2867 | // FIXME: we really want to refer to a single subobject of the array, |
| 2868 | // but Entity doesn't have a way to capture that (yet). |
| 2869 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 2870 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2871 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2872 | // -- if T is a (possibly cv-qualified) non-union class type |
| 2873 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2874 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2875 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2876 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2877 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2878 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2880 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2884 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2885 | Sequence.setSequenceKind(InitializationSequence::ZeroInitialization); |
| 2886 | } |
| 2887 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2888 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 2889 | static void TryDefaultInitialization(Sema &S, |
| 2890 | const InitializedEntity &Entity, |
| 2891 | const InitializationKind &Kind, |
| 2892 | InitializationSequence &Sequence) { |
| 2893 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2895 | // C++ [dcl.init]p6: |
| 2896 | // To default-initialize an object of type T means: |
| 2897 | // - if T is an array type, each element is default-initialized; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2898 | QualType DestType = Entity.getType(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2899 | while (const ArrayType *Array = S.Context.getAsArrayType(DestType)) |
| 2900 | DestType = Array->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2902 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 2903 | // constructor for T is called (and the initialization is ill-formed if |
| 2904 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2905 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 2906 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 2907 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2908 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2909 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2910 | // - otherwise, no initialization is performed. |
| 2911 | Sequence.setSequenceKind(InitializationSequence::NoInitialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2913 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | // 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] | 2915 | // default constructor. |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2916 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2917 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2918 | } |
| 2919 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2920 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 2921 | /// which enumerates all conversion functions and performs overload resolution |
| 2922 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2923 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2924 | const InitializedEntity &Entity, |
| 2925 | const InitializationKind &Kind, |
| 2926 | Expr *Initializer, |
| 2927 | InitializationSequence &Sequence) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2928 | Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2930 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2931 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 2932 | QualType SourceType = Initializer->getType(); |
| 2933 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 2934 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2936 | // Build the candidate set directly in the initialization sequence |
| 2937 | // structure, so that it will persist if we fail. |
| 2938 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2939 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2941 | // Determine whether we are allowed to call explicit constructors or |
| 2942 | // explicit conversion operators. |
| 2943 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2945 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 2946 | // The type we're converting to is a class type. Enumerate its constructors |
| 2947 | // to see if there is a suitable conversion. |
| 2948 | CXXRecordDecl *DestRecordDecl |
| 2949 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2950 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2951 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2953 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2954 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2955 | Con != ConEnd; ++Con) { |
| 2956 | NamedDecl *D = *Con; |
| 2957 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2958 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2959 | // Find the constructor (which may be a template). |
| 2960 | CXXConstructorDecl *Constructor = 0; |
| 2961 | FunctionTemplateDecl *ConstructorTmpl |
| 2962 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2963 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2964 | Constructor = cast<CXXConstructorDecl>( |
| 2965 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2966 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2967 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2969 | if (!Constructor->isInvalidDecl() && |
| 2970 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2971 | if (ConstructorTmpl) |
| 2972 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2973 | /*ExplicitArgs*/ 0, |
| 2974 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2975 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2976 | else |
| 2977 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2978 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 2979 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2980 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2981 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2982 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2983 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2984 | |
| 2985 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2986 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2987 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 2988 | // The type we're converting from is a class type, enumerate its conversion |
| 2989 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2990 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2991 | // We can only enumerate the conversion functions for a complete type; if |
| 2992 | // the type isn't complete, simply skip this step. |
| 2993 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 2994 | CXXRecordDecl *SourceRecordDecl |
| 2995 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2996 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2997 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2998 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2999 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3000 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3001 | I != E; ++I) { |
| 3002 | NamedDecl *D = *I; |
| 3003 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3004 | if (isa<UsingShadowDecl>(D)) |
| 3005 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3007 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3008 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3009 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3010 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3011 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3012 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3013 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3014 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3015 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3016 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3017 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3018 | CandidateSet); |
| 3019 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3020 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3021 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3022 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3023 | } |
| 3024 | } |
| 3025 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3028 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3029 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3030 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3031 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3032 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3033 | Result); |
| 3034 | return; |
| 3035 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3037 | FunctionDecl *Function = Best->Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3039 | if (isa<CXXConstructorDecl>(Function)) { |
| 3040 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3041 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3042 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3043 | return; |
| 3044 | } |
| 3045 | |
| 3046 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3047 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3048 | if (ConvType->getAs<RecordType>()) { |
| 3049 | // If we're converting to a class type, there may be an copy if |
| 3050 | // the resulting temporary object (possible to create an object of |
| 3051 | // a base class type). That copy is not a separate conversion, so |
| 3052 | // we just make a note of the actual destination type (possibly a |
| 3053 | // base class of the type returned by the conversion function) and |
| 3054 | // let the user-defined conversion step handle the conversion. |
| 3055 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3056 | return; |
| 3057 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3058 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3059 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3061 | // If the conversion following the call to the conversion function |
| 3062 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3063 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3064 | Best->FinalConversion.Third) { |
| 3065 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3066 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3067 | ICS.Standard = Best->FinalConversion; |
| 3068 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3069 | } |
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 | InitializationSequence::InitializationSequence(Sema &S, |
| 3073 | const InitializedEntity &Entity, |
| 3074 | const InitializationKind &Kind, |
| 3075 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3076 | unsigned NumArgs) |
| 3077 | : FailedCandidateSet(Kind.getLocation()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3078 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3079 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3080 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3081 | // The semantics of initializers are as follows. The destination type is |
| 3082 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3083 | // 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] | 3084 | // 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] | 3085 | // parenthesized list of expressions. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3086 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3087 | |
| 3088 | if (DestType->isDependentType() || |
| 3089 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3090 | SequenceKind = DependentSequence; |
| 3091 | return; |
| 3092 | } |
| 3093 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3094 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3095 | if (Args[I]->getObjectKind() == OK_ObjCProperty) |
| 3096 | S.ConvertPropertyForRValue(Args[I]); |
| 3097 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3098 | QualType SourceType; |
| 3099 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3100 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3101 | Initializer = Args[0]; |
| 3102 | if (!isa<InitListExpr>(Initializer)) |
| 3103 | SourceType = Initializer->getType(); |
| 3104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3105 | |
| 3106 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3107 | // list-initialized (8.5.4). |
| 3108 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 3109 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3110 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3113 | // - If the destination type is a reference type, see 8.5.3. |
| 3114 | if (DestType->isReferenceType()) { |
| 3115 | // C++0x [dcl.init.ref]p1: |
| 3116 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3117 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3118 | // by an object that can be converted into a T. |
| 3119 | // (Therefore, multiple arguments are not permitted.) |
| 3120 | if (NumArgs != 1) |
| 3121 | SetFailed(FK_TooManyInitsForReference); |
| 3122 | else |
| 3123 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 3124 | return; |
| 3125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3127 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3128 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3129 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3130 | TryValueInitialization(S, Entity, Kind, *this); |
| 3131 | return; |
| 3132 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3134 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3135 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3136 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 3137 | return; |
| 3138 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3139 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3140 | // - If the destination type is an array of characters, an array of |
| 3141 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3142 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3144 | // ill-formed. |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3145 | if (const ArrayType *arrayType = Context.getAsArrayType(DestType)) { |
| 3146 | if (Initializer && IsStringInit(Initializer, arrayType, Context)) { |
| 3147 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | if (arrayType->getElementType()->isAnyCharacterType()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3152 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 3153 | else |
| 3154 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3156 | return; |
| 3157 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3158 | |
| 3159 | // Handle initialization in C |
| 3160 | if (!S.getLangOptions().CPlusPlus) { |
| 3161 | setSequenceKind(CAssignment); |
| 3162 | AddCAssignmentStep(DestType); |
| 3163 | return; |
| 3164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3166 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3167 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3168 | // - If the initialization is direct-initialization, or if it is |
| 3169 | // copy-initialization where the cv-unqualified version of the |
| 3170 | // 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] | 3171 | // class of the destination, constructors are considered. [...] |
| 3172 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3173 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3174 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3175 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3176 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3177 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3178 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3179 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | // used) to a derived class thereof are enumerated as described in |
| 3182 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3183 | // (13.3). |
| 3184 | else |
| 3185 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3186 | return; |
| 3187 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3189 | if (NumArgs > 1) { |
| 3190 | SetFailed(FK_TooManyInitsForScalar); |
| 3191 | return; |
| 3192 | } |
| 3193 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3194 | |
| 3195 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3196 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3197 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3198 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3199 | return; |
| 3200 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3201 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3202 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3203 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3204 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3205 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3206 | // destination type; no user-defined conversions are considered. |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3207 | if (S.TryImplicitConversion(*this, Entity, Initializer, |
| 3208 | /*SuppressUserConversions*/ true, |
| 3209 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3210 | /*InOverloadResolution*/ false, |
| 3211 | /*CStyle=*/Kind.isCStyleOrFunctionalCast())) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3212 | { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3213 | DeclAccessPair dap; |
| 3214 | if (Initializer->getType() == Context.OverloadTy && |
| 3215 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3216 | , DestType, false, dap)) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3217 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3218 | else |
| 3219 | SetFailed(InitializationSequence::FK_ConversionFailed); |
| 3220 | } |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3221 | else |
| 3222 | setSequenceKind(StandardConversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | InitializationSequence::~InitializationSequence() { |
| 3226 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 3227 | StepEnd = Steps.end(); |
| 3228 | Step != StepEnd; ++Step) |
| 3229 | Step->Destroy(); |
| 3230 | } |
| 3231 | |
| 3232 | //===----------------------------------------------------------------------===// |
| 3233 | // Perform initialization |
| 3234 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3235 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3236 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3237 | switch(Entity.getKind()) { |
| 3238 | case InitializedEntity::EK_Variable: |
| 3239 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3240 | case InitializedEntity::EK_Exception: |
| 3241 | case InitializedEntity::EK_Base: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3242 | return Sema::AA_Initializing; |
| 3243 | |
| 3244 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3245 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3246 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3247 | return Sema::AA_Sending; |
| 3248 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3249 | return Sema::AA_Passing; |
| 3250 | |
| 3251 | case InitializedEntity::EK_Result: |
| 3252 | return Sema::AA_Returning; |
| 3253 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3254 | case InitializedEntity::EK_Temporary: |
| 3255 | // FIXME: Can we tell apart casting vs. converting? |
| 3256 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3258 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3259 | case InitializedEntity::EK_ArrayElement: |
| 3260 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3261 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3262 | return Sema::AA_Initializing; |
| 3263 | } |
| 3264 | |
| 3265 | return Sema::AA_Converting; |
| 3266 | } |
| 3267 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3268 | /// \brief Whether we should binding a created object as a temporary when |
| 3269 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3270 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3271 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3272 | case InitializedEntity::EK_ArrayElement: |
| 3273 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3274 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3275 | case InitializedEntity::EK_New: |
| 3276 | case InitializedEntity::EK_Variable: |
| 3277 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3278 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3279 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3280 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3281 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3283 | case InitializedEntity::EK_Parameter: |
| 3284 | case InitializedEntity::EK_Temporary: |
| 3285 | return true; |
| 3286 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3288 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3289 | } |
| 3290 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3291 | /// \brief Whether the given entity, when initialized with an object |
| 3292 | /// created for that initialization, requires destruction. |
| 3293 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3294 | switch (Entity.getKind()) { |
| 3295 | case InitializedEntity::EK_Member: |
| 3296 | case InitializedEntity::EK_Result: |
| 3297 | case InitializedEntity::EK_New: |
| 3298 | case InitializedEntity::EK_Base: |
| 3299 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3300 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3301 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3303 | case InitializedEntity::EK_Variable: |
| 3304 | case InitializedEntity::EK_Parameter: |
| 3305 | case InitializedEntity::EK_Temporary: |
| 3306 | case InitializedEntity::EK_ArrayElement: |
| 3307 | case InitializedEntity::EK_Exception: |
| 3308 | return true; |
| 3309 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | |
| 3311 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3314 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3315 | /// provided by the given initializer by calling the appropriate copy |
| 3316 | /// constructor. |
| 3317 | /// |
| 3318 | /// \param S The Sema object used for type-checking. |
| 3319 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3320 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3321 | /// the type of the initializer expression or a superclass thereof. |
| 3322 | /// |
| 3323 | /// \param Enter The entity being initialized. |
| 3324 | /// |
| 3325 | /// \param CurInit The initializer expression. |
| 3326 | /// |
| 3327 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3328 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3329 | /// an rvalue. |
| 3330 | /// |
| 3331 | /// \returns An expression that copies the initializer expression into |
| 3332 | /// a temporary object, or an error expression if a copy could not be |
| 3333 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3334 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3335 | QualType T, |
| 3336 | const InitializedEntity &Entity, |
| 3337 | ExprResult CurInit, |
| 3338 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3339 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3340 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3341 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3342 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3343 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3344 | if (!Class) |
| 3345 | return move(CurInit); |
| 3346 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3347 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3348 | // When certain criteria are met, an implementation is allowed to |
| 3349 | // omit the copy/move construction of a class object, even if the |
| 3350 | // copy/move constructor and/or destructor for the object have |
| 3351 | // side effects. [...] |
| 3352 | // - when a temporary class object that has not been bound to a |
| 3353 | // reference (12.2) would be copied/moved to a class object |
| 3354 | // with the same cv-unqualified type, the copy/move operation |
| 3355 | // can be omitted by constructing the temporary object |
| 3356 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3358 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3359 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3361 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3362 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3363 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3364 | switch (Entity.getKind()) { |
| 3365 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3366 | Loc = Entity.getReturnLoc(); |
| 3367 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3369 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3370 | Loc = Entity.getThrowLoc(); |
| 3371 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3373 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3374 | Loc = Entity.getDecl()->getLocation(); |
| 3375 | break; |
| 3376 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3377 | case InitializedEntity::EK_ArrayElement: |
| 3378 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3379 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3380 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3381 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3382 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3383 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3384 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3385 | Loc = CurInitExpr->getLocStart(); |
| 3386 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3387 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3388 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3390 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3391 | return move(CurInit); |
| 3392 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3393 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3394 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3395 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3396 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3397 | Con != ConEnd; ++Con) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3398 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3399 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3400 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3401 | CXXConstructorDecl *Constructor = 0; |
| 3402 | |
| 3403 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3404 | // Handle copy/moveconstructors, only. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3405 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3406 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3407 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3408 | continue; |
| 3409 | |
| 3410 | DeclAccessPair FoundDecl |
| 3411 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3412 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3413 | &CurInitExpr, 1, CandidateSet); |
| 3414 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3415 | } |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3416 | |
| 3417 | // Handle constructor templates. |
| 3418 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3419 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3420 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3422 | Constructor = cast<CXXConstructorDecl>( |
| 3423 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3424 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3425 | continue; |
| 3426 | |
| 3427 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3428 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3429 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3430 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3431 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3432 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3433 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3434 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3435 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3436 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3437 | case OR_Success: |
| 3438 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3440 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3441 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3442 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3443 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3444 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3445 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3446 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3447 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3448 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3449 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3450 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3451 | case OR_Ambiguous: |
| 3452 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3453 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3454 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3455 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3456 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3458 | case OR_Deleted: |
| 3459 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3460 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3461 | << CurInitExpr->getSourceRange(); |
| 3462 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3463 | << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3464 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3467 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3468 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3469 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3470 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3471 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3472 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3473 | |
| 3474 | if (IsExtraneousCopy) { |
| 3475 | // If this is a totally extraneous copy for C++03 reference |
| 3476 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3477 | // expression. We don't generate an (elided) copy operation here |
| 3478 | // because doing so would require us to pass down a flag to avoid |
| 3479 | // infinite recursion, where each step adds another extraneous, |
| 3480 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3481 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3482 | // Instantiate the default arguments of any extra parameters in |
| 3483 | // the selected copy constructor, as if we were going to create a |
| 3484 | // proper call to the copy constructor. |
| 3485 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 3486 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 3487 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 3488 | S.PDiag(diag::err_call_incomplete_argument))) |
| 3489 | break; |
| 3490 | |
| 3491 | // Build the default argument expression; we don't actually care |
| 3492 | // if this succeeds or not, because this routine will complain |
| 3493 | // if there was a problem. |
| 3494 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 3495 | } |
| 3496 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3497 | return S.Owned(CurInitExpr); |
| 3498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3500 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3501 | // constructor call (we might have derived-to-base conversions, or |
| 3502 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3503 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3504 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3505 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3507 | // Actually perform the constructor call. |
| 3508 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3509 | move_arg(ConstructorArgs), |
| 3510 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3511 | CXXConstructExpr::CK_Complete, |
| 3512 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3514 | // If we're supposed to bind temporaries, do so. |
| 3515 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 3516 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3517 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3518 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3519 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3520 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 3521 | const InitializedEntity &Entity) { |
| 3522 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 3523 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 3524 | return; |
| 3525 | |
| 3526 | if (Entity.getDecl()->getDeclName()) |
| 3527 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 3528 | << Entity.getDecl()->getDeclName(); |
| 3529 | else |
| 3530 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 3531 | } |
| 3532 | } |
| 3533 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3535 | InitializationSequence::Perform(Sema &S, |
| 3536 | const InitializedEntity &Entity, |
| 3537 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3538 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3539 | QualType *ResultType) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3540 | if (SequenceKind == FailedSequence) { |
| 3541 | unsigned NumArgs = Args.size(); |
| 3542 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3543 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3545 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3546 | if (SequenceKind == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3547 | // If the declaration is a non-dependent, incomplete array type |
| 3548 | // that has an initializer, then its type will be completed once |
| 3549 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3550 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3551 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3552 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3553 | if (const IncompleteArrayType *ArrayT |
| 3554 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 3555 | // FIXME: We don't currently have the ability to accurately |
| 3556 | // compute the length of an initializer list without |
| 3557 | // performing full type-checking of the initializer list |
| 3558 | // (since we have to determine where braces are implicitly |
| 3559 | // introduced and such). So, we fall back to making the array |
| 3560 | // type a dependently-sized array type with no specified |
| 3561 | // bound. |
| 3562 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 3563 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3565 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3566 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 3567 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 3568 | TypeLoc TL = TInfo->getTypeLoc(); |
| 3569 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 3570 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 3571 | Brackets = ArrayLoc->getBracketsRange(); |
| 3572 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | *ResultType |
| 3576 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 3577 | /*NumElts=*/0, |
| 3578 | ArrayT->getSizeModifier(), |
| 3579 | ArrayT->getIndexTypeCVRQualifiers(), |
| 3580 | Brackets); |
| 3581 | } |
| 3582 | |
| 3583 | } |
| 3584 | } |
| 3585 | |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3586 | if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast()) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3587 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | |
Douglas Gregor | 67fa05b | 2010-02-05 07:56:11 +0000 | [diff] [blame] | 3589 | if (Args.size() == 0) |
| 3590 | return S.Owned((Expr *)0); |
| 3591 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3592 | unsigned NumArgs = Args.size(); |
| 3593 | return S.Owned(new (S.Context) ParenListExpr(S.Context, |
| 3594 | SourceLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | (Expr **)Args.release(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3596 | NumArgs, |
| 3597 | SourceLocation())); |
| 3598 | } |
| 3599 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3600 | if (SequenceKind == NoInitialization) |
| 3601 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3603 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 3604 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3605 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 3606 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3607 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3608 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3609 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3610 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3611 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3612 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3613 | assert(!Steps.empty() && "Cannot have an empty initialization sequence"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | |
| 3615 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3616 | // grab the only argument out the Args and place it into the "current" |
| 3617 | // initializer. |
| 3618 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3619 | case SK_ResolveAddressOfOverloadedFunction: |
| 3620 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3621 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3622 | case SK_CastDerivedToBaseLValue: |
| 3623 | case SK_BindReference: |
| 3624 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3625 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3626 | case SK_UserConversion: |
| 3627 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3628 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3629 | case SK_QualificationConversionRValue: |
| 3630 | case SK_ConversionSequence: |
| 3631 | case SK_ListInitialization: |
| 3632 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3633 | case SK_StringInit: |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3634 | case SK_ObjCObjectConversion: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3635 | assert(Args.size() == 1); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3636 | Expr *CurInitExpr = Args.get()[0]; |
| 3637 | if (!CurInitExpr) return ExprError(); |
| 3638 | |
| 3639 | // Read from a property when initializing something with it. |
| 3640 | if (CurInitExpr->getObjectKind() == OK_ObjCProperty) |
| 3641 | S.ConvertPropertyForRValue(CurInitExpr); |
| 3642 | |
| 3643 | CurInit = ExprResult(CurInitExpr); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3644 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3645 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3647 | case SK_ConstructorInitialization: |
| 3648 | case SK_ZeroInitialization: |
| 3649 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3651 | |
| 3652 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3654 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 3656 | Step != StepEnd; ++Step) { |
| 3657 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3658 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3659 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3660 | Expr *CurInitExpr = CurInit.get(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3661 | QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3662 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3663 | switch (Step->Kind) { |
| 3664 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3665 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3666 | // initializer to reflect that choice. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3667 | S.CheckAddressOfMemberAccess(CurInitExpr, Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3668 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3669 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3670 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3671 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3672 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3673 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3674 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3675 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | case SK_CastDerivedToBaseLValue: { |
| 3677 | // We have a derived-to-base cast that produces either an rvalue or an |
| 3678 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3679 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3680 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3681 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3682 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 3683 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 3684 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
| 3685 | CurInitExpr->getLocStart(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | CurInitExpr->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3687 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3688 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3690 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 3691 | QualType T = SourceType; |
| 3692 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 3693 | T = Pointer->getPointeeType(); |
| 3694 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | S.MarkVTableUsed(CurInitExpr->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3696 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 3697 | } |
| 3698 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3699 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3700 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3701 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3702 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3703 | VK_XValue : |
| 3704 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3705 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 3706 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3707 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3708 | CurInit.get(), |
| 3709 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3710 | break; |
| 3711 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3713 | case SK_BindReference: |
| 3714 | if (FieldDecl *BitField = CurInitExpr->getBitField()) { |
| 3715 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 3716 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3717 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3718 | << BitField->getDeclName() |
| 3719 | << CurInitExpr->getSourceRange(); |
| 3720 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3721 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3722 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 3723 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3724 | if (CurInitExpr->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 3725 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3726 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 3727 | << Entity.getType().isVolatileQualified() |
| 3728 | << CurInitExpr->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3729 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3730 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3731 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3732 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3733 | // Reference binding does not have any corresponding ASTs. |
| 3734 | |
| 3735 | // Check exception specifications |
| 3736 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3737 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3739 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3740 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3741 | case SK_BindReferenceToTemporary: |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 3742 | // Reference binding does not have any corresponding ASTs. |
| 3743 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3744 | // Check exception specifications |
| 3745 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3746 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3748 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3749 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3750 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3752 | /*IsExtraneousCopy=*/true); |
| 3753 | break; |
| 3754 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3755 | case SK_UserConversion: { |
| 3756 | // We have a user-defined conversion that invokes either a constructor |
| 3757 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3758 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3759 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3760 | FunctionDecl *Fn = Step->Function.Function; |
| 3761 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3762 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3763 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3764 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3765 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3766 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3767 | SourceLocation Loc = CurInitExpr->getLocStart(); |
| 3768 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3769 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3770 | // Determine the arguments required to actually perform the constructor |
| 3771 | // call. |
| 3772 | if (S.CompleteConstructorCall(Constructor, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3773 | MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3774 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3775 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3777 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3778 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3779 | move_arg(ConstructorArgs), |
| 3780 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3781 | CXXConstructExpr::CK_Complete, |
| 3782 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3783 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3784 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3785 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3786 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3787 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3788 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3789 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3790 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3791 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 3792 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 3793 | S.IsDerivedFrom(SourceType, Class)) |
| 3794 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3796 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3797 | } else { |
| 3798 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3799 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3800 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3801 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3802 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3803 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3804 | |
| 3805 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3806 | // derived-to-base conversion? I believe the answer is "no", because |
| 3807 | // we don't want to turn off access control here for c-style casts. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3808 | if (S.PerformObjectArgumentInitialization(CurInitExpr, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3809 | FoundFn, Conversion)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3810 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3811 | |
| 3812 | // Do a little dance to make sure that CurInit has the proper |
| 3813 | // pointer. |
| 3814 | CurInit.release(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3815 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3816 | // Build the actual call to the conversion function. |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3817 | CurInit = S.BuildCXXMemberCallExpr(CurInitExpr, FoundFn, Conversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3818 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3819 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3821 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3823 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3824 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3825 | |
| 3826 | bool RequiresCopy = !IsCopy && |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3827 | getKind() != InitializationSequence::ReferenceBinding; |
| 3828 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3829 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3830 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
| 3831 | CurInitExpr = static_cast<Expr *>(CurInit.get()); |
| 3832 | QualType T = CurInitExpr->getType(); |
| 3833 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3835 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 | S.CheckDestructorAccess(CurInitExpr->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3837 | S.PDiag(diag::err_access_dtor_temp) << T); |
| 3838 | S.MarkDeclarationReferenced(CurInitExpr->getLocStart(), Destructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3839 | S.DiagnoseUseOfDecl(Destructor, CurInitExpr->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3840 | } |
| 3841 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3843 | CurInitExpr = CurInit.takeAs<Expr>(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3844 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3845 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 3846 | CurInitExpr->getType(), |
| 3847 | CastKind, CurInitExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3848 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3850 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3851 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 3852 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3854 | break; |
| 3855 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3857 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3858 | case SK_QualificationConversionXValue: |
| 3859 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3860 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3861 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3862 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3863 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3864 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3865 | VK_XValue : |
| 3866 | VK_RValue); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3867 | S.ImpCastExprToType(CurInitExpr, Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3868 | CurInit.release(); |
| 3869 | CurInit = S.Owned(CurInitExpr); |
| 3870 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3873 | case SK_ConversionSequence: { |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3874 | if (S.PerformImplicitConversion(CurInitExpr, Step->Type, *Step->ICS, |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3875 | getAssignmentAction(Entity), |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3876 | Kind.isCStyleOrFunctionalCast())) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3877 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3879 | CurInit.release(); |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3880 | CurInit = S.Owned(CurInitExpr); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3881 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3883 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3884 | case SK_ListInitialization: { |
| 3885 | InitListExpr *InitList = cast<InitListExpr>(CurInitExpr); |
| 3886 | QualType Ty = Step->Type; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3887 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3888 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3889 | |
| 3890 | CurInit.release(); |
| 3891 | CurInit = S.Owned(InitList); |
| 3892 | break; |
| 3893 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3894 | |
| 3895 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3896 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3897 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3898 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3900 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3901 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 3902 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 3903 | ? Kind.getEqualLoc() |
| 3904 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3905 | |
| 3906 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 3907 | // Force even a trivial, implicit default constructor to be |
| 3908 | // semantically checked. We do this explicitly because we don't build |
| 3909 | // the definition for completely trivial constructors. |
| 3910 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3911 | assert(ClassDecl && "No parent class for constructor."); |
| 3912 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 3913 | ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false)) |
| 3914 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 3915 | } |
| 3916 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3917 | // Determine the arguments required to actually perform the constructor |
| 3918 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3920 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3921 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | |
| 3923 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3924 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3925 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3926 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 3927 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 3928 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 3929 | unsigned NumExprs = ConstructorArgs.size(); |
| 3930 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 3931 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3932 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3933 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 3934 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 3935 | if (!TSInfo) |
| 3936 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3937 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3938 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 3939 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 3940 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3941 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 3942 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3943 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 3944 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 3947 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3948 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3949 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 3950 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3952 | CXXConstructExpr::CK_NonVirtualBase; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3955 | // Only get the parenthesis range if it is a direct construction. |
| 3956 | SourceRange parenRange = |
| 3957 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 3958 | Kind.getParenRange() : SourceRange(); |
| 3959 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3960 | // If the entity allows NRVO, mark the construction as elidable |
| 3961 | // unconditionally. |
| 3962 | if (Entity.allowsNRVO()) |
| 3963 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 3964 | Constructor, /*Elidable=*/true, |
| 3965 | move_arg(ConstructorArgs), |
| 3966 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3967 | ConstructKind, |
| 3968 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3969 | else |
| 3970 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3972 | move_arg(ConstructorArgs), |
| 3973 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3974 | ConstructKind, |
| 3975 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 3976 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3977 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3978 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3979 | |
| 3980 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3981 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3982 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3983 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3985 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3986 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3988 | break; |
| 3989 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3991 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3992 | step_iterator NextStep = Step; |
| 3993 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3994 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3995 | NextStep->Kind == SK_ConstructorInitialization) { |
| 3996 | // The need for zero-initialization is recorded directly into |
| 3997 | // the call to the object's constructor within the next step. |
| 3998 | ConstructorInitRequiresZeroInit = true; |
| 3999 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4000 | S.getLangOptions().CPlusPlus && |
| 4001 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4002 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4003 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4005 | Kind.getRange().getBegin()); |
| 4006 | |
| 4007 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4008 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4009 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4010 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4011 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4012 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4013 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4014 | break; |
| 4015 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4016 | |
| 4017 | case SK_CAssignment: { |
| 4018 | QualType SourceType = CurInitExpr->getType(); |
| 4019 | Sema::AssignConvertType ConvTy = |
| 4020 | S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4021 | |
| 4022 | // If this is a call, allow conversion to a transparent union. |
| 4023 | if (ConvTy != Sema::Compatible && |
| 4024 | Entity.getKind() == InitializedEntity::EK_Parameter && |
| 4025 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr) |
| 4026 | == Sema::Compatible) |
| 4027 | ConvTy = Sema::Compatible; |
| 4028 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4029 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4030 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4031 | Step->Type, SourceType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4032 | CurInitExpr, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4033 | getAssignmentAction(Entity), |
| 4034 | &Complained)) { |
| 4035 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4036 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4037 | } else if (Complained) |
| 4038 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4039 | |
| 4040 | CurInit.release(); |
| 4041 | CurInit = S.Owned(CurInitExpr); |
| 4042 | break; |
| 4043 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4044 | |
| 4045 | case SK_StringInit: { |
| 4046 | QualType Ty = Step->Type; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4047 | CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, |
| 4048 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4049 | break; |
| 4050 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4051 | |
| 4052 | case SK_ObjCObjectConversion: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4053 | S.ImpCastExprToType(CurInitExpr, Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4054 | CK_ObjCObjectLValueCast, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4055 | S.CastCategory(CurInitExpr)); |
| 4056 | CurInit.release(); |
| 4057 | CurInit = S.Owned(CurInitExpr); |
| 4058 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4061 | |
| 4062 | // Diagnose non-fatal problems with the completed initialization. |
| 4063 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4064 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4065 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4066 | cast<FieldDecl>(Entity.getDecl()), |
| 4067 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4068 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4069 | return move(CurInit); |
| 4070 | } |
| 4071 | |
| 4072 | //===----------------------------------------------------------------------===// |
| 4073 | // Diagnose initialization failures |
| 4074 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4076 | const InitializedEntity &Entity, |
| 4077 | const InitializationKind &Kind, |
| 4078 | Expr **Args, unsigned NumArgs) { |
| 4079 | if (SequenceKind != FailedSequence) |
| 4080 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4082 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4083 | switch (Failure) { |
| 4084 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4085 | // FIXME: Customize for the initialized entity? |
| 4086 | if (NumArgs == 0) |
| 4087 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4088 | << DestType.getNonReferenceType(); |
| 4089 | else // FIXME: diagnostic below could be better! |
| 4090 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4091 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4092 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4094 | case FK_ArrayNeedsInitList: |
| 4095 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4096 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4097 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4098 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4099 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4100 | case FK_AddressOfOverloadFailed: { |
| 4101 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4102 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4103 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4104 | true, |
| 4105 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4106 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4107 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4109 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4110 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4111 | switch (FailedOverloadResult) { |
| 4112 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4113 | if (Failure == FK_UserConversionOverloadFailed) |
| 4114 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4115 | << Args[0]->getType() << DestType |
| 4116 | << Args[0]->getSourceRange(); |
| 4117 | else |
| 4118 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4119 | << DestType << Args[0]->getType() |
| 4120 | << Args[0]->getSourceRange(); |
| 4121 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4122 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4123 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4124 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4125 | case OR_No_Viable_Function: |
| 4126 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4127 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4128 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4129 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4130 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4132 | case OR_Deleted: { |
| 4133 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4134 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4135 | << Args[0]->getSourceRange(); |
| 4136 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4137 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4138 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4139 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4140 | if (Ovl == OR_Deleted) { |
| 4141 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4142 | << Best->Function->isDeleted(); |
| 4143 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4144 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4145 | } |
| 4146 | break; |
| 4147 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4149 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4150 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | } |
| 4153 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4155 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4156 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4157 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4158 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4159 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4160 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4161 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4162 | << DestType.getNonReferenceType() |
| 4163 | << Args[0]->getType() |
| 4164 | << Args[0]->getSourceRange(); |
| 4165 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4167 | case FK_RValueReferenceBindingToLValue: |
| 4168 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4169 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4170 | << Args[0]->getSourceRange(); |
| 4171 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4173 | case FK_ReferenceInitDropsQualifiers: |
| 4174 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4175 | << DestType.getNonReferenceType() |
| 4176 | << Args[0]->getType() |
| 4177 | << Args[0]->getSourceRange(); |
| 4178 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4179 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4180 | case FK_ReferenceInitFailed: |
| 4181 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4182 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4183 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | << Args[0]->getType() |
| 4185 | << Args[0]->getSourceRange(); |
| 4186 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4187 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4188 | case FK_ConversionFailed: { |
| 4189 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4190 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4191 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4192 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4193 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4194 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4195 | << Args[0]->getSourceRange(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4196 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4197 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4198 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4199 | SourceRange R; |
| 4200 | |
| 4201 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4202 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4203 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4205 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4206 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4207 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4208 | if (Kind.isCStyleOrFunctionalCast()) |
| 4209 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4210 | << R; |
| 4211 | else |
| 4212 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4213 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4214 | break; |
| 4215 | } |
| 4216 | |
| 4217 | case FK_ReferenceBindingToInitList: |
| 4218 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4219 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4220 | break; |
| 4221 | |
| 4222 | case FK_InitListBadDestinationType: |
| 4223 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4224 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4225 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4227 | case FK_ConstructorOverloadFailed: { |
| 4228 | SourceRange ArgsRange; |
| 4229 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4231 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4233 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4234 | // bad. |
| 4235 | switch (FailedOverloadResult) { |
| 4236 | case OR_Ambiguous: |
| 4237 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4238 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4239 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4240 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4241 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4242 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4243 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4244 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4245 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4246 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4247 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4248 | // This is implicit default initialization of a member or |
| 4249 | // base within a constructor. If no viable function was |
| 4250 | // found, notify the user that she needs to explicitly |
| 4251 | // initialize this base/member. |
| 4252 | CXXConstructorDecl *Constructor |
| 4253 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4254 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4255 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4256 | << Constructor->isImplicit() |
| 4257 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4258 | << /*base=*/0 |
| 4259 | << Entity.getType(); |
| 4260 | |
| 4261 | RecordDecl *BaseDecl |
| 4262 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4263 | ->getDecl(); |
| 4264 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4265 | << S.Context.getTagDeclType(BaseDecl); |
| 4266 | } else { |
| 4267 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4268 | << Constructor->isImplicit() |
| 4269 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4270 | << /*member=*/1 |
| 4271 | << Entity.getName(); |
| 4272 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4273 | |
| 4274 | if (const RecordType *Record |
| 4275 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4277 | diag::note_previous_decl) |
| 4278 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4279 | } |
| 4280 | break; |
| 4281 | } |
| 4282 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4283 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4284 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4285 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4286 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4287 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4288 | case OR_Deleted: { |
| 4289 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4290 | << true << DestType << ArgsRange; |
| 4291 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4292 | OverloadingResult Ovl |
| 4293 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4294 | if (Ovl == OR_Deleted) { |
| 4295 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4296 | << Best->Function->isDeleted(); |
| 4297 | } else { |
| 4298 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4299 | } |
| 4300 | break; |
| 4301 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4303 | case OR_Success: |
| 4304 | llvm_unreachable("Conversion did not fail!"); |
| 4305 | break; |
| 4306 | } |
| 4307 | break; |
| 4308 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4310 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4311 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4312 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4313 | // This is implicit default-initialization of a const member in |
| 4314 | // a constructor. Complain that it needs to be explicitly |
| 4315 | // initialized. |
| 4316 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4317 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4318 | << Constructor->isImplicit() |
| 4319 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4320 | << /*const=*/1 |
| 4321 | << Entity.getName(); |
| 4322 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4323 | << Entity.getName(); |
| 4324 | } else { |
| 4325 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4326 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4327 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4328 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4330 | case FK_Incomplete: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4332 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4333 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4334 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4336 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4337 | return true; |
| 4338 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4340 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 4341 | switch (SequenceKind) { |
| 4342 | case FailedSequence: { |
| 4343 | OS << "Failed sequence: "; |
| 4344 | switch (Failure) { |
| 4345 | case FK_TooManyInitsForReference: |
| 4346 | OS << "too many initializers for reference"; |
| 4347 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4348 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4349 | case FK_ArrayNeedsInitList: |
| 4350 | OS << "array requires initializer list"; |
| 4351 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4353 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4354 | OS << "array requires initializer list or string literal"; |
| 4355 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4357 | case FK_AddressOfOverloadFailed: |
| 4358 | OS << "address of overloaded function failed"; |
| 4359 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4360 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4361 | case FK_ReferenceInitOverloadFailed: |
| 4362 | OS << "overload resolution for reference initialization failed"; |
| 4363 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4364 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4365 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4366 | OS << "non-const lvalue reference bound to temporary"; |
| 4367 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4369 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4370 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4371 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4373 | case FK_RValueReferenceBindingToLValue: |
| 4374 | OS << "rvalue reference bound to an lvalue"; |
| 4375 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4377 | case FK_ReferenceInitDropsQualifiers: |
| 4378 | OS << "reference initialization drops qualifiers"; |
| 4379 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4381 | case FK_ReferenceInitFailed: |
| 4382 | OS << "reference initialization failed"; |
| 4383 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4385 | case FK_ConversionFailed: |
| 4386 | OS << "conversion failed"; |
| 4387 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4388 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4389 | case FK_TooManyInitsForScalar: |
| 4390 | OS << "too many initializers for scalar"; |
| 4391 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4393 | case FK_ReferenceBindingToInitList: |
| 4394 | OS << "referencing binding to initializer list"; |
| 4395 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4397 | case FK_InitListBadDestinationType: |
| 4398 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 4399 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4401 | case FK_UserConversionOverloadFailed: |
| 4402 | OS << "overloading failed for user-defined conversion"; |
| 4403 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4405 | case FK_ConstructorOverloadFailed: |
| 4406 | OS << "constructor overloading failed"; |
| 4407 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4409 | case FK_DefaultInitOfConst: |
| 4410 | OS << "default initialization of a const variable"; |
| 4411 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4412 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4413 | case FK_Incomplete: |
| 4414 | OS << "initialization of incomplete type"; |
| 4415 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4416 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4417 | OS << '\n'; |
| 4418 | return; |
| 4419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4421 | case DependentSequence: |
| 4422 | OS << "Dependent sequence: "; |
| 4423 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4425 | case UserDefinedConversion: |
| 4426 | OS << "User-defined conversion sequence: "; |
| 4427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4429 | case ConstructorInitialization: |
| 4430 | OS << "Constructor initialization sequence: "; |
| 4431 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4433 | case ReferenceBinding: |
| 4434 | OS << "Reference binding: "; |
| 4435 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4437 | case ListInitialization: |
| 4438 | OS << "List initialization: "; |
| 4439 | break; |
| 4440 | |
| 4441 | case ZeroInitialization: |
| 4442 | OS << "Zero initialization\n"; |
| 4443 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4445 | case NoInitialization: |
| 4446 | OS << "No initialization\n"; |
| 4447 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4449 | case StandardConversion: |
| 4450 | OS << "Standard conversion: "; |
| 4451 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4453 | case CAssignment: |
| 4454 | OS << "C assignment: "; |
| 4455 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4457 | case StringInit: |
| 4458 | OS << "String initialization: "; |
| 4459 | break; |
| 4460 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4461 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4462 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 4463 | if (S != step_begin()) { |
| 4464 | OS << " -> "; |
| 4465 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4467 | switch (S->Kind) { |
| 4468 | case SK_ResolveAddressOfOverloadedFunction: |
| 4469 | OS << "resolve address of overloaded function"; |
| 4470 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4472 | case SK_CastDerivedToBaseRValue: |
| 4473 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 4474 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4476 | case SK_CastDerivedToBaseXValue: |
| 4477 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 4478 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4479 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4480 | case SK_CastDerivedToBaseLValue: |
| 4481 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 4482 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4483 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4484 | case SK_BindReference: |
| 4485 | OS << "bind reference to lvalue"; |
| 4486 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4488 | case SK_BindReferenceToTemporary: |
| 4489 | OS << "bind reference to a temporary"; |
| 4490 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4492 | case SK_ExtraneousCopyToTemporary: |
| 4493 | OS << "extraneous C++03 copy to temporary"; |
| 4494 | break; |
| 4495 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4496 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4497 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4498 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4499 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4500 | case SK_QualificationConversionRValue: |
| 4501 | OS << "qualification conversion (rvalue)"; |
| 4502 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4503 | case SK_QualificationConversionXValue: |
| 4504 | OS << "qualification conversion (xvalue)"; |
| 4505 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4506 | case SK_QualificationConversionLValue: |
| 4507 | OS << "qualification conversion (lvalue)"; |
| 4508 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4509 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4510 | case SK_ConversionSequence: |
| 4511 | OS << "implicit conversion sequence ("; |
| 4512 | S->ICS->DebugPrint(); // FIXME: use OS |
| 4513 | OS << ")"; |
| 4514 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4516 | case SK_ListInitialization: |
| 4517 | OS << "list initialization"; |
| 4518 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4520 | case SK_ConstructorInitialization: |
| 4521 | OS << "constructor initialization"; |
| 4522 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4524 | case SK_ZeroInitialization: |
| 4525 | OS << "zero initialization"; |
| 4526 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4528 | case SK_CAssignment: |
| 4529 | OS << "C assignment"; |
| 4530 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4532 | case SK_StringInit: |
| 4533 | OS << "string initialization"; |
| 4534 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4535 | |
| 4536 | case SK_ObjCObjectConversion: |
| 4537 | OS << "Objective-C object conversion"; |
| 4538 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | void InitializationSequence::dump() const { |
| 4544 | dump(llvm::errs()); |
| 4545 | } |
| 4546 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4547 | //===----------------------------------------------------------------------===// |
| 4548 | // Initialization helper functions |
| 4549 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4551 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 4552 | SourceLocation EqualLoc, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4553 | ExprResult Init) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4554 | if (Init.isInvalid()) |
| 4555 | return ExprError(); |
| 4556 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4557 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4558 | assert(InitE && "No initialization expression?"); |
| 4559 | |
| 4560 | if (EqualLoc.isInvalid()) |
| 4561 | EqualLoc = InitE->getLocStart(); |
| 4562 | |
| 4563 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 4564 | EqualLoc); |
| 4565 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 4566 | Init.release(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4567 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4568 | } |