forked from UbiquityDotNET/Llvm.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTContextExtensions.cs
More file actions
88 lines (75 loc) · 3.21 KB
/
Copy pathASTContextExtensions.cs
File metadata and controls
88 lines (75 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
namespace LlvmBindingsGenerator
{
internal static class ASTContextExtensions
{
public static IEnumerable<TranslationUnit> GeneratedUnits( this ASTContext ctx )
{
return from tu in ctx.TranslationUnits
where tu.IsGenerated
select tu;
}
public static bool IsCoreHeader( this TranslationUnit tu )
{
return tu.IsValid && tu.IncludePath != null && tu.FileRelativeDirectory.StartsWith( "llvm-c", StringComparison.Ordinal );
}
public static bool IsExtensionHeader( this TranslationUnit tu )
{
return !tu.IsCoreHeader()
&& tu.IsValid
&& !tu.IsSystemHeader
&& tu.FileNameWithoutExtension.EndsWith( "Bindings", StringComparison.Ordinal );
}
public static IEnumerable<TypedefNameDecl> GetHandleTypeDefs( this ASTContext ctx )
{
return from tu in ctx.GeneratedUnits()
from td in tu.Typedefs
where td.IsHandleTypeDef()
select td;
}
public static bool TryGetHandleDecl( this CppSharp.AST.Type astType, [MaybeNullWhen(false)] out TypedefNameDecl? decl )
{
switch( astType )
{
case TypedefType tdt when( tdt.Declaration.IsHandleTypeDef() ):
decl = tdt.Declaration;
return true;
case PointerType pt when( pt.Pointee is TypedefType tdt && tdt.Declaration.IsHandleTypeDef() ):
decl = tdt.Declaration;
return true;
default:
decl = null;
return false;
}
}
public static bool IsOpaqueStruct( this TagType tt )
{
return tt.Declaration is Class c && c.IsOpaque;
}
public static bool IsHandleTypeDef( this TypedefNameDecl td )
{
return IsCanonicalHandleTypeDef( td ) || IsOpaqueHandleTypeDef( td );
}
public static bool IsOpaqueHandleTypeDef( this TypedefNameDecl td )
{
// bad form, declaration is the opaque struct, not a pointer to the struct
return td.Type is TagType tt2 && tt2.IsOpaqueStruct();
}
[SuppressMessage( "Style", "IDE0046:Convert to conditional expression", Justification = "Result is anything but simplified!" )]
public static bool IsCanonicalHandleTypeDef( this TypedefNameDecl td )
{
// Canonical form, declaration is a pointer to an opaque struct
if( td.Type is not PointerType pt )
{
return false;
}
return ( pt.Pointee is TagType tt && tt.IsOpaqueStruct() )
|| ( pt.Pointee is BuiltinType bt && bt.Type == PrimitiveType.Void );
}
public static FunctionType? GetFunctionPointerType( this TypedefNameDecl td )
{
return ( td.Type is PointerType pt && pt.Pointee is FunctionType ft ) ? ft : null;
}
}
}