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