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 | // |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 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" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 52 | // char array can be initialized with a narrow string. |
| 53 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 54 | if (!SL->isWide()) |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 55 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 56 | |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 57 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 58 | // correction from DR343): "An array with element type compatible with a |
| 59 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 60 | // string literal, optionally enclosed in braces." |
| 61 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 62 | ElemTy.getUnqualifiedType())) |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 63 | return Init; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 68 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 69 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 70 | if (!arrayType) return 0; |
| 71 | |
| 72 | return IsStringInit(init, arrayType, Context); |
| 73 | } |
| 74 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 75 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 76 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 77 | // Get the length of the string as parsed. |
| 78 | uint64_t StrLength = |
| 79 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 80 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 82 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | // 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] | 84 | // being initialized to a string literal. |
| 85 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 86 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 87 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 88 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 89 | ConstVal, |
| 90 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 91 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 94 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 96 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 97 | // the size may be smaller or larger than the string we are initializing. |
| 98 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 99 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 100 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 101 | // For Pascal strings it's OK to strip off the terminating null character, |
| 102 | // so the example below is valid: |
| 103 | // |
| 104 | // unsigned char a[2] = "\pa"; |
| 105 | if (SL->isPascal()) |
| 106 | StrLength--; |
| 107 | } |
| 108 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 109 | // [dcl.init.string]p2 |
| 110 | if (StrLength > CAT->getSize().getZExtValue()) |
| 111 | S.Diag(Str->getSourceRange().getBegin(), |
| 112 | diag::err_initializer_string_for_char_array_too_long) |
| 113 | << Str->getSourceRange(); |
| 114 | } else { |
| 115 | // C99 6.7.8p14. |
| 116 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 117 | S.Diag(Str->getSourceRange().getBegin(), |
| 118 | diag::warn_initializer_string_for_char_array_too_long) |
| 119 | << Str->getSourceRange(); |
| 120 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 122 | // Set the type to the actual size that we are initializing. If we have |
| 123 | // something like: |
| 124 | // char x[1] = "foo"; |
| 125 | // then this will set the string literal's type to char[1]. |
| 126 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
| 130 | // Semantic checking for initializer lists. |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 133 | /// @brief Semantic checking for initializer lists. |
| 134 | /// |
| 135 | /// The InitListChecker class contains a set of routines that each |
| 136 | /// handle the initialization of a certain kind of entity, e.g., |
| 137 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 138 | /// InitListChecker itself performs a recursive walk of the subobject |
| 139 | /// structure of the type to be initialized, while stepping through |
| 140 | /// the initializer list one element at a time. The IList and Index |
| 141 | /// parameters to each of the Check* routines contain the active |
| 142 | /// (syntactic) initializer list and the index into that initializer |
| 143 | /// list that represents the current initializer. Each routine is |
| 144 | /// responsible for moving that Index forward as it consumes elements. |
| 145 | /// |
| 146 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 147 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 148 | /// initializer list and the index into that initializer list where we |
| 149 | /// are copying initializers as we map them over to the semantic |
| 150 | /// list. Once we have completed our recursive walk of the subobject |
| 151 | /// structure, we will have constructed a full semantic initializer |
| 152 | /// list. |
| 153 | /// |
| 154 | /// C99 designators cause changes in the initializer list traversal, |
| 155 | /// because they make the initialization "jump" into a specific |
| 156 | /// subobject and then continue the initialization from that |
| 157 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 158 | /// designated subobject and manages backing out the recursion to |
| 159 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 160 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 161 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 162 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 163 | bool hadError; |
| 164 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 165 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 167 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 168 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 169 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 170 | unsigned &StructuredIndex, |
| 171 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 172 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 173 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 174 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 175 | unsigned &StructuredIndex, |
| 176 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 177 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 178 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 180 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 181 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 182 | unsigned &StructuredIndex, |
| 183 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 184 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 185 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 186 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 187 | InitListExpr *StructuredList, |
| 188 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 189 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 190 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 191 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 192 | InitListExpr *StructuredList, |
| 193 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 194 | void CheckReferenceType(const InitializedEntity &Entity, |
| 195 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 196 | unsigned &Index, |
| 197 | InitListExpr *StructuredList, |
| 198 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 199 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 200 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 201 | InitListExpr *StructuredList, |
| 202 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 203 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 204 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 206 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 208 | unsigned &StructuredIndex, |
| 209 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 210 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 211 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 213 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 214 | InitListExpr *StructuredList, |
| 215 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 216 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 217 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 218 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 220 | RecordDecl::field_iterator *NextField, |
| 221 | llvm::APSInt *NextElementIndex, |
| 222 | unsigned &Index, |
| 223 | InitListExpr *StructuredList, |
| 224 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 225 | bool FinishSubobjectInit, |
| 226 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 227 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 228 | QualType CurrentObjectType, |
| 229 | InitListExpr *StructuredList, |
| 230 | unsigned StructuredIndex, |
| 231 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 232 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 233 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 234 | Expr *expr); |
| 235 | int numArrayElements(QualType DeclType); |
| 236 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 238 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 239 | const InitializedEntity &ParentEntity, |
| 240 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 241 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 242 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 244 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 245 | InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 246 | bool HadError() { return hadError; } |
| 247 | |
| 248 | // @brief Retrieves the fully-structured initializer list used for |
| 249 | // semantic analysis and code generation. |
| 250 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 251 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 252 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 254 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 255 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 256 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 257 | bool &RequiresSecondPass) { |
| 258 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 259 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 261 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 262 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 263 | // FIXME: We probably don't need to handle references |
| 264 | // specially here, since value-initialization of references is |
| 265 | // handled in InitializationSequence. |
| 266 | if (Field->getType()->isReferenceType()) { |
| 267 | // C++ [dcl.init.aggr]p9: |
| 268 | // If an incomplete or empty initializer-list leaves a |
| 269 | // member of reference type uninitialized, the program is |
| 270 | // ill-formed. |
| 271 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 272 | << Field->getType() |
| 273 | << ILE->getSyntacticForm()->getSourceRange(); |
| 274 | SemaRef.Diag(Field->getLocation(), |
| 275 | diag::note_uninit_reference_member); |
| 276 | hadError = true; |
| 277 | return; |
| 278 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 280 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 281 | true); |
| 282 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 283 | if (!InitSeq) { |
| 284 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 285 | hadError = true; |
| 286 | return; |
| 287 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 289 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 290 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 291 | if (MemberInit.isInvalid()) { |
| 292 | hadError = true; |
| 293 | return; |
| 294 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 296 | if (hadError) { |
| 297 | // Do nothing |
| 298 | } else if (Init < NumInits) { |
| 299 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 300 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 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) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 407 | // For arrays, just set the expression used for value-initialization |
| 408 | // of the "holes" in the array. |
| 409 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 410 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 411 | else |
| 412 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 413 | } else { |
| 414 | // For arrays, just set the expression used for value-initialization |
| 415 | // of the rest of elements and exit. |
| 416 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 417 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 418 | return; |
| 419 | } |
| 420 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 421 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 422 | // Value-initialization requires a constructor call, so |
| 423 | // extend the initializer list to include the constructor |
| 424 | // call and make a note that we'll need to take another pass |
| 425 | // through the initializer list. |
| 426 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 427 | RequiresSecondPass = true; |
| 428 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 429 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 430 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 431 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 432 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 437 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 438 | InitListExpr *IL, QualType &T) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 439 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 440 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 441 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 442 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 443 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 445 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 446 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 447 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 448 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 450 | if (!hadError) { |
| 451 | bool RequiresSecondPass = false; |
| 452 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 453 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 455 | RequiresSecondPass); |
| 456 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 460 | // FIXME: use a proper constant |
| 461 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 462 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 463 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 464 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 465 | } |
| 466 | return maxElements; |
| 467 | } |
| 468 | |
| 469 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 470 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 471 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 473 | Field = structDecl->field_begin(), |
| 474 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 475 | Field != FieldEnd; ++Field) { |
| 476 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 477 | ++InitializableMembers; |
| 478 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 479 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 480 | return std::min(InitializableMembers, 1); |
| 481 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 484 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 485 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 486 | QualType T, unsigned &Index, |
| 487 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 488 | unsigned &StructuredIndex, |
| 489 | bool TopLevelObject) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 490 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 492 | if (T->isArrayType()) |
| 493 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 494 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 495 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 496 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 497 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 498 | else |
| 499 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 500 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 501 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 502 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 503 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 504 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 505 | hadError = true; |
| 506 | return; |
| 507 | } |
| 508 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 509 | // Build a structured initializer list corresponding to this subobject. |
| 510 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 512 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 513 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 514 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 515 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 517 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 518 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 520 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | StructuredSubobjectInitList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 522 | StructuredSubobjectInitIndex, |
| 523 | TopLevelObject); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 524 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 525 | StructuredSubobjectInitList->setType(T); |
| 526 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 527 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 528 | // range corresponds with the end of the last initializer it used. |
| 529 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 531 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 532 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 533 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 534 | |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 535 | // Warn about missing braces. |
| 536 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 5cbff48 | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 537 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 538 | diag::warn_missing_braces) |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 539 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 540 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 541 | "{") |
| 542 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 543 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 544 | "}"); |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 545 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 548 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 549 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 550 | unsigned &Index, |
| 551 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 552 | unsigned &StructuredIndex, |
| 553 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 554 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 555 | SyntacticToSemantic[IList] = StructuredList; |
| 556 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 557 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 558 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 559 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 560 | IList->setType(ExprTy); |
| 561 | StructuredList->setType(ExprTy); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 562 | if (hadError) |
| 563 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 564 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 565 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 566 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 567 | if (StructuredIndex == 1 && |
| 568 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 569 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 570 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 571 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 572 | hadError = true; |
| 573 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 574 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 575 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 576 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 577 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 578 | // Don't complain for incomplete types, since we'll get an error |
| 579 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 580 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 582 | CurrentObjectType->isArrayType()? 0 : |
| 583 | CurrentObjectType->isVectorType()? 1 : |
| 584 | CurrentObjectType->isScalarType()? 2 : |
| 585 | CurrentObjectType->isUnionType()? 3 : |
| 586 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 587 | |
| 588 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 589 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 590 | DK = diag::err_excess_initializers; |
| 591 | hadError = true; |
| 592 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 593 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 594 | DK = diag::err_excess_initializers; |
| 595 | hadError = true; |
| 596 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 597 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 598 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 599 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 602 | |
Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 603 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 604 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 605 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 606 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 607 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 610 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 611 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 613 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 614 | unsigned &Index, |
| 615 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 616 | unsigned &StructuredIndex, |
| 617 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 618 | if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 619 | CheckScalarType(Entity, IList, DeclType, Index, |
| 620 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 621 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 623 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 624 | } else if (DeclType->isAggregateType()) { |
| 625 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 626 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 627 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 628 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 629 | StructuredList, StructuredIndex, |
| 630 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 631 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 632 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 633 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 634 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 635 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 636 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 637 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 638 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 639 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 640 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 641 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 642 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 643 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 644 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 645 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 646 | } else if (DeclType->isRecordType()) { |
| 647 | // C++ [dcl.init]p14: |
| 648 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 649 | // is a brace-enclosed list, see 8.5.1. |
| 650 | // |
| 651 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 652 | // we have an initializer list and a destination type that is not |
| 653 | // an aggregate. |
| 654 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 655 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 656 | << DeclType << IList->getSourceRange(); |
| 657 | hadError = true; |
| 658 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 659 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 660 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 661 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 662 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 663 | << DeclType; |
| 664 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 665 | } else { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 666 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 667 | << DeclType; |
| 668 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 672 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 673 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | unsigned &Index, |
| 676 | InitListExpr *StructuredList, |
| 677 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 678 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 679 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 680 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 681 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 683 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 684 | StructuredList, StructuredIndex, |
| 685 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 686 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 687 | newStructuredList, newStructuredIndex); |
| 688 | ++StructuredIndex; |
| 689 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 690 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 691 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 692 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 693 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 694 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 695 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 696 | StructuredList, StructuredIndex); |
| 697 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 698 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 699 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 700 | // arrayType can be incomplete if we're initializing a flexible |
| 701 | // array member. There's nothing we can do with the completed |
| 702 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 703 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 704 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 705 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 706 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 707 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 708 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 709 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 710 | |
| 711 | // Fall through for subaggregate initialization. |
| 712 | |
| 713 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 714 | // C++ [dcl.init.aggr]p12: |
| 715 | // All implicit type conversions (clause 4) are considered when |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 716 | // initializing the aggregate member with an ini- tializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 717 | // an initializer-list. If the initializer can initialize a |
| 718 | // member, the member is initialized. [...] |
| 719 | |
| 720 | // FIXME: Better EqualLoc? |
| 721 | InitializationKind Kind = |
| 722 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 723 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 724 | |
| 725 | if (Seq) { |
| 726 | ExprResult Result = |
| 727 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 728 | if (Result.isInvalid()) |
| 729 | hadError = true; |
| 730 | |
| 731 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 732 | Result.takeAs<Expr>()); |
| 733 | ++Index; |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | // Fall through for subaggregate initialization |
| 738 | } else { |
| 739 | // C99 6.7.8p13: |
| 740 | // |
| 741 | // The initializer for a structure or union object that has |
| 742 | // automatic storage duration shall be either an initializer |
| 743 | // list as described below, or a single expression that has |
| 744 | // compatible structure or union type. In the latter case, the |
| 745 | // initial value of the object, including unnamed members, is |
| 746 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 747 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 748 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 749 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 750 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 751 | if (ExprRes.isInvalid()) |
| 752 | hadError = true; |
| 753 | else { |
| 754 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 755 | if (ExprRes.isInvalid()) |
| 756 | hadError = true; |
| 757 | } |
| 758 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 759 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 760 | ++Index; |
| 761 | return; |
| 762 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 763 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 764 | // Fall through for subaggregate initialization |
| 765 | } |
| 766 | |
| 767 | // C++ [dcl.init.aggr]p12: |
| 768 | // |
| 769 | // [...] Otherwise, if the member is itself a non-empty |
| 770 | // subaggregate, brace elision is assumed and the initializer is |
| 771 | // considered for the initialization of the first member of |
| 772 | // the subaggregate. |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 773 | if (!SemaRef.getLangOptions().OpenCL && |
| 774 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 775 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 776 | StructuredIndex); |
| 777 | ++StructuredIndex; |
| 778 | } else { |
| 779 | // We cannot initialize this element, so let |
| 780 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 781 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 782 | SemaRef.Owned(expr), |
| 783 | /*TopLevelOfInitList=*/true); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 784 | hadError = true; |
| 785 | ++Index; |
| 786 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 787 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 790 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 791 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 792 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 793 | InitListExpr *StructuredList, |
| 794 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 795 | if (Index >= IList->getNumInits()) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 796 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 797 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 798 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 799 | ++Index; |
| 800 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 801 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 802 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 803 | |
| 804 | Expr *expr = IList->getInit(Index); |
| 805 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 806 | SemaRef.Diag(SubIList->getLocStart(), |
| 807 | diag::warn_many_braces_around_scalar_init) |
| 808 | << SubIList->getSourceRange(); |
| 809 | |
| 810 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 811 | StructuredIndex); |
| 812 | return; |
| 813 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 814 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 815 | diag::err_designator_for_scalar_init) |
| 816 | << DeclType << expr->getSourceRange(); |
| 817 | hadError = true; |
| 818 | ++Index; |
| 819 | ++StructuredIndex; |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | ExprResult Result = |
| 824 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 825 | SemaRef.Owned(expr), |
| 826 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 827 | |
| 828 | Expr *ResultExpr = 0; |
| 829 | |
| 830 | if (Result.isInvalid()) |
| 831 | hadError = true; // types weren't compatible. |
| 832 | else { |
| 833 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 834 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 835 | if (ResultExpr != expr) { |
| 836 | // The type was promoted, update initializer list. |
| 837 | IList->setInit(Index, ResultExpr); |
| 838 | } |
| 839 | } |
| 840 | if (hadError) |
| 841 | ++StructuredIndex; |
| 842 | else |
| 843 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 844 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 847 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 848 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 849 | unsigned &Index, |
| 850 | InitListExpr *StructuredList, |
| 851 | unsigned &StructuredIndex) { |
| 852 | if (Index < IList->getNumInits()) { |
| 853 | Expr *expr = IList->getInit(Index); |
| 854 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 855 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | << DeclType << IList->getSourceRange(); |
| 857 | hadError = true; |
| 858 | ++Index; |
| 859 | ++StructuredIndex; |
| 860 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 862 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 863 | ExprResult Result = |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 864 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 865 | SemaRef.Owned(expr), |
| 866 | /*TopLevelOfInitList=*/true); |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 867 | |
| 868 | if (Result.isInvalid()) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 869 | hadError = true; |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 870 | |
| 871 | expr = Result.takeAs<Expr>(); |
| 872 | IList->setInit(Index, expr); |
| 873 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 874 | if (hadError) |
| 875 | ++StructuredIndex; |
| 876 | else |
| 877 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 878 | ++Index; |
| 879 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 880 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 881 | // general, it would be useful to pass location information down the stack, |
| 882 | // so that we know the location (or decl) of the "current object" being |
| 883 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 885 | diag::err_init_reference_member_uninitialized) |
| 886 | << DeclType |
| 887 | << IList->getSourceRange(); |
| 888 | hadError = true; |
| 889 | ++Index; |
| 890 | ++StructuredIndex; |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 895 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 896 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 897 | unsigned &Index, |
| 898 | InitListExpr *StructuredList, |
| 899 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 900 | if (Index >= IList->getNumInits()) |
| 901 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 903 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 904 | unsigned maxElements = VT->getNumElements(); |
| 905 | unsigned numEltsInit = 0; |
| 906 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 907 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 908 | if (!SemaRef.getLangOptions().OpenCL) { |
| 909 | // If the initializing element is a vector, try to copy-initialize |
| 910 | // instead of breaking it apart (which is doomed to failure anyway). |
| 911 | Expr *Init = IList->getInit(Index); |
| 912 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 913 | ExprResult Result = |
| 914 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 915 | SemaRef.Owned(Init), |
| 916 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 917 | |
| 918 | Expr *ResultExpr = 0; |
| 919 | if (Result.isInvalid()) |
| 920 | hadError = true; // types weren't compatible. |
| 921 | else { |
| 922 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 923 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 924 | if (ResultExpr != Init) { |
| 925 | // The type was promoted, update initializer list. |
| 926 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 929 | if (hadError) |
| 930 | ++StructuredIndex; |
| 931 | else |
| 932 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 933 | ++Index; |
| 934 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 935 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 937 | InitializedEntity ElementEntity = |
| 938 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 939 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 940 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 941 | // Don't attempt to go past the end of the init list |
| 942 | if (Index >= IList->getNumInits()) |
| 943 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 944 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 945 | ElementEntity.setElementIndex(Index); |
| 946 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 947 | StructuredList, StructuredIndex); |
| 948 | } |
| 949 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 950 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 951 | |
| 952 | InitializedEntity ElementEntity = |
| 953 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 955 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 956 | for (unsigned i = 0; i < maxElements; ++i) { |
| 957 | // Don't attempt to go past the end of the init list |
| 958 | if (Index >= IList->getNumInits()) |
| 959 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 961 | ElementEntity.setElementIndex(Index); |
| 962 | |
| 963 | QualType IType = IList->getInit(Index)->getType(); |
| 964 | if (!IType->isVectorType()) { |
| 965 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 966 | StructuredList, StructuredIndex); |
| 967 | ++numEltsInit; |
| 968 | } else { |
| 969 | QualType VecType; |
| 970 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 971 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 972 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 973 | if (IType->isExtVectorType()) |
| 974 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 975 | else |
| 976 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 977 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 978 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 979 | StructuredList, StructuredIndex); |
| 980 | numEltsInit += numIElts; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // OpenCL requires all elements to be initialized. |
| 985 | if (numEltsInit != maxElements) |
| 986 | if (SemaRef.getLangOptions().OpenCL) |
| 987 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 988 | diag::err_vector_incorrect_num_initializers) |
| 989 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 992 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 993 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 994 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 996 | unsigned &Index, |
| 997 | InitListExpr *StructuredList, |
| 998 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 999 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1000 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1001 | // Check for the special-case of initializing an array with a string. |
| 1002 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1003 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1004 | SemaRef.Context)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1005 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1006 | // We place the string literal directly into the resulting |
| 1007 | // initializer list. This is the only place where the structure |
| 1008 | // of the structured initializer list doesn't match exactly, |
| 1009 | // because doing so would involve allocating one character |
| 1010 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1011 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1012 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1013 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1017 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1018 | // Check for VLAs; in standard C it would be possible to check this |
| 1019 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1020 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1021 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1022 | diag::err_variable_object_no_init) |
| 1023 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1024 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1025 | ++Index; |
| 1026 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | } |
| 1029 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1030 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1031 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1032 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1033 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1034 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1035 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1036 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1037 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1038 | maxElementsKnown = true; |
| 1039 | } |
| 1040 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1041 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1042 | while (Index < IList->getNumInits()) { |
| 1043 | Expr *Init = IList->getInit(Index); |
| 1044 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1045 | // If we're not the subobject that matches up with the '{' for |
| 1046 | // the designator, we shouldn't be handling the |
| 1047 | // designator. Return immediately. |
| 1048 | if (!SubobjectIsDesignatorContext) |
| 1049 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1051 | // Handle this designated initializer. elementIndex will be |
| 1052 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1053 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1054 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1055 | StructuredList, StructuredIndex, true, |
| 1056 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1057 | hadError = true; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1061 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1062 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1063 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1064 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1065 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1067 | // If the array is of incomplete type, keep track of the number of |
| 1068 | // elements in the initializer. |
| 1069 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1070 | maxElements = elementIndex; |
| 1071 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1072 | continue; |
| 1073 | } |
| 1074 | |
| 1075 | // If we know the maximum number of elements, and we've already |
| 1076 | // hit it, stop consuming elements in the initializer list. |
| 1077 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1078 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1079 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1080 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1082 | Entity); |
| 1083 | // Check this element. |
| 1084 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1085 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1086 | ++elementIndex; |
| 1087 | |
| 1088 | // If the array is of incomplete type, keep track of the number of |
| 1089 | // elements in the initializer. |
| 1090 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1091 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1092 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1093 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1094 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1095 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1096 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1097 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1098 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1099 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1100 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1101 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1102 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1103 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1105 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1109 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1110 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1112 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1114 | unsigned &Index, |
| 1115 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1116 | unsigned &StructuredIndex, |
| 1117 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1118 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1120 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1121 | // confusion, we forgo checking the intializer for the entire record. |
| 1122 | if (structDecl->isInvalidDecl()) { |
| 1123 | hadError = true; |
| 1124 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1128 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1129 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1130 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1131 | Field != FieldEnd; ++Field) { |
| 1132 | if (Field->getDeclName()) { |
| 1133 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | return; |
| 1138 | } |
| 1139 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1140 | // If structDecl is a forward declaration, this loop won't do |
| 1141 | // anything except look at designated initializers; That's okay, |
| 1142 | // because an error should get printed out elsewhere. It might be |
| 1143 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1144 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1145 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1146 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1147 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1148 | while (Index < IList->getNumInits()) { |
| 1149 | Expr *Init = IList->getInit(Index); |
| 1150 | |
| 1151 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1152 | // If we're not the subobject that matches up with the '{' for |
| 1153 | // the designator, we shouldn't be handling the |
| 1154 | // designator. Return immediately. |
| 1155 | if (!SubobjectIsDesignatorContext) |
| 1156 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1158 | // Handle this designated initializer. Field will be updated to |
| 1159 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1160 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1161 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1162 | StructuredList, StructuredIndex, |
| 1163 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1164 | hadError = true; |
| 1165 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1166 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Disable check for missing fields when designators are used. |
| 1169 | // This matches gcc behaviour. |
| 1170 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | if (Field == FieldEnd) { |
| 1175 | // We've run out of fields. We're done. |
| 1176 | break; |
| 1177 | } |
| 1178 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1179 | // We've already initialized a member of a union. We're done. |
| 1180 | if (InitializedSomething && DeclType->isUnionType()) |
| 1181 | break; |
| 1182 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1183 | // If we've hit the flexible array member at the end, we're done. |
| 1184 | if (Field->getType()->isIncompleteArrayType()) |
| 1185 | break; |
| 1186 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1187 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1188 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1189 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1190 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1191 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1193 | // Make sure we can use this declaration. |
| 1194 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1195 | IList->getInit(Index)->getLocStart())) { |
| 1196 | ++Index; |
| 1197 | ++Field; |
| 1198 | hadError = true; |
| 1199 | continue; |
| 1200 | } |
| 1201 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1202 | InitializedEntity MemberEntity = |
| 1203 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1204 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1205 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1206 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (DeclType->isUnionType()) { |
| 1209 | // Initialize the first field within the union. |
| 1210 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1211 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1212 | |
| 1213 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1214 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1215 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1216 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1217 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1218 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1219 | // It is possible we have one or more unnamed bitfields remaining. |
| 1220 | // Find first (if any) named field and emit warning. |
| 1221 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1222 | it != end; ++it) { |
| 1223 | if (!it->isUnnamedBitfield()) { |
| 1224 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1225 | diag::warn_missing_field_initializers) << it->getName(); |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1232 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1233 | return; |
| 1234 | |
| 1235 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | if (!TopLevelObject && |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1237 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1238 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1240 | diag::err_flexible_array_init_nonempty) |
| 1241 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1242 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1243 | << *Field; |
| 1244 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1245 | ++Index; |
| 1246 | return; |
| 1247 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1249 | diag::ext_flexible_array_init) |
| 1250 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1251 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1252 | << *Field; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1255 | InitializedEntity MemberEntity = |
| 1256 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1257 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1258 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1259 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1260 | StructuredList, StructuredIndex); |
| 1261 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1263 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1264 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1266 | /// \brief Expand a field designator that refers to a member of an |
| 1267 | /// anonymous struct or union into a series of field designators that |
| 1268 | /// refers to the field within the appropriate subobject. |
| 1269 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1270 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | DesignatedInitExpr *DIE, |
| 1272 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1273 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1274 | typedef DesignatedInitExpr::Designator Designator; |
| 1275 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1276 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1277 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1278 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1279 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1280 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1282 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1283 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1284 | else |
| 1285 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1286 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1287 | assert(isa<FieldDecl>(*PI)); |
| 1288 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | // Expand the current designator into the set of replacement |
| 1292 | // designators, so we have a full subobject path down to where the |
| 1293 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1294 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1295 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1296 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1299 | /// corresponds to FieldName. |
| 1300 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1301 | IdentifierInfo *FieldName) { |
| 1302 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1303 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1304 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1305 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1306 | return IF; |
| 1307 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1308 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1309 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1312 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1313 | /// |
| 1314 | /// Determines whether the designated initializer @p DIE, which |
| 1315 | /// resides at the given @p Index within the initializer list @p |
| 1316 | /// IList, is well-formed for a current object of type @p DeclType |
| 1317 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1319 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1320 | /// |
| 1321 | /// @param IList The initializer list in which this designated |
| 1322 | /// initializer occurs. |
| 1323 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1324 | /// @param DIE The designated initializer expression. |
| 1325 | /// |
| 1326 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1327 | /// |
| 1328 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1329 | /// into which the designation in @p DIE should refer. |
| 1330 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1331 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1332 | /// a field, this will be set to the field declaration corresponding |
| 1333 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1334 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1335 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1336 | /// DIE is an array designator or GNU array-range designator, this |
| 1337 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1338 | /// |
| 1339 | /// @param Index Index into @p IList where the designated initializer |
| 1340 | /// @p DIE occurs. |
| 1341 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1342 | /// @param StructuredList The initializer list expression that |
| 1343 | /// describes all of the subobject initializers in the order they'll |
| 1344 | /// actually be initialized. |
| 1345 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1346 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1348 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1349 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1351 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1352 | QualType &CurrentObjectType, |
| 1353 | RecordDecl::field_iterator *NextField, |
| 1354 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1355 | unsigned &Index, |
| 1356 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1357 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1358 | bool FinishSubobjectInit, |
| 1359 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1360 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1361 | // Check the actual initialization for the designated object type. |
| 1362 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Temporarily remove the designator expression from the |
| 1365 | // initializer list that the child calls see, so that we don't try |
| 1366 | // to re-process the designator. |
| 1367 | unsigned OldIndex = Index; |
| 1368 | IList->setInit(OldIndex, DIE->getInit()); |
| 1369 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1370 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1371 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Restore the designated initializer expression in the syntactic |
| 1374 | // form of the initializer list. |
| 1375 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1376 | DIE->setInit(IList->getInit(OldIndex)); |
| 1377 | IList->setInit(OldIndex, DIE); |
| 1378 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1379 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1382 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1384 | "Need a non-designated initializer list to start from"); |
| 1385 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1386 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1387 | // Determine the structural initializer list that corresponds to the |
| 1388 | // current subobject. |
| 1389 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1391 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1392 | SourceRange(D->getStartLocation(), |
| 1393 | DIE->getSourceRange().getEnd())); |
| 1394 | assert(StructuredList && "Expected a structured initializer list"); |
| 1395 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1396 | if (D->isFieldDesignator()) { |
| 1397 | // C99 6.7.8p7: |
| 1398 | // |
| 1399 | // If a designator has the form |
| 1400 | // |
| 1401 | // . identifier |
| 1402 | // |
| 1403 | // then the current object (defined below) shall have |
| 1404 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1406 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1407 | if (!RT) { |
| 1408 | SourceLocation Loc = D->getDotLoc(); |
| 1409 | if (Loc.isInvalid()) |
| 1410 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1411 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1412 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | ++Index; |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1417 | // Note: we perform a linear search of the fields here, despite |
| 1418 | // the fact that we have a faster lookup method, because we always |
| 1419 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1420 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1421 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1422 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1424 | Field = RT->getDecl()->field_begin(), |
| 1425 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1426 | for (; Field != FieldEnd; ++Field) { |
| 1427 | if (Field->isUnnamedBitfield()) |
| 1428 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1429 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1430 | // If we find a field representing an anonymous field, look in the |
| 1431 | // IndirectFieldDecl that follow for the designated initializer. |
| 1432 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1433 | if (IndirectFieldDecl *IF = |
| 1434 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1435 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1436 | D = DIE->getDesignator(DesigIdx); |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1440 | if (KnownField && KnownField == *Field) |
| 1441 | break; |
| 1442 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1443 | break; |
| 1444 | |
| 1445 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1448 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1449 | // There was no normal field in the struct with the designated |
| 1450 | // name. Perform another lookup for this name, which may find |
| 1451 | // something that we can't designate (e.g., a member function), |
| 1452 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1454 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1455 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1456 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1457 | // Name lookup didn't find anything. Determine whether this |
| 1458 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1459 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1460 | Sema::LookupMemberName); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1461 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1462 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1463 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1464 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1465 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1466 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1467 | ->Equals(RT->getDecl())) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1468 | std::string CorrectedStr( |
| 1469 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1470 | std::string CorrectedQuotedStr( |
| 1471 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1472 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1473 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1474 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1475 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1476 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1477 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1478 | } else { |
| 1479 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1480 | << FieldName << CurrentObjectType; |
| 1481 | ++Index; |
| 1482 | return true; |
| 1483 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1484 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1486 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1487 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1488 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1489 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1491 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1492 | ++Index; |
| 1493 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1494 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1495 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1496 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1497 | // The replacement field comes from typo correction; find it |
| 1498 | // in the list of fields. |
| 1499 | FieldIndex = 0; |
| 1500 | Field = RT->getDecl()->field_begin(); |
| 1501 | for (; Field != FieldEnd; ++Field) { |
| 1502 | if (Field->isUnnamedBitfield()) |
| 1503 | continue; |
| 1504 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1505 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1506 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1507 | break; |
| 1508 | |
| 1509 | ++FieldIndex; |
| 1510 | } |
| 1511 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1512 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1513 | |
| 1514 | // All of the fields of a union are located at the same place in |
| 1515 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1516 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1517 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1518 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1519 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1521 | // Make sure we can use this declaration. |
| 1522 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1523 | ++Index; |
| 1524 | return true; |
| 1525 | } |
| 1526 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1527 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1528 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1530 | // Make sure that our non-designated initializer list has space |
| 1531 | // for a subobject corresponding to this field. |
| 1532 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1533 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1534 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1535 | // This designator names a flexible array member. |
| 1536 | if (Field->getType()->isIncompleteArrayType()) { |
| 1537 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1538 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1539 | // We can't designate an object within the flexible array |
| 1540 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1542 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1544 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1546 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1547 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1548 | << *Field; |
| 1549 | Invalid = true; |
| 1550 | } |
| 1551 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1552 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1553 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1554 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1555 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1556 | diag::err_flexible_array_init_needs_braces) |
| 1557 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1558 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1559 | << *Field; |
| 1560 | Invalid = true; |
| 1561 | } |
| 1562 | |
| 1563 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1565 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1567 | diag::err_flexible_array_init_nonempty) |
| 1568 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1569 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1570 | << *Field; |
| 1571 | Invalid = true; |
| 1572 | } |
| 1573 | |
| 1574 | if (Invalid) { |
| 1575 | ++Index; |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | // Initialize the array. |
| 1580 | bool prevHadError = hadError; |
| 1581 | unsigned newStructuredIndex = FieldIndex; |
| 1582 | unsigned OldIndex = Index; |
| 1583 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1584 | |
| 1585 | InitializedEntity MemberEntity = |
| 1586 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1587 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1588 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1590 | IList->setInit(OldIndex, DIE); |
| 1591 | if (hadError && !prevHadError) { |
| 1592 | ++Field; |
| 1593 | ++FieldIndex; |
| 1594 | if (NextField) |
| 1595 | *NextField = Field; |
| 1596 | StructuredIndex = FieldIndex; |
| 1597 | return true; |
| 1598 | } |
| 1599 | } else { |
| 1600 | // Recurse to check later designated subobjects. |
| 1601 | QualType FieldType = (*Field)->getType(); |
| 1602 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1603 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1604 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1605 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1606 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1607 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1608 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1609 | true, false)) |
| 1610 | return true; |
| 1611 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1612 | |
| 1613 | // Find the position of the next field to be initialized in this |
| 1614 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1615 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1616 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1617 | |
| 1618 | // If this the first designator, our caller will continue checking |
| 1619 | // the rest of this struct/class/union subobject. |
| 1620 | if (IsFirstDesignator) { |
| 1621 | if (NextField) |
| 1622 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1623 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1624 | return false; |
| 1625 | } |
| 1626 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1627 | if (!FinishSubobjectInit) |
| 1628 | return false; |
| 1629 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1630 | // We've already initialized something in the union; we're done. |
| 1631 | if (RT->getDecl()->isUnion()) |
| 1632 | return hadError; |
| 1633 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1634 | // Check the remaining fields within this class/struct/union subobject. |
| 1635 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1636 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1637 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1638 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1639 | return hadError && !prevHadError; |
| 1640 | } |
| 1641 | |
| 1642 | // C99 6.7.8p6: |
| 1643 | // |
| 1644 | // If a designator has the form |
| 1645 | // |
| 1646 | // [ constant-expression ] |
| 1647 | // |
| 1648 | // then the current object (defined below) shall have array |
| 1649 | // type and the expression shall be an integer constant |
| 1650 | // expression. If the array is of unknown size, any |
| 1651 | // nonnegative value is valid. |
| 1652 | // |
| 1653 | // Additionally, cope with the GNU extension that permits |
| 1654 | // designators of the form |
| 1655 | // |
| 1656 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1657 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1658 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1659 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1660 | << CurrentObjectType; |
| 1661 | ++Index; |
| 1662 | return true; |
| 1663 | } |
| 1664 | |
| 1665 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1666 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1667 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1668 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1669 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1670 | DesignatedEndIndex = DesignatedStartIndex; |
| 1671 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1672 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1673 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1675 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1677 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1678 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1680 | // Codegen can't handle evaluating array range designators that have side |
| 1681 | // effects, because we replicate the AST value for each initialized element. |
| 1682 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1683 | // elements with something that has a side effect, so codegen can emit an |
| 1684 | // "error unsupported" error instead of miscompiling the app. |
| 1685 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1686 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1687 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1690 | if (isa<ConstantArrayType>(AT)) { |
| 1691 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1692 | DesignatedStartIndex |
| 1693 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1694 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1695 | DesignatedEndIndex |
| 1696 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1697 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1698 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1699 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1700 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1701 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1702 | << IndexExpr->getSourceRange(); |
| 1703 | ++Index; |
| 1704 | return true; |
| 1705 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1706 | } else { |
| 1707 | // Make sure the bit-widths and signedness match. |
| 1708 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1709 | DesignatedEndIndex |
| 1710 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1711 | else if (DesignatedStartIndex.getBitWidth() < |
| 1712 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1713 | DesignatedStartIndex |
| 1714 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1715 | DesignatedStartIndex.setIsUnsigned(true); |
| 1716 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1717 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1719 | // Make sure that our non-designated initializer list has space |
| 1720 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1721 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1723 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1725 | // Repeatedly perform subobject initializations in the range |
| 1726 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1727 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1728 | // Move to the next designator |
| 1729 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1730 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1731 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1732 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1733 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1735 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1736 | // Recurse to check later designated subobjects. |
| 1737 | QualType ElementType = AT->getElementType(); |
| 1738 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1739 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1740 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1741 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1742 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1743 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1744 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1745 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1746 | return true; |
| 1747 | |
| 1748 | // Move to the next index in the array that we'll be initializing. |
| 1749 | ++DesignatedStartIndex; |
| 1750 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1751 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1752 | |
| 1753 | // If this the first designator, our caller will continue checking |
| 1754 | // the rest of this array subobject. |
| 1755 | if (IsFirstDesignator) { |
| 1756 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1757 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1758 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1759 | return false; |
| 1760 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1762 | if (!FinishSubobjectInit) |
| 1763 | return false; |
| 1764 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1765 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1766 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1768 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1769 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1773 | // Get the structured initializer list for a subobject of type |
| 1774 | // @p CurrentObjectType. |
| 1775 | InitListExpr * |
| 1776 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1777 | QualType CurrentObjectType, |
| 1778 | InitListExpr *StructuredList, |
| 1779 | unsigned StructuredIndex, |
| 1780 | SourceRange InitRange) { |
| 1781 | Expr *ExistingInit = 0; |
| 1782 | if (!StructuredList) |
| 1783 | ExistingInit = SyntacticToSemantic[IList]; |
| 1784 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1785 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1787 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1788 | return Result; |
| 1789 | |
| 1790 | if (ExistingInit) { |
| 1791 | // We are creating an initializer list that initializes the |
| 1792 | // subobjects of the current object, but there was already an |
| 1793 | // initialization that completely initialized the current |
| 1794 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1796 | // struct X { int a, b; }; |
| 1797 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1799 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1800 | // designated initializer re-initializes the whole |
| 1801 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1803 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1804 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1806 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1807 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1808 | << ExistingInit->getSourceRange(); |
| 1809 | } |
| 1810 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1812 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1813 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1814 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1815 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1816 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1818 | // Pre-allocate storage for the structured initializer list. |
| 1819 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1820 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1821 | bool GotNumInits = false; |
| 1822 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1823 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1824 | GotNumInits = true; |
| 1825 | } else if (Index < IList->getNumInits()) { |
| 1826 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1827 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1828 | GotNumInits = true; |
| 1829 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1833 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1834 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1835 | NumElements = CAType->getSize().getZExtValue(); |
| 1836 | // Simple heuristic so that we don't allocate a very large |
| 1837 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1838 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1839 | NumElements = 0; |
| 1840 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1841 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1842 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1843 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1844 | RecordDecl *RDecl = RType->getDecl(); |
| 1845 | if (RDecl->isUnion()) |
| 1846 | NumElements = 1; |
| 1847 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1849 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1852 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1853 | NumElements = IList->getNumInits(); |
| 1854 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1855 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1857 | // Link this new initializer list into the structured initializer |
| 1858 | // lists. |
| 1859 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1860 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1861 | else { |
| 1862 | Result->setSyntacticForm(IList); |
| 1863 | SyntacticToSemantic[IList] = Result; |
| 1864 | } |
| 1865 | |
| 1866 | return Result; |
| 1867 | } |
| 1868 | |
| 1869 | /// Update the initializer at index @p StructuredIndex within the |
| 1870 | /// structured initializer list to the value @p expr. |
| 1871 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1872 | unsigned &StructuredIndex, |
| 1873 | Expr *expr) { |
| 1874 | // No structured initializer list to update |
| 1875 | if (!StructuredList) |
| 1876 | return; |
| 1877 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1878 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1879 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1880 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1882 | diag::warn_initializer_overrides) |
| 1883 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1885 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1886 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1887 | << PrevInit->getSourceRange(); |
| 1888 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1890 | ++StructuredIndex; |
| 1891 | } |
| 1892 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1893 | /// Check that the given Index expression is a valid array designator |
| 1894 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1895 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1896 | /// and produces a reasonable diagnostic if there is a |
| 1897 | /// failure. Returns true if there was an error, false otherwise. If |
| 1898 | /// everything went okay, Value will receive the value of the constant |
| 1899 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1901 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1902 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1903 | |
| 1904 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1905 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1906 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1908 | if (Value.isSigned() && Value.isNegative()) |
| 1909 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1910 | << Value.toString(10) << Index->getSourceRange(); |
| 1911 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1912 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1913 | return false; |
| 1914 | } |
| 1915 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1916 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1917 | SourceLocation Loc, |
| 1918 | bool GNUSyntax, |
| 1919 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1920 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1921 | |
| 1922 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1923 | SmallVector<ASTDesignator, 32> Designators; |
| 1924 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1925 | |
| 1926 | // Build designators and check array designator expressions. |
| 1927 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1928 | const Designator &D = Desig.getDesignator(Idx); |
| 1929 | switch (D.getKind()) { |
| 1930 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1932 | D.getFieldLoc())); |
| 1933 | break; |
| 1934 | |
| 1935 | case Designator::ArrayDesignator: { |
| 1936 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1937 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1938 | if (!Index->isTypeDependent() && |
| 1939 | !Index->isValueDependent() && |
| 1940 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1941 | Invalid = true; |
| 1942 | else { |
| 1943 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1945 | D.getRBracketLoc())); |
| 1946 | InitExpressions.push_back(Index); |
| 1947 | } |
| 1948 | break; |
| 1949 | } |
| 1950 | |
| 1951 | case Designator::ArrayRangeDesignator: { |
| 1952 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1953 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1954 | llvm::APSInt StartValue; |
| 1955 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1956 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1957 | StartIndex->isValueDependent(); |
| 1958 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1959 | EndIndex->isValueDependent(); |
| 1960 | if ((!StartDependent && |
| 1961 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1962 | (!EndDependent && |
| 1963 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1964 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1965 | else { |
| 1966 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1967 | if (StartDependent || EndDependent) { |
| 1968 | // Nothing to compute. |
| 1969 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1970 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1971 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1972 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1974 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1975 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1977 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1978 | Invalid = true; |
| 1979 | } else { |
| 1980 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1982 | D.getEllipsisLoc(), |
| 1983 | D.getRBracketLoc())); |
| 1984 | InitExpressions.push_back(StartIndex); |
| 1985 | InitExpressions.push_back(EndIndex); |
| 1986 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1987 | } |
| 1988 | break; |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | if (Invalid || Init.isInvalid()) |
| 1994 | return ExprError(); |
| 1995 | |
| 1996 | // Clear out the expressions within the designation. |
| 1997 | Desig.ClearExprs(*this); |
| 1998 | |
| 1999 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2000 | = DesignatedInitExpr::Create(Context, |
| 2001 | Designators.data(), Designators.size(), |
| 2002 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2003 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2005 | if (getLangOptions().CPlusPlus) |
Eli Friedman | ea7b85b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2006 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2007 | << DIE->getSourceRange(); |
| 2008 | else if (!getLangOptions().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2009 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2010 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2012 | return Owned(DIE); |
| 2013 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2014 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2015 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2016 | InitListExpr *&InitList, QualType &DeclType) { |
| 2017 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2018 | if (!CheckInitList.HadError()) |
| 2019 | InitList = CheckInitList.getFullyStructuredList(); |
| 2020 | |
| 2021 | return CheckInitList.HadError(); |
| 2022 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2023 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2024 | //===----------------------------------------------------------------------===// |
| 2025 | // Initialization entity |
| 2026 | //===----------------------------------------------------------------------===// |
| 2027 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2028 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2029 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2031 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2032 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2033 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2034 | Type = AT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2035 | } else { |
| 2036 | Kind = EK_VectorElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2037 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2038 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2041 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2042 | CXXBaseSpecifier *Base, |
| 2043 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2044 | { |
| 2045 | InitializedEntity Result; |
| 2046 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2047 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2048 | if (IsInheritedVirtualBase) |
| 2049 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2050 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2051 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2052 | return Result; |
| 2053 | } |
| 2054 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2055 | DeclarationName InitializedEntity::getName() const { |
| 2056 | switch (getKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2057 | case EK_Parameter: { |
| 2058 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2059 | return (D ? D->getDeclName() : DeclarationName()); |
| 2060 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2061 | |
| 2062 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2063 | case EK_Member: |
| 2064 | return VariableOrMember->getDeclName(); |
| 2065 | |
| 2066 | case EK_Result: |
| 2067 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2068 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2069 | case EK_Temporary: |
| 2070 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2071 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2072 | case EK_ArrayElement: |
| 2073 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2074 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2075 | return DeclarationName(); |
| 2076 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2077 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2078 | // Silence GCC warning |
| 2079 | return DeclarationName(); |
| 2080 | } |
| 2081 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2082 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2083 | switch (getKind()) { |
| 2084 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2085 | case EK_Member: |
| 2086 | return VariableOrMember; |
| 2087 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2088 | case EK_Parameter: |
| 2089 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2090 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2091 | case EK_Result: |
| 2092 | case EK_Exception: |
| 2093 | case EK_New: |
| 2094 | case EK_Temporary: |
| 2095 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2096 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2097 | case EK_ArrayElement: |
| 2098 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2099 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2100 | return 0; |
| 2101 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2103 | // Silence GCC warning |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2107 | bool InitializedEntity::allowsNRVO() const { |
| 2108 | switch (getKind()) { |
| 2109 | case EK_Result: |
| 2110 | case EK_Exception: |
| 2111 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2113 | case EK_Variable: |
| 2114 | case EK_Parameter: |
| 2115 | case EK_Member: |
| 2116 | case EK_New: |
| 2117 | case EK_Temporary: |
| 2118 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2119 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2120 | case EK_ArrayElement: |
| 2121 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2122 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2123 | break; |
| 2124 | } |
| 2125 | |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2129 | //===----------------------------------------------------------------------===// |
| 2130 | // Initialization sequence |
| 2131 | //===----------------------------------------------------------------------===// |
| 2132 | |
| 2133 | void InitializationSequence::Step::Destroy() { |
| 2134 | switch (Kind) { |
| 2135 | case SK_ResolveAddressOfOverloadedFunction: |
| 2136 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2137 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2138 | case SK_CastDerivedToBaseLValue: |
| 2139 | case SK_BindReference: |
| 2140 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2141 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2142 | case SK_UserConversion: |
| 2143 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2144 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2145 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2146 | case SK_ListInitialization: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2147 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2148 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2149 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2150 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2151 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2152 | case SK_ArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2153 | case SK_PassByIndirectCopyRestore: |
| 2154 | case SK_PassByIndirectRestore: |
| 2155 | case SK_ProduceObjCObject: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2156 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2157 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2158 | case SK_ConversionSequence: |
| 2159 | delete ICS; |
| 2160 | } |
| 2161 | } |
| 2162 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2163 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2164 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2168 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2169 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2171 | switch (getFailureKind()) { |
| 2172 | case FK_TooManyInitsForReference: |
| 2173 | case FK_ArrayNeedsInitList: |
| 2174 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2175 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2176 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2177 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2178 | case FK_RValueReferenceBindingToLValue: |
| 2179 | case FK_ReferenceInitDropsQualifiers: |
| 2180 | case FK_ReferenceInitFailed: |
| 2181 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2182 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2183 | case FK_TooManyInitsForScalar: |
| 2184 | case FK_ReferenceBindingToInitList: |
| 2185 | case FK_InitListBadDestinationType: |
| 2186 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2187 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2188 | case FK_ArrayTypeMismatch: |
| 2189 | case FK_NonConstantArrayInit: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2190 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2192 | case FK_ReferenceInitOverloadFailed: |
| 2193 | case FK_UserConversionOverloadFailed: |
| 2194 | case FK_ConstructorOverloadFailed: |
| 2195 | return FailedOverloadResult == OR_Ambiguous; |
| 2196 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2198 | return false; |
| 2199 | } |
| 2200 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2201 | bool InitializationSequence::isConstructorInitialization() const { |
| 2202 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2203 | } |
| 2204 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 2205 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2206 | const Expr *Initializer, |
| 2207 | bool *isInitializerConstant, |
| 2208 | APValue *ConstantValue) const { |
| 2209 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2210 | return false; |
| 2211 | |
| 2212 | const Step &LastStep = Steps.back(); |
| 2213 | if (LastStep.Kind != SK_ConversionSequence) |
| 2214 | return false; |
| 2215 | |
| 2216 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2217 | const StandardConversionSequence *SCS = NULL; |
| 2218 | switch (ICS.getKind()) { |
| 2219 | case ImplicitConversionSequence::StandardConversion: |
| 2220 | SCS = &ICS.Standard; |
| 2221 | break; |
| 2222 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2223 | SCS = &ICS.UserDefined.After; |
| 2224 | break; |
| 2225 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2226 | case ImplicitConversionSequence::EllipsisConversion: |
| 2227 | case ImplicitConversionSequence::BadConversion: |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
| 2231 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2232 | // [dcl.init.list]p7: |
| 2233 | // |
| 2234 | // A narrowing conversion is an implicit conversion ... |
| 2235 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2236 | QualType FromType = SCS->getToType(0); |
| 2237 | QualType ToType = SCS->getToType(1); |
| 2238 | switch (PossibleNarrowing) { |
| 2239 | // * from a floating-point type to an integer type, or |
| 2240 | // |
| 2241 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2242 | // type, except where the source is a constant expression and the actual |
| 2243 | // value after conversion will fit into the target type and will produce |
| 2244 | // the original value when converted back to the original type, or |
| 2245 | case ICK_Floating_Integral: |
| 2246 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2247 | *isInitializerConstant = false; |
| 2248 | return true; |
| 2249 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2250 | llvm::APSInt IntConstantValue; |
| 2251 | if (Initializer && |
| 2252 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2253 | // Convert the integer to the floating type. |
| 2254 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2255 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2256 | llvm::APFloat::rmNearestTiesToEven); |
| 2257 | // And back. |
| 2258 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2259 | bool ignored; |
| 2260 | Result.convertToInteger(ConvertedValue, |
| 2261 | llvm::APFloat::rmTowardZero, &ignored); |
| 2262 | // If the resulting value is different, this was a narrowing conversion. |
| 2263 | if (IntConstantValue != ConvertedValue) { |
| 2264 | *isInitializerConstant = true; |
| 2265 | *ConstantValue = APValue(IntConstantValue); |
| 2266 | return true; |
| 2267 | } |
| 2268 | } else { |
| 2269 | // Variables are always narrowings. |
| 2270 | *isInitializerConstant = false; |
| 2271 | return true; |
| 2272 | } |
| 2273 | } |
| 2274 | return false; |
| 2275 | |
| 2276 | // * from long double to double or float, or from double to float, except |
| 2277 | // where the source is a constant expression and the actual value after |
| 2278 | // conversion is within the range of values that can be represented (even |
| 2279 | // if it cannot be represented exactly), or |
| 2280 | case ICK_Floating_Conversion: |
| 2281 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2282 | // FromType is larger than ToType. |
| 2283 | Expr::EvalResult InitializerValue; |
| 2284 | // FIXME: Check whether Initializer is a constant expression according |
| 2285 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2286 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2287 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2288 | // Constant! (Except for FIXME above.) |
| 2289 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2290 | // Convert the source value into the target type. |
| 2291 | bool ignored; |
| 2292 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2293 | Ctx.getFloatTypeSemantics(ToType), |
| 2294 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2295 | // If there was no overflow, the source value is within the range of |
| 2296 | // values that can be represented. |
| 2297 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2298 | *isInitializerConstant = true; |
| 2299 | *ConstantValue = InitializerValue.Val; |
| 2300 | return true; |
| 2301 | } |
| 2302 | } else { |
| 2303 | *isInitializerConstant = false; |
| 2304 | return true; |
| 2305 | } |
| 2306 | } |
| 2307 | return false; |
| 2308 | |
| 2309 | // * from an integer type or unscoped enumeration type to an integer type |
| 2310 | // that cannot represent all the values of the original type, except where |
| 2311 | // the source is a constant expression and the actual value after |
| 2312 | // conversion will fit into the target type and will produce the original |
| 2313 | // value when converted back to the original type. |
| 2314 | case ICK_Integral_Conversion: { |
| 2315 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2316 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2317 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2318 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2319 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2320 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2321 | |
| 2322 | if (FromWidth > ToWidth || |
| 2323 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2324 | // Not all values of FromType can be represented in ToType. |
| 2325 | llvm::APSInt InitializerValue; |
| 2326 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2327 | *isInitializerConstant = true; |
| 2328 | *ConstantValue = APValue(InitializerValue); |
| 2329 | |
| 2330 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2331 | // signed vs. unsigned comparisons. |
| 2332 | InitializerValue = InitializerValue.extend( |
| 2333 | InitializerValue.getBitWidth() + 1); |
| 2334 | // Convert the initializer to and from the target width and signed-ness. |
| 2335 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2336 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2337 | ConvertedValue.setIsSigned(ToSigned); |
| 2338 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2339 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2340 | // If the result is different, this was a narrowing conversion. |
| 2341 | return ConvertedValue != InitializerValue; |
| 2342 | } else { |
| 2343 | // Variables are always narrowings. |
| 2344 | *isInitializerConstant = false; |
| 2345 | return true; |
| 2346 | } |
| 2347 | } |
| 2348 | return false; |
| 2349 | } |
| 2350 | |
| 2351 | default: |
| 2352 | // Other kinds of conversions are not narrowings. |
| 2353 | return false; |
| 2354 | } |
| 2355 | } |
| 2356 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2357 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2358 | FunctionDecl *Function, |
| 2359 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2360 | Step S; |
| 2361 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2362 | S.Type = Function->getType(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2363 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2364 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2365 | Steps.push_back(S); |
| 2366 | } |
| 2367 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2369 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2370 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2371 | switch (VK) { |
| 2372 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2373 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2374 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2375 | default: llvm_unreachable("No such category"); |
| 2376 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2377 | S.Type = BaseType; |
| 2378 | Steps.push_back(S); |
| 2379 | } |
| 2380 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2382 | bool BindingTemporary) { |
| 2383 | Step S; |
| 2384 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2385 | S.Type = T; |
| 2386 | Steps.push_back(S); |
| 2387 | } |
| 2388 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2389 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2390 | Step S; |
| 2391 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2392 | S.Type = T; |
| 2393 | Steps.push_back(S); |
| 2394 | } |
| 2395 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2396 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2397 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2398 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | Step S; |
| 2400 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2401 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2402 | S.Function.Function = Function; |
| 2403 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2404 | Steps.push_back(S); |
| 2405 | } |
| 2406 | |
| 2407 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2408 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2409 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2410 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2411 | switch (VK) { |
| 2412 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2413 | S.Kind = SK_QualificationConversionRValue; |
| 2414 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2415 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2416 | S.Kind = SK_QualificationConversionXValue; |
| 2417 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2418 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2419 | S.Kind = SK_QualificationConversionLValue; |
| 2420 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2421 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2422 | S.Type = Ty; |
| 2423 | Steps.push_back(S); |
| 2424 | } |
| 2425 | |
| 2426 | void InitializationSequence::AddConversionSequenceStep( |
| 2427 | const ImplicitConversionSequence &ICS, |
| 2428 | QualType T) { |
| 2429 | Step S; |
| 2430 | S.Kind = SK_ConversionSequence; |
| 2431 | S.Type = T; |
| 2432 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2433 | Steps.push_back(S); |
| 2434 | } |
| 2435 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2436 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2437 | Step S; |
| 2438 | S.Kind = SK_ListInitialization; |
| 2439 | S.Type = T; |
| 2440 | Steps.push_back(S); |
| 2441 | } |
| 2442 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2443 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2444 | InitializationSequence::AddConstructorInitializationStep( |
| 2445 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2446 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2447 | QualType T) { |
| 2448 | Step S; |
| 2449 | S.Kind = SK_ConstructorInitialization; |
| 2450 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2451 | S.Function.Function = Constructor; |
| 2452 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2453 | Steps.push_back(S); |
| 2454 | } |
| 2455 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2456 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2457 | Step S; |
| 2458 | S.Kind = SK_ZeroInitialization; |
| 2459 | S.Type = T; |
| 2460 | Steps.push_back(S); |
| 2461 | } |
| 2462 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2463 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2464 | Step S; |
| 2465 | S.Kind = SK_CAssignment; |
| 2466 | S.Type = T; |
| 2467 | Steps.push_back(S); |
| 2468 | } |
| 2469 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2470 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2471 | Step S; |
| 2472 | S.Kind = SK_StringInit; |
| 2473 | S.Type = T; |
| 2474 | Steps.push_back(S); |
| 2475 | } |
| 2476 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2477 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2478 | Step S; |
| 2479 | S.Kind = SK_ObjCObjectConversion; |
| 2480 | S.Type = T; |
| 2481 | Steps.push_back(S); |
| 2482 | } |
| 2483 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2484 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2485 | Step S; |
| 2486 | S.Kind = SK_ArrayInit; |
| 2487 | S.Type = T; |
| 2488 | Steps.push_back(S); |
| 2489 | } |
| 2490 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2491 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2492 | bool shouldCopy) { |
| 2493 | Step s; |
| 2494 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2495 | : SK_PassByIndirectRestore); |
| 2496 | s.Type = type; |
| 2497 | Steps.push_back(s); |
| 2498 | } |
| 2499 | |
| 2500 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2501 | Step S; |
| 2502 | S.Kind = SK_ProduceObjCObject; |
| 2503 | S.Type = T; |
| 2504 | Steps.push_back(S); |
| 2505 | } |
| 2506 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2508 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2509 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2510 | this->Failure = Failure; |
| 2511 | this->FailedOverloadResult = Result; |
| 2512 | } |
| 2513 | |
| 2514 | //===----------------------------------------------------------------------===// |
| 2515 | // Attempt initialization |
| 2516 | //===----------------------------------------------------------------------===// |
| 2517 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2518 | static void MaybeProduceObjCObject(Sema &S, |
| 2519 | InitializationSequence &Sequence, |
| 2520 | const InitializedEntity &Entity) { |
| 2521 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2522 | |
| 2523 | /// When initializing a parameter, produce the value if it's marked |
| 2524 | /// __attribute__((ns_consumed)). |
| 2525 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2526 | if (!Entity.isParameterConsumed()) |
| 2527 | return; |
| 2528 | |
| 2529 | assert(Entity.getType()->isObjCRetainableType() && |
| 2530 | "consuming an object of unretainable type?"); |
| 2531 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2532 | |
| 2533 | /// When initializing a return value, if the return type is a |
| 2534 | /// retainable type, then returns need to immediately retain the |
| 2535 | /// object. If an autorelease is required, it will be done at the |
| 2536 | /// last instant. |
| 2537 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2538 | if (!Entity.getType()->isObjCRetainableType()) |
| 2539 | return; |
| 2540 | |
| 2541 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2542 | } |
| 2543 | } |
| 2544 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2545 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2546 | static void TryListInitialization(Sema &S, |
| 2547 | const InitializedEntity &Entity, |
| 2548 | const InitializationKind &Kind, |
| 2549 | InitListExpr *InitList, |
| 2550 | InitializationSequence &Sequence) { |
| 2551 | // FIXME: We only perform rudimentary checking of list |
| 2552 | // initializations at this point, then assume that any list |
| 2553 | // initialization of an array, aggregate, or scalar will be |
| 2554 | // well-formed. When we actually "perform" list initialization, we'll |
| 2555 | // do all of the necessary checking. C++0x initializer lists will |
| 2556 | // force us to perform more checking here. |
| 2557 | |
| 2558 | QualType DestType = Entity.getType(); |
| 2559 | |
| 2560 | // C++ [dcl.init]p13: |
| 2561 | // If T is a scalar type, then a declaration of the form |
| 2562 | // |
| 2563 | // T x = { a }; |
| 2564 | // |
| 2565 | // is equivalent to |
| 2566 | // |
| 2567 | // T x = a; |
| 2568 | if (DestType->isScalarType()) { |
| 2569 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2570 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | // Assume scalar initialization from a single value works. |
| 2575 | } else if (DestType->isAggregateType()) { |
| 2576 | // Assume aggregate initialization works. |
| 2577 | } else if (DestType->isVectorType()) { |
| 2578 | // Assume vector initialization works. |
| 2579 | } else if (DestType->isReferenceType()) { |
| 2580 | // FIXME: C++0x defines behavior for this. |
| 2581 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2582 | return; |
| 2583 | } else if (DestType->isRecordType()) { |
| 2584 | // FIXME: C++0x defines behavior for this |
| 2585 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2586 | } |
| 2587 | |
| 2588 | // Add a general "list initialization" step. |
| 2589 | Sequence.AddListInitializationStep(DestType); |
| 2590 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2591 | |
| 2592 | /// \brief Try a reference initialization that involves calling a conversion |
| 2593 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2594 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2595 | const InitializedEntity &Entity, |
| 2596 | const InitializationKind &Kind, |
| 2597 | Expr *Initializer, |
| 2598 | bool AllowRValues, |
| 2599 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2600 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2601 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2602 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2603 | QualType cv2T2 = Initializer->getType(); |
| 2604 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2605 | |
| 2606 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2607 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2608 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2610 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2611 | ObjCConversion, |
| 2612 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2613 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2614 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2615 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2616 | (void)ObjCLifetimeConversion; |
| 2617 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2618 | // Build the candidate set directly in the initialization sequence |
| 2619 | // structure, so that it will persist if we fail. |
| 2620 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2621 | CandidateSet.clear(); |
| 2622 | |
| 2623 | // Determine whether we are allowed to call explicit constructors or |
| 2624 | // explicit conversion operators. |
| 2625 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2627 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2628 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2629 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2630 | // The type we're converting to is a class type. Enumerate its constructors |
| 2631 | // to see if there is a suitable conversion. |
| 2632 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2634 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2635 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2636 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2637 | NamedDecl *D = *Con; |
| 2638 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2639 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2640 | // Find the constructor (which may be a template). |
| 2641 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2642 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2643 | if (ConstructorTmpl) |
| 2644 | Constructor = cast<CXXConstructorDecl>( |
| 2645 | ConstructorTmpl->getTemplatedDecl()); |
| 2646 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2647 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2649 | if (!Constructor->isInvalidDecl() && |
| 2650 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2651 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2652 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2653 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2654 | &Initializer, 1, CandidateSet, |
| 2655 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2656 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2657 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2658 | &Initializer, 1, CandidateSet, |
| 2659 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2660 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2661 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2662 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2663 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2664 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2665 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2666 | const RecordType *T2RecordType = 0; |
| 2667 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2668 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2669 | // The type we're converting from is a class type, enumerate its conversion |
| 2670 | // functions. |
| 2671 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2672 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2673 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2674 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2675 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2676 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2677 | NamedDecl *D = *I; |
| 2678 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2679 | if (isa<UsingShadowDecl>(D)) |
| 2680 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2682 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2683 | CXXConversionDecl *Conv; |
| 2684 | if (ConvTemplate) |
| 2685 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2686 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2687 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2689 | // If the conversion function doesn't return a reference type, |
| 2690 | // it can't be considered for this conversion unless we're allowed to |
| 2691 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2692 | // FIXME: Do we need to make sure that we only consider conversion |
| 2693 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2694 | // break recursion. |
| 2695 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2696 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2697 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2698 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2699 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2700 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2701 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2702 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2703 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2707 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2708 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2710 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2711 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2712 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2713 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2714 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2715 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2716 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2718 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2719 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2720 | // This is the overload that will actually be used for the initialization, so |
| 2721 | // mark it as used. |
| 2722 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2723 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2724 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2725 | if (isa<CXXConversionDecl>(Function)) |
| 2726 | T2 = Function->getResultType(); |
| 2727 | else |
| 2728 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2731 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2732 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2733 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2734 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2735 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2736 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2737 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2738 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2739 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2740 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2742 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2743 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2744 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2745 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2747 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2748 | NewDerivedToBase, NewObjCConversion, |
| 2749 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2750 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2751 | // If the type we've converted to is not reference-related to the |
| 2752 | // type we're looking for, then there is another conversion step |
| 2753 | // we need to perform to produce a temporary of the right type |
| 2754 | // that we'll be binding to. |
| 2755 | ImplicitConversionSequence ICS; |
| 2756 | ICS.setStandard(); |
| 2757 | ICS.Standard = Best->FinalConversion; |
| 2758 | T2 = ICS.Standard.getToType(2); |
| 2759 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2760 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2761 | Sequence.AddDerivedToBaseCastStep( |
| 2762 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2763 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2764 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2765 | else if (NewObjCConversion) |
| 2766 | Sequence.AddObjCObjectConversionStep( |
| 2767 | S.Context.getQualifiedType(T1, |
| 2768 | T2.getNonReferenceType().getQualifiers())); |
| 2769 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2770 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2771 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2773 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2774 | return OR_Success; |
| 2775 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2776 | |
| 2777 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2778 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2779 | const InitializedEntity &Entity, |
| 2780 | const InitializationKind &Kind, |
| 2781 | Expr *Initializer, |
| 2782 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2783 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2784 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2785 | Qualifiers T1Quals; |
| 2786 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2787 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2788 | Qualifiers T2Quals; |
| 2789 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2790 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2792 | // If the initializer is the address of an overloaded function, try |
| 2793 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2794 | // type of the resulting function. |
| 2795 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2796 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2798 | T1, |
| 2799 | false, |
| 2800 | Found)) { |
| 2801 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2802 | cv2T2 = Fn->getType(); |
| 2803 | T2 = cv2T2.getUnqualifiedType(); |
| 2804 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2805 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2806 | return; |
| 2807 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2808 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2810 | // Compute some basic properties of the types and the initializer. |
| 2811 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2812 | bool isRValueRef = !isLValueRef; |
| 2813 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2814 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2815 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2816 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2817 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2818 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2819 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2821 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | // 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] | 2823 | // "cv2 T2" as follows: |
| 2824 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2825 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2826 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2827 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2828 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2829 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2830 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2831 | bool T1Function = T1->isFunctionType(); |
| 2832 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2833 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2834 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2836 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2837 | // - 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] | 2838 | // reference-compatible with "cv2 T2," or |
| 2839 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | // 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] | 2841 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2842 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2843 | // to decide whether we're actually binding to a temporary created from |
| 2844 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2845 | if (DerivedToBase) |
| 2846 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2848 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2849 | else if (ObjCConversion) |
| 2850 | Sequence.AddObjCObjectConversionStep( |
| 2851 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2852 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2853 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2854 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2855 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2856 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2857 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2858 | return; |
| 2859 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | |
| 2861 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2862 | // reference-related to T2, and can be implicitly converted to an |
| 2863 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2864 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2865 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2866 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2867 | // If we have an rvalue ref to function type here, the rhs must be |
| 2868 | // an rvalue. |
| 2869 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2870 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2871 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2872 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2873 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2874 | Sequence); |
| 2875 | if (ConvOvlResult == OR_Success) |
| 2876 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2877 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2878 | Sequence.SetOverloadFailure( |
| 2879 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2880 | ConvOvlResult); |
| 2881 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2882 | } |
| 2883 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2884 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2886 | // 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] | 2887 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2888 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2889 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2890 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2891 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2892 | Sequence.SetOverloadFailure( |
| 2893 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2894 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2895 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2896 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2897 | ? (RefRelationship == Sema::Ref_Related |
| 2898 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2899 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2900 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2902 | return; |
| 2903 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2904 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2905 | // - If the initializer expression |
| 2906 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2907 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2908 | // Note: functions are handled below. |
| 2909 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2910 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2911 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2912 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2913 | (InitCategory.isXValue() || |
| 2914 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2915 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2916 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2917 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2918 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2919 | // compiler the freedom to perform a copy here or bind to the |
| 2920 | // object, while C++0x requires that we bind directly to the |
| 2921 | // object. Hence, we always bind to the object without making an |
| 2922 | // extra copy. However, in C++03 requires that we check for the |
| 2923 | // presence of a suitable copy constructor: |
| 2924 | // |
| 2925 | // The constructor that would be used to make the copy shall |
| 2926 | // be callable whether or not the copy is actually done. |
Francois Pichet | 687aaf0 | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2927 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2928 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2929 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2930 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2931 | if (DerivedToBase) |
| 2932 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2933 | ValueKind); |
| 2934 | else if (ObjCConversion) |
| 2935 | Sequence.AddObjCObjectConversionStep( |
| 2936 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2937 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2938 | if (T1Quals != T2Quals) |
| 2939 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2941 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2942 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2944 | |
| 2945 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2946 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2947 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2948 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2949 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2950 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2951 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2952 | Kind, Initializer, |
| 2953 | /*AllowRValues=*/true, |
| 2954 | Sequence); |
| 2955 | if (ConvOvlResult) |
| 2956 | Sequence.SetOverloadFailure( |
| 2957 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2958 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2960 | return; |
| 2961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2963 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2964 | return; |
| 2965 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2966 | |
| 2967 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2968 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2969 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2970 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2972 | // Determine whether we are allowed to call explicit constructors or |
| 2973 | // explicit conversion operators. |
| 2974 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2975 | |
| 2976 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2977 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2978 | ImplicitConversionSequence ICS |
| 2979 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2980 | /*SuppressUserConversions*/ false, |
| 2981 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2982 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2983 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 2984 | /*AllowObjCWritebackConversion=*/false); |
| 2985 | |
| 2986 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2987 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2988 | // this into an overloading ambiguity diagnostic. However, we need |
| 2989 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2990 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2991 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2992 | Sequence.SetOverloadFailure( |
| 2993 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2994 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2995 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2996 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2997 | else |
| 2998 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2999 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3000 | } else { |
| 3001 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3005 | // same cv-qualification as, or greater cv-qualification |
| 3006 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3007 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3008 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3009 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3010 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3011 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3012 | return; |
| 3013 | } |
| 3014 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3015 | // [...] 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] | 3016 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3017 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3018 | InitCategory.isLValue()) { |
| 3019 | Sequence.SetFailed( |
| 3020 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3021 | return; |
| 3022 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3024 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3025 | return; |
| 3026 | } |
| 3027 | |
| 3028 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3030 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3031 | const InitializedEntity &Entity, |
| 3032 | const InitializationKind &Kind, |
| 3033 | Expr *Initializer, |
| 3034 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3035 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3038 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3039 | /// enumerates the constructors of the initialized entity and performs overload |
| 3040 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3041 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3042 | const InitializedEntity &Entity, |
| 3043 | const InitializationKind &Kind, |
| 3044 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3045 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3046 | InitializationSequence &Sequence) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3047 | // Build the candidate set directly in the initialization sequence |
| 3048 | // structure, so that it will persist if we fail. |
| 3049 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3050 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3052 | // Determine whether we are allowed to call explicit constructors or |
| 3053 | // explicit conversion operators. |
| 3054 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3055 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3056 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3057 | |
| 3058 | // The type we're constructing needs to be complete. |
| 3059 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3060 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3061 | return; |
| 3062 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3064 | // The type we're converting to is a class type. Enumerate its constructors |
| 3065 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3066 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3067 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3068 | CXXRecordDecl *DestRecordDecl |
| 3069 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3071 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3072 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3073 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3074 | NamedDecl *D = *Con; |
| 3075 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3076 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3077 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3078 | // Find the constructor (which may be a template). |
| 3079 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3080 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3081 | if (ConstructorTmpl) |
| 3082 | Constructor = cast<CXXConstructorDecl>( |
| 3083 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3084 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3085 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3086 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3087 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3088 | // suppress user-defined conversions on the arguments. |
| 3089 | // FIXME: Move constructors? |
| 3090 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3091 | Constructor->isCopyConstructor()) |
| 3092 | SuppressUserConversions = true; |
| 3093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3095 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3096 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3097 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3098 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3099 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3100 | Args, NumArgs, CandidateSet, |
| 3101 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3102 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3103 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3104 | Args, NumArgs, CandidateSet, |
| 3105 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3106 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3109 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3110 | |
| 3111 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3112 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3113 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3114 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3115 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3117 | Result); |
| 3118 | return; |
| 3119 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3120 | |
| 3121 | // C++0x [dcl.init]p6: |
| 3122 | // If a program calls for the default initialization of an object |
| 3123 | // of a const-qualified type T, T shall be a class type with a |
| 3124 | // user-provided default constructor. |
| 3125 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3126 | Entity.getType().isConstQualified() && |
| 3127 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3128 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3129 | return; |
| 3130 | } |
| 3131 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3132 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3133 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3134 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3135 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3136 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3137 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3140 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3142 | const InitializedEntity &Entity, |
| 3143 | const InitializationKind &Kind, |
| 3144 | InitializationSequence &Sequence) { |
| 3145 | // C++ [dcl.init]p5: |
| 3146 | // |
| 3147 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3148 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3150 | // -- if T is an array type, then each element is value-initialized; |
| 3151 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3152 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3153 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3154 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3155 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3156 | // -- if T is a class type (clause 9) with a user-declared |
| 3157 | // constructor (12.1), then the default constructor for T is |
| 3158 | // called (and the initialization is ill-formed if T has no |
| 3159 | // accessible default constructor); |
| 3160 | // |
| 3161 | // FIXME: we really want to refer to a single subobject of the array, |
| 3162 | // but Entity doesn't have a way to capture that (yet). |
| 3163 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3164 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3166 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3167 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3168 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3169 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3170 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3171 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3172 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3174 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } |
| 3177 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3178 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3181 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3182 | static void TryDefaultInitialization(Sema &S, |
| 3183 | const InitializedEntity &Entity, |
| 3184 | const InitializationKind &Kind, |
| 3185 | InitializationSequence &Sequence) { |
| 3186 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3188 | // C++ [dcl.init]p6: |
| 3189 | // To default-initialize an object of type T means: |
| 3190 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3191 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3192 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3193 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3194 | // constructor for T is called (and the initialization is ill-formed if |
| 3195 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3196 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3197 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3198 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3199 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3201 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3202 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3203 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3204 | // 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] | 3205 | // default constructor. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3206 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3207 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3208 | return; |
| 3209 | } |
| 3210 | |
| 3211 | // If the destination type has a lifetime property, zero-initialize it. |
| 3212 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3213 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3214 | return; |
| 3215 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3218 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3219 | /// which enumerates all conversion functions and performs overload resolution |
| 3220 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3221 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3222 | const InitializedEntity &Entity, |
| 3223 | const InitializationKind &Kind, |
| 3224 | Expr *Initializer, |
| 3225 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3226 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3227 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3228 | QualType SourceType = Initializer->getType(); |
| 3229 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3230 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3232 | // Build the candidate set directly in the initialization sequence |
| 3233 | // structure, so that it will persist if we fail. |
| 3234 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3235 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3236 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3237 | // Determine whether we are allowed to call explicit constructors or |
| 3238 | // explicit conversion operators. |
| 3239 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3240 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3241 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3242 | // The type we're converting to is a class type. Enumerate its constructors |
| 3243 | // to see if there is a suitable conversion. |
| 3244 | CXXRecordDecl *DestRecordDecl |
| 3245 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3247 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3248 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3249 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3250 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3251 | Con != ConEnd; ++Con) { |
| 3252 | NamedDecl *D = *Con; |
| 3253 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3255 | // Find the constructor (which may be a template). |
| 3256 | CXXConstructorDecl *Constructor = 0; |
| 3257 | FunctionTemplateDecl *ConstructorTmpl |
| 3258 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3259 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3260 | Constructor = cast<CXXConstructorDecl>( |
| 3261 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3262 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3263 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3265 | if (!Constructor->isInvalidDecl() && |
| 3266 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3267 | if (ConstructorTmpl) |
| 3268 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3269 | /*ExplicitArgs*/ 0, |
| 3270 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3271 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3272 | else |
| 3273 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3274 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3275 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3276 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3278 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3279 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3280 | |
| 3281 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3282 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3283 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3284 | // The type we're converting from is a class type, enumerate its conversion |
| 3285 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3286 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3287 | // We can only enumerate the conversion functions for a complete type; if |
| 3288 | // the type isn't complete, simply skip this step. |
| 3289 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3290 | CXXRecordDecl *SourceRecordDecl |
| 3291 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3292 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3293 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3294 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3295 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3297 | I != E; ++I) { |
| 3298 | NamedDecl *D = *I; |
| 3299 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3300 | if (isa<UsingShadowDecl>(D)) |
| 3301 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3303 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3304 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3305 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3306 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3307 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3308 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3309 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3310 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3311 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3312 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3313 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3314 | CandidateSet); |
| 3315 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3316 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3317 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3318 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3319 | } |
| 3320 | } |
| 3321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | |
| 3323 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3324 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3325 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3326 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3327 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3328 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3329 | Result); |
| 3330 | return; |
| 3331 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3332 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3333 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3334 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3336 | if (isa<CXXConstructorDecl>(Function)) { |
| 3337 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3338 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3339 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
| 3342 | |
| 3343 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3344 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3345 | if (ConvType->getAs<RecordType>()) { |
| 3346 | // If we're converting to a class type, there may be an copy if |
| 3347 | // the resulting temporary object (possible to create an object of |
| 3348 | // a base class type). That copy is not a separate conversion, so |
| 3349 | // we just make a note of the actual destination type (possibly a |
| 3350 | // base class of the type returned by the conversion function) and |
| 3351 | // let the user-defined conversion step handle the conversion. |
| 3352 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3353 | return; |
| 3354 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3356 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3358 | // If the conversion following the call to the conversion function |
| 3359 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3360 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3361 | Best->FinalConversion.Third) { |
| 3362 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3363 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3364 | ICS.Standard = Best->FinalConversion; |
| 3365 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3366 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3369 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3370 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3371 | |
| 3372 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3373 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3374 | bool isAddressOf) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3375 | // Skip parens. |
| 3376 | e = e->IgnoreParens(); |
| 3377 | |
| 3378 | // Skip address-of nodes. |
| 3379 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3380 | if (op->getOpcode() == UO_AddrOf) |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3381 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3382 | |
| 3383 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3384 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3385 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3386 | case CK_Dependent: |
| 3387 | case CK_BitCast: |
| 3388 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3389 | case CK_NoOp: |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3390 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3391 | |
| 3392 | case CK_ArrayToPointerDecay: |
| 3393 | return IIK_nonscalar; |
| 3394 | |
| 3395 | case CK_NullToPointer: |
| 3396 | return IIK_okay; |
| 3397 | |
| 3398 | default: |
| 3399 | break; |
| 3400 | } |
| 3401 | |
| 3402 | // If we have a declaration reference, it had better be a local variable. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3403 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3404 | if (!isAddressOf) return IIK_nonlocal; |
| 3405 | |
| 3406 | VarDecl *var; |
| 3407 | if (isa<DeclRefExpr>(e)) { |
| 3408 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3409 | if (!var) return IIK_nonlocal; |
| 3410 | } else { |
| 3411 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3412 | } |
| 3413 | |
| 3414 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3415 | |
| 3416 | // If we have a conditional operator, check both sides. |
| 3417 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3418 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3419 | return iik; |
| 3420 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3421 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3422 | |
| 3423 | // These are never scalar. |
| 3424 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3425 | return IIK_nonscalar; |
| 3426 | |
| 3427 | // Otherwise, it needs to be a null pointer constant. |
| 3428 | } else { |
| 3429 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3430 | ? IIK_okay : IIK_nonlocal); |
| 3431 | } |
| 3432 | |
| 3433 | return IIK_nonlocal; |
| 3434 | } |
| 3435 | |
| 3436 | /// Check whether the given expression is a valid operand for an |
| 3437 | /// indirect copy/restore. |
| 3438 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3439 | assert(src->isRValue()); |
| 3440 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3441 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3442 | if (iik == IIK_okay) return; |
| 3443 | |
| 3444 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3445 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3446 | << src->getSourceRange(); |
| 3447 | } |
| 3448 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3449 | /// \brief Determine whether we have compatible array types for the |
| 3450 | /// purposes of GNU by-copy array initialization. |
| 3451 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3452 | const ArrayType *Dest, |
| 3453 | const ArrayType *Source) { |
| 3454 | // If the source and destination array types are equivalent, we're |
| 3455 | // done. |
| 3456 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3457 | return true; |
| 3458 | |
| 3459 | // Make sure that the element types are the same. |
| 3460 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3461 | return false; |
| 3462 | |
| 3463 | // The only mismatch we allow is when the destination is an |
| 3464 | // incomplete array type and the source is a constant array type. |
| 3465 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3466 | } |
| 3467 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3468 | static bool tryObjCWritebackConversion(Sema &S, |
| 3469 | InitializationSequence &Sequence, |
| 3470 | const InitializedEntity &Entity, |
| 3471 | Expr *Initializer) { |
| 3472 | bool ArrayDecay = false; |
| 3473 | QualType ArgType = Initializer->getType(); |
| 3474 | QualType ArgPointee; |
| 3475 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3476 | ArrayDecay = true; |
| 3477 | ArgPointee = ArgArrayType->getElementType(); |
| 3478 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3479 | } |
| 3480 | |
| 3481 | // Handle write-back conversion. |
| 3482 | QualType ConvertedArgType; |
| 3483 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3484 | ConvertedArgType)) |
| 3485 | return false; |
| 3486 | |
| 3487 | // We should copy unless we're passing to an argument explicitly |
| 3488 | // marked 'out'. |
| 3489 | bool ShouldCopy = true; |
| 3490 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3491 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3492 | |
| 3493 | // Do we need an lvalue conversion? |
| 3494 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3495 | ImplicitConversionSequence ICS; |
| 3496 | ICS.setStandard(); |
| 3497 | ICS.Standard.setAsIdentityConversion(); |
| 3498 | |
| 3499 | QualType ResultType; |
| 3500 | if (ArrayDecay) { |
| 3501 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3502 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3503 | } else { |
| 3504 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3505 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3506 | } |
| 3507 | |
| 3508 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3509 | } |
| 3510 | |
| 3511 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3512 | return true; |
| 3513 | } |
| 3514 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3515 | InitializationSequence::InitializationSequence(Sema &S, |
| 3516 | const InitializedEntity &Entity, |
| 3517 | const InitializationKind &Kind, |
| 3518 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3519 | unsigned NumArgs) |
| 3520 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3521 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3523 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3524 | // The semantics of initializers are as follows. The destination type is |
| 3525 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3526 | // 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] | 3527 | // 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] | 3528 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3529 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3530 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3531 | if (DestType->isDependentType() || |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3532 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3533 | SequenceKind = DependentSequence; |
| 3534 | return; |
| 3535 | } |
| 3536 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3537 | // Almost everything is a normal sequence. |
| 3538 | setSequenceKind(NormalSequence); |
| 3539 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3540 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3541 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3542 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3543 | if (Result.isInvalid()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3544 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3545 | return; |
| 3546 | } |
| 3547 | Args[I] = Result.take(); |
| 3548 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3550 | QualType SourceType; |
| 3551 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3552 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3553 | Initializer = Args[0]; |
| 3554 | if (!isa<InitListExpr>(Initializer)) |
| 3555 | SourceType = Initializer->getType(); |
| 3556 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3557 | |
| 3558 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3559 | // list-initialized (8.5.4). |
| 3560 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3561 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3562 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | // - If the destination type is a reference type, see 8.5.3. |
| 3566 | if (DestType->isReferenceType()) { |
| 3567 | // C++0x [dcl.init.ref]p1: |
| 3568 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3569 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3570 | // by an object that can be converted into a T. |
| 3571 | // (Therefore, multiple arguments are not permitted.) |
| 3572 | if (NumArgs != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3573 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3574 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3575 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3576 | return; |
| 3577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3578 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3579 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3580 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3581 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3582 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3583 | return; |
| 3584 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3586 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3587 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3588 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3589 | return; |
| 3590 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3591 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3592 | // - If the destination type is an array of characters, an array of |
| 3593 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3594 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3596 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3597 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3598 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3599 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3600 | return; |
| 3601 | } |
| 3602 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3603 | // Note: as an GNU C extension, we allow initialization of an |
| 3604 | // array from a compound literal that creates an array of the same |
| 3605 | // type, so long as the initializer has no side effects. |
| 3606 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3607 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3608 | Initializer->getType()->isArrayType()) { |
| 3609 | const ArrayType *SourceAT |
| 3610 | = Context.getAsArrayType(Initializer->getType()); |
| 3611 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3612 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3613 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3614 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3615 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3616 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3617 | } |
| 3618 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3619 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3620 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3621 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3623 | return; |
| 3624 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3625 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3626 | // Determine whether we should consider writeback conversions for |
| 3627 | // Objective-C ARC. |
| 3628 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3629 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3630 | |
| 3631 | // We're at the end of the line for C: it's either a write-back conversion |
| 3632 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3633 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3635 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3636 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3637 | return; |
| 3638 | } |
| 3639 | |
| 3640 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3641 | AddCAssignmentStep(DestType); |
| 3642 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3643 | return; |
| 3644 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3645 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3646 | assert(S.getLangOptions().CPlusPlus); |
| 3647 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3648 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3649 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3650 | // - If the initialization is direct-initialization, or if it is |
| 3651 | // copy-initialization where the cv-unqualified version of the |
| 3652 | // 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] | 3653 | // class of the destination, constructors are considered. [...] |
| 3654 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3655 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3656 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3657 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3658 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3659 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3660 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3661 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3662 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3663 | // used) to a derived class thereof are enumerated as described in |
| 3664 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3665 | // (13.3). |
| 3666 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3667 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3668 | return; |
| 3669 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3671 | if (NumArgs > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3672 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3673 | return; |
| 3674 | } |
| 3675 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3676 | |
| 3677 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3678 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3679 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3680 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3681 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3682 | return; |
| 3683 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3684 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3685 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3686 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3687 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3688 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3690 | |
| 3691 | ImplicitConversionSequence ICS |
| 3692 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3693 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3694 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3695 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3696 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3697 | allowObjCWritebackConversion); |
| 3698 | |
| 3699 | if (ICS.isStandard() && |
| 3700 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3701 | // Objective-C ARC writeback conversion. |
| 3702 | |
| 3703 | // We should copy unless we're passing to an argument explicitly |
| 3704 | // marked 'out'. |
| 3705 | bool ShouldCopy = true; |
| 3706 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3707 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3708 | |
| 3709 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3710 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3711 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3712 | ImplicitConversionSequence LvalueICS; |
| 3713 | LvalueICS.setStandard(); |
| 3714 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3715 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3716 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3717 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3718 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3719 | |
| 3720 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3721 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3722 | DeclAccessPair dap; |
| 3723 | if (Initializer->getType() == Context.OverloadTy && |
| 3724 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3725 | , DestType, false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3726 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3727 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3728 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3729 | } else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3730 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3731 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3732 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3733 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3737 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3738 | StepEnd = Steps.end(); |
| 3739 | Step != StepEnd; ++Step) |
| 3740 | Step->Destroy(); |
| 3741 | } |
| 3742 | |
| 3743 | //===----------------------------------------------------------------------===// |
| 3744 | // Perform initialization |
| 3745 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3746 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3747 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3748 | switch(Entity.getKind()) { |
| 3749 | case InitializedEntity::EK_Variable: |
| 3750 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3751 | case InitializedEntity::EK_Exception: |
| 3752 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3753 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3754 | return Sema::AA_Initializing; |
| 3755 | |
| 3756 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3758 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3759 | return Sema::AA_Sending; |
| 3760 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3761 | return Sema::AA_Passing; |
| 3762 | |
| 3763 | case InitializedEntity::EK_Result: |
| 3764 | return Sema::AA_Returning; |
| 3765 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3766 | case InitializedEntity::EK_Temporary: |
| 3767 | // FIXME: Can we tell apart casting vs. converting? |
| 3768 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3769 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3770 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3771 | case InitializedEntity::EK_ArrayElement: |
| 3772 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3773 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3774 | return Sema::AA_Initializing; |
| 3775 | } |
| 3776 | |
| 3777 | return Sema::AA_Converting; |
| 3778 | } |
| 3779 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3780 | /// \brief Whether we should binding a created object as a temporary when |
| 3781 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3782 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3783 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3784 | case InitializedEntity::EK_ArrayElement: |
| 3785 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3786 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3787 | case InitializedEntity::EK_New: |
| 3788 | case InitializedEntity::EK_Variable: |
| 3789 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3790 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3791 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3792 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3793 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3794 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3796 | case InitializedEntity::EK_Parameter: |
| 3797 | case InitializedEntity::EK_Temporary: |
| 3798 | return true; |
| 3799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3801 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3802 | } |
| 3803 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3804 | /// \brief Whether the given entity, when initialized with an object |
| 3805 | /// created for that initialization, requires destruction. |
| 3806 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3807 | switch (Entity.getKind()) { |
| 3808 | case InitializedEntity::EK_Member: |
| 3809 | case InitializedEntity::EK_Result: |
| 3810 | case InitializedEntity::EK_New: |
| 3811 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3812 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3813 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3814 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3815 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3816 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3817 | case InitializedEntity::EK_Variable: |
| 3818 | case InitializedEntity::EK_Parameter: |
| 3819 | case InitializedEntity::EK_Temporary: |
| 3820 | case InitializedEntity::EK_ArrayElement: |
| 3821 | case InitializedEntity::EK_Exception: |
| 3822 | return true; |
| 3823 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3824 | |
| 3825 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3828 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3829 | /// provided by the given initializer by calling the appropriate copy |
| 3830 | /// constructor. |
| 3831 | /// |
| 3832 | /// \param S The Sema object used for type-checking. |
| 3833 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3834 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3835 | /// the type of the initializer expression or a superclass thereof. |
| 3836 | /// |
| 3837 | /// \param Enter The entity being initialized. |
| 3838 | /// |
| 3839 | /// \param CurInit The initializer expression. |
| 3840 | /// |
| 3841 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3842 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3843 | /// an rvalue. |
| 3844 | /// |
| 3845 | /// \returns An expression that copies the initializer expression into |
| 3846 | /// a temporary object, or an error expression if a copy could not be |
| 3847 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3848 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3849 | QualType T, |
| 3850 | const InitializedEntity &Entity, |
| 3851 | ExprResult CurInit, |
| 3852 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3853 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3854 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3855 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3856 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3857 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3858 | if (!Class) |
| 3859 | return move(CurInit); |
| 3860 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3861 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3862 | // When certain criteria are met, an implementation is allowed to |
| 3863 | // omit the copy/move construction of a class object, even if the |
| 3864 | // copy/move constructor and/or destructor for the object have |
| 3865 | // side effects. [...] |
| 3866 | // - when a temporary class object that has not been bound to a |
| 3867 | // reference (12.2) would be copied/moved to a class object |
| 3868 | // with the same cv-unqualified type, the copy/move operation |
| 3869 | // can be omitted by constructing the temporary object |
| 3870 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3872 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3873 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3875 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3876 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3877 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3878 | switch (Entity.getKind()) { |
| 3879 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3880 | Loc = Entity.getReturnLoc(); |
| 3881 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3883 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3884 | Loc = Entity.getThrowLoc(); |
| 3885 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3887 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3888 | Loc = Entity.getDecl()->getLocation(); |
| 3889 | break; |
| 3890 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3891 | case InitializedEntity::EK_ArrayElement: |
| 3892 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3893 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3894 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3895 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3896 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3897 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3898 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3899 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3900 | Loc = CurInitExpr->getLocStart(); |
| 3901 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3902 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3903 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3904 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3905 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3906 | return move(CurInit); |
| 3907 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3908 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3909 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3910 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3911 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3912 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3913 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3914 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3915 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3916 | CXXConstructorDecl *Constructor = 0; |
| 3917 | |
| 3918 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3919 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3920 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3921 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3922 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3923 | continue; |
| 3924 | |
| 3925 | DeclAccessPair FoundDecl |
| 3926 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3927 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3928 | &CurInitExpr, 1, CandidateSet); |
| 3929 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3931 | |
| 3932 | // Handle constructor templates. |
| 3933 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3934 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3935 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3937 | Constructor = cast<CXXConstructorDecl>( |
| 3938 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3939 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3940 | continue; |
| 3941 | |
| 3942 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3943 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3944 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3945 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3946 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3947 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3948 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3950 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3951 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3952 | case OR_Success: |
| 3953 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3955 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3956 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3957 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3958 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3959 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3960 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3961 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3962 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3963 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3964 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3966 | case OR_Ambiguous: |
| 3967 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3968 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3969 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3970 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3971 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3972 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3973 | case OR_Deleted: |
| 3974 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3975 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3976 | << CurInitExpr->getSourceRange(); |
| 3977 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3978 | << 1 << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3979 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3982 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3983 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3984 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3985 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3986 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3987 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3988 | |
| 3989 | if (IsExtraneousCopy) { |
| 3990 | // If this is a totally extraneous copy for C++03 reference |
| 3991 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3992 | // expression. We don't generate an (elided) copy operation here |
| 3993 | // because doing so would require us to pass down a flag to avoid |
| 3994 | // infinite recursion, where each step adds another extraneous, |
| 3995 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3997 | // Instantiate the default arguments of any extra parameters in |
| 3998 | // the selected copy constructor, as if we were going to create a |
| 3999 | // proper call to the copy constructor. |
| 4000 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4001 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4002 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4003 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4004 | break; |
| 4005 | |
| 4006 | // Build the default argument expression; we don't actually care |
| 4007 | // if this succeeds or not, because this routine will complain |
| 4008 | // if there was a problem. |
| 4009 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4010 | } |
| 4011 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4012 | return S.Owned(CurInitExpr); |
| 4013 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4014 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4015 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4016 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4017 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4018 | // constructor call (we might have derived-to-base conversions, or |
| 4019 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4020 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4021 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4022 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4024 | // Actually perform the constructor call. |
| 4025 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4026 | move_arg(ConstructorArgs), |
| 4027 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4028 | CXXConstructExpr::CK_Complete, |
| 4029 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4030 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4031 | // If we're supposed to bind temporaries, do so. |
| 4032 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4033 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4034 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4035 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4036 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4037 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4038 | const InitializedEntity &Entity) { |
| 4039 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4040 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4041 | return; |
| 4042 | |
| 4043 | if (Entity.getDecl()->getDeclName()) |
| 4044 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4045 | << Entity.getDecl()->getDeclName(); |
| 4046 | else |
| 4047 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4048 | } |
| 4049 | } |
| 4050 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4051 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4052 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4053 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4054 | } |
| 4055 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4056 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4057 | InitializationSequence::Perform(Sema &S, |
| 4058 | const InitializedEntity &Entity, |
| 4059 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4060 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4061 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4062 | if (Failed()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4063 | unsigned NumArgs = Args.size(); |
| 4064 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4065 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4066 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4068 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4069 | // If the declaration is a non-dependent, incomplete array type |
| 4070 | // that has an initializer, then its type will be completed once |
| 4071 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4072 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4073 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4074 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4075 | if (const IncompleteArrayType *ArrayT |
| 4076 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4077 | // FIXME: We don't currently have the ability to accurately |
| 4078 | // compute the length of an initializer list without |
| 4079 | // performing full type-checking of the initializer list |
| 4080 | // (since we have to determine where braces are implicitly |
| 4081 | // introduced and such). So, we fall back to making the array |
| 4082 | // type a dependently-sized array type with no specified |
| 4083 | // bound. |
| 4084 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4085 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4086 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4087 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4088 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4089 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4090 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4091 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4092 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4093 | Brackets = ArrayLoc->getBracketsRange(); |
| 4094 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | *ResultType |
| 4098 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4099 | /*NumElts=*/0, |
| 4100 | ArrayT->getSizeModifier(), |
| 4101 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4102 | Brackets); |
| 4103 | } |
| 4104 | |
| 4105 | } |
| 4106 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4107 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4108 | Kind.isExplicitCast()); |
| 4109 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4112 | // No steps means no initialization. |
| 4113 | if (Steps.empty()) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4114 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4116 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4117 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4118 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4119 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4120 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4121 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4122 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4123 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4124 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4126 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4127 | // grab the only argument out the Args and place it into the "current" |
| 4128 | // initializer. |
| 4129 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4130 | case SK_ResolveAddressOfOverloadedFunction: |
| 4131 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4132 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4133 | case SK_CastDerivedToBaseLValue: |
| 4134 | case SK_BindReference: |
| 4135 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4136 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4137 | case SK_UserConversion: |
| 4138 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4139 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4140 | case SK_QualificationConversionRValue: |
| 4141 | case SK_ConversionSequence: |
| 4142 | case SK_ListInitialization: |
| 4143 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4144 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4145 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4146 | case SK_ArrayInit: |
| 4147 | case SK_PassByIndirectCopyRestore: |
| 4148 | case SK_PassByIndirectRestore: |
| 4149 | case SK_ProduceObjCObject: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4150 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4151 | CurInit = Args.get()[0]; |
| 4152 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4153 | |
| 4154 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4155 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4156 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4157 | if (CurInit.isInvalid()) |
| 4158 | return ExprError(); |
| 4159 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4160 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4161 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4162 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4163 | case SK_ConstructorInitialization: |
| 4164 | case SK_ZeroInitialization: |
| 4165 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4166 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | |
| 4168 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4169 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4170 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4171 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4172 | Step != StepEnd; ++Step) { |
| 4173 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4174 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4176 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4178 | switch (Step->Kind) { |
| 4179 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4180 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4181 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4182 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4183 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4184 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4185 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4186 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4187 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4189 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4190 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4191 | case SK_CastDerivedToBaseLValue: { |
| 4192 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4193 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4194 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4195 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4197 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4198 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4199 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4200 | CurInit.get()->getLocStart(), |
| 4201 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4202 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4203 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4205 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4206 | QualType T = SourceType; |
| 4207 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4208 | T = Pointer->getPointeeType(); |
| 4209 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4210 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4211 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4212 | } |
| 4213 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4214 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4215 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4216 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4217 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4218 | VK_XValue : |
| 4219 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4220 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4221 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4222 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4223 | CurInit.get(), |
| 4224 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4225 | break; |
| 4226 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4228 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4229 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4230 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4231 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4232 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4233 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4234 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4235 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4236 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4237 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4238 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4239 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4240 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4241 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4242 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4243 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4244 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4245 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4246 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4248 | // Reference binding does not have any corresponding ASTs. |
| 4249 | |
| 4250 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4251 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4252 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4254 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4256 | case SK_BindReferenceToTemporary: |
| 4257 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4258 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4259 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4261 | // Materialize the temporary into memory. |
Douglas Gregor | 2fa40a3 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4262 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4263 | Entity.getType().getNonReferenceType(), |
| 4264 | CurInit.get(), |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4265 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4266 | |
| 4267 | // If we're binding to an Objective-C object that has lifetime, we |
| 4268 | // need cleanups. |
| 4269 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4270 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4271 | S.ExprNeedsCleanups = true; |
| 4272 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4273 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4275 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4277 | /*IsExtraneousCopy=*/true); |
| 4278 | break; |
| 4279 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | case SK_UserConversion: { |
| 4281 | // We have a user-defined conversion that invokes either a constructor |
| 4282 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4283 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4284 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4285 | FunctionDecl *Fn = Step->Function.Function; |
| 4286 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4287 | bool CreatedObject = false; |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4288 | bool IsLvalue = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4289 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4290 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4291 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4292 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4293 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4294 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4295 | // Determine the arguments required to actually perform the constructor |
| 4296 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4297 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4298 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4299 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4300 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4301 | return ExprError(); |
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 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4304 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4305 | move_arg(ConstructorArgs), |
| 4306 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4307 | CXXConstructExpr::CK_Complete, |
| 4308 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4309 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4310 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4311 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4312 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4313 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4314 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4316 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4317 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4318 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4319 | S.IsDerivedFrom(SourceType, Class)) |
| 4320 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4321 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4322 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4323 | } else { |
| 4324 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4325 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4326 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4327 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4328 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4329 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | |
| 4331 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4332 | // derived-to-base conversion? I believe the answer is "no", because |
| 4333 | // 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] | 4334 | ExprResult CurInitExprRes = |
| 4335 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4336 | FoundFn, Conversion); |
| 4337 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4338 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4339 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4341 | // Build the actual call to the conversion function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4342 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4343 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4344 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4346 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4347 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4348 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4349 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4350 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4351 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4352 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4353 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4354 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4355 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4356 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4358 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4359 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4360 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4361 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4362 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4363 | } |
| 4364 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4366 | // FIXME: xvalues |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4367 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4368 | CurInit.get()->getType(), |
| 4369 | CastKind, CurInit.get(), 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4370 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4372 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4373 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4374 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4375 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4376 | break; |
| 4377 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4378 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4379 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4380 | case SK_QualificationConversionXValue: |
| 4381 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4382 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4383 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4384 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4385 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4386 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4387 | VK_XValue : |
| 4388 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4389 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4390 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4393 | case SK_ConversionSequence: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4394 | Sema::CheckedConversionKind CCK |
| 4395 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4396 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4397 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4398 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4399 | ExprResult CurInitExprRes = |
| 4400 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4401 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4402 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4403 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4404 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4405 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4408 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4409 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4410 | QualType Ty = Step->Type; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4411 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4412 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4413 | |
| 4414 | CurInit.release(); |
| 4415 | CurInit = S.Owned(InitList); |
| 4416 | break; |
| 4417 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4418 | |
| 4419 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4420 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4421 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4422 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4424 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4425 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4426 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4427 | ? Kind.getEqualLoc() |
| 4428 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4429 | |
| 4430 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4431 | // Force even a trivial, implicit default constructor to be |
| 4432 | // semantically checked. We do this explicitly because we don't build |
| 4433 | // the definition for completely trivial constructors. |
| 4434 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4435 | assert(ClassDecl && "No parent class for constructor."); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4436 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4437 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4438 | !Constructor->isUsed(false)) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4439 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4440 | } |
| 4441 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4442 | // Determine the arguments required to actually perform the constructor |
| 4443 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4445 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4446 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | |
| 4448 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4449 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4450 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4451 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4452 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4453 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4454 | unsigned NumExprs = ConstructorArgs.size(); |
| 4455 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4456 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4457 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4458 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4459 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4460 | if (!TSInfo) |
| 4461 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4463 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4464 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4465 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4466 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4467 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4468 | Kind.getParenRange(), |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4469 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4470 | } else { |
| 4471 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4472 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4473 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4474 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4475 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4476 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4477 | CXXConstructExpr::CK_NonVirtualBase; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4478 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4479 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4480 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4482 | // Only get the parenthesis range if it is a direct construction. |
| 4483 | SourceRange parenRange = |
| 4484 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4485 | Kind.getParenRange() : SourceRange(); |
| 4486 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4487 | // If the entity allows NRVO, mark the construction as elidable |
| 4488 | // unconditionally. |
| 4489 | if (Entity.allowsNRVO()) |
| 4490 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4491 | Constructor, /*Elidable=*/true, |
| 4492 | move_arg(ConstructorArgs), |
| 4493 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4494 | ConstructKind, |
| 4495 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4496 | else |
| 4497 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4498 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4499 | move_arg(ConstructorArgs), |
| 4500 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4501 | ConstructKind, |
| 4502 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4503 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4504 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4505 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4506 | |
| 4507 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4508 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4509 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4510 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4512 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4513 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4514 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4515 | break; |
| 4516 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4517 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4518 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4519 | step_iterator NextStep = Step; |
| 4520 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4521 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4522 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4523 | // The need for zero-initialization is recorded directly into |
| 4524 | // the call to the object's constructor within the next step. |
| 4525 | ConstructorInitRequiresZeroInit = true; |
| 4526 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4527 | S.getLangOptions().CPlusPlus && |
| 4528 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4529 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4530 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4532 | Kind.getRange().getBegin()); |
| 4533 | |
| 4534 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4535 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4536 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4537 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4538 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4539 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4540 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4541 | break; |
| 4542 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4543 | |
| 4544 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4545 | QualType SourceType = CurInit.get()->getType(); |
| 4546 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4547 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4548 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4549 | if (Result.isInvalid()) |
| 4550 | return ExprError(); |
| 4551 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4552 | |
| 4553 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4554 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4555 | if (ConvTy != Sema::Compatible && |
| 4556 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4557 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4558 | == Sema::Compatible) |
| 4559 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4560 | if (CurInitExprRes.isInvalid()) |
| 4561 | return ExprError(); |
| 4562 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4564 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4565 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4566 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4567 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4568 | getAssignmentAction(Entity), |
| 4569 | &Complained)) { |
| 4570 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4571 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4572 | } else if (Complained) |
| 4573 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4574 | break; |
| 4575 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4576 | |
| 4577 | case SK_StringInit: { |
| 4578 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4579 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4580 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4581 | break; |
| 4582 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4583 | |
| 4584 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4585 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4586 | CK_ObjCObjectLValueCast, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4587 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4588 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4589 | |
| 4590 | case SK_ArrayInit: |
| 4591 | // Okay: we checked everything before creating this step. Note that |
| 4592 | // this is a GNU extension. |
| 4593 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4594 | << Step->Type << CurInit.get()->getType() |
| 4595 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4596 | |
| 4597 | // If the destination type is an incomplete array type, update the |
| 4598 | // type accordingly. |
| 4599 | if (ResultType) { |
| 4600 | if (const IncompleteArrayType *IncompleteDest |
| 4601 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4602 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4603 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4604 | *ResultType = S.Context.getConstantArrayType( |
| 4605 | IncompleteDest->getElementType(), |
| 4606 | ConstantSource->getSize(), |
| 4607 | ArrayType::Normal, 0); |
| 4608 | } |
| 4609 | } |
| 4610 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4611 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4612 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4613 | case SK_PassByIndirectCopyRestore: |
| 4614 | case SK_PassByIndirectRestore: |
| 4615 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4616 | CurInit = S.Owned(new (S.Context) |
| 4617 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4618 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4619 | break; |
| 4620 | |
| 4621 | case SK_ProduceObjCObject: |
| 4622 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 4623 | CK_ObjCProduceObject, |
| 4624 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4625 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4626 | } |
| 4627 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4628 | |
| 4629 | // Diagnose non-fatal problems with the completed initialization. |
| 4630 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4631 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4632 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4633 | cast<FieldDecl>(Entity.getDecl()), |
| 4634 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4636 | return move(CurInit); |
| 4637 | } |
| 4638 | |
| 4639 | //===----------------------------------------------------------------------===// |
| 4640 | // Diagnose initialization failures |
| 4641 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4643 | const InitializedEntity &Entity, |
| 4644 | const InitializationKind &Kind, |
| 4645 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4646 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4647 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4649 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4650 | switch (Failure) { |
| 4651 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4652 | // FIXME: Customize for the initialized entity? |
| 4653 | if (NumArgs == 0) |
| 4654 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4655 | << DestType.getNonReferenceType(); |
| 4656 | else // FIXME: diagnostic below could be better! |
| 4657 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4658 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4659 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4661 | case FK_ArrayNeedsInitList: |
| 4662 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4663 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4664 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4665 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4667 | case FK_ArrayTypeMismatch: |
| 4668 | case FK_NonConstantArrayInit: |
| 4669 | S.Diag(Kind.getLocation(), |
| 4670 | (Failure == FK_ArrayTypeMismatch |
| 4671 | ? diag::err_array_init_different_type |
| 4672 | : diag::err_array_init_non_constant_array)) |
| 4673 | << DestType.getNonReferenceType() |
| 4674 | << Args[0]->getType() |
| 4675 | << Args[0]->getSourceRange(); |
| 4676 | break; |
| 4677 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4678 | case FK_AddressOfOverloadFailed: { |
| 4679 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4680 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4681 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4682 | true, |
| 4683 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4684 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4685 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4686 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4687 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4688 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4689 | switch (FailedOverloadResult) { |
| 4690 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4691 | if (Failure == FK_UserConversionOverloadFailed) |
| 4692 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4693 | << Args[0]->getType() << DestType |
| 4694 | << Args[0]->getSourceRange(); |
| 4695 | else |
| 4696 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4697 | << DestType << Args[0]->getType() |
| 4698 | << Args[0]->getSourceRange(); |
| 4699 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4700 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4701 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4703 | case OR_No_Viable_Function: |
| 4704 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4705 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4706 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4707 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4708 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4710 | case OR_Deleted: { |
| 4711 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4712 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4713 | << Args[0]->getSourceRange(); |
| 4714 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4715 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4716 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4717 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4718 | if (Ovl == OR_Deleted) { |
| 4719 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4720 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4721 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4722 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4723 | } |
| 4724 | break; |
| 4725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4727 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4728 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4729 | break; |
| 4730 | } |
| 4731 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4733 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4734 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4736 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4737 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4738 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4739 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4740 | << DestType.getNonReferenceType() |
| 4741 | << Args[0]->getType() |
| 4742 | << Args[0]->getSourceRange(); |
| 4743 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4744 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4745 | case FK_RValueReferenceBindingToLValue: |
| 4746 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4747 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4748 | << Args[0]->getSourceRange(); |
| 4749 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4751 | case FK_ReferenceInitDropsQualifiers: |
| 4752 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4753 | << DestType.getNonReferenceType() |
| 4754 | << Args[0]->getType() |
| 4755 | << Args[0]->getSourceRange(); |
| 4756 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4757 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4758 | case FK_ReferenceInitFailed: |
| 4759 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4760 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4761 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4762 | << Args[0]->getType() |
| 4763 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4764 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4765 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4766 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4767 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4769 | case FK_ConversionFailed: { |
| 4770 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4771 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4772 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4773 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4774 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4775 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4776 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4777 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4778 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4779 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4780 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4781 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4782 | |
| 4783 | case FK_ConversionFromPropertyFailed: |
| 4784 | // No-op. This error has already been reported. |
| 4785 | break; |
| 4786 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4787 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4788 | SourceRange R; |
| 4789 | |
| 4790 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4791 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4792 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4794 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4795 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4796 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4797 | if (Kind.isCStyleOrFunctionalCast()) |
| 4798 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4799 | << R; |
| 4800 | else |
| 4801 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4802 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4803 | break; |
| 4804 | } |
| 4805 | |
| 4806 | case FK_ReferenceBindingToInitList: |
| 4807 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4808 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4809 | break; |
| 4810 | |
| 4811 | case FK_InitListBadDestinationType: |
| 4812 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4813 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4814 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4816 | case FK_ConstructorOverloadFailed: { |
| 4817 | SourceRange ArgsRange; |
| 4818 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4820 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4822 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4823 | // bad. |
| 4824 | switch (FailedOverloadResult) { |
| 4825 | case OR_Ambiguous: |
| 4826 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4827 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4828 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4829 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4830 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4831 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4832 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4833 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4834 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4835 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4836 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4837 | // This is implicit default initialization of a member or |
| 4838 | // base within a constructor. If no viable function was |
| 4839 | // found, notify the user that she needs to explicitly |
| 4840 | // initialize this base/member. |
| 4841 | CXXConstructorDecl *Constructor |
| 4842 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4843 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4844 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4845 | << Constructor->isImplicit() |
| 4846 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4847 | << /*base=*/0 |
| 4848 | << Entity.getType(); |
| 4849 | |
| 4850 | RecordDecl *BaseDecl |
| 4851 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4852 | ->getDecl(); |
| 4853 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4854 | << S.Context.getTagDeclType(BaseDecl); |
| 4855 | } else { |
| 4856 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4857 | << Constructor->isImplicit() |
| 4858 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4859 | << /*member=*/1 |
| 4860 | << Entity.getName(); |
| 4861 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4862 | |
| 4863 | if (const RecordType *Record |
| 4864 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4866 | diag::note_previous_decl) |
| 4867 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4868 | } |
| 4869 | break; |
| 4870 | } |
| 4871 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4872 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4873 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4874 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4875 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4876 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4877 | case OR_Deleted: { |
| 4878 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4879 | << true << DestType << ArgsRange; |
| 4880 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4881 | OverloadingResult Ovl |
| 4882 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4883 | if (Ovl == OR_Deleted) { |
| 4884 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4885 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4886 | } else { |
| 4887 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4888 | } |
| 4889 | break; |
| 4890 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4892 | case OR_Success: |
| 4893 | llvm_unreachable("Conversion did not fail!"); |
| 4894 | break; |
| 4895 | } |
| 4896 | break; |
| 4897 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4899 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4900 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4901 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4902 | // This is implicit default-initialization of a const member in |
| 4903 | // a constructor. Complain that it needs to be explicitly |
| 4904 | // initialized. |
| 4905 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4906 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4907 | << Constructor->isImplicit() |
| 4908 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4909 | << /*const=*/1 |
| 4910 | << Entity.getName(); |
| 4911 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4912 | << Entity.getName(); |
| 4913 | } else { |
| 4914 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4915 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4916 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4917 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4919 | case FK_Incomplete: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4920 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4921 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4922 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4923 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4925 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4926 | return true; |
| 4927 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4928 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4929 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4930 | switch (SequenceKind) { |
| 4931 | case FailedSequence: { |
| 4932 | OS << "Failed sequence: "; |
| 4933 | switch (Failure) { |
| 4934 | case FK_TooManyInitsForReference: |
| 4935 | OS << "too many initializers for reference"; |
| 4936 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4938 | case FK_ArrayNeedsInitList: |
| 4939 | OS << "array requires initializer list"; |
| 4940 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4942 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4943 | OS << "array requires initializer list or string literal"; |
| 4944 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4946 | case FK_ArrayTypeMismatch: |
| 4947 | OS << "array type mismatch"; |
| 4948 | break; |
| 4949 | |
| 4950 | case FK_NonConstantArrayInit: |
| 4951 | OS << "non-constant array initializer"; |
| 4952 | break; |
| 4953 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4954 | case FK_AddressOfOverloadFailed: |
| 4955 | OS << "address of overloaded function failed"; |
| 4956 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4958 | case FK_ReferenceInitOverloadFailed: |
| 4959 | OS << "overload resolution for reference initialization failed"; |
| 4960 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4962 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4963 | OS << "non-const lvalue reference bound to temporary"; |
| 4964 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4966 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4967 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4968 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4970 | case FK_RValueReferenceBindingToLValue: |
| 4971 | OS << "rvalue reference bound to an lvalue"; |
| 4972 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4974 | case FK_ReferenceInitDropsQualifiers: |
| 4975 | OS << "reference initialization drops qualifiers"; |
| 4976 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4978 | case FK_ReferenceInitFailed: |
| 4979 | OS << "reference initialization failed"; |
| 4980 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4981 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4982 | case FK_ConversionFailed: |
| 4983 | OS << "conversion failed"; |
| 4984 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4985 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4986 | case FK_ConversionFromPropertyFailed: |
| 4987 | OS << "conversion from property failed"; |
| 4988 | break; |
| 4989 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4990 | case FK_TooManyInitsForScalar: |
| 4991 | OS << "too many initializers for scalar"; |
| 4992 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4994 | case FK_ReferenceBindingToInitList: |
| 4995 | OS << "referencing binding to initializer list"; |
| 4996 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4997 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4998 | case FK_InitListBadDestinationType: |
| 4999 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5000 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5001 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5002 | case FK_UserConversionOverloadFailed: |
| 5003 | OS << "overloading failed for user-defined conversion"; |
| 5004 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5005 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5006 | case FK_ConstructorOverloadFailed: |
| 5007 | OS << "constructor overloading failed"; |
| 5008 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5009 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5010 | case FK_DefaultInitOfConst: |
| 5011 | OS << "default initialization of a const variable"; |
| 5012 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5013 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5014 | case FK_Incomplete: |
| 5015 | OS << "initialization of incomplete type"; |
| 5016 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5017 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5018 | OS << '\n'; |
| 5019 | return; |
| 5020 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5021 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5022 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5023 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5024 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5026 | case NormalSequence: |
| 5027 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5028 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5029 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5030 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5031 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5032 | if (S != step_begin()) { |
| 5033 | OS << " -> "; |
| 5034 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5036 | switch (S->Kind) { |
| 5037 | case SK_ResolveAddressOfOverloadedFunction: |
| 5038 | OS << "resolve address of overloaded function"; |
| 5039 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5041 | case SK_CastDerivedToBaseRValue: |
| 5042 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5043 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5045 | case SK_CastDerivedToBaseXValue: |
| 5046 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5047 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5049 | case SK_CastDerivedToBaseLValue: |
| 5050 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5051 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5053 | case SK_BindReference: |
| 5054 | OS << "bind reference to lvalue"; |
| 5055 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5057 | case SK_BindReferenceToTemporary: |
| 5058 | OS << "bind reference to a temporary"; |
| 5059 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5061 | case SK_ExtraneousCopyToTemporary: |
| 5062 | OS << "extraneous C++03 copy to temporary"; |
| 5063 | break; |
| 5064 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5065 | case SK_UserConversion: |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5066 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5067 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5068 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5069 | case SK_QualificationConversionRValue: |
| 5070 | OS << "qualification conversion (rvalue)"; |
| 5071 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5072 | case SK_QualificationConversionXValue: |
| 5073 | OS << "qualification conversion (xvalue)"; |
| 5074 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5075 | case SK_QualificationConversionLValue: |
| 5076 | OS << "qualification conversion (lvalue)"; |
| 5077 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5079 | case SK_ConversionSequence: |
| 5080 | OS << "implicit conversion sequence ("; |
| 5081 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5082 | OS << ")"; |
| 5083 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5085 | case SK_ListInitialization: |
| 5086 | OS << "list initialization"; |
| 5087 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5089 | case SK_ConstructorInitialization: |
| 5090 | OS << "constructor initialization"; |
| 5091 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5093 | case SK_ZeroInitialization: |
| 5094 | OS << "zero initialization"; |
| 5095 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5097 | case SK_CAssignment: |
| 5098 | OS << "C assignment"; |
| 5099 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5101 | case SK_StringInit: |
| 5102 | OS << "string initialization"; |
| 5103 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5104 | |
| 5105 | case SK_ObjCObjectConversion: |
| 5106 | OS << "Objective-C object conversion"; |
| 5107 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5108 | |
| 5109 | case SK_ArrayInit: |
| 5110 | OS << "array initialization"; |
| 5111 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5112 | |
| 5113 | case SK_PassByIndirectCopyRestore: |
| 5114 | OS << "pass by indirect copy and restore"; |
| 5115 | break; |
| 5116 | |
| 5117 | case SK_PassByIndirectRestore: |
| 5118 | OS << "pass by indirect restore"; |
| 5119 | break; |
| 5120 | |
| 5121 | case SK_ProduceObjCObject: |
| 5122 | OS << "Objective-C object retension"; |
| 5123 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | void InitializationSequence::dump() const { |
| 5129 | dump(llvm::errs()); |
| 5130 | } |
| 5131 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 5132 | static void DiagnoseNarrowingInInitList( |
| 5133 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5134 | bool Constant, const APValue &ConstantValue) { |
| 5135 | if (Constant) { |
| 5136 | S.Diag(InitE->getLocStart(), |
| 5137 | S.getLangOptions().CPlusPlus0x |
| 5138 | ? diag::err_init_list_constant_narrowing |
| 5139 | : diag::warn_init_list_constant_narrowing) |
| 5140 | << InitE->getSourceRange() |
| 5141 | << ConstantValue |
| 5142 | << EntityType; |
| 5143 | } else |
| 5144 | S.Diag(InitE->getLocStart(), |
| 5145 | S.getLangOptions().CPlusPlus0x |
| 5146 | ? diag::err_init_list_variable_narrowing |
| 5147 | : diag::warn_init_list_variable_narrowing) |
| 5148 | << InitE->getSourceRange() |
| 5149 | << InitE->getType() |
| 5150 | << EntityType; |
| 5151 | |
| 5152 | llvm::SmallString<128> StaticCast; |
| 5153 | llvm::raw_svector_ostream OS(StaticCast); |
| 5154 | OS << "static_cast<"; |
| 5155 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5156 | // It's important to use the typedef's name if there is one so that the |
| 5157 | // fixit doesn't break code using types like int64_t. |
| 5158 | // |
| 5159 | // FIXME: This will break if the typedef requires qualification. But |
| 5160 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5161 | OS << TT->getDecl(); |
| 5162 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5163 | OS << BT->getName(S.getLangOptions()); |
| 5164 | else { |
| 5165 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5166 | // with a broken cast. |
| 5167 | return; |
| 5168 | } |
| 5169 | OS << ">("; |
| 5170 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5171 | << InitE->getSourceRange() |
| 5172 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5173 | << FixItHint::CreateInsertion( |
| 5174 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5175 | } |
| 5176 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5177 | //===----------------------------------------------------------------------===// |
| 5178 | // Initialization helper functions |
| 5179 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5180 | bool |
| 5181 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5182 | ExprResult Init) { |
| 5183 | if (Init.isInvalid()) |
| 5184 | return false; |
| 5185 | |
| 5186 | Expr *InitE = Init.get(); |
| 5187 | assert(InitE && "No initialization expression"); |
| 5188 | |
| 5189 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5190 | SourceLocation()); |
| 5191 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5192 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5195 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5196 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5197 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 5198 | ExprResult Init, |
| 5199 | bool TopLevelOfInitList) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5200 | if (Init.isInvalid()) |
| 5201 | return ExprError(); |
| 5202 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5203 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5204 | assert(InitE && "No initialization expression?"); |
| 5205 | |
| 5206 | if (EqualLoc.isInvalid()) |
| 5207 | EqualLoc = InitE->getLocStart(); |
| 5208 | |
| 5209 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5210 | EqualLoc); |
| 5211 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5212 | Init.release(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame^] | 5213 | |
| 5214 | bool Constant = false; |
| 5215 | APValue Result; |
| 5216 | if (TopLevelOfInitList && |
| 5217 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5218 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5219 | Constant, Result); |
| 5220 | } |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5221 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5222 | } |