kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

pp.h (3025B)


      1 #ifndef KIT_PP_H
      2 #define KIT_PP_H
      3 
      4 #include <stdint.h>
      5 
      6 #include "lex/lex.h"
      7 
      8 typedef struct Pp Pp;
      9 
     10 /* PP reads the compiler file-IO service for include search. If include search
     11  * is configured but file_io is missing, include resolution panics. */
     12 Pp* pp_new(Compiler*);
     13 void pp_free(Pp*);
     14 
     15 void pp_add_include_dir(Pp*, const char* dir, int system);
     16 void pp_define(Pp*, const char* name, const char* body); /* -D */
     17 void pp_undef(Pp*, const char* name);                    /* -U */
     18 uint32_t pp_pack_alignment(const Pp*);
     19 
     20 /* Open `spec` as a new top-of-stack source: PP creates the lexer, registers the
     21  * source in the SrcInfo registry (so its buffer + line map outlive the lexer
     22  * for lazy loc/text materialization), and pushes it. PP owns the lexer and its
     23  * buffer thereafter (freed at EOF-pop / pp_free). The borrowed spec->bytes must
     24  * outlive pp_free.
     25  *
     26  * The SRC_PARSER_FEED flag on the primary source selects cc parser-feed mode:
     27  * non-directive newline tokens are suppressed at every lexer (primary + each
     28  * #include), since the C parser drops them. The -E / cpp path leaves the flag
     29  * clear so newlines surface for text reconstruction. SRC_PRIMARY additionally
     30  * applies shebang skipping. */
     31 void pp_push_source(Pp*, const SourceSpec* spec);
     32 void pp_add_include_edge(Pp*, u32 includer_file_id, u32 included_file_id,
     33                          LocRef include_loc, int system);
     34 
     35 /* Streaming. Both fill a caller-owned slot (no by-value sret).
     36  *
     37  * pp_next_parse: the C parser stream — macro-expanded, directives consumed,
     38  *   non-directive newlines absent, forwarded #pragma lines swallowed.
     39  * pp_next_raw: the -E / cpp stream — macro-expanded, directives consumed,
     40  *   TOK_NEWLINE preserved for text reconstruction. */
     41 void pp_next_parse(Pp*, Tok* out);
     42 void pp_next_raw(Pp*, Tok* out);
     43 
     44 /* Drains pp_next_raw into `out` as preprocessed C source text: token spellings
     45  * separated by single spaces where TF_HAS_SPACE/TF_AT_BOL is set, with newlines
     46  * for TOK_NEWLINE. Stops on TOK_EOF. Used by the C driver. */
     47 void pp_emit_text(Pp*, Writer* out);
     48 
     49 /* ============================================================
     50  * Lazy materialization helpers (loc + text)
     51  * ============================================================ */
     52 
     53 /* Materialize a LocRef into a full (file_id, line, col) SrcLoc, consulting the
     54  * source's retained buffer/splice table and the active #line overlay. */
     55 SrcLoc pp_materialize_loc(Pp*, LocRef loc);
     56 
     57 /* Resolve a token's exact spelling. TEXT_SRC reads the retained source buffer;
     58  * TEXT_SYM reads the interned symbol; a TEXT_NONE punctuator reconstructs its
     59  * canonical spelling from the punctuator code; otherwise returns an empty
     60  * slice. The returned bytes are valid until pp_free. */
     61 KitSlice pp_text_slice(Pp*, const Tok* t);
     62 /* As pp_text_slice but interns the result and returns the Sym. */
     63 Sym pp_text_intern(Pp*, const Tok* t);
     64 /* True iff the token's spelling equals the NUL-terminated cstr. */
     65 int pp_text_eq_cstr(Pp*, const Tok* t, const char* s);
     66 
     67 #endif