[cfe-commits] [PATCH] Bug 3558: make ASTContext methods const

Jay Foad jay.foad at gmail.com
Wed Dec 15 01:24:43 PST 2010


http://llvm.org/bugs/show_bug.cgi?id=3558

The bug says:

  There are several accessors that are "logically" const but not
marked as such.
  This causes const_casts in other logically const methods like
  getLegacyIntegralTypeEncoding.  We should just mark the data they use as
  mutable or avoid calling getCanonicalType in it.

The attached patch makes ASTContext::getIntWidth() const, and then
follows it to some sort of logical conclusion, by making methods const
and fields mutable until the whole thing compiles cleanly again. (I've
summarised the changes in a bit more detail below.)

The result is that most ASTContext methods are const, and most fields
are mutable. Does this seem reasonable?


Summary of changes:

1. Change the following ASTContext fields to be mutable:
    ASTRecordLayouts
    BlockDescriptorExtendedType
    BlockDescriptorType
    BlockPointerTypes
    BumpAlloc
    CFConstantStringTypeDecl
    CanonTemplateTemplateParms
    ComplexTypes
    ConstantArrayTypes
    DeclarationNames
    DependentDecltypeTypes
    DependentNameTypes
    DependentSizedArrayTypes
    DependentSizedExtVectorTypes
    DependentTemplateNames
    DependentTypeOfExprTypes
    ElaboratedTypes
    ExtQualNodes
    FunctionNoProtoTypes
    FunctionProtoTypes
    GlobalNestedNameSpecifier
    IncompleteArrayTypes
    LValueReferenceTypes
    MemberPointerTypes
    NSConstantStringTypeDecl
    NestedNameSpecifiers
    ObjCFastEnumerationStateTypeDecl
    ObjCLayouts
    ObjCObjectPointerTypes
    ObjCObjectTypes
    ParenTypes
    PointerTypes
    QualifiedTemplateNames
    RValueReferenceTypes
    SubstTemplateTypeParmTypes
    TemplateSpecializationTypes
    TemplateSpecializationTypes
    TemplateTypeParmTypes
    Types
    UniqueBlockByRefTypeID
    UniqueBlockParmTypeID
    VariableArrayTypes
    VectorTypes

2. Change the following ASTContext methods to be const:
    Allocate(unsigned Size, unsigned Align = 8)
    BlockRequiresCopying(QualType Ty)
    BuildByRefType(llvm::StringRef DeclName, QualType Ty)
    CountNonClassIvars(const ObjCInterfaceDecl *OI)
    CreateTypeSourceInfo(QualType T, unsigned Size = 0)
    Deallocate(void *Ptr)
    DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass,
llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars)
    DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS)
    GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, unsigned
*IntegerConstantArgs = 0)
    MakeIntValue(uint64_t Value, QualType Type)
    ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
ObjCProtocolDecl *rProto)
    ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars)
    getASTObjCImplementationLayout(const ObjCImplementationDecl *D)
    getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D)
    getASTRecordLayout(const RecordDecl *D)
    getAddrSpaceQualType(QualType T, unsigned AddressSpace)
    getArrayDecayedType(QualType T)
    getAsArrayType(QualType T)
    getAsConstantArrayType(QualType T)
    getAsDependentSizedArrayType(QualType T)
    getAsIncompleteArrayType(QualType T)
    getAsVariableArrayType(QualType T)
    getBaseElementType(QualType QT)
    getBaseElementType(const ArrayType *VAT)
    getBlockDescriptorExtendedType()
    getBlockDescriptorType()
    getBlockParmType(bool BlockHasCopyDispose,
llvm::SmallVectorImpl<const Expr *> &Layout)
    getBlockPointerType(QualType T)
    getCFConstantStringType()
    getCVRQualifiedType(QualType T, unsigned CVR)
    getCallConvType(QualType T, CallingConv CallConv)
    getCanonicalCallConv(CallingConv CC)
    getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS)
    getCanonicalParamType(QualType T)
    getCanonicalTemplateArgument(const TemplateArgument &Arg)
    getCanonicalTemplateName(TemplateName Name)
    getCanonicalTemplateSpecializationType(TemplateName T, const
TemplateArgument *Args, unsigned NumArgs)
    getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP)
    getCanonicalType(QualType T)
    getCanonicalType(const Type *T)
    getCharWidth()
    getComplexType(CanQualType T)
    getComplexType(QualType T)
    getConstType(QualType T)
    getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
