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 | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 95 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 96 | // the size may be smaller or larger than the string we are initializing. |
| 97 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 98 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 99 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 100 | // For Pascal strings it's OK to strip off the terminating null character, |
| 101 | // so the example below is valid: |
| 102 | // |
| 103 | // unsigned char a[2] = "\pa"; |
| 104 | if (SL->isPascal()) |
| 105 | StrLength--; |
| 106 | } |
| 107 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 108 | // [dcl.init.string]p2 |
| 109 | if (StrLength > CAT->getSize().getZExtValue()) |
| 110 | S.Diag(Str->getSourceRange().getBegin(), |
| 111 | diag::err_initializer_string_for_char_array_too_long) |
| 112 | << Str->getSourceRange(); |
| 113 | } else { |
| 114 | // C99 6.7.8p14. |
| 115 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 116 | S.Diag(Str->getSourceRange().getBegin(), |
| 117 | diag::warn_initializer_string_for_char_array_too_long) |
| 118 | << Str->getSourceRange(); |
| 119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 121 | // Set the type to the actual size that we are initializing. If we have |
| 122 | // something like: |
| 123 | // char x[1] = "foo"; |
| 124 | // then this will set the string literal's type to char[1]. |
| 125 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 128 | //===----------------------------------------------------------------------===// |
| 129 | // Semantic checking for initializer lists. |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 132 | /// @brief Semantic checking for initializer lists. |
| 133 | /// |
| 134 | /// The InitListChecker class contains a set of routines that each |
| 135 | /// handle the initialization of a certain kind of entity, e.g., |
| 136 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 137 | /// InitListChecker itself performs a recursive walk of the subobject |
| 138 | /// structure of the type to be initialized, while stepping through |
| 139 | /// the initializer list one element at a time. The IList and Index |
| 140 | /// parameters to each of the Check* routines contain the active |
| 141 | /// (syntactic) initializer list and the index into that initializer |
| 142 | /// list that represents the current initializer. Each routine is |
| 143 | /// responsible for moving that Index forward as it consumes elements. |
| 144 | /// |
| 145 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 146 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 147 | /// initializer list and the index into that initializer list where we |
| 148 | /// are copying initializers as we map them over to the semantic |
| 149 | /// list. Once we have completed our recursive walk of the subobject |
| 150 | /// structure, we will have constructed a full semantic initializer |
| 151 | /// list. |
| 152 | /// |
| 153 | /// C99 designators cause changes in the initializer list traversal, |
| 154 | /// because they make the initialization "jump" into a specific |
| 155 | /// subobject and then continue the initialization from that |
| 156 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 157 | /// designated subobject and manages backing out the recursion to |
| 158 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 159 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 160 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 161 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 162 | bool hadError; |
| 163 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 164 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 166 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 167 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 168 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 169 | unsigned &StructuredIndex, |
| 170 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 171 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 172 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 173 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 174 | unsigned &StructuredIndex, |
| 175 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 176 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 177 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 179 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 180 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 181 | unsigned &StructuredIndex, |
| 182 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 183 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 184 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 185 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 186 | InitListExpr *StructuredList, |
| 187 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 188 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 189 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 190 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 191 | InitListExpr *StructuredList, |
| 192 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckReferenceType(const InitializedEntity &Entity, |
| 194 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 195 | unsigned &Index, |
| 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 198 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 199 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 200 | InitListExpr *StructuredList, |
| 201 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 202 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 203 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 205 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 207 | unsigned &StructuredIndex, |
| 208 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 209 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 210 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 212 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 213 | InitListExpr *StructuredList, |
| 214 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 215 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 216 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 217 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | RecordDecl::field_iterator *NextField, |
| 220 | llvm::APSInt *NextElementIndex, |
| 221 | unsigned &Index, |
| 222 | InitListExpr *StructuredList, |
| 223 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 224 | bool FinishSubobjectInit, |
| 225 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 226 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 227 | QualType CurrentObjectType, |
| 228 | InitListExpr *StructuredList, |
| 229 | unsigned StructuredIndex, |
| 230 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 231 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 232 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 233 | Expr *expr); |
| 234 | int numArrayElements(QualType DeclType); |
| 235 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 237 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 238 | const InitializedEntity &ParentEntity, |
| 239 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 240 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 241 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 242 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 243 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 244 | InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 245 | bool HadError() { return hadError; } |
| 246 | |
| 247 | // @brief Retrieves the fully-structured initializer list used for |
| 248 | // semantic analysis and code generation. |
| 249 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 250 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 251 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 253 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 254 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 255 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 256 | bool &RequiresSecondPass) { |
| 257 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 258 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 260 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 261 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 262 | // FIXME: We probably don't need to handle references |
| 263 | // specially here, since value-initialization of references is |
| 264 | // handled in InitializationSequence. |
| 265 | if (Field->getType()->isReferenceType()) { |
| 266 | // C++ [dcl.init.aggr]p9: |
| 267 | // If an incomplete or empty initializer-list leaves a |
| 268 | // member of reference type uninitialized, the program is |
| 269 | // ill-formed. |
| 270 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 271 | << Field->getType() |
| 272 | << ILE->getSyntacticForm()->getSourceRange(); |
| 273 | SemaRef.Diag(Field->getLocation(), |
| 274 | diag::note_uninit_reference_member); |
| 275 | hadError = true; |
| 276 | return; |
| 277 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 279 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 280 | true); |
| 281 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 282 | if (!InitSeq) { |
| 283 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 284 | hadError = true; |
| 285 | return; |
| 286 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 288 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 289 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 290 | if (MemberInit.isInvalid()) { |
| 291 | hadError = true; |
| 292 | return; |
| 293 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 295 | if (hadError) { |
| 296 | // Do nothing |
| 297 | } else if (Init < NumInits) { |
| 298 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 299 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 300 | // Value-initialization requires a constructor call, so |
| 301 | // extend the initializer list to include the constructor |
| 302 | // call and make a note that we'll need to take another pass |
| 303 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 304 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 305 | RequiresSecondPass = true; |
| 306 | } |
| 307 | } else if (InitListExpr *InnerILE |
| 308 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 309 | FillInValueInitializations(MemberEntity, InnerILE, |
| 310 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 313 | /// Recursively replaces NULL values within the given initializer list |
| 314 | /// with expressions that perform value-initialization of the |
| 315 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 316 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 317 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 318 | InitListExpr *ILE, |
| 319 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 321 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 322 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 323 | if (ILE->getSyntacticForm()) |
| 324 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 326 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 327 | if (RType->getDecl()->isUnion() && |
| 328 | ILE->getInitializedFieldInUnion()) |
| 329 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 330 | Entity, ILE, RequiresSecondPass); |
| 331 | else { |
| 332 | unsigned Init = 0; |
| 333 | for (RecordDecl::field_iterator |
| 334 | Field = RType->getDecl()->field_begin(), |
| 335 | FieldEnd = RType->getDecl()->field_end(); |
| 336 | Field != FieldEnd; ++Field) { |
| 337 | if (Field->isUnnamedBitfield()) |
| 338 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 340 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 341 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 342 | |
| 343 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 344 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 345 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 347 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 349 | // Only look at the first initialization of a union. |
| 350 | if (RType->getDecl()->isUnion()) |
| 351 | break; |
| 352 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 357 | |
| 358 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 360 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 361 | unsigned NumInits = ILE->getNumInits(); |
| 362 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 363 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 364 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 365 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 366 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 367 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 368 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 369 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 370 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 371 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 372 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 373 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 375 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 378 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 379 | if (hadError) |
| 380 | return; |
| 381 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 382 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 383 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 384 | ElementEntity.setElementIndex(Init); |
| 385 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 386 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 387 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 388 | true); |
| 389 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 390 | if (!InitSeq) { |
| 391 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 392 | hadError = true; |
| 393 | return; |
| 394 | } |
| 395 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 396 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 397 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 398 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 399 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | if (hadError) { |
| 404 | // Do nothing |
| 405 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 406 | // For arrays, just set the expression used for value-initialization |
| 407 | // of the "holes" in the array. |
| 408 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 409 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 410 | else |
| 411 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 412 | } else { |
| 413 | // For arrays, just set the expression used for value-initialization |
| 414 | // of the rest of elements and exit. |
| 415 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 416 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 417 | return; |
| 418 | } |
| 419 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 420 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 421 | // Value-initialization requires a constructor call, so |
| 422 | // extend the initializer list to include the constructor |
| 423 | // call and make a note that we'll need to take another pass |
| 424 | // through the initializer list. |
| 425 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 426 | RequiresSecondPass = true; |
| 427 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 428 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 429 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 430 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 431 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 435 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 436 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 437 | InitListExpr *IL, QualType &T) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 438 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 439 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 440 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 441 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 442 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 444 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 446 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 447 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 449 | if (!hadError) { |
| 450 | bool RequiresSecondPass = false; |
| 451 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 452 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 453 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 454 | RequiresSecondPass); |
| 455 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 459 | // FIXME: use a proper constant |
| 460 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 461 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 462 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 463 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 464 | } |
| 465 | return maxElements; |
| 466 | } |
| 467 | |
| 468 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 469 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 470 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 472 | Field = structDecl->field_begin(), |
| 473 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 474 | Field != FieldEnd; ++Field) { |
| 475 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 476 | ++InitializableMembers; |
| 477 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 478 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 479 | return std::min(InitializableMembers, 1); |
| 480 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 483 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 484 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 485 | QualType T, unsigned &Index, |
| 486 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 487 | unsigned &StructuredIndex, |
| 488 | bool TopLevelObject) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 489 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 491 | if (T->isArrayType()) |
| 492 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 493 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 494 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 495 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 496 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 497 | else |
| 498 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 499 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 500 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 501 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 502 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 503 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 504 | hadError = true; |
| 505 | return; |
| 506 | } |
| 507 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 508 | // Build a structured initializer list corresponding to this subobject. |
| 509 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 511 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 512 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 513 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 514 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 516 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 517 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 518 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 519 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | StructuredSubobjectInitList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 521 | StructuredSubobjectInitIndex, |
| 522 | TopLevelObject); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 523 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 524 | StructuredSubobjectInitList->setType(T); |
| 525 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 526 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 527 | // range corresponds with the end of the last initializer it used. |
| 528 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 530 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 531 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 533 | |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 534 | // Warn about missing braces. |
| 535 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 47f164e | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 536 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 537 | diag::warn_missing_braces) |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 538 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 539 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 540 | "{") |
| 541 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 542 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 543 | "}"); |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 544 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 547 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 548 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 549 | unsigned &Index, |
| 550 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 551 | unsigned &StructuredIndex, |
| 552 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 553 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 554 | SyntacticToSemantic[IList] = StructuredList; |
| 555 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 556 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 557 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 558 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 559 | IList->setType(ExprTy); |
| 560 | StructuredList->setType(ExprTy); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 561 | if (hadError) |
| 562 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 563 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 564 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 565 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 566 | if (StructuredIndex == 1 && |
| 567 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 568 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 569 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 570 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 571 | hadError = true; |
| 572 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 573 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 574 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 575 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 576 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 577 | // Don't complain for incomplete types, since we'll get an error |
| 578 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 579 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 581 | CurrentObjectType->isArrayType()? 0 : |
| 582 | CurrentObjectType->isVectorType()? 1 : |
| 583 | CurrentObjectType->isScalarType()? 2 : |
| 584 | CurrentObjectType->isUnionType()? 3 : |
| 585 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 586 | |
| 587 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 588 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 589 | DK = diag::err_excess_initializers; |
| 590 | hadError = true; |
| 591 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 592 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 593 | DK = diag::err_excess_initializers; |
| 594 | hadError = true; |
| 595 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 597 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 598 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 601 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 602 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 603 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 604 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 605 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 606 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 609 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 610 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 612 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 613 | unsigned &Index, |
| 614 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 615 | unsigned &StructuredIndex, |
| 616 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 617 | if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 618 | CheckScalarType(Entity, IList, DeclType, Index, |
| 619 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 620 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 621 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 622 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 623 | } else if (DeclType->isAggregateType()) { |
| 624 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 625 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 626 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 627 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 628 | StructuredList, StructuredIndex, |
| 629 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 630 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 631 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 632 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 633 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 635 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 636 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 637 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 638 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 639 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 640 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 641 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 642 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 643 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 644 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 645 | } else if (DeclType->isRecordType()) { |
| 646 | // C++ [dcl.init]p14: |
| 647 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 648 | // is a brace-enclosed list, see 8.5.1. |
| 649 | // |
| 650 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 651 | // we have an initializer list and a destination type that is not |
| 652 | // an aggregate. |
| 653 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 654 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 655 | << DeclType << IList->getSourceRange(); |
| 656 | hadError = true; |
| 657 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 658 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 659 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 660 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 661 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 662 | << DeclType; |
| 663 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 664 | } else { |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 665 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 666 | << DeclType; |
| 667 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 671 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 672 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 674 | unsigned &Index, |
| 675 | InitListExpr *StructuredList, |
| 676 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 677 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 678 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 679 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 680 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 682 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 683 | StructuredList, StructuredIndex, |
| 684 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 685 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 686 | newStructuredList, newStructuredIndex); |
| 687 | ++StructuredIndex; |
| 688 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 689 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 690 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 691 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 692 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 693 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 694 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 695 | StructuredList, StructuredIndex); |
| 696 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 697 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 698 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 699 | // arrayType can be incomplete if we're initializing a flexible |
| 700 | // array member. There's nothing we can do with the completed |
| 701 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 702 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 703 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 704 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 705 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 706 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 707 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 708 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 709 | |
| 710 | // Fall through for subaggregate initialization. |
| 711 | |
| 712 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 713 | // C++ [dcl.init.aggr]p12: |
| 714 | // All implicit type conversions (clause 4) are considered when |
| 715 | // initializing the aggregate member with an ini- tializer from |
| 716 | // an initializer-list. If the initializer can initialize a |
| 717 | // member, the member is initialized. [...] |
| 718 | |
| 719 | // FIXME: Better EqualLoc? |
| 720 | InitializationKind Kind = |
| 721 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 722 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 723 | |
| 724 | if (Seq) { |
| 725 | ExprResult Result = |
| 726 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 727 | if (Result.isInvalid()) |
| 728 | hadError = true; |
| 729 | |
| 730 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 731 | Result.takeAs<Expr>()); |
| 732 | ++Index; |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | // Fall through for subaggregate initialization |
| 737 | } else { |
| 738 | // C99 6.7.8p13: |
| 739 | // |
| 740 | // The initializer for a structure or union object that has |
| 741 | // automatic storage duration shall be either an initializer |
| 742 | // list as described below, or a single expression that has |
| 743 | // compatible structure or union type. In the latter case, the |
| 744 | // initial value of the object, including unnamed members, is |
| 745 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 746 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 747 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 748 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 749 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 750 | if (ExprRes.isInvalid()) |
| 751 | hadError = true; |
| 752 | else { |
| 753 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 754 | if (ExprRes.isInvalid()) |
| 755 | hadError = true; |
| 756 | } |
| 757 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 758 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 759 | ++Index; |
| 760 | return; |
| 761 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 762 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 763 | // Fall through for subaggregate initialization |
| 764 | } |
| 765 | |
| 766 | // C++ [dcl.init.aggr]p12: |
| 767 | // |
| 768 | // [...] Otherwise, if the member is itself a non-empty |
| 769 | // subaggregate, brace elision is assumed and the initializer is |
| 770 | // considered for the initialization of the first member of |
| 771 | // the subaggregate. |
| 772 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 773 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 774 | StructuredIndex); |
| 775 | ++StructuredIndex; |
| 776 | } else { |
| 777 | // We cannot initialize this element, so let |
| 778 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 779 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 780 | SemaRef.Owned(expr)); |
| 781 | hadError = true; |
| 782 | ++Index; |
| 783 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 784 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 787 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 788 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 789 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 790 | InitListExpr *StructuredList, |
| 791 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 792 | if (Index >= IList->getNumInits()) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 793 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 794 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 795 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 796 | ++Index; |
| 797 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 798 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 799 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 800 | |
| 801 | Expr *expr = IList->getInit(Index); |
| 802 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 803 | SemaRef.Diag(SubIList->getLocStart(), |
| 804 | diag::warn_many_braces_around_scalar_init) |
| 805 | << SubIList->getSourceRange(); |
| 806 | |
| 807 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 808 | StructuredIndex); |
| 809 | return; |
| 810 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 811 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 812 | diag::err_designator_for_scalar_init) |
| 813 | << DeclType << expr->getSourceRange(); |
| 814 | hadError = true; |
| 815 | ++Index; |
| 816 | ++StructuredIndex; |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | ExprResult Result = |
| 821 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 822 | SemaRef.Owned(expr)); |
| 823 | |
| 824 | Expr *ResultExpr = 0; |
| 825 | |
| 826 | if (Result.isInvalid()) |
| 827 | hadError = true; // types weren't compatible. |
| 828 | else { |
| 829 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 830 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 831 | if (ResultExpr != expr) { |
| 832 | // The type was promoted, update initializer list. |
| 833 | IList->setInit(Index, ResultExpr); |
| 834 | } |
| 835 | } |
| 836 | if (hadError) |
| 837 | ++StructuredIndex; |
| 838 | else |
| 839 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 840 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 843 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 844 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 845 | unsigned &Index, |
| 846 | InitListExpr *StructuredList, |
| 847 | unsigned &StructuredIndex) { |
| 848 | if (Index < IList->getNumInits()) { |
| 849 | Expr *expr = IList->getInit(Index); |
| 850 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 851 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 852 | << DeclType << IList->getSourceRange(); |
| 853 | hadError = true; |
| 854 | ++Index; |
| 855 | ++StructuredIndex; |
| 856 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 858 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 859 | ExprResult Result = |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 860 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 861 | SemaRef.Owned(expr)); |
| 862 | |
| 863 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 864 | hadError = true; |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 865 | |
| 866 | expr = Result.takeAs<Expr>(); |
| 867 | IList->setInit(Index, expr); |
| 868 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 869 | if (hadError) |
| 870 | ++StructuredIndex; |
| 871 | else |
| 872 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 873 | ++Index; |
| 874 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 875 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 876 | // general, it would be useful to pass location information down the stack, |
| 877 | // so that we know the location (or decl) of the "current object" being |
| 878 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 880 | diag::err_init_reference_member_uninitialized) |
| 881 | << DeclType |
| 882 | << IList->getSourceRange(); |
| 883 | hadError = true; |
| 884 | ++Index; |
| 885 | ++StructuredIndex; |
| 886 | return; |
| 887 | } |
| 888 | } |
| 889 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 890 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 891 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 892 | unsigned &Index, |
| 893 | InitListExpr *StructuredList, |
| 894 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 895 | if (Index >= IList->getNumInits()) |
| 896 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 898 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 899 | unsigned maxElements = VT->getNumElements(); |
| 900 | unsigned numEltsInit = 0; |
| 901 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 902 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 903 | if (!SemaRef.getLangOptions().OpenCL) { |
| 904 | // If the initializing element is a vector, try to copy-initialize |
| 905 | // instead of breaking it apart (which is doomed to failure anyway). |
| 906 | Expr *Init = IList->getInit(Index); |
| 907 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 908 | ExprResult Result = |
| 909 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
| 910 | SemaRef.Owned(Init)); |
| 911 | |
| 912 | Expr *ResultExpr = 0; |
| 913 | if (Result.isInvalid()) |
| 914 | hadError = true; // types weren't compatible. |
| 915 | else { |
| 916 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 917 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 918 | if (ResultExpr != Init) { |
| 919 | // The type was promoted, update initializer list. |
| 920 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 923 | if (hadError) |
| 924 | ++StructuredIndex; |
| 925 | else |
| 926 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 927 | ++Index; |
| 928 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 931 | InitializedEntity ElementEntity = |
| 932 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 933 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 934 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 935 | // Don't attempt to go past the end of the init list |
| 936 | if (Index >= IList->getNumInits()) |
| 937 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 938 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 939 | ElementEntity.setElementIndex(Index); |
| 940 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 941 | StructuredList, StructuredIndex); |
| 942 | } |
| 943 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 944 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 945 | |
| 946 | InitializedEntity ElementEntity = |
| 947 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 948 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 949 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 950 | for (unsigned i = 0; i < maxElements; ++i) { |
| 951 | // Don't attempt to go past the end of the init list |
| 952 | if (Index >= IList->getNumInits()) |
| 953 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 955 | ElementEntity.setElementIndex(Index); |
| 956 | |
| 957 | QualType IType = IList->getInit(Index)->getType(); |
| 958 | if (!IType->isVectorType()) { |
| 959 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 960 | StructuredList, StructuredIndex); |
| 961 | ++numEltsInit; |
| 962 | } else { |
| 963 | QualType VecType; |
| 964 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 965 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 966 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 967 | if (IType->isExtVectorType()) |
| 968 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 969 | else |
| 970 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 971 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 972 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 973 | StructuredList, StructuredIndex); |
| 974 | numEltsInit += numIElts; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | // OpenCL requires all elements to be initialized. |
| 979 | if (numEltsInit != maxElements) |
| 980 | if (SemaRef.getLangOptions().OpenCL) |
| 981 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 982 | diag::err_vector_incorrect_num_initializers) |
| 983 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 986 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 987 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 988 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 990 | unsigned &Index, |
| 991 | InitListExpr *StructuredList, |
| 992 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 993 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 994 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 995 | // Check for the special-case of initializing an array with a string. |
| 996 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 997 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 998 | SemaRef.Context)) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 999 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1000 | // We place the string literal directly into the resulting |
| 1001 | // initializer list. This is the only place where the structure |
| 1002 | // of the structured initializer list doesn't match exactly, |
| 1003 | // because doing so would involve allocating one character |
| 1004 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1005 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1006 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1007 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1008 | return; |
| 1009 | } |
| 1010 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1011 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1012 | // Check for VLAs; in standard C it would be possible to check this |
| 1013 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1014 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1015 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1016 | diag::err_variable_object_no_init) |
| 1017 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1018 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1019 | ++Index; |
| 1020 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1021 | return; |
| 1022 | } |
| 1023 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1024 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1025 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1026 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1027 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1028 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1029 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1030 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1031 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1032 | maxElementsKnown = true; |
| 1033 | } |
| 1034 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1035 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1036 | while (Index < IList->getNumInits()) { |
| 1037 | Expr *Init = IList->getInit(Index); |
| 1038 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1039 | // If we're not the subobject that matches up with the '{' for |
| 1040 | // the designator, we shouldn't be handling the |
| 1041 | // designator. Return immediately. |
| 1042 | if (!SubobjectIsDesignatorContext) |
| 1043 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1045 | // Handle this designated initializer. elementIndex will be |
| 1046 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1047 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1048 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1049 | StructuredList, StructuredIndex, true, |
| 1050 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1051 | hadError = true; |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1055 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1056 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1057 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1058 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1059 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1061 | // If the array is of incomplete type, keep track of the number of |
| 1062 | // elements in the initializer. |
| 1063 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1064 | maxElements = elementIndex; |
| 1065 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1066 | continue; |
| 1067 | } |
| 1068 | |
| 1069 | // If we know the maximum number of elements, and we've already |
| 1070 | // hit it, stop consuming elements in the initializer list. |
| 1071 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1072 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1073 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1074 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1075 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1076 | Entity); |
| 1077 | // Check this element. |
| 1078 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1079 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1080 | ++elementIndex; |
| 1081 | |
| 1082 | // If the array is of incomplete type, keep track of the number of |
| 1083 | // elements in the initializer. |
| 1084 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1085 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1086 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1087 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1088 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1089 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1090 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1091 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1092 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1093 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1094 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1095 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1096 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1097 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1099 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1103 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1104 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1106 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1108 | unsigned &Index, |
| 1109 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1110 | unsigned &StructuredIndex, |
| 1111 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1112 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1114 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1115 | // confusion, we forgo checking the intializer for the entire record. |
| 1116 | if (structDecl->isInvalidDecl()) { |
| 1117 | hadError = true; |
| 1118 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1122 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1123 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1124 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1125 | Field != FieldEnd; ++Field) { |
| 1126 | if (Field->getDeclName()) { |
| 1127 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | return; |
| 1132 | } |
| 1133 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1134 | // If structDecl is a forward declaration, this loop won't do |
| 1135 | // anything except look at designated initializers; That's okay, |
| 1136 | // because an error should get printed out elsewhere. It might be |
| 1137 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1138 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1139 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1140 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1141 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1142 | while (Index < IList->getNumInits()) { |
| 1143 | Expr *Init = IList->getInit(Index); |
| 1144 | |
| 1145 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1146 | // If we're not the subobject that matches up with the '{' for |
| 1147 | // the designator, we shouldn't be handling the |
| 1148 | // designator. Return immediately. |
| 1149 | if (!SubobjectIsDesignatorContext) |
| 1150 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1152 | // Handle this designated initializer. Field will be updated to |
| 1153 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1154 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1155 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1156 | StructuredList, StructuredIndex, |
| 1157 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1158 | hadError = true; |
| 1159 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1160 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Disable check for missing fields when designators are used. |
| 1163 | // This matches gcc behaviour. |
| 1164 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if (Field == FieldEnd) { |
| 1169 | // We've run out of fields. We're done. |
| 1170 | break; |
| 1171 | } |
| 1172 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1173 | // We've already initialized a member of a union. We're done. |
| 1174 | if (InitializedSomething && DeclType->isUnionType()) |
| 1175 | break; |
| 1176 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1177 | // If we've hit the flexible array member at the end, we're done. |
| 1178 | if (Field->getType()->isIncompleteArrayType()) |
| 1179 | break; |
| 1180 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1181 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1182 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1183 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1184 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1185 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1187 | // Make sure we can use this declaration. |
| 1188 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1189 | IList->getInit(Index)->getLocStart())) { |
| 1190 | ++Index; |
| 1191 | ++Field; |
| 1192 | hadError = true; |
| 1193 | continue; |
| 1194 | } |
| 1195 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1196 | InitializedEntity MemberEntity = |
| 1197 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1198 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1199 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1200 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (DeclType->isUnionType()) { |
| 1203 | // Initialize the first field within the union. |
| 1204 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1205 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1206 | |
| 1207 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1208 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1209 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1210 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1211 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1212 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1213 | // It is possible we have one or more unnamed bitfields remaining. |
| 1214 | // Find first (if any) named field and emit warning. |
| 1215 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1216 | it != end; ++it) { |
| 1217 | if (!it->isUnnamedBitfield()) { |
| 1218 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1219 | diag::warn_missing_field_initializers) << it->getName(); |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1226 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | |
| 1229 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | if (!TopLevelObject && |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1231 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1232 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1234 | diag::err_flexible_array_init_nonempty) |
| 1235 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1236 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1237 | << *Field; |
| 1238 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1239 | ++Index; |
| 1240 | return; |
| 1241 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1243 | diag::ext_flexible_array_init) |
| 1244 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1245 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1246 | << *Field; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1249 | InitializedEntity MemberEntity = |
| 1250 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1252 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1254 | StructuredList, StructuredIndex); |
| 1255 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1256 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1257 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1258 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1260 | /// \brief Expand a field designator that refers to a member of an |
| 1261 | /// anonymous struct or union into a series of field designators that |
| 1262 | /// refers to the field within the appropriate subobject. |
| 1263 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1264 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | DesignatedInitExpr *DIE, |
| 1266 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1267 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1268 | typedef DesignatedInitExpr::Designator Designator; |
| 1269 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1270 | // Build the replacement designators. |
| 1271 | llvm::SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1272 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1273 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1274 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1276 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1277 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1278 | else |
| 1279 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1280 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1281 | assert(isa<FieldDecl>(*PI)); |
| 1282 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | // Expand the current designator into the set of replacement |
| 1286 | // designators, so we have a full subobject path down to where the |
| 1287 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1288 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1289 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1292 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1293 | /// corresponds to FieldName. |
| 1294 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1295 | IdentifierInfo *FieldName) { |
| 1296 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1297 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1298 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1299 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1300 | return IF; |
| 1301 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1302 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1303 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1306 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1307 | /// |
| 1308 | /// Determines whether the designated initializer @p DIE, which |
| 1309 | /// resides at the given @p Index within the initializer list @p |
| 1310 | /// IList, is well-formed for a current object of type @p DeclType |
| 1311 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1313 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1314 | /// |
| 1315 | /// @param IList The initializer list in which this designated |
| 1316 | /// initializer occurs. |
| 1317 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1318 | /// @param DIE The designated initializer expression. |
| 1319 | /// |
| 1320 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1321 | /// |
| 1322 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1323 | /// into which the designation in @p DIE should refer. |
| 1324 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1325 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1326 | /// a field, this will be set to the field declaration corresponding |
| 1327 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1328 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1329 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1330 | /// DIE is an array designator or GNU array-range designator, this |
| 1331 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1332 | /// |
| 1333 | /// @param Index Index into @p IList where the designated initializer |
| 1334 | /// @p DIE occurs. |
| 1335 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1336 | /// @param StructuredList The initializer list expression that |
| 1337 | /// describes all of the subobject initializers in the order they'll |
| 1338 | /// actually be initialized. |
| 1339 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1340 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1342 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1343 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1345 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1346 | QualType &CurrentObjectType, |
| 1347 | RecordDecl::field_iterator *NextField, |
| 1348 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1349 | unsigned &Index, |
| 1350 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1351 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1352 | bool FinishSubobjectInit, |
| 1353 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1354 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1355 | // Check the actual initialization for the designated object type. |
| 1356 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Temporarily remove the designator expression from the |
| 1359 | // initializer list that the child calls see, so that we don't try |
| 1360 | // to re-process the designator. |
| 1361 | unsigned OldIndex = Index; |
| 1362 | IList->setInit(OldIndex, DIE->getInit()); |
| 1363 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1364 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1365 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Restore the designated initializer expression in the syntactic |
| 1368 | // form of the initializer list. |
| 1369 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1370 | DIE->setInit(IList->getInit(OldIndex)); |
| 1371 | IList->setInit(OldIndex, DIE); |
| 1372 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1373 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1376 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1378 | "Need a non-designated initializer list to start from"); |
| 1379 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1380 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1381 | // Determine the structural initializer list that corresponds to the |
| 1382 | // current subobject. |
| 1383 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1385 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1386 | SourceRange(D->getStartLocation(), |
| 1387 | DIE->getSourceRange().getEnd())); |
| 1388 | assert(StructuredList && "Expected a structured initializer list"); |
| 1389 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1390 | if (D->isFieldDesignator()) { |
| 1391 | // C99 6.7.8p7: |
| 1392 | // |
| 1393 | // If a designator has the form |
| 1394 | // |
| 1395 | // . identifier |
| 1396 | // |
| 1397 | // then the current object (defined below) shall have |
| 1398 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1400 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1401 | if (!RT) { |
| 1402 | SourceLocation Loc = D->getDotLoc(); |
| 1403 | if (Loc.isInvalid()) |
| 1404 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1405 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1406 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1407 | ++Index; |
| 1408 | return true; |
| 1409 | } |
| 1410 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1411 | // Note: we perform a linear search of the fields here, despite |
| 1412 | // the fact that we have a faster lookup method, because we always |
| 1413 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1414 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1415 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1416 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1418 | Field = RT->getDecl()->field_begin(), |
| 1419 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1420 | for (; Field != FieldEnd; ++Field) { |
| 1421 | if (Field->isUnnamedBitfield()) |
| 1422 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1423 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1424 | // If we find a field representing an anonymous field, look in the |
| 1425 | // IndirectFieldDecl that follow for the designated initializer. |
| 1426 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1427 | if (IndirectFieldDecl *IF = |
| 1428 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1429 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1430 | D = DIE->getDesignator(DesigIdx); |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1434 | if (KnownField && KnownField == *Field) |
| 1435 | break; |
| 1436 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | |
| 1439 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1442 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1443 | // There was no normal field in the struct with the designated |
| 1444 | // name. Perform another lookup for this name, which may find |
| 1445 | // something that we can't designate (e.g., a member function), |
| 1446 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1448 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1449 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1450 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1451 | // Name lookup didn't find anything. Determine whether this |
| 1452 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1453 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1454 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1455 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1456 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1457 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1458 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1459 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1460 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1461 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1462 | std::string CorrectedStr( |
| 1463 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1464 | std::string CorrectedQuotedStr( |
| 1465 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1466 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1467 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1468 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1469 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1471 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1474 | << FieldName << CurrentObjectType; |
| 1475 | ++Index; |
| 1476 | return true; |
| 1477 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1478 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1479 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1480 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1481 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1482 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1483 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1485 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1486 | ++Index; |
| 1487 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1488 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1489 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1490 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1491 | // The replacement field comes from typo correction; find it |
| 1492 | // in the list of fields. |
| 1493 | FieldIndex = 0; |
| 1494 | Field = RT->getDecl()->field_begin(); |
| 1495 | for (; Field != FieldEnd; ++Field) { |
| 1496 | if (Field->isUnnamedBitfield()) |
| 1497 | continue; |
| 1498 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1499 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1500 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1501 | break; |
| 1502 | |
| 1503 | ++FieldIndex; |
| 1504 | } |
| 1505 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1506 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1507 | |
| 1508 | // All of the fields of a union are located at the same place in |
| 1509 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1510 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1511 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1512 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1513 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1515 | // Make sure we can use this declaration. |
| 1516 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1517 | ++Index; |
| 1518 | return true; |
| 1519 | } |
| 1520 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1521 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1522 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1524 | // Make sure that our non-designated initializer list has space |
| 1525 | // for a subobject corresponding to this field. |
| 1526 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1527 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1529 | // This designator names a flexible array member. |
| 1530 | if (Field->getType()->isIncompleteArrayType()) { |
| 1531 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1532 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1533 | // We can't designate an object within the flexible array |
| 1534 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1536 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1538 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1540 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1541 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1542 | << *Field; |
| 1543 | Invalid = true; |
| 1544 | } |
| 1545 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1546 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1547 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1548 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1549 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1550 | diag::err_flexible_array_init_needs_braces) |
| 1551 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1552 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1553 | << *Field; |
| 1554 | Invalid = true; |
| 1555 | } |
| 1556 | |
| 1557 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1559 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1561 | diag::err_flexible_array_init_nonempty) |
| 1562 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1563 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1564 | << *Field; |
| 1565 | Invalid = true; |
| 1566 | } |
| 1567 | |
| 1568 | if (Invalid) { |
| 1569 | ++Index; |
| 1570 | return true; |
| 1571 | } |
| 1572 | |
| 1573 | // Initialize the array. |
| 1574 | bool prevHadError = hadError; |
| 1575 | unsigned newStructuredIndex = FieldIndex; |
| 1576 | unsigned OldIndex = Index; |
| 1577 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1578 | |
| 1579 | InitializedEntity MemberEntity = |
| 1580 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1581 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1582 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1583 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1584 | IList->setInit(OldIndex, DIE); |
| 1585 | if (hadError && !prevHadError) { |
| 1586 | ++Field; |
| 1587 | ++FieldIndex; |
| 1588 | if (NextField) |
| 1589 | *NextField = Field; |
| 1590 | StructuredIndex = FieldIndex; |
| 1591 | return true; |
| 1592 | } |
| 1593 | } else { |
| 1594 | // Recurse to check later designated subobjects. |
| 1595 | QualType FieldType = (*Field)->getType(); |
| 1596 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1597 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1598 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1599 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1600 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1601 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1602 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1603 | true, false)) |
| 1604 | return true; |
| 1605 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Find the position of the next field to be initialized in this |
| 1608 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1609 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1610 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1611 | |
| 1612 | // If this the first designator, our caller will continue checking |
| 1613 | // the rest of this struct/class/union subobject. |
| 1614 | if (IsFirstDesignator) { |
| 1615 | if (NextField) |
| 1616 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1617 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1618 | return false; |
| 1619 | } |
| 1620 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1621 | if (!FinishSubobjectInit) |
| 1622 | return false; |
| 1623 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1624 | // We've already initialized something in the union; we're done. |
| 1625 | if (RT->getDecl()->isUnion()) |
| 1626 | return hadError; |
| 1627 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1628 | // Check the remaining fields within this class/struct/union subobject. |
| 1629 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1630 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1631 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1632 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1633 | return hadError && !prevHadError; |
| 1634 | } |
| 1635 | |
| 1636 | // C99 6.7.8p6: |
| 1637 | // |
| 1638 | // If a designator has the form |
| 1639 | // |
| 1640 | // [ constant-expression ] |
| 1641 | // |
| 1642 | // then the current object (defined below) shall have array |
| 1643 | // type and the expression shall be an integer constant |
| 1644 | // expression. If the array is of unknown size, any |
| 1645 | // nonnegative value is valid. |
| 1646 | // |
| 1647 | // Additionally, cope with the GNU extension that permits |
| 1648 | // designators of the form |
| 1649 | // |
| 1650 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1651 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1652 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1653 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1654 | << CurrentObjectType; |
| 1655 | ++Index; |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
| 1659 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1660 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1661 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1662 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1663 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1664 | DesignatedEndIndex = DesignatedStartIndex; |
| 1665 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1666 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1669 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1671 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1672 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1674 | // Codegen can't handle evaluating array range designators that have side |
| 1675 | // effects, because we replicate the AST value for each initialized element. |
| 1676 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1677 | // elements with something that has a side effect, so codegen can emit an |
| 1678 | // "error unsupported" error instead of miscompiling the app. |
| 1679 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1680 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1681 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1684 | if (isa<ConstantArrayType>(AT)) { |
| 1685 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1686 | DesignatedStartIndex |
| 1687 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1688 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1689 | DesignatedEndIndex |
| 1690 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1691 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1692 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1693 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1694 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1695 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1696 | << IndexExpr->getSourceRange(); |
| 1697 | ++Index; |
| 1698 | return true; |
| 1699 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | // Make sure the bit-widths and signedness match. |
| 1702 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1703 | DesignatedEndIndex |
| 1704 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1705 | else if (DesignatedStartIndex.getBitWidth() < |
| 1706 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1707 | DesignatedStartIndex |
| 1708 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1709 | DesignatedStartIndex.setIsUnsigned(true); |
| 1710 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1713 | // Make sure that our non-designated initializer list has space |
| 1714 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1715 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1717 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1719 | // Repeatedly perform subobject initializations in the range |
| 1720 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1721 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1722 | // Move to the next designator |
| 1723 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1724 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1725 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1726 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1727 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1728 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1729 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1730 | // Recurse to check later designated subobjects. |
| 1731 | QualType ElementType = AT->getElementType(); |
| 1732 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1733 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1734 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1735 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1736 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1737 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1738 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1739 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1740 | return true; |
| 1741 | |
| 1742 | // Move to the next index in the array that we'll be initializing. |
| 1743 | ++DesignatedStartIndex; |
| 1744 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1745 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1746 | |
| 1747 | // If this the first designator, our caller will continue checking |
| 1748 | // the rest of this array subobject. |
| 1749 | if (IsFirstDesignator) { |
| 1750 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1751 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1752 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1756 | if (!FinishSubobjectInit) |
| 1757 | return false; |
| 1758 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1759 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1760 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1761 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1762 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1763 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1767 | // Get the structured initializer list for a subobject of type |
| 1768 | // @p CurrentObjectType. |
| 1769 | InitListExpr * |
| 1770 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1771 | QualType CurrentObjectType, |
| 1772 | InitListExpr *StructuredList, |
| 1773 | unsigned StructuredIndex, |
| 1774 | SourceRange InitRange) { |
| 1775 | Expr *ExistingInit = 0; |
| 1776 | if (!StructuredList) |
| 1777 | ExistingInit = SyntacticToSemantic[IList]; |
| 1778 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1779 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1781 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1782 | return Result; |
| 1783 | |
| 1784 | if (ExistingInit) { |
| 1785 | // We are creating an initializer list that initializes the |
| 1786 | // subobjects of the current object, but there was already an |
| 1787 | // initialization that completely initialized the current |
| 1788 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1790 | // struct X { int a, b; }; |
| 1791 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1793 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1794 | // designated initializer re-initializes the whole |
| 1795 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1797 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1798 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1800 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1801 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1802 | << ExistingInit->getSourceRange(); |
| 1803 | } |
| 1804 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1806 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1807 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1808 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1810 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1811 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1812 | // Pre-allocate storage for the structured initializer list. |
| 1813 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1814 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1815 | bool GotNumInits = false; |
| 1816 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1817 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1818 | GotNumInits = true; |
| 1819 | } else if (Index < IList->getNumInits()) { |
| 1820 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1821 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1822 | GotNumInits = true; |
| 1823 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1827 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1828 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1829 | NumElements = CAType->getSize().getZExtValue(); |
| 1830 | // Simple heuristic so that we don't allocate a very large |
| 1831 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1832 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1833 | NumElements = 0; |
| 1834 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1835 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1836 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1837 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1838 | RecordDecl *RDecl = RType->getDecl(); |
| 1839 | if (RDecl->isUnion()) |
| 1840 | NumElements = 1; |
| 1841 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1843 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1846 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1847 | NumElements = IList->getNumInits(); |
| 1848 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1849 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1851 | // Link this new initializer list into the structured initializer |
| 1852 | // lists. |
| 1853 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1854 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1855 | else { |
| 1856 | Result->setSyntacticForm(IList); |
| 1857 | SyntacticToSemantic[IList] = Result; |
| 1858 | } |
| 1859 | |
| 1860 | return Result; |
| 1861 | } |
| 1862 | |
| 1863 | /// Update the initializer at index @p StructuredIndex within the |
| 1864 | /// structured initializer list to the value @p expr. |
| 1865 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1866 | unsigned &StructuredIndex, |
| 1867 | Expr *expr) { |
| 1868 | // No structured initializer list to update |
| 1869 | if (!StructuredList) |
| 1870 | return; |
| 1871 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1872 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1873 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1874 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1876 | diag::warn_initializer_overrides) |
| 1877 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1879 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1880 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1881 | << PrevInit->getSourceRange(); |
| 1882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1884 | ++StructuredIndex; |
| 1885 | } |
| 1886 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1887 | /// Check that the given Index expression is a valid array designator |
| 1888 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1889 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1890 | /// and produces a reasonable diagnostic if there is a |
| 1891 | /// failure. Returns true if there was an error, false otherwise. If |
| 1892 | /// everything went okay, Value will receive the value of the constant |
| 1893 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1895 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1896 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1897 | |
| 1898 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1899 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1900 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1902 | if (Value.isSigned() && Value.isNegative()) |
| 1903 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1904 | << Value.toString(10) << Index->getSourceRange(); |
| 1905 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1906 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1907 | return false; |
| 1908 | } |
| 1909 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1910 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1911 | SourceLocation Loc, |
| 1912 | bool GNUSyntax, |
| 1913 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1914 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1915 | |
| 1916 | bool Invalid = false; |
| 1917 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1918 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1919 | |
| 1920 | // Build designators and check array designator expressions. |
| 1921 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1922 | const Designator &D = Desig.getDesignator(Idx); |
| 1923 | switch (D.getKind()) { |
| 1924 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1926 | D.getFieldLoc())); |
| 1927 | break; |
| 1928 | |
| 1929 | case Designator::ArrayDesignator: { |
| 1930 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1931 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1932 | if (!Index->isTypeDependent() && |
| 1933 | !Index->isValueDependent() && |
| 1934 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1935 | Invalid = true; |
| 1936 | else { |
| 1937 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1939 | D.getRBracketLoc())); |
| 1940 | InitExpressions.push_back(Index); |
| 1941 | } |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | case Designator::ArrayRangeDesignator: { |
| 1946 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1947 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1948 | llvm::APSInt StartValue; |
| 1949 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1950 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1951 | StartIndex->isValueDependent(); |
| 1952 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1953 | EndIndex->isValueDependent(); |
| 1954 | if ((!StartDependent && |
| 1955 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1956 | (!EndDependent && |
| 1957 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1958 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1959 | else { |
| 1960 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1961 | if (StartDependent || EndDependent) { |
| 1962 | // Nothing to compute. |
| 1963 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1964 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1965 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1966 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1968 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1969 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1971 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1972 | Invalid = true; |
| 1973 | } else { |
| 1974 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1976 | D.getEllipsisLoc(), |
| 1977 | D.getRBracketLoc())); |
| 1978 | InitExpressions.push_back(StartIndex); |
| 1979 | InitExpressions.push_back(EndIndex); |
| 1980 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1981 | } |
| 1982 | break; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | if (Invalid || Init.isInvalid()) |
| 1988 | return ExprError(); |
| 1989 | |
| 1990 | // Clear out the expressions within the designation. |
| 1991 | Desig.ClearExprs(*this); |
| 1992 | |
| 1993 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1994 | = DesignatedInitExpr::Create(Context, |
| 1995 | Designators.data(), Designators.size(), |
| 1996 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1997 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 1999 | if (getLangOptions().CPlusPlus) |
Eli Friedman | a47317b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2000 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2001 | << DIE->getSourceRange(); |
| 2002 | else if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2003 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2004 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2006 | return Owned(DIE); |
| 2007 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2009 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2010 | InitListExpr *&InitList, QualType &DeclType) { |
| 2011 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2012 | if (!CheckInitList.HadError()) |
| 2013 | InitList = CheckInitList.getFullyStructuredList(); |
| 2014 | |
| 2015 | return CheckInitList.HadError(); |
| 2016 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2018 | //===----------------------------------------------------------------------===// |
| 2019 | // Initialization entity |
| 2020 | //===----------------------------------------------------------------------===// |
| 2021 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2022 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2023 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2024 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2025 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2026 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2027 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2028 | Type = AT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2029 | } else { |
| 2030 | Kind = EK_VectorElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2031 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2032 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2035 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2036 | CXXBaseSpecifier *Base, |
| 2037 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2038 | { |
| 2039 | InitializedEntity Result; |
| 2040 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2041 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2042 | if (IsInheritedVirtualBase) |
| 2043 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2044 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2045 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2046 | return Result; |
| 2047 | } |
| 2048 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2049 | DeclarationName InitializedEntity::getName() const { |
| 2050 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2051 | case EK_Parameter: { |
| 2052 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2053 | return (D ? D->getDeclName() : DeclarationName()); |
| 2054 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2055 | |
| 2056 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2057 | case EK_Member: |
| 2058 | return VariableOrMember->getDeclName(); |
| 2059 | |
| 2060 | case EK_Result: |
| 2061 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2062 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2063 | case EK_Temporary: |
| 2064 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2065 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2066 | case EK_ArrayElement: |
| 2067 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2068 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2069 | return DeclarationName(); |
| 2070 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2072 | // Silence GCC warning |
| 2073 | return DeclarationName(); |
| 2074 | } |
| 2075 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2076 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2077 | switch (getKind()) { |
| 2078 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2079 | case EK_Member: |
| 2080 | return VariableOrMember; |
| 2081 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2082 | case EK_Parameter: |
| 2083 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2084 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2085 | case EK_Result: |
| 2086 | case EK_Exception: |
| 2087 | case EK_New: |
| 2088 | case EK_Temporary: |
| 2089 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2090 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2091 | case EK_ArrayElement: |
| 2092 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2093 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2094 | return 0; |
| 2095 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2097 | // Silence GCC warning |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2101 | bool InitializedEntity::allowsNRVO() const { |
| 2102 | switch (getKind()) { |
| 2103 | case EK_Result: |
| 2104 | case EK_Exception: |
| 2105 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2106 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2107 | case EK_Variable: |
| 2108 | case EK_Parameter: |
| 2109 | case EK_Member: |
| 2110 | case EK_New: |
| 2111 | case EK_Temporary: |
| 2112 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2113 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2114 | case EK_ArrayElement: |
| 2115 | case EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2116 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2117 | break; |
| 2118 | } |
| 2119 | |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2123 | //===----------------------------------------------------------------------===// |
| 2124 | // Initialization sequence |
| 2125 | //===----------------------------------------------------------------------===// |
| 2126 | |
| 2127 | void InitializationSequence::Step::Destroy() { |
| 2128 | switch (Kind) { |
| 2129 | case SK_ResolveAddressOfOverloadedFunction: |
| 2130 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2131 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2132 | case SK_CastDerivedToBaseLValue: |
| 2133 | case SK_BindReference: |
| 2134 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2135 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2136 | case SK_UserConversion: |
| 2137 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2138 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2139 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2140 | case SK_ListInitialization: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2141 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2142 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2143 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2144 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2145 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2146 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2147 | case SK_PassByIndirectCopyRestore: |
| 2148 | case SK_PassByIndirectRestore: |
| 2149 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2150 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2152 | case SK_ConversionSequence: |
| 2153 | delete ICS; |
| 2154 | } |
| 2155 | } |
| 2156 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2157 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame^] | 2158 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2162 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2163 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2165 | switch (getFailureKind()) { |
| 2166 | case FK_TooManyInitsForReference: |
| 2167 | case FK_ArrayNeedsInitList: |
| 2168 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2169 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2170 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2171 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2172 | case FK_RValueReferenceBindingToLValue: |
| 2173 | case FK_ReferenceInitDropsQualifiers: |
| 2174 | case FK_ReferenceInitFailed: |
| 2175 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2176 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2177 | case FK_TooManyInitsForScalar: |
| 2178 | case FK_ReferenceBindingToInitList: |
| 2179 | case FK_InitListBadDestinationType: |
| 2180 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2181 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2182 | case FK_ArrayTypeMismatch: |
| 2183 | case FK_NonConstantArrayInit: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2184 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2186 | case FK_ReferenceInitOverloadFailed: |
| 2187 | case FK_UserConversionOverloadFailed: |
| 2188 | case FK_ConstructorOverloadFailed: |
| 2189 | return FailedOverloadResult == OR_Ambiguous; |
| 2190 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2192 | return false; |
| 2193 | } |
| 2194 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2195 | bool InitializationSequence::isConstructorInitialization() const { |
| 2196 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2197 | } |
| 2198 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2199 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2200 | FunctionDecl *Function, |
| 2201 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2202 | Step S; |
| 2203 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2204 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2205 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2206 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2207 | Steps.push_back(S); |
| 2208 | } |
| 2209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2210 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2211 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2212 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2213 | switch (VK) { |
| 2214 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2215 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2216 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2217 | default: llvm_unreachable("No such category"); |
| 2218 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2219 | S.Type = BaseType; |
| 2220 | Steps.push_back(S); |
| 2221 | } |
| 2222 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2224 | bool BindingTemporary) { |
| 2225 | Step S; |
| 2226 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2227 | S.Type = T; |
| 2228 | Steps.push_back(S); |
| 2229 | } |
| 2230 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2231 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2232 | Step S; |
| 2233 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2234 | S.Type = T; |
| 2235 | Steps.push_back(S); |
| 2236 | } |
| 2237 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2238 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2239 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2240 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2241 | Step S; |
| 2242 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2243 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2244 | S.Function.Function = Function; |
| 2245 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2246 | Steps.push_back(S); |
| 2247 | } |
| 2248 | |
| 2249 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2250 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2251 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2252 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2253 | switch (VK) { |
| 2254 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2255 | S.Kind = SK_QualificationConversionRValue; |
| 2256 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2257 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2258 | S.Kind = SK_QualificationConversionXValue; |
| 2259 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2260 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2261 | S.Kind = SK_QualificationConversionLValue; |
| 2262 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2263 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2264 | S.Type = Ty; |
| 2265 | Steps.push_back(S); |
| 2266 | } |
| 2267 | |
| 2268 | void InitializationSequence::AddConversionSequenceStep( |
| 2269 | const ImplicitConversionSequence &ICS, |
| 2270 | QualType T) { |
| 2271 | Step S; |
| 2272 | S.Kind = SK_ConversionSequence; |
| 2273 | S.Type = T; |
| 2274 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2275 | Steps.push_back(S); |
| 2276 | } |
| 2277 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2278 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2279 | Step S; |
| 2280 | S.Kind = SK_ListInitialization; |
| 2281 | S.Type = T; |
| 2282 | Steps.push_back(S); |
| 2283 | } |
| 2284 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2285 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2286 | InitializationSequence::AddConstructorInitializationStep( |
| 2287 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2288 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2289 | QualType T) { |
| 2290 | Step S; |
| 2291 | S.Kind = SK_ConstructorInitialization; |
| 2292 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2293 | S.Function.Function = Constructor; |
| 2294 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2295 | Steps.push_back(S); |
| 2296 | } |
| 2297 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2298 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2299 | Step S; |
| 2300 | S.Kind = SK_ZeroInitialization; |
| 2301 | S.Type = T; |
| 2302 | Steps.push_back(S); |
| 2303 | } |
| 2304 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2305 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2306 | Step S; |
| 2307 | S.Kind = SK_CAssignment; |
| 2308 | S.Type = T; |
| 2309 | Steps.push_back(S); |
| 2310 | } |
| 2311 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2312 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2313 | Step S; |
| 2314 | S.Kind = SK_StringInit; |
| 2315 | S.Type = T; |
| 2316 | Steps.push_back(S); |
| 2317 | } |
| 2318 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2319 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2320 | Step S; |
| 2321 | S.Kind = SK_ObjCObjectConversion; |
| 2322 | S.Type = T; |
| 2323 | Steps.push_back(S); |
| 2324 | } |
| 2325 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2326 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2327 | Step S; |
| 2328 | S.Kind = SK_ArrayInit; |
| 2329 | S.Type = T; |
| 2330 | Steps.push_back(S); |
| 2331 | } |
| 2332 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2333 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2334 | bool shouldCopy) { |
| 2335 | Step s; |
| 2336 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2337 | : SK_PassByIndirectRestore); |
| 2338 | s.Type = type; |
| 2339 | Steps.push_back(s); |
| 2340 | } |
| 2341 | |
| 2342 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2343 | Step S; |
| 2344 | S.Kind = SK_ProduceObjCObject; |
| 2345 | S.Type = T; |
| 2346 | Steps.push_back(S); |
| 2347 | } |
| 2348 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2349 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2350 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2351 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2352 | this->Failure = Failure; |
| 2353 | this->FailedOverloadResult = Result; |
| 2354 | } |
| 2355 | |
| 2356 | //===----------------------------------------------------------------------===// |
| 2357 | // Attempt initialization |
| 2358 | //===----------------------------------------------------------------------===// |
| 2359 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2360 | static void MaybeProduceObjCObject(Sema &S, |
| 2361 | InitializationSequence &Sequence, |
| 2362 | const InitializedEntity &Entity) { |
| 2363 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2364 | |
| 2365 | /// When initializing a parameter, produce the value if it's marked |
| 2366 | /// __attribute__((ns_consumed)). |
| 2367 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2368 | if (!Entity.isParameterConsumed()) |
| 2369 | return; |
| 2370 | |
| 2371 | assert(Entity.getType()->isObjCRetainableType() && |
| 2372 | "consuming an object of unretainable type?"); |
| 2373 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2374 | |
| 2375 | /// When initializing a return value, if the return type is a |
| 2376 | /// retainable type, then returns need to immediately retain the |
| 2377 | /// object. If an autorelease is required, it will be done at the |
| 2378 | /// last instant. |
| 2379 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2380 | if (!Entity.getType()->isObjCRetainableType()) |
| 2381 | return; |
| 2382 | |
| 2383 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2384 | } |
| 2385 | } |
| 2386 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2387 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2388 | static void TryListInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2389 | const InitializedEntity &Entity, |
| 2390 | const InitializationKind &Kind, |
| 2391 | InitListExpr *InitList, |
| 2392 | InitializationSequence &Sequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2393 | // FIXME: We only perform rudimentary checking of list |
| 2394 | // initializations at this point, then assume that any list |
| 2395 | // initialization of an array, aggregate, or scalar will be |
Sebastian Redl | 36c28db | 2010-06-30 16:41:54 +0000 | [diff] [blame] | 2396 | // well-formed. When we actually "perform" list initialization, we'll |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2397 | // do all of the necessary checking. C++0x initializer lists will |
| 2398 | // force us to perform more checking here. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2399 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2400 | QualType DestType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2401 | |
| 2402 | // C++ [dcl.init]p13: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2403 | // If T is a scalar type, then a declaration of the form |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2404 | // |
| 2405 | // T x = { a }; |
| 2406 | // |
| 2407 | // is equivalent to |
| 2408 | // |
| 2409 | // T x = a; |
| 2410 | if (DestType->isScalarType()) { |
| 2411 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2412 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | // Assume scalar initialization from a single value works. |
| 2417 | } else if (DestType->isAggregateType()) { |
| 2418 | // Assume aggregate initialization works. |
| 2419 | } else if (DestType->isVectorType()) { |
| 2420 | // Assume vector initialization works. |
| 2421 | } else if (DestType->isReferenceType()) { |
| 2422 | // FIXME: C++0x defines behavior for this. |
| 2423 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2424 | return; |
| 2425 | } else if (DestType->isRecordType()) { |
| 2426 | // FIXME: C++0x defines behavior for this |
| 2427 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2428 | } |
| 2429 | |
| 2430 | // Add a general "list initialization" step. |
| 2431 | Sequence.AddListInitializationStep(DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /// \brief Try a reference initialization that involves calling a conversion |
| 2435 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2436 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2437 | const InitializedEntity &Entity, |
| 2438 | const InitializationKind &Kind, |
| 2439 | Expr *Initializer, |
| 2440 | bool AllowRValues, |
| 2441 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2442 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2443 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2444 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2445 | QualType cv2T2 = Initializer->getType(); |
| 2446 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2447 | |
| 2448 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2449 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2450 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2451 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2452 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2453 | ObjCConversion, |
| 2454 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2455 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2456 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2457 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2458 | (void)ObjCLifetimeConversion; |
| 2459 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2460 | // Build the candidate set directly in the initialization sequence |
| 2461 | // structure, so that it will persist if we fail. |
| 2462 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2463 | CandidateSet.clear(); |
| 2464 | |
| 2465 | // Determine whether we are allowed to call explicit constructors or |
| 2466 | // explicit conversion operators. |
| 2467 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2468 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2469 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2470 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2471 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2472 | // The type we're converting to is a class type. Enumerate its constructors |
| 2473 | // to see if there is a suitable conversion. |
| 2474 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2476 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2477 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2478 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2479 | NamedDecl *D = *Con; |
| 2480 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2481 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2482 | // Find the constructor (which may be a template). |
| 2483 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2484 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2485 | if (ConstructorTmpl) |
| 2486 | Constructor = cast<CXXConstructorDecl>( |
| 2487 | ConstructorTmpl->getTemplatedDecl()); |
| 2488 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2489 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2490 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2491 | if (!Constructor->isInvalidDecl() && |
| 2492 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2493 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2494 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2495 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2496 | &Initializer, 1, CandidateSet, |
| 2497 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2498 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2499 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2500 | &Initializer, 1, CandidateSet, |
| 2501 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2503 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2504 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2505 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2506 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2508 | const RecordType *T2RecordType = 0; |
| 2509 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2510 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | // The type we're converting from is a class type, enumerate its conversion |
| 2512 | // functions. |
| 2513 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2514 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2515 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2516 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2517 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2518 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2519 | NamedDecl *D = *I; |
| 2520 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2521 | if (isa<UsingShadowDecl>(D)) |
| 2522 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2524 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2525 | CXXConversionDecl *Conv; |
| 2526 | if (ConvTemplate) |
| 2527 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2528 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2529 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2531 | // If the conversion function doesn't return a reference type, |
| 2532 | // it can't be considered for this conversion unless we're allowed to |
| 2533 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2534 | // FIXME: Do we need to make sure that we only consider conversion |
| 2535 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2536 | // break recursion. |
| 2537 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2538 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2539 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2540 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2541 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2542 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2543 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2544 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2545 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2549 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2550 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2552 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2553 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2554 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2555 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2557 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2558 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2560 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2561 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2562 | // This is the overload that will actually be used for the initialization, so |
| 2563 | // mark it as used. |
| 2564 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2565 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2566 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2567 | if (isa<CXXConversionDecl>(Function)) |
| 2568 | T2 = Function->getResultType(); |
| 2569 | else |
| 2570 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2573 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2574 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2575 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2576 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2577 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2578 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2579 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2580 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2581 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2582 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2583 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2584 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2585 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2586 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2587 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2588 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2589 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2590 | NewDerivedToBase, NewObjCConversion, |
| 2591 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2592 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2593 | // If the type we've converted to is not reference-related to the |
| 2594 | // type we're looking for, then there is another conversion step |
| 2595 | // we need to perform to produce a temporary of the right type |
| 2596 | // that we'll be binding to. |
| 2597 | ImplicitConversionSequence ICS; |
| 2598 | ICS.setStandard(); |
| 2599 | ICS.Standard = Best->FinalConversion; |
| 2600 | T2 = ICS.Standard.getToType(2); |
| 2601 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2602 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2603 | Sequence.AddDerivedToBaseCastStep( |
| 2604 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2606 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2607 | else if (NewObjCConversion) |
| 2608 | Sequence.AddObjCObjectConversionStep( |
| 2609 | S.Context.getQualifiedType(T1, |
| 2610 | T2.getNonReferenceType().getQualifiers())); |
| 2611 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2612 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2613 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2614 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2616 | return OR_Success; |
| 2617 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2618 | |
| 2619 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2620 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2621 | const InitializedEntity &Entity, |
| 2622 | const InitializationKind &Kind, |
| 2623 | Expr *Initializer, |
| 2624 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2625 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2626 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2627 | Qualifiers T1Quals; |
| 2628 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2629 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2630 | Qualifiers T2Quals; |
| 2631 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2632 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2634 | // If the initializer is the address of an overloaded function, try |
| 2635 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2636 | // type of the resulting function. |
| 2637 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2638 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2639 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2640 | T1, |
| 2641 | false, |
| 2642 | Found)) { |
| 2643 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2644 | cv2T2 = Fn->getType(); |
| 2645 | T2 = cv2T2.getUnqualifiedType(); |
| 2646 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2647 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2648 | return; |
| 2649 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2650 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2652 | // Compute some basic properties of the types and the initializer. |
| 2653 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2654 | bool isRValueRef = !isLValueRef; |
| 2655 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2656 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2657 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2658 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2659 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2660 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2661 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2663 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2664 | // 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] | 2665 | // "cv2 T2" as follows: |
| 2666 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2667 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2668 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2669 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2670 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2671 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2672 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2673 | bool T1Function = T1->isFunctionType(); |
| 2674 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2675 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2676 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2677 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2678 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | // - 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] | 2680 | // reference-compatible with "cv2 T2," or |
| 2681 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | // 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] | 2683 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2684 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2685 | // to decide whether we're actually binding to a temporary created from |
| 2686 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2687 | if (DerivedToBase) |
| 2688 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2689 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2690 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2691 | else if (ObjCConversion) |
| 2692 | Sequence.AddObjCObjectConversionStep( |
| 2693 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2694 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2695 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2696 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2697 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2698 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2699 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2702 | |
| 2703 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2704 | // reference-related to T2, and can be implicitly converted to an |
| 2705 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2706 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2707 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2708 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2709 | // If we have an rvalue ref to function type here, the rhs must be |
| 2710 | // an rvalue. |
| 2711 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2712 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2713 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2714 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2715 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2716 | Sequence); |
| 2717 | if (ConvOvlResult == OR_Success) |
| 2718 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2719 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2720 | Sequence.SetOverloadFailure( |
| 2721 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2722 | ConvOvlResult); |
| 2723 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2724 | } |
| 2725 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2726 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2728 | // 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] | 2729 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2730 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2731 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2732 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2733 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2734 | Sequence.SetOverloadFailure( |
| 2735 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2736 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2737 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2738 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2739 | ? (RefRelationship == Sema::Ref_Related |
| 2740 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2741 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2742 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2744 | return; |
| 2745 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2747 | // - If the initializer expression |
| 2748 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2749 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2750 | // Note: functions are handled below. |
| 2751 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2752 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2753 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2754 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2755 | (InitCategory.isXValue() || |
| 2756 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2757 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2758 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2759 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2760 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2761 | // compiler the freedom to perform a copy here or bind to the |
| 2762 | // object, while C++0x requires that we bind directly to the |
| 2763 | // object. Hence, we always bind to the object without making an |
| 2764 | // extra copy. However, in C++03 requires that we check for the |
| 2765 | // presence of a suitable copy constructor: |
| 2766 | // |
| 2767 | // The constructor that would be used to make the copy shall |
| 2768 | // be callable whether or not the copy is actually done. |
Francois Pichet | f57258b | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2769 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2770 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2773 | if (DerivedToBase) |
| 2774 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2775 | ValueKind); |
| 2776 | else if (ObjCConversion) |
| 2777 | Sequence.AddObjCObjectConversionStep( |
| 2778 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2779 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2780 | if (T1Quals != T2Quals) |
| 2781 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2782 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2783 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2785 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
| 2787 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2788 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2789 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2790 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2791 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2792 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2793 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2794 | Kind, Initializer, |
| 2795 | /*AllowRValues=*/true, |
| 2796 | Sequence); |
| 2797 | if (ConvOvlResult) |
| 2798 | Sequence.SetOverloadFailure( |
| 2799 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2800 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2805 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2806 | return; |
| 2807 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2808 | |
| 2809 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2810 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2811 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2812 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2814 | // Determine whether we are allowed to call explicit constructors or |
| 2815 | // explicit conversion operators. |
| 2816 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2817 | |
| 2818 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2819 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2820 | ImplicitConversionSequence ICS |
| 2821 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2822 | /*SuppressUserConversions*/ false, |
| 2823 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2824 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2825 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 2826 | /*AllowObjCWritebackConversion=*/false); |
| 2827 | |
| 2828 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2829 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2830 | // this into an overloading ambiguity diagnostic. However, we need |
| 2831 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2832 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2833 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2834 | Sequence.SetOverloadFailure( |
| 2835 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2836 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2837 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2838 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2839 | else |
| 2840 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2841 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2842 | } else { |
| 2843 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2847 | // same cv-qualification as, or greater cv-qualification |
| 2848 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2849 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2850 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2851 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2852 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2853 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2854 | return; |
| 2855 | } |
| 2856 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | // [...] 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] | 2858 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2860 | InitCategory.isLValue()) { |
| 2861 | Sequence.SetFailed( |
| 2862 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2863 | return; |
| 2864 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2866 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2867 | return; |
| 2868 | } |
| 2869 | |
| 2870 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2871 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2872 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2873 | const InitializedEntity &Entity, |
| 2874 | const InitializationKind &Kind, |
| 2875 | Expr *Initializer, |
| 2876 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2877 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2880 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2881 | /// enumerates the constructors of the initialized entity and performs overload |
| 2882 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2883 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2884 | const InitializedEntity &Entity, |
| 2885 | const InitializationKind &Kind, |
| 2886 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2887 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2888 | InitializationSequence &Sequence) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2889 | // Build the candidate set directly in the initialization sequence |
| 2890 | // structure, so that it will persist if we fail. |
| 2891 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2892 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2894 | // Determine whether we are allowed to call explicit constructors or |
| 2895 | // explicit conversion operators. |
| 2896 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2897 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2898 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2899 | |
| 2900 | // The type we're constructing needs to be complete. |
| 2901 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2902 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2903 | return; |
| 2904 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2906 | // The type we're converting to is a class type. Enumerate its constructors |
| 2907 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2908 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2909 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2910 | CXXRecordDecl *DestRecordDecl |
| 2911 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2913 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2914 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2915 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2916 | NamedDecl *D = *Con; |
| 2917 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2918 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2920 | // Find the constructor (which may be a template). |
| 2921 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2922 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2923 | if (ConstructorTmpl) |
| 2924 | Constructor = cast<CXXConstructorDecl>( |
| 2925 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2926 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2927 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2928 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2930 | // suppress user-defined conversions on the arguments. |
| 2931 | // FIXME: Move constructors? |
| 2932 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2933 | Constructor->isCopyConstructor()) |
| 2934 | SuppressUserConversions = true; |
| 2935 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2937 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2938 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2939 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2940 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2941 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2942 | Args, NumArgs, CandidateSet, |
| 2943 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2944 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2945 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2946 | Args, NumArgs, CandidateSet, |
| 2947 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2951 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2954 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2955 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2956 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2957 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2958 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2959 | Result); |
| 2960 | return; |
| 2961 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2962 | |
| 2963 | // C++0x [dcl.init]p6: |
| 2964 | // If a program calls for the default initialization of an object |
| 2965 | // of a const-qualified type T, T shall be a class type with a |
| 2966 | // user-provided default constructor. |
| 2967 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2968 | Entity.getType().isConstQualified() && |
| 2969 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2970 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2971 | return; |
| 2972 | } |
| 2973 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2974 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2975 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2976 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2978 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2979 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2982 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2983 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2984 | const InitializedEntity &Entity, |
| 2985 | const InitializationKind &Kind, |
| 2986 | InitializationSequence &Sequence) { |
| 2987 | // C++ [dcl.init]p5: |
| 2988 | // |
| 2989 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2990 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2992 | // -- if T is an array type, then each element is value-initialized; |
| 2993 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2994 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2996 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2997 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2998 | // -- if T is a class type (clause 9) with a user-declared |
| 2999 | // constructor (12.1), then the default constructor for T is |
| 3000 | // called (and the initialization is ill-formed if T has no |
| 3001 | // accessible default constructor); |
| 3002 | // |
| 3003 | // FIXME: we really want to refer to a single subobject of the array, |
| 3004 | // but Entity doesn't have a way to capture that (yet). |
| 3005 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3006 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3007 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3008 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3009 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3010 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3011 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3012 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3013 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3014 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3015 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3016 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
| 3019 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3020 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3023 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3024 | static void TryDefaultInitialization(Sema &S, |
| 3025 | const InitializedEntity &Entity, |
| 3026 | const InitializationKind &Kind, |
| 3027 | InitializationSequence &Sequence) { |
| 3028 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3030 | // C++ [dcl.init]p6: |
| 3031 | // To default-initialize an object of type T means: |
| 3032 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3033 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3034 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3035 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3036 | // constructor for T is called (and the initialization is ill-formed if |
| 3037 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3038 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3039 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3040 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3041 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3043 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3045 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3046 | // 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] | 3047 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3048 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3049 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3050 | return; |
| 3051 | } |
| 3052 | |
| 3053 | // If the destination type has a lifetime property, zero-initialize it. |
| 3054 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3055 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3056 | return; |
| 3057 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3060 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3061 | /// which enumerates all conversion functions and performs overload resolution |
| 3062 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3064 | const InitializedEntity &Entity, |
| 3065 | const InitializationKind &Kind, |
| 3066 | Expr *Initializer, |
| 3067 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3068 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3069 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3070 | QualType SourceType = Initializer->getType(); |
| 3071 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3072 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3073 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3074 | // Build the candidate set directly in the initialization sequence |
| 3075 | // structure, so that it will persist if we fail. |
| 3076 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3077 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3079 | // Determine whether we are allowed to call explicit constructors or |
| 3080 | // explicit conversion operators. |
| 3081 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3083 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3084 | // The type we're converting to is a class type. Enumerate its constructors |
| 3085 | // to see if there is a suitable conversion. |
| 3086 | CXXRecordDecl *DestRecordDecl |
| 3087 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3088 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3089 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3091 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3092 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3093 | Con != ConEnd; ++Con) { |
| 3094 | NamedDecl *D = *Con; |
| 3095 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3096 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3097 | // Find the constructor (which may be a template). |
| 3098 | CXXConstructorDecl *Constructor = 0; |
| 3099 | FunctionTemplateDecl *ConstructorTmpl |
| 3100 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3101 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3102 | Constructor = cast<CXXConstructorDecl>( |
| 3103 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3104 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3105 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3107 | if (!Constructor->isInvalidDecl() && |
| 3108 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3109 | if (ConstructorTmpl) |
| 3110 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3111 | /*ExplicitArgs*/ 0, |
| 3112 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3113 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3114 | else |
| 3115 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3116 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3117 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3118 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3119 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3120 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3121 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3122 | |
| 3123 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3124 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3125 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3126 | // The type we're converting from is a class type, enumerate its conversion |
| 3127 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3128 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3129 | // We can only enumerate the conversion functions for a complete type; if |
| 3130 | // the type isn't complete, simply skip this step. |
| 3131 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3132 | CXXRecordDecl *SourceRecordDecl |
| 3133 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3134 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3135 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3136 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3137 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3138 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3139 | I != E; ++I) { |
| 3140 | NamedDecl *D = *I; |
| 3141 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3142 | if (isa<UsingShadowDecl>(D)) |
| 3143 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3145 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3146 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3147 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3148 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3149 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3150 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3151 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3152 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3153 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3154 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3155 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3156 | CandidateSet); |
| 3157 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3158 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3159 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3160 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3164 | |
| 3165 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3166 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3167 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3168 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3169 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3170 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3171 | Result); |
| 3172 | return; |
| 3173 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3175 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3176 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3178 | if (isa<CXXConstructorDecl>(Function)) { |
| 3179 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3180 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3181 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3182 | return; |
| 3183 | } |
| 3184 | |
| 3185 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3186 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3187 | if (ConvType->getAs<RecordType>()) { |
| 3188 | // If we're converting to a class type, there may be an copy if |
| 3189 | // the resulting temporary object (possible to create an object of |
| 3190 | // a base class type). That copy is not a separate conversion, so |
| 3191 | // we just make a note of the actual destination type (possibly a |
| 3192 | // base class of the type returned by the conversion function) and |
| 3193 | // let the user-defined conversion step handle the conversion. |
| 3194 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3195 | return; |
| 3196 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3198 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3200 | // If the conversion following the call to the conversion function |
| 3201 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3202 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3203 | Best->FinalConversion.Third) { |
| 3204 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3205 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3206 | ICS.Standard = Best->FinalConversion; |
| 3207 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3208 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3211 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3212 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3213 | |
| 3214 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3215 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3216 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3217 | // Skip parens. |
| 3218 | e = e->IgnoreParens(); |
| 3219 | |
| 3220 | // Skip address-of nodes. |
| 3221 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3222 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3223 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3224 | |
| 3225 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3226 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3227 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3228 | case CK_Dependent: |
| 3229 | case CK_BitCast: |
| 3230 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3231 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3232 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3233 | |
| 3234 | case CK_ArrayToPointerDecay: |
| 3235 | return IIK_nonscalar; |
| 3236 | |
| 3237 | case CK_NullToPointer: |
| 3238 | return IIK_okay; |
| 3239 | |
| 3240 | default: |
| 3241 | break; |
| 3242 | } |
| 3243 | |
| 3244 | // If we have a declaration reference, it had better be a local variable. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3245 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3246 | if (!isAddressOf) return IIK_nonlocal; |
| 3247 | |
| 3248 | VarDecl *var; |
| 3249 | if (isa<DeclRefExpr>(e)) { |
| 3250 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3251 | if (!var) return IIK_nonlocal; |
| 3252 | } else { |
| 3253 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3254 | } |
| 3255 | |
| 3256 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3257 | |
| 3258 | // If we have a conditional operator, check both sides. |
| 3259 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3260 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3261 | return iik; |
| 3262 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3263 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3264 | |
| 3265 | // These are never scalar. |
| 3266 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3267 | return IIK_nonscalar; |
| 3268 | |
| 3269 | // Otherwise, it needs to be a null pointer constant. |
| 3270 | } else { |
| 3271 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3272 | ? IIK_okay : IIK_nonlocal); |
| 3273 | } |
| 3274 | |
| 3275 | return IIK_nonlocal; |
| 3276 | } |
| 3277 | |
| 3278 | /// Check whether the given expression is a valid operand for an |
| 3279 | /// indirect copy/restore. |
| 3280 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3281 | assert(src->isRValue()); |
| 3282 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3283 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3284 | if (iik == IIK_okay) return; |
| 3285 | |
| 3286 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3287 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3288 | << src->getSourceRange(); |
| 3289 | } |
| 3290 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3291 | /// \brief Determine whether we have compatible array types for the |
| 3292 | /// purposes of GNU by-copy array initialization. |
| 3293 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3294 | const ArrayType *Dest, |
| 3295 | const ArrayType *Source) { |
| 3296 | // If the source and destination array types are equivalent, we're |
| 3297 | // done. |
| 3298 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3299 | return true; |
| 3300 | |
| 3301 | // Make sure that the element types are the same. |
| 3302 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3303 | return false; |
| 3304 | |
| 3305 | // The only mismatch we allow is when the destination is an |
| 3306 | // incomplete array type and the source is a constant array type. |
| 3307 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3308 | } |
| 3309 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3310 | static bool tryObjCWritebackConversion(Sema &S, |
| 3311 | InitializationSequence &Sequence, |
| 3312 | const InitializedEntity &Entity, |
| 3313 | Expr *Initializer) { |
| 3314 | bool ArrayDecay = false; |
| 3315 | QualType ArgType = Initializer->getType(); |
| 3316 | QualType ArgPointee; |
| 3317 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3318 | ArrayDecay = true; |
| 3319 | ArgPointee = ArgArrayType->getElementType(); |
| 3320 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3321 | } |
| 3322 | |
| 3323 | // Handle write-back conversion. |
| 3324 | QualType ConvertedArgType; |
| 3325 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3326 | ConvertedArgType)) |
| 3327 | return false; |
| 3328 | |
| 3329 | // We should copy unless we're passing to an argument explicitly |
| 3330 | // marked 'out'. |
| 3331 | bool ShouldCopy = true; |
| 3332 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3333 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3334 | |
| 3335 | // Do we need an lvalue conversion? |
| 3336 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3337 | ImplicitConversionSequence ICS; |
| 3338 | ICS.setStandard(); |
| 3339 | ICS.Standard.setAsIdentityConversion(); |
| 3340 | |
| 3341 | QualType ResultType; |
| 3342 | if (ArrayDecay) { |
| 3343 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3344 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3345 | } else { |
| 3346 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3347 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3348 | } |
| 3349 | |
| 3350 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3351 | } |
| 3352 | |
| 3353 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3354 | return true; |
| 3355 | } |
| 3356 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3357 | InitializationSequence::InitializationSequence(Sema &S, |
| 3358 | const InitializedEntity &Entity, |
| 3359 | const InitializationKind &Kind, |
| 3360 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3361 | unsigned NumArgs) |
| 3362 | : FailedCandidateSet(Kind.getLocation()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3363 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3365 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3366 | // The semantics of initializers are as follows. The destination type is |
| 3367 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3368 | // 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] | 3369 | // 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] | 3370 | // parenthesized list of expressions. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3371 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3372 | |
| 3373 | if (DestType->isDependentType() || |
| 3374 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3375 | SequenceKind = DependentSequence; |
| 3376 | return; |
| 3377 | } |
| 3378 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3379 | // Almost everything is a normal sequence. |
| 3380 | setSequenceKind(NormalSequence); |
| 3381 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3382 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3383 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3384 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3385 | if (Result.isInvalid()) { |
| 3386 | SetFailed(FK_ConversionFromPropertyFailed); |
| 3387 | return; |
| 3388 | } |
| 3389 | Args[I] = Result.take(); |
| 3390 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3392 | QualType SourceType; |
| 3393 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3394 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | Initializer = Args[0]; |
| 3396 | if (!isa<InitListExpr>(Initializer)) |
| 3397 | SourceType = Initializer->getType(); |
| 3398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3399 | |
| 3400 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3401 | // list-initialized (8.5.4). |
| 3402 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 3403 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3404 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3407 | // - If the destination type is a reference type, see 8.5.3. |
| 3408 | if (DestType->isReferenceType()) { |
| 3409 | // C++0x [dcl.init.ref]p1: |
| 3410 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3411 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3412 | // by an object that can be converted into a T. |
| 3413 | // (Therefore, multiple arguments are not permitted.) |
| 3414 | if (NumArgs != 1) |
| 3415 | SetFailed(FK_TooManyInitsForReference); |
| 3416 | else |
| 3417 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 3418 | return; |
| 3419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3420 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3421 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3422 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3423 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3424 | TryValueInitialization(S, Entity, Kind, *this); |
| 3425 | return; |
| 3426 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3428 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3429 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3430 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 3431 | return; |
| 3432 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3433 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3434 | // - If the destination type is an array of characters, an array of |
| 3435 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3436 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3438 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3439 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3440 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3441 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 3442 | return; |
| 3443 | } |
| 3444 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3445 | // Note: as an GNU C extension, we allow initialization of an |
| 3446 | // array from a compound literal that creates an array of the same |
| 3447 | // type, so long as the initializer has no side effects. |
| 3448 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3449 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3450 | Initializer->getType()->isArrayType()) { |
| 3451 | const ArrayType *SourceAT |
| 3452 | = Context.getAsArrayType(Initializer->getType()); |
| 3453 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
| 3454 | SetFailed(FK_ArrayTypeMismatch); |
| 3455 | else if (Initializer->HasSideEffects(S.Context)) |
| 3456 | SetFailed(FK_NonConstantArrayInit); |
| 3457 | else { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3458 | AddArrayInitStep(DestType); |
| 3459 | } |
| 3460 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3461 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 3462 | else |
| 3463 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3467 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3468 | // Determine whether we should consider writeback conversions for |
| 3469 | // Objective-C ARC. |
| 3470 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3471 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3472 | |
| 3473 | // We're at the end of the line for C: it's either a write-back conversion |
| 3474 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3475 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3476 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3477 | if (allowObjCWritebackConversion && |
| 3478 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
| 3479 | return; |
| 3480 | } |
| 3481 | |
| 3482 | // Handle initialization in C |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3483 | AddCAssignmentStep(DestType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3484 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3485 | return; |
| 3486 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3487 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3488 | assert(S.getLangOptions().CPlusPlus); |
| 3489 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3490 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3491 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | // - If the initialization is direct-initialization, or if it is |
| 3493 | // copy-initialization where the cv-unqualified version of the |
| 3494 | // 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] | 3495 | // class of the destination, constructors are considered. [...] |
| 3496 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3497 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3498 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3499 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3500 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3501 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3503 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3505 | // used) to a derived class thereof are enumerated as described in |
| 3506 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3507 | // (13.3). |
| 3508 | else |
| 3509 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3510 | return; |
| 3511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3513 | if (NumArgs > 1) { |
| 3514 | SetFailed(FK_TooManyInitsForScalar); |
| 3515 | return; |
| 3516 | } |
| 3517 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | |
| 3519 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3520 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3521 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3522 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3523 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3524 | return; |
| 3525 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3527 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3528 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3531 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3532 | |
| 3533 | ImplicitConversionSequence ICS |
| 3534 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3535 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3536 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3537 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3538 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3539 | allowObjCWritebackConversion); |
| 3540 | |
| 3541 | if (ICS.isStandard() && |
| 3542 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3543 | // Objective-C ARC writeback conversion. |
| 3544 | |
| 3545 | // We should copy unless we're passing to an argument explicitly |
| 3546 | // marked 'out'. |
| 3547 | bool ShouldCopy = true; |
| 3548 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3549 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3550 | |
| 3551 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3552 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3553 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3554 | ImplicitConversionSequence LvalueICS; |
| 3555 | LvalueICS.setStandard(); |
| 3556 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3557 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3558 | LvalueICS.Standard.First = ICS.Standard.First; |
| 3559 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
| 3560 | } |
| 3561 | |
| 3562 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3563 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3564 | DeclAccessPair dap; |
| 3565 | if (Initializer->getType() == Context.OverloadTy && |
| 3566 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3567 | , DestType, false, dap)) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3568 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3569 | else |
| 3570 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3571 | } else { |
| 3572 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3573 | |
| 3574 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3575 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | InitializationSequence::~InitializationSequence() { |
| 3579 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 3580 | StepEnd = Steps.end(); |
| 3581 | Step != StepEnd; ++Step) |
| 3582 | Step->Destroy(); |
| 3583 | } |
| 3584 | |
| 3585 | //===----------------------------------------------------------------------===// |
| 3586 | // Perform initialization |
| 3587 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3589 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3590 | switch(Entity.getKind()) { |
| 3591 | case InitializedEntity::EK_Variable: |
| 3592 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3593 | case InitializedEntity::EK_Exception: |
| 3594 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3595 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3596 | return Sema::AA_Initializing; |
| 3597 | |
| 3598 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3600 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3601 | return Sema::AA_Sending; |
| 3602 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3603 | return Sema::AA_Passing; |
| 3604 | |
| 3605 | case InitializedEntity::EK_Result: |
| 3606 | return Sema::AA_Returning; |
| 3607 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3608 | case InitializedEntity::EK_Temporary: |
| 3609 | // FIXME: Can we tell apart casting vs. converting? |
| 3610 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3611 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3612 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3613 | case InitializedEntity::EK_ArrayElement: |
| 3614 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3615 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3616 | return Sema::AA_Initializing; |
| 3617 | } |
| 3618 | |
| 3619 | return Sema::AA_Converting; |
| 3620 | } |
| 3621 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3622 | /// \brief Whether we should binding a created object as a temporary when |
| 3623 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3624 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3625 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3626 | case InitializedEntity::EK_ArrayElement: |
| 3627 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3628 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3629 | case InitializedEntity::EK_New: |
| 3630 | case InitializedEntity::EK_Variable: |
| 3631 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3632 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3633 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3634 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3635 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3636 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3638 | case InitializedEntity::EK_Parameter: |
| 3639 | case InitializedEntity::EK_Temporary: |
| 3640 | return true; |
| 3641 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3643 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3644 | } |
| 3645 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3646 | /// \brief Whether the given entity, when initialized with an object |
| 3647 | /// created for that initialization, requires destruction. |
| 3648 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3649 | switch (Entity.getKind()) { |
| 3650 | case InitializedEntity::EK_Member: |
| 3651 | case InitializedEntity::EK_Result: |
| 3652 | case InitializedEntity::EK_New: |
| 3653 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3654 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3655 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3656 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3657 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3658 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3659 | case InitializedEntity::EK_Variable: |
| 3660 | case InitializedEntity::EK_Parameter: |
| 3661 | case InitializedEntity::EK_Temporary: |
| 3662 | case InitializedEntity::EK_ArrayElement: |
| 3663 | case InitializedEntity::EK_Exception: |
| 3664 | return true; |
| 3665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | |
| 3667 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3670 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3671 | /// provided by the given initializer by calling the appropriate copy |
| 3672 | /// constructor. |
| 3673 | /// |
| 3674 | /// \param S The Sema object used for type-checking. |
| 3675 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3676 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3677 | /// the type of the initializer expression or a superclass thereof. |
| 3678 | /// |
| 3679 | /// \param Enter The entity being initialized. |
| 3680 | /// |
| 3681 | /// \param CurInit The initializer expression. |
| 3682 | /// |
| 3683 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3684 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3685 | /// an rvalue. |
| 3686 | /// |
| 3687 | /// \returns An expression that copies the initializer expression into |
| 3688 | /// a temporary object, or an error expression if a copy could not be |
| 3689 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3690 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3691 | QualType T, |
| 3692 | const InitializedEntity &Entity, |
| 3693 | ExprResult CurInit, |
| 3694 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3695 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3696 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3698 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3699 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3700 | if (!Class) |
| 3701 | return move(CurInit); |
| 3702 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3703 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3704 | // When certain criteria are met, an implementation is allowed to |
| 3705 | // omit the copy/move construction of a class object, even if the |
| 3706 | // copy/move constructor and/or destructor for the object have |
| 3707 | // side effects. [...] |
| 3708 | // - when a temporary class object that has not been bound to a |
| 3709 | // reference (12.2) would be copied/moved to a class object |
| 3710 | // with the same cv-unqualified type, the copy/move operation |
| 3711 | // can be omitted by constructing the temporary object |
| 3712 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3714 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3715 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3717 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3718 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3719 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3720 | switch (Entity.getKind()) { |
| 3721 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3722 | Loc = Entity.getReturnLoc(); |
| 3723 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3724 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3725 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3726 | Loc = Entity.getThrowLoc(); |
| 3727 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3729 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3730 | Loc = Entity.getDecl()->getLocation(); |
| 3731 | break; |
| 3732 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3733 | case InitializedEntity::EK_ArrayElement: |
| 3734 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3735 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3736 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3737 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3738 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3739 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3740 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3741 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3742 | Loc = CurInitExpr->getLocStart(); |
| 3743 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3744 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3745 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3746 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3747 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3748 | return move(CurInit); |
| 3749 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3750 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3751 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3752 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3753 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3754 | Con != ConEnd; ++Con) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3755 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3756 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3757 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3758 | CXXConstructorDecl *Constructor = 0; |
| 3759 | |
| 3760 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3761 | // Handle copy/moveconstructors, only. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3762 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3763 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3764 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3765 | continue; |
| 3766 | |
| 3767 | DeclAccessPair FoundDecl |
| 3768 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3769 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3770 | &CurInitExpr, 1, CandidateSet); |
| 3771 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3772 | } |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3773 | |
| 3774 | // Handle constructor templates. |
| 3775 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3776 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3777 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3779 | Constructor = cast<CXXConstructorDecl>( |
| 3780 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3781 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3782 | continue; |
| 3783 | |
| 3784 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3785 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3786 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3787 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3788 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3789 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3792 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3793 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3794 | case OR_Success: |
| 3795 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3796 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3797 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3798 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3799 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3800 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3801 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3802 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3803 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3804 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3805 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3806 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3808 | case OR_Ambiguous: |
| 3809 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3810 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3811 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3812 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3813 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3815 | case OR_Deleted: |
| 3816 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3817 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3818 | << CurInitExpr->getSourceRange(); |
| 3819 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3820 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3821 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3824 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3825 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3826 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3827 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3828 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3829 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3830 | |
| 3831 | if (IsExtraneousCopy) { |
| 3832 | // If this is a totally extraneous copy for C++03 reference |
| 3833 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3834 | // expression. We don't generate an (elided) copy operation here |
| 3835 | // because doing so would require us to pass down a flag to avoid |
| 3836 | // infinite recursion, where each step adds another extraneous, |
| 3837 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3838 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3839 | // Instantiate the default arguments of any extra parameters in |
| 3840 | // the selected copy constructor, as if we were going to create a |
| 3841 | // proper call to the copy constructor. |
| 3842 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 3843 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 3844 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 3845 | S.PDiag(diag::err_call_incomplete_argument))) |
| 3846 | break; |
| 3847 | |
| 3848 | // Build the default argument expression; we don't actually care |
| 3849 | // if this succeeds or not, because this routine will complain |
| 3850 | // if there was a problem. |
| 3851 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 3852 | } |
| 3853 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3854 | return S.Owned(CurInitExpr); |
| 3855 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3856 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3857 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 3858 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3859 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3860 | // constructor call (we might have derived-to-base conversions, or |
| 3861 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3862 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3863 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3864 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3865 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3866 | // Actually perform the constructor call. |
| 3867 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3868 | move_arg(ConstructorArgs), |
| 3869 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3870 | CXXConstructExpr::CK_Complete, |
| 3871 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3872 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3873 | // If we're supposed to bind temporaries, do so. |
| 3874 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 3875 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3876 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3877 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3879 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 3880 | const InitializedEntity &Entity) { |
| 3881 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 3882 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 3883 | return; |
| 3884 | |
| 3885 | if (Entity.getDecl()->getDeclName()) |
| 3886 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 3887 | << Entity.getDecl()->getDeclName(); |
| 3888 | else |
| 3889 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 3890 | } |
| 3891 | } |
| 3892 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame^] | 3893 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 3894 | return s.Kind == InitializationSequence::SK_BindReference || |
| 3895 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 3896 | } |
| 3897 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3899 | InitializationSequence::Perform(Sema &S, |
| 3900 | const InitializedEntity &Entity, |
| 3901 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3902 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3903 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3904 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3905 | unsigned NumArgs = Args.size(); |
| 3906 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3907 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3908 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3909 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3910 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3911 | // If the declaration is a non-dependent, incomplete array type |
| 3912 | // that has an initializer, then its type will be completed once |
| 3913 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3914 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3915 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3916 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3917 | if (const IncompleteArrayType *ArrayT |
| 3918 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 3919 | // FIXME: We don't currently have the ability to accurately |
| 3920 | // compute the length of an initializer list without |
| 3921 | // performing full type-checking of the initializer list |
| 3922 | // (since we have to determine where braces are implicitly |
| 3923 | // introduced and such). So, we fall back to making the array |
| 3924 | // type a dependently-sized array type with no specified |
| 3925 | // bound. |
| 3926 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 3927 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3928 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3929 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3930 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 3931 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 3932 | TypeLoc TL = TInfo->getTypeLoc(); |
| 3933 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 3934 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 3935 | Brackets = ArrayLoc->getBracketsRange(); |
| 3936 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | *ResultType |
| 3940 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 3941 | /*NumElts=*/0, |
| 3942 | ArrayT->getSizeModifier(), |
| 3943 | ArrayT->getIndexTypeCVRQualifiers(), |
| 3944 | Brackets); |
| 3945 | } |
| 3946 | |
| 3947 | } |
| 3948 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 3949 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 3950 | Kind.isExplicitCast()); |
| 3951 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3954 | // No steps means no initialization. |
| 3955 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3956 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3957 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3958 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 3959 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3960 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 3961 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3962 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3963 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3964 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3965 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3966 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3967 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3968 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3969 | // grab the only argument out the Args and place it into the "current" |
| 3970 | // initializer. |
| 3971 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3972 | case SK_ResolveAddressOfOverloadedFunction: |
| 3973 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3974 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3975 | case SK_CastDerivedToBaseLValue: |
| 3976 | case SK_BindReference: |
| 3977 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3978 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3979 | case SK_UserConversion: |
| 3980 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3981 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3982 | case SK_QualificationConversionRValue: |
| 3983 | case SK_ConversionSequence: |
| 3984 | case SK_ListInitialization: |
| 3985 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3986 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3987 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3988 | case SK_ArrayInit: |
| 3989 | case SK_PassByIndirectCopyRestore: |
| 3990 | case SK_PassByIndirectRestore: |
| 3991 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3992 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3993 | CurInit = Args.get()[0]; |
| 3994 | if (!CurInit.get()) return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3995 | |
| 3996 | // Read from a property when initializing something with it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3997 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 3998 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 3999 | if (CurInit.isInvalid()) |
| 4000 | return ExprError(); |
| 4001 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4002 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4003 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4005 | case SK_ConstructorInitialization: |
| 4006 | case SK_ZeroInitialization: |
| 4007 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4008 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | |
| 4010 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4011 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4012 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4013 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4014 | Step != StepEnd; ++Step) { |
| 4015 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4016 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4018 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4020 | switch (Step->Kind) { |
| 4021 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4023 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4024 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4025 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4026 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4027 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4028 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4029 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4030 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4031 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4032 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4033 | case SK_CastDerivedToBaseLValue: { |
| 4034 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4035 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4036 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4037 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4039 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4040 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4041 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4042 | CurInit.get()->getLocStart(), |
| 4043 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4044 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4045 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4047 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4048 | QualType T = SourceType; |
| 4049 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4050 | T = Pointer->getPointeeType(); |
| 4051 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4052 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4053 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4054 | } |
| 4055 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4056 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4057 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4058 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4059 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4060 | VK_XValue : |
| 4061 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4062 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4063 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4064 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4065 | CurInit.get(), |
| 4066 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4067 | break; |
| 4068 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4070 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4071 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4072 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4073 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4074 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4075 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4076 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4077 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4078 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4079 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4080 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4081 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4082 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4083 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4084 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4085 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4086 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4087 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4088 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4089 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4090 | // Reference binding does not have any corresponding ASTs. |
| 4091 | |
| 4092 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4094 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4095 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4096 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4098 | case SK_BindReferenceToTemporary: |
| 4099 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4100 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4101 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4103 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4104 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4105 | Entity.getType().getNonReferenceType(), |
| 4106 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4107 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4108 | |
| 4109 | // If we're binding to an Objective-C object that has lifetime, we |
| 4110 | // need cleanups. |
| 4111 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4112 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4113 | S.ExprNeedsCleanups = true; |
| 4114 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4115 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4117 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4118 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4119 | /*IsExtraneousCopy=*/true); |
| 4120 | break; |
| 4121 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4122 | case SK_UserConversion: { |
| 4123 | // We have a user-defined conversion that invokes either a constructor |
| 4124 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4125 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4126 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4127 | FunctionDecl *Fn = Step->Function.Function; |
| 4128 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4129 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4130 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4131 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4132 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4133 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4134 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4135 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4136 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4137 | // Determine the arguments required to actually perform the constructor |
| 4138 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4139 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4140 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4141 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4142 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4143 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4144 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4145 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4146 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4147 | move_arg(ConstructorArgs), |
| 4148 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4149 | CXXConstructExpr::CK_Complete, |
| 4150 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4151 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4152 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4153 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4154 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4155 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4156 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4157 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4158 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4159 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4160 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4161 | S.IsDerivedFrom(SourceType, Class)) |
| 4162 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4164 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4165 | } else { |
| 4166 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4167 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4168 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4169 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4170 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4171 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4172 | |
| 4173 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4174 | // derived-to-base conversion? I believe the answer is "no", because |
| 4175 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4176 | ExprResult CurInitExprRes = |
| 4177 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4178 | FoundFn, Conversion); |
| 4179 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4180 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4181 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4182 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4183 | // Build the actual call to the conversion function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4184 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4185 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4186 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4187 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4188 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4189 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4190 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4191 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame^] | 4193 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4194 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4195 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4196 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4197 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4198 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4199 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4200 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4201 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4202 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4203 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4204 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4205 | } |
| 4206 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4208 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4209 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4210 | CurInit.get()->getType(), |
| 4211 | CastKind, CurInit.get(), 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4212 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4214 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4215 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4216 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4217 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4218 | break; |
| 4219 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4221 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4222 | case SK_QualificationConversionXValue: |
| 4223 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4224 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4225 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4226 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4227 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4228 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4229 | VK_XValue : |
| 4230 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4231 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4232 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4235 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4236 | Sema::CheckedConversionKind CCK |
| 4237 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4238 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4239 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4240 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4241 | ExprResult CurInitExprRes = |
| 4242 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4243 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4244 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4245 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4246 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4247 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4250 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4251 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4252 | QualType Ty = Step->Type; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4253 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4254 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4255 | |
| 4256 | CurInit.release(); |
| 4257 | CurInit = S.Owned(InitList); |
| 4258 | break; |
| 4259 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4260 | |
| 4261 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4262 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4263 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4264 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4266 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4267 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4268 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4269 | ? Kind.getEqualLoc() |
| 4270 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4271 | |
| 4272 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4273 | // Force even a trivial, implicit default constructor to be |
| 4274 | // semantically checked. We do this explicitly because we don't build |
| 4275 | // the definition for completely trivial constructors. |
| 4276 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4277 | assert(ClassDecl && "No parent class for constructor."); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4278 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4279 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4280 | !Constructor->isUsed(false)) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4281 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4282 | } |
| 4283 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4284 | // Determine the arguments required to actually perform the constructor |
| 4285 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4286 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4287 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4288 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | |
| 4290 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4291 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4292 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4293 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4294 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4295 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4296 | unsigned NumExprs = ConstructorArgs.size(); |
| 4297 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4298 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4299 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4301 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4302 | if (!TSInfo) |
| 4303 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4304 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4305 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4306 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4307 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4309 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4310 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4311 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4312 | } else { |
| 4313 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4314 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4316 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4317 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4319 | CXXConstructExpr::CK_NonVirtualBase; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4320 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4321 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4324 | // Only get the parenthesis range if it is a direct construction. |
| 4325 | SourceRange parenRange = |
| 4326 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4327 | Kind.getParenRange() : SourceRange(); |
| 4328 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4329 | // If the entity allows NRVO, mark the construction as elidable |
| 4330 | // unconditionally. |
| 4331 | if (Entity.allowsNRVO()) |
| 4332 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4333 | Constructor, /*Elidable=*/true, |
| 4334 | move_arg(ConstructorArgs), |
| 4335 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4336 | ConstructKind, |
| 4337 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4338 | else |
| 4339 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4341 | move_arg(ConstructorArgs), |
| 4342 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4343 | ConstructKind, |
| 4344 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4345 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4346 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4347 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4348 | |
| 4349 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4350 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4351 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4352 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4354 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4355 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4357 | break; |
| 4358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4360 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4361 | step_iterator NextStep = Step; |
| 4362 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4363 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4364 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4365 | // The need for zero-initialization is recorded directly into |
| 4366 | // the call to the object's constructor within the next step. |
| 4367 | ConstructorInitRequiresZeroInit = true; |
| 4368 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4369 | S.getLangOptions().CPlusPlus && |
| 4370 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4371 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4372 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4374 | Kind.getRange().getBegin()); |
| 4375 | |
| 4376 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4377 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4378 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4379 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4380 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4381 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4382 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4383 | break; |
| 4384 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4385 | |
| 4386 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4387 | QualType SourceType = CurInit.get()->getType(); |
| 4388 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4389 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4390 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4391 | if (Result.isInvalid()) |
| 4392 | return ExprError(); |
| 4393 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4394 | |
| 4395 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4396 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4397 | if (ConvTy != Sema::Compatible && |
| 4398 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4399 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4400 | == Sema::Compatible) |
| 4401 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4402 | if (CurInitExprRes.isInvalid()) |
| 4403 | return ExprError(); |
| 4404 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4406 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4407 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4408 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4409 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4410 | getAssignmentAction(Entity), |
| 4411 | &Complained)) { |
| 4412 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4413 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4414 | } else if (Complained) |
| 4415 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4416 | break; |
| 4417 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4418 | |
| 4419 | case SK_StringInit: { |
| 4420 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4421 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4422 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4423 | break; |
| 4424 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4425 | |
| 4426 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4427 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4428 | CK_ObjCObjectLValueCast, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4429 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4430 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4431 | |
| 4432 | case SK_ArrayInit: |
| 4433 | // Okay: we checked everything before creating this step. Note that |
| 4434 | // this is a GNU extension. |
| 4435 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4436 | << Step->Type << CurInit.get()->getType() |
| 4437 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4438 | |
| 4439 | // If the destination type is an incomplete array type, update the |
| 4440 | // type accordingly. |
| 4441 | if (ResultType) { |
| 4442 | if (const IncompleteArrayType *IncompleteDest |
| 4443 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4444 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4445 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4446 | *ResultType = S.Context.getConstantArrayType( |
| 4447 | IncompleteDest->getElementType(), |
| 4448 | ConstantSource->getSize(), |
| 4449 | ArrayType::Normal, 0); |
| 4450 | } |
| 4451 | } |
| 4452 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4453 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4454 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4455 | case SK_PassByIndirectCopyRestore: |
| 4456 | case SK_PassByIndirectRestore: |
| 4457 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4458 | CurInit = S.Owned(new (S.Context) |
| 4459 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4460 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4461 | break; |
| 4462 | |
| 4463 | case SK_ProduceObjCObject: |
| 4464 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 4465 | CK_ObjCProduceObject, |
| 4466 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4467 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4468 | } |
| 4469 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4470 | |
| 4471 | // Diagnose non-fatal problems with the completed initialization. |
| 4472 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4473 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4474 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4475 | cast<FieldDecl>(Entity.getDecl()), |
| 4476 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4478 | return move(CurInit); |
| 4479 | } |
| 4480 | |
| 4481 | //===----------------------------------------------------------------------===// |
| 4482 | // Diagnose initialization failures |
| 4483 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4485 | const InitializedEntity &Entity, |
| 4486 | const InitializationKind &Kind, |
| 4487 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4488 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4489 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4491 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4492 | switch (Failure) { |
| 4493 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4494 | // FIXME: Customize for the initialized entity? |
| 4495 | if (NumArgs == 0) |
| 4496 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4497 | << DestType.getNonReferenceType(); |
| 4498 | else // FIXME: diagnostic below could be better! |
| 4499 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4500 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4501 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4502 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4503 | case FK_ArrayNeedsInitList: |
| 4504 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4505 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4506 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4507 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4509 | case FK_ArrayTypeMismatch: |
| 4510 | case FK_NonConstantArrayInit: |
| 4511 | S.Diag(Kind.getLocation(), |
| 4512 | (Failure == FK_ArrayTypeMismatch |
| 4513 | ? diag::err_array_init_different_type |
| 4514 | : diag::err_array_init_non_constant_array)) |
| 4515 | << DestType.getNonReferenceType() |
| 4516 | << Args[0]->getType() |
| 4517 | << Args[0]->getSourceRange(); |
| 4518 | break; |
| 4519 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4520 | case FK_AddressOfOverloadFailed: { |
| 4521 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4523 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4524 | true, |
| 4525 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4526 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4527 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4528 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4529 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4530 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4531 | switch (FailedOverloadResult) { |
| 4532 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4533 | if (Failure == FK_UserConversionOverloadFailed) |
| 4534 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4535 | << Args[0]->getType() << DestType |
| 4536 | << Args[0]->getSourceRange(); |
| 4537 | else |
| 4538 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4539 | << DestType << Args[0]->getType() |
| 4540 | << Args[0]->getSourceRange(); |
| 4541 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4542 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4543 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4544 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4545 | case OR_No_Viable_Function: |
| 4546 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4547 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4548 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4549 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4550 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4552 | case OR_Deleted: { |
| 4553 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4554 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4555 | << Args[0]->getSourceRange(); |
| 4556 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4557 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4558 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4559 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4560 | if (Ovl == OR_Deleted) { |
| 4561 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4562 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4563 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4564 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4565 | } |
| 4566 | break; |
| 4567 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4569 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4570 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | break; |
| 4572 | } |
| 4573 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4574 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4575 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4576 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4578 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4579 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4580 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4581 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4582 | << DestType.getNonReferenceType() |
| 4583 | << Args[0]->getType() |
| 4584 | << Args[0]->getSourceRange(); |
| 4585 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4586 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4587 | case FK_RValueReferenceBindingToLValue: |
| 4588 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4589 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4590 | << Args[0]->getSourceRange(); |
| 4591 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4592 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4593 | case FK_ReferenceInitDropsQualifiers: |
| 4594 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4595 | << DestType.getNonReferenceType() |
| 4596 | << Args[0]->getType() |
| 4597 | << Args[0]->getSourceRange(); |
| 4598 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4600 | case FK_ReferenceInitFailed: |
| 4601 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4602 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4603 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4604 | << Args[0]->getType() |
| 4605 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4606 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4607 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4608 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4609 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4610 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4611 | case FK_ConversionFailed: { |
| 4612 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4613 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4614 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4615 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4616 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4617 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4618 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4619 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4620 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4621 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4622 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4623 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4624 | |
| 4625 | case FK_ConversionFromPropertyFailed: |
| 4626 | // No-op. This error has already been reported. |
| 4627 | break; |
| 4628 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4629 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4630 | SourceRange R; |
| 4631 | |
| 4632 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4633 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4634 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4636 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4638 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4639 | if (Kind.isCStyleOrFunctionalCast()) |
| 4640 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4641 | << R; |
| 4642 | else |
| 4643 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4644 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4645 | break; |
| 4646 | } |
| 4647 | |
| 4648 | case FK_ReferenceBindingToInitList: |
| 4649 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4650 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4651 | break; |
| 4652 | |
| 4653 | case FK_InitListBadDestinationType: |
| 4654 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4655 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4656 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4657 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4658 | case FK_ConstructorOverloadFailed: { |
| 4659 | SourceRange ArgsRange; |
| 4660 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4661 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4662 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4664 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4665 | // bad. |
| 4666 | switch (FailedOverloadResult) { |
| 4667 | case OR_Ambiguous: |
| 4668 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4669 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4670 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4671 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4672 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4674 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4675 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4676 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4677 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4678 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4679 | // This is implicit default initialization of a member or |
| 4680 | // base within a constructor. If no viable function was |
| 4681 | // found, notify the user that she needs to explicitly |
| 4682 | // initialize this base/member. |
| 4683 | CXXConstructorDecl *Constructor |
| 4684 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4685 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4686 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4687 | << Constructor->isImplicit() |
| 4688 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4689 | << /*base=*/0 |
| 4690 | << Entity.getType(); |
| 4691 | |
| 4692 | RecordDecl *BaseDecl |
| 4693 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4694 | ->getDecl(); |
| 4695 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4696 | << S.Context.getTagDeclType(BaseDecl); |
| 4697 | } else { |
| 4698 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4699 | << Constructor->isImplicit() |
| 4700 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4701 | << /*member=*/1 |
| 4702 | << Entity.getName(); |
| 4703 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4704 | |
| 4705 | if (const RecordType *Record |
| 4706 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4708 | diag::note_previous_decl) |
| 4709 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4710 | } |
| 4711 | break; |
| 4712 | } |
| 4713 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4714 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4715 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4716 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4717 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4719 | case OR_Deleted: { |
| 4720 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4721 | << true << DestType << ArgsRange; |
| 4722 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4723 | OverloadingResult Ovl |
| 4724 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4725 | if (Ovl == OR_Deleted) { |
| 4726 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4727 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4728 | } else { |
| 4729 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4730 | } |
| 4731 | break; |
| 4732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4734 | case OR_Success: |
| 4735 | llvm_unreachable("Conversion did not fail!"); |
| 4736 | break; |
| 4737 | } |
| 4738 | break; |
| 4739 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4741 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4742 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4743 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4744 | // This is implicit default-initialization of a const member in |
| 4745 | // a constructor. Complain that it needs to be explicitly |
| 4746 | // initialized. |
| 4747 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4748 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4749 | << Constructor->isImplicit() |
| 4750 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4751 | << /*const=*/1 |
| 4752 | << Entity.getName(); |
| 4753 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4754 | << Entity.getName(); |
| 4755 | } else { |
| 4756 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4757 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4758 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4759 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4760 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4761 | case FK_Incomplete: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4762 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4763 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4767 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4768 | return true; |
| 4769 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4770 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4771 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 4772 | switch (SequenceKind) { |
| 4773 | case FailedSequence: { |
| 4774 | OS << "Failed sequence: "; |
| 4775 | switch (Failure) { |
| 4776 | case FK_TooManyInitsForReference: |
| 4777 | OS << "too many initializers for reference"; |
| 4778 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4779 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4780 | case FK_ArrayNeedsInitList: |
| 4781 | OS << "array requires initializer list"; |
| 4782 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4784 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4785 | OS << "array requires initializer list or string literal"; |
| 4786 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4788 | case FK_ArrayTypeMismatch: |
| 4789 | OS << "array type mismatch"; |
| 4790 | break; |
| 4791 | |
| 4792 | case FK_NonConstantArrayInit: |
| 4793 | OS << "non-constant array initializer"; |
| 4794 | break; |
| 4795 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4796 | case FK_AddressOfOverloadFailed: |
| 4797 | OS << "address of overloaded function failed"; |
| 4798 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4800 | case FK_ReferenceInitOverloadFailed: |
| 4801 | OS << "overload resolution for reference initialization failed"; |
| 4802 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4804 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4805 | OS << "non-const lvalue reference bound to temporary"; |
| 4806 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4808 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4809 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4810 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4812 | case FK_RValueReferenceBindingToLValue: |
| 4813 | OS << "rvalue reference bound to an lvalue"; |
| 4814 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4816 | case FK_ReferenceInitDropsQualifiers: |
| 4817 | OS << "reference initialization drops qualifiers"; |
| 4818 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4820 | case FK_ReferenceInitFailed: |
| 4821 | OS << "reference initialization failed"; |
| 4822 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4824 | case FK_ConversionFailed: |
| 4825 | OS << "conversion failed"; |
| 4826 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4827 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4828 | case FK_ConversionFromPropertyFailed: |
| 4829 | OS << "conversion from property failed"; |
| 4830 | break; |
| 4831 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4832 | case FK_TooManyInitsForScalar: |
| 4833 | OS << "too many initializers for scalar"; |
| 4834 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4836 | case FK_ReferenceBindingToInitList: |
| 4837 | OS << "referencing binding to initializer list"; |
| 4838 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4840 | case FK_InitListBadDestinationType: |
| 4841 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 4842 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4844 | case FK_UserConversionOverloadFailed: |
| 4845 | OS << "overloading failed for user-defined conversion"; |
| 4846 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4848 | case FK_ConstructorOverloadFailed: |
| 4849 | OS << "constructor overloading failed"; |
| 4850 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4852 | case FK_DefaultInitOfConst: |
| 4853 | OS << "default initialization of a const variable"; |
| 4854 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4855 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4856 | case FK_Incomplete: |
| 4857 | OS << "initialization of incomplete type"; |
| 4858 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4859 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4860 | OS << '\n'; |
| 4861 | return; |
| 4862 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4864 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4865 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4866 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4868 | case NormalSequence: |
| 4869 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4870 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4871 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4873 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 4874 | if (S != step_begin()) { |
| 4875 | OS << " -> "; |
| 4876 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4878 | switch (S->Kind) { |
| 4879 | case SK_ResolveAddressOfOverloadedFunction: |
| 4880 | OS << "resolve address of overloaded function"; |
| 4881 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4882 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4883 | case SK_CastDerivedToBaseRValue: |
| 4884 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 4885 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4887 | case SK_CastDerivedToBaseXValue: |
| 4888 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 4889 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4890 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4891 | case SK_CastDerivedToBaseLValue: |
| 4892 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 4893 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4894 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4895 | case SK_BindReference: |
| 4896 | OS << "bind reference to lvalue"; |
| 4897 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4899 | case SK_BindReferenceToTemporary: |
| 4900 | OS << "bind reference to a temporary"; |
| 4901 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4903 | case SK_ExtraneousCopyToTemporary: |
| 4904 | OS << "extraneous C++03 copy to temporary"; |
| 4905 | break; |
| 4906 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4907 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4908 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4909 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4910 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4911 | case SK_QualificationConversionRValue: |
| 4912 | OS << "qualification conversion (rvalue)"; |
| 4913 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4914 | case SK_QualificationConversionXValue: |
| 4915 | OS << "qualification conversion (xvalue)"; |
| 4916 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4917 | case SK_QualificationConversionLValue: |
| 4918 | OS << "qualification conversion (lvalue)"; |
| 4919 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4921 | case SK_ConversionSequence: |
| 4922 | OS << "implicit conversion sequence ("; |
| 4923 | S->ICS->DebugPrint(); // FIXME: use OS |
| 4924 | OS << ")"; |
| 4925 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4927 | case SK_ListInitialization: |
| 4928 | OS << "list initialization"; |
| 4929 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4931 | case SK_ConstructorInitialization: |
| 4932 | OS << "constructor initialization"; |
| 4933 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4934 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4935 | case SK_ZeroInitialization: |
| 4936 | OS << "zero initialization"; |
| 4937 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4939 | case SK_CAssignment: |
| 4940 | OS << "C assignment"; |
| 4941 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4943 | case SK_StringInit: |
| 4944 | OS << "string initialization"; |
| 4945 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4946 | |
| 4947 | case SK_ObjCObjectConversion: |
| 4948 | OS << "Objective-C object conversion"; |
| 4949 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4950 | |
| 4951 | case SK_ArrayInit: |
| 4952 | OS << "array initialization"; |
| 4953 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4954 | |
| 4955 | case SK_PassByIndirectCopyRestore: |
| 4956 | OS << "pass by indirect copy and restore"; |
| 4957 | break; |
| 4958 | |
| 4959 | case SK_PassByIndirectRestore: |
| 4960 | OS << "pass by indirect restore"; |
| 4961 | break; |
| 4962 | |
| 4963 | case SK_ProduceObjCObject: |
| 4964 | OS << "Objective-C object retension"; |
| 4965 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | } |
| 4969 | |
| 4970 | void InitializationSequence::dump() const { |
| 4971 | dump(llvm::errs()); |
| 4972 | } |
| 4973 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4974 | //===----------------------------------------------------------------------===// |
| 4975 | // Initialization helper functions |
| 4976 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 4977 | bool |
| 4978 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 4979 | ExprResult Init) { |
| 4980 | if (Init.isInvalid()) |
| 4981 | return false; |
| 4982 | |
| 4983 | Expr *InitE = Init.get(); |
| 4984 | assert(InitE && "No initialization expression"); |
| 4985 | |
| 4986 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 4987 | SourceLocation()); |
| 4988 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 4989 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4992 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4993 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 4994 | SourceLocation EqualLoc, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4995 | ExprResult Init) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4996 | if (Init.isInvalid()) |
| 4997 | return ExprError(); |
| 4998 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4999 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5000 | assert(InitE && "No initialization expression?"); |
| 5001 | |
| 5002 | if (EqualLoc.isInvalid()) |
| 5003 | EqualLoc = InitE->getLocStart(); |
| 5004 | |
| 5005 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5006 | EqualLoc); |
| 5007 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5008 | Init.release(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5009 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5010 | } |