LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
CommonCAPIGen.h File Reference
#include <mlir/TableGen/Dialect.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/StringSwitch.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FormatVariadic.h>
#include <memory>
#include <string>
Include dependency graph for CommonCAPIGen.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  ClangLexerContext
 RAII wrapper for Clang lexer infrastructure. More...
struct  MethodParameter
 Structure to represent a parameter in a parsed method signature from an extraClassDeclaration. More...
struct  ExtraMethod
 Structure to represent a parsed method signature from an extraClassDeclaration. More...
struct  Generator
 Base class for C API generators. More...
struct  HeaderGenerator
 Generator for common C header file elements. More...
struct  ImplementationGenerator
 Generator for common C implementation file elements. More...
struct  TestGenerator
 Generator for common test implementation file elements. More...

Namespaces

namespace  clang
namespace  mlir
namespace  mlir::tblgen

Functions

template<typename S>
void warnSkipped (const S &methodName, const std::string &message)
 Print warning about skipping a function.
template<typename S>
void warnSkippedNoConversion (const S &methodName, const std::string &cppType)
 Print warning about skipping a function due to no conversion of C++ type to C API type.
llvm::cl::opt< std::string > & getDialectName ()
 Return the DialectName option, registering it on first access.
llvm::cl::opt< std::string > & getFunctionPrefix ()
 Return the FunctionPrefix option, registering it on first access.
llvm::cl::opt< bool > & getGenIsA ()
 Return the GenIsA option, registering it on first access.
llvm::cl::opt< bool > & getGenOpBuild ()
 Return the GenOpBuild option, registering it on first access.
llvm::cl::opt< bool > & getGenOpOperandGetters ()
 Return the GenOpOperandGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenOpOperandSetters ()
 Return the GenOpOperandSetters option, registering it on first access.
llvm::cl::opt< bool > & getGenOpAttributeGetters ()
 Return the GenOpAttributeGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenOpAttributeSetters ()
 Return the GenOpAttributeSetters option, registering it on first access.
llvm::cl::opt< bool > & getGenOpRegionGetters ()
 Return the GenOpRegionGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenOpResultGetters ()
 Return the GenOpResultGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrGet ()
 Return the GenTypeOrAttrGet option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrParamGetters ()
 Return the GenTypeOrAttrParamGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenExtraClassMethods ()
 Return the GenExtraClassMethods option, registering it on first access.
std::string toPascalCase (mlir::StringRef str)
 Convert names separated by underscore or colon to PascalCase.
bool isIntegerType (mlir::StringRef type)
 Check if a C++ type is a known integer type.
bool isPrimitiveType (mlir::StringRef cppType)
 Check if a C++ type is a known primitive type.
bool isCppModifierKeyword (mlir::StringRef tokenText)
 Check if a token text represents a C++ modifier/specifier keyword.
bool isCppLanguageConstruct (mlir::StringRef methodName)
 Check if a method name represents a C++ control flow keyword or language construct.
bool isDynamicAPIntType (mlir::StringRef cppType)
 Whether a parameter represents an arbitrary-precision mathematical integer.
bool isArrayRefType (mlir::StringRef cppType)
 Check if a C++ type is an ArrayRef type.
mlir::StringRef extractArrayRefElementType (mlir::StringRef cppType)
 Extract element type from ArrayRef<...>.
llvm::SmallVector< ExtraMethod > getCAPIExposedOpMethods (const mlir::tblgen::Operator &op)
 Return public operation methods explicitly requested by DeclareOpInterfaceMethods traits and declared by extraClassDeclaration.
llvm::SmallVector< ExtraMethod > parseExtraMethods (mlir::StringRef extraDecl)
 Parse method declarations from an extraClassDeclaration using Clang's Lexer.
bool matchesMLIRClass (mlir::StringRef cppType, mlir::StringRef typeName)
 Check if a C++ type matches an MLIR type pattern.
std::optional< std::string > tryCppTypeToCapiType (mlir::StringRef cppType)
 Convert C++ type to MLIR C API type.
std::string mapCppTypeToCapiType (mlir::StringRef cppType)
 Map C++ type to corresponding C API type.
std::optional< std::string > mapCapiTypeToBasicCppType (mlir::StringRef capiType)
 Map C API type to corresponding basic (not dialect-defined) C++ type.

Variables

constexpr bool WARN_SKIPPED_METHODS = false

Function Documentation

◆ extractArrayRefElementType()

mlir::StringRef extractArrayRefElementType ( mlir::StringRef cppType)
inline

Extract element type from ArrayRef<...>.

