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