Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 1 | //===--- SemaInit.h - Semantic Analysis for Initializers --------*- C++ -*-===// |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 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 | // |
| 10 | // This file provides supporting data types for initialization of objects. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef LLVM_CLANG_SEMA_INIT_H |
| 14 | #define LLVM_CLANG_SEMA_INIT_H |
| 15 | |
| 16 | #include "SemaOverload.h" |
| 17 | #include "clang/AST/TypeLoc.h" |
| 18 | #include "clang/Parse/Action.h" |
| 19 | #include "clang/Basic/SourceLocation.h" |
| 20 | #include "llvm/ADT/PointerIntPair.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include <cassert> |
| 23 | |
| 24 | namespace clang { |
| 25 | |
| 26 | class CXXBaseSpecifier; |
| 27 | class DeclaratorDecl; |
| 28 | class DeclaratorInfo; |
| 29 | class FieldDecl; |
| 30 | class FunctionDecl; |
| 31 | class ParmVarDecl; |
| 32 | class Sema; |
| 33 | class TypeLoc; |
| 34 | class VarDecl; |
| 35 | |
| 36 | /// \brief Describes an entity that is being initialized. |
| 37 | class InitializedEntity { |
| 38 | public: |
| 39 | /// \brief Specifies the kind of entity being initialized. |
| 40 | enum EntityKind { |
| 41 | /// \brief The entity being initialized is a variable. |
| 42 | EK_Variable, |
| 43 | /// \brief The entity being initialized is a function parameter. |
| 44 | EK_Parameter, |
| 45 | /// \brief The entity being initialized is the result of a function call. |
| 46 | EK_Result, |
| 47 | /// \brief The entity being initialized is an exception object that |
| 48 | /// is being thrown. |
| 49 | EK_Exception, |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 50 | /// \brief The entity being initialized is an object (or array of |
| 51 | /// objects) allocated via new. |
| 52 | EK_New, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 53 | /// \brief The entity being initialized is a temporary object. |
| 54 | EK_Temporary, |
| 55 | /// \brief The entity being initialized is a base member subobject. |
| 56 | EK_Base, |
| 57 | /// \brief The entity being initialized is a non-static data member |
| 58 | /// subobject. |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 59 | EK_Member, |
| 60 | /// \brief The entity being initialized is an element of an array |
| 61 | /// or vector. |
| 62 | EK_ArrayOrVectorElement |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | private: |
| 66 | /// \brief The kind of entity being initialized. |
| 67 | EntityKind Kind; |
| 68 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 69 | /// \brief If non-NULL, the parent entity in which this |
| 70 | /// initialization occurs. |
| 71 | const InitializedEntity *Parent; |
| 72 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 73 | /// \brief The type of the object or reference being initialized along with |
| 74 | /// its location information. |
| 75 | TypeLoc TL; |
| 76 | |
| 77 | union { |
| 78 | /// \brief When Kind == EK_Variable, EK_Parameter, or EK_Member, |
| 79 | /// the VarDecl, ParmVarDecl, or FieldDecl, respectively. |
| 80 | DeclaratorDecl *VariableOrMember; |
| 81 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 82 | /// \brief When Kind == EK_Result, EK_Exception, or EK_New, the |
| 83 | /// location of the 'return', 'throw', or 'new' keyword, |
| 84 | /// respectively. When Kind == EK_Temporary, the location where |
| 85 | /// the temporary is being created. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 86 | unsigned Location; |
| 87 | |
| 88 | /// \brief When Kind == EK_Base, the base specifier that provides the |
| 89 | /// base class. |
| 90 | CXXBaseSpecifier *Base; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 91 | |
| 92 | /// \brief When Kind = EK_ArrayOrVectorElement, the index of the |
| 93 | /// array or vector element being initialized. |
| 94 | unsigned Index; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | InitializedEntity() { } |
| 98 | |
| 99 | /// \brief Create the initialization entity for a variable. |
| 100 | InitializedEntity(VarDecl *Var) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 101 | : Kind(EK_Variable), Parent(0), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 102 | VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Var)) |
| 103 | { |
| 104 | InitDeclLoc(); |
| 105 | } |
| 106 | |
| 107 | /// \brief Create the initialization entity for a parameter. |
| 108 | InitializedEntity(ParmVarDecl *Parm) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 109 | : Kind(EK_Parameter), Parent(0), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 110 | VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Parm)) |
| 111 | { |
| 112 | InitDeclLoc(); |
| 113 | } |
| 114 | |
| 115 | /// \brief Create the initialization entity for the result of a function, |
| 116 | /// throwing an object, or performing an explicit cast. |
| 117 | InitializedEntity(EntityKind Kind, SourceLocation Loc, TypeLoc TL) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 118 | : Kind(Kind), Parent(0), TL(TL), Location(Loc.getRawEncoding()) { } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 119 | |
| 120 | /// \brief Create the initialization entity for a member subobject. |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 121 | InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent) |
| 122 | : Kind(EK_Member), Parent(Parent), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 123 | VariableOrMember(reinterpret_cast<DeclaratorDecl*>(Member)) |
| 124 | { |
| 125 | InitDeclLoc(); |
| 126 | } |
| 127 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 128 | /// \brief Create the initialization entity for an array element. |
| 129 | InitializedEntity(ASTContext &Context, unsigned Index, |
| 130 | const InitializedEntity &Parent); |
| 131 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 132 | /// \brief Initialize type-location information from a declaration. |
| 133 | void InitDeclLoc(); |
| 134 | |
| 135 | public: |
| 136 | /// \brief Create the initialization entity for a variable. |
| 137 | static InitializedEntity InitializeVariable(VarDecl *Var) { |
| 138 | return InitializedEntity(Var); |
| 139 | } |
| 140 | |
| 141 | /// \brief Create the initialization entity for a parameter. |
| 142 | static InitializedEntity InitializeParameter(ParmVarDecl *Parm) { |
| 143 | return InitializedEntity(Parm); |
| 144 | } |
| 145 | |
| 146 | /// \brief Create the initialization entity for the result of a function. |
| 147 | static InitializedEntity InitializeResult(SourceLocation ReturnLoc, |
| 148 | TypeLoc TL) { |
| 149 | return InitializedEntity(EK_Result, ReturnLoc, TL); |
| 150 | } |
| 151 | |
| 152 | /// \brief Create the initialization entity for an exception object. |
| 153 | static InitializedEntity InitializeException(SourceLocation ThrowLoc, |
| 154 | TypeLoc TL) { |
| 155 | return InitializedEntity(EK_Exception, ThrowLoc, TL); |
| 156 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 157 | |
| 158 | /// \brief Create the initialization entity for an object allocated via new. |
| 159 | static InitializedEntity InitializeNew(SourceLocation NewLoc, TypeLoc TL) { |
| 160 | return InitializedEntity(EK_New, NewLoc, TL); |
| 161 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 162 | |
| 163 | /// \brief Create the initialization entity for a temporary. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 164 | static InitializedEntity InitializeTemporary(TypeLoc TL) { |
| 165 | return InitializedEntity(EK_Temporary, SourceLocation(), TL); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /// \brief Create the initialization entity for a base class subobject. |
| 169 | static InitializedEntity InitializeBase(ASTContext &Context, |
| 170 | CXXBaseSpecifier *Base); |
| 171 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 172 | /// \brief Create the initialization entity for a member subobject. |
| 173 | static InitializedEntity InitializeMember(FieldDecl *Member, |
| 174 | const InitializedEntity *Parent = 0) { |
| 175 | return InitializedEntity(Member, Parent); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 178 | /// \brief Create the initialization entity for an array element. |
| 179 | static InitializedEntity InitializeElement(ASTContext &Context, |
| 180 | unsigned Index, |
| 181 | const InitializedEntity &Parent) { |
| 182 | return InitializedEntity(Context, Index, Parent); |
| 183 | } |
| 184 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 185 | /// \brief Determine the kind of initialization. |
| 186 | EntityKind getKind() const { return Kind; } |
| 187 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 188 | /// \brief Retrieve the parent of the entity being initialized, when |
| 189 | /// the initialization itself is occuring within the context of a |
| 190 | /// larger initialization. |
| 191 | const InitializedEntity *getParent() const { return Parent; } |
| 192 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 193 | /// \brief Retrieve type being initialized. |
| 194 | TypeLoc getType() const { return TL; } |
| 195 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 196 | /// \brief Retrieve the name of the entity being initialized. |
| 197 | DeclarationName getName() const; |
| 198 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 199 | /// \brief Determine the location of the 'return' keyword when initializing |
| 200 | /// the result of a function call. |
| 201 | SourceLocation getReturnLoc() const { |
| 202 | assert(getKind() == EK_Result && "No 'return' location!"); |
| 203 | return SourceLocation::getFromRawEncoding(Location); |
| 204 | } |
| 205 | |
| 206 | /// \brief Determine the location of the 'throw' keyword when initializing |
| 207 | /// an exception object. |
| 208 | SourceLocation getThrowLoc() const { |
| 209 | assert(getKind() == EK_Exception && "No 'throw' location!"); |
| 210 | return SourceLocation::getFromRawEncoding(Location); |
| 211 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 212 | |
| 213 | /// \brief If this is already the initializer for an array or vector |
| 214 | /// element, sets the element index. |
| 215 | void setElementIndex(unsigned Index) { |
| 216 | assert(getKind() == EK_ArrayOrVectorElement); |
| 217 | this->Index = Index; |
| 218 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | /// \brief Describes the kind of initialization being performed, along with |
| 222 | /// location information for tokens related to the initialization (equal sign, |
| 223 | /// parentheses). |
| 224 | class InitializationKind { |
| 225 | public: |
| 226 | /// \brief The kind of initialization being performed. |
| 227 | enum InitKind { |
| 228 | IK_Direct, ///< Direct initialization |
| 229 | IK_Copy, ///< Copy initialization |
| 230 | IK_Default, ///< Default initialization |
| 231 | IK_Value ///< Value initialization |
| 232 | }; |
| 233 | |
| 234 | private: |
| 235 | /// \brief The kind of initialization that we're storing. |
| 236 | enum StoredInitKind { |
| 237 | SIK_Direct = IK_Direct, ///< Direct initialization |
| 238 | SIK_Copy = IK_Copy, ///< Copy initialization |
| 239 | SIK_Default = IK_Default, ///< Default initialization |
| 240 | SIK_Value = IK_Value, ///< Value initialization |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 241 | SIK_ImplicitValue, ///< Implicit value initialization |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 242 | SIK_DirectCast, ///< Direct initialization due to a cast |
| 243 | /// \brief Direct initialization due to a C-style or functional cast. |
| 244 | SIK_DirectCStyleOrFunctionalCast |
| 245 | }; |
| 246 | |
| 247 | /// \brief The kind of initialization being performed. |
| 248 | StoredInitKind Kind; |
| 249 | |
| 250 | /// \brief The source locations involved in the initialization. |
| 251 | SourceLocation Locations[3]; |
| 252 | |
| 253 | InitializationKind(StoredInitKind Kind, SourceLocation Loc1, |
| 254 | SourceLocation Loc2, SourceLocation Loc3) |
| 255 | : Kind(Kind) |
| 256 | { |
| 257 | Locations[0] = Loc1; |
| 258 | Locations[1] = Loc2; |
| 259 | Locations[2] = Loc3; |
| 260 | } |
| 261 | |
| 262 | public: |
| 263 | /// \brief Create a direct initialization. |
| 264 | static InitializationKind CreateDirect(SourceLocation InitLoc, |
| 265 | SourceLocation LParenLoc, |
| 266 | SourceLocation RParenLoc) { |
| 267 | return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc); |
| 268 | } |
| 269 | |
| 270 | /// \brief Create a direct initialization due to a cast. |
| 271 | static InitializationKind CreateCast(SourceRange TypeRange, |
| 272 | bool IsCStyleCast) { |
| 273 | return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast |
| 274 | : SIK_DirectCast, |
| 275 | TypeRange.getBegin(), TypeRange.getBegin(), |
| 276 | TypeRange.getEnd()); |
| 277 | } |
| 278 | |
| 279 | /// \brief Create a copy initialization. |
| 280 | static InitializationKind CreateCopy(SourceLocation InitLoc, |
| 281 | SourceLocation EqualLoc) { |
| 282 | return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc); |
| 283 | } |
| 284 | |
| 285 | /// \brief Create a default initialization. |
| 286 | static InitializationKind CreateDefault(SourceLocation InitLoc) { |
| 287 | return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc); |
| 288 | } |
| 289 | |
| 290 | /// \brief Create a value initialization. |
| 291 | static InitializationKind CreateValue(SourceLocation InitLoc, |
| 292 | SourceLocation LParenLoc, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 293 | SourceLocation RParenLoc, |
| 294 | bool isImplicit = false) { |
| 295 | return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value, |
| 296 | InitLoc, LParenLoc, RParenLoc); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /// \brief Determine the initialization kind. |
| 300 | InitKind getKind() const { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 301 | if (Kind > SIK_ImplicitValue) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 302 | return IK_Direct; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 303 | if (Kind == SIK_ImplicitValue) |
| 304 | return IK_Value; |
| 305 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 306 | return (InitKind)Kind; |
| 307 | } |
| 308 | |
| 309 | /// \brief Determine whether this initialization is an explicit cast. |
| 310 | bool isExplicitCast() const { |
| 311 | return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast; |
| 312 | } |
| 313 | |
| 314 | /// \brief Determine whether this initialization is a C-style cast. |
| 315 | bool isCStyleOrFunctionalCast() const { |
| 316 | return Kind == SIK_DirectCStyleOrFunctionalCast; |
| 317 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 318 | |
| 319 | /// \brief Determine whether this initialization is an implicit |
| 320 | /// value-initialization, e.g., as occurs during aggregate |
| 321 | /// initialization. |
| 322 | bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; } |
| 323 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 324 | /// \brief Retrieve the location at which initialization is occurring. |
| 325 | SourceLocation getLocation() const { return Locations[0]; } |
| 326 | |
| 327 | /// \brief Retrieve the source range that covers the initialization. |
| 328 | SourceRange getRange() const { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 329 | return SourceRange(Locations[0], Locations[2]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /// \brief Retrieve the location of the equal sign for copy initialization |
| 333 | /// (if present). |
| 334 | SourceLocation getEqualLoc() const { |
| 335 | assert(Kind == SIK_Copy && "Only copy initialization has an '='"); |
| 336 | return Locations[1]; |
| 337 | } |
| 338 | |
| 339 | /// \brief Retrieve the source range containing the locations of the open |
| 340 | /// and closing parentheses for value and direct initializations. |
| 341 | SourceRange getParenRange() const { |
| 342 | assert((getKind() == IK_Direct || Kind == SIK_Value) && |
| 343 | "Only direct- and value-initialization have parentheses"); |
| 344 | return SourceRange(Locations[1], Locations[2]); |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | /// \brief Describes the sequence of initializations required to initialize |
| 349 | /// a given object or reference with a set of arguments. |
| 350 | class InitializationSequence { |
| 351 | public: |
| 352 | /// \brief Describes the kind of initialization sequence computed. |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 353 | /// |
| 354 | /// FIXME: Much of this information is in the initialization steps... why is |
| 355 | /// it duplicated here? |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 356 | enum SequenceKind { |
| 357 | /// \brief A failed initialization sequence. The failure kind tells what |
| 358 | /// happened. |
| 359 | FailedSequence = 0, |
| 360 | |
| 361 | /// \brief A dependent initialization, which could not be |
| 362 | /// type-checked due to the presence of dependent types or |
| 363 | /// dependently-type expressions. |
| 364 | DependentSequence, |
| 365 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 366 | /// \brief A user-defined conversion sequence. |
| 367 | UserDefinedConversion, |
| 368 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 369 | /// \brief A constructor call. |
Douglas Gregor | a6ca650 | 2009-12-14 20:57:13 +0000 | [diff] [blame] | 370 | ConstructorInitialization, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 372 | /// \brief A reference binding. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 373 | ReferenceBinding, |
| 374 | |
| 375 | /// \brief List initialization |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 376 | ListInitialization, |
| 377 | |
| 378 | /// \brief Zero-initialization. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 379 | ZeroInitialization, |
| 380 | |
| 381 | /// \brief No initialization required. |
| 382 | NoInitialization, |
| 383 | |
| 384 | /// \brief Standard conversion sequence. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 385 | StandardConversion, |
| 386 | |
| 387 | /// \brief C conversion sequence. |
| 388 | CAssignment |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | /// \brief Describes the kind of a particular step in an initialization |
| 392 | /// sequence. |
| 393 | enum StepKind { |
| 394 | /// \brief Resolve the address of an overloaded function to a specific |
| 395 | /// function declaration. |
| 396 | SK_ResolveAddressOfOverloadedFunction, |
| 397 | /// \brief Perform a derived-to-base cast, producing an rvalue. |
| 398 | SK_CastDerivedToBaseRValue, |
| 399 | /// \brief Perform a derived-to-base cast, producing an lvalue. |
| 400 | SK_CastDerivedToBaseLValue, |
| 401 | /// \brief Reference binding to an lvalue. |
| 402 | SK_BindReference, |
| 403 | /// \brief Reference binding to a temporary. |
| 404 | SK_BindReferenceToTemporary, |
| 405 | /// \brief Perform a user-defined conversion, either via a conversion |
| 406 | /// function or via a constructor. |
| 407 | SK_UserConversion, |
| 408 | /// \brief Perform a qualification conversion, producing an rvalue. |
| 409 | SK_QualificationConversionRValue, |
| 410 | /// \brief Perform a qualification conversion, producing an lvalue. |
| 411 | SK_QualificationConversionLValue, |
| 412 | /// \brief Perform an implicit conversion sequence. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 413 | SK_ConversionSequence, |
| 414 | /// \brief Perform list-initialization |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 415 | SK_ListInitialization, |
| 416 | /// \brief Perform initialization via a constructor. |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 417 | SK_ConstructorInitialization, |
| 418 | /// \brief Zero-initialize the object |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 419 | SK_ZeroInitialization, |
| 420 | /// \brief C assignment |
| 421 | SK_CAssignment |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | /// \brief A single step in the initialization sequence. |
| 425 | class Step { |
| 426 | public: |
| 427 | /// \brief The kind of conversion or initialization step we are taking. |
| 428 | StepKind Kind; |
| 429 | |
| 430 | // \brief The type that results from this initialization. |
| 431 | QualType Type; |
| 432 | |
| 433 | union { |
| 434 | /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind == |
| 435 | /// SK_UserConversion, the function that the expression should be |
| 436 | /// resolved to or the conversion function to call, respectively. |
| 437 | FunctionDecl *Function; |
| 438 | |
| 439 | /// \brief When Kind = SK_ConversionSequence, the implicit conversion |
| 440 | /// sequence |
| 441 | ImplicitConversionSequence *ICS; |
| 442 | }; |
| 443 | |
| 444 | void Destroy(); |
| 445 | }; |
| 446 | |
| 447 | private: |
| 448 | /// \brief The kind of initialization sequence computed. |
| 449 | enum SequenceKind SequenceKind; |
| 450 | |
| 451 | /// \brief Steps taken by this initialization. |
| 452 | llvm::SmallVector<Step, 4> Steps; |
| 453 | |
| 454 | public: |
| 455 | /// \brief Describes why initialization failed. |
| 456 | enum FailureKind { |
| 457 | /// \brief Too many initializers provided for a reference. |
| 458 | FK_TooManyInitsForReference, |
| 459 | /// \brief Array must be initialized with an initializer list. |
| 460 | FK_ArrayNeedsInitList, |
| 461 | /// \brief Array must be initialized with an initializer list or a |
| 462 | /// string literal. |
| 463 | FK_ArrayNeedsInitListOrStringLiteral, |
| 464 | /// \brief Cannot resolve the address of an overloaded function. |
| 465 | FK_AddressOfOverloadFailed, |
| 466 | /// \brief Overloading due to reference initialization failed. |
| 467 | FK_ReferenceInitOverloadFailed, |
| 468 | /// \brief Non-const lvalue reference binding to a temporary. |
| 469 | FK_NonConstLValueReferenceBindingToTemporary, |
| 470 | /// \brief Non-const lvalue reference binding to an lvalue of unrelated |
| 471 | /// type. |
| 472 | FK_NonConstLValueReferenceBindingToUnrelated, |
| 473 | /// \brief Rvalue reference binding to an lvalue. |
| 474 | FK_RValueReferenceBindingToLValue, |
| 475 | /// \brief Reference binding drops qualifiers. |
| 476 | FK_ReferenceInitDropsQualifiers, |
| 477 | /// \brief Reference binding failed. |
| 478 | FK_ReferenceInitFailed, |
| 479 | /// \brief Implicit conversion failed. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 480 | FK_ConversionFailed, |
| 481 | /// \brief Too many initializers for scalar |
| 482 | FK_TooManyInitsForScalar, |
| 483 | /// \brief Reference initialization from an initializer list |
| 484 | FK_ReferenceBindingToInitList, |
| 485 | /// \brief Initialization of some unused destination type with an |
| 486 | /// initializer list. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 487 | FK_InitListBadDestinationType, |
| 488 | /// \brief Overloading for a user-defined conversion failed. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 489 | FK_UserConversionOverloadFailed, |
| 490 | /// \brief Overloaded for initialization by constructor failed. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 491 | FK_ConstructorOverloadFailed, |
| 492 | /// \brief Default-initialization of a 'const' object. |
| 493 | FK_DefaultInitOfConst |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | private: |
| 497 | /// \brief The reason why initialization failued. |
| 498 | FailureKind Failure; |
| 499 | |
| 500 | /// \brief The failed result of overload resolution. |
| 501 | OverloadingResult FailedOverloadResult; |
| 502 | |
| 503 | /// \brief The candidate set created when initialization failed. |
| 504 | OverloadCandidateSet FailedCandidateSet; |
| 505 | |
| 506 | public: |
| 507 | /// \brief Try to perform initialization of the given entity, creating a |
| 508 | /// record of the steps required to perform the initialization. |
| 509 | /// |
| 510 | /// The generated initialization sequence will either contain enough |
| 511 | /// information to diagnose |
| 512 | /// |
| 513 | /// \param S the semantic analysis object. |
| 514 | /// |
| 515 | /// \param Entity the entity being initialized. |
| 516 | /// |
| 517 | /// \param Kind the kind of initialization being performed. |
| 518 | /// |
| 519 | /// \param Args the argument(s) provided for initialization. |
| 520 | /// |
| 521 | /// \param NumArgs the number of arguments provided for initialization. |
| 522 | InitializationSequence(Sema &S, |
| 523 | const InitializedEntity &Entity, |
| 524 | const InitializationKind &Kind, |
| 525 | Expr **Args, |
| 526 | unsigned NumArgs); |
| 527 | |
| 528 | ~InitializationSequence(); |
| 529 | |
| 530 | /// \brief Perform the actual initialization of the given entity based on |
| 531 | /// the computed initialization sequence. |
| 532 | /// |
| 533 | /// \param S the semantic analysis object. |
| 534 | /// |
| 535 | /// \param Entity the entity being initialized. |
| 536 | /// |
| 537 | /// \param Kind the kind of initialization being performed. |
| 538 | /// |
| 539 | /// \param Args the argument(s) provided for initialization, ownership of |
| 540 | /// which is transfered into the routine. |
| 541 | /// |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 542 | /// \param ResultType if non-NULL, will be set to the type of the |
| 543 | /// initialized object, which is the type of the declaration in most |
| 544 | /// cases. However, when the initialized object is a variable of |
| 545 | /// incomplete array type and the initializer is an initializer |
| 546 | /// list, this type will be set to the completed array type. |
| 547 | /// |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 548 | /// \returns an expression that performs the actual object initialization, if |
| 549 | /// the initialization is well-formed. Otherwise, emits diagnostics |
| 550 | /// and returns an invalid expression. |
| 551 | Action::OwningExprResult Perform(Sema &S, |
| 552 | const InitializedEntity &Entity, |
| 553 | const InitializationKind &Kind, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 554 | Action::MultiExprArg Args, |
| 555 | QualType *ResultType = 0); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 556 | |
| 557 | /// \brief Diagnose an potentially-invalid initialization sequence. |
| 558 | /// |
| 559 | /// \returns true if the initialization sequence was ill-formed, |
| 560 | /// false otherwise. |
| 561 | bool Diagnose(Sema &S, |
| 562 | const InitializedEntity &Entity, |
| 563 | const InitializationKind &Kind, |
| 564 | Expr **Args, unsigned NumArgs); |
| 565 | |
| 566 | /// \brief Determine the kind of initialization sequence computed. |
| 567 | enum SequenceKind getKind() const { return SequenceKind; } |
| 568 | |
| 569 | /// \brief Set the kind of sequence computed. |
| 570 | void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } |
| 571 | |
| 572 | /// \brief Determine whether the initialization sequence is valid. |
| 573 | operator bool() const { return SequenceKind != FailedSequence; } |
| 574 | |
| 575 | typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator; |
| 576 | step_iterator step_begin() const { return Steps.begin(); } |
| 577 | step_iterator step_end() const { return Steps.end(); } |
| 578 | |
| 579 | /// \brief Add a new step in the initialization that resolves the address |
| 580 | /// of an overloaded function to a specific function declaration. |
| 581 | /// |
| 582 | /// \param Function the function to which the overloaded function reference |
| 583 | /// resolves. |
| 584 | void AddAddressOverloadResolutionStep(FunctionDecl *Function); |
| 585 | |
| 586 | /// \brief Add a new step in the initialization that performs a derived-to- |
| 587 | /// base cast. |
| 588 | /// |
| 589 | /// \param BaseType the base type to which we will be casting. |
| 590 | /// |
| 591 | /// \param IsLValue true if the result of this cast will be treated as |
| 592 | /// an lvalue. |
| 593 | void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue); |
| 594 | |
| 595 | /// \brief Add a new step binding a reference to an object. |
| 596 | /// |
| 597 | /// \param BindingTemporary true if we are binding a reference to a temporary |
| 598 | /// object (thereby extending its lifetime); false if we are binding to an |
| 599 | /// lvalue or an lvalue treated as an rvalue. |
| 600 | void AddReferenceBindingStep(QualType T, bool BindingTemporary); |
| 601 | |
| 602 | /// \brief Add a new step invoking a conversion function, which is either |
| 603 | /// a constructor or a conversion function. |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 604 | void AddUserConversionStep(FunctionDecl *Function, QualType T); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 605 | |
| 606 | /// \brief Add a new step that performs a qualification conversion to the |
| 607 | /// given type. |
| 608 | void AddQualificationConversionStep(QualType Ty, bool IsLValue); |
| 609 | |
| 610 | /// \brief Add a new step that applies an implicit conversion sequence. |
| 611 | void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, |
| 612 | QualType T); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 613 | |
| 614 | /// \brief Add a list-initialiation step |
| 615 | void AddListInitializationStep(QualType T); |
| 616 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 617 | /// \brief Add a constructor-initialization step. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 618 | void AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 619 | QualType T); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 620 | |
| 621 | /// \brief Add a zero-initialization step. |
| 622 | void AddZeroInitializationStep(QualType T); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 624 | /// \brief Add a C assignment step. |
| 625 | // |
| 626 | // FIXME: It isn't clear whether this should ever be needed; |
| 627 | // ideally, we would handle everything needed in C in the common |
| 628 | // path. However, that isn't the case yet. |
| 629 | void AddCAssignmentStep(QualType T); |
| 630 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 631 | /// \brief Note that this initialization sequence failed. |
| 632 | void SetFailed(FailureKind Failure) { |
| 633 | SequenceKind = FailedSequence; |
| 634 | this->Failure = Failure; |
| 635 | } |
| 636 | |
| 637 | /// \brief Note that this initialization sequence failed due to failed |
| 638 | /// overload resolution. |
| 639 | void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); |
| 640 | |
| 641 | /// \brief Retrieve a reference to the candidate set when overload |
| 642 | /// resolution fails. |
| 643 | OverloadCandidateSet &getFailedCandidateSet() { |
| 644 | return FailedCandidateSet; |
| 645 | } |
| 646 | |
| 647 | /// \brief Determine why initialization failed. |
| 648 | FailureKind getFailureKind() const { |
| 649 | assert(getKind() == FailedSequence && "Not an initialization failure!"); |
| 650 | return Failure; |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | } // end namespace clang |
| 655 | |
| 656 | #endif // LLVM_CLANG_SEMA_INIT_H |