Definition at line 219 of file CommonCAPIGen.h.

◆ getCAPIExposedOpMethods()

llvm::SmallVector< ExtraMethod > getCAPIExposedOpMethods ( const mlir::tblgen::Operator & op)

Return public operation methods explicitly requested by DeclareOpInterfaceMethods traits and declared by extraClassDeclaration.

Interface methods must be named in alwaysOverriddenMethods (the optional method list passed to DeclareOpInterfaceMethods). This makes C API exposure an explicit opt-in and avoids colliding with the standard op C API accessors. Static methods and methods implemented directly in an interface trait are excluded. An extra class method takes precedence over an interface method with the same name, because the C API does not support overloads.

Definition at line 20 of file OpCAPIParamHelper.cpp.

◆ getDialectName()

llvm::cl::opt< std::string > & getDialectName ( )

Return the DialectName option, registering it on first access.

Definition at line 37 of file CommonCAPIGen.cpp.

◆ getFunctionPrefix()

llvm::cl::opt< std::string > & getFunctionPrefix ( )

Return the FunctionPrefix option, registering it on first access.

Definition at line 49 of file CommonCAPIGen.cpp.

◆ getGenExtraClassMethods()

llvm::cl::opt< bool > & getGenExtraClassMethods ( )

Return the GenExtraClassMethods option, registering it on first access.

Definition at line 142 of file CommonCAPIGen.cpp.

◆ getGenIsA()

llvm::cl::opt< bool > & getGenIsA ( )

Return the GenIsA option, registering it on first access.

Definition at line 61 of file CommonCAPIGen.cpp.

◆ getGenOpAttributeGetters()

llvm::cl::opt< bool > & getGenOpAttributeGetters ( )

Return the GenOpAttributeGetters option, registering it on first access.

Definition at line 93 of file CommonCAPIGen.cpp.

◆ getGenOpAttributeSetters()

llvm::cl::opt< bool > & getGenOpAttributeSetters ( )

Return the GenOpAttributeSetters option, registering it on first access.

Definition at line 101 of file CommonCAPIGen.cpp.

◆ getGenOpBuild()

llvm::cl::opt< bool > & getGenOpBuild ( )

Return the GenOpBuild option, registering it on first access.

Definition at line 69 of file CommonCAPIGen.cpp.

◆ getGenOpOperandGetters()

llvm::cl::opt< bool > & getGenOpOperandGetters ( )

Return the GenOpOperandGetters option, registering it on first access.

Definition at line 77 of file CommonCAPIGen.cpp.

◆ getGenOpOperandSetters()

llvm::cl::opt< bool > & getGenOpOperandSetters ( )

Return the GenOpOperandSetters option, registering it on first access.

Definition at line 85 of file CommonCAPIGen.cpp.

◆ getGenOpRegionGetters()

llvm::cl::opt< bool > & getGenOpRegionGetters ( )

Return the GenOpRegionGetters option, registering it on first access.

Definition at line 109 of file CommonCAPIGen.cpp.

◆ getGenOpResultGetters()

llvm::cl::opt< bool > & getGenOpResultGetters ( )

Return the GenOpResultGetters option, registering it on first access.

Definition at line 117 of file CommonCAPIGen.cpp.

◆ getGenTypeOrAttrGet()

llvm::cl::opt< bool > & getGenTypeOrAttrGet ( )

Return the GenTypeOrAttrGet option, registering it on first access.

Definition at line 125 of file CommonCAPIGen.cpp.

◆ getGenTypeOrAttrParamGetters()

llvm::cl::opt< bool > & getGenTypeOrAttrParamGetters ( )

Return the GenTypeOrAttrParamGetters option, registering it on first access.

Definition at line 133 of file CommonCAPIGen.cpp.

◆ isArrayRefType()

bool isArrayRefType ( mlir::StringRef cppType)
inline

Check if a C++ type is an ArrayRef type.

Parameters
cppTypeThe C++ type string to check
Returns
true if the type is ArrayRef, llvm::ArrayRef, or ::llvm::ArrayRef

Definition at line 212 of file CommonCAPIGen.h.

◆ isCppLanguageConstruct()

bool isCppLanguageConstruct ( mlir::StringRef methodName)
inline

Check if a method name represents a C++ control flow keyword or language construct.

Parameters
methodNameThe method name to check
Returns
true if the name is a C++ language construct (if, for, while, etc.)

Definition at line 187 of file CommonCAPIGen.h.

◆ isCppModifierKeyword()

bool isCppModifierKeyword ( mlir::StringRef tokenText)
inline

