diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-02-20 03:19:35 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-02-20 03:19:35 +0000 |
commit | 34b41d939a1328f484511c6002ba2456db879a29 (patch) | |
tree | 9631c2ce7dde0b9c680fe463d5718d0cb8f92e45 | |
parent | 9ed9a250180f19b2c44df83a196fc3a2ac82f817 (diff) |
Implement the C++0x deduced 'auto' feature.
This fixes PR 8738, 9060 and 9132.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126069 91177308-0d34-0410-b5e6-96231b3b80d8
49 files changed, 1019 insertions, 189 deletions
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 1ddeaf1c46..0e887133d0 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -412,7 +412,7 @@ public: CanQualType FloatTy, DoubleTy, LongDoubleTy; CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; CanQualType VoidPtrTy, NullPtrTy; - CanQualType OverloadTy, UndeducedAutoTy; + CanQualType OverloadTy; CanQualType DependentTy; CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy; @@ -740,6 +740,9 @@ public: /// getDecltypeType - C++0x decltype. QualType getDecltypeType(Expr *e) const; + /// getAutoType - C++0x deduced auto type. + QualType getAutoType(QualType DeducedType) const; + /// getTagDeclType - Return the unique reference to the type for the /// specified TagDecl (struct/union/class/enum) decl. QualType getTagDeclType(const TagDecl *Decl) const; diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index dd0401f23e..ee515da083 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -651,6 +651,10 @@ private: /// \brief Whether this local variable could be allocated in the return /// slot of its function, enabling the named return value optimization (NRVO). bool NRVOVariable : 1; + + /// \brief Whether this variable has a deduced C++0x auto type for which we're + /// currently parsing the initializer. + bool ParsingAutoInit : 1; friend class StmtIteratorBase; friend class ASTDeclReader; @@ -661,7 +665,7 @@ protected: StorageClass SCAsWritten) : DeclaratorDecl(DK, DC, L, Id, T, TInfo), Init(), ThreadSpecified(false), HasCXXDirectInit(false), - ExceptionVar(false), NRVOVariable(false) { + ExceptionVar(false), NRVOVariable(false), ParsingAutoInit(false) { SClass = SC; SClassAsWritten = SCAsWritten; } @@ -885,6 +889,18 @@ public: void setInit(Expr *I); + /// \brief Check whether we are in the process of parsing an initializer + /// needed to deduce the type of this variable. + bool isParsingAutoInit() const { + return ParsingAutoInit; + } + + /// \brief Note whether we are currently parsing an initializer needed to + /// deduce the type of this variable. + void setParsingAutoInit(bool P) { + ParsingAutoInit = P; + } + EvaluatedStmt *EnsureEvaluatedStmt() const { EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); if (!Eval) { diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h index ade0b2a799..921b799b94 100644 --- a/include/clang/AST/RecursiveASTVisitor.h +++ b/include/clang/AST/RecursiveASTVisitor.h @@ -715,6 +715,10 @@ DEF_TRAVERSE_TYPE(DecltypeType, { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) +DEF_TRAVERSE_TYPE(AutoType, { + TRY_TO(TraverseType(T->getDeducedType())); + }) + DEF_TRAVERSE_TYPE(RecordType, { }) DEF_TRAVERSE_TYPE(EnumType, { }) DEF_TRAVERSE_TYPE(TemplateTypeParmType, { }) @@ -923,6 +927,10 @@ DEF_TRAVERSE_TYPELOC(DecltypeType, { TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr())); }) +DEF_TRAVERSE_TYPELOC(AutoType, { + TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType())); + }) + DEF_TRAVERSE_TYPELOC(RecordType, { }) DEF_TRAVERSE_TYPELOC(EnumType, { }) DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { }) diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 8a983fb8a1..9b177cceed 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -1328,6 +1328,11 @@ public: /// because the type is a RecordType or because it is the injected-class-name /// type of a class template or class template partial specialization. CXXRecordDecl *getAsCXXRecordDecl() const; + + /// \brief Get the AutoType whose type will be deduced for a variable with + /// an initializer of this type. This looks through declarators like pointer + /// types, but not through decltype or typedefs. + AutoType *getContainedAutoType() const; /// Member-template getAs<specific type>'. Look through sugar for /// an instance of <specific type>. This scheme will eventually @@ -1478,9 +1483,6 @@ public: Overload, // This represents the type of an overloaded function declaration. - UndeducedAuto, // In C++0x, this represents the type of an auto variable - // that has not been deduced yet. - /// The primitive Objective C 'id' type. The type pointed to by the /// user-visible 'id' type. Only ever shows up in an AST as the base /// type of an ObjCObjectType. @@ -1528,8 +1530,7 @@ public: /// i.e. a type which cannot appear in arbitrary positions in a /// fully-formed expression. bool isPlaceholderType() const { - return getKind() == Overload || - getKind() == UndeducedAuto; + return getKind() == Overload; } static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } @@ -3014,6 +3015,48 @@ public: static bool classof(const SubstTemplateTypeParmPackType *T) { return true; } }; +/// \brief Represents a C++0x auto type. +/// +/// These types are usually a placeholder for a deduced type. However, within +/// templates and before the initializer is attached, there is no deduced type +/// and an auto type is type-dependent and canonical. +class AutoType : public Type, public llvm::FoldingSetNode { + AutoType(QualType DeducedType) + : Type(Auto, DeducedType.isNull() ? QualType(this, 0) : DeducedType, + /*Dependent=*/DeducedType.isNull(), + /*VariablyModified=*/false, /*ContainsParameterPack=*/false) { + assert((DeducedType.isNull() || !DeducedType->isDependentType()) && + "deduced a dependent type for auto"); + } + + friend class ASTContext; // ASTContext creates these + +public: + bool isSugared() const { return isDeduced(); } + QualType desugar() const { return getCanonicalTypeInternal(); } + + QualType getDeducedType() const { + return isDeduced() ? getCanonicalTypeInternal() : QualType(); + } + bool isDeduced() const { + return !isDependentType(); + } + + void Profile(llvm::FoldingSetNodeID &ID) { + Profile(ID, getDeducedType()); + } + + static void Profile(llvm::FoldingSetNodeID &ID, + QualType Deduced) { + ID.AddPointer(Deduced.getAsOpaquePtr()); + } + + static bool classof(const Type *T) { + return T->getTypeClass() == Auto; + } + static bool classof(const AutoType *T) { return true; } +}; + /// \brief Represents the type of a template specialization as written /// in the source code. /// diff --git a/include/clang/AST/TypeLoc.h b/include/clang/AST/TypeLoc.h index 8af6bbd053..c7f5ee7633 100644 --- a/include/clang/AST/TypeLoc.h +++ b/include/clang/AST/TypeLoc.h @@ -1381,6 +1381,11 @@ class DecltypeTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc, DecltypeType> { }; +class AutoTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc, + AutoTypeLoc, + AutoType> { +}; + struct ElaboratedLocInfo { SourceLocation KeywordLoc; SourceRange QualifierRange; diff --git a/include/clang/AST/TypeNodes.def b/include/clang/AST/TypeNodes.def index 3587767f8f..b2591cc0fb 100644 --- a/include/clang/AST/TypeNodes.def +++ b/include/clang/AST/TypeNodes.def @@ -93,6 +93,7 @@ DEPENDENT_TYPE(TemplateTypeParm, Type) NON_CANONICAL_TYPE(SubstTemplateTypeParm, Type) DEPENDENT_TYPE(SubstTemplateTypeParmPack, Type) NON_CANONICAL_UNLESS_DEPENDENT_TYPE(TemplateSpecialization, Type) +NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Auto, Type) DEPENDENT_TYPE(InjectedClassName, Type) DEPENDENT_TYPE(DependentName, Type) DEPENDENT_TYPE(DependentTemplateSpecialization, Type) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 803df69da1..2e7f274b8e 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -892,16 +892,31 @@ def err_cannot_determine_declared_type_of_overloaded_function : Error< def err_auto_variable_cannot_appear_in_own_initializer : Error< "variable %0 declared with 'auto' type cannot appear in its own initializer">; def err_illegal_decl_array_of_auto : Error< - "'%0' declared as array of 'auto'">; + "'%0' declared as array of %1">; +def err_new_array_of_auto : Error< + "cannot allocate array of 'auto'">; def err_auto_not_allowed : Error< - "'auto' not allowed in %select{function prototype|struct member|union member" - "|class member|exception declaration|template parameter|block literal}0">; + "'auto' not allowed %select{in function prototype|in struct member" + "|in union member|in class member|in exception declaration" + "|in template parameter|in block literal|in template argument|here}0">; def err_auto_var_requires_init : Error< "declaration of variable %0 with type %1 requires an initializer">; +def err_auto_new_requires_ctor_arg : Error< + "new expression for type %0 requires a constructor argument">; +def err_auto_var_init_multiple_expressions : Error< + "initializer for variable %0 with type %1 contains multiple expressions">; +def err_auto_new_ctor_multiple_expressions : Error< + "new expression for type %0 contains multiple constructor arguments">; def err_auto_missing_trailing_return : Error< "'auto' return without trailing return type">; def err_trailing_return_without_auto : Error< - "trailing return type without 'auto' return">; + "function with trailing return type must specify return type 'auto', not %0">; +def err_auto_var_deduction_failure : Error< + "variable %0 with type %1 has incompatible initializer of type %2">; +def err_auto_new_deduction_failure : Error< + "new expression for type %0 has incompatible constructor argument of type %1">; +def err_auto_different_deductions : Error< + "'auto' deduced as %0 in declaration of %1 and deduced as %2 in declaration of %3">; // C++0x override control def override_keyword_only_allowed_on_virtual_member_functions : Error< diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index c95ce62eec..91d6914f24 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -730,7 +730,8 @@ public: QualType BuildParenType(QualType T); TypeSourceInfo *GetTypeForDeclarator(Declarator &D, Scope *S, - TagDecl **OwnedDecl = 0); + TagDecl **OwnedDecl = 0, + bool AllowAutoInTypeName = false); TypeSourceInfo *GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, TypeSourceInfo *ReturnTypeInfo); /// \brief Package the given type and TSI into a ParsedType. @@ -850,9 +851,9 @@ public: bool SetParamDefaultArgument(ParmVarDecl *Param, Expr *DefaultArg, SourceLocation EqualLoc); - void AddInitializerToDecl(Decl *dcl, Expr *init); - void AddInitializerToDecl(Decl *dcl, Expr *init, bool DirectInit); - void ActOnUninitializedDecl(Decl *dcl, bool TypeContainsUndeducedAuto); + void AddInitializerToDecl(Decl *dcl, Expr *init, bool DirectInit, + bool TypeMayContainAuto); + void ActOnUninitializedDecl(Decl *dcl, bool TypeMayContainAuto); void ActOnInitializerError(Decl *Dcl); void SetDeclDeleted(Decl *dcl, SourceLocation DelLoc); DeclGroupPtrTy FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, @@ -1058,6 +1059,7 @@ public: void MergeTypeDefDecl(TypedefDecl *New, LookupResult &OldDecls); bool MergeFunctionDecl(FunctionDecl *New, Decl *Old); bool MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old); + void MergeVarDeclTypes(VarDecl *New, VarDecl *Old); void MergeVarDecl(VarDecl *New, LookupResult &OldDecls); bool MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old); @@ -2237,7 +2239,8 @@ public: void AddCXXDirectInitializerToDecl(Decl *Dcl, SourceLocation LParenLoc, MultiExprArg Exprs, - SourceLocation RParenLoc); + SourceLocation RParenLoc, + bool TypeMayContainAuto); /// InitializeVarWithConstructor - Creates an CXXConstructExpr /// and sets it as the initializer for the the passed in VarDecl. @@ -2458,7 +2461,8 @@ public: Expr *ArraySize, SourceLocation ConstructorLParen, MultiExprArg ConstructorArgs, - SourceLocation ConstructorRParen); + SourceLocation ConstructorRParen, + bool TypeMayContainAuto = true); bool CheckAllocatedType(QualType AllocType, SourceLocation Loc, SourceRange R); @@ -3706,6 +3710,8 @@ public: FunctionDecl *&Specialization, sema::TemplateDeductionInfo &Info); + bool DeduceAutoType(QualType AutoType, Expr *Initializer, QualType &Result); + FunctionTemplateDecl *getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, FunctionTemplateDecl *FT2, SourceLocation Loc, diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index 5b77dff7f2..68fd91d4c0 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -578,7 +578,9 @@ namespace clang { /// \brief An AttributedType record. TYPE_ATTRIBUTED = 36, /// \brief A SubstTemplateTypeParmPackType record. - TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK = 37 + TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK = 37, + /// \brief A AutoType record. + TYPE_AUTO = 38 }; /// \brief The type IDs for special types constructed by semantic diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 50c295f241..945dfb87f2 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -380,10 +380,6 @@ void ASTContext::InitBuiltinTypes() { // Placeholder type for functions. InitBuiltinType(OverloadTy, BuiltinType::Overload); - // Placeholder type for C++0x auto declarations whose real type has - // not yet been deduced. - InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); - // C99 6.2.5p11. FloatComplexTy = getComplexType(FloatTy); DoubleComplexTy = getComplexType(DoubleTy); @@ -875,6 +871,12 @@ ASTContext::getTypeInfo(const Type *T) const { return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> getReplacementType().getTypePtr()); + case Type::Auto: { + const AutoType *A = cast<AutoType>(T); + assert(A->isDeduced() && "Cannot request the size of a dependent type"); + return getTypeInfo(cast<AutoType>(T)->getDeducedType().getTypePtr()); + } + case Type::Paren: return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); @@ -1532,6 +1534,7 @@ QualType ASTContext::getVariableArrayDecayedType(QualType type) const { case Type::DependentTemplateSpecialization: case Type::TemplateTypeParm: case Type::SubstTemplateTypeParmPack: + case Type::Auto: case Type::PackExpansion: llvm_unreachable("type should never be variably-modified"); @@ -2680,6 +2683,14 @@ QualType ASTContext::getDecltypeType(Expr *e) const { return QualType(dt, 0); } +/// getAutoType - Unlike many "get<Type>" functions, we don't unique +/// AutoType AST's. +QualType ASTContext::getAutoType(QualType DeducedType) const { + AutoType *at = new (*this, TypeAlignment) AutoType(DeducedType); + Types.push_back(at); + return QualType(at, 0); +} + /// getTagDeclType - Return the unique reference to the type for the /// specified TagDecl (struct/union/class/enum) decl. QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { diff --git a/lib/AST/ASTDiagnostic.cpp b/lib/AST/ASTDiagnostic.cpp index 9870b515c6..5bf8a38199 100644 --- a/lib/AST/ASTDiagnostic.cpp +++ b/lib/AST/ASTDiagnostic.cpp @@ -28,18 +28,26 @@ static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) { const Type *Ty = QC.strip(QT); // Don't aka just because we saw an elaborated type... - if (isa<ElaboratedType>(Ty)) { - QT = cast<ElaboratedType>(Ty)->desugar(); + if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) { + QT = ET->desugar(); continue; } // ... or a paren type ... - if (isa<ParenType>(Ty)) { - QT = cast<ParenType>(Ty)->desugar(); + if (const ParenType *PT = dyn_cast<ParenType>(Ty)) { + QT = PT->desugar(); continue; } - // ...or a substituted template type parameter. - if (isa<SubstTemplateTypeParmType>(Ty)) { - QT = cast<SubstTemplateTypeParmType>(Ty)->desugar(); + // ...or a substituted template type parameter ... + if (const SubstTemplateTypeParmType *ST = + dyn_cast<SubstTemplateTypeParmType>(Ty)) { + QT = ST->desugar(); + continue; + } + // ... or an auto type. + if (const AutoType *AT = dyn_cast<AutoType>(Ty)) { + if (!AT->isSugared()) + break; + QT = AT->desugar(); continue; } diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index a1e0070422..65c0a3bb61 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -64,6 +64,7 @@ namespace { // FIXME: DependentTypeOfExprType QualType VisitTypeOfType(const TypeOfType *T); QualType VisitDecltypeType(const DecltypeType *T); + QualType VisitAutoType(const AutoType *T); // FIXME: DependentDecltypeType QualType VisitRecordType(const RecordType *T); QualType VisitEnumType(const EnumType *T); @@ -604,6 +605,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, return false; break; + case Type::Auto: + if (!IsStructurallyEquivalent(Context, + cast<AutoType>(T1)->getDeducedType(), + cast<AutoType>(T2)->getDeducedType())) + return false; + break; + case Type::Record: case Type::Enum: if (!IsStructurallyEquivalent(Context, @@ -1347,9 +1355,6 @@ QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { case BuiltinType::Overload: return Importer.getToContext().OverloadTy; case BuiltinType::Dependent: return Importer.getToContext().DependentTy; - case BuiltinType::UndeducedAuto: - // FIXME: Make sure that the "to" context supports C++0x! - return Importer.getToContext().UndeducedAutoTy; case BuiltinType::ObjCId: // FIXME: Make sure that the "to" context supports Objective-C! @@ -1550,6 +1555,7 @@ QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { } QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { + // FIXME: Make sure that the "to" context supports C++0x! Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); if (!ToExpr) return QualType(); @@ -1557,6 +1563,19 @@ QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { return Importer.getToContext().getDecltypeType(ToExpr); } +QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { + // FIXME: Make sure that the "to" context supports C++0x! + QualType FromDeduced = T->getDeducedType(); + QualType ToDeduced; + if (!FromDeduced.isNull()) { + ToDeduced = Importer.Import(FromDeduced); + if (ToDeduced.isNull()) + return QualType(); + } + + return Importer.getToContext().getAutoType(ToDeduced); +} + QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { RecordDecl *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); diff --git a/lib/AST/ItaniumMangle.cpp b/lib/AST/ItaniumMangle.cpp index 2819a7e4b9..d66c374cbe 100644 --- a/lib/AST/ItaniumMangle.cpp +++ b/lib/AST/ItaniumMangle.cpp @@ -1313,9 +1313,6 @@ void CXXNameMangler::mangleType(const BuiltinType *T) { assert(false && "Overloaded and dependent types shouldn't get to name mangling"); break; - case BuiltinType::UndeducedAuto: - assert(0 && "Should not see undeduced auto here"); - break; case BuiltinType::ObjCId: Out << "11objc_object"; break; case BuiltinType::ObjCClass: Out << "10objc_class"; break; case BuiltinType::ObjCSel: Out << "13objc_selector"; break; @@ -1648,6 +1645,12 @@ void CXXNameMangler::mangleType(const DecltypeType *T) { Out << 'E'; } +void CXXNameMangler::mangleType(const AutoType *T) { + QualType D = T->getDeducedType(); + assert(!D.isNull() && "can't mangle undeduced auto type"); + mangleType(D); +} + void CXXNameMangler::mangleIntegerLiteral(QualType T, const llvm::APSInt &Value) { // <expr-primary> ::= L <type> <value number> E # integer literal diff --git a/lib/AST/MicrosoftMangle.cpp b/lib/AST/MicrosoftMangle.cpp index 7aafac0ad0..4bf7f23a0a 100644 --- a/lib/AST/MicrosoftMangle.cpp +++ b/lib/AST/MicrosoftMangle.cpp @@ -720,9 +720,6 @@ void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { assert(false && "Overloaded and dependent types shouldn't get to name mangling"); break; - case BuiltinType::UndeducedAuto: - assert(0 && "Should not see undeduced auto here"); - break; case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; @@ -1119,6 +1116,10 @@ void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) { assert(false && "Don't know how to mangle DecltypeTypes yet!"); } +void MicrosoftCXXNameMangler::mangleType(const AutoType *T) { + assert(false && "Don't know how to mangle AutoTypes yet!"); +} + void MicrosoftMangleContext::mangleName(const NamedDecl *D, llvm::raw_ostream &Out) { assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && diff --git |