ArrayType::ArraySizeModifier ASM, unsigned EltTypeQuals)
    getDeclAlign(const Decl *D, bool RefAsPointee = false)
    getDecltypeType(Expr *e)
    getDependentNameType(ElaboratedTypeKeyword Keyword,
NestedNameSpecifier *NNS, const IdentifierInfo *Name, QualType Canon =
QualType())
    getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
ArrayType::ArraySizeModifier ASM, unsigned EltTypeQuals, SourceRange
Brackets)
    getDependentSizedExtVectorType(QualType VectorType, Expr
*SizeExpr, SourceLocation AttrLoc)
    getDependentTemplateName(NestedNameSpecifier *NNS,
OverloadedOperatorKind Operator)
    getDependentTemplateName(NestedNameSpecifier *NNS, const
IdentifierInfo *Name)
    getDependentTemplateSpecializationType(ElaboratedTypeKeyword
Keyword, NestedNameSpecifier *NNS, const IdentifierInfo *Name, const
TemplateArgumentListInfo &Args)
    getDependentTemplateSpecializationType(ElaboratedTypeKeyword
Keyword, NestedNameSpecifier *NNS, const IdentifierInfo *Name,
unsigned NumArgs, const TemplateArgument *Args)
    getElaboratedType(ElaboratedTypeKeyword Keyword,
NestedNameSpecifier *NNS, QualType NamedType)
    getEnumType(const EnumDecl *Decl)
    getExtQualType(const Type *Base, Qualifiers Quals)
    getExtVectorType(QualType VectorType, unsigned NumElts)
    getFILEType()
    getFloatingTypeOrder(QualType LHS, QualType RHS)
    getFunctionNoProtoType(QualType ResultTy)
    getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info)
    getFunctionType(QualType ResultTy, const QualType *Args, unsigned
NumArgs, const FunctionProtoType::ExtProtoInfo &EPI)
    getIncompleteArrayType(QualType EltTy,
ArrayType::ArraySizeModifier ASM, unsigned EltTypeQuals)
    getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST)
    getIntWidth(QualType T)
    getIntegerRank(Type* T)
    getIntegerTypeOrder(QualType LHS, QualType RHS)
    getLValueReferenceType(QualType T, bool SpelledAsLValue = true)
    getMemberPointerType(QualType T, const Type *Cls)
    getNSConstantStringType()
    getNameForTemplate(TemplateName Name, SourceLocation NameLoc)
    getNoReturnType(QualType T, bool AddNoReturn = true)
    getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S)
    getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
    getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, const
Decl *Container, std::string &S)
    getObjCEncodingForType(QualType t, std::string &S, const FieldDecl *Field=0)
    getObjCEncodingForTypeImpl(QualType t, std::string &S, bool
ExpandPointedToStructures, bool ExpandStructures, const FieldDecl
*Field, bool OutermostType = false, bool EncodingProperty = false)
    getObjCEncodingTypeSize(QualType t)
    getObjCFastEnumerationStateType()
    getObjCGCQualType(QualType T, Qualifiers::GC gcAttr)
    getObjCInterfaceType(const ObjCInterfaceDecl *Decl)
    getObjCLayout(const ObjCInterfaceDecl *D, const
ObjCImplementationDecl *Impl)
    getObjCObjectPointerType(QualType OIT)
    getObjCObjectType(QualType Base, ObjCProtocolDecl * const
*Protocols, unsigned NumProtocols)
    getOverloadedTemplateName(UnresolvedSetIterator Begin,
UnresolvedSetIterator End)
    getParenType(QualType NamedType)
    getPointerType(CanQualType T)
    getPointerType(QualType T)
    getPreferredTypeAlign(const Type *T)
    getPromotedIntegerType(QualType PromotableType)
    getQualifiedTemplateName(NestedNameSpecifier *NNS, bool
TemplateKeyword, TemplateDecl *Template)
    getQualifiedType(QualType T, Qualifiers Qs)
    getQualifiedType(const Type *T, Qualifiers Qs)
    getRValueReferenceType(QualType T)
    getRawBlockdescriptorExtendedType()
    getRawCFConstantStringType()
    getRawNSConstantStringType()
    getRawObjCFastEnumerationStateType()
    getRecordType(const RecordDecl *Decl)
    getRegParmType(QualType T, unsigned RegParm)
    getRestrictType(QualType T)
    getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