Check if a token text represents a C++ modifier/specifier keyword.

Parameters
tokenTextThe token to check
Returns
true if the token is a C++ modifier (inline, static, virtual, etc.)

Definition at line 170 of file CommonCAPIGen.h.

◆ isDynamicAPIntType()

bool isDynamicAPIntType ( mlir::StringRef cppType)
inline

Whether a parameter represents an arbitrary-precision mathematical integer.

Definition at line 204 of file CommonCAPIGen.h.

◆ isIntegerType()

bool isIntegerType ( mlir::StringRef type)
inline

Check if a C++ type is a known integer type.

Parameters
typeThe type string to check
Returns
true if the type is an integer type (size_t, unsigned, int*, uint*, etc.)

Definition at line 125 of file CommonCAPIGen.h.

◆ isPrimitiveType()

bool isPrimitiveType ( mlir::StringRef cppType)
inline

Check if a C++ type is a known primitive type.

Parameters
cppTypeThe C++ type string to check
Returns
true if the type is a primitive (bool, void, int, etc.)
Note
This function must be called on the CPP type because after converting to CAPI type, some C++ wrapper types require explicit conversion even if their C representation is primitive.

Definition at line 161 of file CommonCAPIGen.h.

◆ mapCapiTypeToBasicCppType()

std::optional< std::string > mapCapiTypeToBasicCppType ( mlir::StringRef capiType)

Map C API type to corresponding basic (not dialect-defined) C++ type.

Parameters
capiTypeThe C API type to map
Returns
The corresponding C++ type string if convertible, std::nullopt otherwise
Note
The parameter to this function should be something returned from tryCppTypeToCapiType().

◆ mapCppTypeToCapiType()

std::string mapCppTypeToCapiType ( mlir::StringRef cppType)

Map C++ type to corresponding C API type.

Parameters
cppTypeThe C++ type to map
Returns
The corresponding C API type string
Note
This function should not be called directly for ArrayRef types. Use extractArrayRefElementType() first and then use this on the result.

◆ matchesMLIRClass()

bool matchesMLIRClass ( mlir::StringRef cppType,
mlir::StringRef typeName )

Check if a C++ type matches an MLIR type pattern.

Parameters
cppTypeThe C++ type to check
typeNameThe MLIR type name to match against
Returns
true if the C++ type matches the MLIR type

◆ parseExtraMethods()

llvm::SmallVector< ExtraMethod > parseExtraMethods ( mlir::StringRef extraDecl)

Parse method declarations from an extraClassDeclaration using Clang's Lexer.

Parameters
extraDeclThe C++ code from an extraClassDeclaration
Returns
Vector of parsed method signatures

This function parses C++ method declarations to extract signatures that can be wrapped in C API functions. It identifies methods by looking for the pattern: [modifiers] <return_type> <identifier> '(' [params] ')' [const] ';'

Example input:

unsigned getWidth() const;
bool isSignless() const;

Example output:

  • ExtraMethod { returnType="unsigned", methodName="getWidth", documentation="Get the width of this type", isConst=true, hasParameters=false, parameters={} }
  • ExtraMethod { returnType="bool", methodName="isSignless", documentation="", isConst=true, hasParameters=false, parameters={} }

◆ toPascalCase()

std::string toPascalCase ( mlir::StringRef str)
inline

Convert names separated by underscore or colon to PascalCase.

Parameters
strThe input string to convert (may contain underscores or colons)
Returns
The converted PascalCase string

Examples: "no_inline" -> "NoInline" "::llzk::boolean::Type" -> "LlzkBooleanType"

Definition at line 100 of file CommonCAPIGen.h.

◆ tryCppTypeToCapiType()

std::optional< std::string > tryCppTypeToCapiType ( mlir::StringRef cppType)

Convert C++ type to MLIR C API type.

Parameters
cppTypeThe C++ type to convert
Returns
The corresponding MLIR C API type if convertible, std::nullopt otherwise

◆ warnSkipped()

template<typename S>
void warnSkipped ( const S & methodName,
const std::string & message )
inline

Print warning about skipping a function.

Definition at line 30 of file CommonCAPIGen.h.

◆ warnSkippedNoConversion()

template<typename S>
void warnSkippedNoConversion ( const S & methodName,
const std::string & cppType )
inline

Print warning about skipping a function due to no conversion of C++ type to C API type.

Definition at line 38 of file CommonCAPIGen.h.

Variable Documentation

◆ WARN_SKIPPED_METHODS

bool WARN_SKIPPED_METHODS = false
constexpr

Definition at line 27 of file CommonCAPIGen.h.