Skip to content

Commit

Permalink
Merge pull request #6 from bottlenoselabs/macro-objects
Browse files Browse the repository at this point in the history
Object macros
  • Loading branch information
lithiumtoast authored Mar 24, 2024
2 parents d5eeb05 + 8ac3e08 commit 9e74355
Show file tree
Hide file tree
Showing 46 changed files with 1,056 additions and 469 deletions.
43 changes: 23 additions & 20 deletions src/c/tests/functions/function_int/config.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
43 changes: 23 additions & 20 deletions src/c/tests/functions/function_int_params_int/config.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
27 changes: 27 additions & 0 deletions src/c/tests/macro_objects/macro_object_int/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
4 changes: 4 additions & 0 deletions src/c/tests/macro_objects/macro_object_int/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdio.h>
#include "ffi_helper.h"

#define MACRO_OBJECT_INT 42
2 changes: 1 addition & 1 deletion src/cs/production/c2ffi.Data/Nodes/CMacroObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CMacroObject : CNodeWithLocation
[ExcludeFromCodeCoverage]
public override string ToString()
{
return $"Macro '{Name}' : {Value} @ {Location}";
return $"{nameof(CMacroObject)} '{Name}' : {Value} @ {Location}";
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace c2ffi.Tool.Commands.Extract.Domain.Explore;

public sealed class ExploreInfoNode
public sealed class ExploreCandidateInfoNode
{
public CNodeKind Kind { get; init; }
public CNodeKind NodeKind { get; init; }

public string Name { get; init; } = string.Empty;

Expand All @@ -26,7 +26,7 @@ public sealed class ExploreInfoNode

public bool IsAnonymous { get; init; }

public ExploreInfoNode? Parent { get; init; }
public ExploreCandidateInfoNode? Parent { get; init; }

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class ExploreContext : IDisposable
public readonly ParseContext ParseContext;
private readonly ImmutableDictionary<CNodeKind, NodeExplorer> _nodeHandlers;
private readonly FfiBuilder _ffiBuilder;
private readonly ImmutableHashSet<string> _ignoredIncludeFiles;

public string FilePath => ParseContext.FilePath;

Expand All @@ -27,16 +28,17 @@ public ExploreContext(
ParseContext = parseContext;
_nodeHandlers = GetNodeHandlers(services);
_ffiBuilder = new FfiBuilder();
_ignoredIncludeFiles = parseContext.ExtractOptions.IgnoredIncludeFiles.ToImmutableHashSet();
}

public CFfiTargetPlatform GetFfi()
{
return _ffiBuilder.GetFfi(ParseContext);
}

public CNode? Explore(ExploreInfoNode info)
public CNode? Explore(ExploreCandidateInfoNode info)
{
var handler = GetHandler(info.Kind);
var handler = GetHandler(info.NodeKind);
var node = handler.ExploreInternal(this, info);
if (node != null)
{
Expand All @@ -46,30 +48,35 @@ public CFfiTargetPlatform GetFfi()
return node;
}

public bool CanExplore(CNodeKind kind, ExploreInfoNode node)
public bool CanVisit(CNodeKind kind, ExploreCandidateInfoNode node)
{
var handler = GetHandler(kind);
var result = handler.CanExploreInternal(this, node);
var result = handler.CanVisitInternal(this, node);
return result;
}

public bool IsIncludeIgnored(string filePath)
{
return _ignoredIncludeFiles.Contains(filePath);
}

public bool IsSystemCursor(clang.CXCursor cursor)
{
return ParseContext.IsSystemCursor(cursor);
}

public CTypeInfo? GetTypeInfo(clang.CXType type, ExploreInfoNode info)
public CTypeInfo? GetTypeInfo(clang.CXType type, ExploreCandidateInfoNode info)
{
throw new NotImplementedException();
}

public CTypeInfo? VisitType(
clang.CXType clangTypeCandidate,
ExploreInfoNode info,
ExploreCandidateInfoNode info,
CNodeKind? nodeKind = null,
int? fieldIndex = 0)
{
var clangTypeInfo = ClangTypeInfoProvider.GetTypeInfo(clangTypeCandidate, info.Kind);
var clangTypeInfo = ClangTypeInfoProvider.GetTypeInfo(clangTypeCandidate, info.NodeKind);
var nodeKindUsed = nodeKind ?? clangTypeInfo.NodeKind;

var rootInfo = info;
Expand Down Expand Up @@ -100,21 +107,30 @@ public bool IsSystemCursor(clang.CXCursor cursor)
return string.IsNullOrEmpty(commentString) ? null : commentString;
}

public ExploreInfoNode CreateInfoNode(
public ExploreCandidateInfoNode CreateCandidateInfoNode(
CNodeKind nodeKind,
clang.CXCursor cursor)
{
var cursorName = cursor.Spelling();
var cursorType = clang.clang_getCursorType(cursor);
return CreateCandidateInfoNode(nodeKind, cursorName, cursor, cursorType, null);
}

public ExploreCandidateInfoNode CreateCandidateInfoNode(
CNodeKind kind,
string name,
clang.CXCursor cursor,
clang.CXType type,
ExploreInfoNode? parentInfo)
ExploreCandidateInfoNode? parentInfo)
{
var location = cursor.Location();
var typeName = type.Spelling();
var sizeOf = ParseContext.SizeOf(kind, type);
var alignOf = ParseContext.AlignOf(kind, type);

var result = new ExploreInfoNode
var result = new ExploreCandidateInfoNode
{
Kind = kind,
NodeKind = kind,
Name = name,
TypeName = typeName,
Type = type,
Expand All @@ -133,11 +149,6 @@ public void Dispose()
ParseContext.Dispose();
}

private ImmutableArray<CMacroObject> CollectMacroObjects(ExploreContext context)
{
throw new NotImplementedException();
}

private static ImmutableDictionary<CNodeKind, NodeExplorer> GetNodeHandlers(IServiceProvider services)
{
var result = new Dictionary<CNodeKind, NodeExplorer>
Expand All @@ -151,6 +162,7 @@ private static ImmutableDictionary<CNodeKind, NodeExplorer> GetNodeHandlers(ISer
{ CNodeKind.TypeAlias, services.GetService<TypeAliasExplorer>()! },
{ CNodeKind.OpaqueType, services.GetService<OpaqueTypeExplorer>()! },
{ CNodeKind.FunctionPointer, services.GetService<FunctionPointerExplorer>()! },
{ CNodeKind.MacroObject, services.GetService<MacroObjectExplorer>()! },
{ CNodeKind.Array, services.GetService<ArrayExplorer>()! },
{ CNodeKind.Pointer, services.GetService<PointerExplorer>()! },
{ CNodeKind.Primitive, services.GetService<PrimitiveExplorer>()! },
Expand Down
Loading

0 comments on commit 9e74355

Please sign in to comment.