File indexing completed on 2024-05-12 04:39:12
0001 /* 0002 SPDX-FileCopyrightText: 2013 Olivier de Gaalon <olivier.jg@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef CURSORKINDTRAITS_H 0008 #define CURSORKINDTRAITS_H 0009 0010 #include <clang-c/Index.h> 0011 #include <language/duchain/ducontext.h> 0012 #include <language/duchain/forwarddeclaration.h> 0013 #include <language/duchain/classdeclaration.h> 0014 #include <language/duchain/classfunctiondeclaration.h> 0015 #include <language/duchain/functiondeclaration.h> 0016 #include <language/duchain/functiondefinition.h> 0017 #include <language/duchain/namespacealiasdeclaration.h> 0018 #include <language/duchain/types/integraltype.h> 0019 0020 #include "templatehelpers.h" 0021 0022 namespace CursorKindTraits { 0023 0024 using namespace KDevelop; 0025 0026 constexpr bool isClassTemplate(CXCursorKind CK) 0027 { 0028 return CK == CXCursor_ClassTemplate || CK == CXCursor_ClassTemplatePartialSpecialization; 0029 } 0030 0031 constexpr bool isClass(CXCursorKind CK) 0032 { 0033 return isClassTemplate(CK) 0034 || CK == CXCursor_StructDecl 0035 || CK == CXCursor_ClassDecl 0036 || CK == CXCursor_UnionDecl 0037 || CK == CXCursor_ObjCInterfaceDecl 0038 || CK == CXCursor_ObjCCategoryDecl 0039 || CK == CXCursor_ObjCImplementationDecl 0040 || CK == CXCursor_ObjCCategoryImplDecl; 0041 } 0042 0043 constexpr bool isFunction(CXCursorKind CK) 0044 { 0045 return CK == CXCursor_FunctionDecl 0046 || CK == CXCursor_CXXMethod 0047 || CK == CXCursor_Constructor 0048 || CK == CXCursor_Destructor 0049 || CK == CXCursor_ConversionFunction 0050 || CK == CXCursor_FunctionTemplate 0051 || CK == CXCursor_ObjCInstanceMethodDecl 0052 || CK == CXCursor_ObjCClassMethodDecl; 0053 } 0054 0055 constexpr bool isDeclaration(CXCursorKind CK) 0056 { 0057 return isClass(CK) || isFunction(CK) 0058 || CK == CXCursor_UnexposedDecl 0059 || CK == CXCursor_FieldDecl 0060 || CK == CXCursor_ObjCProtocolDecl 0061 || CK == CXCursor_ObjCPropertyDecl 0062 || CK == CXCursor_ObjCIvarDecl 0063 || CK == CXCursor_EnumConstantDecl 0064 || CK == CXCursor_VarDecl 0065 || CK == CXCursor_ParmDecl 0066 || CK == CXCursor_TypedefDecl 0067 || CK == CXCursor_TemplateTypeParameter 0068 || CK == CXCursor_NonTypeTemplateParameter 0069 || CK == CXCursor_TemplateTemplateParameter 0070 || CK == CXCursor_NamespaceAlias 0071 || CK == CXCursor_UsingDirective 0072 || CK == CXCursor_UsingDeclaration 0073 || CK == CXCursor_TypeAliasDecl 0074 || CK == CXCursor_LabelStmt; 0075 } 0076 0077 constexpr Decision isDefinition(CXCursorKind CK) 0078 { 0079 return CK == CXCursor_Namespace || CK == CXCursor_MacroDefinition ? 0080 Decision::True : 0081 isClass(CK) || isFunction(CK) || CK == CXCursor_EnumDecl ? 0082 Decision::Maybe : 0083 Decision::False; 0084 } 0085 0086 constexpr Decision isInClass(CXCursorKind CK) 0087 { 0088 return CK == CXCursor_FieldDecl 0089 || CK == CXCursor_ObjCPropertyDecl 0090 || CK == CXCursor_ObjCIvarDecl 0091 || CK == CXCursor_ObjCInstanceMethodDecl 0092 || CK == CXCursor_ObjCClassMethodDecl ? 0093 Decision::True : 0094 CK == CXCursor_Namespace 0095 || CK == CXCursor_TemplateTypeParameter 0096 || CK == CXCursor_FunctionDecl 0097 || CK == CXCursor_TemplateTemplateParameter 0098 || CK == CXCursor_NonTypeTemplateParameter 0099 || CK == CXCursor_MacroDefinition 0100 || CK == CXCursor_MacroExpansion ? 0101 Decision::False : 0102 Decision::Maybe; 0103 } 0104 0105 constexpr DUContext::ContextType contextType(CXCursorKind CK) 0106 { 0107 return CK == CXCursor_StructDecl ? DUContext::Class 0108 : CK == CXCursor_UnionDecl ? DUContext::Class 0109 : CK == CXCursor_ClassDecl ? DUContext::Class 0110 : CK == CXCursor_EnumDecl ? DUContext::Enum 0111 : CK == CXCursor_FunctionDecl ? DUContext::Function 0112 : CK == CXCursor_CXXMethod ? DUContext::Function 0113 : CK == CXCursor_Namespace ? DUContext::Namespace 0114 : CK == CXCursor_Constructor ? DUContext::Function 0115 : CK == CXCursor_Destructor ? DUContext::Function 0116 : CK == CXCursor_ConversionFunction ? DUContext::Function 0117 : CK == CXCursor_FunctionTemplate ? DUContext::Function 0118 : CK == CXCursor_ClassTemplate ? DUContext::Class 0119 : CK == CXCursor_ClassTemplatePartialSpecialization ? DUContext::Class 0120 : CK == CXCursor_MacroDefinition ? DUContext::Other 0121 : static_cast<DUContext::ContextType>(-1); 0122 } 0123 0124 constexpr bool isKDevDeclaration(CXCursorKind CK, bool isClassMember) 0125 { 0126 return !isClassMember && 0127 (CK == CXCursor_UnexposedDecl 0128 || CK == CXCursor_FieldDecl 0129 || CK == CXCursor_ObjCProtocolDecl 0130 || CK == CXCursor_ObjCPropertyDecl 0131 || CK == CXCursor_ObjCIvarDecl 0132 || CK == CXCursor_EnumConstantDecl 0133 || CK == CXCursor_VarDecl 0134 || CK == CXCursor_ParmDecl 0135 || CK == CXCursor_TypedefDecl 0136 || CK == CXCursor_TemplateTypeParameter 0137 || CK == CXCursor_NonTypeTemplateParameter 0138 || CK == CXCursor_TemplateTemplateParameter 0139 || CK == CXCursor_UsingDirective 0140 || CK == CXCursor_UsingDeclaration 0141 || CK == CXCursor_TypeAliasDecl 0142 || CK == CXCursor_Namespace 0143 || CK == CXCursor_EnumDecl 0144 || CK == CXCursor_LabelStmt); 0145 } 0146 0147 constexpr bool isKDevClassDeclaration(CXCursorKind CK, bool isDefinition) 0148 { 0149 return isDefinition && isClass(CK); 0150 } 0151 0152 constexpr bool isKDevForwardDeclaration(CXCursorKind CK, bool isDefinition) 0153 { 0154 return !isDefinition && isClass(CK); 0155 } 0156 0157 constexpr bool isKDevClassFunctionDeclaration(CXCursorKind CK, bool isInClass) 0158 { 0159 return isInClass && isFunction(CK); 0160 } 0161 0162 constexpr bool isKDevFunctionDeclaration(CXCursorKind CK, bool isDefinition, bool isInClass) 0163 { 0164 return !isDefinition && !isInClass && isFunction(CK); 0165 } 0166 0167 constexpr bool isKDevFunctionDefinition(CXCursorKind CK, bool isDefinition, bool isInClass) 0168 { 0169 return isDefinition && !isInClass && isFunction(CK); 0170 } 0171 0172 constexpr bool isKDevNamespaceAliasDeclaration(CXCursorKind CK, bool isDefinition) 0173 { 0174 return !isDefinition && CK == CXCursor_NamespaceAlias; 0175 } 0176 0177 constexpr bool isKDevClassMemberDeclaration(CXCursorKind CK, bool isInClass) 0178 { 0179 return isInClass && isKDevDeclaration(CK, false); 0180 } 0181 0182 constexpr Declaration::AccessPolicy kdevAccessPolicy(CX_CXXAccessSpecifier access) 0183 { 0184 return access == CX_CXXPrivate ? Declaration::Private 0185 : access == CX_CXXProtected ? Declaration::Protected 0186 : access == CX_CXXPublic ? Declaration::Public 0187 : Declaration::DefaultAccess; 0188 } 0189 0190 constexpr IntegralType::CommonIntegralTypes integralType(CXTypeKind TK) 0191 { 0192 return TK == CXType_Void ? IntegralType::TypeVoid 0193 : TK == CXType_Bool ? IntegralType::TypeBoolean 0194 : TK == CXType_Half ? IntegralType::TypeHalf 0195 : TK == CXType_Float ? IntegralType::TypeFloat 0196 : TK == CXType_Char16 ? IntegralType::TypeChar16_t 0197 : TK == CXType_Char32 ? IntegralType::TypeChar32_t 0198 : TK == CXType_WChar ? IntegralType::TypeWchar_t 0199 : ( TK == CXType_LongDouble 0200 ||TK == CXType_Double 0201 #if CINDEX_VERSION_MINOR >= 38 0202 ||TK == CXType_Float128 0203 #endif 0204 ) ? IntegralType::TypeDouble 0205 : ( TK == CXType_Short 0206 ||TK == CXType_UShort 0207 ||TK == CXType_Int 0208 ||TK == CXType_UInt 0209 ||TK == CXType_Long 0210 ||TK == CXType_ULong 0211 ||TK == CXType_LongLong 0212 ||TK == CXType_ULongLong) ? IntegralType::TypeInt 0213 : ( TK == CXType_Char_U 0214 ||TK == CXType_Char_S 0215 ||TK == CXType_UChar 0216 ||TK == CXType_SChar) ? IntegralType::TypeChar 0217 : IntegralType::TypeNotIntegralType; 0218 } 0219 0220 constexpr bool isArrayType(CXTypeKind TK) 0221 { 0222 return TK == CXType_ConstantArray 0223 || TK == CXType_IncompleteArray 0224 || TK == CXType_VariableArray 0225 || TK == CXType_DependentSizedArray; 0226 } 0227 0228 constexpr bool isPointerType(CXTypeKind TK) 0229 { 0230 return TK == CXType_Pointer 0231 || TK == CXType_BlockPointer 0232 || TK == CXType_MemberPointer 0233 || TK == CXType_ObjCObjectPointer; 0234 } 0235 0236 0237 constexpr bool isOpenCLType(CXTypeKind CK) 0238 { 0239 return (CK == CXType_OCLImage1dRO || 0240 CK == CXType_OCLImage1dArrayRO || 0241 CK == CXType_OCLImage1dBufferRO || 0242 CK == CXType_OCLImage2dRO || 0243 CK == CXType_OCLImage2dArrayRO || 0244 CK == CXType_OCLImage2dDepthRO || 0245 CK == CXType_OCLImage2dArrayDepthRO || 0246 CK == CXType_OCLImage2dMSAARO || 0247 CK == CXType_OCLImage2dArrayMSAARO || 0248 CK == CXType_OCLImage2dMSAADepthRO || 0249 CK == CXType_OCLImage2dArrayMSAADepthRO || 0250 CK == CXType_OCLImage3dRO || 0251 CK == CXType_OCLImage1dWO || 0252 CK == CXType_OCLImage1dArrayWO || 0253 CK == CXType_OCLImage1dBufferWO || 0254 CK == CXType_OCLImage2dWO || 0255 CK == CXType_OCLImage2dArrayWO || 0256 CK == CXType_OCLImage2dDepthWO || 0257 CK == CXType_OCLImage2dArrayDepthWO || 0258 CK == CXType_OCLImage2dMSAAWO || 0259 CK == CXType_OCLImage2dArrayMSAAWO || 0260 CK == CXType_OCLImage2dMSAADepthWO || 0261 CK == CXType_OCLImage2dArrayMSAADepthWO || 0262 CK == CXType_OCLImage3dWO || 0263 CK == CXType_OCLImage1dRW || 0264 CK == CXType_OCLImage1dArrayRW || 0265 CK == CXType_OCLImage1dBufferRW || 0266 CK == CXType_OCLImage2dRW || 0267 CK == CXType_OCLImage2dArrayRW || 0268 CK == CXType_OCLImage2dDepthRW || 0269 CK == CXType_OCLImage2dArrayDepthRW || 0270 CK == CXType_OCLImage2dMSAARW || 0271 CK == CXType_OCLImage2dArrayMSAARW || 0272 CK == CXType_OCLImage2dMSAADepthRW || 0273 CK == CXType_OCLImage2dArrayMSAADepthRW || 0274 CK == CXType_OCLImage3dRW || 0275 CK == CXType_OCLSampler || 0276 CK == CXType_OCLEvent || 0277 CK == CXType_OCLQueue || 0278 CK == CXType_OCLReserveID); 0279 } 0280 0281 constexpr bool isAliasType(CXCursorKind CK) 0282 { 0283 return CK == CXCursor_TypedefDecl || CK == CXCursor_TypeAliasDecl; 0284 } 0285 0286 constexpr bool isIdentifiedType(CXCursorKind CK) 0287 { 0288 return isClass(CK) || isAliasType(CK) || CK == CXCursor_EnumDecl || CK == CXCursor_EnumConstantDecl; 0289 } 0290 0291 constexpr const char16_t* delayedTypeName(CXTypeKind TK) 0292 { 0293 return TK == CXType_Int128 ? u"__int128" 0294 : TK == CXType_UInt128 ? u"unsigned __int128" 0295 : TK == CXType_ObjCId ? u"id" 0296 : TK == CXType_ObjCSel ? u"SEL" 0297 : TK == CXType_NullPtr ? u"nullptr_t" 0298 : nullptr; 0299 } 0300 0301 } 0302 0303 #endif //CURSORKINDTRAITS_H