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; |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 198 | |
| 199 | /// \brief Retrieve the variable, parameter, or field being |
| 200 | /// initialized. |
| 201 | DeclaratorDecl *getDecl() const; |
| 202 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 203 | /// \brief Determine the location of the 'return' keyword when initializing |
| 204 | /// the result of a function call. |
| 205 | SourceLocation getReturnLoc() const { |
| 206 | assert(getKind() == EK_Result && "No 'return' location!"); |
| 207 | return SourceLocation::getFromRawEncoding(Location); |
| 208 | } |
| 209 | |
| 210 | /// \brief Determine the location of the 'throw' keyword when initializing |
| 211 | /// an exception object. |
| 212 | SourceLocation getThrowLoc() const { |
| 213 | assert(getKind() == EK_Exception && "No 'throw' location!"); |
| 214 | return SourceLocation::getFromRawEncoding(Location); |
| 215 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 216 | |
| 217 | /// \brief If this is already the initializer for an array or vector |
| 218 | /// element, sets the element index. |
| 219 | void setElementIndex(unsigned Index) { |
| 220 | assert(getKind() == EK_ArrayOrVectorElement); |
| 221 | this->Index = Index; |
| 222 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | /// \brief Describes the kind of initialization being performed, along with |
| 226 | /// location information for tokens related to the initialization (equal sign, |
| 227 | /// parentheses). |
| 228 | class InitializationKind { |
| 229 | public: |
| 230 | /// \brief The kind of initialization being performed. |
| 231 | enum InitKind { |
| 232 | IK_Direct, ///< Direct initialization |
| 233 | IK_Copy, ///< Copy initialization |
| 234 | IK_Default, ///< Default initialization |
| 235 | IK_Value ///< Value initialization |
| 236 | }; |
| 237 | |
| 238 | private: |
| 239 | /// \brief The kind of initialization that we're storing. |
| 240 | enum StoredInitKind { |
| 241 | SIK_Direct = IK_Direct, ///< Direct initialization |
| 242 | SIK_Copy = IK_Copy, ///< Copy initialization |
| 243 | SIK_Default = IK_Default, ///< Default initialization |
| 244 | SIK_Value = IK_Value, ///< Value initialization |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 245 | SIK_ImplicitValue, ///< Implicit value initialization |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 246 | SIK_DirectCast, ///< Direct initialization due to a cast |
| 247 | /// \brief Direct initialization due to a C-style or functional cast. |
| 248 | SIK_DirectCStyleOrFunctionalCast |
| 249 | }; |
| 250 | |
| 251 | /// \brief The kind of initialization being performed. |
| 252 | StoredInitKind Kind; |
| 253 | |
| 254 | /// \brief The source locations involved in the initialization. |
| 255 | SourceLocation Locations[3]; |
| 256 | |
| 257 | InitializationKind(StoredInitKind Kind, SourceLocation Loc1, |
| 258 | SourceLocation Loc2, SourceLocation Loc3) |
| 259 | : Kind(Kind) |
| 260 | { |
| 261 | Locations[0] = Loc1; |
| 262 | Locations[1] = Loc2; |
| 263 | Locations[2] = Loc3; |
| 264 | } |
| 265 | |
| 266 | public: |
| 267 | /// \brief Create a direct initialization. |
| 268 | static InitializationKind CreateDirect(SourceLocation InitLoc, |
| 269 | SourceLocation LParenLoc, |
| 270 | SourceLocation RParenLoc) { |
| 271 | return InitializationKind(SIK_Direct, InitLoc, LParenLoc, RParenLoc); |
| 272 | } |
| 273 | |
| 274 | /// \brief Create a direct initialization due to a cast. |
| 275 | static InitializationKind CreateCast(SourceRange TypeRange, |
| 276 | bool IsCStyleCast) { |
| 277 | return InitializationKind(IsCStyleCast? SIK_DirectCStyleOrFunctionalCast |
| 278 | : SIK_DirectCast, |
| 279 | TypeRange.getBegin(), TypeRange.getBegin(), |
| 280 | TypeRange.getEnd()); |
| 281 | } |
| 282 | |
| 283 | /// \brief Create a copy initialization. |
| 284 | static InitializationKind CreateCopy(SourceLocation InitLoc, |
| 285 | SourceLocation EqualLoc) { |
| 286 | return InitializationKind(SIK_Copy, InitLoc, EqualLoc, EqualLoc); |
| 287 | } |
| 288 | |
| 289 | /// \brief Create a default initialization. |
| 290 | static InitializationKind CreateDefault(SourceLocation InitLoc) { |
| 291 | return InitializationKind(SIK_Default, InitLoc, InitLoc, InitLoc); |
| 292 | } |
| 293 | |
| 294 | /// \brief Create a value initialization. |
| 295 | static InitializationKind CreateValue(SourceLocation InitLoc, |
| 296 | SourceLocation LParenLoc, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 297 | SourceLocation RParenLoc, |
| 298 | bool isImplicit = false) { |
| 299 | return InitializationKind(isImplicit? SIK_ImplicitValue : SIK_Value, |
| 300 | InitLoc, LParenLoc, RParenLoc); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// \brief Determine the initialization kind. |
| 304 | InitKind getKind() const { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 305 | if (Kind > SIK_ImplicitValue) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 306 | return IK_Direct; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 307 | if (Kind == SIK_ImplicitValue) |
| 308 | return IK_Value; |
| 309 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 310 | return (InitKind)Kind; |
| 311 | } |
| 312 | |
| 313 | /// \brief Determine whether this initialization is an explicit cast. |
| 314 | bool isExplicitCast() const { |
| 315 | return Kind == SIK_DirectCast || Kind == SIK_DirectCStyleOrFunctionalCast; |
| 316 | } |
| 317 | |
| 318 | /// \brief Determine whether this initialization is a C-style cast. |
| 319 | bool isCStyleOrFunctionalCast() const { |
| 320 | return Kind == SIK_DirectCStyleOrFunctionalCast; |
| 321 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 322 | |
| 323 | /// \brief Determine whether this initialization is an implicit |
| 324 | /// value-initialization, e.g., as occurs during aggregate |
| 325 | /// initialization. |
| 326 | bool isImplicitValueInit() const { return Kind == SIK_ImplicitValue; } |
| 327 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 328 | /// \brief Retrieve the location at which initialization is occurring. |
| 329 | SourceLocation getLocation() const { return Locations[0]; } |
| 330 | |
| 331 | /// \brief Retrieve the source range that covers the initialization. |
| 332 | SourceRange getRange() const { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 333 | return SourceRange(Locations[0], Locations[2]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /// \brief Retrieve the location of the equal sign for copy initialization |
| 337 | /// (if present). |
| 338 | SourceLocation getEqualLoc() const { |
| 339 | assert(Kind == SIK_Copy && "Only copy initialization has an '='"); |
| 340 | return Locations[1]; |
| 341 | } |
| 342 | |
| 343 | /// \brief Retrieve the source range containing the locations of the open |
| 344 | /// and closing parentheses for value and direct initializations. |
| 345 | SourceRange getParenRange() const { |
| 346 | assert((getKind() == IK_Direct || Kind == SIK_Value) && |
| 347 | "Only direct- and value-initialization have parentheses"); |
| 348 | return SourceRange(Locations[1], Locations[2]); |
| 349 | } |
| 350 | }; |
| 351 | |
| 352 | /// \brief Describes the sequence of initializations required to initialize |
| 353 | /// a given object or reference with a set of arguments. |
| 354 | class InitializationSequence { |
| 355 | public: |
| 356 | /// \brief Describes the kind of initialization sequence computed. |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 357 | /// |
| 358 | /// FIXME: Much of this information is in the initialization steps... why is |
| 359 | /// it duplicated here? |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 360 | enum SequenceKind { |
| 361 | /// \brief A failed initialization sequence. The failure kind tells what |
| 362 | /// happened. |
| 363 | FailedSequence = 0, |
| 364 | |
| 365 | /// \brief A dependent initialization, which could not be |
| 366 | /// type-checked due to the presence of dependent types or |
| 367 | /// dependently-type expressions. |
| 368 | DependentSequence, |
| 369 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 370 | /// \brief A user-defined conversion sequence. |
| 371 | UserDefinedConversion, |
| 372 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 373 | /// \brief A constructor call. |
Douglas Gregor | a6ca650 | 2009-12-14 20:57:13 +0000 | [diff] [blame] | 374 | ConstructorInitialization, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 376 | /// \brief A reference binding. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 377 | ReferenceBinding, |
| 378 | |
| 379 | /// \brief List initialization |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 380 | ListInitialization, |
| 381 | |
| 382 | /// \brief Zero-initialization. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 383 | ZeroInitialization, |
| 384 | |
| 385 | /// \brief No initialization required. |
| 386 | NoInitialization, |
| 387 | |
| 388 | /// \brief Standard conversion sequence. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 389 | StandardConversion, |
| 390 | |
| 391 | /// \brief C conversion sequence. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame^] | 392 | CAssignment, |
| 393 | |
| 394 | /// \brief String initialization |
| 395 | StringInit |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | /// \brief Describes the kind of a particular step in an initialization |
| 399 | /// sequence. |
| 400 | enum StepKind { |
| 401 | /// \brief Resolve the address of an overloaded function to a specific |
| 402 | /// function declaration. |
| 403 | SK_ResolveAddressOfOverloadedFunction, |
| 404 | /// \brief Perform a derived-to-base cast, producing an rvalue. |
| 405 | SK_CastDerivedToBaseRValue, |
| 406 | /// \brief Perform a derived-to-base cast, producing an lvalue. |
| 407 | SK_CastDerivedToBaseLValue, |
| 408 | /// \brief Reference binding to an lvalue. |
| 409 | SK_BindReference, |
| 410 | /// \brief Reference binding to a temporary. |
| 411 | SK_BindReferenceToTemporary, |
| 412 | /// \brief Perform a user-defined conversion, either via a conversion |
| 413 | /// function or via a constructor. |
| 414 | SK_UserConversion, |
| 415 | /// \brief Perform a qualification conversion, producing an rvalue. |
| 416 | SK_QualificationConversionRValue, |
| 417 | /// \brief Perform a qualification conversion, producing an lvalue. |
| 418 | SK_QualificationConversionLValue, |
| 419 | /// \brief Perform an implicit conversion sequence. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 420 | SK_ConversionSequence, |
| 421 | /// \brief Perform list-initialization |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 422 | SK_ListInitialization, |
| 423 | /// \brief Perform initialization via a constructor. |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 424 | SK_ConstructorInitialization, |
| 425 | /// \brief Zero-initialize the object |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 426 | SK_ZeroInitialization, |
| 427 | /// \brief C assignment |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame^] | 428 | SK_CAssignment, |
| 429 | /// \brief Initialization by string |
| 430 | SK_StringInit |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | /// \brief A single step in the initialization sequence. |
| 434 | class Step { |
| 435 | public: |
| 436 | /// \brief The kind of conversion or initialization step we are taking. |
| 437 | StepKind Kind; |
| 438 | |
| 439 | // \brief The type that results from this initialization. |
| 440 | QualType Type; |
| 441 | |
| 442 | union { |
| 443 | /// \brief When Kind == SK_ResolvedOverloadedFunction or Kind == |
| 444 | /// SK_UserConversion, the function that the expression should be |
| 445 | /// resolved to or the conversion function to call, respectively. |
| 446 | FunctionDecl *Function; |
| 447 | |
| 448 | /// \brief When Kind = SK_ConversionSequence, the implicit conversion |
| 449 | /// sequence |
| 450 | ImplicitConversionSequence *ICS; |
| 451 | }; |
| 452 | |
| 453 | void Destroy(); |
| 454 | }; |
| 455 | |
| 456 | private: |
| 457 | /// \brief The kind of initialization sequence computed. |
| 458 | enum SequenceKind SequenceKind; |
| 459 | |
| 460 | /// \brief Steps taken by this initialization. |
| 461 | llvm::SmallVector<Step, 4> Steps; |
| 462 | |
| 463 | public: |
| 464 | /// \brief Describes why initialization failed. |
| 465 | enum FailureKind { |
| 466 | /// \brief Too many initializers provided for a reference. |
| 467 | FK_TooManyInitsForReference, |
| 468 | /// \brief Array must be initialized with an initializer list. |
| 469 | FK_ArrayNeedsInitList, |
| 470 | /// \brief Array must be initialized with an initializer list or a |
| 471 | /// string literal. |
| 472 | FK_ArrayNeedsInitListOrStringLiteral, |
| 473 | /// \brief Cannot resolve the address of an overloaded function. |
| 474 | FK_AddressOfOverloadFailed, |
| 475 | /// \brief Overloading due to reference initialization failed. |
| 476 | FK_ReferenceInitOverloadFailed, |
| 477 | /// \brief Non-const lvalue reference binding to a temporary. |
| 478 | FK_NonConstLValueReferenceBindingToTemporary, |
| 479 | /// \brief Non-const lvalue reference binding to an lvalue of unrelated |
| 480 | /// type. |
| 481 | FK_NonConstLValueReferenceBindingToUnrelated, |
| 482 | /// \brief Rvalue reference binding to an lvalue. |
| 483 | FK_RValueReferenceBindingToLValue, |
| 484 | /// \brief Reference binding drops qualifiers. |
| 485 | FK_ReferenceInitDropsQualifiers, |
| 486 | /// \brief Reference binding failed. |
| 487 | FK_ReferenceInitFailed, |
| 488 | /// \brief Implicit conversion failed. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 489 | FK_ConversionFailed, |
| 490 | /// \brief Too many initializers for scalar |
| 491 | FK_TooManyInitsForScalar, |
| 492 | /// \brief Reference initialization from an initializer list |
| 493 | FK_ReferenceBindingToInitList, |
| 494 | /// \brief Initialization of some unused destination type with an |
| 495 | /// initializer list. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 496 | FK_InitListBadDestinationType, |
| 497 | /// \brief Overloading for a user-defined conversion failed. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 498 | FK_UserConversionOverloadFailed, |
| 499 | /// \brief Overloaded for initialization by constructor failed. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 500 | FK_ConstructorOverloadFailed, |
| 501 | /// \brief Default-initialization of a 'const' object. |
| 502 | FK_DefaultInitOfConst |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | private: |
| 506 | /// \brief The reason why initialization failued. |
| 507 | FailureKind Failure; |
| 508 | |
| 509 | /// \brief The failed result of overload resolution. |
| 510 | OverloadingResult FailedOverloadResult; |
| 511 | |
| 512 | /// \brief The candidate set created when initialization failed. |
| 513 | OverloadCandidateSet FailedCandidateSet; |
| 514 | |
| 515 | public: |
| 516 | /// \brief Try to perform initialization of the given entity, creating a |
| 517 | /// record of the steps required to perform the initialization. |
| 518 | /// |
| 519 | /// The generated initialization sequence will either contain enough |
| 520 | /// information to diagnose |
| 521 | /// |
| 522 | /// \param S the semantic analysis object. |
| 523 | /// |
| 524 | /// \param Entity the entity being initialized. |
| 525 | /// |
| 526 | /// \param Kind the kind of initialization being performed. |
| 527 | /// |
| 528 | /// \param Args the argument(s) provided for initialization. |
| 529 | /// |
| 530 | /// \param NumArgs the number of arguments provided for initialization. |
| 531 | InitializationSequence(Sema &S, |
| 532 | const InitializedEntity &Entity, |
| 533 | const InitializationKind &Kind, |
| 534 | Expr **Args, |
| 535 | unsigned NumArgs); |
| 536 | |
| 537 | ~InitializationSequence(); |
| 538 | |
| 539 | /// \brief Perform the actual initialization of the given entity based on |
| 540 | /// the computed initialization sequence. |
| 541 | /// |
| 542 | /// \param S the semantic analysis object. |
| 543 | /// |
| 544 | /// \param Entity the entity being initialized. |
| 545 | /// |
| 546 | /// \param Kind the kind of initialization being performed. |
| 547 | /// |
| 548 | /// \param Args the argument(s) provided for initialization, ownership of |
| 549 | /// which is transfered into the routine. |
| 550 | /// |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 551 | /// \param ResultType if non-NULL, will be set to the type of the |
| 552 | /// initialized object, which is the type of the declaration in most |
| 553 | /// cases. However, when the initialized object is a variable of |
| 554 | /// incomplete array type and the initializer is an initializer |
| 555 | /// list, this type will be set to the completed array type. |
| 556 | /// |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 557 | /// \returns an expression that performs the actual object initialization, if |
| 558 | /// the initialization is well-formed. Otherwise, emits diagnostics |
| 559 | /// and returns an invalid expression. |
| 560 | Action::OwningExprResult Perform(Sema &S, |
| 561 | const InitializedEntity &Entity, |
| 562 | const InitializationKind &Kind, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 563 | Action::MultiExprArg Args, |
| 564 | QualType *ResultType = 0); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 565 | |
| 566 | /// \brief Diagnose an potentially-invalid initialization sequence. |
| 567 | /// |
| 568 | /// \returns true if the initialization sequence was ill-formed, |
| 569 | /// false otherwise. |
| 570 | bool Diagnose(Sema &S, |
| 571 | const InitializedEntity &Entity, |
| 572 | const InitializationKind &Kind, |
| 573 | Expr **Args, unsigned NumArgs); |
| 574 | |
| 575 | /// \brief Determine the kind of initialization sequence computed. |
| 576 | enum SequenceKind getKind() const { return SequenceKind; } |
| 577 | |
| 578 | /// \brief Set the kind of sequence computed. |
| 579 | void setSequenceKind(enum SequenceKind SK) { SequenceKind = SK; } |
| 580 | |
| 581 | /// \brief Determine whether the initialization sequence is valid. |
| 582 | operator bool() const { return SequenceKind != FailedSequence; } |
| 583 | |
| 584 | typedef llvm::SmallVector<Step, 4>::const_iterator step_iterator; |
| 585 | step_iterator step_begin() const { return Steps.begin(); } |
| 586 | step_iterator step_end() const { return Steps.end(); } |
| 587 | |
| 588 | /// \brief Add a new step in the initialization that resolves the address |
| 589 | /// of an overloaded function to a specific function declaration. |
| 590 | /// |
| 591 | /// \param Function the function to which the overloaded function reference |
| 592 | /// resolves. |
| 593 | void AddAddressOverloadResolutionStep(FunctionDecl *Function); |
| 594 | |
| 595 | /// \brief Add a new step in the initialization that performs a derived-to- |
| 596 | /// base cast. |
| 597 | /// |
| 598 | /// \param BaseType the base type to which we will be casting. |
| 599 | /// |
| 600 | /// \param IsLValue true if the result of this cast will be treated as |
| 601 | /// an lvalue. |
| 602 | void AddDerivedToBaseCastStep(QualType BaseType, bool IsLValue); |
| 603 | |
| 604 | /// \brief Add a new step binding a reference to an object. |
| 605 | /// |
| 606 | /// \param BindingTemporary true if we are binding a reference to a temporary |
| 607 | /// object (thereby extending its lifetime); false if we are binding to an |
| 608 | /// lvalue or an lvalue treated as an rvalue. |
| 609 | void AddReferenceBindingStep(QualType T, bool BindingTemporary); |
| 610 | |
| 611 | /// \brief Add a new step invoking a conversion function, which is either |
| 612 | /// a constructor or a conversion function. |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 613 | void AddUserConversionStep(FunctionDecl *Function, QualType T); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 614 | |
| 615 | /// \brief Add a new step that performs a qualification conversion to the |
| 616 | /// given type. |
| 617 | void AddQualificationConversionStep(QualType Ty, bool IsLValue); |
| 618 | |
| 619 | /// \brief Add a new step that applies an implicit conversion sequence. |
| 620 | void AddConversionSequenceStep(const ImplicitConversionSequence &ICS, |
| 621 | QualType T); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 622 | |
| 623 | /// \brief Add a list-initialiation step |
| 624 | void AddListInitializationStep(QualType T); |
| 625 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 626 | /// \brief Add a constructor-initialization step. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 627 | void AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 628 | QualType T); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 629 | |
| 630 | /// \brief Add a zero-initialization step. |
| 631 | void AddZeroInitializationStep(QualType T); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 633 | /// \brief Add a C assignment step. |
| 634 | // |
| 635 | // FIXME: It isn't clear whether this should ever be needed; |
| 636 | // ideally, we would handle everything needed in C in the common |
| 637 | // path. However, that isn't the case yet. |
| 638 | void AddCAssignmentStep(QualType T); |
| 639 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame^] | 640 | /// \brief Add a string init step. |
| 641 | void AddStringInitStep(QualType T); |
| 642 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 643 | /// \brief Note that this initialization sequence failed. |
| 644 | void SetFailed(FailureKind Failure) { |
| 645 | SequenceKind = FailedSequence; |
| 646 | this->Failure = Failure; |
| 647 | } |
| 648 | |
| 649 | /// \brief Note that this initialization sequence failed due to failed |
| 650 | /// overload resolution. |
| 651 | void SetOverloadFailure(FailureKind Failure, OverloadingResult Result); |
| 652 | |
| 653 | /// \brief Retrieve a reference to the candidate set when overload |
| 654 | /// resolution fails. |
| 655 | OverloadCandidateSet &getFailedCandidateSet() { |
| 656 | return FailedCandidateSet; |
| 657 | } |
| 658 | |
| 659 | /// \brief Determine why initialization failed. |
| 660 | FailureKind getFailureKind() const { |
| 661 | assert(getKind() == FailedSequence && "Not an initialization failure!"); |
| 662 | return Failure; |
| 663 | } |
| 664 | }; |
| 665 | |
| 666 | } // end namespace clang |
| 667 | |
| 668 | #endif // LLVM_CLANG_SEMA_INIT_H |