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