File indexing completed on 2024-06-16 04:23:17

0001 /*
0002     SPDX-FileCopyrightText: 2006 Roberto Raggi <roberto@kdevelop.org>
0003     SPDX-FileCopyrightText: 2006-2008 Hamish Rodda <rodda@kde.org>
0004     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KDEVPLATFORM_TYPESYSTEM_H
0010 #define KDEVPLATFORM_TYPESYSTEM_H
0011 
0012 #include "typepointer.h"
0013 #include "../identifier.h"
0014 #include "abstracttype.h"
0015 
0016 namespace KDevelop {
0017 class AbstractTypeDataRequest;
0018 
0019 class AbstractType;
0020 class IntegralType;
0021 class PointerType;
0022 class ReferenceType;
0023 class FunctionType;
0024 class StructureType;
0025 class ArrayType;
0026 
0027 class TypeExchanger;
0028 
0029 class KDEVPLATFORMLANGUAGE_EXPORT TypeVisitor
0030 {
0031 public:
0032     virtual ~TypeVisitor ();
0033 
0034     virtual bool preVisit (const AbstractType*) = 0;
0035     virtual void postVisit (const AbstractType*) = 0;
0036 
0037     ///Return whether sub-types should be visited(same for the other visit functions)
0038     virtual bool visit(const AbstractType*) = 0;
0039 
0040     virtual void visit (const IntegralType*) = 0;
0041 
0042     virtual bool visit (const PointerType*) = 0;
0043     virtual void endVisit (const PointerType*) = 0;
0044 
0045     virtual bool visit (const ReferenceType*) = 0;
0046     virtual void endVisit (const ReferenceType*) = 0;
0047 
0048     virtual bool visit (const FunctionType*) = 0;
0049     virtual void endVisit (const FunctionType*) = 0;
0050 
0051     virtual bool visit (const StructureType*) = 0;
0052     virtual void endVisit (const StructureType*) = 0;
0053 
0054     virtual bool visit (const ArrayType*) = 0;
0055     virtual void endVisit (const ArrayType*) = 0;
0056 };
0057 
0058 class KDEVPLATFORMLANGUAGE_EXPORT SimpleTypeVisitor
0059     : public TypeVisitor
0060 {
0061 public:
0062     /// When using SimpleTypeVisitor, the visit taking an AbstractType is the only function
0063     /// you must override to collect all types.
0064     using TypeVisitor::visit;
0065 
0066     bool preVisit (const AbstractType*) override;
0067     void postVisit (const AbstractType*) override;
0068 
0069     void visit (const IntegralType*) override;
0070 
0071     bool visit (const PointerType*) override;
0072     void endVisit (const PointerType*) override;
0073 
0074     bool visit (const ReferenceType*) override;
0075     void endVisit (const ReferenceType*) override;
0076 
0077     bool visit (const FunctionType*) override;
0078     void endVisit (const FunctionType*) override;
0079 
0080     bool visit (const StructureType*) override;
0081     void endVisit (const StructureType*) override;
0082 
0083     bool visit (const ArrayType*) override;
0084     void endVisit (const ArrayType*) override;
0085 };
0086 
0087 /**
0088  * A class that can be used to walk through all types that are references from one type, and exchange them with other types.
0089  * Examples for such types: Base-classes of a class, function-argument types of a function, etc.
0090  * */
0091 class KDEVPLATFORMLANGUAGE_EXPORT TypeExchanger
0092 {
0093 public:
0094     virtual ~TypeExchanger()
0095     {
0096     }
0097 
0098     /**
0099      * By default should return the given type, and can return another type that the given should be replaced with.
0100      * Types should allow replacing all their held types using this from within their exchangeTypes function.
0101      * The default-implementation recurses the exchange, so should be called from within the derived implementation if that is wished.
0102      * */
0103     virtual AbstractType::Ptr exchange(const AbstractType::Ptr&);
0104 };
0105 
0106 ///A simple type-exchanger that replaces one type with another
0107 class KDEVPLATFORMLANGUAGE_EXPORT SimpleTypeExchanger
0108     : public TypeExchanger
0109 {
0110 public:
0111     SimpleTypeExchanger(const AbstractType::Ptr& replace, const AbstractType::Ptr& replaceWith);
0112     AbstractType::Ptr exchange(const AbstractType::Ptr&) override;
0113 
0114 private:
0115     AbstractType::Ptr m_replace, m_replaceWith;
0116 };
0117 }
0118 
0119 #endif // KDEVPLATFORM_TYPESYSTEM_H