Blob Blame History Raw
#ifndef APIMAGIC_H
#define APIMAGIC_H

#include <stdint.h>
#include <stdio.h>

#include <cparser/ast/entity.h>
#include <cparser/driver/c_driver.h>
#include <cparser/driver/driver_t.h>

#include "apimagic_api.h"

#ifdef __cplusplus
extern "C" {
#endif

/* pre-alpha */
#ifndef AMGC_APP
#ifndef AMGC_PRE_ALPHA
#error  libapimagic: pre-alpha: ABI is not final!
#error  to use the library, please pass -DAMGC_PRE_ALPHA to the compiler.
#endif
#endif

/* status codes */
#define AMGC_OK				0x00
#define AMGC_USAGE			0x01
#define AMGC_ERROR			0x02

/* driver flags */
#define AMGC_DRIVER_VERBOSITY_NONE	0x0000
#define AMGC_DRIVER_VERBOSITY_ERRORS	0x0001
#define AMGC_DRIVER_VERBOSITY_STATUS	0x0002
#define AMGC_DRIVER_VERBOSITY_USAGE	0x0004
#define AMGC_DRIVER_CLONE_VECTOR	0x0008

#define AMGC_DRIVER_VERSION		0x0010
#define AMGC_DRIVER_DRY_RUN		0x0020

#define AMGC_DRIVER_ANNOTATE_ALWAYS	0x10000000
#define AMGC_DRIVER_ANNOTATE_NEVER	0x20000000
#define AMGC_DRIVER_ANNOTATE_FULL	0x40000000

/* error flags */
#define AMGC_ERROR_TOP_LEVEL		0x0001
#define AMGC_ERROR_NESTED		0x0002
#define AMGC_ERROR_CHILD		0x0004
#define AMGC_ERROR_CUSTOM		0x0008

enum amgc_custom_error {
	AMGC_ERR_FLOW_ERROR,
	AMGC_ERR_FLEE_ERROR,
	AMGC_ERR_NULL_CONTEXT,
	AMGC_ERR_NULL_SOURCE,
	AMGC_ERR_INVALID_CONTEXT,
	AMGC_ERR_INVALID_SOURCE,
	AMGC_ERR_SOURCE_SIZE_ZERO,
	AMGC_ERR_CAP,
};

enum amgc_action_type {
	AMGC_ACTION_NONE,
	AMGC_ACTION_OUTPUT,
};

enum amgc_output_action {
	AMGC_OUTPUT_DEFINE,
	AMGC_OUTPUT_ENUM,
	AMGC_OUTPUT_TYPEDEF,
	AMGC_OUTPUT_STRUCT,
	AMGC_OUTPUT_UNION,
	AMGC_OUTPUT_FUNCTION,
	AMGC_LIST_DEFINE,
	AMGC_LIST_ENUM,
	AMGC_LIST_TYPEDEF,
	AMGC_LIST_STRUCT,
	AMGC_LIST_UNION,
	AMGC_LIST_FUNCTION,
};

struct amgc_source_version {
	int		major;
	int		minor;
	int		revision;
	const char *	commit;
};

struct amgc_fd_ctx {
	int		fdin;
	int		fdout;
	int		fderr;
	int		fdlog;
	int		fdcwd;
	int		fddst;
};

struct amgc_action {
	enum amgc_action_type		type;
	int				action;
	const char *			symbol;
	const char *			filter;
	int				subset;
};

struct amgc_error_info {
	const struct amgc_driver_ctx *	edctx;
	const struct amgc_unit_ctx *	euctx;
	const char *			eunit;
	int				esyscode;
	int				elibcode;
	const char *			efunction;
	int				eline;
	unsigned			eflags;
	void *				eany;
};

struct amgc_common_ctx {
	uint64_t			drvflags;
	uint64_t			actflags;
	uint64_t			fmtflags;
	const struct amgc_action *	actions;
	struct compilation_env_t *	ccenv;
	enum lang_standard_t		std;
};

struct amgc_driver_ctx {
	const char **			units;
	const char *			program;
	const char *			module;
	const struct amgc_common_ctx *	cctx;
	struct amgc_error_info **	errv;
	void *				any;
};

struct amgc_unit_meta {
	int	ndefines;
	int	nenums;
	int	nenumvals;
	int	ntypedefs;
	int	nstructs;
	int	nunions;
	int	nfunctions;
	int	ngenerated;
};

struct amgc_define {
	const pp_definition_t *		symbol;
	const char *			definition;
	const char *			altname;
};

struct amgc_entity {
	const union entity_t *		entity;
	const union type_t *		reftype;
	const struct amgc_entity *	parent;
	const char * 			altname;
	int 				enumval;
	int				ptrdepth;
	bool 				fmbstr;
	bool 				futf16;
	bool				fset;
	bool				fexclude;
};

struct amgc_unit_entities {
	struct amgc_define *		defines;
	struct amgc_entity *		enums;
	struct amgc_entity *		enumvals;
	struct amgc_entity *		typedefs;
	struct amgc_entity *		structs;
	struct amgc_entity *		unions;
	struct amgc_entity *		functions;
	struct amgc_entity *		generated;
};

struct amgc_unit_ctx {
	const char * const *		path;
	const struct amgc_common_ctx *	cctx;
	const struct amgc_unit_meta *	meta;
	const struct amgc_unit_entities*entities;
	const struct compilation_unit_t*ccunit;
	void *				any;
};

struct amgc_layout {
	const char *	header;
	const char *	footer;
	int		symwidth;
	int		tabwidth;
};

/* driver api */
amgc_api int  amgc_get_driver_ctx       (char **, char **, uint32_t,
                                         const struct amgc_fd_ctx *,
                                         struct amgc_driver_ctx **);

amgc_api void amgc_free_driver_ctx      (struct amgc_driver_ctx *);

amgc_api int  amgc_get_unit_ctx	        (const struct amgc_driver_ctx *,
                                         const char *,
                                         struct amgc_unit_ctx **);

amgc_api void amgc_free_unit_ctx        (struct amgc_unit_ctx *);

amgc_api int  amgc_get_driver_fdctx     (const struct amgc_driver_ctx *, struct amgc_fd_ctx *);
amgc_api int  amgc_set_driver_fdctx     (struct amgc_driver_ctx *, const struct amgc_fd_ctx *);

/* driver helper api */
amgc_api int  amgc_lang_std_from_string (const char *);

/* utility api */
amgc_api int  amgc_main                 (char **, char **, const struct amgc_fd_ctx *);

amgc_api int  amgc_list_unit_defines    (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_list_unit_enums      (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_list_unit_typedefs   (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_list_unit_structs    (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_list_unit_unions     (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_list_unit_functions  (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_defines  (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_enums    (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_typedefs (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_structs  (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_unions   (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_functions(const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_layout *);

/* utility helper api */
amgc_api int  amgc_perform_unit_action  (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_action *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_unit_entities (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         int, int, const struct amgc_layout *);

amgc_api int  amgc_get_entity_index     (const struct amgc_entity *,   const char *);
amgc_api int  amgc_get_define_index     (const struct amgc_unit_ctx *, const char *);
amgc_api int  amgc_get_enum_index       (const struct amgc_unit_ctx *, const char *);
amgc_api int  amgc_get_typedef_index    (const struct amgc_unit_ctx *, const char *);
amgc_api int  amgc_get_struct_index     (const struct amgc_unit_ctx *, const char *);
amgc_api int  amgc_get_union_index      (const struct amgc_unit_ctx *, const char *);
amgc_api int  amgc_get_function_index   (const struct amgc_unit_ctx *, const char *);

amgc_api int  amgc_output_pad_symbol    (const struct amgc_driver_ctx *,
                                         const char *, const struct amgc_layout *);

amgc_api int  amgc_output_unit_enum     (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const union entity_t *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_custom_enum   (const struct amgc_driver_ctx *,
                                         const struct amgc_entity *,
                                         const struct amgc_entity *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_typedef       (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_entity *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_compound      (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_entity *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_struct        (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_entity *,
                                         const struct amgc_layout *);

amgc_api int  amgc_output_union         (const struct amgc_driver_ctx *,
                                         const struct amgc_unit_ctx *,
                                         const struct amgc_entity *,
                                         const struct amgc_layout *);

/* error trace api */
amgc_api int  amgc_output_error_record  (const struct amgc_driver_ctx *, const struct amgc_error_info *);
amgc_api int  amgc_output_error_vector  (const struct amgc_driver_ctx *);

/* low-level api */
amgc_api int  amgc_init_unit_meta       (const struct amgc_unit_ctx *, struct amgc_unit_meta *);

amgc_api int  amgc_get_unit_entities    (const struct amgc_unit_ctx *, struct amgc_unit_meta *, struct amgc_unit_entities **);
amgc_api void amgc_free_unit_entities   (struct amgc_unit_entities *);

amgc_api int  amgc_get_enum_members     (const struct amgc_unit_ctx *, const union entity_t *, struct amgc_entity **);
amgc_api void amgc_free_enum_members    (struct amgc_entity *);

/* package info */
amgc_api const struct amgc_source_version * amgc_source_version(void);

#ifdef __cplusplus
}
#endif

#endif