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