Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Designator.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 26 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 27 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 28 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Sema Initialization Checking |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 33 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 34 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 35 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 36 | return 0; |
| 37 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 38 | // See if this is a string literal or @encode. |
| 39 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // Handle @encode, which is a narrow string. |
| 42 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 43 | return Init; |
| 44 | |
| 45 | // Otherwise we can only handle string literals. |
| 46 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 47 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 48 | |
| 49 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 50 | |
| 51 | switch (SL->getKind()) { |
| 52 | case StringLiteral::Ascii: |
| 53 | case StringLiteral::UTF8: |
| 54 | // char array can be initialized with a narrow string. |
| 55 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 57 | case StringLiteral::UTF16: |
| 58 | return ElemTy->isChar16Type() ? Init : 0; |
| 59 | case StringLiteral::UTF32: |
| 60 | return ElemTy->isChar32Type() ? Init : 0; |
| 61 | case StringLiteral::Wide: |
| 62 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 63 | // correction from DR343): "An array with element type compatible with a |
| 64 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 65 | // string literal, optionally enclosed in braces." |
| 66 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 67 | ElemTy.getUnqualifiedType())) |
| 68 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 73 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 76 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 77 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 78 | if (!arrayType) return 0; |
| 79 | |
| 80 | return IsStringInit(init, arrayType, Context); |
| 81 | } |
| 82 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 83 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 84 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 85 | // Get the length of the string as parsed. |
| 86 | uint64_t StrLength = |
| 87 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 88 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 90 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | // being initialized to a string literal. |
| 93 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 94 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 95 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 96 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 97 | ConstVal, |
| 98 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 99 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 102 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 104 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 105 | // the size may be smaller or larger than the string we are initializing. |
| 106 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 107 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 108 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 109 | // For Pascal strings it's OK to strip off the terminating null character, |
| 110 | // so the example below is valid: |
| 111 | // |
| 112 | // unsigned char a[2] = "\pa"; |
| 113 | if (SL->isPascal()) |
| 114 | StrLength--; |
| 115 | } |
| 116 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 117 | // [dcl.init.string]p2 |
| 118 | if (StrLength > CAT->getSize().getZExtValue()) |
| 119 | S.Diag(Str->getSourceRange().getBegin(), |
| 120 | diag::err_initializer_string_for_char_array_too_long) |
| 121 | << Str->getSourceRange(); |
| 122 | } else { |
| 123 | // C99 6.7.8p14. |
| 124 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 125 | S.Diag(Str->getSourceRange().getBegin(), |
| 126 | diag::warn_initializer_string_for_char_array_too_long) |
| 127 | << Str->getSourceRange(); |
| 128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 130 | // Set the type to the actual size that we are initializing. If we have |
| 131 | // something like: |
| 132 | // char x[1] = "foo"; |
| 133 | // then this will set the string literal's type to char[1]. |
| 134 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // Semantic checking for initializer lists. |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 141 | /// @brief Semantic checking for initializer lists. |
| 142 | /// |
| 143 | /// The InitListChecker class contains a set of routines that each |
| 144 | /// handle the initialization of a certain kind of entity, e.g., |
| 145 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 146 | /// InitListChecker itself performs a recursive walk of the subobject |
| 147 | /// structure of the type to be initialized, while stepping through |
| 148 | /// the initializer list one element at a time. The IList and Index |
| 149 | /// parameters to each of the Check* routines contain the active |
| 150 | /// (syntactic) initializer list and the index into that initializer |
| 151 | /// list that represents the current initializer. Each routine is |
| 152 | /// responsible for moving that Index forward as it consumes elements. |
| 153 | /// |
| 154 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 155 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 156 | /// initializer list and the index into that initializer list where we |
| 157 | /// are copying initializers as we map them over to the semantic |
| 158 | /// list. Once we have completed our recursive walk of the subobject |
| 159 | /// structure, we will have constructed a full semantic initializer |
| 160 | /// list. |
| 161 | /// |
| 162 | /// C99 designators cause changes in the initializer list traversal, |
| 163 | /// because they make the initialization "jump" into a specific |
| 164 | /// subobject and then continue the initialization from that |
| 165 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 166 | /// designated subobject and manages backing out the recursion to |
| 167 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 168 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 169 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 170 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 172 | bool VerifyOnly; // no diagnostics, no structure building |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 174 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 176 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 177 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 178 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 179 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 180 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 181 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 182 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 183 | unsigned &StructuredIndex, |
| 184 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 185 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 186 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 188 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 189 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 190 | unsigned &StructuredIndex, |
| 191 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 192 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 193 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 194 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 195 | InitListExpr *StructuredList, |
| 196 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 197 | void CheckComplexType(const InitializedEntity &Entity, |
| 198 | InitListExpr *IList, QualType DeclType, |
| 199 | unsigned &Index, |
| 200 | InitListExpr *StructuredList, |
| 201 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 202 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 203 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 204 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 205 | InitListExpr *StructuredList, |
| 206 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 207 | void CheckReferenceType(const InitializedEntity &Entity, |
| 208 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 209 | unsigned &Index, |
| 210 | InitListExpr *StructuredList, |
| 211 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 212 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 213 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 214 | InitListExpr *StructuredList, |
| 215 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 216 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 217 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 220 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 221 | unsigned &StructuredIndex, |
| 222 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 223 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 224 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 226 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 227 | InitListExpr *StructuredList, |
| 228 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 229 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 230 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 231 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 233 | RecordDecl::field_iterator *NextField, |
| 234 | llvm::APSInt *NextElementIndex, |
| 235 | unsigned &Index, |
| 236 | InitListExpr *StructuredList, |
| 237 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 238 | bool FinishSubobjectInit, |
| 239 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 241 | QualType CurrentObjectType, |
| 242 | InitListExpr *StructuredList, |
| 243 | unsigned StructuredIndex, |
| 244 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 245 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 246 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 247 | Expr *expr); |
| 248 | int numArrayElements(QualType DeclType); |
| 249 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 251 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 252 | const InitializedEntity &ParentEntity, |
| 253 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 254 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 255 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 256 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 257 | Expr *InitExpr, FieldDecl *Field, |
| 258 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 260 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 261 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 262 | bool HadError() { return hadError; } |
| 263 | |
| 264 | // @brief Retrieves the fully-structured initializer list used for |
| 265 | // semantic analysis and code generation. |
| 266 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 267 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 268 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 270 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 271 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 273 | bool &RequiresSecondPass) { |
| 274 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 275 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 277 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 278 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 279 | // FIXME: We probably don't need to handle references |
| 280 | // specially here, since value-initialization of references is |
| 281 | // handled in InitializationSequence. |
| 282 | if (Field->getType()->isReferenceType()) { |
| 283 | // C++ [dcl.init.aggr]p9: |
| 284 | // If an incomplete or empty initializer-list leaves a |
| 285 | // member of reference type uninitialized, the program is |
| 286 | // ill-formed. |
| 287 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 288 | << Field->getType() |
| 289 | << ILE->getSyntacticForm()->getSourceRange(); |
| 290 | SemaRef.Diag(Field->getLocation(), |
| 291 | diag::note_uninit_reference_member); |
| 292 | hadError = true; |
| 293 | return; |
| 294 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 296 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 297 | true); |
| 298 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 299 | if (!InitSeq) { |
| 300 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 301 | hadError = true; |
| 302 | return; |
| 303 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 304 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 305 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 306 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 307 | if (MemberInit.isInvalid()) { |
| 308 | hadError = true; |
| 309 | return; |
| 310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 312 | if (hadError) { |
| 313 | // Do nothing |
| 314 | } else if (Init < NumInits) { |
| 315 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 316 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 317 | // Value-initialization requires a constructor call, so |
| 318 | // extend the initializer list to include the constructor |
| 319 | // call and make a note that we'll need to take another pass |
| 320 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 321 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 322 | RequiresSecondPass = true; |
| 323 | } |
| 324 | } else if (InitListExpr *InnerILE |
| 325 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 326 | FillInValueInitializations(MemberEntity, InnerILE, |
| 327 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 330 | /// Recursively replaces NULL values within the given initializer list |
| 331 | /// with expressions that perform value-initialization of the |
| 332 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 333 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 334 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 335 | InitListExpr *ILE, |
| 336 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 338 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 339 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 340 | if (ILE->getSyntacticForm()) |
| 341 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 343 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 344 | if (RType->getDecl()->isUnion() && |
| 345 | ILE->getInitializedFieldInUnion()) |
| 346 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 347 | Entity, ILE, RequiresSecondPass); |
| 348 | else { |
| 349 | unsigned Init = 0; |
| 350 | for (RecordDecl::field_iterator |
| 351 | Field = RType->getDecl()->field_begin(), |
| 352 | FieldEnd = RType->getDecl()->field_end(); |
| 353 | Field != FieldEnd; ++Field) { |
| 354 | if (Field->isUnnamedBitfield()) |
| 355 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 357 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 358 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 359 | |
| 360 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 361 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 362 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 363 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 364 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 365 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 366 | // Only look at the first initialization of a union. |
| 367 | if (RType->getDecl()->isUnion()) |
| 368 | break; |
| 369 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 374 | |
| 375 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 377 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 378 | unsigned NumInits = ILE->getNumInits(); |
| 379 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 380 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 381 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 382 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 383 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 385 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 386 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 387 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 388 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 390 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 392 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 395 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 396 | if (hadError) |
| 397 | return; |
| 398 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 399 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 400 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 401 | ElementEntity.setElementIndex(Init); |
| 402 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 403 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 404 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 405 | true); |
| 406 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 407 | if (!InitSeq) { |
| 408 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 409 | hadError = true; |
| 410 | return; |
| 411 | } |
| 412 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 413 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 414 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 415 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 416 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| 420 | if (hadError) { |
| 421 | // Do nothing |
| 422 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 423 | // For arrays, just set the expression used for value-initialization |
| 424 | // of the "holes" in the array. |
| 425 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 426 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 427 | else |
| 428 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 429 | } else { |
| 430 | // For arrays, just set the expression used for value-initialization |
| 431 | // of the rest of elements and exit. |
| 432 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 433 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 434 | return; |
| 435 | } |
| 436 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 437 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 438 | // Value-initialization requires a constructor call, so |
| 439 | // extend the initializer list to include the constructor |
| 440 | // call and make a note that we'll need to take another pass |
| 441 | // through the initializer list. |
| 442 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 443 | RequiresSecondPass = true; |
| 444 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 445 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 446 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 447 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 448 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 453 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 454 | InitListExpr *IL, QualType &T, |
| 455 | bool VerifyOnly) |
| 456 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 457 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 458 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 459 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 460 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 462 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 464 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 465 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 466 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 467 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 468 | bool RequiresSecondPass = false; |
| 469 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 470 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 472 | RequiresSecondPass); |
| 473 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 477 | // FIXME: use a proper constant |
| 478 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 479 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 480 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 481 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 482 | } |
| 483 | return maxElements; |
| 484 | } |
| 485 | |
| 486 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 487 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 488 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 490 | Field = structDecl->field_begin(), |
| 491 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | Field != FieldEnd; ++Field) { |
| 493 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 494 | ++InitializableMembers; |
| 495 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 496 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 497 | return std::min(InitializableMembers, 1); |
| 498 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 501 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 502 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 503 | QualType T, unsigned &Index, |
| 504 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 505 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 506 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 508 | if (T->isArrayType()) |
| 509 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 510 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 511 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 512 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 513 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 514 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 515 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 516 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 517 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 518 | if (!VerifyOnly) |
| 519 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 520 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 521 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 522 | hadError = true; |
| 523 | return; |
| 524 | } |
| 525 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 526 | // Build a structured initializer list corresponding to this subobject. |
| 527 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 529 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 530 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 531 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 532 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 534 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 535 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 536 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 537 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 539 | StructuredSubobjectInitIndex); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 540 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 541 | if (!VerifyOnly) { |
| 542 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 543 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 544 | // Update the structured sub-object initializer so that it's ending |
| 545 | // range corresponds with the end of the last initializer it used. |
| 546 | if (EndIndex < ParentIList->getNumInits()) { |
| 547 | SourceLocation EndLoc |
| 548 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 549 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 550 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 552 | // Warn about missing braces. |
| 553 | if (T->isArrayType() || T->isRecordType()) { |
| 554 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 555 | diag::warn_missing_braces) |
| 556 | << StructuredSubobjectInitList->getSourceRange() |
| 557 | << FixItHint::CreateInsertion( |
| 558 | StructuredSubobjectInitList->getLocStart(), "{") |
| 559 | << FixItHint::CreateInsertion( |
| 560 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 561 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 562 | "}"); |
| 563 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 564 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 567 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 568 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 569 | unsigned &Index, |
| 570 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 571 | unsigned &StructuredIndex, |
| 572 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 573 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 574 | if (!VerifyOnly) { |
| 575 | SyntacticToSemantic[IList] = StructuredList; |
| 576 | StructuredList->setSyntacticForm(IList); |
| 577 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 579 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 580 | if (!VerifyOnly) { |
| 581 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 582 | IList->setType(ExprTy); |
| 583 | StructuredList->setType(ExprTy); |
| 584 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 585 | if (hadError) |
| 586 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 587 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 588 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 589 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 590 | if (VerifyOnly) { |
| 591 | if (SemaRef.getLangOptions().CPlusPlus || |
| 592 | (SemaRef.getLangOptions().OpenCL && |
| 593 | IList->getType()->isVectorType())) { |
| 594 | hadError = true; |
| 595 | } |
| 596 | return; |
| 597 | } |
| 598 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 599 | if (StructuredIndex == 1 && |
| 600 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 601 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 602 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 603 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 604 | hadError = true; |
| 605 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 606 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 607 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 608 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 609 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 610 | // Don't complain for incomplete types, since we'll get an error |
| 611 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 612 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 614 | CurrentObjectType->isArrayType()? 0 : |
| 615 | CurrentObjectType->isVectorType()? 1 : |
| 616 | CurrentObjectType->isScalarType()? 2 : |
| 617 | CurrentObjectType->isUnionType()? 3 : |
| 618 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 619 | |
| 620 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 621 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 622 | DK = diag::err_excess_initializers; |
| 623 | hadError = true; |
| 624 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 625 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 626 | DK = diag::err_excess_initializers; |
| 627 | hadError = true; |
| 628 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 630 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 631 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 634 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 635 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 636 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 637 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 638 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 639 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 640 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 643 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 644 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 646 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 647 | unsigned &Index, |
| 648 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 649 | unsigned &StructuredIndex, |
| 650 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 651 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 652 | // Explicitly braced initializer for complex type can be real+imaginary |
| 653 | // parts. |
| 654 | CheckComplexType(Entity, IList, DeclType, Index, |
| 655 | StructuredList, StructuredIndex); |
| 656 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 657 | CheckScalarType(Entity, IList, DeclType, Index, |
| 658 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 659 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 661 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 662 | } else if (DeclType->isAggregateType()) { |
| 663 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 664 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 665 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 666 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 667 | StructuredList, StructuredIndex, |
| 668 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 669 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 670 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 671 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 672 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 673 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 674 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 676 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 677 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 678 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 679 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 680 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 681 | if (!VerifyOnly) |
| 682 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 683 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 684 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 685 | } else if (DeclType->isRecordType()) { |
| 686 | // C++ [dcl.init]p14: |
| 687 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 688 | // is a brace-enclosed list, see 8.5.1. |
| 689 | // |
| 690 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 691 | // we have an initializer list and a destination type that is not |
| 692 | // an aggregate. |
| 693 | // FIXME: In C++0x, this is yet another form of initialization. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 694 | if (!VerifyOnly) |
| 695 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 696 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 697 | hadError = true; |
| 698 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 699 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 700 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 701 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 702 | if (!VerifyOnly) |
| 703 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 704 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 705 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 706 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 707 | if (!VerifyOnly) |
| 708 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 709 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 710 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 714 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 715 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 717 | unsigned &Index, |
| 718 | InitListExpr *StructuredList, |
| 719 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 720 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 721 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 722 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 723 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 725 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 726 | StructuredList, StructuredIndex, |
| 727 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 728 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 729 | newStructuredList, newStructuredIndex); |
| 730 | ++StructuredIndex; |
| 731 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 732 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 733 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 734 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 735 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 736 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 737 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 738 | StructuredList, StructuredIndex); |
| 739 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 740 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 741 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 742 | // arrayType can be incomplete if we're initializing a flexible |
| 743 | // array member. There's nothing we can do with the completed |
| 744 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 745 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 746 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 747 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 748 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 749 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 750 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 751 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 752 | |
| 753 | // Fall through for subaggregate initialization. |
| 754 | |
| 755 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 756 | // C++ [dcl.init.aggr]p12: |
| 757 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 758 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 759 | // an initializer-list. If the initializer can initialize a |
| 760 | // member, the member is initialized. [...] |
| 761 | |
| 762 | // FIXME: Better EqualLoc? |
| 763 | InitializationKind Kind = |
| 764 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 765 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 766 | |
| 767 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 768 | if (!VerifyOnly) { |
| 769 | ExprResult Result = |
| 770 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 771 | if (Result.isInvalid()) |
| 772 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 773 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 774 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 775 | Result.takeAs<Expr>()); |
| 776 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 777 | ++Index; |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | // Fall through for subaggregate initialization |
| 782 | } else { |
| 783 | // C99 6.7.8p13: |
| 784 | // |
| 785 | // The initializer for a structure or union object that has |
| 786 | // automatic storage duration shall be either an initializer |
| 787 | // list as described below, or a single expression that has |
| 788 | // compatible structure or union type. In the latter case, the |
| 789 | // initial value of the object, including unnamed members, is |
| 790 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 791 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 792 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 793 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 794 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 795 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 796 | if (ExprRes.isInvalid()) |
| 797 | hadError = true; |
| 798 | else { |
| 799 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 800 | if (ExprRes.isInvalid()) |
| 801 | hadError = true; |
| 802 | } |
| 803 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 804 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 805 | ++Index; |
| 806 | return; |
| 807 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 808 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 809 | // Fall through for subaggregate initialization |
| 810 | } |
| 811 | |
| 812 | // C++ [dcl.init.aggr]p12: |
| 813 | // |
| 814 | // [...] Otherwise, if the member is itself a non-empty |
| 815 | // subaggregate, brace elision is assumed and the initializer is |
| 816 | // considered for the initialization of the first member of |
| 817 | // the subaggregate. |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 818 | if (!SemaRef.getLangOptions().OpenCL && |
| 819 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 820 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 821 | StructuredIndex); |
| 822 | ++StructuredIndex; |
| 823 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 824 | if (!VerifyOnly) { |
| 825 | // We cannot initialize this element, so let |
| 826 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 827 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 828 | SemaRef.Owned(expr), |
| 829 | /*TopLevelOfInitList=*/true); |
| 830 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 831 | hadError = true; |
| 832 | ++Index; |
| 833 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 834 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 837 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 838 | InitListExpr *IList, QualType DeclType, |
| 839 | unsigned &Index, |
| 840 | InitListExpr *StructuredList, |
| 841 | unsigned &StructuredIndex) { |
| 842 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 843 | |
| 844 | // As an extension, clang supports complex initializers, which initialize |
| 845 | // a complex number component-wise. When an explicit initializer list for |
| 846 | // a complex number contains two two initializers, this extension kicks in: |
| 847 | // it exepcts the initializer list to contain two elements convertible to |
| 848 | // the element type of the complex type. The first element initializes |
| 849 | // the real part, and the second element intitializes the imaginary part. |
| 850 | |
| 851 | if (IList->getNumInits() != 2) |
| 852 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 853 | StructuredIndex); |
| 854 | |
| 855 | // This is an extension in C. (The builtin _Complex type does not exist |
| 856 | // in the C++ standard.) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 857 | if (!SemaRef.getLangOptions().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 858 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 859 | << IList->getSourceRange(); |
| 860 | |
| 861 | // Initialize the complex number. |
| 862 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 863 | InitializedEntity ElementEntity = |
| 864 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 865 | |
| 866 | for (unsigned i = 0; i < 2; ++i) { |
| 867 | ElementEntity.setElementIndex(Index); |
| 868 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 869 | StructuredList, StructuredIndex); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 874 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 875 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 876 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 877 | InitListExpr *StructuredList, |
| 878 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 879 | if (Index >= IList->getNumInits()) { |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 880 | if (!SemaRef.getLangOptions().CPlusPlus0x) { |
| 881 | if (!VerifyOnly) |
| 882 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 883 | << IList->getSourceRange(); |
| 884 | hadError = true; |
| 885 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 886 | ++Index; |
| 887 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 888 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 889 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 890 | |
| 891 | Expr *expr = IList->getInit(Index); |
| 892 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 893 | if (!VerifyOnly) |
| 894 | SemaRef.Diag(SubIList->getLocStart(), |
| 895 | diag::warn_many_braces_around_scalar_init) |
| 896 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 897 | |
| 898 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 899 | StructuredIndex); |
| 900 | return; |
| 901 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 902 | if (!VerifyOnly) |
| 903 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 904 | diag::err_designator_for_scalar_init) |
| 905 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 906 | hadError = true; |
| 907 | ++Index; |
| 908 | ++StructuredIndex; |
| 909 | return; |
| 910 | } |
| 911 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 912 | if (VerifyOnly) { |
| 913 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 914 | hadError = true; |
| 915 | ++Index; |
| 916 | return; |
| 917 | } |
| 918 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 919 | ExprResult Result = |
| 920 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 921 | SemaRef.Owned(expr), |
| 922 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 923 | |
| 924 | Expr *ResultExpr = 0; |
| 925 | |
| 926 | if (Result.isInvalid()) |
| 927 | hadError = true; // types weren't compatible. |
| 928 | else { |
| 929 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 930 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 931 | if (ResultExpr != expr) { |
| 932 | // The type was promoted, update initializer list. |
| 933 | IList->setInit(Index, ResultExpr); |
| 934 | } |
| 935 | } |
| 936 | if (hadError) |
| 937 | ++StructuredIndex; |
| 938 | else |
| 939 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 940 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 943 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 944 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 945 | unsigned &Index, |
| 946 | InitListExpr *StructuredList, |
| 947 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 948 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 949 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 950 | // general, it would be useful to pass location information down the stack, |
| 951 | // so that we know the location (or decl) of the "current object" being |
| 952 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 953 | if (!VerifyOnly) |
| 954 | SemaRef.Diag(IList->getLocStart(), |
| 955 | diag::err_init_reference_member_uninitialized) |
| 956 | << DeclType |
| 957 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 958 | hadError = true; |
| 959 | ++Index; |
| 960 | ++StructuredIndex; |
| 961 | return; |
| 962 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 963 | |
| 964 | Expr *expr = IList->getInit(Index); |
| 965 | if (isa<InitListExpr>(expr)) { |
| 966 | // FIXME: Allowed in C++11. |
| 967 | if (!VerifyOnly) |
| 968 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 969 | << DeclType << IList->getSourceRange(); |
| 970 | hadError = true; |
| 971 | ++Index; |
| 972 | ++StructuredIndex; |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (VerifyOnly) { |
| 977 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 978 | hadError = true; |
| 979 | ++Index; |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | ExprResult Result = |
| 984 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 985 | SemaRef.Owned(expr), |
| 986 | /*TopLevelOfInitList=*/true); |
| 987 | |
| 988 | if (Result.isInvalid()) |
| 989 | hadError = true; |
| 990 | |
| 991 | expr = Result.takeAs<Expr>(); |
| 992 | IList->setInit(Index, expr); |
| 993 | |
| 994 | if (hadError) |
| 995 | ++StructuredIndex; |
| 996 | else |
| 997 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 998 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1001 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1002 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1003 | unsigned &Index, |
| 1004 | InitListExpr *StructuredList, |
| 1005 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1006 | if (Index >= IList->getNumInits()) |
| 1007 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1009 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1010 | unsigned maxElements = VT->getNumElements(); |
| 1011 | unsigned numEltsInit = 0; |
| 1012 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1013 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1014 | if (!SemaRef.getLangOptions().OpenCL) { |
| 1015 | // If the initializing element is a vector, try to copy-initialize |
| 1016 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1017 | Expr *Init = IList->getInit(Index); |
| 1018 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1019 | if (VerifyOnly) { |
| 1020 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1021 | hadError = true; |
| 1022 | ++Index; |
| 1023 | return; |
| 1024 | } |
| 1025 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1026 | ExprResult Result = |
| 1027 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1028 | SemaRef.Owned(Init), |
| 1029 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1030 | |
| 1031 | Expr *ResultExpr = 0; |
| 1032 | if (Result.isInvalid()) |
| 1033 | hadError = true; // types weren't compatible. |
| 1034 | else { |
| 1035 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1037 | if (ResultExpr != Init) { |
| 1038 | // The type was promoted, update initializer list. |
| 1039 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1042 | if (hadError) |
| 1043 | ++StructuredIndex; |
| 1044 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1045 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1046 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1047 | ++Index; |
| 1048 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1051 | InitializedEntity ElementEntity = |
| 1052 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1054 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1055 | // Don't attempt to go past the end of the init list |
| 1056 | if (Index >= IList->getNumInits()) |
| 1057 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1059 | ElementEntity.setElementIndex(Index); |
| 1060 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1061 | StructuredList, StructuredIndex); |
| 1062 | } |
| 1063 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1064 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1065 | |
| 1066 | InitializedEntity ElementEntity = |
| 1067 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1069 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1070 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1071 | // Don't attempt to go past the end of the init list |
| 1072 | if (Index >= IList->getNumInits()) |
| 1073 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1075 | ElementEntity.setElementIndex(Index); |
| 1076 | |
| 1077 | QualType IType = IList->getInit(Index)->getType(); |
| 1078 | if (!IType->isVectorType()) { |
| 1079 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1080 | StructuredList, StructuredIndex); |
| 1081 | ++numEltsInit; |
| 1082 | } else { |
| 1083 | QualType VecType; |
| 1084 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1085 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1087 | if (IType->isExtVectorType()) |
| 1088 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1089 | else |
| 1090 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1091 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1092 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1093 | StructuredList, StructuredIndex); |
| 1094 | numEltsInit += numIElts; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1099 | // FIXME: Shouldn't this set hadError to true then? |
| 1100 | if (numEltsInit != maxElements && !VerifyOnly) |
| 1101 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1102 | diag::err_vector_incorrect_num_initializers) |
| 1103 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1106 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1107 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1108 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1110 | unsigned &Index, |
| 1111 | InitListExpr *StructuredList, |
| 1112 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1113 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1114 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1115 | // Check for the special-case of initializing an array with a string. |
| 1116 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1117 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1118 | SemaRef.Context)) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1119 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1120 | // We place the string literal directly into the resulting |
| 1121 | // initializer list. This is the only place where the structure |
| 1122 | // of the structured initializer list doesn't match exactly, |
| 1123 | // because doing so would involve allocating one character |
| 1124 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1125 | if (!VerifyOnly) { |
| 1126 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1127 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1128 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1129 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1133 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1134 | // Check for VLAs; in standard C it would be possible to check this |
| 1135 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1136 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1137 | if (!VerifyOnly) |
| 1138 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1139 | diag::err_variable_object_no_init) |
| 1140 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1141 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1142 | ++Index; |
| 1143 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1147 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1148 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1149 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1150 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1151 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1152 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1153 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1154 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1155 | maxElementsKnown = true; |
| 1156 | } |
| 1157 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1158 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1159 | while (Index < IList->getNumInits()) { |
| 1160 | Expr *Init = IList->getInit(Index); |
| 1161 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1162 | // If we're not the subobject that matches up with the '{' for |
| 1163 | // the designator, we shouldn't be handling the |
| 1164 | // designator. Return immediately. |
| 1165 | if (!SubobjectIsDesignatorContext) |
| 1166 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1168 | // Handle this designated initializer. elementIndex will be |
| 1169 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1170 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1171 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1172 | StructuredList, StructuredIndex, true, |
| 1173 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1174 | hadError = true; |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1178 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1179 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1180 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1181 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1182 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1183 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1184 | // If the array is of incomplete type, keep track of the number of |
| 1185 | // elements in the initializer. |
| 1186 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1187 | maxElements = elementIndex; |
| 1188 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | // If we know the maximum number of elements, and we've already |
| 1193 | // hit it, stop consuming elements in the initializer list. |
| 1194 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1195 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1196 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1197 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1198 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1199 | Entity); |
| 1200 | // Check this element. |
| 1201 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1202 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1203 | ++elementIndex; |
| 1204 | |
| 1205 | // If the array is of incomplete type, keep track of the number of |
| 1206 | // elements in the initializer. |
| 1207 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1208 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1209 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1210 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1211 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1212 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1213 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1214 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1215 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1216 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1217 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1218 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1219 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1222 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1226 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1227 | Expr *InitExpr, |
| 1228 | FieldDecl *Field, |
| 1229 | bool TopLevelObject) { |
| 1230 | // Handle GNU flexible array initializers. |
| 1231 | unsigned FlexArrayDiag; |
| 1232 | if (isa<InitListExpr>(InitExpr) && |
| 1233 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1234 | // Empty flexible array init always allowed as an extension |
| 1235 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1236 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1237 | // Disallow flexible array init in C++; it is not required for gcc |
| 1238 | // compatibility, and it needs work to IRGen correctly in general. |
| 1239 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1240 | } else if (!TopLevelObject) { |
| 1241 | // Disallow flexible array init on non-top-level object |
| 1242 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1243 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1244 | // Disallow flexible array init on anything which is not a variable. |
| 1245 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1246 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1247 | // Disallow flexible array init on local variables. |
| 1248 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1249 | } else { |
| 1250 | // Allow other cases. |
| 1251 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1252 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (!VerifyOnly) { |
| 1255 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1256 | FlexArrayDiag) |
| 1257 | << InitExpr->getSourceRange().getBegin(); |
| 1258 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1259 | << Field; |
| 1260 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1261 | |
| 1262 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1263 | } |
| 1264 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1265 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1266 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1268 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1270 | unsigned &Index, |
| 1271 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1272 | unsigned &StructuredIndex, |
| 1273 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1274 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1276 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1277 | // confusion, we forgo checking the intializer for the entire record. |
| 1278 | if (structDecl->isInvalidDecl()) { |
| 1279 | hadError = true; |
| 1280 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1282 | |
| 1283 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1284 | if (!VerifyOnly) { |
| 1285 | // Value-initialize the first named member of the union. |
| 1286 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1287 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1288 | Field != FieldEnd; ++Field) { |
| 1289 | if (Field->getDeclName()) { |
| 1290 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1291 | break; |
| 1292 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | return; |
| 1296 | } |
| 1297 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1298 | // If structDecl is a forward declaration, this loop won't do |
| 1299 | // anything except look at designated initializers; That's okay, |
| 1300 | // because an error should get printed out elsewhere. It might be |
| 1301 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1302 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1303 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1304 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1305 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1306 | while (Index < IList->getNumInits()) { |
| 1307 | Expr *Init = IList->getInit(Index); |
| 1308 | |
| 1309 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1310 | // If we're not the subobject that matches up with the '{' for |
| 1311 | // the designator, we shouldn't be handling the |
| 1312 | // designator. Return immediately. |
| 1313 | if (!SubobjectIsDesignatorContext) |
| 1314 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1316 | // Handle this designated initializer. Field will be updated to |
| 1317 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1318 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1319 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1320 | StructuredList, StructuredIndex, |
| 1321 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1322 | hadError = true; |
| 1323 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1324 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Disable check for missing fields when designators are used. |
| 1327 | // This matches gcc behaviour. |
| 1328 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1329 | continue; |
| 1330 | } |
| 1331 | |
| 1332 | if (Field == FieldEnd) { |
| 1333 | // We've run out of fields. We're done. |
| 1334 | break; |
| 1335 | } |
| 1336 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1337 | // We've already initialized a member of a union. We're done. |
| 1338 | if (InitializedSomething && DeclType->isUnionType()) |
| 1339 | break; |
| 1340 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1341 | // If we've hit the flexible array member at the end, we're done. |
| 1342 | if (Field->getType()->isIncompleteArrayType()) |
| 1343 | break; |
| 1344 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1345 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1346 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1347 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1348 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1349 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1351 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1352 | bool InvalidUse; |
| 1353 | if (VerifyOnly) |
| 1354 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1355 | else |
| 1356 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
| 1357 | IList->getInit(Index)->getLocStart()); |
| 1358 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1359 | ++Index; |
| 1360 | ++Field; |
| 1361 | hadError = true; |
| 1362 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1363 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1364 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1365 | InitializedEntity MemberEntity = |
| 1366 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1367 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1368 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1369 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1370 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1371 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1372 | // Initialize the first field within the union. |
| 1373 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1374 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1375 | |
| 1376 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1377 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1378 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1379 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1380 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1381 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1382 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1383 | // It is possible we have one or more unnamed bitfields remaining. |
| 1384 | // Find first (if any) named field and emit warning. |
| 1385 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1386 | it != end; ++it) { |
| 1387 | if (!it->isUnnamedBitfield()) { |
| 1388 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1389 | diag::warn_missing_field_initializers) << it->getName(); |
| 1390 | break; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1396 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1397 | return; |
| 1398 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1399 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1400 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1401 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1402 | ++Index; |
| 1403 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1406 | InitializedEntity MemberEntity = |
| 1407 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1409 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1411 | StructuredList, StructuredIndex); |
| 1412 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1413 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1414 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1415 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1417 | /// \brief Expand a field designator that refers to a member of an |
| 1418 | /// anonymous struct or union into a series of field designators that |
| 1419 | /// refers to the field within the appropriate subobject. |
| 1420 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1421 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | DesignatedInitExpr *DIE, |
| 1423 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1424 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1425 | typedef DesignatedInitExpr::Designator Designator; |
| 1426 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1427 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1428 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1429 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1430 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1431 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1433 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1434 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1435 | else |
| 1436 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1437 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1438 | assert(isa<FieldDecl>(*PI)); |
| 1439 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | // Expand the current designator into the set of replacement |
| 1443 | // designators, so we have a full subobject path down to where the |
| 1444 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1445 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1446 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1447 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1450 | /// corresponds to FieldName. |
| 1451 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1452 | IdentifierInfo *FieldName) { |
| 1453 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1454 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1455 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1456 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1457 | return IF; |
| 1458 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1459 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1460 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1463 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1464 | DesignatedInitExpr *DIE) { |
| 1465 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1466 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1467 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1468 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1469 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1470 | DIE->size(), IndexExprs.data(), |
| 1471 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1472 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1473 | } |
| 1474 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1475 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1476 | /// |
| 1477 | /// Determines whether the designated initializer @p DIE, which |
| 1478 | /// resides at the given @p Index within the initializer list @p |
| 1479 | /// IList, is well-formed for a current object of type @p DeclType |
| 1480 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1482 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1483 | /// |
| 1484 | /// @param IList The initializer list in which this designated |
| 1485 | /// initializer occurs. |
| 1486 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1487 | /// @param DIE The designated initializer expression. |
| 1488 | /// |
| 1489 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1490 | /// |
| 1491 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1492 | /// into which the designation in @p DIE should refer. |
| 1493 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1494 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1495 | /// a field, this will be set to the field declaration corresponding |
| 1496 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1497 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1498 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1499 | /// DIE is an array designator or GNU array-range designator, this |
| 1500 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1501 | /// |
| 1502 | /// @param Index Index into @p IList where the designated initializer |
| 1503 | /// @p DIE occurs. |
| 1504 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1505 | /// @param StructuredList The initializer list expression that |
| 1506 | /// describes all of the subobject initializers in the order they'll |
| 1507 | /// actually be initialized. |
| 1508 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1509 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1511 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1512 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1513 | DesignatedInitExpr *DIE, |
| 1514 | unsigned DesigIdx, |
| 1515 | QualType &CurrentObjectType, |
| 1516 | RecordDecl::field_iterator *NextField, |
| 1517 | llvm::APSInt *NextElementIndex, |
| 1518 | unsigned &Index, |
| 1519 | InitListExpr *StructuredList, |
| 1520 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1521 | bool FinishSubobjectInit, |
| 1522 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1523 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1524 | // Check the actual initialization for the designated object type. |
| 1525 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Temporarily remove the designator expression from the |
| 1528 | // initializer list that the child calls see, so that we don't try |
| 1529 | // to re-process the designator. |
| 1530 | unsigned OldIndex = Index; |
| 1531 | IList->setInit(OldIndex, DIE->getInit()); |
| 1532 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1533 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1534 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Restore the designated initializer expression in the syntactic |
| 1537 | // form of the initializer list. |
| 1538 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1539 | DIE->setInit(IList->getInit(OldIndex)); |
| 1540 | IList->setInit(OldIndex, DIE); |
| 1541 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1542 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1545 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1546 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1547 | if (!VerifyOnly) { |
| 1548 | assert((IsFirstDesignator || StructuredList) && |
| 1549 | "Need a non-designated initializer list to start from"); |
| 1550 | |
| 1551 | // Determine the structural initializer list that corresponds to the |
| 1552 | // current subobject. |
| 1553 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 1554 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1555 | StructuredList, StructuredIndex, |
| 1556 | SourceRange(D->getStartLocation(), |
| 1557 | DIE->getSourceRange().getEnd())); |
| 1558 | assert(StructuredList && "Expected a structured initializer list"); |
| 1559 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1561 | if (D->isFieldDesignator()) { |
| 1562 | // C99 6.7.8p7: |
| 1563 | // |
| 1564 | // If a designator has the form |
| 1565 | // |
| 1566 | // . identifier |
| 1567 | // |
| 1568 | // then the current object (defined below) shall have |
| 1569 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1571 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1572 | if (!RT) { |
| 1573 | SourceLocation Loc = D->getDotLoc(); |
| 1574 | if (Loc.isInvalid()) |
| 1575 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1576 | if (!VerifyOnly) |
| 1577 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1578 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1579 | ++Index; |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1583 | // Note: we perform a linear search of the fields here, despite |
| 1584 | // the fact that we have a faster lookup method, because we always |
| 1585 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1586 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1587 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1588 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1590 | Field = RT->getDecl()->field_begin(), |
| 1591 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1592 | for (; Field != FieldEnd; ++Field) { |
| 1593 | if (Field->isUnnamedBitfield()) |
| 1594 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1595 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1596 | // If we find a field representing an anonymous field, look in the |
| 1597 | // IndirectFieldDecl that follow for the designated initializer. |
| 1598 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1599 | if (IndirectFieldDecl *IF = |
| 1600 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1601 | // In verify mode, don't modify the original. |
| 1602 | if (VerifyOnly) |
| 1603 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1604 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1605 | D = DIE->getDesignator(DesigIdx); |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1609 | if (KnownField && KnownField == *Field) |
| 1610 | break; |
| 1611 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | |
| 1614 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1617 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame^] | 1618 | if (VerifyOnly) { |
| 1619 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1620 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame^] | 1621 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1623 | // There was no normal field in the struct with the designated |
| 1624 | // name. Perform another lookup for this name, which may find |
| 1625 | // something that we can't designate (e.g., a member function), |
| 1626 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1628 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1629 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1630 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1631 | // Name lookup didn't find anything. Determine whether this |
| 1632 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1633 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1634 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1635 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1636 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1637 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1638 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1639 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1640 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1641 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1642 | std::string CorrectedStr( |
| 1643 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1644 | std::string CorrectedQuotedStr( |
| 1645 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1646 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1647 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1648 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1649 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1650 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1651 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame^] | 1652 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1653 | } else { |
| 1654 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1655 | << FieldName << CurrentObjectType; |
| 1656 | ++Index; |
| 1657 | return true; |
| 1658 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1661 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1662 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1663 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1666 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1667 | ++Index; |
| 1668 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1669 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1670 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1671 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1672 | // The replacement field comes from typo correction; find it |
| 1673 | // in the list of fields. |
| 1674 | FieldIndex = 0; |
| 1675 | Field = RT->getDecl()->field_begin(); |
| 1676 | for (; Field != FieldEnd; ++Field) { |
| 1677 | if (Field->isUnnamedBitfield()) |
| 1678 | continue; |
| 1679 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1680 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1681 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1682 | break; |
| 1683 | |
| 1684 | ++FieldIndex; |
| 1685 | } |
| 1686 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1687 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1688 | |
| 1689 | // All of the fields of a union are located at the same place in |
| 1690 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1691 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1692 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1693 | if (!VerifyOnly) |
| 1694 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1695 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1697 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1698 | bool InvalidUse; |
| 1699 | if (VerifyOnly) |
| 1700 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1701 | else |
| 1702 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 1703 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1704 | ++Index; |
| 1705 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1706 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1707 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1708 | if (!VerifyOnly) { |
| 1709 | // Update the designator with the field declaration. |
| 1710 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1712 | // Make sure that our non-designated initializer list has space |
| 1713 | // for a subobject corresponding to this field. |
| 1714 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1715 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1716 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1718 | // This designator names a flexible array member. |
| 1719 | if (Field->getType()->isIncompleteArrayType()) { |
| 1720 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1721 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1722 | // We can't designate an object within the flexible array |
| 1723 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1724 | if (!VerifyOnly) { |
| 1725 | DesignatedInitExpr::Designator *NextD |
| 1726 | = DIE->getDesignator(DesigIdx + 1); |
| 1727 | SemaRef.Diag(NextD->getStartLocation(), |
| 1728 | diag::err_designator_into_flexible_array_member) |
| 1729 | << SourceRange(NextD->getStartLocation(), |
| 1730 | DIE->getSourceRange().getEnd()); |
| 1731 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1732 | << *Field; |
| 1733 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1734 | Invalid = true; |
| 1735 | } |
| 1736 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1737 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1738 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1739 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1740 | if (!VerifyOnly) { |
| 1741 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
| 1742 | diag::err_flexible_array_init_needs_braces) |
| 1743 | << DIE->getInit()->getSourceRange(); |
| 1744 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1745 | << *Field; |
| 1746 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1747 | Invalid = true; |
| 1748 | } |
| 1749 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1750 | // Check GNU flexible array initializer. |
| 1751 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1752 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1753 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1754 | |
| 1755 | if (Invalid) { |
| 1756 | ++Index; |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
| 1760 | // Initialize the array. |
| 1761 | bool prevHadError = hadError; |
| 1762 | unsigned newStructuredIndex = FieldIndex; |
| 1763 | unsigned OldIndex = Index; |
| 1764 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1765 | |
| 1766 | InitializedEntity MemberEntity = |
| 1767 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1768 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1769 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1771 | IList->setInit(OldIndex, DIE); |
| 1772 | if (hadError && !prevHadError) { |
| 1773 | ++Field; |
| 1774 | ++FieldIndex; |
| 1775 | if (NextField) |
| 1776 | *NextField = Field; |
| 1777 | StructuredIndex = FieldIndex; |
| 1778 | return true; |
| 1779 | } |
| 1780 | } else { |
| 1781 | // Recurse to check later designated subobjects. |
| 1782 | QualType FieldType = (*Field)->getType(); |
| 1783 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1785 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1786 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1787 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1788 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1789 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1790 | true, false)) |
| 1791 | return true; |
| 1792 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Find the position of the next field to be initialized in this |
| 1795 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1796 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1797 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1798 | |
| 1799 | // If this the first designator, our caller will continue checking |
| 1800 | // the rest of this struct/class/union subobject. |
| 1801 | if (IsFirstDesignator) { |
| 1802 | if (NextField) |
| 1803 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1804 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1805 | return false; |
| 1806 | } |
| 1807 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1808 | if (!FinishSubobjectInit) |
| 1809 | return false; |
| 1810 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1811 | // We've already initialized something in the union; we're done. |
| 1812 | if (RT->getDecl()->isUnion()) |
| 1813 | return hadError; |
| 1814 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1815 | // Check the remaining fields within this class/struct/union subobject. |
| 1816 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1817 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1818 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1819 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1820 | return hadError && !prevHadError; |
| 1821 | } |
| 1822 | |
| 1823 | // C99 6.7.8p6: |
| 1824 | // |
| 1825 | // If a designator has the form |
| 1826 | // |
| 1827 | // [ constant-expression ] |
| 1828 | // |
| 1829 | // then the current object (defined below) shall have array |
| 1830 | // type and the expression shall be an integer constant |
| 1831 | // expression. If the array is of unknown size, any |
| 1832 | // nonnegative value is valid. |
| 1833 | // |
| 1834 | // Additionally, cope with the GNU extension that permits |
| 1835 | // designators of the form |
| 1836 | // |
| 1837 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1838 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1839 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1840 | if (!VerifyOnly) |
| 1841 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1842 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1843 | ++Index; |
| 1844 | return true; |
| 1845 | } |
| 1846 | |
| 1847 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1848 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1849 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1850 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1851 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1852 | DesignatedEndIndex = DesignatedStartIndex; |
| 1853 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1854 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1855 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1857 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1859 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1860 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1862 | // Codegen can't handle evaluating array range designators that have side |
| 1863 | // effects, because we replicate the AST value for each initialized element. |
| 1864 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1865 | // elements with something that has a side effect, so codegen can emit an |
| 1866 | // "error unsupported" error instead of miscompiling the app. |
| 1867 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1868 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1869 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1872 | if (isa<ConstantArrayType>(AT)) { |
| 1873 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1874 | DesignatedStartIndex |
| 1875 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1876 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1877 | DesignatedEndIndex |
| 1878 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1879 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1880 | if (DesignatedEndIndex >= MaxElements) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1881 | if (VerifyOnly) |
| 1882 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
| 1883 | diag::err_array_designator_too_large) |
| 1884 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1885 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1886 | ++Index; |
| 1887 | return true; |
| 1888 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1889 | } else { |
| 1890 | // Make sure the bit-widths and signedness match. |
| 1891 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1892 | DesignatedEndIndex |
| 1893 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1894 | else if (DesignatedStartIndex.getBitWidth() < |
| 1895 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1896 | DesignatedStartIndex |
| 1897 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1898 | DesignatedStartIndex.setIsUnsigned(true); |
| 1899 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1902 | // Make sure that our non-designated initializer list has space |
| 1903 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1904 | if (!VerifyOnly && |
| 1905 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1907 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1909 | // Repeatedly perform subobject initializations in the range |
| 1910 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1912 | // Move to the next designator |
| 1913 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1914 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1915 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1916 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1917 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1919 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1920 | // Recurse to check later designated subobjects. |
| 1921 | QualType ElementType = AT->getElementType(); |
| 1922 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1923 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1924 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1925 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1926 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1927 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1928 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1929 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1930 | return true; |
| 1931 | |
| 1932 | // Move to the next index in the array that we'll be initializing. |
| 1933 | ++DesignatedStartIndex; |
| 1934 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1935 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1936 | |
| 1937 | // If this the first designator, our caller will continue checking |
| 1938 | // the rest of this array subobject. |
| 1939 | if (IsFirstDesignator) { |
| 1940 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1941 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1942 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1943 | return false; |
| 1944 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1946 | if (!FinishSubobjectInit) |
| 1947 | return false; |
| 1948 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1949 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1950 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1952 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1953 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1957 | // Get the structured initializer list for a subobject of type |
| 1958 | // @p CurrentObjectType. |
| 1959 | InitListExpr * |
| 1960 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1961 | QualType CurrentObjectType, |
| 1962 | InitListExpr *StructuredList, |
| 1963 | unsigned StructuredIndex, |
| 1964 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1965 | if (VerifyOnly) |
| 1966 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1967 | Expr *ExistingInit = 0; |
| 1968 | if (!StructuredList) |
| 1969 | ExistingInit = SyntacticToSemantic[IList]; |
| 1970 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1971 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1973 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1974 | return Result; |
| 1975 | |
| 1976 | if (ExistingInit) { |
| 1977 | // We are creating an initializer list that initializes the |
| 1978 | // subobjects of the current object, but there was already an |
| 1979 | // initialization that completely initialized the current |
| 1980 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | // struct X { int a, b; }; |
| 1983 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1985 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1986 | // designated initializer re-initializes the whole |
| 1987 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1989 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1990 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1992 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1993 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1994 | << ExistingInit->getSourceRange(); |
| 1995 | } |
| 1996 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1998 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1999 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2000 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2002 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2004 | // Pre-allocate storage for the structured initializer list. |
| 2005 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2006 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2007 | bool GotNumInits = false; |
| 2008 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2009 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2010 | GotNumInits = true; |
| 2011 | } else if (Index < IList->getNumInits()) { |
| 2012 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2013 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2014 | GotNumInits = true; |
| 2015 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2019 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2020 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2021 | NumElements = CAType->getSize().getZExtValue(); |
| 2022 | // Simple heuristic so that we don't allocate a very large |
| 2023 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2024 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2025 | NumElements = 0; |
| 2026 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2027 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2028 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2029 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2030 | RecordDecl *RDecl = RType->getDecl(); |
| 2031 | if (RDecl->isUnion()) |
| 2032 | NumElements = 1; |
| 2033 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2035 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2038 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2039 | NumElements = IList->getNumInits(); |
| 2040 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2041 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2043 | // Link this new initializer list into the structured initializer |
| 2044 | // lists. |
| 2045 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2046 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2047 | else { |
| 2048 | Result->setSyntacticForm(IList); |
| 2049 | SyntacticToSemantic[IList] = Result; |
| 2050 | } |
| 2051 | |
| 2052 | return Result; |
| 2053 | } |
| 2054 | |
| 2055 | /// Update the initializer at index @p StructuredIndex within the |
| 2056 | /// structured initializer list to the value @p expr. |
| 2057 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2058 | unsigned &StructuredIndex, |
| 2059 | Expr *expr) { |
| 2060 | // No structured initializer list to update |
| 2061 | if (!StructuredList) |
| 2062 | return; |
| 2063 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2064 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2065 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2066 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2068 | diag::warn_initializer_overrides) |
| 2069 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2071 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2072 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2073 | << PrevInit->getSourceRange(); |
| 2074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2076 | ++StructuredIndex; |
| 2077 | } |
| 2078 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2079 | /// Check that the given Index expression is a valid array designator |
| 2080 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2081 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2082 | /// and produces a reasonable diagnostic if there is a |
| 2083 | /// failure. Returns true if there was an error, false otherwise. If |
| 2084 | /// everything went okay, Value will receive the value of the constant |
| 2085 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2087 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2088 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 2089 | |
| 2090 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2091 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 2092 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2094 | if (Value.isSigned() && Value.isNegative()) |
| 2095 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2096 | << Value.toString(10) << Index->getSourceRange(); |
| 2097 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2098 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2099 | return false; |
| 2100 | } |
| 2101 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2102 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2103 | SourceLocation Loc, |
| 2104 | bool GNUSyntax, |
| 2105 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2106 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2107 | |
| 2108 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2109 | SmallVector<ASTDesignator, 32> Designators; |
| 2110 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2111 | |
| 2112 | // Build designators and check array designator expressions. |
| 2113 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2114 | const Designator &D = Desig.getDesignator(Idx); |
| 2115 | switch (D.getKind()) { |
| 2116 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2118 | D.getFieldLoc())); |
| 2119 | break; |
| 2120 | |
| 2121 | case Designator::ArrayDesignator: { |
| 2122 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2123 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2124 | if (!Index->isTypeDependent() && |
| 2125 | !Index->isValueDependent() && |
| 2126 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2127 | Invalid = true; |
| 2128 | else { |
| 2129 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2131 | D.getRBracketLoc())); |
| 2132 | InitExpressions.push_back(Index); |
| 2133 | } |
| 2134 | break; |
| 2135 | } |
| 2136 | |
| 2137 | case Designator::ArrayRangeDesignator: { |
| 2138 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2139 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2140 | llvm::APSInt StartValue; |
| 2141 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2142 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2143 | StartIndex->isValueDependent(); |
| 2144 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2145 | EndIndex->isValueDependent(); |
| 2146 | if ((!StartDependent && |
| 2147 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2148 | (!EndDependent && |
| 2149 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2150 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2151 | else { |
| 2152 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2153 | if (StartDependent || EndDependent) { |
| 2154 | // Nothing to compute. |
| 2155 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2156 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2157 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2158 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2160 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2161 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2163 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2164 | Invalid = true; |
| 2165 | } else { |
| 2166 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2168 | D.getEllipsisLoc(), |
| 2169 | D.getRBracketLoc())); |
| 2170 | InitExpressions.push_back(StartIndex); |
| 2171 | InitExpressions.push_back(EndIndex); |
| 2172 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2173 | } |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | if (Invalid || Init.isInvalid()) |
| 2180 | return ExprError(); |
| 2181 | |
| 2182 | // Clear out the expressions within the designation. |
| 2183 | Desig.ClearExprs(*this); |
| 2184 | |
| 2185 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2186 | = DesignatedInitExpr::Create(Context, |
| 2187 | Designators.data(), Designators.size(), |
| 2188 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2189 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2191 | if (getLangOptions().CPlusPlus) |
Eli Friedman | a47317b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2192 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2193 | << DIE->getSourceRange(); |
| 2194 | else if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2195 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2196 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2198 | return Owned(DIE); |
| 2199 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2201 | //===----------------------------------------------------------------------===// |
| 2202 | // Initialization entity |
| 2203 | //===----------------------------------------------------------------------===// |
| 2204 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2205 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2206 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2207 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2208 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2209 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2210 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2211 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2212 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2213 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2214 | Type = VT->getElementType(); |
| 2215 | } else { |
| 2216 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2217 | assert(CT && "Unexpected type"); |
| 2218 | Kind = EK_ComplexElement; |
| 2219 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2220 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2224 | CXXBaseSpecifier *Base, |
| 2225 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2226 | { |
| 2227 | InitializedEntity Result; |
| 2228 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2229 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2230 | if (IsInheritedVirtualBase) |
| 2231 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2232 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2233 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2234 | return Result; |
| 2235 | } |
| 2236 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2237 | DeclarationName InitializedEntity::getName() const { |
| 2238 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2239 | case EK_Parameter: { |
| 2240 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2241 | return (D ? D->getDeclName() : DeclarationName()); |
| 2242 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2243 | |
| 2244 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2245 | case EK_Member: |
| 2246 | return VariableOrMember->getDeclName(); |
| 2247 | |
| 2248 | case EK_Result: |
| 2249 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2250 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2251 | case EK_Temporary: |
| 2252 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2253 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2254 | case EK_ArrayElement: |
| 2255 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2256 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2257 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2258 | return DeclarationName(); |
| 2259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2261 | // Silence GCC warning |
| 2262 | return DeclarationName(); |
| 2263 | } |
| 2264 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2265 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2266 | switch (getKind()) { |
| 2267 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2268 | case EK_Member: |
| 2269 | return VariableOrMember; |
| 2270 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2271 | case EK_Parameter: |
| 2272 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2273 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2274 | case EK_Result: |
| 2275 | case EK_Exception: |
| 2276 | case EK_New: |
| 2277 | case EK_Temporary: |
| 2278 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2279 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2280 | case EK_ArrayElement: |
| 2281 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2282 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2283 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2286 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2287 | // Silence GCC warning |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2291 | bool InitializedEntity::allowsNRVO() const { |
| 2292 | switch (getKind()) { |
| 2293 | case EK_Result: |
| 2294 | case EK_Exception: |
| 2295 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2297 | case EK_Variable: |
| 2298 | case EK_Parameter: |
| 2299 | case EK_Member: |
| 2300 | case EK_New: |
| 2301 | case EK_Temporary: |
| 2302 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2303 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2304 | case EK_ArrayElement: |
| 2305 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2306 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2307 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2308 | break; |
| 2309 | } |
| 2310 | |
| 2311 | return false; |
| 2312 | } |
| 2313 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2314 | //===----------------------------------------------------------------------===// |
| 2315 | // Initialization sequence |
| 2316 | //===----------------------------------------------------------------------===// |
| 2317 | |
| 2318 | void InitializationSequence::Step::Destroy() { |
| 2319 | switch (Kind) { |
| 2320 | case SK_ResolveAddressOfOverloadedFunction: |
| 2321 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2322 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2323 | case SK_CastDerivedToBaseLValue: |
| 2324 | case SK_BindReference: |
| 2325 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2326 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2327 | case SK_UserConversion: |
| 2328 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2329 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2330 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2331 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2332 | case SK_ListConstructorCall: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2333 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2334 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2335 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2336 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2337 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2338 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2339 | case SK_PassByIndirectCopyRestore: |
| 2340 | case SK_PassByIndirectRestore: |
| 2341 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2342 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2344 | case SK_ConversionSequence: |
| 2345 | delete ICS; |
| 2346 | } |
| 2347 | } |
| 2348 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2349 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2350 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2354 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2355 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2356 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2357 | switch (getFailureKind()) { |
| 2358 | case FK_TooManyInitsForReference: |
| 2359 | case FK_ArrayNeedsInitList: |
| 2360 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2361 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2362 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2363 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2364 | case FK_RValueReferenceBindingToLValue: |
| 2365 | case FK_ReferenceInitDropsQualifiers: |
| 2366 | case FK_ReferenceInitFailed: |
| 2367 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2368 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2369 | case FK_TooManyInitsForScalar: |
| 2370 | case FK_ReferenceBindingToInitList: |
| 2371 | case FK_InitListBadDestinationType: |
| 2372 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2373 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2374 | case FK_ArrayTypeMismatch: |
| 2375 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2376 | case FK_ListInitializationFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2377 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2378 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2379 | case FK_ReferenceInitOverloadFailed: |
| 2380 | case FK_UserConversionOverloadFailed: |
| 2381 | case FK_ConstructorOverloadFailed: |
| 2382 | return FailedOverloadResult == OR_Ambiguous; |
| 2383 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2384 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2385 | return false; |
| 2386 | } |
| 2387 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2388 | bool InitializationSequence::isConstructorInitialization() const { |
| 2389 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2390 | } |
| 2391 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2392 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2393 | const Expr *Initializer, |
| 2394 | bool *isInitializerConstant, |
| 2395 | APValue *ConstantValue) const { |
| 2396 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2397 | return false; |
| 2398 | |
| 2399 | const Step &LastStep = Steps.back(); |
| 2400 | if (LastStep.Kind != SK_ConversionSequence) |
| 2401 | return false; |
| 2402 | |
| 2403 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2404 | const StandardConversionSequence *SCS = NULL; |
| 2405 | switch (ICS.getKind()) { |
| 2406 | case ImplicitConversionSequence::StandardConversion: |
| 2407 | SCS = &ICS.Standard; |
| 2408 | break; |
| 2409 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2410 | SCS = &ICS.UserDefined.After; |
| 2411 | break; |
| 2412 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2413 | case ImplicitConversionSequence::EllipsisConversion: |
| 2414 | case ImplicitConversionSequence::BadConversion: |
| 2415 | return false; |
| 2416 | } |
| 2417 | |
| 2418 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2419 | // [dcl.init.list]p7: |
| 2420 | // |
| 2421 | // A narrowing conversion is an implicit conversion ... |
| 2422 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2423 | QualType FromType = SCS->getToType(0); |
| 2424 | QualType ToType = SCS->getToType(1); |
| 2425 | switch (PossibleNarrowing) { |
| 2426 | // * from a floating-point type to an integer type, or |
| 2427 | // |
| 2428 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2429 | // type, except where the source is a constant expression and the actual |
| 2430 | // value after conversion will fit into the target type and will produce |
| 2431 | // the original value when converted back to the original type, or |
| 2432 | case ICK_Floating_Integral: |
| 2433 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2434 | *isInitializerConstant = false; |
| 2435 | return true; |
| 2436 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2437 | llvm::APSInt IntConstantValue; |
| 2438 | if (Initializer && |
| 2439 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2440 | // Convert the integer to the floating type. |
| 2441 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2442 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2443 | llvm::APFloat::rmNearestTiesToEven); |
| 2444 | // And back. |
| 2445 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2446 | bool ignored; |
| 2447 | Result.convertToInteger(ConvertedValue, |
| 2448 | llvm::APFloat::rmTowardZero, &ignored); |
| 2449 | // If the resulting value is different, this was a narrowing conversion. |
| 2450 | if (IntConstantValue != ConvertedValue) { |
| 2451 | *isInitializerConstant = true; |
| 2452 | *ConstantValue = APValue(IntConstantValue); |
| 2453 | return true; |
| 2454 | } |
| 2455 | } else { |
| 2456 | // Variables are always narrowings. |
| 2457 | *isInitializerConstant = false; |
| 2458 | return true; |
| 2459 | } |
| 2460 | } |
| 2461 | return false; |
| 2462 | |
| 2463 | // * from long double to double or float, or from double to float, except |
| 2464 | // where the source is a constant expression and the actual value after |
| 2465 | // conversion is within the range of values that can be represented (even |
| 2466 | // if it cannot be represented exactly), or |
| 2467 | case ICK_Floating_Conversion: |
| 2468 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2469 | // FromType is larger than ToType. |
| 2470 | Expr::EvalResult InitializerValue; |
| 2471 | // FIXME: Check whether Initializer is a constant expression according |
| 2472 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2473 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2474 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2475 | // Constant! (Except for FIXME above.) |
| 2476 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2477 | // Convert the source value into the target type. |
| 2478 | bool ignored; |
| 2479 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2480 | Ctx.getFloatTypeSemantics(ToType), |
| 2481 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2482 | // If there was no overflow, the source value is within the range of |
| 2483 | // values that can be represented. |
| 2484 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2485 | *isInitializerConstant = true; |
| 2486 | *ConstantValue = InitializerValue.Val; |
| 2487 | return true; |
| 2488 | } |
| 2489 | } else { |
| 2490 | *isInitializerConstant = false; |
| 2491 | return true; |
| 2492 | } |
| 2493 | } |
| 2494 | return false; |
| 2495 | |
| 2496 | // * from an integer type or unscoped enumeration type to an integer type |
| 2497 | // that cannot represent all the values of the original type, except where |
| 2498 | // the source is a constant expression and the actual value after |
| 2499 | // conversion will fit into the target type and will produce the original |
| 2500 | // value when converted back to the original type. |
Jeffrey Yasskin | 6d0ee8d | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2501 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | b89d5ed | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2502 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2503 | // Boolean conversions can be from pointers and pointers to members |
| 2504 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2505 | return false; |
| 2506 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2507 | case ICK_Integral_Conversion: { |
| 2508 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2509 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2510 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2511 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2512 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2513 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2514 | |
| 2515 | if (FromWidth > ToWidth || |
| 2516 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2517 | // Not all values of FromType can be represented in ToType. |
| 2518 | llvm::APSInt InitializerValue; |
| 2519 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2520 | *isInitializerConstant = true; |
| 2521 | *ConstantValue = APValue(InitializerValue); |
| 2522 | |
| 2523 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2524 | // signed vs. unsigned comparisons. |
| 2525 | InitializerValue = InitializerValue.extend( |
| 2526 | InitializerValue.getBitWidth() + 1); |
| 2527 | // Convert the initializer to and from the target width and signed-ness. |
| 2528 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2529 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2530 | ConvertedValue.setIsSigned(ToSigned); |
| 2531 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2532 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2533 | // If the result is different, this was a narrowing conversion. |
| 2534 | return ConvertedValue != InitializerValue; |
| 2535 | } else { |
| 2536 | // Variables are always narrowings. |
| 2537 | *isInitializerConstant = false; |
| 2538 | return true; |
| 2539 | } |
| 2540 | } |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
| 2544 | default: |
| 2545 | // Other kinds of conversions are not narrowings. |
| 2546 | return false; |
| 2547 | } |
| 2548 | } |
| 2549 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2550 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2551 | FunctionDecl *Function, |
| 2552 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2553 | Step S; |
| 2554 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2555 | S.Type = Function->getType(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2556 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2557 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2558 | Steps.push_back(S); |
| 2559 | } |
| 2560 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2561 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2562 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2563 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2564 | switch (VK) { |
| 2565 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2566 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2567 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2568 | default: llvm_unreachable("No such category"); |
| 2569 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2570 | S.Type = BaseType; |
| 2571 | Steps.push_back(S); |
| 2572 | } |
| 2573 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2575 | bool BindingTemporary) { |
| 2576 | Step S; |
| 2577 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2578 | S.Type = T; |
| 2579 | Steps.push_back(S); |
| 2580 | } |
| 2581 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2582 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2583 | Step S; |
| 2584 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2585 | S.Type = T; |
| 2586 | Steps.push_back(S); |
| 2587 | } |
| 2588 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2589 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2590 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2591 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2592 | Step S; |
| 2593 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2594 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2595 | S.Function.Function = Function; |
| 2596 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2597 | Steps.push_back(S); |
| 2598 | } |
| 2599 | |
| 2600 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2601 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2602 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2603 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2604 | switch (VK) { |
| 2605 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2606 | S.Kind = SK_QualificationConversionRValue; |
| 2607 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2608 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2609 | S.Kind = SK_QualificationConversionXValue; |
| 2610 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2611 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2612 | S.Kind = SK_QualificationConversionLValue; |
| 2613 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2614 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | S.Type = Ty; |
| 2616 | Steps.push_back(S); |
| 2617 | } |
| 2618 | |
| 2619 | void InitializationSequence::AddConversionSequenceStep( |
| 2620 | const ImplicitConversionSequence &ICS, |
| 2621 | QualType T) { |
| 2622 | Step S; |
| 2623 | S.Kind = SK_ConversionSequence; |
| 2624 | S.Type = T; |
| 2625 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2626 | Steps.push_back(S); |
| 2627 | } |
| 2628 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2629 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2630 | Step S; |
| 2631 | S.Kind = SK_ListInitialization; |
| 2632 | S.Type = T; |
| 2633 | Steps.push_back(S); |
| 2634 | } |
| 2635 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2636 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2637 | InitializationSequence::AddConstructorInitializationStep( |
| 2638 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2639 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2640 | QualType T) { |
| 2641 | Step S; |
| 2642 | S.Kind = SK_ConstructorInitialization; |
| 2643 | S.Type = T; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2644 | S.Function.Function = Constructor; |
| 2645 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2646 | Steps.push_back(S); |
| 2647 | } |
| 2648 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2649 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2650 | Step S; |
| 2651 | S.Kind = SK_ZeroInitialization; |
| 2652 | S.Type = T; |
| 2653 | Steps.push_back(S); |
| 2654 | } |
| 2655 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2656 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2657 | Step S; |
| 2658 | S.Kind = SK_CAssignment; |
| 2659 | S.Type = T; |
| 2660 | Steps.push_back(S); |
| 2661 | } |
| 2662 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2663 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2664 | Step S; |
| 2665 | S.Kind = SK_StringInit; |
| 2666 | S.Type = T; |
| 2667 | Steps.push_back(S); |
| 2668 | } |
| 2669 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2670 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2671 | Step S; |
| 2672 | S.Kind = SK_ObjCObjectConversion; |
| 2673 | S.Type = T; |
| 2674 | Steps.push_back(S); |
| 2675 | } |
| 2676 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2677 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2678 | Step S; |
| 2679 | S.Kind = SK_ArrayInit; |
| 2680 | S.Type = T; |
| 2681 | Steps.push_back(S); |
| 2682 | } |
| 2683 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2684 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2685 | bool shouldCopy) { |
| 2686 | Step s; |
| 2687 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2688 | : SK_PassByIndirectRestore); |
| 2689 | s.Type = type; |
| 2690 | Steps.push_back(s); |
| 2691 | } |
| 2692 | |
| 2693 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2694 | Step S; |
| 2695 | S.Kind = SK_ProduceObjCObject; |
| 2696 | S.Type = T; |
| 2697 | Steps.push_back(S); |
| 2698 | } |
| 2699 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2700 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2701 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2702 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2703 | this->Failure = Failure; |
| 2704 | this->FailedOverloadResult = Result; |
| 2705 | } |
| 2706 | |
| 2707 | //===----------------------------------------------------------------------===// |
| 2708 | // Attempt initialization |
| 2709 | //===----------------------------------------------------------------------===// |
| 2710 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2711 | static void MaybeProduceObjCObject(Sema &S, |
| 2712 | InitializationSequence &Sequence, |
| 2713 | const InitializedEntity &Entity) { |
| 2714 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2715 | |
| 2716 | /// When initializing a parameter, produce the value if it's marked |
| 2717 | /// __attribute__((ns_consumed)). |
| 2718 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2719 | if (!Entity.isParameterConsumed()) |
| 2720 | return; |
| 2721 | |
| 2722 | assert(Entity.getType()->isObjCRetainableType() && |
| 2723 | "consuming an object of unretainable type?"); |
| 2724 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2725 | |
| 2726 | /// When initializing a return value, if the return type is a |
| 2727 | /// retainable type, then returns need to immediately retain the |
| 2728 | /// object. If an autorelease is required, it will be done at the |
| 2729 | /// last instant. |
| 2730 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2731 | if (!Entity.getType()->isObjCRetainableType()) |
| 2732 | return; |
| 2733 | |
| 2734 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2735 | } |
| 2736 | } |
| 2737 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2738 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2739 | static void TryListInitialization(Sema &S, |
| 2740 | const InitializedEntity &Entity, |
| 2741 | const InitializationKind &Kind, |
| 2742 | InitListExpr *InitList, |
| 2743 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2744 | QualType DestType = Entity.getType(); |
| 2745 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2746 | // C++ doesn't allow scalar initialization with more than one argument. |
| 2747 | // But C99 complex numbers are scalars and it makes sense there. |
| 2748 | if (S.getLangOptions().CPlusPlus && DestType->isScalarType() && |
| 2749 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 2750 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2751 | return; |
| 2752 | } |
| 2753 | // FIXME: C++0x defines behavior for these two cases. |
| 2754 | if (DestType->isReferenceType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2755 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2756 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2757 | } |
| 2758 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2759 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2760 | return; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2763 | InitListChecker CheckInitList(S, Entity, InitList, |
| 2764 | DestType, /*VerifyOnly=*/true); |
| 2765 | if (CheckInitList.HadError()) { |
| 2766 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 2767 | return; |
| 2768 | } |
| 2769 | |
| 2770 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2771 | Sequence.AddListInitializationStep(DestType); |
| 2772 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2773 | |
| 2774 | /// \brief Try a reference initialization that involves calling a conversion |
| 2775 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2776 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2777 | const InitializedEntity &Entity, |
| 2778 | const InitializationKind &Kind, |
| 2779 | Expr *Initializer, |
| 2780 | bool AllowRValues, |
| 2781 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2782 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2783 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2784 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2785 | QualType cv2T2 = Initializer->getType(); |
| 2786 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2787 | |
| 2788 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2789 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2790 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2791 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2792 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2793 | ObjCConversion, |
| 2794 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2795 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2796 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2797 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2798 | (void)ObjCLifetimeConversion; |
| 2799 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2800 | // Build the candidate set directly in the initialization sequence |
| 2801 | // structure, so that it will persist if we fail. |
| 2802 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2803 | CandidateSet.clear(); |
| 2804 | |
| 2805 | // Determine whether we are allowed to call explicit constructors or |
| 2806 | // explicit conversion operators. |
| 2807 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2808 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2809 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2810 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2811 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2812 | // The type we're converting to is a class type. Enumerate its constructors |
| 2813 | // to see if there is a suitable conversion. |
| 2814 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2816 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2817 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2818 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2819 | NamedDecl *D = *Con; |
| 2820 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2821 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2822 | // Find the constructor (which may be a template). |
| 2823 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2824 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2825 | if (ConstructorTmpl) |
| 2826 | Constructor = cast<CXXConstructorDecl>( |
| 2827 | ConstructorTmpl->getTemplatedDecl()); |
| 2828 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2829 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2831 | if (!Constructor->isInvalidDecl() && |
| 2832 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2833 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2834 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2835 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2836 | &Initializer, 1, CandidateSet, |
| 2837 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2838 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2839 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2840 | &Initializer, 1, CandidateSet, |
| 2841 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2843 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2845 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2846 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2848 | const RecordType *T2RecordType = 0; |
| 2849 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2850 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2851 | // The type we're converting from is a class type, enumerate its conversion |
| 2852 | // functions. |
| 2853 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2854 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2855 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2856 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2857 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2858 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2859 | NamedDecl *D = *I; |
| 2860 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2861 | if (isa<UsingShadowDecl>(D)) |
| 2862 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2864 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2865 | CXXConversionDecl *Conv; |
| 2866 | if (ConvTemplate) |
| 2867 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2868 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2869 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2871 | // If the conversion function doesn't return a reference type, |
| 2872 | // it can't be considered for this conversion unless we're allowed to |
| 2873 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2874 | // FIXME: Do we need to make sure that we only consider conversion |
| 2875 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2876 | // break recursion. |
| 2877 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2878 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2879 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2880 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2881 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2882 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2883 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2884 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2885 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2889 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2890 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2892 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2893 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2895 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2896 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2897 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2898 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2900 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2901 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2902 | // This is the overload that will actually be used for the initialization, so |
| 2903 | // mark it as used. |
| 2904 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2905 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2906 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2907 | if (isa<CXXConversionDecl>(Function)) |
| 2908 | T2 = Function->getResultType(); |
| 2909 | else |
| 2910 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2911 | |
| 2912 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2913 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2914 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2915 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2916 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2917 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2918 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2919 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2920 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2921 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2922 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2924 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2925 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2926 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2927 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2928 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2929 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2930 | NewDerivedToBase, NewObjCConversion, |
| 2931 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2932 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2933 | // If the type we've converted to is not reference-related to the |
| 2934 | // type we're looking for, then there is another conversion step |
| 2935 | // we need to perform to produce a temporary of the right type |
| 2936 | // that we'll be binding to. |
| 2937 | ImplicitConversionSequence ICS; |
| 2938 | ICS.setStandard(); |
| 2939 | ICS.Standard = Best->FinalConversion; |
| 2940 | T2 = ICS.Standard.getToType(2); |
| 2941 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2942 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2943 | Sequence.AddDerivedToBaseCastStep( |
| 2944 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2945 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2946 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2947 | else if (NewObjCConversion) |
| 2948 | Sequence.AddObjCObjectConversionStep( |
| 2949 | S.Context.getQualifiedType(T1, |
| 2950 | T2.getNonReferenceType().getQualifiers())); |
| 2951 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2952 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2953 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2955 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2956 | return OR_Success; |
| 2957 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2958 | |
| 2959 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2960 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2961 | const InitializedEntity &Entity, |
| 2962 | const InitializationKind &Kind, |
| 2963 | Expr *Initializer, |
| 2964 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2965 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2966 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2967 | Qualifiers T1Quals; |
| 2968 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2969 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2970 | Qualifiers T2Quals; |
| 2971 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2972 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2974 | // If the initializer is the address of an overloaded function, try |
| 2975 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2976 | // type of the resulting function. |
| 2977 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2978 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2979 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2980 | T1, |
| 2981 | false, |
| 2982 | Found)) { |
| 2983 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2984 | cv2T2 = Fn->getType(); |
| 2985 | T2 = cv2T2.getUnqualifiedType(); |
| 2986 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2987 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2988 | return; |
| 2989 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2990 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2992 | // Compute some basic properties of the types and the initializer. |
| 2993 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2994 | bool isRValueRef = !isLValueRef; |
| 2995 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2996 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2997 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2998 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2999 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3000 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3001 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3003 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3004 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3005 | // "cv2 T2" as follows: |
| 3006 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3007 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3008 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3009 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3010 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3011 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3012 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3013 | bool T1Function = T1->isFunctionType(); |
| 3014 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3015 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3016 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3017 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3018 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3019 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3020 | // reference-compatible with "cv2 T2," or |
| 3021 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3022 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3023 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3024 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3025 | // to decide whether we're actually binding to a temporary created from |
| 3026 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3027 | if (DerivedToBase) |
| 3028 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3030 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3031 | else if (ObjCConversion) |
| 3032 | Sequence.AddObjCObjectConversionStep( |
| 3033 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3034 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3035 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3036 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3037 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3038 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3039 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3040 | return; |
| 3041 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
| 3043 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3044 | // reference-related to T2, and can be implicitly converted to an |
| 3045 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3046 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3047 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3048 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3049 | // If we have an rvalue ref to function type here, the rhs must be |
| 3050 | // an rvalue. |
| 3051 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3052 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3053 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3054 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3055 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3056 | Sequence); |
| 3057 | if (ConvOvlResult == OR_Success) |
| 3058 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3059 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3060 | Sequence.SetOverloadFailure( |
| 3061 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3062 | ConvOvlResult); |
| 3063 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3066 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3067 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3068 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 3069 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3070 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3071 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3072 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3073 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3074 | Sequence.SetOverloadFailure( |
| 3075 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3076 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3077 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3078 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3079 | ? (RefRelationship == Sema::Ref_Related |
| 3080 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3081 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3082 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3084 | return; |
| 3085 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3086 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3087 | // - If the initializer expression |
| 3088 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3089 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3090 | // Note: functions are handled below. |
| 3091 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3092 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3093 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3094 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3095 | (InitCategory.isXValue() || |
| 3096 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3097 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3098 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3099 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3100 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3101 | // compiler the freedom to perform a copy here or bind to the |
| 3102 | // object, while C++0x requires that we bind directly to the |
| 3103 | // object. Hence, we always bind to the object without making an |
| 3104 | // extra copy. However, in C++03 requires that we check for the |
| 3105 | // presence of a suitable copy constructor: |
| 3106 | // |
| 3107 | // The constructor that would be used to make the copy shall |
| 3108 | // be callable whether or not the copy is actually done. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3109 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3110 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3113 | if (DerivedToBase) |
| 3114 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3115 | ValueKind); |
| 3116 | else if (ObjCConversion) |
| 3117 | Sequence.AddObjCObjectConversionStep( |
| 3118 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3120 | if (T1Quals != T2Quals) |
| 3121 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3122 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3123 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3124 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3126 | |
| 3127 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3128 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3129 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3130 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3131 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3132 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3133 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3134 | Kind, Initializer, |
| 3135 | /*AllowRValues=*/true, |
| 3136 | Sequence); |
| 3137 | if (ConvOvlResult) |
| 3138 | Sequence.SetOverloadFailure( |
| 3139 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3140 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3142 | return; |
| 3143 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3145 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3146 | return; |
| 3147 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3148 | |
| 3149 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3150 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3151 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3152 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3153 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3154 | // Determine whether we are allowed to call explicit constructors or |
| 3155 | // explicit conversion operators. |
| 3156 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3157 | |
| 3158 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3159 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3160 | ImplicitConversionSequence ICS |
| 3161 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3162 | /*SuppressUserConversions*/ false, |
| 3163 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3164 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3165 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3166 | /*AllowObjCWritebackConversion=*/false); |
| 3167 | |
| 3168 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3169 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3170 | // this into an overloading ambiguity diagnostic. However, we need |
| 3171 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3172 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3173 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3174 | Sequence.SetOverloadFailure( |
| 3175 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3176 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3177 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3178 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3179 | else |
| 3180 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3182 | } else { |
| 3183 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3187 | // same cv-qualification as, or greater cv-qualification |
| 3188 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3189 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3190 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3192 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3193 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3194 | return; |
| 3195 | } |
| 3196 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3197 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3198 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3200 | InitCategory.isLValue()) { |
| 3201 | Sequence.SetFailed( |
| 3202 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3203 | return; |
| 3204 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3205 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3206 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3207 | return; |
| 3208 | } |
| 3209 | |
| 3210 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3211 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3212 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3213 | const InitializedEntity &Entity, |
| 3214 | const InitializationKind &Kind, |
| 3215 | Expr *Initializer, |
| 3216 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3217 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3220 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3221 | /// enumerates the constructors of the initialized entity and performs overload |
| 3222 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3224 | const InitializedEntity &Entity, |
| 3225 | const InitializationKind &Kind, |
| 3226 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3227 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3228 | InitializationSequence &Sequence) { |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3229 | // Check constructor arguments for self reference. |
| 3230 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3231 | // Parameters arguments are occassionially constructed with itself, |
| 3232 | // for instance, in recursive functions. Skip them. |
| 3233 | if (!isa<ParmVarDecl>(DD)) |
| 3234 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3235 | S.CheckSelfReference(DD, Args[i]); |
| 3236 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3237 | // Build the candidate set directly in the initialization sequence |
| 3238 | // structure, so that it will persist if we fail. |
| 3239 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3240 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3242 | // Determine whether we are allowed to call explicit constructors or |
| 3243 | // explicit conversion operators. |
| 3244 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3245 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3246 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3247 | |
| 3248 | // The type we're constructing needs to be complete. |
| 3249 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3250 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3251 | return; |
| 3252 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3253 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3254 | // The type we're converting to is a class type. Enumerate its constructors |
| 3255 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3256 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3258 | CXXRecordDecl *DestRecordDecl |
| 3259 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3261 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3262 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3263 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3264 | NamedDecl *D = *Con; |
| 3265 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3266 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3268 | // Find the constructor (which may be a template). |
| 3269 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3270 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3271 | if (ConstructorTmpl) |
| 3272 | Constructor = cast<CXXConstructorDecl>( |
| 3273 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3274 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3275 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3276 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3278 | // suppress user-defined conversions on the arguments. |
| 3279 | // FIXME: Move constructors? |
| 3280 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3281 | Constructor->isCopyConstructor()) |
| 3282 | SuppressUserConversions = true; |
| 3283 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3285 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3286 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3287 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3288 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3289 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3290 | Args, NumArgs, CandidateSet, |
| 3291 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3292 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3293 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3294 | Args, NumArgs, CandidateSet, |
| 3295 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3296 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3299 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
| 3301 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3302 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3304 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3305 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3306 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3307 | Result); |
| 3308 | return; |
| 3309 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3310 | |
| 3311 | // C++0x [dcl.init]p6: |
| 3312 | // If a program calls for the default initialization of an object |
| 3313 | // of a const-qualified type T, T shall be a class type with a |
| 3314 | // user-provided default constructor. |
| 3315 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3316 | Entity.getType().isConstQualified() && |
| 3317 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3318 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3319 | return; |
| 3320 | } |
| 3321 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3322 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3323 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3324 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3325 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3326 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3327 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3330 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3332 | const InitializedEntity &Entity, |
| 3333 | const InitializationKind &Kind, |
| 3334 | InitializationSequence &Sequence) { |
| 3335 | // C++ [dcl.init]p5: |
| 3336 | // |
| 3337 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3338 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3340 | // -- if T is an array type, then each element is value-initialized; |
| 3341 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3342 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3344 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3345 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3346 | // -- if T is a class type (clause 9) with a user-declared |
| 3347 | // constructor (12.1), then the default constructor for T is |
| 3348 | // called (and the initialization is ill-formed if T has no |
| 3349 | // accessible default constructor); |
| 3350 | // |
| 3351 | // FIXME: we really want to refer to a single subobject of the array, |
| 3352 | // but Entity doesn't have a way to capture that (yet). |
| 3353 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3354 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3356 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3357 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3358 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3359 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3360 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3361 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3362 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3363 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3364 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3368 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3371 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3372 | static void TryDefaultInitialization(Sema &S, |
| 3373 | const InitializedEntity &Entity, |
| 3374 | const InitializationKind &Kind, |
| 3375 | InitializationSequence &Sequence) { |
| 3376 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3377 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3378 | // C++ [dcl.init]p6: |
| 3379 | // To default-initialize an object of type T means: |
| 3380 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3381 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3382 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3383 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3384 | // constructor for T is called (and the initialization is ill-formed if |
| 3385 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3386 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3387 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3388 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3389 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3391 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3393 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3395 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3396 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3397 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
| 3400 | |
| 3401 | // If the destination type has a lifetime property, zero-initialize it. |
| 3402 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3403 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3404 | return; |
| 3405 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3408 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3409 | /// which enumerates all conversion functions and performs overload resolution |
| 3410 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3411 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3412 | const InitializedEntity &Entity, |
| 3413 | const InitializationKind &Kind, |
| 3414 | Expr *Initializer, |
| 3415 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3416 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3417 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3418 | QualType SourceType = Initializer->getType(); |
| 3419 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3420 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3422 | // Build the candidate set directly in the initialization sequence |
| 3423 | // structure, so that it will persist if we fail. |
| 3424 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3425 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3427 | // Determine whether we are allowed to call explicit constructors or |
| 3428 | // explicit conversion operators. |
| 3429 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3430 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3431 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3432 | // The type we're converting to is a class type. Enumerate its constructors |
| 3433 | // to see if there is a suitable conversion. |
| 3434 | CXXRecordDecl *DestRecordDecl |
| 3435 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3436 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3437 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3439 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3440 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3441 | Con != ConEnd; ++Con) { |
| 3442 | NamedDecl *D = *Con; |
| 3443 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3445 | // Find the constructor (which may be a template). |
| 3446 | CXXConstructorDecl *Constructor = 0; |
| 3447 | FunctionTemplateDecl *ConstructorTmpl |
| 3448 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3449 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3450 | Constructor = cast<CXXConstructorDecl>( |
| 3451 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3452 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3453 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3455 | if (!Constructor->isInvalidDecl() && |
| 3456 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3457 | if (ConstructorTmpl) |
| 3458 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3459 | /*ExplicitArgs*/ 0, |
| 3460 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3461 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3462 | else |
| 3463 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3464 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3465 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3467 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3468 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3469 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3470 | |
| 3471 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3472 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3473 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3474 | // The type we're converting from is a class type, enumerate its conversion |
| 3475 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3476 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3477 | // We can only enumerate the conversion functions for a complete type; if |
| 3478 | // the type isn't complete, simply skip this step. |
| 3479 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3480 | CXXRecordDecl *SourceRecordDecl |
| 3481 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3482 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3483 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3484 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3485 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3486 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3487 | I != E; ++I) { |
| 3488 | NamedDecl *D = *I; |
| 3489 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3490 | if (isa<UsingShadowDecl>(D)) |
| 3491 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3493 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3494 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3495 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3496 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3497 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3498 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3499 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3500 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3501 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3502 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3503 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3504 | CandidateSet); |
| 3505 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3506 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3507 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3508 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3512 | |
| 3513 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3514 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3515 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3516 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3517 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3519 | Result); |
| 3520 | return; |
| 3521 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3523 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3524 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3526 | if (isa<CXXConstructorDecl>(Function)) { |
| 3527 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3528 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3529 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3530 | return; |
| 3531 | } |
| 3532 | |
| 3533 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3534 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3535 | if (ConvType->getAs<RecordType>()) { |
| 3536 | // If we're converting to a class type, there may be an copy if |
| 3537 | // the resulting temporary object (possible to create an object of |
| 3538 | // a base class type). That copy is not a separate conversion, so |
| 3539 | // we just make a note of the actual destination type (possibly a |
| 3540 | // base class of the type returned by the conversion function) and |
| 3541 | // let the user-defined conversion step handle the conversion. |
| 3542 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3543 | return; |
| 3544 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3545 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3546 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3547 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3548 | // If the conversion following the call to the conversion function |
| 3549 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3550 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3551 | Best->FinalConversion.Third) { |
| 3552 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3553 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3554 | ICS.Standard = Best->FinalConversion; |
| 3555 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3556 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3559 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3560 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3561 | |
| 3562 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3563 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3564 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3565 | // Skip parens. |
| 3566 | e = e->IgnoreParens(); |
| 3567 | |
| 3568 | // Skip address-of nodes. |
| 3569 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3570 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3571 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3574 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3575 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3576 | case CK_Dependent: |
| 3577 | case CK_BitCast: |
| 3578 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3579 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3580 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3581 | |
| 3582 | case CK_ArrayToPointerDecay: |
| 3583 | return IIK_nonscalar; |
| 3584 | |
| 3585 | case CK_NullToPointer: |
| 3586 | return IIK_okay; |
| 3587 | |
| 3588 | default: |
| 3589 | break; |
| 3590 | } |
| 3591 | |
| 3592 | // If we have a declaration reference, it had better be a local variable. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3593 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3594 | if (!isAddressOf) return IIK_nonlocal; |
| 3595 | |
| 3596 | VarDecl *var; |
| 3597 | if (isa<DeclRefExpr>(e)) { |
| 3598 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3599 | if (!var) return IIK_nonlocal; |
| 3600 | } else { |
| 3601 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3602 | } |
| 3603 | |
| 3604 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3605 | |
| 3606 | // If we have a conditional operator, check both sides. |
| 3607 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3608 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3609 | return iik; |
| 3610 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3611 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3612 | |
| 3613 | // These are never scalar. |
| 3614 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3615 | return IIK_nonscalar; |
| 3616 | |
| 3617 | // Otherwise, it needs to be a null pointer constant. |
| 3618 | } else { |
| 3619 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3620 | ? IIK_okay : IIK_nonlocal); |
| 3621 | } |
| 3622 | |
| 3623 | return IIK_nonlocal; |
| 3624 | } |
| 3625 | |
| 3626 | /// Check whether the given expression is a valid operand for an |
| 3627 | /// indirect copy/restore. |
| 3628 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3629 | assert(src->isRValue()); |
| 3630 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3631 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3632 | if (iik == IIK_okay) return; |
| 3633 | |
| 3634 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3635 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3636 | << src->getSourceRange(); |
| 3637 | } |
| 3638 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3639 | /// \brief Determine whether we have compatible array types for the |
| 3640 | /// purposes of GNU by-copy array initialization. |
| 3641 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3642 | const ArrayType *Dest, |
| 3643 | const ArrayType *Source) { |
| 3644 | // If the source and destination array types are equivalent, we're |
| 3645 | // done. |
| 3646 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3647 | return true; |
| 3648 | |
| 3649 | // Make sure that the element types are the same. |
| 3650 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3651 | return false; |
| 3652 | |
| 3653 | // The only mismatch we allow is when the destination is an |
| 3654 | // incomplete array type and the source is a constant array type. |
| 3655 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3656 | } |
| 3657 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3658 | static bool tryObjCWritebackConversion(Sema &S, |
| 3659 | InitializationSequence &Sequence, |
| 3660 | const InitializedEntity &Entity, |
| 3661 | Expr *Initializer) { |
| 3662 | bool ArrayDecay = false; |
| 3663 | QualType ArgType = Initializer->getType(); |
| 3664 | QualType ArgPointee; |
| 3665 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3666 | ArrayDecay = true; |
| 3667 | ArgPointee = ArgArrayType->getElementType(); |
| 3668 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3669 | } |
| 3670 | |
| 3671 | // Handle write-back conversion. |
| 3672 | QualType ConvertedArgType; |
| 3673 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3674 | ConvertedArgType)) |
| 3675 | return false; |
| 3676 | |
| 3677 | // We should copy unless we're passing to an argument explicitly |
| 3678 | // marked 'out'. |
| 3679 | bool ShouldCopy = true; |
| 3680 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3681 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3682 | |
| 3683 | // Do we need an lvalue conversion? |
| 3684 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3685 | ImplicitConversionSequence ICS; |
| 3686 | ICS.setStandard(); |
| 3687 | ICS.Standard.setAsIdentityConversion(); |
| 3688 | |
| 3689 | QualType ResultType; |
| 3690 | if (ArrayDecay) { |
| 3691 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3692 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3693 | } else { |
| 3694 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3695 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3696 | } |
| 3697 | |
| 3698 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3699 | } |
| 3700 | |
| 3701 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3702 | return true; |
| 3703 | } |
| 3704 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3705 | InitializationSequence::InitializationSequence(Sema &S, |
| 3706 | const InitializedEntity &Entity, |
| 3707 | const InitializationKind &Kind, |
| 3708 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3709 | unsigned NumArgs) |
| 3710 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3711 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3713 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3714 | // The semantics of initializers are as follows. The destination type is |
| 3715 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3716 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3718 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3719 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3720 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3721 | if (DestType->isDependentType() || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3722 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3723 | SequenceKind = DependentSequence; |
| 3724 | return; |
| 3725 | } |
| 3726 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3727 | // Almost everything is a normal sequence. |
| 3728 | setSequenceKind(NormalSequence); |
| 3729 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3730 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3731 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3732 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3733 | if (Result.isInvalid()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3734 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3735 | return; |
| 3736 | } |
| 3737 | Args[I] = Result.take(); |
| 3738 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3739 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3740 | QualType SourceType; |
| 3741 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3742 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3743 | Initializer = Args[0]; |
| 3744 | if (!isa<InitListExpr>(Initializer)) |
| 3745 | SourceType = Initializer->getType(); |
| 3746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3747 | |
| 3748 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3749 | // list-initialized (8.5.4). |
| 3750 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3751 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3752 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3753 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3755 | // - If the destination type is a reference type, see 8.5.3. |
| 3756 | if (DestType->isReferenceType()) { |
| 3757 | // C++0x [dcl.init.ref]p1: |
| 3758 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3759 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3760 | // by an object that can be converted into a T. |
| 3761 | // (Therefore, multiple arguments are not permitted.) |
| 3762 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3763 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3764 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3765 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3766 | return; |
| 3767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3769 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3770 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3771 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3772 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3773 | return; |
| 3774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3775 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3776 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3777 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3778 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3779 | return; |
| 3780 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3781 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3782 | // - If the destination type is an array of characters, an array of |
| 3783 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3784 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3786 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3787 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3788 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3789 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3790 | return; |
| 3791 | } |
| 3792 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3793 | // Note: as an GNU C extension, we allow initialization of an |
| 3794 | // array from a compound literal that creates an array of the same |
| 3795 | // type, so long as the initializer has no side effects. |
| 3796 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3797 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3798 | Initializer->getType()->isArrayType()) { |
| 3799 | const ArrayType *SourceAT |
| 3800 | = Context.getAsArrayType(Initializer->getType()); |
| 3801 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3802 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3803 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3804 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3805 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3806 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3807 | } |
| 3808 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3809 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3810 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3811 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3813 | return; |
| 3814 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3815 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3816 | // Determine whether we should consider writeback conversions for |
| 3817 | // Objective-C ARC. |
| 3818 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3819 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3820 | |
| 3821 | // We're at the end of the line for C: it's either a write-back conversion |
| 3822 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3823 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3824 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3825 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3826 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3827 | return; |
| 3828 | } |
| 3829 | |
| 3830 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3831 | AddCAssignmentStep(DestType); |
| 3832 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3833 | return; |
| 3834 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3835 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3836 | assert(S.getLangOptions().CPlusPlus); |
| 3837 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3838 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3839 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3840 | // - If the initialization is direct-initialization, or if it is |
| 3841 | // copy-initialization where the cv-unqualified version of the |
| 3842 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3843 | // class of the destination, constructors are considered. [...] |
| 3844 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3845 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3846 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3847 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3848 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3849 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3850 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3851 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3852 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3853 | // used) to a derived class thereof are enumerated as described in |
| 3854 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3855 | // (13.3). |
| 3856 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3857 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3858 | return; |
| 3859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3860 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3861 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3862 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3863 | return; |
| 3864 | } |
| 3865 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | |
| 3867 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3868 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3869 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3870 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3871 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3872 | return; |
| 3873 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3875 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3876 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3877 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3879 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3880 | |
| 3881 | ImplicitConversionSequence ICS |
| 3882 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3883 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3884 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3885 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3886 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3887 | allowObjCWritebackConversion); |
| 3888 | |
| 3889 | if (ICS.isStandard() && |
| 3890 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3891 | // Objective-C ARC writeback conversion. |
| 3892 | |
| 3893 | // We should copy unless we're passing to an argument explicitly |
| 3894 | // marked 'out'. |
| 3895 | bool ShouldCopy = true; |
| 3896 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3897 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3898 | |
| 3899 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3900 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3901 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3902 | ImplicitConversionSequence LvalueICS; |
| 3903 | LvalueICS.setStandard(); |
| 3904 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3905 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3906 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3907 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3908 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3909 | |
| 3910 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3911 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3912 | DeclAccessPair dap; |
| 3913 | if (Initializer->getType() == Context.OverloadTy && |
| 3914 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3915 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3916 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3917 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3918 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3919 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3920 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3921 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3922 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3923 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3927 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3928 | StepEnd = Steps.end(); |
| 3929 | Step != StepEnd; ++Step) |
| 3930 | Step->Destroy(); |
| 3931 | } |
| 3932 | |
| 3933 | //===----------------------------------------------------------------------===// |
| 3934 | // Perform initialization |
| 3935 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3937 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3938 | switch(Entity.getKind()) { |
| 3939 | case InitializedEntity::EK_Variable: |
| 3940 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3941 | case InitializedEntity::EK_Exception: |
| 3942 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3943 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3944 | return Sema::AA_Initializing; |
| 3945 | |
| 3946 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3948 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3949 | return Sema::AA_Sending; |
| 3950 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3951 | return Sema::AA_Passing; |
| 3952 | |
| 3953 | case InitializedEntity::EK_Result: |
| 3954 | return Sema::AA_Returning; |
| 3955 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3956 | case InitializedEntity::EK_Temporary: |
| 3957 | // FIXME: Can we tell apart casting vs. converting? |
| 3958 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3959 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3960 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3961 | case InitializedEntity::EK_ArrayElement: |
| 3962 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3963 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3964 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3965 | return Sema::AA_Initializing; |
| 3966 | } |
| 3967 | |
| 3968 | return Sema::AA_Converting; |
| 3969 | } |
| 3970 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3971 | /// \brief Whether we should binding a created object as a temporary when |
| 3972 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3973 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3974 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3975 | case InitializedEntity::EK_ArrayElement: |
| 3976 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3977 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3978 | case InitializedEntity::EK_New: |
| 3979 | case InitializedEntity::EK_Variable: |
| 3980 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3981 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3982 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3983 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3984 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3985 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3986 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3988 | case InitializedEntity::EK_Parameter: |
| 3989 | case InitializedEntity::EK_Temporary: |
| 3990 | return true; |
| 3991 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3992 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3993 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3994 | } |
| 3995 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3996 | /// \brief Whether the given entity, when initialized with an object |
| 3997 | /// created for that initialization, requires destruction. |
| 3998 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3999 | switch (Entity.getKind()) { |
| 4000 | case InitializedEntity::EK_Member: |
| 4001 | case InitializedEntity::EK_Result: |
| 4002 | case InitializedEntity::EK_New: |
| 4003 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4004 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4005 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4006 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4007 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4008 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4010 | case InitializedEntity::EK_Variable: |
| 4011 | case InitializedEntity::EK_Parameter: |
| 4012 | case InitializedEntity::EK_Temporary: |
| 4013 | case InitializedEntity::EK_ArrayElement: |
| 4014 | case InitializedEntity::EK_Exception: |
| 4015 | return true; |
| 4016 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | |
| 4018 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4021 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4022 | /// provided by the given initializer by calling the appropriate copy |
| 4023 | /// constructor. |
| 4024 | /// |
| 4025 | /// \param S The Sema object used for type-checking. |
| 4026 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4027 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4028 | /// the type of the initializer expression or a superclass thereof. |
| 4029 | /// |
| 4030 | /// \param Enter The entity being initialized. |
| 4031 | /// |
| 4032 | /// \param CurInit The initializer expression. |
| 4033 | /// |
| 4034 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4035 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4036 | /// an rvalue. |
| 4037 | /// |
| 4038 | /// \returns An expression that copies the initializer expression into |
| 4039 | /// a temporary object, or an error expression if a copy could not be |
| 4040 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4041 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4042 | QualType T, |
| 4043 | const InitializedEntity &Entity, |
| 4044 | ExprResult CurInit, |
| 4045 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4046 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4047 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4048 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4049 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4050 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4051 | if (!Class) |
| 4052 | return move(CurInit); |
| 4053 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4054 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4055 | // When certain criteria are met, an implementation is allowed to |
| 4056 | // omit the copy/move construction of a class object, even if the |
| 4057 | // copy/move constructor and/or destructor for the object have |
| 4058 | // side effects. [...] |
| 4059 | // - when a temporary class object that has not been bound to a |
| 4060 | // reference (12.2) would be copied/moved to a class object |
| 4061 | // with the same cv-unqualified type, the copy/move operation |
| 4062 | // can be omitted by constructing the temporary object |
| 4063 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4065 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4066 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4068 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4069 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4070 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4071 | switch (Entity.getKind()) { |
| 4072 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4073 | Loc = Entity.getReturnLoc(); |
| 4074 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4076 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4077 | Loc = Entity.getThrowLoc(); |
| 4078 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4079 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4080 | case InitializedEntity::EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4081 | Loc = Entity.getDecl()->getLocation(); |
| 4082 | break; |
| 4083 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4084 | case InitializedEntity::EK_ArrayElement: |
| 4085 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4086 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4087 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4088 | case InitializedEntity::EK_New: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4089 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4090 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4091 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4092 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4093 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4094 | Loc = CurInitExpr->getLocStart(); |
| 4095 | break; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4096 | } |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4097 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4098 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4099 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4100 | return move(CurInit); |
| 4101 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4102 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4103 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4104 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 4105 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4106 | Con != ConEnd; ++Con) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4107 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4108 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 4109 | // initialization is direct-initialization. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4110 | CXXConstructorDecl *Constructor = 0; |
| 4111 | |
| 4112 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4113 | // Handle copy/moveconstructors, only. |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4114 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4115 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4116 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4117 | continue; |
| 4118 | |
| 4119 | DeclAccessPair FoundDecl |
| 4120 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4121 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4122 | &CurInitExpr, 1, CandidateSet); |
| 4123 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4124 | } |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4125 | |
| 4126 | // Handle constructor templates. |
| 4127 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4128 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4129 | continue; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4131 | Constructor = cast<CXXConstructorDecl>( |
| 4132 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 8ff338b | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4133 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4134 | continue; |
| 4135 | |
| 4136 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4137 | // candidates? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4138 | DeclAccessPair FoundDecl |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4139 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4140 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4141 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4142 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4143 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4144 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4145 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4146 | case OR_Success: |
| 4147 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4149 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4150 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4151 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4152 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4153 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4154 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4155 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4156 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4157 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4158 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4159 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4160 | case OR_Ambiguous: |
| 4161 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4162 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4163 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4164 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4165 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4167 | case OR_Deleted: |
| 4168 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4169 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4170 | << CurInitExpr->getSourceRange(); |
| 4171 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4172 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4173 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4176 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4177 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4178 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4179 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4180 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4181 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (IsExtraneousCopy) { |
| 4184 | // If this is a totally extraneous copy for C++03 reference |
| 4185 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4186 | // expression. We don't generate an (elided) copy operation here |
| 4187 | // because doing so would require us to pass down a flag to avoid |
| 4188 | // infinite recursion, where each step adds another extraneous, |
| 4189 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4191 | // Instantiate the default arguments of any extra parameters in |
| 4192 | // the selected copy constructor, as if we were going to create a |
| 4193 | // proper call to the copy constructor. |
| 4194 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4195 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4196 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4197 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4198 | break; |
| 4199 | |
| 4200 | // Build the default argument expression; we don't actually care |
| 4201 | // if this succeeds or not, because this routine will complain |
| 4202 | // if there was a problem. |
| 4203 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4204 | } |
| 4205 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4206 | return S.Owned(CurInitExpr); |
| 4207 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4208 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4209 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4210 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4211 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4212 | // constructor call (we might have derived-to-base conversions, or |
| 4213 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4214 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4215 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4216 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4217 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4218 | // Actually perform the constructor call. |
| 4219 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4220 | move_arg(ConstructorArgs), |
| 4221 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4222 | CXXConstructExpr::CK_Complete, |
| 4223 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4225 | // If we're supposed to bind temporaries, do so. |
| 4226 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4227 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4228 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4229 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4231 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4232 | const InitializedEntity &Entity) { |
| 4233 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4234 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4235 | return; |
| 4236 | |
| 4237 | if (Entity.getDecl()->getDeclName()) |
| 4238 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4239 | << Entity.getDecl()->getDeclName(); |
| 4240 | else |
| 4241 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4242 | } |
| 4243 | } |
| 4244 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4245 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4246 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4247 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4248 | } |
| 4249 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4250 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4251 | InitializationSequence::Perform(Sema &S, |
| 4252 | const InitializedEntity &Entity, |
| 4253 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4254 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4255 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4256 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4257 | unsigned NumArgs = Args.size(); |
| 4258 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4259 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4260 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4262 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4263 | // If the declaration is a non-dependent, incomplete array type |
| 4264 | // that has an initializer, then its type will be completed once |
| 4265 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4266 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4267 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4268 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4269 | if (const IncompleteArrayType *ArrayT |
| 4270 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4271 | // FIXME: We don't currently have the ability to accurately |
| 4272 | // compute the length of an initializer list without |
| 4273 | // performing full type-checking of the initializer list |
| 4274 | // (since we have to determine where braces are implicitly |
| 4275 | // introduced and such). So, we fall back to making the array |
| 4276 | // type a dependently-sized array type with no specified |
| 4277 | // bound. |
| 4278 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4279 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4281 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4282 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4283 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4284 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4285 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4286 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4287 | Brackets = ArrayLoc->getBracketsRange(); |
| 4288 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | *ResultType |
| 4292 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4293 | /*NumElts=*/0, |
| 4294 | ArrayT->getSizeModifier(), |
| 4295 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4296 | Brackets); |
| 4297 | } |
| 4298 | |
| 4299 | } |
| 4300 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4301 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4302 | Kind.isExplicitCast()); |
| 4303 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4306 | // No steps means no initialization. |
| 4307 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4308 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4310 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4311 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4312 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4313 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4314 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4315 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4316 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4317 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4318 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4321 | // grab the only argument out the Args and place it into the "current" |
| 4322 | // initializer. |
| 4323 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4324 | case SK_ResolveAddressOfOverloadedFunction: |
| 4325 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4326 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4327 | case SK_CastDerivedToBaseLValue: |
| 4328 | case SK_BindReference: |
| 4329 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4330 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4331 | case SK_UserConversion: |
| 4332 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4333 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4334 | case SK_QualificationConversionRValue: |
| 4335 | case SK_ConversionSequence: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4336 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4337 | case SK_ListInitialization: |
| 4338 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4339 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4340 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4341 | case SK_ArrayInit: |
| 4342 | case SK_PassByIndirectCopyRestore: |
| 4343 | case SK_PassByIndirectRestore: |
| 4344 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4345 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4346 | CurInit = Args.get()[0]; |
| 4347 | if (!CurInit.get()) return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4348 | |
| 4349 | // Read from a property when initializing something with it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4350 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4351 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4352 | if (CurInit.isInvalid()) |
| 4353 | return ExprError(); |
| 4354 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4355 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4356 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4358 | case SK_ConstructorInitialization: |
| 4359 | case SK_ZeroInitialization: |
| 4360 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4361 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4362 | |
| 4363 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4364 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4365 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4366 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4367 | Step != StepEnd; ++Step) { |
| 4368 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4369 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4371 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | switch (Step->Kind) { |
| 4374 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4375 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4376 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4377 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4378 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4379 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4380 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4381 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4382 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4384 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4385 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4386 | case SK_CastDerivedToBaseLValue: { |
| 4387 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4388 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4390 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4391 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4392 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4393 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4394 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4395 | CurInit.get()->getLocStart(), |
| 4396 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4397 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4398 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4400 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4401 | QualType T = SourceType; |
| 4402 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4403 | T = Pointer->getPointeeType(); |
| 4404 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4405 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4406 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4407 | } |
| 4408 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4409 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4410 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4411 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4412 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4413 | VK_XValue : |
| 4414 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4415 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4416 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4417 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4418 | CurInit.get(), |
| 4419 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4420 | break; |
| 4421 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4423 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4424 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4425 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4426 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4427 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4428 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4429 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4430 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4431 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4433 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4434 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4435 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4436 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4437 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4438 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4439 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4440 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4441 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4443 | // Reference binding does not have any corresponding ASTs. |
| 4444 | |
| 4445 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4446 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4447 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4449 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4450 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4451 | case SK_BindReferenceToTemporary: |
| 4452 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4453 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4454 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4455 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4456 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4457 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4458 | Entity.getType().getNonReferenceType(), |
| 4459 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4460 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4461 | |
| 4462 | // If we're binding to an Objective-C object that has lifetime, we |
| 4463 | // need cleanups. |
| 4464 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4465 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4466 | S.ExprNeedsCleanups = true; |
| 4467 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4468 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4470 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4472 | /*IsExtraneousCopy=*/true); |
| 4473 | break; |
| 4474 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4475 | case SK_UserConversion: { |
| 4476 | // We have a user-defined conversion that invokes either a constructor |
| 4477 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4478 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4479 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4480 | FunctionDecl *Fn = Step->Function.Function; |
| 4481 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4482 | bool CreatedObject = false; |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4483 | bool IsLvalue = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4484 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4485 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4486 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4487 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4488 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4489 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4490 | // Determine the arguments required to actually perform the constructor |
| 4491 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4492 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4493 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4494 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4495 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4496 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4498 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4499 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4500 | move_arg(ConstructorArgs), |
| 4501 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4502 | CXXConstructExpr::CK_Complete, |
| 4503 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4504 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4505 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4506 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4507 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4508 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4509 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4510 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4511 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4512 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4513 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4514 | S.IsDerivedFrom(SourceType, Class)) |
| 4515 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4516 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4517 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4518 | } else { |
| 4519 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4520 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | f0e0b17 | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4521 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4522 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4523 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4524 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4525 | |
| 4526 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4527 | // derived-to-base conversion? I believe the answer is "no", because |
| 4528 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4529 | ExprResult CurInitExprRes = |
| 4530 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4531 | FoundFn, Conversion); |
| 4532 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4533 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4534 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4535 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4536 | // Build the actual call to the conversion function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4537 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4538 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4539 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4540 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4541 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4543 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4546 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4547 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4548 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4549 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4550 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4551 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4552 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4553 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4554 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4555 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4556 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4557 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4558 | } |
| 4559 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4560 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4561 | // FIXME: xvalues |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4562 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4563 | CurInit.get()->getType(), |
| 4564 | CastKind, CurInit.get(), 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4565 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4566 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4567 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4568 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4569 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | break; |
| 4572 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4574 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4575 | case SK_QualificationConversionXValue: |
| 4576 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4577 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4578 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4579 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4580 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4581 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4582 | VK_XValue : |
| 4583 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4584 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4585 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4588 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4589 | Sema::CheckedConversionKind CCK |
| 4590 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4591 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4592 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4593 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4594 | ExprResult CurInitExprRes = |
| 4595 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4596 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4597 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4598 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4599 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4600 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4601 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4603 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4604 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4605 | QualType Ty = Step->Type; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4606 | InitListChecker PerformInitList(S, Entity, InitList, |
| 4607 | ResultType ? *ResultType : Ty, /*VerifyOnly=*/false); |
| 4608 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4609 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4610 | |
| 4611 | CurInit.release(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4612 | CurInit = S.Owned(PerformInitList.getFullyStructuredList()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4613 | break; |
| 4614 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4615 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4616 | case SK_ListConstructorCall: |
| 4617 | assert(false && "List constructor calls not yet supported."); |
| 4618 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4619 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4620 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4621 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4622 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4623 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4624 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4625 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4626 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4627 | ? Kind.getEqualLoc() |
| 4628 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4629 | |
| 4630 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4631 | // Force even a trivial, implicit default constructor to be |
| 4632 | // semantically checked. We do this explicitly because we don't build |
| 4633 | // the definition for completely trivial constructors. |
| 4634 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4635 | assert(ClassDecl && "No parent class for constructor."); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4636 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4637 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4638 | !Constructor->isUsed(false)) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4639 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4640 | } |
| 4641 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4642 | // Determine the arguments required to actually perform the constructor |
| 4643 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4644 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4645 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4646 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4647 | |
| 4648 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4649 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4650 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4651 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4652 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4653 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4654 | unsigned NumExprs = ConstructorArgs.size(); |
| 4655 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4656 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4657 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4659 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4660 | if (!TSInfo) |
| 4661 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4663 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4664 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4665 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4667 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4668 | Kind.getParenRange(), |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4669 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4670 | } else { |
| 4671 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4672 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4674 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4675 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4677 | CXXConstructExpr::CK_NonVirtualBase; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4678 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4679 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4682 | // Only get the parenthesis range if it is a direct construction. |
| 4683 | SourceRange parenRange = |
| 4684 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4685 | Kind.getParenRange() : SourceRange(); |
| 4686 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4687 | // If the entity allows NRVO, mark the construction as elidable |
| 4688 | // unconditionally. |
| 4689 | if (Entity.allowsNRVO()) |
| 4690 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4691 | Constructor, /*Elidable=*/true, |
| 4692 | move_arg(ConstructorArgs), |
| 4693 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4694 | ConstructKind, |
| 4695 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4696 | else |
| 4697 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4698 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4699 | move_arg(ConstructorArgs), |
| 4700 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4701 | ConstructKind, |
| 4702 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4703 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4704 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4705 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4706 | |
| 4707 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4708 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4709 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4710 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4712 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4713 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4715 | break; |
| 4716 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4718 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4719 | step_iterator NextStep = Step; |
| 4720 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4722 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4723 | // The need for zero-initialization is recorded directly into |
| 4724 | // the call to the object's constructor within the next step. |
| 4725 | ConstructorInitRequiresZeroInit = true; |
| 4726 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4727 | S.getLangOptions().CPlusPlus && |
| 4728 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4729 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4730 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4732 | Kind.getRange().getBegin()); |
| 4733 | |
| 4734 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4735 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4736 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4737 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4738 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4739 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4740 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4741 | break; |
| 4742 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4743 | |
| 4744 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4745 | QualType SourceType = CurInit.get()->getType(); |
| 4746 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4747 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4749 | if (Result.isInvalid()) |
| 4750 | return ExprError(); |
| 4751 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4752 | |
| 4753 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4754 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4755 | if (ConvTy != Sema::Compatible && |
| 4756 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4757 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4758 | == Sema::Compatible) |
| 4759 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4760 | if (CurInitExprRes.isInvalid()) |
| 4761 | return ExprError(); |
| 4762 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4764 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4765 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4766 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4767 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4768 | getAssignmentAction(Entity), |
| 4769 | &Complained)) { |
| 4770 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4771 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4772 | } else if (Complained) |
| 4773 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4774 | break; |
| 4775 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4776 | |
| 4777 | case SK_StringInit: { |
| 4778 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4779 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4780 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4781 | break; |
| 4782 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4783 | |
| 4784 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4785 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4786 | CK_ObjCObjectLValueCast, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4787 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4788 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4789 | |
| 4790 | case SK_ArrayInit: |
| 4791 | // Okay: we checked everything before creating this step. Note that |
| 4792 | // this is a GNU extension. |
| 4793 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4794 | << Step->Type << CurInit.get()->getType() |
| 4795 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4796 | |
| 4797 | // If the destination type is an incomplete array type, update the |
| 4798 | // type accordingly. |
| 4799 | if (ResultType) { |
| 4800 | if (const IncompleteArrayType *IncompleteDest |
| 4801 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4802 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4803 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4804 | *ResultType = S.Context.getConstantArrayType( |
| 4805 | IncompleteDest->getElementType(), |
| 4806 | ConstantSource->getSize(), |
| 4807 | ArrayType::Normal, 0); |
| 4808 | } |
| 4809 | } |
| 4810 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4811 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4812 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4813 | case SK_PassByIndirectCopyRestore: |
| 4814 | case SK_PassByIndirectRestore: |
| 4815 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4816 | CurInit = S.Owned(new (S.Context) |
| 4817 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4818 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4819 | break; |
| 4820 | |
| 4821 | case SK_ProduceObjCObject: |
| 4822 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4823 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4824 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4825 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4826 | } |
| 4827 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4828 | |
| 4829 | // Diagnose non-fatal problems with the completed initialization. |
| 4830 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4831 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4832 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4833 | cast<FieldDecl>(Entity.getDecl()), |
| 4834 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4836 | return move(CurInit); |
| 4837 | } |
| 4838 | |
| 4839 | //===----------------------------------------------------------------------===// |
| 4840 | // Diagnose initialization failures |
| 4841 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4843 | const InitializedEntity &Entity, |
| 4844 | const InitializationKind &Kind, |
| 4845 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4846 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4847 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4848 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4849 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4850 | switch (Failure) { |
| 4851 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4852 | // FIXME: Customize for the initialized entity? |
| 4853 | if (NumArgs == 0) |
| 4854 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4855 | << DestType.getNonReferenceType(); |
| 4856 | else // FIXME: diagnostic below could be better! |
| 4857 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4858 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4859 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4861 | case FK_ArrayNeedsInitList: |
| 4862 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4863 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4864 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4865 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4867 | case FK_ArrayTypeMismatch: |
| 4868 | case FK_NonConstantArrayInit: |
| 4869 | S.Diag(Kind.getLocation(), |
| 4870 | (Failure == FK_ArrayTypeMismatch |
| 4871 | ? diag::err_array_init_different_type |
| 4872 | : diag::err_array_init_non_constant_array)) |
| 4873 | << DestType.getNonReferenceType() |
| 4874 | << Args[0]->getType() |
| 4875 | << Args[0]->getSourceRange(); |
| 4876 | break; |
| 4877 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4878 | case FK_AddressOfOverloadFailed: { |
| 4879 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4880 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4881 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4882 | true, |
| 4883 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4884 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4887 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4888 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4889 | switch (FailedOverloadResult) { |
| 4890 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4891 | if (Failure == FK_UserConversionOverloadFailed) |
| 4892 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4893 | << Args[0]->getType() << DestType |
| 4894 | << Args[0]->getSourceRange(); |
| 4895 | else |
| 4896 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4897 | << DestType << Args[0]->getType() |
| 4898 | << Args[0]->getSourceRange(); |
| 4899 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4900 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4901 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4903 | case OR_No_Viable_Function: |
| 4904 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4905 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4906 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4907 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4908 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4909 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4910 | case OR_Deleted: { |
| 4911 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4912 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4913 | << Args[0]->getSourceRange(); |
| 4914 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4915 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4916 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4917 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4918 | if (Ovl == OR_Deleted) { |
| 4919 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4920 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4921 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4922 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4923 | } |
| 4924 | break; |
| 4925 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4927 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4928 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4929 | break; |
| 4930 | } |
| 4931 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4933 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4934 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4936 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4937 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4938 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4939 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4940 | << DestType.getNonReferenceType() |
| 4941 | << Args[0]->getType() |
| 4942 | << Args[0]->getSourceRange(); |
| 4943 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4944 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4945 | case FK_RValueReferenceBindingToLValue: |
| 4946 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4947 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4948 | << Args[0]->getSourceRange(); |
| 4949 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4950 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4951 | case FK_ReferenceInitDropsQualifiers: |
| 4952 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4953 | << DestType.getNonReferenceType() |
| 4954 | << Args[0]->getType() |
| 4955 | << Args[0]->getSourceRange(); |
| 4956 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4958 | case FK_ReferenceInitFailed: |
| 4959 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4960 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4961 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4962 | << Args[0]->getType() |
| 4963 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4964 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4965 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4966 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4967 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4968 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4969 | case FK_ConversionFailed: { |
| 4970 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4971 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4972 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4973 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4974 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4975 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4976 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4977 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4978 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4979 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4980 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4981 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4982 | |
| 4983 | case FK_ConversionFromPropertyFailed: |
| 4984 | // No-op. This error has already been reported. |
| 4985 | break; |
| 4986 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4987 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4988 | SourceRange R; |
| 4989 | |
| 4990 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4991 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4992 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4994 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4995 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4996 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4997 | if (Kind.isCStyleOrFunctionalCast()) |
| 4998 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4999 | << R; |
| 5000 | else |
| 5001 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5002 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5003 | break; |
| 5004 | } |
| 5005 | |
| 5006 | case FK_ReferenceBindingToInitList: |
| 5007 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5008 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5009 | break; |
| 5010 | |
| 5011 | case FK_InitListBadDestinationType: |
| 5012 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5013 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5014 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5016 | case FK_ConstructorOverloadFailed: { |
| 5017 | SourceRange ArgsRange; |
| 5018 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5020 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5021 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5022 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5023 | // bad. |
| 5024 | switch (FailedOverloadResult) { |
| 5025 | case OR_Ambiguous: |
| 5026 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5027 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5028 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5029 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5030 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5032 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5033 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5034 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5035 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5036 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5037 | // This is implicit default initialization of a member or |
| 5038 | // base within a constructor. If no viable function was |
| 5039 | // found, notify the user that she needs to explicitly |
| 5040 | // initialize this base/member. |
| 5041 | CXXConstructorDecl *Constructor |
| 5042 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5043 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5044 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5045 | << Constructor->isImplicit() |
| 5046 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5047 | << /*base=*/0 |
| 5048 | << Entity.getType(); |
| 5049 | |
| 5050 | RecordDecl *BaseDecl |
| 5051 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5052 | ->getDecl(); |
| 5053 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5054 | << S.Context.getTagDeclType(BaseDecl); |
| 5055 | } else { |
| 5056 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5057 | << Constructor->isImplicit() |
| 5058 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5059 | << /*member=*/1 |
| 5060 | << Entity.getName(); |
| 5061 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5062 | |
| 5063 | if (const RecordType *Record |
| 5064 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5065 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5066 | diag::note_previous_decl) |
| 5067 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5068 | } |
| 5069 | break; |
| 5070 | } |
| 5071 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5072 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5073 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5074 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5075 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5076 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5077 | case OR_Deleted: { |
| 5078 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5079 | << true << DestType << ArgsRange; |
| 5080 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5081 | OverloadingResult Ovl |
| 5082 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5083 | if (Ovl == OR_Deleted) { |
| 5084 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5085 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5086 | } else { |
| 5087 | llvm_unreachable("Inconsistent overload resolution?"); |
| 5088 | } |
| 5089 | break; |
| 5090 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5092 | case OR_Success: |
| 5093 | llvm_unreachable("Conversion did not fail!"); |
| 5094 | break; |
| 5095 | } |
| 5096 | break; |
| 5097 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5099 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5100 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5101 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5102 | // This is implicit default-initialization of a const member in |
| 5103 | // a constructor. Complain that it needs to be explicitly |
| 5104 | // initialized. |
| 5105 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5106 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5107 | << Constructor->isImplicit() |
| 5108 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5109 | << /*const=*/1 |
| 5110 | << Entity.getName(); |
| 5111 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5112 | << Entity.getName(); |
| 5113 | } else { |
| 5114 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5115 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5116 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5117 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5118 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5119 | case FK_Incomplete: |
| 5120 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 5121 | diag::err_init_incomplete_type); |
| 5122 | break; |
| 5123 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5124 | case FK_ListInitializationFailed: { |
| 5125 | // Run the init list checker again to emit diagnostics. |
| 5126 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5127 | QualType DestType = Entity.getType(); |
| 5128 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
| 5129 | DestType, /*VerifyOnly=*/false); |
| 5130 | assert(DiagnoseInitList.HadError() && |
| 5131 | "Inconsistent init list check result."); |
| 5132 | break; |
| 5133 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5134 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5135 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5136 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5137 | return true; |
| 5138 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5139 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5140 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5141 | switch (SequenceKind) { |
| 5142 | case FailedSequence: { |
| 5143 | OS << "Failed sequence: "; |
| 5144 | switch (Failure) { |
| 5145 | case FK_TooManyInitsForReference: |
| 5146 | OS << "too many initializers for reference"; |
| 5147 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5148 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5149 | case FK_ArrayNeedsInitList: |
| 5150 | OS << "array requires initializer list"; |
| 5151 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5153 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5154 | OS << "array requires initializer list or string literal"; |
| 5155 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5156 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5157 | case FK_ArrayTypeMismatch: |
| 5158 | OS << "array type mismatch"; |
| 5159 | break; |
| 5160 | |
| 5161 | case FK_NonConstantArrayInit: |
| 5162 | OS << "non-constant array initializer"; |
| 5163 | break; |
| 5164 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5165 | case FK_AddressOfOverloadFailed: |
| 5166 | OS << "address of overloaded function failed"; |
| 5167 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5169 | case FK_ReferenceInitOverloadFailed: |
| 5170 | OS << "overload resolution for reference initialization failed"; |
| 5171 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5173 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5174 | OS << "non-const lvalue reference bound to temporary"; |
| 5175 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5176 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5177 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5178 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5179 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5180 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5181 | case FK_RValueReferenceBindingToLValue: |
| 5182 | OS << "rvalue reference bound to an lvalue"; |
| 5183 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5184 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5185 | case FK_ReferenceInitDropsQualifiers: |
| 5186 | OS << "reference initialization drops qualifiers"; |
| 5187 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5189 | case FK_ReferenceInitFailed: |
| 5190 | OS << "reference initialization failed"; |
| 5191 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5193 | case FK_ConversionFailed: |
| 5194 | OS << "conversion failed"; |
| 5195 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5196 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5197 | case FK_ConversionFromPropertyFailed: |
| 5198 | OS << "conversion from property failed"; |
| 5199 | break; |
| 5200 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5201 | case FK_TooManyInitsForScalar: |
| 5202 | OS << "too many initializers for scalar"; |
| 5203 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5204 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5205 | case FK_ReferenceBindingToInitList: |
| 5206 | OS << "referencing binding to initializer list"; |
| 5207 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5208 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5209 | case FK_InitListBadDestinationType: |
| 5210 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5211 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5213 | case FK_UserConversionOverloadFailed: |
| 5214 | OS << "overloading failed for user-defined conversion"; |
| 5215 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5216 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5217 | case FK_ConstructorOverloadFailed: |
| 5218 | OS << "constructor overloading failed"; |
| 5219 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5220 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5221 | case FK_DefaultInitOfConst: |
| 5222 | OS << "default initialization of a const variable"; |
| 5223 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5225 | case FK_Incomplete: |
| 5226 | OS << "initialization of incomplete type"; |
| 5227 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5228 | |
| 5229 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5230 | OS << "list initialization checker failure"; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5231 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5232 | OS << '\n'; |
| 5233 | return; |
| 5234 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5235 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5236 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5237 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5238 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5240 | case NormalSequence: |
| 5241 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5242 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5243 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5245 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5246 | if (S != step_begin()) { |
| 5247 | OS << " -> "; |
| 5248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5250 | switch (S->Kind) { |
| 5251 | case SK_ResolveAddressOfOverloadedFunction: |
| 5252 | OS << "resolve address of overloaded function"; |
| 5253 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5255 | case SK_CastDerivedToBaseRValue: |
| 5256 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5257 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5258 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5259 | case SK_CastDerivedToBaseXValue: |
| 5260 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5261 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5263 | case SK_CastDerivedToBaseLValue: |
| 5264 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5265 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5267 | case SK_BindReference: |
| 5268 | OS << "bind reference to lvalue"; |
| 5269 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5271 | case SK_BindReferenceToTemporary: |
| 5272 | OS << "bind reference to a temporary"; |
| 5273 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5275 | case SK_ExtraneousCopyToTemporary: |
| 5276 | OS << "extraneous C++03 copy to temporary"; |
| 5277 | break; |
| 5278 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5279 | case SK_UserConversion: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5280 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5281 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5283 | case SK_QualificationConversionRValue: |
| 5284 | OS << "qualification conversion (rvalue)"; |
| 5285 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5286 | case SK_QualificationConversionXValue: |
| 5287 | OS << "qualification conversion (xvalue)"; |
| 5288 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5289 | case SK_QualificationConversionLValue: |
| 5290 | OS << "qualification conversion (lvalue)"; |
| 5291 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5293 | case SK_ConversionSequence: |
| 5294 | OS << "implicit conversion sequence ("; |
| 5295 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5296 | OS << ")"; |
| 5297 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5298 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5299 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5300 | OS << "list aggregate initialization"; |
| 5301 | break; |
| 5302 | |
| 5303 | case SK_ListConstructorCall: |
| 5304 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5305 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5307 | case SK_ConstructorInitialization: |
| 5308 | OS << "constructor initialization"; |
| 5309 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5311 | case SK_ZeroInitialization: |
| 5312 | OS << "zero initialization"; |
| 5313 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5314 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5315 | case SK_CAssignment: |
| 5316 | OS << "C assignment"; |
| 5317 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5319 | case SK_StringInit: |
| 5320 | OS << "string initialization"; |
| 5321 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5322 | |
| 5323 | case SK_ObjCObjectConversion: |
| 5324 | OS << "Objective-C object conversion"; |
| 5325 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5326 | |
| 5327 | case SK_ArrayInit: |
| 5328 | OS << "array initialization"; |
| 5329 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5330 | |
| 5331 | case SK_PassByIndirectCopyRestore: |
| 5332 | OS << "pass by indirect copy and restore"; |
| 5333 | break; |
| 5334 | |
| 5335 | case SK_PassByIndirectRestore: |
| 5336 | OS << "pass by indirect restore"; |
| 5337 | break; |
| 5338 | |
| 5339 | case SK_ProduceObjCObject: |
| 5340 | OS << "Objective-C object retension"; |
| 5341 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | void InitializationSequence::dump() const { |
| 5347 | dump(llvm::errs()); |
| 5348 | } |
| 5349 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5350 | static void DiagnoseNarrowingInInitList( |
| 5351 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5352 | bool Constant, const APValue &ConstantValue) { |
| 5353 | if (Constant) { |
| 5354 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5355 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5356 | ? diag::err_init_list_constant_narrowing |
| 5357 | : diag::warn_init_list_constant_narrowing) |
| 5358 | << InitE->getSourceRange() |
| 5359 | << ConstantValue |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5360 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5361 | } else |
| 5362 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5363 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5364 | ? diag::err_init_list_variable_narrowing |
| 5365 | : diag::warn_init_list_variable_narrowing) |
| 5366 | << InitE->getSourceRange() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5367 | << InitE->getType().getLocalUnqualifiedType() |
| 5368 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5369 | |
| 5370 | llvm::SmallString<128> StaticCast; |
| 5371 | llvm::raw_svector_ostream OS(StaticCast); |
| 5372 | OS << "static_cast<"; |
| 5373 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5374 | // It's important to use the typedef's name if there is one so that the |
| 5375 | // fixit doesn't break code using types like int64_t. |
| 5376 | // |
| 5377 | // FIXME: This will break if the typedef requires qualification. But |
| 5378 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5379 | OS << TT->getDecl(); |
| 5380 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5381 | OS << BT->getName(S.getLangOptions()); |
| 5382 | else { |
| 5383 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5384 | // with a broken cast. |
| 5385 | return; |
| 5386 | } |
| 5387 | OS << ">("; |
| 5388 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5389 | << InitE->getSourceRange() |
| 5390 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5391 | << FixItHint::CreateInsertion( |
| 5392 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5393 | } |
| 5394 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5395 | //===----------------------------------------------------------------------===// |
| 5396 | // Initialization helper functions |
| 5397 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5398 | bool |
| 5399 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5400 | ExprResult Init) { |
| 5401 | if (Init.isInvalid()) |
| 5402 | return false; |
| 5403 | |
| 5404 | Expr *InitE = Init.get(); |
| 5405 | assert(InitE && "No initialization expression"); |
| 5406 | |
| 5407 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5408 | SourceLocation()); |
| 5409 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5410 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5414 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5415 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5416 | ExprResult Init, |
| 5417 | bool TopLevelOfInitList) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5418 | if (Init.isInvalid()) |
| 5419 | return ExprError(); |
| 5420 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5421 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5422 | assert(InitE && "No initialization expression?"); |
| 5423 | |
| 5424 | if (EqualLoc.isInvalid()) |
| 5425 | EqualLoc = InitE->getLocStart(); |
| 5426 | |
| 5427 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5428 | EqualLoc); |
| 5429 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5430 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5431 | |
| 5432 | bool Constant = false; |
| 5433 | APValue Result; |
| 5434 | if (TopLevelOfInitList && |
| 5435 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5436 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5437 | Constant, Result); |
| 5438 | } |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5439 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5440 | } |