/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Sema/Initialization.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Initialization.h - Semantic Analysis for Initializers ----*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file provides supporting data types for initialization of objects. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #ifndef LLVM_CLANG_SEMA_INITIALIZATION_H |
14 | | #define LLVM_CLANG_SEMA_INITIALIZATION_H |
15 | | |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/Attr.h" |
18 | | #include "clang/AST/Decl.h" |
19 | | #include "clang/AST/DeclAccessPair.h" |
20 | | #include "clang/AST/DeclarationName.h" |
21 | | #include "clang/AST/Expr.h" |
22 | | #include "clang/AST/Type.h" |
23 | | #include "clang/Basic/IdentifierTable.h" |
24 | | #include "clang/Basic/LLVM.h" |
25 | | #include "clang/Basic/LangOptions.h" |
26 | | #include "clang/Basic/SourceLocation.h" |
27 | | #include "clang/Basic/Specifiers.h" |
28 | | #include "clang/Sema/Overload.h" |
29 | | #include "clang/Sema/Ownership.h" |
30 | | #include "llvm/ADT/ArrayRef.h" |
31 | | #include "llvm/ADT/SmallVector.h" |
32 | | #include "llvm/ADT/StringRef.h" |
33 | | #include "llvm/ADT/iterator_range.h" |
34 | | #include "llvm/Support/Casting.h" |
35 | | #include <cassert> |
36 | | #include <cstdint> |
37 | | #include <string> |
38 | | |
39 | | namespace clang { |
40 | | |
41 | | class CXXBaseSpecifier; |
42 | | class CXXConstructorDecl; |
43 | | class ObjCMethodDecl; |
44 | | class Sema; |
45 | | |
46 | | /// Describes an entity that is being initialized. |
47 | | class alignas(8) InitializedEntity { |
48 | | public: |
49 | | /// Specifies the kind of entity being initialized. |
50 | | enum EntityKind { |
51 | | /// The entity being initialized is a variable. |
52 | | EK_Variable, |
53 | | |
54 | | /// The entity being initialized is a function parameter. |
55 | | EK_Parameter, |
56 | | |
57 | | /// The entity being initialized is a non-type template parameter. |
58 | | EK_TemplateParameter, |
59 | | |
60 | | /// The entity being initialized is the result of a function call. |
61 | | EK_Result, |
62 | | |
63 | | /// The entity being initialized is the result of a statement expression. |
64 | | EK_StmtExprResult, |
65 | | |
66 | | /// The entity being initialized is an exception object that |
67 | | /// is being thrown. |
68 | | EK_Exception, |
69 | | |
70 | | /// The entity being initialized is a non-static data member |
71 | | /// subobject. |
72 | | EK_Member, |
73 | | |
74 | | /// The entity being initialized is an element of an array. |
75 | | EK_ArrayElement, |
76 | | |
77 | | /// The entity being initialized is an object (or array of |
78 | | /// objects) allocated via new. |
79 | | EK_New, |
80 | | |
81 | | /// The entity being initialized is a temporary object. |
82 | | EK_Temporary, |
83 | | |
84 | | /// The entity being initialized is a base member subobject. |
85 | | EK_Base, |
86 | | |
87 | | /// The initialization is being done by a delegating constructor. |
88 | | EK_Delegating, |
89 | | |
90 | | /// The entity being initialized is an element of a vector. |
91 | | /// or vector. |
92 | | EK_VectorElement, |
93 | | |
94 | | /// The entity being initialized is a field of block descriptor for |
95 | | /// the copied-in c++ object. |
96 | | EK_BlockElement, |
97 | | |
98 | | /// The entity being initialized is a field of block descriptor for the |
99 | | /// copied-in lambda object that's used in the lambda to block conversion. |
100 | | EK_LambdaToBlockConversionBlockElement, |
101 | | |
102 | | /// The entity being initialized is the real or imaginary part of a |
103 | | /// complex number. |
104 | | EK_ComplexElement, |
105 | | |
106 | | /// The entity being initialized is the field that captures a |
107 | | /// variable in a lambda. |
108 | | EK_LambdaCapture, |
109 | | |
110 | | /// The entity being initialized is the initializer for a compound |
111 | | /// literal. |
112 | | EK_CompoundLiteralInit, |
113 | | |
114 | | /// The entity being implicitly initialized back to the formal |
115 | | /// result type. |
116 | | EK_RelatedResult, |
117 | | |
118 | | /// The entity being initialized is a function parameter; function |
119 | | /// is member of group of audited CF APIs. |
120 | | EK_Parameter_CF_Audited, |
121 | | |
122 | | /// The entity being initialized is a structured binding of a |
123 | | /// decomposition declaration. |
124 | | EK_Binding, |
125 | | |
126 | | // Note: err_init_conversion_failed in DiagnosticSemaKinds.td uses this |
127 | | // enum as an index for its first %select. When modifying this list, |
128 | | // that diagnostic text needs to be updated as well. |
129 | | }; |
130 | | |
131 | | private: |
132 | | /// The kind of entity being initialized. |
133 | | EntityKind Kind; |
134 | | |
135 | | /// If non-NULL, the parent entity in which this |
136 | | /// initialization occurs. |
137 | | const InitializedEntity *Parent = nullptr; |
138 | | |
139 | | /// The type of the object or reference being initialized. |
140 | | QualType Type; |
141 | | |
142 | | /// The mangling number for the next reference temporary to be created. |
143 | | mutable unsigned ManglingNumber = 0; |
144 | | |
145 | | struct LN { |
146 | | /// When Kind == EK_Result, EK_Exception, EK_New, the |
147 | | /// location of the 'return', 'throw', or 'new' keyword, |
148 | | /// respectively. When Kind == EK_Temporary, the location where |
149 | | /// the temporary is being created. |
150 | | SourceLocation Location; |
151 | | |
152 | | /// Whether the entity being initialized may end up using the |
153 | | /// named return value optimization (NRVO). |
154 | | bool NRVO; |
155 | | }; |
156 | | |
157 | | struct VD { |
158 | | /// The VarDecl, FieldDecl, or BindingDecl being initialized. |
159 | | ValueDecl *VariableOrMember; |
160 | | |
161 | | /// When Kind == EK_Member, whether this is an implicit member |
162 | | /// initialization in a copy or move constructor. These can perform array |
163 | | /// copies. |
164 | | bool IsImplicitFieldInit; |
165 | | |
166 | | /// When Kind == EK_Member, whether this is the initial initialization |
167 | | /// check for a default member initializer. |
168 | | bool IsDefaultMemberInit; |
169 | | }; |
170 | | |
171 | | struct C { |
172 | | /// The name of the variable being captured by an EK_LambdaCapture. |
173 | | IdentifierInfo *VarID; |
174 | | |
175 | | /// The source location at which the capture occurs. |
176 | | SourceLocation Location; |
177 | | }; |
178 | | |
179 | | union { |
180 | | /// When Kind == EK_Variable, EK_Member, EK_Binding, or |
181 | | /// EK_TemplateParameter, the variable, binding, or template parameter. |
182 | | VD Variable; |
183 | | |
184 | | /// When Kind == EK_RelatedResult, the ObjectiveC method where |
185 | | /// result type was implicitly changed to accommodate ARC semantics. |
186 | | ObjCMethodDecl *MethodDecl; |
187 | | |
188 | | /// When Kind == EK_Parameter, the ParmVarDecl, with the |
189 | | /// integer indicating whether the parameter is "consumed". |
190 | | llvm::PointerIntPair<ParmVarDecl *, 1> Parameter; |
191 | | |
192 | | /// When Kind == EK_Temporary or EK_CompoundLiteralInit, the type |
193 | | /// source information for the temporary. |
194 | | TypeSourceInfo *TypeInfo; |
195 | | |
196 | | struct LN LocAndNRVO; |
197 | | |
198 | | /// When Kind == EK_Base, the base specifier that provides the |
199 | | /// base class. The integer specifies whether the base is an inherited |
200 | | /// virtual base. |
201 | | llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base; |
202 | | |
203 | | /// When Kind == EK_ArrayElement, EK_VectorElement, or |
204 | | /// EK_ComplexElement, the index of the array or vector element being |
205 | | /// initialized. |
206 | | unsigned Index; |
207 | | |
208 | | struct C Capture; |
209 | | }; |
210 | | |
211 | 6.49M | InitializedEntity() {}; |
212 | | |
213 | | /// Create the initialization entity for a variable. |
214 | | InitializedEntity(VarDecl *Var, EntityKind EK = EK_Variable) |
215 | 2.65M | : Kind(EK), Type(Var->getType()), Variable{Var, false, false} {} |
216 | | |
217 | | /// Create the initialization entity for the result of a |
218 | | /// function, throwing an object, performing an explicit cast, or |
219 | | /// initializing a parameter for which there is no declaration. |
220 | | InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type, |
221 | | bool NRVO = false) |
222 | 4.40M | : Kind(Kind), Type(Type) { |
223 | 4.40M | new (&LocAndNRVO) LN; |
224 | 4.40M | LocAndNRVO.Location = Loc; |
225 | 4.40M | LocAndNRVO.NRVO = NRVO; |
226 | 4.40M | } |
227 | | |
228 | | /// Create the initialization entity for a member subobject. |
229 | | InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent, |
230 | | bool Implicit, bool DefaultMemberInit) |
231 | | : Kind(EK_Member), Parent(Parent), Type(Member->getType()), |
232 | 241k | Variable{Member, Implicit, DefaultMemberInit} {} |
233 | | |
234 | | /// Create the initialization entity for an array element. |
235 | | InitializedEntity(ASTContext &Context, unsigned Index, |
236 | | const InitializedEntity &Parent); |
237 | | |
238 | | /// Create the initialization entity for a lambda capture. |
239 | | InitializedEntity(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc) |
240 | 6.27k | : Kind(EK_LambdaCapture), Type(FieldType) { |
241 | 6.27k | new (&Capture) C; |
242 | 6.27k | Capture.VarID = VarID; |
243 | 6.27k | Capture.Location = Loc; |
244 | 6.27k | } |
245 | | |
246 | | public: |
247 | | /// Create the initialization entity for a variable. |
248 | 2.65M | static InitializedEntity InitializeVariable(VarDecl *Var) { |
249 | 2.65M | return InitializedEntity(Var); |
250 | 2.65M | } |
251 | | |
252 | | /// Create the initialization entity for a parameter. |
253 | | static InitializedEntity InitializeParameter(ASTContext &Context, |
254 | 228k | ParmVarDecl *Parm) { |
255 | 228k | return InitializeParameter(Context, Parm, Parm->getType()); |
256 | 228k | } |
257 | | |
258 | | /// Create the initialization entity for a parameter, but use |
259 | | /// another type. |
260 | | static InitializedEntity |
261 | 6.39M | InitializeParameter(ASTContext &Context, ParmVarDecl *Parm, QualType Type) { |
262 | 6.39M | bool Consumed = (Context.getLangOpts().ObjCAutoRefCount && |
263 | 6.39M | Parm->hasAttr<NSConsumedAttr>()18.1k ); |
264 | | |
265 | 6.39M | InitializedEntity Entity; |
266 | 6.39M | Entity.Kind = EK_Parameter; |
267 | 6.39M | Entity.Type = |
268 | 6.39M | Context.getVariableArrayDecayedType(Type.getUnqualifiedType()); |
269 | 6.39M | Entity.Parent = nullptr; |
270 | 6.39M | Entity.Parameter = {Parm, Consumed}; |
271 | 6.39M | return Entity; |
272 | 6.39M | } |
273 | | |
274 | | /// Create the initialization entity for a parameter that is |
275 | | /// only known by its type. |
276 | | static InitializedEntity InitializeParameter(ASTContext &Context, |
277 | | QualType Type, |
278 | 55.6k | bool Consumed) { |
279 | 55.6k | InitializedEntity Entity; |
280 | 55.6k | Entity.Kind = EK_Parameter; |
281 | 55.6k | Entity.Type = Context.getVariableArrayDecayedType(Type); |
282 | 55.6k | Entity.Parent = nullptr; |
283 | 55.6k | Entity.Parameter = {nullptr, Consumed}; |
284 | 55.6k | return Entity; |
285 | 55.6k | } |
286 | | |
287 | | /// Create the initialization entity for a template parameter. |
288 | | static InitializedEntity |
289 | 499 | InitializeTemplateParameter(QualType T, NonTypeTemplateParmDecl *Param) { |
290 | 499 | InitializedEntity Entity; |
291 | 499 | Entity.Kind = EK_TemplateParameter; |
292 | 499 | Entity.Type = T; |
293 | 499 | Entity.Parent = nullptr; |
294 | 499 | Entity.Variable = {Param, false, false}; |
295 | 499 | return Entity; |
296 | 499 | } |
297 | | |
298 | | /// Create the initialization entity for the result of a function. |
299 | | static InitializedEntity InitializeResult(SourceLocation ReturnLoc, |
300 | 2.54M | QualType Type) { |
301 | 2.54M | return InitializedEntity(EK_Result, ReturnLoc, Type); |
302 | 2.54M | } |
303 | | |
304 | | static InitializedEntity InitializeStmtExprResult(SourceLocation ReturnLoc, |
305 | 7.67k | QualType Type) { |
306 | 7.67k | return InitializedEntity(EK_StmtExprResult, ReturnLoc, Type); |
307 | 7.67k | } |
308 | | |
309 | | static InitializedEntity InitializeBlock(SourceLocation BlockVarLoc, |
310 | 477 | QualType Type) { |
311 | 477 | return InitializedEntity(EK_BlockElement, BlockVarLoc, Type); |
312 | 477 | } |
313 | | |
314 | | static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc, |
315 | 31 | QualType Type) { |
316 | 31 | return InitializedEntity(EK_LambdaToBlockConversionBlockElement, |
317 | 31 | BlockVarLoc, Type); |
318 | 31 | } |
319 | | |
320 | | /// Create the initialization entity for an exception object. |
321 | | static InitializedEntity InitializeException(SourceLocation ThrowLoc, |
322 | 7.15k | QualType Type) { |
323 | 7.15k | return InitializedEntity(EK_Exception, ThrowLoc, Type); |
324 | 7.15k | } |
325 | | |
326 | | /// Create the initialization entity for an object allocated via new. |
327 | 5.34k | static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) { |
328 | 5.34k | return InitializedEntity(EK_New, NewLoc, Type); |
329 | 5.34k | } |
330 | | |
331 | | /// Create the initialization entity for a temporary. |
332 | 1.24M | static InitializedEntity InitializeTemporary(QualType Type) { |
333 | 1.24M | return InitializeTemporary(nullptr, Type); |
334 | 1.24M | } |
335 | | |
336 | | /// Create the initialization entity for a temporary. |
337 | | static InitializedEntity InitializeTemporary(ASTContext &Context, |
338 | 535k | TypeSourceInfo *TypeInfo) { |
339 | 535k | QualType Type = TypeInfo->getType(); |
340 | 535k | if (Context.getLangOpts().OpenCLCPlusPlus) { |
341 | 7 | assert(!Type.hasAddressSpace() && "Temporary already has address space!"); |
342 | 0 | Type = Context.getAddrSpaceQualType(Type, LangAS::opencl_private); |
343 | 7 | } |
344 | | |
345 | 0 | return InitializeTemporary(TypeInfo, Type); |
346 | 535k | } |
347 | | |
348 | | /// Create the initialization entity for a temporary. |
349 | | static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo, |
350 | 1.77M | QualType Type) { |
351 | 1.77M | InitializedEntity Result(EK_Temporary, SourceLocation(), Type); |
352 | 1.77M | Result.TypeInfo = TypeInfo; |
353 | 1.77M | return Result; |
354 | 1.77M | } |
355 | | |
356 | | /// Create the initialization entity for a related result. |
357 | | static InitializedEntity InitializeRelatedResult(ObjCMethodDecl *MD, |
358 | 925 | QualType Type) { |
359 | 925 | InitializedEntity Result(EK_RelatedResult, SourceLocation(), Type); |
360 | 925 | Result.MethodDecl = MD; |
361 | 925 | return Result; |
362 | 925 | } |
363 | | |
364 | | /// Create the initialization entity for a base class subobject. |
365 | | static InitializedEntity |
366 | | InitializeBase(ASTContext &Context, const CXXBaseSpecifier *Base, |
367 | | bool IsInheritedVirtualBase, |
368 | | const InitializedEntity *Parent = nullptr); |
369 | | |
370 | | /// Create the initialization entity for a delegated constructor. |
371 | 544 | static InitializedEntity InitializeDelegation(QualType Type) { |
372 | 544 | return InitializedEntity(EK_Delegating, SourceLocation(), Type); |
373 | 544 | } |
374 | | |
375 | | /// Create the initialization entity for a member subobject. |
376 | | static InitializedEntity |
377 | | InitializeMember(FieldDecl *Member, |
378 | | const InitializedEntity *Parent = nullptr, |
379 | 237k | bool Implicit = false) { |
380 | 237k | return InitializedEntity(Member, Parent, Implicit, false); |
381 | 237k | } |
382 | | |
383 | | /// Create the initialization entity for a member subobject. |
384 | | static InitializedEntity |
385 | | InitializeMember(IndirectFieldDecl *Member, |
386 | | const InitializedEntity *Parent = nullptr, |
387 | 285 | bool Implicit = false) { |
388 | 285 | return InitializedEntity(Member->getAnonField(), Parent, Implicit, false); |
389 | 285 | } |
390 | | |
391 | | /// Create the initialization entity for a default member initializer. |
392 | | static InitializedEntity |
393 | 4.15k | InitializeMemberFromDefaultMemberInitializer(FieldDecl *Member) { |
394 | 4.15k | return InitializedEntity(Member, nullptr, false, true); |
395 | 4.15k | } |
396 | | |
397 | | /// Create the initialization entity for an array element. |
398 | | static InitializedEntity InitializeElement(ASTContext &Context, |
399 | | unsigned Index, |
400 | 307k | const InitializedEntity &Parent) { |
401 | 307k | return InitializedEntity(Context, Index, Parent); |
402 | 307k | } |
403 | | |
404 | | /// Create the initialization entity for a structured binding. |
405 | 100 | static InitializedEntity InitializeBinding(VarDecl *Binding) { |
406 | 100 | return InitializedEntity(Binding, EK_Binding); |
407 | 100 | } |
408 | | |
409 | | /// Create the initialization entity for a lambda capture. |
410 | | /// |
411 | | /// \p VarID The name of the entity being captured, or nullptr for 'this'. |
412 | | static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID, |
413 | | QualType FieldType, |
414 | 6.27k | SourceLocation Loc) { |
415 | 6.27k | return InitializedEntity(VarID, FieldType, Loc); |
416 | 6.27k | } |
417 | | |
418 | | /// Create the entity for a compound literal initializer. |
419 | 55.6k | static InitializedEntity InitializeCompoundLiteralInit(TypeSourceInfo *TSI) { |
420 | 55.6k | InitializedEntity Result(EK_CompoundLiteralInit, SourceLocation(), |
421 | 55.6k | TSI->getType()); |
422 | 55.6k | Result.TypeInfo = TSI; |
423 | 55.6k | return Result; |
424 | 55.6k | } |
425 | | |
426 | | /// Determine the kind of initialization. |
427 | 87.0M | EntityKind getKind() const { return Kind; } |
428 | | |
429 | | /// Retrieve the parent of the entity being initialized, when |
430 | | /// the initialization itself is occurring within the context of a |
431 | | /// larger initialization. |
432 | 321k | const InitializedEntity *getParent() const { return Parent; } |
433 | | |
434 | | /// Retrieve type being initialized. |
435 | 48.3M | QualType getType() const { return Type; } |
436 | | |
437 | | /// Retrieve complete type-source information for the object being |
438 | | /// constructed, if known. |
439 | 94.3k | TypeSourceInfo *getTypeSourceInfo() const { |
440 | 94.3k | if (Kind == EK_Temporary || Kind == EK_CompoundLiteralInit8 ) |
441 | 94.3k | return TypeInfo; |
442 | | |
443 | 0 | return nullptr; |
444 | 94.3k | } |
445 | | |
446 | | /// Retrieve the name of the entity being initialized. |
447 | | DeclarationName getName() const; |
448 | | |
449 | | /// Retrieve the variable, parameter, or field being |
450 | | /// initialized. |
451 | | ValueDecl *getDecl() const; |
452 | | |
453 | | /// Retrieve the ObjectiveC method being initialized. |
454 | 6 | ObjCMethodDecl *getMethodDecl() const { return MethodDecl; } |
455 | | |
456 | | /// Determine whether this initialization allows the named return |
457 | | /// value optimization, which also applies to thrown objects. |
458 | | bool allowsNRVO() const; |
459 | | |
460 | 9.32M | bool isParameterKind() const { |
461 | 9.32M | return (getKind() == EK_Parameter || |
462 | 9.32M | getKind() == EK_Parameter_CF_Audited2.92M ); |
463 | 9.32M | } |
464 | | |
465 | 7.81k | bool isParamOrTemplateParamKind() const { |
466 | 7.81k | return isParameterKind() || getKind() == EK_TemplateParameter5.90k ; |
467 | 7.81k | } |
468 | | |
469 | | /// Determine whether this initialization consumes the |
470 | | /// parameter. |
471 | 17.9k | bool isParameterConsumed() const { |
472 | 17.9k | assert(isParameterKind() && "Not a parameter"); |
473 | 0 | return Parameter.getInt(); |
474 | 17.9k | } |
475 | | |
476 | | /// Retrieve the base specifier. |
477 | 44.2k | const CXXBaseSpecifier *getBaseSpecifier() const { |
478 | 44.2k | assert(getKind() == EK_Base && "Not a base specifier"); |
479 | 0 | return Base.getPointer(); |
480 | 44.2k | } |
481 | | |
482 | | /// Return whether the base is an inherited virtual base. |
483 | 2.46k | bool isInheritedVirtualBase() const { |
484 | 2.46k | assert(getKind() == EK_Base && "Not a base specifier"); |
485 | 0 | return Base.getInt(); |
486 | 2.46k | } |
487 | | |
488 | | /// Determine whether this is an array new with an unknown bound. |
489 | 35.4k | bool isVariableLengthArrayNew() const { |
490 | 35.4k | return getKind() == EK_New && isa_and_nonnull<IncompleteArrayType>( |
491 | 173 | getType()->getAsArrayTypeUnsafe()); |
492 | 35.4k | } |
493 | | |
494 | | /// Is this the implicit initialization of a member of a class from |
495 | | /// a defaulted constructor? |
496 | 930 | bool isImplicitMemberInitializer() const { |
497 | 930 | return getKind() == EK_Member && Variable.IsImplicitFieldInit; |
498 | 930 | } |
499 | | |
500 | | /// Is this the default member initializer of a member (specified inside |
501 | | /// the class definition)? |
502 | 117k | bool isDefaultMemberInitializer() const { |
503 | 117k | return getKind() == EK_Member && Variable.IsDefaultMemberInit; |
504 | 117k | } |
505 | | |
506 | | /// Determine the location of the 'return' keyword when initializing |
507 | | /// the result of a function call. |
508 | 1.32k | SourceLocation getReturnLoc() const { |
509 | 1.32k | assert(getKind() == EK_Result && "No 'return' location!"); |
510 | 0 | return LocAndNRVO.Location; |
511 | 1.32k | } |
512 | | |
513 | | /// Determine the location of the 'throw' keyword when initializing |
514 | | /// an exception object. |
515 | 0 | SourceLocation getThrowLoc() const { |
516 | 0 | assert(getKind() == EK_Exception && "No 'throw' location!"); |
517 | 0 | return LocAndNRVO.Location; |
518 | 0 | } |
519 | | |
520 | | /// If this is an array, vector, or complex number element, get the |
521 | | /// element's index. |
522 | 17 | unsigned getElementIndex() const { |
523 | 17 | assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement || |
524 | 17 | getKind() == EK_ComplexElement); |
525 | 0 | return Index; |
526 | 17 | } |
527 | | |
528 | | /// If this is already the initializer for an array or vector |
529 | | /// element, sets the element index. |
530 | 1.11M | void setElementIndex(unsigned Index) { |
531 | 1.11M | assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement || |
532 | 1.11M | getKind() == EK_ComplexElement); |
533 | 0 | this->Index = Index; |
534 | 1.11M | } |
535 | | |
536 | | /// For a lambda capture, return the capture's name. |
537 | 2 | StringRef getCapturedVarName() const { |
538 | 2 | assert(getKind() == EK_LambdaCapture && "Not a lambda capture!"); |
539 | 2 | return Capture.VarID ? Capture.VarID->getName() : "this"0 ; |
540 | 2 | } |
541 | | |
542 | | /// Determine the location of the capture when initializing |
543 | | /// field from a captured variable in a lambda. |
544 | 0 | SourceLocation getCaptureLoc() const { |
545 | 0 | assert(getKind() == EK_LambdaCapture && "Not a lambda capture!"); |
546 | 0 | return Capture.Location; |
547 | 0 | } |
548 | | |
549 | 30 | void setParameterCFAudited() { |
550 | 30 | Kind = EK_Parameter_CF_Audited; |
551 | 30 | } |
552 | | |
553 | 2.41k | unsigned allocateManglingNumber() const { return ++ManglingNumber; } |
554 | | |
555 | | /// Dump a representation of the initialized entity to standard error, |
556 | | /// for debugging purposes. |
557 | | void dump() const; |
558 | | |
559 | | private: |
560 | | unsigned dumpImpl(raw_ostream &OS) const; |
561 | | }; |
562 | | |
563 | | /// Describes the kind of initialization being performed, along with |
564 | | /// location information for tokens related to the initialization (equal sign, |
565 | | /// parentheses). |
566 | | class InitializationKind { |
567 | | public: |
568 | | /// The kind of initialization being performed. |
569 | | enum InitKind { |
570 | | /// Direct initialization |
571 | | IK_Direct, |
572 | | |
573 | | /// Direct list-initialization |
574 | | IK_DirectList, |
575 | | |
576 | | /// Copy initialization |
577 | | IK_Copy, |
578 | | |
579 | | /// Default initialization |
580 | | IK_Default, |
581 | | |
582 | | /// Value initialization |
583 | | IK_Value |
584 | | }; |
585 | | |
586 | | private: |
587 | | /// The context of the initialization. |
588 | | enum InitContext { |
589 | | /// Normal context |
590 | | IC_Normal, |
591 | | |
592 | | /// Normal context, but allows explicit conversion functionss |
593 | | IC_ExplicitConvs, |
594 | | |
595 | | /// Implicit context (value initialization) |
596 | | IC_Implicit, |
597 | | |
598 | | /// Static cast context |
599 | | IC_StaticCast, |
600 | | |
601 | | /// C-style cast context |
602 | | IC_CStyleCast, |
603 | | |
604 | | /// Functional cast context |
605 | | IC_FunctionalCast |
606 | | }; |
607 | | |
608 | | /// The kind of initialization being performed. |
609 | | InitKind Kind : 8; |
610 | | |
611 | | /// The context of the initialization. |
612 | | InitContext Context : 8; |
613 | | |
614 | | /// The source locations involved in the initialization. |
615 | | SourceLocation Locations[3]; |
616 | | |
617 | | InitializationKind(InitKind Kind, InitContext Context, SourceLocation Loc1, |
618 | | SourceLocation Loc2, SourceLocation Loc3) |
619 | 14.1M | : Kind(Kind), Context(Context) { |
620 | 14.1M | Locations[0] = Loc1; |
621 | 14.1M | Locations[1] = Loc2; |
622 | 14.1M | Locations[2] = Loc3; |
623 | 14.1M | } |
624 | | |
625 | | public: |
626 | | /// Create a direct initialization. |
627 | | static InitializationKind CreateDirect(SourceLocation InitLoc, |
628 | | SourceLocation LParenLoc, |
629 | 583k | SourceLocation RParenLoc) { |
630 | 583k | return InitializationKind(IK_Direct, IC_Normal, |
631 | 583k | InitLoc, LParenLoc, RParenLoc); |
632 | 583k | } |
633 | | |
634 | 58 | static InitializationKind CreateDirectList(SourceLocation InitLoc) { |
635 | 58 | return InitializationKind(IK_DirectList, IC_Normal, InitLoc, InitLoc, |
636 | 58 | InitLoc); |
637 | 58 | } |
638 | | |
639 | | static InitializationKind CreateDirectList(SourceLocation InitLoc, |
640 | | SourceLocation LBraceLoc, |
641 | 21.7k | SourceLocation RBraceLoc) { |
642 | 21.7k | return InitializationKind(IK_DirectList, IC_Normal, InitLoc, LBraceLoc, |
643 | 21.7k | RBraceLoc); |
644 | 21.7k | } |
645 | | |
646 | | /// Create a direct initialization due to a cast that isn't a C-style |
647 | | /// or functional cast. |
648 | 77.8k | static InitializationKind CreateCast(SourceRange TypeRange) { |
649 | 77.8k | return InitializationKind(IK_Direct, IC_StaticCast, TypeRange.getBegin(), |
650 | 77.8k | TypeRange.getBegin(), TypeRange.getEnd()); |
651 | 77.8k | } |
652 | | |
653 | | /// Create a direct initialization for a C-style cast. |
654 | | static InitializationKind CreateCStyleCast(SourceLocation StartLoc, |
655 | | SourceRange TypeRange, |
656 | 602k | bool InitList) { |
657 | | // C++ cast syntax doesn't permit init lists, but C compound literals are |
658 | | // exactly that. |
659 | 602k | return InitializationKind(InitList ? IK_DirectList55.6k : IK_Direct546k , |
660 | 602k | IC_CStyleCast, StartLoc, TypeRange.getBegin(), |
661 | 602k | TypeRange.getEnd()); |
662 | 602k | } |
663 | | |
664 | | /// Create a direct initialization for a functional cast. |
665 | | static InitializationKind CreateFunctionalCast(SourceRange TypeRange, |
666 | 68.7k | bool InitList) { |
667 | 68.7k | return InitializationKind(InitList ? IK_DirectList0 : IK_Direct, |
668 | 68.7k | IC_FunctionalCast, TypeRange.getBegin(), |
669 | 68.7k | TypeRange.getBegin(), TypeRange.getEnd()); |
670 | 68.7k | } |
671 | | |
672 | | /// Create a copy initialization. |
673 | | static InitializationKind CreateCopy(SourceLocation InitLoc, |
674 | | SourceLocation EqualLoc, |
675 | 11.8M | bool AllowExplicitConvs = false) { |
676 | 11.8M | return InitializationKind(IK_Copy, |
677 | 11.8M | AllowExplicitConvs? IC_ExplicitConvs27.9k : IC_Normal11.8M , |
678 | 11.8M | InitLoc, EqualLoc, EqualLoc); |
679 | 11.8M | } |
680 | | |
681 | | /// Create a default initialization. |
682 | 631k | static InitializationKind CreateDefault(SourceLocation InitLoc) { |
683 | 631k | return InitializationKind(IK_Default, IC_Normal, InitLoc, InitLoc, InitLoc); |
684 | 631k | } |
685 | | |
686 | | /// Create a value initialization. |
687 | | static InitializationKind CreateValue(SourceLocation InitLoc, |
688 | | SourceLocation LParenLoc, |
689 | | SourceLocation RParenLoc, |
690 | 230k | bool isImplicit = false) { |
691 | 230k | return InitializationKind(IK_Value, isImplicit ? IC_Implicit33.1k : IC_Normal197k , |
692 | 230k | InitLoc, LParenLoc, RParenLoc); |
693 | 230k | } |
694 | | |
695 | | /// Create an initialization from an initializer (which, for direct |
696 | | /// initialization from a parenthesized list, will be a ParenListExpr). |
697 | | static InitializationKind CreateForInit(SourceLocation Loc, bool DirectInit, |
698 | 2.04M | Expr *Init) { |
699 | 2.04M | if (!Init) return CreateDefault(Loc)7 ; |
700 | 2.04M | if (!DirectInit) |
701 | 1.94M | return CreateCopy(Loc, Init->getBeginLoc()); |
702 | 100k | if (isa<InitListExpr>(Init)) |
703 | 5.96k | return CreateDirectList(Loc, Init->getBeginLoc(), Init->getEndLoc()); |
704 | 94.2k | return CreateDirect(Loc, Init->getBeginLoc(), Init->getEndLoc()); |
705 | 100k | } |
706 | | |
707 | | /// Determine the initialization kind. |
708 | 54.1M | InitKind getKind() const { |
709 | 54.1M | return Kind; |
710 | 54.1M | } |
711 | | |
712 | | /// Determine whether this initialization is an explicit cast. |
713 | 4.15M | bool isExplicitCast() const { |
714 | 4.15M | return Context >= IC_StaticCast; |
715 | 4.15M | } |
716 | | |
717 | | /// Determine whether this initialization is a static cast. |
718 | 6 | bool isStaticCast() const { return Context == IC_StaticCast; } |
719 | | |
720 | | /// Determine whether this initialization is a C-style cast. |
721 | 4.83M | bool isCStyleOrFunctionalCast() const { |
722 | 4.83M | return Context >= IC_CStyleCast; |
723 | 4.83M | } |
724 | | |
725 | | /// Determine whether this is a C-style cast. |
726 | 4.61M | bool isCStyleCast() const { |
727 | 4.61M | return Context == IC_CStyleCast; |
728 | 4.61M | } |
729 | | |
730 | | /// Determine whether this is a functional-style cast. |
731 | 4.13M | bool isFunctionalCast() const { |
732 | 4.13M | return Context == IC_FunctionalCast; |
733 | 4.13M | } |
734 | | |
735 | | /// Determine whether this initialization is an implicit |
736 | | /// value-initialization, e.g., as occurs during aggregate |
737 | | /// initialization. |
738 | 28.5k | bool isImplicitValueInit() const { return Context == IC_Implicit; } |
739 | | |
740 | | /// Retrieve the location at which initialization is occurring. |
741 | 21.6M | SourceLocation getLocation() const { return Locations[0]; } |
742 | | |
743 | | /// Retrieve the source range that covers the initialization. |
744 | 14.3k | SourceRange getRange() const { |
745 | 14.3k | return SourceRange(Locations[0], Locations[2]); |
746 | 14.3k | } |
747 | | |
748 | | /// Retrieve the location of the equal sign for copy initialization |
749 | | /// (if present). |
750 | 235k | SourceLocation getEqualLoc() const { |
751 | 235k | assert(Kind == IK_Copy && "Only copy initialization has an '='"); |
752 | 0 | return Locations[1]; |
753 | 235k | } |
754 | | |
755 | 1.42M | bool isCopyInit() const { return Kind == IK_Copy; } |
756 | | |
757 | | /// Retrieve whether this initialization allows the use of explicit |
758 | | /// constructors. |
759 | 781k | bool AllowExplicit() const { return !isCopyInit(); } |
760 | | |
761 | | /// Retrieve whether this initialization allows the use of explicit |
762 | | /// conversion functions when binding a reference. If the reference is the |
763 | | /// first parameter in a copy or move constructor, such conversions are |
764 | | /// permitted even though we are performing copy-initialization. |
765 | 2.17k | bool allowExplicitConversionFunctionsInRefBinding() const { |
766 | 2.17k | return !isCopyInit() || Context == IC_ExplicitConvs2.00k ; |
767 | 2.17k | } |
768 | | |
769 | | /// Determine whether this initialization has a source range containing the |
770 | | /// locations of open and closing parentheses or braces. |
771 | 780k | bool hasParenOrBraceRange() const { |
772 | 780k | return Kind == IK_Direct || Kind == IK_Value412k || Kind == IK_DirectList204k ; |
773 | 780k | } |
774 | | |
775 | | /// Retrieve the source range containing the locations of the open |
776 | | /// and closing parentheses or braces for value, direct, and direct list |
777 | | /// initializations. |
778 | 405k | SourceRange getParenOrBraceRange() const { |
779 | 405k | assert(hasParenOrBraceRange() && "Only direct, value, and direct-list " |
780 | 405k | "initialization have parentheses or " |
781 | 405k | "braces"); |
782 | 0 | return SourceRange(Locations[1], Locations[2]); |
783 | 405k | } |
784 | | }; |
785 | | |
786 | | /// Describes the sequence of initializations required to initialize |
787 | | /// a given object or reference with a set of arguments. |
788 | | class InitializationSequence { |
789 | | public: |
790 | | /// Describes the kind of initialization sequence computed. |
791 | | enum SequenceKind { |
792 | | /// A failed initialization sequence. The failure kind tells what |
793 | | /// happened. |
794 | | FailedSequence = 0, |
795 | | |
796 | | /// A dependent initialization, which could not be |
797 | | /// type-checked due to the presence of dependent types or |
798 | | /// dependently-typed expressions. |
799 | | DependentSequence, |
800 | | |
801 | | /// A normal sequence. |
802 | | NormalSequence |
803 | | }; |
804 | | |
805 | | /// Describes the kind of a particular step in an initialization |
806 | | /// sequence. |
807 | | enum StepKind { |
808 | | /// Resolve the address of an overloaded function to a specific |
809 | | /// function declaration. |
810 | | SK_ResolveAddressOfOverloadedFunction, |
811 | | |
812 | | /// Perform a derived-to-base cast, producing an rvalue. |
813 | | SK_CastDerivedToBasePRValue, |
814 | | |
815 | | /// Perform a derived-to-base cast, producing an xvalue. |
816 | | SK_CastDerivedToBaseXValue, |
817 | | |
818 | | /// Perform a derived-to-base cast, producing an lvalue. |
819 | | SK_CastDerivedToBaseLValue, |
820 | | |
821 | | /// Reference binding to an lvalue. |
822 | | SK_BindReference, |
823 | | |
824 | | /// Reference binding to a temporary. |
825 | | SK_BindReferenceToTemporary, |
826 | | |
827 | | /// An optional copy of a temporary object to another |
828 | | /// temporary object, which is permitted (but not required) by |
829 | | /// C++98/03 but not C++0x. |
830 | | SK_ExtraneousCopyToTemporary, |
831 | | |
832 | | /// Direct-initialization from a reference-related object in the |
833 | | /// final stage of class copy-initialization. |
834 | | SK_FinalCopy, |
835 | | |
836 | | /// Perform a user-defined conversion, either via a conversion |
837 | | /// function or via a constructor. |
838 | | SK_UserConversion, |
839 | | |
840 | | /// Perform a qualification conversion, producing a prvalue. |
841 | | SK_QualificationConversionPRValue, |
842 | | |
843 | | /// Perform a qualification conversion, producing an xvalue. |
844 | | SK_QualificationConversionXValue, |
845 | | |
846 | | /// Perform a qualification conversion, producing an lvalue. |
847 | | SK_QualificationConversionLValue, |
848 | | |
849 | | /// Perform a function reference conversion, see [dcl.init.ref]p4. |
850 | | SK_FunctionReferenceConversion, |
851 | | |
852 | | /// Perform a conversion adding _Atomic to a type. |
853 | | SK_AtomicConversion, |
854 | | |
855 | | /// Perform an implicit conversion sequence. |
856 | | SK_ConversionSequence, |
857 | | |
858 | | /// Perform an implicit conversion sequence without narrowing. |
859 | | SK_ConversionSequenceNoNarrowing, |
860 | | |
861 | | /// Perform list-initialization without a constructor. |
862 | | SK_ListInitialization, |
863 | | |
864 | | /// Unwrap the single-element initializer list for a reference. |
865 | | SK_UnwrapInitList, |
866 | | |
867 | | /// Rewrap the single-element initializer list for a reference. |
868 | | SK_RewrapInitList, |
869 | | |
870 | | /// Perform initialization via a constructor. |
871 | | SK_ConstructorInitialization, |
872 | | |
873 | | /// Perform initialization via a constructor, taking arguments from |
874 | | /// a single InitListExpr. |
875 | | SK_ConstructorInitializationFromList, |
876 | | |
877 | | /// Zero-initialize the object |
878 | | SK_ZeroInitialization, |
879 | | |
880 | | /// C assignment |
881 | | SK_CAssignment, |
882 | | |
883 | | /// Initialization by string |
884 | | SK_StringInit, |
885 | | |
886 | | /// An initialization that "converts" an Objective-C object |
887 | | /// (not a point to an object) to another Objective-C object type. |
888 | | SK_ObjCObjectConversion, |
889 | | |
890 | | /// Array indexing for initialization by elementwise copy. |
891 | | SK_ArrayLoopIndex, |
892 | | |
893 | | /// Array initialization by elementwise copy. |
894 | | SK_ArrayLoopInit, |
895 | | |
896 | | /// Array initialization (from an array rvalue). |
897 | | SK_ArrayInit, |
898 | | |
899 | | /// Array initialization (from an array rvalue) as a GNU extension. |
900 | | SK_GNUArrayInit, |
901 | | |
902 | | /// Array initialization from a parenthesized initializer list. |
903 | | /// This is a GNU C++ extension. |
904 | | SK_ParenthesizedArrayInit, |
905 | | |
906 | | /// Pass an object by indirect copy-and-restore. |
907 | | SK_PassByIndirectCopyRestore, |
908 | | |
909 | | /// Pass an object by indirect restore. |
910 | | SK_PassByIndirectRestore, |
911 | | |
912 | | /// Produce an Objective-C object pointer. |
913 | | SK_ProduceObjCObject, |
914 | | |
915 | | /// Construct a std::initializer_list from an initializer list. |
916 | | SK_StdInitializerList, |
917 | | |
918 | | /// Perform initialization via a constructor taking a single |
919 | | /// std::initializer_list argument. |
920 | | SK_StdInitializerListConstructorCall, |
921 | | |
922 | | /// Initialize an OpenCL sampler from an integer. |
923 | | SK_OCLSamplerInit, |
924 | | |
925 | | /// Initialize an opaque OpenCL type (event_t, queue_t, etc.) with zero |
926 | | SK_OCLZeroOpaqueType |
927 | | }; |
928 | | |
929 | | /// A single step in the initialization sequence. |
930 | | class Step { |
931 | | public: |
932 | | /// The kind of conversion or initialization step we are taking. |
933 | | StepKind Kind; |
934 | | |
935 | | // The type that results from this initialization. |
936 | | QualType Type; |
937 | | |
938 | | struct F { |
939 | | bool HadMultipleCandidates; |
940 | | FunctionDecl *Function; |
941 | | DeclAccessPair FoundDecl; |
942 | | }; |
943 | | |
944 | | union { |
945 | | /// When Kind == SK_ResolvedOverloadedFunction or Kind == |
946 | | /// SK_UserConversion, the function that the expression should be |
947 | | /// resolved to or the conversion function to call, respectively. |
948 | | /// When Kind == SK_ConstructorInitialization or SK_ListConstruction, |
949 | | /// the constructor to be called. |
950 | | /// |
951 | | /// Always a FunctionDecl, plus a Boolean flag telling if it was |
952 | | /// selected from an overloaded set having size greater than 1. |
953 | | /// For conversion decls, the naming class is the source type. |
954 | | /// For construct decls, the naming class is the target type. |
955 | | struct F Function; |
956 | | |
957 | | /// When Kind = SK_ConversionSequence, the implicit conversion |
958 | | /// sequence. |
959 | | ImplicitConversionSequence *ICS; |
960 | | |
961 | | /// When Kind = SK_RewrapInitList, the syntactic form of the |
962 | | /// wrapping list. |
963 | | InitListExpr *WrappingSyntacticList; |
964 | | }; |
965 | | |
966 | | void Destroy(); |
967 | | }; |
968 | | |
969 | | private: |
970 | | /// The kind of initialization sequence computed. |
971 | | enum SequenceKind SequenceKind; |
972 | | |
973 | | /// Steps taken by this initialization. |
974 | | SmallVector<Step, 4> Steps; |
975 | | |
976 | | public: |
977 | | /// Describes why initialization failed. |
978 | | enum FailureKind { |
979 | | /// Too many initializers provided for a reference. |
980 | | FK_TooManyInitsForReference, |
981 | | |
982 | | /// Reference initialized from a parenthesized initializer list. |
983 | | FK_ParenthesizedListInitForReference, |
984 | | |
985 | | /// Array must be initialized with an initializer list. |
986 | | FK_ArrayNeedsInitList, |
987 | | |
988 | | /// Array must be initialized with an initializer list or a |
989 | | /// string literal. |
990 | | FK_ArrayNeedsInitListOrStringLiteral, |
991 | | |
992 | | /// Array must be initialized with an initializer list or a |
993 | | /// wide string literal. |
994 | | FK_ArrayNeedsInitListOrWideStringLiteral, |
995 | | |
996 | | /// Initializing a wide char array with narrow string literal. |
997 | | FK_NarrowStringIntoWideCharArray, |
998 | | |
999 | | /// Initializing char array with wide string literal. |
1000 | | FK_WideStringIntoCharArray, |
1001 | | |
1002 | | /// Initializing wide char array with incompatible wide string |
1003 | | /// literal. |
1004 | | FK_IncompatWideStringIntoWideChar, |
1005 | | |
1006 | | /// Initializing char8_t array with plain string literal. |
1007 | | FK_PlainStringIntoUTF8Char, |
1008 | | |
1009 | | /// Initializing char array with UTF-8 string literal. |
1010 | | FK_UTF8StringIntoPlainChar, |
1011 | | |
1012 | | /// Array type mismatch. |
1013 | | FK_ArrayTypeMismatch, |
1014 | | |
1015 | | /// Non-constant array initializer |
1016 | | FK_NonConstantArrayInit, |
1017 | | |
1018 | | /// Cannot resolve the address of an overloaded function. |
1019 | | FK_AddressOfOverloadFailed, |
1020 | | |
1021 | | /// Overloading due to reference initialization failed. |
1022 | | FK_ReferenceInitOverloadFailed, |
1023 | | |
1024 | | /// Non-const lvalue reference binding to a temporary. |
1025 | | FK_NonConstLValueReferenceBindingToTemporary, |
1026 | | |
1027 | | /// Non-const lvalue reference binding to a bit-field. |
1028 | | FK_NonConstLValueReferenceBindingToBitfield, |
1029 | | |
1030 | | /// Non-const lvalue reference binding to a vector element. |
1031 | | FK_NonConstLValueReferenceBindingToVectorElement, |
1032 | | |
1033 | | /// Non-const lvalue reference binding to a matrix element. |
1034 | | FK_NonConstLValueReferenceBindingToMatrixElement, |
1035 | | |
1036 | | /// Non-const lvalue reference binding to an lvalue of unrelated |
1037 | | /// type. |
1038 | | FK_NonConstLValueReferenceBindingToUnrelated, |
1039 | | |
1040 | | /// Rvalue reference binding to an lvalue. |
1041 | | FK_RValueReferenceBindingToLValue, |
1042 | | |
1043 | | /// Reference binding drops qualifiers. |
1044 | | FK_ReferenceInitDropsQualifiers, |
1045 | | |
1046 | | /// Reference with mismatching address space binding to temporary. |
1047 | | FK_ReferenceAddrspaceMismatchTemporary, |
1048 | | |
1049 | | /// Reference binding failed. |
1050 | | FK_ReferenceInitFailed, |
1051 | | |
1052 | | /// Implicit conversion failed. |
1053 | | FK_ConversionFailed, |
1054 | | |
1055 | | /// Implicit conversion failed. |
1056 | | FK_ConversionFromPropertyFailed, |
1057 | | |
1058 | | /// Too many initializers for scalar |
1059 | | FK_TooManyInitsForScalar, |
1060 | | |
1061 | | /// Scalar initialized from a parenthesized initializer list. |
1062 | | FK_ParenthesizedListInitForScalar, |
1063 | | |
1064 | | /// Reference initialization from an initializer list |
1065 | | FK_ReferenceBindingToInitList, |
1066 | | |
1067 | | /// Initialization of some unused destination type with an |
1068 | | /// initializer list. |
1069 | | FK_InitListBadDestinationType, |
1070 | | |
1071 | | /// Overloading for a user-defined conversion failed. |
1072 | | FK_UserConversionOverloadFailed, |
1073 | | |
1074 | | /// Overloading for initialization by constructor failed. |
1075 | | FK_ConstructorOverloadFailed, |
1076 | | |
1077 | | /// Overloading for list-initialization by constructor failed. |
1078 | | FK_ListConstructorOverloadFailed, |
1079 | | |
1080 | | /// Default-initialization of a 'const' object. |
1081 | | FK_DefaultInitOfConst, |
1082 | | |
1083 | | /// Initialization of an incomplete type. |
1084 | | FK_Incomplete, |
1085 | | |
1086 | | /// Variable-length array must not have an initializer. |
1087 | | FK_VariableLengthArrayHasInitializer, |
1088 | | |
1089 | | /// List initialization failed at some point. |
1090 | | FK_ListInitializationFailed, |
1091 | | |
1092 | | /// Initializer has a placeholder type which cannot be |
1093 | | /// resolved by initialization. |
1094 | | FK_PlaceholderType, |
1095 | | |
1096 | | /// Trying to take the address of a function that doesn't support |
1097 | | /// having its address taken. |
1098 | | FK_AddressOfUnaddressableFunction, |
1099 | | |
1100 | | /// List-copy-initialization chose an explicit constructor. |
1101 | | FK_ExplicitConstructor, |
1102 | | }; |
1103 | | |
1104 | | private: |
1105 | | /// The reason why initialization failed. |
1106 | | FailureKind Failure; |
1107 | | |
1108 | | /// The failed result of overload resolution. |
1109 | | OverloadingResult FailedOverloadResult; |
1110 | | |
1111 | | /// The candidate set created when initialization failed. |
1112 | | OverloadCandidateSet FailedCandidateSet; |
1113 | | |
1114 | | /// The incomplete type that caused a failure. |
1115 | | QualType FailedIncompleteType; |
1116 | | |
1117 | | /// The fixit that needs to be applied to make this initialization |
1118 | | /// succeed. |
1119 | | std::string ZeroInitializationFixit; |
1120 | | SourceLocation ZeroInitializationFixitLoc; |
1121 | | |
1122 | | public: |
1123 | | /// Call for initializations are invalid but that would be valid |
1124 | | /// zero initialzations if Fixit was applied. |
1125 | 74 | void SetZeroInitializationFixit(const std::string& Fixit, SourceLocation L) { |
1126 | 74 | ZeroInitializationFixit = Fixit; |
1127 | 74 | ZeroInitializationFixitLoc = L; |
1128 | 74 | } |
1129 | | |
1130 | | private: |
1131 | | /// Prints a follow-up note that highlights the location of |
1132 | | /// the initialized entity, if it's remote. |
1133 | | void PrintInitLocationNote(Sema &S, const InitializedEntity &Entity); |
1134 | | |
1135 | | public: |
1136 | | /// Try to perform initialization of the given entity, creating a |
1137 | | /// record of the steps required to perform the initialization. |
1138 | | /// |
1139 | | /// The generated initialization sequence will either contain enough |
1140 | | /// information to diagnose |
1141 | | /// |
1142 | | /// \param S the semantic analysis object. |
1143 | | /// |
1144 | | /// \param Entity the entity being initialized. |
1145 | | /// |
1146 | | /// \param Kind the kind of initialization being performed. |
1147 | | /// |
1148 | | /// \param Args the argument(s) provided for initialization. |
1149 | | /// |
1150 | | /// \param TopLevelOfInitList true if we are initializing from an expression |
1151 | | /// at the top level inside an initializer list. This disallows |
1152 | | /// narrowing conversions in C++11 onwards. |
1153 | | /// \param TreatUnavailableAsInvalid true if we want to treat unavailable |
1154 | | /// as invalid. |
1155 | | InitializationSequence(Sema &S, |
1156 | | const InitializedEntity &Entity, |
1157 | | const InitializationKind &Kind, |
1158 | | MultiExprArg Args, |
1159 | | bool TopLevelOfInitList = false, |
1160 | | bool TreatUnavailableAsInvalid = true); |
1161 | | void InitializeFrom(Sema &S, const InitializedEntity &Entity, |
1162 | | const InitializationKind &Kind, MultiExprArg Args, |
1163 | | bool TopLevelOfInitList, bool TreatUnavailableAsInvalid); |
1164 | | |
1165 | | ~InitializationSequence(); |
1166 | | |
1167 | | /// Perform the actual initialization of the given entity based on |
1168 | | /// the computed initialization sequence. |
1169 | | /// |
1170 | | /// \param S the semantic analysis object. |
1171 | | /// |
1172 | | /// \param Entity the entity being initialized. |
1173 | | /// |
1174 | | /// \param Kind the kind of initialization being performed. |
1175 | | /// |
1176 | | /// \param Args the argument(s) provided for initialization, ownership of |
1177 | | /// which is transferred into the routine. |
1178 | | /// |
1179 | | /// \param ResultType if non-NULL, will be set to the type of the |
1180 | | /// initialized object, which is the type of the declaration in most |
1181 | | /// cases. However, when the initialized object is a variable of |
1182 | | /// incomplete array type and the initializer is an initializer |
1183 | | /// list, this type will be set to the completed array type. |
1184 | | /// |
1185 | | /// \returns an expression that performs the actual object initialization, if |
1186 | | /// the initialization is well-formed. Otherwise, emits diagnostics |
1187 | | /// and returns an invalid expression. |
1188 | | ExprResult Perform(Sema &S, |
1189 | | const InitializedEntity &Entity, |
1190 | | const InitializationKind &Kind, |
1191 | | MultiExprArg Args, |
1192 | | QualType *ResultType = nullptr); |
1193 | | |
1194 | | /// Diagnose an potentially-invalid initialization sequence. |
1195 | | /// |
1196 | | /// \returns true if the initialization sequence was ill-formed, |
1197 | | /// false otherwise. |
1198 | | bool Diagnose(Sema &S, |
1199 | | const InitializedEntity &Entity, |
1200 | | const InitializationKind &Kind, |
1201 | | ArrayRef<Expr *> Args); |
1202 | | |
1203 | | /// Determine the kind of initialization sequence computed. |
1204 | 13.1M | enum SequenceKind getKind() const { return SequenceKind; } |
1205 | | |
1206 | | /// Set the kind of sequence computed. |
1207 | 13.1M | void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } |
1208 | | |
1209 | | /// Determine whether the initialization sequence is valid. |
1210 | 505k | explicit operator bool() const { return !Failed(); } |
1211 | | |
1212 | | /// Determine whether the initialization sequence is invalid. |
1213 | 14.6M | bool Failed() const { return SequenceKind == FailedSequence; } |
1214 | | |
1215 | | using step_iterator = SmallVectorImpl<Step>::const_iterator; |
1216 | | |
1217 | 11.9M | step_iterator step_begin() const { return Steps.begin(); } |
1218 | 11.9M | step_iterator step_end() const { return Steps.end(); } |
1219 | | |
1220 | | using step_range = llvm::iterator_range<step_iterator>; |
1221 | | |
1222 | 242 | step_range steps() const { return {step_begin(), step_end()}; } |
1223 | | |
1224 | | /// Determine whether this initialization is a direct reference |
1225 | | /// binding (C++ [dcl.init.ref]). |
1226 | | bool isDirectReferenceBinding() const; |
1227 | | |
1228 | | /// Determine whether this initialization failed due to an ambiguity. |
1229 | | bool isAmbiguous() const; |
1230 | | |
1231 | | /// Determine whether this initialization is direct call to a |
1232 | | /// constructor. |
1233 | | bool isConstructorInitialization() const; |
1234 | | |
1235 | | /// Add a new step in the initialization that resolves the address |
1236 | | /// of an overloaded function to a specific function declaration. |
1237 | | /// |
1238 | | /// \param Function the function to which the overloaded function reference |
1239 | | /// resolves. |
1240 | | void AddAddressOverloadResolutionStep(FunctionDecl *Function, |
1241 | | DeclAccessPair Found, |
1242 | | bool HadMultipleCandidates); |
1243 | | |
1244 | | /// Add a new step in the initialization that performs a derived-to- |
1245 | | /// base cast. |
1246 | | /// |
1247 | | /// \param BaseType the base type to which we will be casting. |
1248 | | /// |
1249 | | /// \param Category Indicates whether the result will be treated as an |
1250 | | /// rvalue, an xvalue, or an lvalue. |
1251 | | void AddDerivedToBaseCastStep(QualType BaseType, |
1252 | | ExprValueKind Category); |
1253 | | |
1254 | | /// Add a new step binding a reference to an object. |
1255 | | /// |
1256 | | /// \param BindingTemporary True if we are binding a reference to a temporary |
1257 | | /// object (thereby extending its lifetime); false if we are binding to an |
1258 | | /// lvalue or an lvalue treated as an rvalue. |
1259 | | void AddReferenceBindingStep(QualType T, bool BindingTemporary); |
1260 | | |
1261 | | /// Add a new step that makes an extraneous copy of the input |
1262 | | /// to a temporary of the same class type. |
1263 | | /// |
1264 | | /// This extraneous copy only occurs during reference binding in |
1265 | | /// C++98/03, where we are permitted (but not required) to introduce |
1266 | | /// an extra copy. At a bare minimum, we must check that we could |
1267 | | /// call the copy constructor, and produce a diagnostic if the copy |
1268 | | /// constructor is inaccessible or no copy constructor matches. |
1269 | | // |
1270 | | /// \param T The type of the temporary being created. |
1271 | | void AddExtraneousCopyToTemporary(QualType T); |
1272 | | |
1273 | | /// Add a new step that makes a copy of the input to an object of |
1274 | | /// the given type, as the final step in class copy-initialization. |
1275 | | void AddFinalCopy(QualType T); |
1276 | | |
1277 | | /// Add a new step invoking a conversion function, which is either |
1278 | | /// a constructor or a conversion function. |
1279 | | void AddUserConversionStep(FunctionDecl *Function, |
1280 | | DeclAccessPair FoundDecl, |
1281 | | QualType T, |
1282 | | bool HadMultipleCandidates); |
1283 | | |
1284 | | /// Add a new step that performs a qualification conversion to the |
1285 | | /// given type. |
1286 | | void AddQualificationConversionStep(QualType Ty, |
1287 | | ExprValueKind Category); |
1288 | | |
1289 | | /// Add a new step that performs a function reference conversion to the |
1290 | | /// given type. |
1291 | | void AddFunctionReferenceConversionStep(QualType Ty); |
1292 | | |
1293 | | /// Add a new step that performs conversion from non-atomic to atomic |
1294 | | /// type. |
1295 | | void AddAtomicConversionStep(QualType Ty); |
1296 | | |
1297 | | /// Add a new step that applies an implicit conversion sequence. |
1298 | | void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, |
1299 | | QualType T, bool TopLevelOfInitList = false); |
1300 | | |
1301 | | /// Add a list-initialization step. |
1302 | | void AddListInitializationStep(QualType T); |
1303 | | |
1304 | | /// Add a constructor-initialization step. |
1305 | | /// |
1306 | | /// \param FromInitList The constructor call is syntactically an initializer |
1307 | | /// list. |
1308 | | /// \param AsInitList The constructor is called as an init list constructor. |
1309 | | void AddConstructorInitializationStep(DeclAccessPair FoundDecl, |
1310 | | CXXConstructorDecl *Constructor, |
1311 | | QualType T, |
1312 | | bool HadMultipleCandidates, |
1313 | | bool FromInitList, bool AsInitList); |
1314 | | |
1315 | | /// Add a zero-initialization step. |
1316 | | void AddZeroInitializationStep(QualType T); |
1317 | | |
1318 | | /// Add a C assignment step. |
1319 | | // |
1320 | | // FIXME: It isn't clear whether this should ever be needed; |
1321 | | // ideally, we would handle everything needed in C in the common |
1322 | | // path. However, that isn't the case yet. |
1323 | | void AddCAssignmentStep(QualType T); |
1324 | | |
1325 | | /// Add a string init step. |
1326 | | void AddStringInitStep(QualType T); |
1327 | | |
1328 | | /// Add an Objective-C object conversion step, which is |
1329 | | /// always a no-op. |
1330 | | void AddObjCObjectConversionStep(QualType T); |
1331 | | |
1332 | | /// Add an array initialization loop step. |
1333 | | void AddArrayInitLoopStep(QualType T, QualType EltTy); |
1334 | | |
1335 | | /// Add an array initialization step. |
1336 | | void AddArrayInitStep(QualType T, bool IsGNUExtension); |
1337 | | |
1338 | | /// Add a parenthesized array initialization step. |
1339 | | void AddParenthesizedArrayInitStep(QualType T); |
1340 | | |
1341 | | /// Add a step to pass an object by indirect copy-restore. |
1342 | | void AddPassByIndirectCopyRestoreStep(QualType T, bool shouldCopy); |
1343 | | |
1344 | | /// Add a step to "produce" an Objective-C object (by |
1345 | | /// retaining it). |
1346 | | void AddProduceObjCObjectStep(QualType T); |
1347 | | |
1348 | | /// Add a step to construct a std::initializer_list object from an |
1349 | | /// initializer list. |
1350 | | void AddStdInitializerListConstructionStep(QualType T); |
1351 | | |
1352 | | /// Add a step to initialize an OpenCL sampler from an integer |
1353 | | /// constant. |
1354 | | void AddOCLSamplerInitStep(QualType T); |
1355 | | |
1356 | | /// Add a step to initialzie an OpenCL opaque type (event_t, queue_t, etc.) |
1357 | | /// from a zero constant. |
1358 | | void AddOCLZeroOpaqueTypeStep(QualType T); |
1359 | | |
1360 | | /// Add steps to unwrap a initializer list for a reference around a |
1361 | | /// single element and rewrap it at the end. |
1362 | | void RewrapReferenceInitList(QualType T, InitListExpr *Syntactic); |
1363 | | |
1364 | | /// Note that this initialization sequence failed. |
1365 | 84.3k | void SetFailed(FailureKind Failure) { |
1366 | 84.3k | SequenceKind = FailedSequence; |
1367 | 84.3k | this->Failure = Failure; |
1368 | 84.3k | assert((Failure != FK_Incomplete || !FailedIncompleteType.isNull()) && |
1369 | 84.3k | "Incomplete type failure requires a type!"); |
1370 | 84.3k | } |
1371 | | |
1372 | | /// Note that this initialization sequence failed due to failed |
1373 | | /// overload resolution. |
1374 | | void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); |
1375 | | |
1376 | | /// Retrieve a reference to the candidate set when overload |
1377 | | /// resolution fails. |
1378 | 403k | OverloadCandidateSet &getFailedCandidateSet() { |
1379 | 403k | return FailedCandidateSet; |
1380 | 403k | } |
1381 | | |
1382 | | /// Get the overloading result, for when the initialization |
1383 | | /// sequence failed due to a bad overload. |
1384 | 6.30k | OverloadingResult getFailedOverloadResult() const { |
1385 | 6.30k | return FailedOverloadResult; |
1386 | 6.30k | } |
1387 | | |
1388 | | /// Note that this initialization sequence failed due to an |
1389 | | /// incomplete type. |
1390 | 57 | void setIncompleteTypeFailure(QualType IncompleteType) { |
1391 | 57 | FailedIncompleteType = IncompleteType; |
1392 | 57 | SetFailed(FK_Incomplete); |
1393 | 57 | } |
1394 | | |
1395 | | /// Determine why initialization failed. |
1396 | 680 | FailureKind getFailureKind() const { |
1397 | 680 | assert(Failed() && "Not an initialization failure!"); |
1398 | 0 | return Failure; |
1399 | 680 | } |
1400 | | |
1401 | | /// Dump a representation of this initialization sequence to |
1402 | | /// the given stream, for debugging purposes. |
1403 | | void dump(raw_ostream &OS) const; |
1404 | | |
1405 | | /// Dump a representation of this initialization sequence to |
1406 | | /// standard error, for debugging purposes. |
1407 | | void dump() const; |
1408 | | }; |
1409 | | |
1410 | | } // namespace clang |
1411 | | |
1412 | | #endif // LLVM_CLANG_SEMA_INITIALIZATION_H |