Steve Naroff | f8ecff2 | 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 | 0cb7803 | 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 | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Designator.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 27 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 28 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Sema Initialization Checking |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 34 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 35 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 36 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 37 | return 0; |
| 38 | |
Chris Lattner | a919681 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Chris Lattner | a919681 | 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 | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 48 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 49 | |
| 50 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | a919681 | 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 | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 55 | |
Eli Friedman | 42a8465 | 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 | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 62 | return Init; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
John McCall | 66884dd | 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 | 5decec9 | 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 | d8b741c8 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 81 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 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 | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 83 | // being initialized to a string literal. |
| 84 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 85 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 86 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 87 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 88 | ConstVal, |
| 89 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 90 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 91 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 93 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 95 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 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 | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 98 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 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 | 554eba9 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Eli Friedman | 893abe4 | 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 | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 128 | //===----------------------------------------------------------------------===// |
| 129 | // Semantic checking for initializer lists. |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
Douglas Gregor | cde232f | 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 | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 146 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 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 | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 159 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 160 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 161 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 162 | bool hadError; |
| 163 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 164 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 166 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 167 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 168 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 169 | unsigned &StructuredIndex, |
| 170 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 171 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 172 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 173 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 174 | unsigned &StructuredIndex, |
| 175 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 176 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 177 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 179 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 180 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 181 | unsigned &StructuredIndex, |
| 182 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 183 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 184 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 185 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 186 | InitListExpr *StructuredList, |
| 187 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 188 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 189 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 190 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 191 | InitListExpr *StructuredList, |
| 192 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckReferenceType(const InitializedEntity &Entity, |
| 194 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 195 | unsigned &Index, |
| 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 198 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 199 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 200 | InitListExpr *StructuredList, |
| 201 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 202 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 203 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 205 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 207 | unsigned &StructuredIndex, |
| 208 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 209 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 210 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 212 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 213 | InitListExpr *StructuredList, |
| 214 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 215 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 216 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 217 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 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 | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 224 | bool FinishSubobjectInit, |
| 225 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 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 | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 231 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 232 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 233 | Expr *expr); |
| 234 | int numArrayElements(QualType DeclType); |
| 235 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 2bb0765 | 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 | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 240 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 241 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 242 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 243 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 244 | InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 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 | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 251 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 253 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 254 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 255 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 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 | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 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 | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 2bb0765 | 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 | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 288 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 289 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 290 | if (MemberInit.isInvalid()) { |
| 291 | hadError = true; |
| 292 | return; |
| 293 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | 2bb0765 | 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>()); |
| 299 | } else if (InitSeq.getKind() |
| 300 | == InitializationSequence::ConstructorInitialization) { |
| 301 | // Value-initialization requires a constructor call, so |
| 302 | // extend the initializer list to include the constructor |
| 303 | // call and make a note that we'll need to take another pass |
| 304 | // through the initializer list. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 305 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 306 | RequiresSecondPass = true; |
| 307 | } |
| 308 | } else if (InitListExpr *InnerILE |
| 309 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 310 | FillInValueInitializations(MemberEntity, InnerILE, |
| 311 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 314 | /// Recursively replaces NULL values within the given initializer list |
| 315 | /// with expressions that perform value-initialization of the |
| 316 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 317 | void |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 318 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 319 | InitListExpr *ILE, |
| 320 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 322 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 323 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 324 | if (ILE->getSyntacticForm()) |
| 325 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 327 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 328 | if (RType->getDecl()->isUnion() && |
| 329 | ILE->getInitializedFieldInUnion()) |
| 330 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 331 | Entity, ILE, RequiresSecondPass); |
| 332 | else { |
| 333 | unsigned Init = 0; |
| 334 | for (RecordDecl::field_iterator |
| 335 | Field = RType->getDecl()->field_begin(), |
| 336 | FieldEnd = RType->getDecl()->field_end(); |
| 337 | Field != FieldEnd; ++Field) { |
| 338 | if (Field->isUnnamedBitfield()) |
| 339 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 340 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 341 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 342 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 343 | |
| 344 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 345 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 346 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 347 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 348 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 349 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 350 | // Only look at the first initialization of a union. |
| 351 | if (RType->getDecl()->isUnion()) |
| 352 | break; |
| 353 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 358 | |
| 359 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 361 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 362 | unsigned NumInits = ILE->getNumInits(); |
| 363 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 364 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 365 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 366 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 367 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 368 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 369 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 370 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 371 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 372 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 373 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 374 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 376 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 378 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 379 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 380 | if (hadError) |
| 381 | return; |
| 382 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 383 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 384 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 385 | ElementEntity.setElementIndex(Init); |
| 386 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 387 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 388 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 389 | true); |
| 390 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 391 | if (!InitSeq) { |
| 392 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 393 | hadError = true; |
| 394 | return; |
| 395 | } |
| 396 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 397 | ExprResult ElementInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 398 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 399 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 400 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | |
| 404 | if (hadError) { |
| 405 | // Do nothing |
| 406 | } else if (Init < NumInits) { |
| 407 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame^] | 408 | } else { |
| 409 | // For arrays, just set the expression used for value-initialization |
| 410 | // of the rest of elements and exit. |
| 411 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 412 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | if (InitSeq.getKind() |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 417 | == InitializationSequence::ConstructorInitialization) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame^] | 418 | // Value-initialization requires a constructor call, so |
| 419 | // extend the initializer list to include the constructor |
| 420 | // call and make a note that we'll need to take another pass |
| 421 | // through the initializer list. |
| 422 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 423 | RequiresSecondPass = true; |
| 424 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 425 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 426 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 427 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 428 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 433 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 434 | InitListExpr *IL, QualType &T) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 435 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 436 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 437 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 438 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 439 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 441 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 442 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 443 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 444 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 446 | if (!hadError) { |
| 447 | bool RequiresSecondPass = false; |
| 448 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 449 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 450 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 451 | RequiresSecondPass); |
| 452 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 456 | // FIXME: use a proper constant |
| 457 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 458 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 459 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 460 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 461 | } |
| 462 | return maxElements; |
| 463 | } |
| 464 | |
| 465 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 466 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 467 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 469 | Field = structDecl->field_begin(), |
| 470 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 471 | Field != FieldEnd; ++Field) { |
| 472 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 473 | ++InitializableMembers; |
| 474 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 475 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 476 | return std::min(InitializableMembers, 1); |
| 477 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 480 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 481 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 482 | QualType T, unsigned &Index, |
| 483 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 484 | unsigned &StructuredIndex, |
| 485 | bool TopLevelObject) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 486 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 488 | if (T->isArrayType()) |
| 489 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 490 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 491 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 492 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 493 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 494 | else |
| 495 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 496 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 497 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 498 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 499 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 500 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 501 | hadError = true; |
| 502 | return; |
| 503 | } |
| 504 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 505 | // Build a structured initializer list corresponding to this subobject. |
| 506 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 508 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 509 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 510 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 511 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 513 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 514 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 516 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | StructuredSubobjectInitList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 518 | StructuredSubobjectInitIndex, |
| 519 | TopLevelObject); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 520 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 521 | StructuredSubobjectInitList->setType(T); |
| 522 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 523 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 524 | // range corresponds with the end of the last initializer it used. |
| 525 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 527 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 528 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 530 | |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 531 | // Warn about missing braces. |
| 532 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 5cbff48 | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 533 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 534 | diag::warn_missing_braces) |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 535 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 536 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 537 | "{") |
| 538 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 539 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 540 | "}"); |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 541 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 544 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 545 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 546 | unsigned &Index, |
| 547 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 548 | unsigned &StructuredIndex, |
| 549 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 550 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 551 | SyntacticToSemantic[IList] = StructuredList; |
| 552 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 553 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 554 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 555 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 556 | IList->setType(ExprTy); |
| 557 | StructuredList->setType(ExprTy); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 558 | if (hadError) |
| 559 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 560 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 561 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 562 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 563 | if (StructuredIndex == 1 && |
| 564 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 565 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 566 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 567 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 568 | hadError = true; |
| 569 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 570 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 571 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 572 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 573 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 574 | // Don't complain for incomplete types, since we'll get an error |
| 575 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 576 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 578 | CurrentObjectType->isArrayType()? 0 : |
| 579 | CurrentObjectType->isVectorType()? 1 : |
| 580 | CurrentObjectType->isScalarType()? 2 : |
| 581 | CurrentObjectType->isUnionType()? 3 : |
| 582 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 583 | |
| 584 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 585 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 586 | DK = diag::err_excess_initializers; |
| 587 | hadError = true; |
| 588 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 589 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 590 | DK = diag::err_excess_initializers; |
| 591 | hadError = true; |
| 592 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 593 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 594 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 595 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 598 | |
Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 599 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 600 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 601 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 602 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 603 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 606 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 607 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 609 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 610 | unsigned &Index, |
| 611 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 612 | unsigned &StructuredIndex, |
| 613 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 614 | if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 615 | CheckScalarType(Entity, IList, DeclType, Index, |
| 616 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 617 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 619 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 620 | } else if (DeclType->isAggregateType()) { |
| 621 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 622 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 623 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 624 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 625 | StructuredList, StructuredIndex, |
| 626 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 627 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 628 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 629 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 630 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 631 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 632 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 633 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 634 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 636 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 637 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 638 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 639 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 640 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 641 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 642 | } else if (DeclType->isRecordType()) { |
| 643 | // C++ [dcl.init]p14: |
| 644 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 645 | // is a brace-enclosed list, see 8.5.1. |
| 646 | // |
| 647 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 648 | // we have an initializer list and a destination type that is not |
| 649 | // an aggregate. |
| 650 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 651 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 652 | << DeclType << IList->getSourceRange(); |
| 653 | hadError = true; |
| 654 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 655 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 656 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 657 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 658 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 659 | << DeclType; |
| 660 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 661 | } else { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 662 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 663 | << DeclType; |
| 664 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 668 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 669 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 671 | unsigned &Index, |
| 672 | InitListExpr *StructuredList, |
| 673 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 674 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 675 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 676 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 677 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 679 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 680 | StructuredList, StructuredIndex, |
| 681 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 682 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 683 | newStructuredList, newStructuredIndex); |
| 684 | ++StructuredIndex; |
| 685 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 686 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 687 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 688 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 689 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 690 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 691 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 692 | StructuredList, StructuredIndex); |
| 693 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 694 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 695 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 696 | // arrayType can be incomplete if we're initializing a flexible |
| 697 | // array member. There's nothing we can do with the completed |
| 698 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 700 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 701 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 702 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 703 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 704 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 705 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 706 | |
| 707 | // Fall through for subaggregate initialization. |
| 708 | |
| 709 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 710 | // C++ [dcl.init.aggr]p12: |
| 711 | // All implicit type conversions (clause 4) are considered when |
| 712 | // initializing the aggregate member with an ini- tializer from |
| 713 | // an initializer-list. If the initializer can initialize a |
| 714 | // member, the member is initialized. [...] |
| 715 | |
| 716 | // FIXME: Better EqualLoc? |
| 717 | InitializationKind Kind = |
| 718 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 719 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 720 | |
| 721 | if (Seq) { |
| 722 | ExprResult Result = |
| 723 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 724 | if (Result.isInvalid()) |
| 725 | hadError = true; |
| 726 | |
| 727 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 728 | Result.takeAs<Expr>()); |
| 729 | ++Index; |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | // Fall through for subaggregate initialization |
| 734 | } else { |
| 735 | // C99 6.7.8p13: |
| 736 | // |
| 737 | // The initializer for a structure or union object that has |
| 738 | // automatic storage duration shall be either an initializer |
| 739 | // list as described below, or a single expression that has |
| 740 | // compatible structure or union type. In the latter case, the |
| 741 | // initial value of the object, including unnamed members, is |
| 742 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 743 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 744 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 745 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 746 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 747 | if (ExprRes.isInvalid()) |
| 748 | hadError = true; |
| 749 | else { |
| 750 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 751 | if (ExprRes.isInvalid()) |
| 752 | hadError = true; |
| 753 | } |
| 754 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 755 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 756 | ++Index; |
| 757 | return; |
| 758 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 759 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 760 | // Fall through for subaggregate initialization |
| 761 | } |
| 762 | |
| 763 | // C++ [dcl.init.aggr]p12: |
| 764 | // |
| 765 | // [...] Otherwise, if the member is itself a non-empty |
| 766 | // subaggregate, brace elision is assumed and the initializer is |
| 767 | // considered for the initialization of the first member of |
| 768 | // the subaggregate. |
| 769 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 770 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 771 | StructuredIndex); |
| 772 | ++StructuredIndex; |
| 773 | } else { |
| 774 | // We cannot initialize this element, so let |
| 775 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 776 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 777 | SemaRef.Owned(expr)); |
| 778 | hadError = true; |
| 779 | ++Index; |
| 780 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 781 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 784 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 785 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 786 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 787 | InitListExpr *StructuredList, |
| 788 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 789 | if (Index >= IList->getNumInits()) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 790 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 791 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 792 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 793 | ++Index; |
| 794 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 795 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 796 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 797 | |
| 798 | Expr *expr = IList->getInit(Index); |
| 799 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 800 | SemaRef.Diag(SubIList->getLocStart(), |
| 801 | diag::warn_many_braces_around_scalar_init) |
| 802 | << SubIList->getSourceRange(); |
| 803 | |
| 804 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 805 | StructuredIndex); |
| 806 | return; |
| 807 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 808 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 809 | diag::err_designator_for_scalar_init) |
| 810 | << DeclType << expr->getSourceRange(); |
| 811 | hadError = true; |
| 812 | ++Index; |
| 813 | ++StructuredIndex; |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | ExprResult Result = |
| 818 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 819 | SemaRef.Owned(expr)); |
| 820 | |
| 821 | Expr *ResultExpr = 0; |
| 822 | |
| 823 | if (Result.isInvalid()) |
| 824 | hadError = true; // types weren't compatible. |
| 825 | else { |
| 826 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 828 | if (ResultExpr != expr) { |
| 829 | // The type was promoted, update initializer list. |
| 830 | IList->setInit(Index, ResultExpr); |
| 831 | } |
| 832 | } |
| 833 | if (hadError) |
| 834 | ++StructuredIndex; |
| 835 | else |
| 836 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 837 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 840 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 841 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 842 | unsigned &Index, |
| 843 | InitListExpr *StructuredList, |
| 844 | unsigned &StructuredIndex) { |
| 845 | if (Index < IList->getNumInits()) { |
| 846 | Expr *expr = IList->getInit(Index); |
| 847 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 848 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 849 | << DeclType << IList->getSourceRange(); |
| 850 | hadError = true; |
| 851 | ++Index; |
| 852 | ++StructuredIndex; |
| 853 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 855 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 856 | ExprResult Result = |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 857 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 858 | SemaRef.Owned(expr)); |
| 859 | |
| 860 | if (Result.isInvalid()) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 861 | hadError = true; |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 862 | |
| 863 | expr = Result.takeAs<Expr>(); |
| 864 | IList->setInit(Index, expr); |
| 865 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 866 | if (hadError) |
| 867 | ++StructuredIndex; |
| 868 | else |
| 869 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 870 | ++Index; |
| 871 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 872 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 873 | // general, it would be useful to pass location information down the stack, |
| 874 | // so that we know the location (or decl) of the "current object" being |
| 875 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 877 | diag::err_init_reference_member_uninitialized) |
| 878 | << DeclType |
| 879 | << IList->getSourceRange(); |
| 880 | hadError = true; |
| 881 | ++Index; |
| 882 | ++StructuredIndex; |
| 883 | return; |
| 884 | } |
| 885 | } |
| 886 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 887 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 888 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 889 | unsigned &Index, |
| 890 | InitListExpr *StructuredList, |
| 891 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 892 | if (Index >= IList->getNumInits()) |
| 893 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 895 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 896 | unsigned maxElements = VT->getNumElements(); |
| 897 | unsigned numEltsInit = 0; |
| 898 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 899 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 900 | if (!SemaRef.getLangOptions().OpenCL) { |
| 901 | // If the initializing element is a vector, try to copy-initialize |
| 902 | // instead of breaking it apart (which is doomed to failure anyway). |
| 903 | Expr *Init = IList->getInit(Index); |
| 904 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 905 | ExprResult Result = |
| 906 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
| 907 | SemaRef.Owned(Init)); |
| 908 | |
| 909 | Expr *ResultExpr = 0; |
| 910 | if (Result.isInvalid()) |
| 911 | hadError = true; // types weren't compatible. |
| 912 | else { |
| 913 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 915 | if (ResultExpr != Init) { |
| 916 | // The type was promoted, update initializer list. |
| 917 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 920 | if (hadError) |
| 921 | ++StructuredIndex; |
| 922 | else |
| 923 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 924 | ++Index; |
| 925 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 926 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 928 | InitializedEntity ElementEntity = |
| 929 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 930 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 931 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 932 | // Don't attempt to go past the end of the init list |
| 933 | if (Index >= IList->getNumInits()) |
| 934 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 935 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 936 | ElementEntity.setElementIndex(Index); |
| 937 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 938 | StructuredList, StructuredIndex); |
| 939 | } |
| 940 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 941 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 942 | |
| 943 | InitializedEntity ElementEntity = |
| 944 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 945 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 946 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 947 | for (unsigned i = 0; i < maxElements; ++i) { |
| 948 | // Don't attempt to go past the end of the init list |
| 949 | if (Index >= IList->getNumInits()) |
| 950 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 952 | ElementEntity.setElementIndex(Index); |
| 953 | |
| 954 | QualType IType = IList->getInit(Index)->getType(); |
| 955 | if (!IType->isVectorType()) { |
| 956 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 957 | StructuredList, StructuredIndex); |
| 958 | ++numEltsInit; |
| 959 | } else { |
| 960 | QualType VecType; |
| 961 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 962 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 964 | if (IType->isExtVectorType()) |
| 965 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 966 | else |
| 967 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 968 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 969 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 970 | StructuredList, StructuredIndex); |
| 971 | numEltsInit += numIElts; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // OpenCL requires all elements to be initialized. |
| 976 | if (numEltsInit != maxElements) |
| 977 | if (SemaRef.getLangOptions().OpenCL) |
| 978 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 979 | diag::err_vector_incorrect_num_initializers) |
| 980 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 983 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 984 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 985 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 987 | unsigned &Index, |
| 988 | InitListExpr *StructuredList, |
| 989 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 990 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 991 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 992 | // Check for the special-case of initializing an array with a string. |
| 993 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 994 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 995 | SemaRef.Context)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 996 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 997 | // We place the string literal directly into the resulting |
| 998 | // initializer list. This is the only place where the structure |
| 999 | // of the structured initializer list doesn't match exactly, |
| 1000 | // because doing so would involve allocating one character |
| 1001 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1002 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1003 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1004 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1005 | return; |
| 1006 | } |
| 1007 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1008 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1009 | // Check for VLAs; in standard C it would be possible to check this |
| 1010 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1011 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1012 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1013 | diag::err_variable_object_no_init) |
| 1014 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1015 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1016 | ++Index; |
| 1017 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1018 | return; |
| 1019 | } |
| 1020 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1021 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1022 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1023 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1024 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1025 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1026 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1027 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1028 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1029 | maxElementsKnown = true; |
| 1030 | } |
| 1031 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1032 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1033 | while (Index < IList->getNumInits()) { |
| 1034 | Expr *Init = IList->getInit(Index); |
| 1035 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1036 | // If we're not the subobject that matches up with the '{' for |
| 1037 | // the designator, we shouldn't be handling the |
| 1038 | // designator. Return immediately. |
| 1039 | if (!SubobjectIsDesignatorContext) |
| 1040 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1042 | // Handle this designated initializer. elementIndex will be |
| 1043 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1044 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1045 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1046 | StructuredList, StructuredIndex, true, |
| 1047 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1048 | hadError = true; |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1052 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1053 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1054 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1055 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1056 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1058 | // If the array is of incomplete type, keep track of the number of |
| 1059 | // elements in the initializer. |
| 1060 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1061 | maxElements = elementIndex; |
| 1062 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | // If we know the maximum number of elements, and we've already |
| 1067 | // hit it, stop consuming elements in the initializer list. |
| 1068 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1069 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1070 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1071 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1073 | Entity); |
| 1074 | // Check this element. |
| 1075 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1076 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1077 | ++elementIndex; |
| 1078 | |
| 1079 | // If the array is of incomplete type, keep track of the number of |
| 1080 | // elements in the initializer. |
| 1081 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1082 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1083 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1084 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1085 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1086 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1087 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1088 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1089 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1090 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1091 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1092 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1093 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1094 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1096 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1100 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1101 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1103 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1105 | unsigned &Index, |
| 1106 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1107 | unsigned &StructuredIndex, |
| 1108 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1109 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1111 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1112 | // confusion, we forgo checking the intializer for the entire record. |
| 1113 | if (structDecl->isInvalidDecl()) { |
| 1114 | hadError = true; |
| 1115 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1119 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1120 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1121 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1122 | Field != FieldEnd; ++Field) { |
| 1123 | if (Field->getDeclName()) { |
| 1124 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1125 | break; |
| 1126 | } |
| 1127 | } |
| 1128 | return; |
| 1129 | } |
| 1130 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1131 | // If structDecl is a forward declaration, this loop won't do |
| 1132 | // anything except look at designated initializers; That's okay, |
| 1133 | // because an error should get printed out elsewhere. It might be |
| 1134 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1135 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1136 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1137 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1138 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1139 | while (Index < IList->getNumInits()) { |
| 1140 | Expr *Init = IList->getInit(Index); |
| 1141 | |
| 1142 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1143 | // If we're not the subobject that matches up with the '{' for |
| 1144 | // the designator, we shouldn't be handling the |
| 1145 | // designator. Return immediately. |
| 1146 | if (!SubobjectIsDesignatorContext) |
| 1147 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1149 | // Handle this designated initializer. Field will be updated to |
| 1150 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1151 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1152 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1153 | StructuredList, StructuredIndex, |
| 1154 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1155 | hadError = true; |
| 1156 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1157 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Disable check for missing fields when designators are used. |
| 1160 | // This matches gcc behaviour. |
| 1161 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | if (Field == FieldEnd) { |
| 1166 | // We've run out of fields. We're done. |
| 1167 | break; |
| 1168 | } |
| 1169 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1170 | // We've already initialized a member of a union. We're done. |
| 1171 | if (InitializedSomething && DeclType->isUnionType()) |
| 1172 | break; |
| 1173 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1174 | // If we've hit the flexible array member at the end, we're done. |
| 1175 | if (Field->getType()->isIncompleteArrayType()) |
| 1176 | break; |
| 1177 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1178 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1179 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1180 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1181 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1182 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1183 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1184 | InitializedEntity MemberEntity = |
| 1185 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1186 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1187 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1188 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (DeclType->isUnionType()) { |
| 1191 | // Initialize the first field within the union. |
| 1192 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1193 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1196 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1197 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1198 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1199 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1200 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1201 | // It is possible we have one or more unnamed bitfields remaining. |
| 1202 | // Find first (if any) named field and emit warning. |
| 1203 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1204 | it != end; ++it) { |
| 1205 | if (!it->isUnnamedBitfield()) { |
| 1206 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1207 | diag::warn_missing_field_initializers) << it->getName(); |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1214 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1215 | return; |
| 1216 | |
| 1217 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | if (!TopLevelObject && |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1219 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1220 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1222 | diag::err_flexible_array_init_nonempty) |
| 1223 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1224 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1225 | << *Field; |
| 1226 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1227 | ++Index; |
| 1228 | return; |
| 1229 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1231 | diag::ext_flexible_array_init) |
| 1232 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1233 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1234 | << *Field; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1237 | InitializedEntity MemberEntity = |
| 1238 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1239 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1240 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1241 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1242 | StructuredList, StructuredIndex); |
| 1243 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1245 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1246 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1248 | /// \brief Expand a field designator that refers to a member of an |
| 1249 | /// anonymous struct or union into a series of field designators that |
| 1250 | /// refers to the field within the appropriate subobject. |
| 1251 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1252 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | DesignatedInitExpr *DIE, |
| 1254 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1255 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1256 | typedef DesignatedInitExpr::Designator Designator; |
| 1257 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1258 | // Build the replacement designators. |
| 1259 | llvm::SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1260 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1261 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1262 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1264 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1265 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1266 | else |
| 1267 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1268 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1269 | assert(isa<FieldDecl>(*PI)); |
| 1270 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | // Expand the current designator into the set of replacement |
| 1274 | // designators, so we have a full subobject path down to where the |
| 1275 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1276 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1277 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1278 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1280 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1281 | /// corresponds to FieldName. |
| 1282 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1283 | IdentifierInfo *FieldName) { |
| 1284 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1285 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1286 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1287 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1288 | return IF; |
| 1289 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1290 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1291 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1294 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1295 | /// |
| 1296 | /// Determines whether the designated initializer @p DIE, which |
| 1297 | /// resides at the given @p Index within the initializer list @p |
| 1298 | /// IList, is well-formed for a current object of type @p DeclType |
| 1299 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1301 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1302 | /// |
| 1303 | /// @param IList The initializer list in which this designated |
| 1304 | /// initializer occurs. |
| 1305 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1306 | /// @param DIE The designated initializer expression. |
| 1307 | /// |
| 1308 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1309 | /// |
| 1310 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1311 | /// into which the designation in @p DIE should refer. |
| 1312 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1313 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1314 | /// a field, this will be set to the field declaration corresponding |
| 1315 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1316 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1317 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1318 | /// DIE is an array designator or GNU array-range designator, this |
| 1319 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1320 | /// |
| 1321 | /// @param Index Index into @p IList where the designated initializer |
| 1322 | /// @p DIE occurs. |
| 1323 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1324 | /// @param StructuredList The initializer list expression that |
| 1325 | /// describes all of the subobject initializers in the order they'll |
| 1326 | /// actually be initialized. |
| 1327 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1328 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1330 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1331 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1333 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1334 | QualType &CurrentObjectType, |
| 1335 | RecordDecl::field_iterator *NextField, |
| 1336 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1337 | unsigned &Index, |
| 1338 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1339 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1340 | bool FinishSubobjectInit, |
| 1341 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1342 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1343 | // Check the actual initialization for the designated object type. |
| 1344 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Temporarily remove the designator expression from the |
| 1347 | // initializer list that the child calls see, so that we don't try |
| 1348 | // to re-process the designator. |
| 1349 | unsigned OldIndex = Index; |
| 1350 | IList->setInit(OldIndex, DIE->getInit()); |
| 1351 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1352 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1353 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Restore the designated initializer expression in the syntactic |
| 1356 | // form of the initializer list. |
| 1357 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1358 | DIE->setInit(IList->getInit(OldIndex)); |
| 1359 | IList->setInit(OldIndex, DIE); |
| 1360 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1361 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1364 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1366 | "Need a non-designated initializer list to start from"); |
| 1367 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1368 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1369 | // Determine the structural initializer list that corresponds to the |
| 1370 | // current subobject. |
| 1371 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1373 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1374 | SourceRange(D->getStartLocation(), |
| 1375 | DIE->getSourceRange().getEnd())); |
| 1376 | assert(StructuredList && "Expected a structured initializer list"); |
| 1377 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1378 | if (D->isFieldDesignator()) { |
| 1379 | // C99 6.7.8p7: |
| 1380 | // |
| 1381 | // If a designator has the form |
| 1382 | // |
| 1383 | // . identifier |
| 1384 | // |
| 1385 | // then the current object (defined below) shall have |
| 1386 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1388 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1389 | if (!RT) { |
| 1390 | SourceLocation Loc = D->getDotLoc(); |
| 1391 | if (Loc.isInvalid()) |
| 1392 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1393 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1394 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1395 | ++Index; |
| 1396 | return true; |
| 1397 | } |
| 1398 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1399 | // Note: we perform a linear search of the fields here, despite |
| 1400 | // the fact that we have a faster lookup method, because we always |
| 1401 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1402 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1403 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1404 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1406 | Field = RT->getDecl()->field_begin(), |
| 1407 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1408 | for (; Field != FieldEnd; ++Field) { |
| 1409 | if (Field->isUnnamedBitfield()) |
| 1410 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1411 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1412 | // If we find a field representing an anonymous field, look in the |
| 1413 | // IndirectFieldDecl that follow for the designated initializer. |
| 1414 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1415 | if (IndirectFieldDecl *IF = |
| 1416 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1417 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1418 | D = DIE->getDesignator(DesigIdx); |
| 1419 | break; |
| 1420 | } |
| 1421 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1422 | if (KnownField && KnownField == *Field) |
| 1423 | break; |
| 1424 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | |
| 1427 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1430 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1431 | // There was no normal field in the struct with the designated |
| 1432 | // name. Perform another lookup for this name, which may find |
| 1433 | // something that we can't designate (e.g., a member function), |
| 1434 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1436 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1437 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1438 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1439 | // Name lookup didn't find anything. Determine whether this |
| 1440 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1441 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1442 | Sema::LookupMemberName); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1443 | if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1444 | Sema::CTC_NoKeywords) && |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1445 | (ReplacementField = R.getAsSingle<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1446 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1447 | ->Equals(RT->getDecl())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1448 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1449 | diag::err_field_designator_unknown_suggest) |
| 1450 | << FieldName << CurrentObjectType << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1451 | << FixItHint::CreateReplacement(D->getFieldLoc(), |
| 1452 | R.getLookupName().getAsString()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1453 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1454 | diag::note_previous_decl) |
| 1455 | << ReplacementField->getDeclName(); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1456 | } else { |
| 1457 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1458 | << FieldName << CurrentObjectType; |
| 1459 | ++Index; |
| 1460 | return true; |
| 1461 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1462 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1464 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1465 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1466 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1467 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1469 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1470 | ++Index; |
| 1471 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1472 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1473 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1474 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1475 | // The replacement field comes from typo correction; find it |
| 1476 | // in the list of fields. |
| 1477 | FieldIndex = 0; |
| 1478 | Field = RT->getDecl()->field_begin(); |
| 1479 | for (; Field != FieldEnd; ++Field) { |
| 1480 | if (Field->isUnnamedBitfield()) |
| 1481 | continue; |
| 1482 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1483 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1484 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1485 | break; |
| 1486 | |
| 1487 | ++FieldIndex; |
| 1488 | } |
| 1489 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1490 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1491 | |
| 1492 | // All of the fields of a union are located at the same place in |
| 1493 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1494 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1495 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1496 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1497 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1499 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1500 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1502 | // Make sure that our non-designated initializer list has space |
| 1503 | // for a subobject corresponding to this field. |
| 1504 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1505 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1507 | // This designator names a flexible array member. |
| 1508 | if (Field->getType()->isIncompleteArrayType()) { |
| 1509 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1510 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1511 | // We can't designate an object within the flexible array |
| 1512 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1514 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1516 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1518 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1519 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1520 | << *Field; |
| 1521 | Invalid = true; |
| 1522 | } |
| 1523 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1524 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1525 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1526 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1527 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1528 | diag::err_flexible_array_init_needs_braces) |
| 1529 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1530 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1531 | << *Field; |
| 1532 | Invalid = true; |
| 1533 | } |
| 1534 | |
| 1535 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1537 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1539 | diag::err_flexible_array_init_nonempty) |
| 1540 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1541 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1542 | << *Field; |
| 1543 | Invalid = true; |
| 1544 | } |
| 1545 | |
| 1546 | if (Invalid) { |
| 1547 | ++Index; |
| 1548 | return true; |
| 1549 | } |
| 1550 | |
| 1551 | // Initialize the array. |
| 1552 | bool prevHadError = hadError; |
| 1553 | unsigned newStructuredIndex = FieldIndex; |
| 1554 | unsigned OldIndex = Index; |
| 1555 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1556 | |
| 1557 | InitializedEntity MemberEntity = |
| 1558 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1559 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1560 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1561 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1562 | IList->setInit(OldIndex, DIE); |
| 1563 | if (hadError && !prevHadError) { |
| 1564 | ++Field; |
| 1565 | ++FieldIndex; |
| 1566 | if (NextField) |
| 1567 | *NextField = Field; |
| 1568 | StructuredIndex = FieldIndex; |
| 1569 | return true; |
| 1570 | } |
| 1571 | } else { |
| 1572 | // Recurse to check later designated subobjects. |
| 1573 | QualType FieldType = (*Field)->getType(); |
| 1574 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1575 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1576 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1577 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1578 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1579 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1580 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1581 | true, false)) |
| 1582 | return true; |
| 1583 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1584 | |
| 1585 | // Find the position of the next field to be initialized in this |
| 1586 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1587 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1588 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1589 | |
| 1590 | // If this the first designator, our caller will continue checking |
| 1591 | // the rest of this struct/class/union subobject. |
| 1592 | if (IsFirstDesignator) { |
| 1593 | if (NextField) |
| 1594 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1595 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1596 | return false; |
| 1597 | } |
| 1598 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1599 | if (!FinishSubobjectInit) |
| 1600 | return false; |
| 1601 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1602 | // We've already initialized something in the union; we're done. |
| 1603 | if (RT->getDecl()->isUnion()) |
| 1604 | return hadError; |
| 1605 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1606 | // Check the remaining fields within this class/struct/union subobject. |
| 1607 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1608 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1609 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1610 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1611 | return hadError && !prevHadError; |
| 1612 | } |
| 1613 | |
| 1614 | // C99 6.7.8p6: |
| 1615 | // |
| 1616 | // If a designator has the form |
| 1617 | // |
| 1618 | // [ constant-expression ] |
| 1619 | // |
| 1620 | // then the current object (defined below) shall have array |
| 1621 | // type and the expression shall be an integer constant |
| 1622 | // expression. If the array is of unknown size, any |
| 1623 | // nonnegative value is valid. |
| 1624 | // |
| 1625 | // Additionally, cope with the GNU extension that permits |
| 1626 | // designators of the form |
| 1627 | // |
| 1628 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1629 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1630 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1631 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1632 | << CurrentObjectType; |
| 1633 | ++Index; |
| 1634 | return true; |
| 1635 | } |
| 1636 | |
| 1637 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1638 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1639 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1640 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1641 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1642 | DesignatedEndIndex = DesignatedStartIndex; |
| 1643 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1644 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1645 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1647 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1649 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1650 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1652 | // Codegen can't handle evaluating array range designators that have side |
| 1653 | // effects, because we replicate the AST value for each initialized element. |
| 1654 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1655 | // elements with something that has a side effect, so codegen can emit an |
| 1656 | // "error unsupported" error instead of miscompiling the app. |
| 1657 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1658 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1659 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1662 | if (isa<ConstantArrayType>(AT)) { |
| 1663 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1664 | DesignatedStartIndex |
| 1665 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1666 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1667 | DesignatedEndIndex |
| 1668 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1669 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1670 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1671 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1672 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1673 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1674 | << IndexExpr->getSourceRange(); |
| 1675 | ++Index; |
| 1676 | return true; |
| 1677 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1678 | } else { |
| 1679 | // Make sure the bit-widths and signedness match. |
| 1680 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1681 | DesignatedEndIndex |
| 1682 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1683 | else if (DesignatedStartIndex.getBitWidth() < |
| 1684 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1685 | DesignatedStartIndex |
| 1686 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1687 | DesignatedStartIndex.setIsUnsigned(true); |
| 1688 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1689 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1691 | // Make sure that our non-designated initializer list has space |
| 1692 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1693 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1695 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1697 | // Repeatedly perform subobject initializations in the range |
| 1698 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1700 | // Move to the next designator |
| 1701 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1702 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1703 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1704 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1705 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1707 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1708 | // Recurse to check later designated subobjects. |
| 1709 | QualType ElementType = AT->getElementType(); |
| 1710 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1711 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1712 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1713 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1714 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1715 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1716 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1717 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1718 | return true; |
| 1719 | |
| 1720 | // Move to the next index in the array that we'll be initializing. |
| 1721 | ++DesignatedStartIndex; |
| 1722 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1723 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1724 | |
| 1725 | // If this the first designator, our caller will continue checking |
| 1726 | // the rest of this array subobject. |
| 1727 | if (IsFirstDesignator) { |
| 1728 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1729 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1730 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1731 | return false; |
| 1732 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1734 | if (!FinishSubobjectInit) |
| 1735 | return false; |
| 1736 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1737 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1738 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1739 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1740 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1741 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1745 | // Get the structured initializer list for a subobject of type |
| 1746 | // @p CurrentObjectType. |
| 1747 | InitListExpr * |
| 1748 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1749 | QualType CurrentObjectType, |
| 1750 | InitListExpr *StructuredList, |
| 1751 | unsigned StructuredIndex, |
| 1752 | SourceRange InitRange) { |
| 1753 | Expr *ExistingInit = 0; |
| 1754 | if (!StructuredList) |
| 1755 | ExistingInit = SyntacticToSemantic[IList]; |
| 1756 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1757 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1759 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1760 | return Result; |
| 1761 | |
| 1762 | if (ExistingInit) { |
| 1763 | // We are creating an initializer list that initializes the |
| 1764 | // subobjects of the current object, but there was already an |
| 1765 | // initialization that completely initialized the current |
| 1766 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1768 | // struct X { int a, b; }; |
| 1769 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1771 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1772 | // designated initializer re-initializes the whole |
| 1773 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1775 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1776 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1778 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1779 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1780 | << ExistingInit->getSourceRange(); |
| 1781 | } |
| 1782 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1784 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1785 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1786 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1787 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1788 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1790 | // Pre-allocate storage for the structured initializer list. |
| 1791 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1792 | unsigned NumInits = 0; |
| 1793 | if (!StructuredList) |
| 1794 | NumInits = IList->getNumInits(); |
| 1795 | else if (Index < IList->getNumInits()) { |
| 1796 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1797 | NumInits = SubList->getNumInits(); |
| 1798 | } |
| 1799 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1801 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1802 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1803 | NumElements = CAType->getSize().getZExtValue(); |
| 1804 | // Simple heuristic so that we don't allocate a very large |
| 1805 | // initializer with many empty entries at the end. |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1806 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1807 | NumElements = 0; |
| 1808 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1809 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1810 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1811 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1812 | RecordDecl *RDecl = RType->getDecl(); |
| 1813 | if (RDecl->isUnion()) |
| 1814 | NumElements = 1; |
| 1815 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1817 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1820 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1821 | NumElements = IList->getNumInits(); |
| 1822 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1823 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1825 | // Link this new initializer list into the structured initializer |
| 1826 | // lists. |
| 1827 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1828 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1829 | else { |
| 1830 | Result->setSyntacticForm(IList); |
| 1831 | SyntacticToSemantic[IList] = Result; |
| 1832 | } |
| 1833 | |
| 1834 | return Result; |
| 1835 | } |
| 1836 | |
| 1837 | /// Update the initializer at index @p StructuredIndex within the |
| 1838 | /// structured initializer list to the value @p expr. |
| 1839 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1840 | unsigned &StructuredIndex, |
| 1841 | Expr *expr) { |
| 1842 | // No structured initializer list to update |
| 1843 | if (!StructuredList) |
| 1844 | return; |
| 1845 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1846 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1847 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1848 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1850 | diag::warn_initializer_overrides) |
| 1851 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1853 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1854 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1855 | << PrevInit->getSourceRange(); |
| 1856 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1858 | ++StructuredIndex; |
| 1859 | } |
| 1860 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1861 | /// Check that the given Index expression is a valid array designator |
| 1862 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1863 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1864 | /// and produces a reasonable diagnostic if there is a |
| 1865 | /// failure. Returns true if there was an error, false otherwise. If |
| 1866 | /// everything went okay, Value will receive the value of the constant |
| 1867 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1869 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1870 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1871 | |
| 1872 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1873 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1874 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1875 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1876 | if (Value.isSigned() && Value.isNegative()) |
| 1877 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1878 | << Value.toString(10) << Index->getSourceRange(); |
| 1879 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1880 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1881 | return false; |
| 1882 | } |
| 1883 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1884 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1885 | SourceLocation Loc, |
| 1886 | bool GNUSyntax, |
| 1887 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1888 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1889 | |
| 1890 | bool Invalid = false; |
| 1891 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1892 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1893 | |
| 1894 | // Build designators and check array designator expressions. |
| 1895 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1896 | const Designator &D = Desig.getDesignator(Idx); |
| 1897 | switch (D.getKind()) { |
| 1898 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1900 | D.getFieldLoc())); |
| 1901 | break; |
| 1902 | |
| 1903 | case Designator::ArrayDesignator: { |
| 1904 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1905 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1906 | if (!Index->isTypeDependent() && |
| 1907 | !Index->isValueDependent() && |
| 1908 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1909 | Invalid = true; |
| 1910 | else { |
| 1911 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1913 | D.getRBracketLoc())); |
| 1914 | InitExpressions.push_back(Index); |
| 1915 | } |
| 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | case Designator::ArrayRangeDesignator: { |
| 1920 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1921 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1922 | llvm::APSInt StartValue; |
| 1923 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1924 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1925 | StartIndex->isValueDependent(); |
| 1926 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1927 | EndIndex->isValueDependent(); |
| 1928 | if ((!StartDependent && |
| 1929 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1930 | (!EndDependent && |
| 1931 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1932 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1933 | else { |
| 1934 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1935 | if (StartDependent || EndDependent) { |
| 1936 | // Nothing to compute. |
| 1937 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1938 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1939 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1940 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1942 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1943 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1945 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1946 | Invalid = true; |
| 1947 | } else { |
| 1948 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1950 | D.getEllipsisLoc(), |
| 1951 | D.getRBracketLoc())); |
| 1952 | InitExpressions.push_back(StartIndex); |
| 1953 | InitExpressions.push_back(EndIndex); |
| 1954 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1955 | } |
| 1956 | break; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | if (Invalid || Init.isInvalid()) |
| 1962 | return ExprError(); |
| 1963 | |
| 1964 | // Clear out the expressions within the designation. |
| 1965 | Desig.ClearExprs(*this); |
| 1966 | |
| 1967 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1968 | = DesignatedInitExpr::Create(Context, |
| 1969 | Designators.data(), Designators.size(), |
| 1970 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1971 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 1973 | if (getLangOptions().CPlusPlus) |
| 1974 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 1975 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1976 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1977 | return Owned(DIE); |
| 1978 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1979 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1980 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 1981 | InitListExpr *&InitList, QualType &DeclType) { |
| 1982 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1983 | if (!CheckInitList.HadError()) |
| 1984 | InitList = CheckInitList.getFullyStructuredList(); |
| 1985 | |
| 1986 | return CheckInitList.HadError(); |
| 1987 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1989 | //===----------------------------------------------------------------------===// |
| 1990 | // Initialization entity |
| 1991 | //===----------------------------------------------------------------------===// |
| 1992 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1993 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1994 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1996 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1997 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 1998 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1999 | Type = AT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2000 | } else { |
| 2001 | Kind = EK_VectorElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2002 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2003 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2007 | CXXBaseSpecifier *Base, |
| 2008 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2009 | { |
| 2010 | InitializedEntity Result; |
| 2011 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2012 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2013 | if (IsInheritedVirtualBase) |
| 2014 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2016 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2017 | return Result; |
| 2018 | } |
| 2019 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2020 | DeclarationName InitializedEntity::getName() const { |
| 2021 | switch (getKind()) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2022 | case EK_Parameter: |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2023 | if (!VariableOrMember) |
| 2024 | return DeclarationName(); |
| 2025 | // Fall through |
| 2026 | |
| 2027 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2028 | case EK_Member: |
| 2029 | return VariableOrMember->getDeclName(); |
| 2030 | |
| 2031 | case EK_Result: |
| 2032 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2033 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2034 | case EK_Temporary: |
| 2035 | case EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2036 | case EK_Delegation: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2037 | case EK_ArrayElement: |
| 2038 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2039 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2040 | return DeclarationName(); |
| 2041 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2043 | // Silence GCC warning |
| 2044 | return DeclarationName(); |
| 2045 | } |
| 2046 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2047 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2048 | switch (getKind()) { |
| 2049 | case EK_Variable: |
| 2050 | case EK_Parameter: |
| 2051 | case EK_Member: |
| 2052 | return VariableOrMember; |
| 2053 | |
| 2054 | case EK_Result: |
| 2055 | case EK_Exception: |
| 2056 | case EK_New: |
| 2057 | case EK_Temporary: |
| 2058 | case EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2059 | case EK_Delegation: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2060 | case EK_ArrayElement: |
| 2061 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2062 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2063 | return 0; |
| 2064 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2065 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2066 | // Silence GCC warning |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2070 | bool InitializedEntity::allowsNRVO() const { |
| 2071 | switch (getKind()) { |
| 2072 | case EK_Result: |
| 2073 | case EK_Exception: |
| 2074 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2075 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2076 | case EK_Variable: |
| 2077 | case EK_Parameter: |
| 2078 | case EK_Member: |
| 2079 | case EK_New: |
| 2080 | case EK_Temporary: |
| 2081 | case EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2082 | case EK_Delegation: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2083 | case EK_ArrayElement: |
| 2084 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2085 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2086 | break; |
| 2087 | } |
| 2088 | |
| 2089 | return false; |
| 2090 | } |
| 2091 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2092 | //===----------------------------------------------------------------------===// |
| 2093 | // Initialization sequence |
| 2094 | //===----------------------------------------------------------------------===// |
| 2095 | |
| 2096 | void InitializationSequence::Step::Destroy() { |
| 2097 | switch (Kind) { |
| 2098 | case SK_ResolveAddressOfOverloadedFunction: |
| 2099 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2100 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2101 | case SK_CastDerivedToBaseLValue: |
| 2102 | case SK_BindReference: |
| 2103 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2104 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2105 | case SK_UserConversion: |
| 2106 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2107 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2108 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2109 | case SK_ListInitialization: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2110 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2111 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2112 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2113 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2114 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2115 | case SK_ArrayInit: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2116 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2118 | case SK_ConversionSequence: |
| 2119 | delete ICS; |
| 2120 | } |
| 2121 | } |
| 2122 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2123 | bool InitializationSequence::isDirectReferenceBinding() const { |
| 2124 | return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference; |
| 2125 | } |
| 2126 | |
| 2127 | bool InitializationSequence::isAmbiguous() const { |
| 2128 | if (getKind() != FailedSequence) |
| 2129 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2130 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2131 | switch (getFailureKind()) { |
| 2132 | case FK_TooManyInitsForReference: |
| 2133 | case FK_ArrayNeedsInitList: |
| 2134 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2135 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2136 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2137 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2138 | case FK_RValueReferenceBindingToLValue: |
| 2139 | case FK_ReferenceInitDropsQualifiers: |
| 2140 | case FK_ReferenceInitFailed: |
| 2141 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2142 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2143 | case FK_TooManyInitsForScalar: |
| 2144 | case FK_ReferenceBindingToInitList: |
| 2145 | case FK_InitListBadDestinationType: |
| 2146 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2147 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2148 | case FK_ArrayTypeMismatch: |
| 2149 | case FK_NonConstantArrayInit: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2150 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2152 | case FK_ReferenceInitOverloadFailed: |
| 2153 | case FK_UserConversionOverloadFailed: |
| 2154 | case FK_ConstructorOverloadFailed: |
| 2155 | return FailedOverloadResult == OR_Ambiguous; |
| 2156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2157 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2158 | return false; |
| 2159 | } |
| 2160 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2161 | bool InitializationSequence::isConstructorInitialization() const { |
| 2162 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2163 | } |
| 2164 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2165 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2166 | FunctionDecl *Function, |
| 2167 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2168 | Step S; |
| 2169 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2170 | S.Type = Function->getType(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2171 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2172 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2173 | Steps.push_back(S); |
| 2174 | } |
| 2175 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2176 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2177 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2178 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2179 | switch (VK) { |
| 2180 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2181 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2182 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2183 | default: llvm_unreachable("No such category"); |
| 2184 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2185 | S.Type = BaseType; |
| 2186 | Steps.push_back(S); |
| 2187 | } |
| 2188 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2190 | bool BindingTemporary) { |
| 2191 | Step S; |
| 2192 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2193 | S.Type = T; |
| 2194 | Steps.push_back(S); |
| 2195 | } |
| 2196 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2197 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2198 | Step S; |
| 2199 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2200 | S.Type = T; |
| 2201 | Steps.push_back(S); |
| 2202 | } |
| 2203 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2204 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2205 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2206 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2207 | Step S; |
| 2208 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2209 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2210 | S.Function.Function = Function; |
| 2211 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2212 | Steps.push_back(S); |
| 2213 | } |
| 2214 | |
| 2215 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2216 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2217 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2218 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2219 | switch (VK) { |
| 2220 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2221 | S.Kind = SK_QualificationConversionRValue; |
| 2222 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2223 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2224 | S.Kind = SK_QualificationConversionXValue; |
| 2225 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2226 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2227 | S.Kind = SK_QualificationConversionLValue; |
| 2228 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2229 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2230 | S.Type = Ty; |
| 2231 | Steps.push_back(S); |
| 2232 | } |
| 2233 | |
| 2234 | void InitializationSequence::AddConversionSequenceStep( |
| 2235 | const ImplicitConversionSequence &ICS, |
| 2236 | QualType T) { |
| 2237 | Step S; |
| 2238 | S.Kind = SK_ConversionSequence; |
| 2239 | S.Type = T; |
| 2240 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2241 | Steps.push_back(S); |
| 2242 | } |
| 2243 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2244 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2245 | Step S; |
| 2246 | S.Kind = SK_ListInitialization; |
| 2247 | S.Type = T; |
| 2248 | Steps.push_back(S); |
| 2249 | } |
| 2250 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2251 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2252 | InitializationSequence::AddConstructorInitializationStep( |
| 2253 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2254 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2255 | QualType T) { |
| 2256 | Step S; |
| 2257 | S.Kind = SK_ConstructorInitialization; |
| 2258 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2259 | S.Function.Function = Constructor; |
| 2260 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2261 | Steps.push_back(S); |
| 2262 | } |
| 2263 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2264 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2265 | Step S; |
| 2266 | S.Kind = SK_ZeroInitialization; |
| 2267 | S.Type = T; |
| 2268 | Steps.push_back(S); |
| 2269 | } |
| 2270 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2271 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2272 | Step S; |
| 2273 | S.Kind = SK_CAssignment; |
| 2274 | S.Type = T; |
| 2275 | Steps.push_back(S); |
| 2276 | } |
| 2277 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2278 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2279 | Step S; |
| 2280 | S.Kind = SK_StringInit; |
| 2281 | S.Type = T; |
| 2282 | Steps.push_back(S); |
| 2283 | } |
| 2284 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2285 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2286 | Step S; |
| 2287 | S.Kind = SK_ObjCObjectConversion; |
| 2288 | S.Type = T; |
| 2289 | Steps.push_back(S); |
| 2290 | } |
| 2291 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2292 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2293 | Step S; |
| 2294 | S.Kind = SK_ArrayInit; |
| 2295 | S.Type = T; |
| 2296 | Steps.push_back(S); |
| 2297 | } |
| 2298 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2299 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2300 | OverloadingResult Result) { |
| 2301 | SequenceKind = FailedSequence; |
| 2302 | this->Failure = Failure; |
| 2303 | this->FailedOverloadResult = Result; |
| 2304 | } |
| 2305 | |
| 2306 | //===----------------------------------------------------------------------===// |
| 2307 | // Attempt initialization |
| 2308 | //===----------------------------------------------------------------------===// |
| 2309 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2310 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2311 | static void TryListInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2312 | const InitializedEntity &Entity, |
| 2313 | const InitializationKind &Kind, |
| 2314 | InitListExpr *InitList, |
| 2315 | InitializationSequence &Sequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2316 | // FIXME: We only perform rudimentary checking of list |
| 2317 | // initializations at this point, then assume that any list |
| 2318 | // initialization of an array, aggregate, or scalar will be |
Sebastian Redl | d559a54 | 2010-06-30 16:41:54 +0000 | [diff] [blame] | 2319 | // well-formed. When we actually "perform" list initialization, we'll |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2320 | // do all of the necessary checking. C++0x initializer lists will |
| 2321 | // force us to perform more checking here. |
| 2322 | Sequence.setSequenceKind(InitializationSequence::ListInitialization); |
| 2323 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2324 | QualType DestType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2325 | |
| 2326 | // C++ [dcl.init]p13: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2327 | // If T is a scalar type, then a declaration of the form |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2328 | // |
| 2329 | // T x = { a }; |
| 2330 | // |
| 2331 | // is equivalent to |
| 2332 | // |
| 2333 | // T x = a; |
| 2334 | if (DestType->isScalarType()) { |
| 2335 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2336 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2337 | return; |
| 2338 | } |
| 2339 | |
| 2340 | // Assume scalar initialization from a single value works. |
| 2341 | } else if (DestType->isAggregateType()) { |
| 2342 | // Assume aggregate initialization works. |
| 2343 | } else if (DestType->isVectorType()) { |
| 2344 | // Assume vector initialization works. |
| 2345 | } else if (DestType->isReferenceType()) { |
| 2346 | // FIXME: C++0x defines behavior for this. |
| 2347 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2348 | return; |
| 2349 | } else if (DestType->isRecordType()) { |
| 2350 | // FIXME: C++0x defines behavior for this |
| 2351 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2352 | } |
| 2353 | |
| 2354 | // Add a general "list initialization" step. |
| 2355 | Sequence.AddListInitializationStep(DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /// \brief Try a reference initialization that involves calling a conversion |
| 2359 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2360 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2361 | const InitializedEntity &Entity, |
| 2362 | const InitializationKind &Kind, |
| 2363 | Expr *Initializer, |
| 2364 | bool AllowRValues, |
| 2365 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2366 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2367 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2368 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2369 | QualType cv2T2 = Initializer->getType(); |
| 2370 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2371 | |
| 2372 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2373 | bool ObjCConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2374 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2375 | T1, T2, DerivedToBase, |
| 2376 | ObjCConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2377 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2378 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2379 | (void)ObjCConversion; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2380 | |
| 2381 | // Build the candidate set directly in the initialization sequence |
| 2382 | // structure, so that it will persist if we fail. |
| 2383 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2384 | CandidateSet.clear(); |
| 2385 | |
| 2386 | // Determine whether we are allowed to call explicit constructors or |
| 2387 | // explicit conversion operators. |
| 2388 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2390 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2391 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2392 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2393 | // The type we're converting to is a class type. Enumerate its constructors |
| 2394 | // to see if there is a suitable conversion. |
| 2395 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2397 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2398 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2400 | NamedDecl *D = *Con; |
| 2401 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2402 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2403 | // Find the constructor (which may be a template). |
| 2404 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2405 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2406 | if (ConstructorTmpl) |
| 2407 | Constructor = cast<CXXConstructorDecl>( |
| 2408 | ConstructorTmpl->getTemplatedDecl()); |
| 2409 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2410 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2412 | if (!Constructor->isInvalidDecl() && |
| 2413 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2414 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2415 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2416 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2417 | &Initializer, 1, CandidateSet, |
| 2418 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2419 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2420 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2421 | &Initializer, 1, CandidateSet, |
| 2422 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2423 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2425 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2426 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2427 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2429 | const RecordType *T2RecordType = 0; |
| 2430 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2431 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2432 | // The type we're converting from is a class type, enumerate its conversion |
| 2433 | // functions. |
| 2434 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2435 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2436 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2437 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2438 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2439 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2440 | NamedDecl *D = *I; |
| 2441 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2442 | if (isa<UsingShadowDecl>(D)) |
| 2443 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2445 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2446 | CXXConversionDecl *Conv; |
| 2447 | if (ConvTemplate) |
| 2448 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2449 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2450 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2451 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2452 | // If the conversion function doesn't return a reference type, |
| 2453 | // it can't be considered for this conversion unless we're allowed to |
| 2454 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2455 | // FIXME: Do we need to make sure that we only consider conversion |
| 2456 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2457 | // break recursion. |
| 2458 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2459 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2460 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2461 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2462 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2463 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2464 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2465 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2466 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2470 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2471 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2472 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2473 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2474 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2475 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2476 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2477 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2478 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2479 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2480 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2481 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2482 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2483 | // This is the overload that will actually be used for the initialization, so |
| 2484 | // mark it as used. |
| 2485 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2486 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2487 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2488 | if (isa<CXXConversionDecl>(Function)) |
| 2489 | T2 = Function->getResultType(); |
| 2490 | else |
| 2491 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2494 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2495 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2496 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2497 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2498 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2499 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2500 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2501 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2502 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2503 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2505 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2506 | bool NewObjCConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2507 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2508 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2509 | T2.getNonLValueExprType(S.Context), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2510 | NewDerivedToBase, NewObjCConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2511 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2512 | // If the type we've converted to is not reference-related to the |
| 2513 | // type we're looking for, then there is another conversion step |
| 2514 | // we need to perform to produce a temporary of the right type |
| 2515 | // that we'll be binding to. |
| 2516 | ImplicitConversionSequence ICS; |
| 2517 | ICS.setStandard(); |
| 2518 | ICS.Standard = Best->FinalConversion; |
| 2519 | T2 = ICS.Standard.getToType(2); |
| 2520 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2521 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2522 | Sequence.AddDerivedToBaseCastStep( |
| 2523 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2524 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2525 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2526 | else if (NewObjCConversion) |
| 2527 | Sequence.AddObjCObjectConversionStep( |
| 2528 | S.Context.getQualifiedType(T1, |
| 2529 | T2.getNonReferenceType().getQualifiers())); |
| 2530 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2531 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2532 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2533 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2534 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2535 | return OR_Success; |
| 2536 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2537 | |
| 2538 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2539 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2540 | const InitializedEntity &Entity, |
| 2541 | const InitializationKind &Kind, |
| 2542 | Expr *Initializer, |
| 2543 | InitializationSequence &Sequence) { |
| 2544 | Sequence.setSequenceKind(InitializationSequence::ReferenceBinding); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2546 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2547 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2548 | Qualifiers T1Quals; |
| 2549 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2550 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2551 | Qualifiers T2Quals; |
| 2552 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2553 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2555 | // If the initializer is the address of an overloaded function, try |
| 2556 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2557 | // type of the resulting function. |
| 2558 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2559 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2560 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2561 | T1, |
| 2562 | false, |
| 2563 | Found)) { |
| 2564 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2565 | cv2T2 = Fn->getType(); |
| 2566 | T2 = cv2T2.getUnqualifiedType(); |
| 2567 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2568 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2569 | return; |
| 2570 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2571 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2572 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2573 | // Compute some basic properties of the types and the initializer. |
| 2574 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2575 | bool isRValueRef = !isLValueRef; |
| 2576 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2577 | bool ObjCConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2578 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2579 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2580 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
| 2581 | ObjCConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2583 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2584 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2585 | // "cv2 T2" as follows: |
| 2586 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2587 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2588 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2589 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2590 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2591 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2592 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2593 | bool T1Function = T1->isFunctionType(); |
| 2594 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2595 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2596 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2597 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2598 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2599 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2600 | // reference-compatible with "cv2 T2," or |
| 2601 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2602 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2603 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2604 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2605 | // to decide whether we're actually binding to a temporary created from |
| 2606 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2607 | if (DerivedToBase) |
| 2608 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2610 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2611 | else if (ObjCConversion) |
| 2612 | Sequence.AddObjCObjectConversionStep( |
| 2613 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2614 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2615 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2616 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2617 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2618 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2619 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2622 | |
| 2623 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2624 | // reference-related to T2, and can be implicitly converted to an |
| 2625 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2626 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2627 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2628 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2629 | // If we have an rvalue ref to function type here, the rhs must be |
| 2630 | // an rvalue. |
| 2631 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2632 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2633 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2634 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2635 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2636 | Sequence); |
| 2637 | if (ConvOvlResult == OR_Success) |
| 2638 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2639 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2640 | Sequence.SetOverloadFailure( |
| 2641 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2642 | ConvOvlResult); |
| 2643 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2644 | } |
| 2645 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2646 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2647 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2648 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 7a2a116 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 2649 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2650 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2651 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2652 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2653 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2654 | Sequence.SetOverloadFailure( |
| 2655 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2656 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2657 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2658 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2659 | ? (RefRelationship == Sema::Ref_Related |
| 2660 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2661 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2662 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2664 | return; |
| 2665 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2667 | // - If the initializer expression |
| 2668 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2669 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2670 | // Note: functions are handled below. |
| 2671 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2672 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2674 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2675 | (InitCategory.isXValue() || |
| 2676 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2677 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2678 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2679 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2680 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2681 | // compiler the freedom to perform a copy here or bind to the |
| 2682 | // object, while C++0x requires that we bind directly to the |
| 2683 | // object. Hence, we always bind to the object without making an |
| 2684 | // extra copy. However, in C++03 requires that we check for the |
| 2685 | // presence of a suitable copy constructor: |
| 2686 | // |
| 2687 | // The constructor that would be used to make the copy shall |
| 2688 | // be callable whether or not the copy is actually done. |
Francois Pichet | 687aaf0 | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2689 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2690 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2693 | if (DerivedToBase) |
| 2694 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2695 | ValueKind); |
| 2696 | else if (ObjCConversion) |
| 2697 | Sequence.AddObjCObjectConversionStep( |
| 2698 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2700 | if (T1Quals != T2Quals) |
| 2701 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2702 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2703 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2704 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2705 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2706 | |
| 2707 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2708 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2709 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2710 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2711 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2712 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2713 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2714 | Kind, Initializer, |
| 2715 | /*AllowRValues=*/true, |
| 2716 | Sequence); |
| 2717 | if (ConvOvlResult) |
| 2718 | Sequence.SetOverloadFailure( |
| 2719 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2720 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2722 | return; |
| 2723 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2725 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2726 | return; |
| 2727 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2728 | |
| 2729 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2730 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2732 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2734 | // Determine whether we are allowed to call explicit constructors or |
| 2735 | // explicit conversion operators. |
| 2736 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2737 | |
| 2738 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2739 | |
| 2740 | if (S.TryImplicitConversion(Sequence, TempEntity, Initializer, |
| 2741 | /*SuppressUserConversions*/ false, |
| 2742 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2743 | /*FIXME:InOverloadResolution=*/false, |
| 2744 | /*CStyle=*/Kind.isCStyleOrFunctionalCast())) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2745 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2746 | // this into an overloading ambiguity diagnostic. However, we need |
| 2747 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2748 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2749 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2750 | Sequence.SetOverloadFailure( |
| 2751 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2752 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2753 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2754 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2755 | else |
| 2756 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
| 2759 | |
| 2760 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2761 | // same cv-qualification as, or greater cv-qualification |
| 2762 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2763 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2764 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2765 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2766 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2767 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2768 | return; |
| 2769 | } |
| 2770 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2771 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2772 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2774 | InitCategory.isLValue()) { |
| 2775 | Sequence.SetFailed( |
| 2776 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2777 | return; |
| 2778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2779 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2780 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2781 | return; |
| 2782 | } |
| 2783 | |
| 2784 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2786 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2787 | const InitializedEntity &Entity, |
| 2788 | const InitializationKind &Kind, |
| 2789 | Expr *Initializer, |
| 2790 | InitializationSequence &Sequence) { |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2791 | Sequence.setSequenceKind(InitializationSequence::StringInit); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2792 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2795 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2796 | /// enumerates the constructors of the initialized entity and performs overload |
| 2797 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2798 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2799 | const InitializedEntity &Entity, |
| 2800 | const InitializationKind &Kind, |
| 2801 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2802 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2803 | InitializationSequence &Sequence) { |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2804 | Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2806 | // Build the candidate set directly in the initialization sequence |
| 2807 | // structure, so that it will persist if we fail. |
| 2808 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2809 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2810 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2811 | // Determine whether we are allowed to call explicit constructors or |
| 2812 | // explicit conversion operators. |
| 2813 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2814 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2815 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2816 | |
| 2817 | // The type we're constructing needs to be complete. |
| 2818 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2819 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2820 | return; |
| 2821 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2823 | // The type we're converting to is a class type. Enumerate its constructors |
| 2824 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2825 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2827 | CXXRecordDecl *DestRecordDecl |
| 2828 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2829 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2830 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2831 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2832 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2833 | NamedDecl *D = *Con; |
| 2834 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2835 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2837 | // Find the constructor (which may be a template). |
| 2838 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2839 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2840 | if (ConstructorTmpl) |
| 2841 | Constructor = cast<CXXConstructorDecl>( |
| 2842 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2843 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2844 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2845 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2846 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2847 | // suppress user-defined conversions on the arguments. |
| 2848 | // FIXME: Move constructors? |
| 2849 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2850 | Constructor->isCopyConstructor()) |
| 2851 | SuppressUserConversions = true; |
| 2852 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2853 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2854 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2855 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2856 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2857 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2858 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2859 | Args, NumArgs, CandidateSet, |
| 2860 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2861 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2862 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2863 | Args, NumArgs, CandidateSet, |
| 2864 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2865 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2868 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2871 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2872 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2873 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2874 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2875 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2876 | Result); |
| 2877 | return; |
| 2878 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2879 | |
| 2880 | // C++0x [dcl.init]p6: |
| 2881 | // If a program calls for the default initialization of an object |
| 2882 | // of a const-qualified type T, T shall be a class type with a |
| 2883 | // user-provided default constructor. |
| 2884 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2885 | Entity.getType().isConstQualified() && |
| 2886 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2887 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2891 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2892 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2893 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2895 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2896 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2899 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2900 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2901 | const InitializedEntity &Entity, |
| 2902 | const InitializationKind &Kind, |
| 2903 | InitializationSequence &Sequence) { |
| 2904 | // C++ [dcl.init]p5: |
| 2905 | // |
| 2906 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2907 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2909 | // -- if T is an array type, then each element is value-initialized; |
| 2910 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2911 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2913 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2914 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2915 | // -- if T is a class type (clause 9) with a user-declared |
| 2916 | // constructor (12.1), then the default constructor for T is |
| 2917 | // called (and the initialization is ill-formed if T has no |
| 2918 | // accessible default constructor); |
| 2919 | // |
| 2920 | // FIXME: we really want to refer to a single subobject of the array, |
| 2921 | // but Entity doesn't have a way to capture that (yet). |
| 2922 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 2923 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2925 | // -- if T is a (possibly cv-qualified) non-union class type |
| 2926 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2927 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2928 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2929 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2930 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2931 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2933 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2934 | } |
| 2935 | } |
| 2936 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2937 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2938 | Sequence.setSequenceKind(InitializationSequence::ZeroInitialization); |
| 2939 | } |
| 2940 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2941 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 2942 | static void TryDefaultInitialization(Sema &S, |
| 2943 | const InitializedEntity &Entity, |
| 2944 | const InitializationKind &Kind, |
| 2945 | InitializationSequence &Sequence) { |
| 2946 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2948 | // C++ [dcl.init]p6: |
| 2949 | // To default-initialize an object of type T means: |
| 2950 | // - if T is an array type, each element is default-initialized; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2951 | QualType DestType = Entity.getType(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2952 | while (const ArrayType *Array = S.Context.getAsArrayType(DestType)) |
| 2953 | DestType = Array->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2955 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 2956 | // constructor for T is called (and the initialization is ill-formed if |
| 2957 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2958 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 2959 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 2960 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2963 | // - otherwise, no initialization is performed. |
| 2964 | Sequence.setSequenceKind(InitializationSequence::NoInitialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2966 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2967 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2968 | // default constructor. |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2969 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2970 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2971 | } |
| 2972 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2973 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 2974 | /// which enumerates all conversion functions and performs overload resolution |
| 2975 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2977 | const InitializedEntity &Entity, |
| 2978 | const InitializationKind &Kind, |
| 2979 | Expr *Initializer, |
| 2980 | InitializationSequence &Sequence) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2981 | Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2983 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2984 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 2985 | QualType SourceType = Initializer->getType(); |
| 2986 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 2987 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2988 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2989 | // Build the candidate set directly in the initialization sequence |
| 2990 | // structure, so that it will persist if we fail. |
| 2991 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2992 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2993 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2994 | // Determine whether we are allowed to call explicit constructors or |
| 2995 | // explicit conversion operators. |
| 2996 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2997 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2998 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 2999 | // The type we're converting to is a class type. Enumerate its constructors |
| 3000 | // to see if there is a suitable conversion. |
| 3001 | CXXRecordDecl *DestRecordDecl |
| 3002 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3004 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3006 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3007 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3008 | Con != ConEnd; ++Con) { |
| 3009 | NamedDecl *D = *Con; |
| 3010 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3012 | // Find the constructor (which may be a template). |
| 3013 | CXXConstructorDecl *Constructor = 0; |
| 3014 | FunctionTemplateDecl *ConstructorTmpl |
| 3015 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3016 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3017 | Constructor = cast<CXXConstructorDecl>( |
| 3018 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3019 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3020 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3022 | if (!Constructor->isInvalidDecl() && |
| 3023 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3024 | if (ConstructorTmpl) |
| 3025 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3026 | /*ExplicitArgs*/ 0, |
| 3027 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3028 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3029 | else |
| 3030 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3031 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3032 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3034 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3035 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3036 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3037 | |
| 3038 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3039 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3040 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3041 | // The type we're converting from is a class type, enumerate its conversion |
| 3042 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3043 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3044 | // We can only enumerate the conversion functions for a complete type; if |
| 3045 | // the type isn't complete, simply skip this step. |
| 3046 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3047 | CXXRecordDecl *SourceRecordDecl |
| 3048 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3050 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3051 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3052 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3053 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3054 | I != E; ++I) { |
| 3055 | NamedDecl *D = *I; |
| 3056 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3057 | if (isa<UsingShadowDecl>(D)) |
| 3058 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3060 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3061 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3062 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3063 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3064 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3065 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3067 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3068 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3069 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3070 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3071 | CandidateSet); |
| 3072 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3073 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3074 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3075 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3079 | |
| 3080 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3081 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3082 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3083 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3084 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3085 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3086 | Result); |
| 3087 | return; |
| 3088 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3090 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3091 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3093 | if (isa<CXXConstructorDecl>(Function)) { |
| 3094 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3095 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3096 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3097 | return; |
| 3098 | } |
| 3099 | |
| 3100 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3101 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3102 | if (ConvType->getAs<RecordType>()) { |
| 3103 | // If we're converting to a class type, there may be an copy if |
| 3104 | // the resulting temporary object (possible to create an object of |
| 3105 | // a base class type). That copy is not a separate conversion, so |
| 3106 | // we just make a note of the actual destination type (possibly a |
| 3107 | // base class of the type returned by the conversion function) and |
| 3108 | // let the user-defined conversion step handle the conversion. |
| 3109 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3110 | return; |
| 3111 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3113 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3115 | // If the conversion following the call to the conversion function |
| 3116 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3117 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3118 | Best->FinalConversion.Third) { |
| 3119 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3120 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3121 | ICS.Standard = Best->FinalConversion; |
| 3122 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3123 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3126 | /// \brief Determine whether we have compatible array types for the |
| 3127 | /// purposes of GNU by-copy array initialization. |
| 3128 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3129 | const ArrayType *Dest, |
| 3130 | const ArrayType *Source) { |
| 3131 | // If the source and destination array types are equivalent, we're |
| 3132 | // done. |
| 3133 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3134 | return true; |
| 3135 | |
| 3136 | // Make sure that the element types are the same. |
| 3137 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3138 | return false; |
| 3139 | |
| 3140 | // The only mismatch we allow is when the destination is an |
| 3141 | // incomplete array type and the source is a constant array type. |
| 3142 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3143 | } |
| 3144 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3145 | InitializationSequence::InitializationSequence(Sema &S, |
| 3146 | const InitializedEntity &Entity, |
| 3147 | const InitializationKind &Kind, |
| 3148 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3149 | unsigned NumArgs) |
| 3150 | : FailedCandidateSet(Kind.getLocation()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3151 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3153 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3154 | // The semantics of initializers are as follows. The destination type is |
| 3155 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3156 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3157 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3158 | // parenthesized list of expressions. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3159 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3160 | |
| 3161 | if (DestType->isDependentType() || |
| 3162 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3163 | SequenceKind = DependentSequence; |
| 3164 | return; |
| 3165 | } |
| 3166 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3167 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3168 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3169 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3170 | if (Result.isInvalid()) { |
| 3171 | SetFailed(FK_ConversionFromPropertyFailed); |
| 3172 | return; |
| 3173 | } |
| 3174 | Args[I] = Result.take(); |
| 3175 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3176 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3177 | QualType SourceType; |
| 3178 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3179 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3180 | Initializer = Args[0]; |
| 3181 | if (!isa<InitListExpr>(Initializer)) |
| 3182 | SourceType = Initializer->getType(); |
| 3183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3184 | |
| 3185 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3186 | // list-initialized (8.5.4). |
| 3187 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 3188 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3189 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3190 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3192 | // - If the destination type is a reference type, see 8.5.3. |
| 3193 | if (DestType->isReferenceType()) { |
| 3194 | // C++0x [dcl.init.ref]p1: |
| 3195 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3196 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3197 | // by an object that can be converted into a T. |
| 3198 | // (Therefore, multiple arguments are not permitted.) |
| 3199 | if (NumArgs != 1) |
| 3200 | SetFailed(FK_TooManyInitsForReference); |
| 3201 | else |
| 3202 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 3203 | return; |
| 3204 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3205 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3206 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3207 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3208 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3209 | TryValueInitialization(S, Entity, Kind, *this); |
| 3210 | return; |
| 3211 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3212 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3213 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3214 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3215 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 3216 | return; |
| 3217 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3218 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3219 | // - If the destination type is an array of characters, an array of |
| 3220 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3221 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3222 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3223 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3224 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3225 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3226 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 3227 | return; |
| 3228 | } |
| 3229 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3230 | // Note: as an GNU C extension, we allow initialization of an |
| 3231 | // array from a compound literal that creates an array of the same |
| 3232 | // type, so long as the initializer has no side effects. |
| 3233 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3234 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3235 | Initializer->getType()->isArrayType()) { |
| 3236 | const ArrayType *SourceAT |
| 3237 | = Context.getAsArrayType(Initializer->getType()); |
| 3238 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
| 3239 | SetFailed(FK_ArrayTypeMismatch); |
| 3240 | else if (Initializer->HasSideEffects(S.Context)) |
| 3241 | SetFailed(FK_NonConstantArrayInit); |
| 3242 | else { |
| 3243 | setSequenceKind(ArrayInit); |
| 3244 | AddArrayInitStep(DestType); |
| 3245 | } |
| 3246 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3247 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 3248 | else |
| 3249 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3251 | return; |
| 3252 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Handle initialization in C |
| 3255 | if (!S.getLangOptions().CPlusPlus) { |
| 3256 | setSequenceKind(CAssignment); |
| 3257 | AddCAssignmentStep(DestType); |
| 3258 | return; |
| 3259 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3261 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3262 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 | // - If the initialization is direct-initialization, or if it is |
| 3264 | // copy-initialization where the cv-unqualified version of the |
| 3265 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3266 | // class of the destination, constructors are considered. [...] |
| 3267 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3268 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3269 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3270 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3271 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3272 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3274 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3275 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3276 | // used) to a derived class thereof are enumerated as described in |
| 3277 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3278 | // (13.3). |
| 3279 | else |
| 3280 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3281 | return; |
| 3282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3283 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3284 | if (NumArgs > 1) { |
| 3285 | SetFailed(FK_TooManyInitsForScalar); |
| 3286 | return; |
| 3287 | } |
| 3288 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | |
| 3290 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3291 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3292 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3293 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3294 | return; |
| 3295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3298 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3299 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3301 | // destination type; no user-defined conversions are considered. |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3302 | if (S.TryImplicitConversion(*this, Entity, Initializer, |
| 3303 | /*SuppressUserConversions*/ true, |
| 3304 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3305 | /*InOverloadResolution*/ false, |
| 3306 | /*CStyle=*/Kind.isCStyleOrFunctionalCast())) |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3307 | { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3308 | DeclAccessPair dap; |
| 3309 | if (Initializer->getType() == Context.OverloadTy && |
| 3310 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3311 | , DestType, false, dap)) |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3312 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3313 | else |
| 3314 | SetFailed(InitializationSequence::FK_ConversionFailed); |
| 3315 | } |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3316 | else |
| 3317 | setSequenceKind(StandardConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | InitializationSequence::~InitializationSequence() { |
| 3321 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 3322 | StepEnd = Steps.end(); |
| 3323 | Step != StepEnd; ++Step) |
| 3324 | Step->Destroy(); |
| 3325 | } |
| 3326 | |
| 3327 | //===----------------------------------------------------------------------===// |
| 3328 | // Perform initialization |
| 3329 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3330 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3331 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3332 | switch(Entity.getKind()) { |
| 3333 | case InitializedEntity::EK_Variable: |
| 3334 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3335 | case InitializedEntity::EK_Exception: |
| 3336 | case InitializedEntity::EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3337 | case InitializedEntity::EK_Delegation: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3338 | return Sema::AA_Initializing; |
| 3339 | |
| 3340 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3341 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3342 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3343 | return Sema::AA_Sending; |
| 3344 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3345 | return Sema::AA_Passing; |
| 3346 | |
| 3347 | case InitializedEntity::EK_Result: |
| 3348 | return Sema::AA_Returning; |
| 3349 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3350 | case InitializedEntity::EK_Temporary: |
| 3351 | // FIXME: Can we tell apart casting vs. converting? |
| 3352 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3354 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3355 | case InitializedEntity::EK_ArrayElement: |
| 3356 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3357 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3358 | return Sema::AA_Initializing; |
| 3359 | } |
| 3360 | |
| 3361 | return Sema::AA_Converting; |
| 3362 | } |
| 3363 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3364 | /// \brief Whether we should binding a created object as a temporary when |
| 3365 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3366 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3367 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3368 | case InitializedEntity::EK_ArrayElement: |
| 3369 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3370 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3371 | case InitializedEntity::EK_New: |
| 3372 | case InitializedEntity::EK_Variable: |
| 3373 | case InitializedEntity::EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3374 | case InitializedEntity::EK_Delegation: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3375 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3376 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3377 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3378 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3380 | case InitializedEntity::EK_Parameter: |
| 3381 | case InitializedEntity::EK_Temporary: |
| 3382 | return true; |
| 3383 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3385 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3386 | } |
| 3387 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3388 | /// \brief Whether the given entity, when initialized with an object |
| 3389 | /// created for that initialization, requires destruction. |
| 3390 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3391 | switch (Entity.getKind()) { |
| 3392 | case InitializedEntity::EK_Member: |
| 3393 | case InitializedEntity::EK_Result: |
| 3394 | case InitializedEntity::EK_New: |
| 3395 | case InitializedEntity::EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3396 | case InitializedEntity::EK_Delegation: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3397 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3398 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3399 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3401 | case InitializedEntity::EK_Variable: |
| 3402 | case InitializedEntity::EK_Parameter: |
| 3403 | case InitializedEntity::EK_Temporary: |
| 3404 | case InitializedEntity::EK_ArrayElement: |
| 3405 | case InitializedEntity::EK_Exception: |
| 3406 | return true; |
| 3407 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | |
| 3409 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3412 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3413 | /// provided by the given initializer by calling the appropriate copy |
| 3414 | /// constructor. |
| 3415 | /// |
| 3416 | /// \param S The Sema object used for type-checking. |
| 3417 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3418 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3419 | /// the type of the initializer expression or a superclass thereof. |
| 3420 | /// |
| 3421 | /// \param Enter The entity being initialized. |
| 3422 | /// |
| 3423 | /// \param CurInit The initializer expression. |
| 3424 | /// |
| 3425 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3426 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3427 | /// an rvalue. |
| 3428 | /// |
| 3429 | /// \returns An expression that copies the initializer expression into |
| 3430 | /// a temporary object, or an error expression if a copy could not be |
| 3431 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3432 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3433 | QualType T, |
| 3434 | const InitializedEntity &Entity, |
| 3435 | ExprResult CurInit, |
| 3436 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3437 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3438 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3440 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3441 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3442 | if (!Class) |
| 3443 | return move(CurInit); |
| 3444 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3445 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3446 | // When certain criteria are met, an implementation is allowed to |
| 3447 | // omit the copy/move construction of a class object, even if the |
| 3448 | // copy/move constructor and/or destructor for the object have |
| 3449 | // side effects. [...] |
| 3450 | // - when a temporary class object that has not been bound to a |
| 3451 | // reference (12.2) would be copied/moved to a class object |
| 3452 | // with the same cv-unqualified type, the copy/move operation |
| 3453 | // can be omitted by constructing the temporary object |
| 3454 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3455 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3456 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3457 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3458 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3459 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3460 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3461 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3462 | switch (Entity.getKind()) { |
| 3463 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3464 | Loc = Entity.getReturnLoc(); |
| 3465 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3467 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3468 | Loc = Entity.getThrowLoc(); |
| 3469 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3471 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3472 | Loc = Entity.getDecl()->getLocation(); |
| 3473 | break; |
| 3474 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3475 | case InitializedEntity::EK_ArrayElement: |
| 3476 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3477 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3478 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3479 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3480 | case InitializedEntity::EK_Base: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3481 | case InitializedEntity::EK_Delegation: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3482 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3483 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3484 | Loc = CurInitExpr->getLocStart(); |
| 3485 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3486 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3487 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3489 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3490 | return move(CurInit); |
| 3491 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3492 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3493 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3494 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3495 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3496 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3497 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3498 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3499 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3500 | CXXConstructorDecl *Constructor = 0; |
| 3501 | |
| 3502 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3503 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3504 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3505 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3506 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3507 | continue; |
| 3508 | |
| 3509 | DeclAccessPair FoundDecl |
| 3510 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3511 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3512 | &CurInitExpr, 1, CandidateSet); |
| 3513 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3515 | |
| 3516 | // Handle constructor templates. |
| 3517 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3518 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3519 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3521 | Constructor = cast<CXXConstructorDecl>( |
| 3522 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3523 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3524 | continue; |
| 3525 | |
| 3526 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3527 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3528 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3529 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3530 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3531 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3534 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3535 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3536 | case OR_Success: |
| 3537 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3538 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3539 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3540 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3541 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3542 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3543 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3544 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3545 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3546 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3547 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3548 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3550 | case OR_Ambiguous: |
| 3551 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3552 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3553 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3554 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3555 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3556 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3557 | case OR_Deleted: |
| 3558 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3559 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3560 | << CurInitExpr->getSourceRange(); |
| 3561 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3562 | << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3563 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3566 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3567 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3568 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3569 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3570 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3571 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3572 | |
| 3573 | if (IsExtraneousCopy) { |
| 3574 | // If this is a totally extraneous copy for C++03 reference |
| 3575 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3576 | // expression. We don't generate an (elided) copy operation here |
| 3577 | // because doing so would require us to pass down a flag to avoid |
| 3578 | // infinite recursion, where each step adds another extraneous, |
| 3579 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3580 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3581 | // Instantiate the default arguments of any extra parameters in |
| 3582 | // the selected copy constructor, as if we were going to create a |
| 3583 | // proper call to the copy constructor. |
| 3584 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 3585 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 3586 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 3587 | S.PDiag(diag::err_call_incomplete_argument))) |
| 3588 | break; |
| 3589 | |
| 3590 | // Build the default argument expression; we don't actually care |
| 3591 | // if this succeeds or not, because this routine will complain |
| 3592 | // if there was a problem. |
| 3593 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 3594 | } |
| 3595 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3596 | return S.Owned(CurInitExpr); |
| 3597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3598 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3599 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 3600 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3601 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3602 | // constructor call (we might have derived-to-base conversions, or |
| 3603 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3604 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3605 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3606 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3607 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3608 | // Actually perform the constructor call. |
| 3609 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3610 | move_arg(ConstructorArgs), |
| 3611 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3612 | CXXConstructExpr::CK_Complete, |
| 3613 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3615 | // If we're supposed to bind temporaries, do so. |
| 3616 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 3617 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3618 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3619 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3620 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3621 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 3622 | const InitializedEntity &Entity) { |
| 3623 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 3624 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 3625 | return; |
| 3626 | |
| 3627 | if (Entity.getDecl()->getDeclName()) |
| 3628 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 3629 | << Entity.getDecl()->getDeclName(); |
| 3630 | else |
| 3631 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 3632 | } |
| 3633 | } |
| 3634 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3635 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3636 | InitializationSequence::Perform(Sema &S, |
| 3637 | const InitializedEntity &Entity, |
| 3638 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3639 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3640 | QualType *ResultType) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3641 | if (SequenceKind == FailedSequence) { |
| 3642 | unsigned NumArgs = Args.size(); |
| 3643 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3644 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3647 | if (SequenceKind == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3648 | // If the declaration is a non-dependent, incomplete array type |
| 3649 | // that has an initializer, then its type will be completed once |
| 3650 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3651 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3652 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3653 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3654 | if (const IncompleteArrayType *ArrayT |
| 3655 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 3656 | // FIXME: We don't currently have the ability to accurately |
| 3657 | // compute the length of an initializer list without |
| 3658 | // performing full type-checking of the initializer list |
| 3659 | // (since we have to determine where braces are implicitly |
| 3660 | // introduced and such). So, we fall back to making the array |
| 3661 | // type a dependently-sized array type with no specified |
| 3662 | // bound. |
| 3663 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 3664 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3665 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3666 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3667 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 3668 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 3669 | TypeLoc TL = TInfo->getTypeLoc(); |
| 3670 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 3671 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 3672 | Brackets = ArrayLoc->getBracketsRange(); |
| 3673 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | *ResultType |
| 3677 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 3678 | /*NumElts=*/0, |
| 3679 | ArrayT->getSizeModifier(), |
| 3680 | ArrayT->getIndexTypeCVRQualifiers(), |
| 3681 | Brackets); |
| 3682 | } |
| 3683 | |
| 3684 | } |
| 3685 | } |
| 3686 | |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3687 | if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast()) |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3688 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 0ab7af6 | 2010-02-05 07:56:11 +0000 | [diff] [blame] | 3690 | if (Args.size() == 0) |
| 3691 | return S.Owned((Expr *)0); |
| 3692 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3693 | unsigned NumArgs = Args.size(); |
| 3694 | return S.Owned(new (S.Context) ParenListExpr(S.Context, |
| 3695 | SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3696 | (Expr **)Args.release(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3697 | NumArgs, |
| 3698 | SourceLocation())); |
| 3699 | } |
| 3700 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3701 | if (SequenceKind == NoInitialization) |
| 3702 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3704 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 3705 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3706 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 3707 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3708 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3709 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3710 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3711 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3712 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3714 | assert(!Steps.empty() && "Cannot have an empty initialization sequence"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3715 | |
| 3716 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3717 | // grab the only argument out the Args and place it into the "current" |
| 3718 | // initializer. |
| 3719 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3720 | case SK_ResolveAddressOfOverloadedFunction: |
| 3721 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3722 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3723 | case SK_CastDerivedToBaseLValue: |
| 3724 | case SK_BindReference: |
| 3725 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3726 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3727 | case SK_UserConversion: |
| 3728 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3729 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3730 | case SK_QualificationConversionRValue: |
| 3731 | case SK_ConversionSequence: |
| 3732 | case SK_ListInitialization: |
| 3733 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3734 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3735 | case SK_ObjCObjectConversion: |
| 3736 | case SK_ArrayInit: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3737 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3738 | CurInit = Args.get()[0]; |
| 3739 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3740 | |
| 3741 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3742 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 3743 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 3744 | if (CurInit.isInvalid()) |
| 3745 | return ExprError(); |
| 3746 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3747 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3748 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3749 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3750 | case SK_ConstructorInitialization: |
| 3751 | case SK_ZeroInitialization: |
| 3752 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3753 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
| 3755 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3756 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3757 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3758 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 3759 | Step != StepEnd; ++Step) { |
| 3760 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3761 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3762 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3763 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3765 | switch (Step->Kind) { |
| 3766 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3767 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3768 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3769 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3770 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3771 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3772 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3773 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3774 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3775 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3776 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3777 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3778 | case SK_CastDerivedToBaseLValue: { |
| 3779 | // We have a derived-to-base cast that produces either an rvalue or an |
| 3780 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3781 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3782 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3784 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 3785 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 3786 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3787 | CurInit.get()->getLocStart(), |
| 3788 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3789 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3790 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3792 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 3793 | QualType T = SourceType; |
| 3794 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 3795 | T = Pointer->getPointeeType(); |
| 3796 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3797 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3798 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 3799 | } |
| 3800 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3801 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3802 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3803 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3804 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3805 | VK_XValue : |
| 3806 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3807 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 3808 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3809 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3810 | CurInit.get(), |
| 3811 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3812 | break; |
| 3813 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3815 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3816 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3817 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 3818 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3819 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3820 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3821 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3822 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3823 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3824 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 3825 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3826 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 3827 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3828 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 3829 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3830 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 3831 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3832 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3833 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3835 | // Reference binding does not have any corresponding ASTs. |
| 3836 | |
| 3837 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3838 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3839 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3841 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3842 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3843 | case SK_BindReferenceToTemporary: |
Anders Carlsson | 3b227bd | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 3844 | // Reference binding does not have any corresponding ASTs. |
| 3845 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3846 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3847 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3848 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3850 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3852 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3854 | /*IsExtraneousCopy=*/true); |
| 3855 | break; |
| 3856 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3857 | case SK_UserConversion: { |
| 3858 | // We have a user-defined conversion that invokes either a constructor |
| 3859 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3860 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3861 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3862 | FunctionDecl *Fn = Step->Function.Function; |
| 3863 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3864 | bool CreatedObject = false; |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3865 | bool IsLvalue = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3866 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3867 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3868 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3869 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3870 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3872 | // Determine the arguments required to actually perform the constructor |
| 3873 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3874 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3875 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3876 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3877 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3878 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3880 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 3882 | move_arg(ConstructorArgs), |
| 3883 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 3884 | CXXConstructExpr::CK_Complete, |
| 3885 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3886 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3887 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3888 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3889 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3890 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3891 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3893 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3894 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 3895 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 3896 | S.IsDerivedFrom(SourceType, Class)) |
| 3897 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3899 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3900 | } else { |
| 3901 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3902 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 3903 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3904 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3905 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 3906 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | |
| 3908 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3909 | // derived-to-base conversion? I believe the answer is "no", because |
| 3910 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3911 | ExprResult CurInitExprRes = |
| 3912 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 3913 | FoundFn, Conversion); |
| 3914 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3915 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3916 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3918 | // Build the actual call to the conversion function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3919 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3920 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3921 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3923 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3925 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3926 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | |
| 3928 | bool RequiresCopy = !IsCopy && |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3929 | getKind() != InitializationSequence::ReferenceBinding; |
| 3930 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3931 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3932 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3933 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3934 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3935 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3936 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3937 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3938 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3939 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 3940 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3941 | } |
| 3942 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3944 | // FIXME: xvalues |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3945 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3946 | CurInit.get()->getType(), |
| 3947 | CastKind, CurInit.get(), 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3948 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3950 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3951 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 3952 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3953 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3954 | break; |
| 3955 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3957 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3958 | case SK_QualificationConversionXValue: |
| 3959 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3960 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3961 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3962 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3963 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3964 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3965 | VK_XValue : |
| 3966 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3967 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3968 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3971 | case SK_ConversionSequence: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3972 | ExprResult CurInitExprRes = |
| 3973 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
| 3974 | getAssignmentAction(Entity), |
| 3975 | Kind.isCStyleOrFunctionalCast()); |
| 3976 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3977 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3978 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3979 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 3980 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3982 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3983 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3984 | QualType Ty = Step->Type; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3985 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3986 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3987 | |
| 3988 | CurInit.release(); |
| 3989 | CurInit = S.Owned(InitList); |
| 3990 | break; |
| 3991 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3992 | |
| 3993 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3994 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3995 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3996 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3998 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3999 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4000 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4001 | ? Kind.getEqualLoc() |
| 4002 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4003 | |
| 4004 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4005 | // Force even a trivial, implicit default constructor to be |
| 4006 | // semantically checked. We do this explicitly because we don't build |
| 4007 | // the definition for completely trivial constructors. |
| 4008 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4009 | assert(ClassDecl && "No parent class for constructor."); |
| 4010 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 4011 | ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false)) |
| 4012 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4013 | } |
| 4014 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4015 | // Determine the arguments required to actually perform the constructor |
| 4016 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4018 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4019 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4020 | |
| 4021 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4022 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4023 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4024 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4025 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4026 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4027 | unsigned NumExprs = ConstructorArgs.size(); |
| 4028 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4029 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4030 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4031 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4032 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4033 | if (!TSInfo) |
| 4034 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4035 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4036 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4037 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4038 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4039 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4040 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4041 | Kind.getParenRange(), |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4042 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4043 | } else { |
| 4044 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4045 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4047 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4048 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4050 | CXXConstructExpr::CK_NonVirtualBase; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4053 | // Only get the parenthesis range if it is a direct construction. |
| 4054 | SourceRange parenRange = |
| 4055 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4056 | Kind.getParenRange() : SourceRange(); |
| 4057 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4058 | // If the entity allows NRVO, mark the construction as elidable |
| 4059 | // unconditionally. |
| 4060 | if (Entity.allowsNRVO()) |
| 4061 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4062 | Constructor, /*Elidable=*/true, |
| 4063 | move_arg(ConstructorArgs), |
| 4064 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4065 | ConstructKind, |
| 4066 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4067 | else |
| 4068 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4070 | move_arg(ConstructorArgs), |
| 4071 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4072 | ConstructKind, |
| 4073 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4074 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4075 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4076 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4077 | |
| 4078 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4079 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4080 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4081 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4082 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4083 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4084 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4086 | break; |
| 4087 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4089 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4090 | step_iterator NextStep = Step; |
| 4091 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4092 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4093 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4094 | // The need for zero-initialization is recorded directly into |
| 4095 | // the call to the object's constructor within the next step. |
| 4096 | ConstructorInitRequiresZeroInit = true; |
| 4097 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4098 | S.getLangOptions().CPlusPlus && |
| 4099 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4100 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4101 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4102 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4103 | Kind.getRange().getBegin()); |
| 4104 | |
| 4105 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4106 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4107 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4108 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4109 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4110 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4111 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4112 | break; |
| 4113 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4114 | |
| 4115 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4116 | QualType SourceType = CurInit.get()->getType(); |
| 4117 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4118 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4119 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4120 | if (Result.isInvalid()) |
| 4121 | return ExprError(); |
| 4122 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4123 | |
| 4124 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4125 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4126 | if (ConvTy != Sema::Compatible && |
| 4127 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4128 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4129 | == Sema::Compatible) |
| 4130 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4131 | if (CurInitExprRes.isInvalid()) |
| 4132 | return ExprError(); |
| 4133 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4135 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4136 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4137 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4138 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4139 | getAssignmentAction(Entity), |
| 4140 | &Complained)) { |
| 4141 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4142 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4143 | } else if (Complained) |
| 4144 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4145 | break; |
| 4146 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4147 | |
| 4148 | case SK_StringInit: { |
| 4149 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4150 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4151 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4152 | break; |
| 4153 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4154 | |
| 4155 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4156 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4157 | CK_ObjCObjectLValueCast, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4158 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4159 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4160 | |
| 4161 | case SK_ArrayInit: |
| 4162 | // Okay: we checked everything before creating this step. Note that |
| 4163 | // this is a GNU extension. |
| 4164 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4165 | << Step->Type << CurInit.get()->getType() |
| 4166 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4167 | |
| 4168 | // If the destination type is an incomplete array type, update the |
| 4169 | // type accordingly. |
| 4170 | if (ResultType) { |
| 4171 | if (const IncompleteArrayType *IncompleteDest |
| 4172 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4173 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4174 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4175 | *ResultType = S.Context.getConstantArrayType( |
| 4176 | IncompleteDest->getElementType(), |
| 4177 | ConstantSource->getSize(), |
| 4178 | ArrayType::Normal, 0); |
| 4179 | } |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | } |
| 4185 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4186 | |
| 4187 | // Diagnose non-fatal problems with the completed initialization. |
| 4188 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4189 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4190 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4191 | cast<FieldDecl>(Entity.getDecl()), |
| 4192 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4194 | return move(CurInit); |
| 4195 | } |
| 4196 | |
| 4197 | //===----------------------------------------------------------------------===// |
| 4198 | // Diagnose initialization failures |
| 4199 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4201 | const InitializedEntity &Entity, |
| 4202 | const InitializationKind &Kind, |
| 4203 | Expr **Args, unsigned NumArgs) { |
| 4204 | if (SequenceKind != FailedSequence) |
| 4205 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4207 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4208 | switch (Failure) { |
| 4209 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4210 | // FIXME: Customize for the initialized entity? |
| 4211 | if (NumArgs == 0) |
| 4212 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4213 | << DestType.getNonReferenceType(); |
| 4214 | else // FIXME: diagnostic below could be better! |
| 4215 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4216 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4217 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4219 | case FK_ArrayNeedsInitList: |
| 4220 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4221 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4222 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4223 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4225 | case FK_ArrayTypeMismatch: |
| 4226 | case FK_NonConstantArrayInit: |
| 4227 | S.Diag(Kind.getLocation(), |
| 4228 | (Failure == FK_ArrayTypeMismatch |
| 4229 | ? diag::err_array_init_different_type |
| 4230 | : diag::err_array_init_non_constant_array)) |
| 4231 | << DestType.getNonReferenceType() |
| 4232 | << Args[0]->getType() |
| 4233 | << Args[0]->getSourceRange(); |
| 4234 | break; |
| 4235 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4236 | case FK_AddressOfOverloadFailed: { |
| 4237 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4238 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4239 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4240 | true, |
| 4241 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4242 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4243 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4245 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4246 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4247 | switch (FailedOverloadResult) { |
| 4248 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4249 | if (Failure == FK_UserConversionOverloadFailed) |
| 4250 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4251 | << Args[0]->getType() << DestType |
| 4252 | << Args[0]->getSourceRange(); |
| 4253 | else |
| 4254 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4255 | << DestType << Args[0]->getType() |
| 4256 | << Args[0]->getSourceRange(); |
| 4257 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4258 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4259 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4261 | case OR_No_Viable_Function: |
| 4262 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4263 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4264 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4265 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4266 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4268 | case OR_Deleted: { |
| 4269 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4270 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4271 | << Args[0]->getSourceRange(); |
| 4272 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4273 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4274 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4275 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4276 | if (Ovl == OR_Deleted) { |
| 4277 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4278 | << Best->Function->isDeleted(); |
| 4279 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4280 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4281 | } |
| 4282 | break; |
| 4283 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4285 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4286 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4287 | break; |
| 4288 | } |
| 4289 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4291 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4292 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4293 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4294 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4295 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4296 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4297 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4298 | << DestType.getNonReferenceType() |
| 4299 | << Args[0]->getType() |
| 4300 | << Args[0]->getSourceRange(); |
| 4301 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4303 | case FK_RValueReferenceBindingToLValue: |
| 4304 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4305 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4306 | << Args[0]->getSourceRange(); |
| 4307 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4309 | case FK_ReferenceInitDropsQualifiers: |
| 4310 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4311 | << DestType.getNonReferenceType() |
| 4312 | << Args[0]->getType() |
| 4313 | << Args[0]->getSourceRange(); |
| 4314 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4316 | case FK_ReferenceInitFailed: |
| 4317 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4318 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4319 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4320 | << Args[0]->getType() |
| 4321 | << Args[0]->getSourceRange(); |
| 4322 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4324 | case FK_ConversionFailed: { |
| 4325 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4326 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4327 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4328 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4329 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4330 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4331 | << Args[0]->getSourceRange(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4332 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4333 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4334 | |
| 4335 | case FK_ConversionFromPropertyFailed: |
| 4336 | // No-op. This error has already been reported. |
| 4337 | break; |
| 4338 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4339 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4340 | SourceRange R; |
| 4341 | |
| 4342 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4343 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4344 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4346 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4347 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4348 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4349 | if (Kind.isCStyleOrFunctionalCast()) |
| 4350 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4351 | << R; |
| 4352 | else |
| 4353 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4354 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4355 | break; |
| 4356 | } |
| 4357 | |
| 4358 | case FK_ReferenceBindingToInitList: |
| 4359 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4360 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4361 | break; |
| 4362 | |
| 4363 | case FK_InitListBadDestinationType: |
| 4364 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4365 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4366 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4368 | case FK_ConstructorOverloadFailed: { |
| 4369 | SourceRange ArgsRange; |
| 4370 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4372 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4374 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4375 | // bad. |
| 4376 | switch (FailedOverloadResult) { |
| 4377 | case OR_Ambiguous: |
| 4378 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4379 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4380 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4381 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4382 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4384 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4385 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4386 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4387 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4388 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4389 | // This is implicit default initialization of a member or |
| 4390 | // base within a constructor. If no viable function was |
| 4391 | // found, notify the user that she needs to explicitly |
| 4392 | // initialize this base/member. |
| 4393 | CXXConstructorDecl *Constructor |
| 4394 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4395 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4396 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4397 | << Constructor->isImplicit() |
| 4398 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4399 | << /*base=*/0 |
| 4400 | << Entity.getType(); |
| 4401 | |
| 4402 | RecordDecl *BaseDecl |
| 4403 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4404 | ->getDecl(); |
| 4405 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4406 | << S.Context.getTagDeclType(BaseDecl); |
| 4407 | } else { |
| 4408 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4409 | << Constructor->isImplicit() |
| 4410 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4411 | << /*member=*/1 |
| 4412 | << Entity.getName(); |
| 4413 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4414 | |
| 4415 | if (const RecordType *Record |
| 4416 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4418 | diag::note_previous_decl) |
| 4419 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4420 | } |
| 4421 | break; |
| 4422 | } |
| 4423 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4424 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4425 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4426 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4427 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4429 | case OR_Deleted: { |
| 4430 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4431 | << true << DestType << ArgsRange; |
| 4432 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4433 | OverloadingResult Ovl |
| 4434 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4435 | if (Ovl == OR_Deleted) { |
| 4436 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4437 | << Best->Function->isDeleted(); |
| 4438 | } else { |
| 4439 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4440 | } |
| 4441 | break; |
| 4442 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4444 | case OR_Success: |
| 4445 | llvm_unreachable("Conversion did not fail!"); |
| 4446 | break; |
| 4447 | } |
| 4448 | break; |
| 4449 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4450 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4451 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4452 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4453 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4454 | // This is implicit default-initialization of a const member in |
| 4455 | // a constructor. Complain that it needs to be explicitly |
| 4456 | // initialized. |
| 4457 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4458 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4459 | << Constructor->isImplicit() |
| 4460 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4461 | << /*const=*/1 |
| 4462 | << Entity.getName(); |
| 4463 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4464 | << Entity.getName(); |
| 4465 | } else { |
| 4466 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4467 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4468 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4469 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4471 | case FK_Incomplete: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4473 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4476 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4477 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4478 | return true; |
| 4479 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4481 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 4482 | switch (SequenceKind) { |
| 4483 | case FailedSequence: { |
| 4484 | OS << "Failed sequence: "; |
| 4485 | switch (Failure) { |
| 4486 | case FK_TooManyInitsForReference: |
| 4487 | OS << "too many initializers for reference"; |
| 4488 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4489 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4490 | case FK_ArrayNeedsInitList: |
| 4491 | OS << "array requires initializer list"; |
| 4492 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4493 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4494 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4495 | OS << "array requires initializer list or string literal"; |
| 4496 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4498 | case FK_ArrayTypeMismatch: |
| 4499 | OS << "array type mismatch"; |
| 4500 | break; |
| 4501 | |
| 4502 | case FK_NonConstantArrayInit: |
| 4503 | OS << "non-constant array initializer"; |
| 4504 | break; |
| 4505 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4506 | case FK_AddressOfOverloadFailed: |
| 4507 | OS << "address of overloaded function failed"; |
| 4508 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4509 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4510 | case FK_ReferenceInitOverloadFailed: |
| 4511 | OS << "overload resolution for reference initialization failed"; |
| 4512 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4514 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4515 | OS << "non-const lvalue reference bound to temporary"; |
| 4516 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4517 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4518 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4519 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4520 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4521 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4522 | case FK_RValueReferenceBindingToLValue: |
| 4523 | OS << "rvalue reference bound to an lvalue"; |
| 4524 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4525 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4526 | case FK_ReferenceInitDropsQualifiers: |
| 4527 | OS << "reference initialization drops qualifiers"; |
| 4528 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4530 | case FK_ReferenceInitFailed: |
| 4531 | OS << "reference initialization failed"; |
| 4532 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4534 | case FK_ConversionFailed: |
| 4535 | OS << "conversion failed"; |
| 4536 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4538 | case FK_ConversionFromPropertyFailed: |
| 4539 | OS << "conversion from property failed"; |
| 4540 | break; |
| 4541 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4542 | case FK_TooManyInitsForScalar: |
| 4543 | OS << "too many initializers for scalar"; |
| 4544 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4546 | case FK_ReferenceBindingToInitList: |
| 4547 | OS << "referencing binding to initializer list"; |
| 4548 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4550 | case FK_InitListBadDestinationType: |
| 4551 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 4552 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4554 | case FK_UserConversionOverloadFailed: |
| 4555 | OS << "overloading failed for user-defined conversion"; |
| 4556 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4557 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4558 | case FK_ConstructorOverloadFailed: |
| 4559 | OS << "constructor overloading failed"; |
| 4560 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4562 | case FK_DefaultInitOfConst: |
| 4563 | OS << "default initialization of a const variable"; |
| 4564 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4566 | case FK_Incomplete: |
| 4567 | OS << "initialization of incomplete type"; |
| 4568 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4570 | OS << '\n'; |
| 4571 | return; |
| 4572 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4574 | case DependentSequence: |
| 4575 | OS << "Dependent sequence: "; |
| 4576 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4578 | case UserDefinedConversion: |
| 4579 | OS << "User-defined conversion sequence: "; |
| 4580 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4582 | case ConstructorInitialization: |
| 4583 | OS << "Constructor initialization sequence: "; |
| 4584 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4586 | case ReferenceBinding: |
| 4587 | OS << "Reference binding: "; |
| 4588 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4589 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4590 | case ListInitialization: |
| 4591 | OS << "List initialization: "; |
| 4592 | break; |
| 4593 | |
| 4594 | case ZeroInitialization: |
| 4595 | OS << "Zero initialization\n"; |
| 4596 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4598 | case NoInitialization: |
| 4599 | OS << "No initialization\n"; |
| 4600 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4602 | case StandardConversion: |
| 4603 | OS << "Standard conversion: "; |
| 4604 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4605 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4606 | case CAssignment: |
| 4607 | OS << "C assignment: "; |
| 4608 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4609 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4610 | case StringInit: |
| 4611 | OS << "String initialization: "; |
| 4612 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4613 | |
| 4614 | case ArrayInit: |
| 4615 | OS << "Array initialization: "; |
| 4616 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4617 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4619 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 4620 | if (S != step_begin()) { |
| 4621 | OS << " -> "; |
| 4622 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4623 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4624 | switch (S->Kind) { |
| 4625 | case SK_ResolveAddressOfOverloadedFunction: |
| 4626 | OS << "resolve address of overloaded function"; |
| 4627 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4629 | case SK_CastDerivedToBaseRValue: |
| 4630 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 4631 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4633 | case SK_CastDerivedToBaseXValue: |
| 4634 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 4635 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4636 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4637 | case SK_CastDerivedToBaseLValue: |
| 4638 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 4639 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4641 | case SK_BindReference: |
| 4642 | OS << "bind reference to lvalue"; |
| 4643 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4645 | case SK_BindReferenceToTemporary: |
| 4646 | OS << "bind reference to a temporary"; |
| 4647 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4649 | case SK_ExtraneousCopyToTemporary: |
| 4650 | OS << "extraneous C++03 copy to temporary"; |
| 4651 | break; |
| 4652 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4653 | case SK_UserConversion: |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4654 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4655 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4657 | case SK_QualificationConversionRValue: |
| 4658 | OS << "qualification conversion (rvalue)"; |
| 4659 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4660 | case SK_QualificationConversionXValue: |
| 4661 | OS << "qualification conversion (xvalue)"; |
| 4662 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4663 | case SK_QualificationConversionLValue: |
| 4664 | OS << "qualification conversion (lvalue)"; |
| 4665 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4667 | case SK_ConversionSequence: |
| 4668 | OS << "implicit conversion sequence ("; |
| 4669 | S->ICS->DebugPrint(); // FIXME: use OS |
| 4670 | OS << ")"; |
| 4671 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4672 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4673 | case SK_ListInitialization: |
| 4674 | OS << "list initialization"; |
| 4675 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4677 | case SK_ConstructorInitialization: |
| 4678 | OS << "constructor initialization"; |
| 4679 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4680 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4681 | case SK_ZeroInitialization: |
| 4682 | OS << "zero initialization"; |
| 4683 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4684 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4685 | case SK_CAssignment: |
| 4686 | OS << "C assignment"; |
| 4687 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4689 | case SK_StringInit: |
| 4690 | OS << "string initialization"; |
| 4691 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4692 | |
| 4693 | case SK_ObjCObjectConversion: |
| 4694 | OS << "Objective-C object conversion"; |
| 4695 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4696 | |
| 4697 | case SK_ArrayInit: |
| 4698 | OS << "array initialization"; |
| 4699 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | void InitializationSequence::dump() const { |
| 4705 | dump(llvm::errs()); |
| 4706 | } |
| 4707 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4708 | //===----------------------------------------------------------------------===// |
| 4709 | // Initialization helper functions |
| 4710 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4712 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 4713 | SourceLocation EqualLoc, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4714 | ExprResult Init) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4715 | if (Init.isInvalid()) |
| 4716 | return ExprError(); |
| 4717 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4718 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4719 | assert(InitE && "No initialization expression?"); |
| 4720 | |
| 4721 | if (EqualLoc.isInvalid()) |
| 4722 | EqualLoc = InitE->getLocStart(); |
| 4723 | |
| 4724 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 4725 | EqualLoc); |
| 4726 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 4727 | Init.release(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4728 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4729 | } |