QualType Replacement)
    getTagDeclType(const TagDecl *Decl)
    getTemplateSpecializationType(TemplateName T, const
TemplateArgument *Args, unsigned NumArgs, QualType Canon = QualType())
    getTemplateSpecializationType(TemplateName T, const
TemplateArgumentListInfo &Args, QualType Canon = QualType())
    getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation
TLoc, const TemplateArgumentListInfo &Args, QualType Canon =
QualType())
    getTemplateTypeParmType(unsigned Depth, unsigned Index, bool
ParameterPack, IdentifierInfo *Name = 0)
    getTypeAlign(QualType T)
    getTypeAlign(const Type *T)
    getTypeAlignInChars(QualType T)
    getTypeAlignInChars(const Type *T)
    getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl = 0)
    getTypeDeclTypeSlow(const TypeDecl *Decl)
    getTypeInfo(QualType T)
    getTypeInfo(const Type *T)
    getTypeOfExprType(Expr *e)
    getTypeOfType(QualType t)
    getTypeSize(QualType T)
    getTypeSize(const Type *T)
    getTypeSizeInChars(QualType T)
    getTypeSizeInChars(const Type *T)
    getTypedefType(const TypedefDecl *Decl, QualType Canon = QualType())
    getUnknownSizeVariableArrayType(QualType Ty)
    getVariableArrayDecayedType(QualType Ty)
    getVariableArrayType(QualType EltTy, Expr *NumElts,
ArrayType::ArraySizeModifier ASM, unsigned EltTypeQuals, SourceRange
Brackets)
    getVectorType(QualType VectorType, unsigned NumElts,
VectorType::VectorKind VecKind)
    getVolatileType(QualType T)
    getjmp_bufType()
    getsigjmp_bufType()
    isNearlyEmpty(const CXXRecordDecl *RD)
    isPromotableBitField(Expr *E)

3. Change lots of other AST code to use const references to
ASTContext, instead of non-const references.

4. Remove some const_cast<>s from ASTContext, as mentioned in the bug.


diffstat:
 include/clang/AST/ASTContext.h          |  376 ++++++++++++++++----------------
 include/clang/AST/Attr.h                |    8
 include/clang/AST/Decl.h                |    8
 include/clang/AST/DeclCXX.h             |    8
 include/clang/AST/DeclTemplate.h        |   10
 include/clang/AST/DeclarationName.h     |    4
 include/clang/AST/Expr.h                |   20 -
 include/clang/AST/NestedNameSpecifier.h |   13 -
 include/clang/AST/RecordLayout.h        |    4
 include/clang/AST/Stmt.h                |    2
 include/clang/AST/TemplateBase.h        |    2
 include/clang/AST/Type.h                |   40 +--
 lib/AST/ASTContext.cpp                  |  255 +++++++++++----------
 lib/AST/Decl.cpp                        |    8
 lib/AST/DeclCXX.cpp                     |   12 -
 lib/AST/DeclTemplate.cpp                |   10
 lib/AST/DeclarationName.cpp             |    2
 lib/AST/Expr.cpp                        |    4
 lib/AST/ExprConstant.cpp                |   30 +-
 lib/AST/NestedNameSpecifier.cpp         |   18 -
 lib/AST/RecordLayout.cpp                |    9
 lib/AST/RecordLayoutBuilder.cpp         |   21 +
 lib/AST/StmtProfile.cpp                 |    6
 lib/AST/TemplateBase.cpp                |    2
 lib/AST/Type.cpp                        |   27 +-
 25 files changed, 467 insertions(+), 432 deletions(-)


Comments? Or, OK to commit?

Thanks,
Jay.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mutable.diff
Type: text/x-patch
Size: 135845 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20101215/60dbec8c/attachment.bin>


More information about the cfe-commits mailing list