Staged metaprogramming in C++ for C/C++
Go to file
2024-12-19 09:40:41 -05:00
base Code type coercion for builder_print in C11 library using generic selector. 2024-12-17 10:03:50 -05:00
docs Proofing 2024-12-16 21:48:01 -05:00
gen_c_library typo in _Generic psuedo 2024-12-17 13:30:41 -05:00
gen_segmented Proofing 2024-12-16 21:48:01 -05:00
gen_singleheader Proofing 2024-12-16 21:48:01 -05:00
gen_unreal_engine removed custom attribute form gen_unreal_engine (mistake) 2024-12-16 22:00:56 -05:00
scripts Added refactor.exe binary 2024-12-19 09:40:41 -05:00
test Code type coercion for builder_print in C11 library using generic selector. 2024-12-17 10:03:50 -05:00
.editorconfig WIP Change to code types [ Broken ] 2023-07-13 23:01:20 -04:00
.gitignore Added refactor.exe binary 2024-12-19 09:40:41 -05:00
LICENSE update license again... 2024-12-16 10:43:28 -05:00
Readme.md Update main readme (old example code) 2024-12-16 21:19:19 -05:00

gencpp

An attempt at simple staged metaprogramming for C/C++. Reflect and generate code for your codebase at runtime!

splash-cpp splash-c

The library API is a composition of code element constructors, and a non-standards-compliant single-pass C/C++ parser.
These build up a code AST to then serialize with a file builder, or can be traversed for staged-reflection of C/C++ code.

This code base attempts follow the handmade philosophy.
Its not meant to be a black box metaprogramming utility, it should be easy to integrate into a user's project domain.

Documentation

Notes

This project is still in development (very much an alpha state), so expect bugs and missing features.
See issues for a list of known bugs or todos.

The library can already be used to generate code just fine, but the parser is where the most work is needed. If your C++ isn't "down to earth" expect issues.

A natvis and natstepfilter are provided in the scripts directory (its outdated, I'll update this readme when its not).
Minor update: I've been using RAD Debugger with this and the code structures should be easy to debug even without natvis.

Usage

A metaprogram is built to generate files before the main program is built. We'll term runtime for this program as GEN_TIME. The metaprogram's core implementation are within gen.hpp and gen.cpp in the project directory.

gen.cpp `s main() is defined as gen_main() which the user will have to define once for their program. There they may reflect and/or generate code.

In order to keep the locality of this code within the same files the following pattern may be used (although this pattern isn't the best to use):

Within program.cpp :

#ifdef GEN_TIME
#include "gen.hpp"

...

u32 gen_main()
{
    gen::Context ctx;
    gen::init(& ctx);
    ...
    gen::deinit(& ctx);
    return 0;
}
#endif

// "Stage" agnostic code.

#ifndef GEN_TIME
#include "program.gen.cpp"

    // Regular runtime dependent on the generated code here.
#endif

The design uses a constructive builder API for the code to generate.
The user is provided Code objects that are used to build up the AST.

Example using each construction interface:

Upfront

Validation and construction through a functional interface.

CodeTypename t_uw           = def_type( name(usize) );
CodeTypename t_allocator    = def_type( name(allocator) );
CodeTypename t_string_const = def_type( name(char), def_specifiers( args( ESpecifier::Const, ESpecifier::Ptr ) ));

CodeStruct header;
{
    CodeVar  num       = def_variable( t_uw,        name(Num) );
    CodeVar  cap       = def_variable( t_uw,        name(Capacity) );
    CodeVar  mem_alloc = def_variable( t_allocator, name(Allocator) );
    CodeBody body      = def_struct_body( args( num, cap, mem_alloc ) );

    header = def_struct( name(ArrayHeader), { body });
}

Parse

Validation through ast construction.

CodeStruct header = parse_struct( code(
    struct ArrayHeader
    {
        usize     Num;
        usize     Capacity;
        allocator Allocator;
    };
));

Untyped

No validation, just glorified text injection.

Code header = code_str(
    struct ArrayHeader
    {
        usize     Num;
        usize     Capacity;
        allocator Allocator;
    };
);

name is a helper macro for providing a string literal with its size, intended for the name parameter of functions.
code is a helper macro for providing a string literal with its size, but intended for code string parameters.
args is a helper macro for providing the number of arguments to varadic constructors.
code_str is a helper macro for writing untyped_str( code( <content> ))

All three construction interfaces will generate the following C code:

struct ArrayHeader
{
    usize     Num;
    usize     Capacity;
    allocator Allocator;
};

Note: The formatting shown here is not how it will look. For your desired formatting its recommended to run a pass through the files with an auto-formatter.
(The library currently uses clang-format for formatting, beware its pretty slow...)

Building

See the scripts directory.

Listing definitions in the Cuik Compiler

https://github.com/user-attachments/assets/2302240c-01f1-4e1b-a4b5-292eb3186648

Unreal: Generating a UAttributeSet from a UDataTable

https://github.com/user-attachments/assets/2a07b743-825d-4f9f-beaf-3559e8748a4d