1 semantic
liquibyte edited this page 2026-11-19 16:23:29 -06:00

semantic.h

What the Code Means: the Semantic Model.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

The semantic model of a file lists the symbols the file declares and says, for every token of the file, which symbol it names. semantic_build() makes it for C and C++; language_symbols_build() (language.h) makes a model of the same form for every other language.

Features:

  • Every symbol a C file declares: variables, functions, parameters, typedefs, struct, union and enum tags, members, enumerators, labels and macros, each with the extent of its declaration and its documentation comment (the comments right above it, or one at the end of its line)
  • For every token of the file, the symbol it names (a use, or the name in the declaration itself), resolved by C's scopes
  • What each use of a macro becomes, as text
  • It works on broken code: a declaration that does not parse is skipped, and the rest of the file is still read

Internal design notes:

  • It is built from the preprocessor (macros expanded, if decided as the compiler decides it) and the parser's record. Positions are mapped back to the file's own tokens, so a name written in a macro's argument is found where it is written
  • A member is resolved by the type of what it is taken from: for a->b.c, c is looked for in the struct that b has as its type, so two structs can have a member of the same name. Only when no type can be told does it fall back on a name that only one struct declares, which is a guess: a caller that knows other files may have a better answer
  • Pointers do not matter for finding a member, so a type here is only the name it comes down to and the body that name has. A type that this file does not declare (it is in a header) stays a name: the symbol's type_token, and member_base, let a caller that has other files' models go on from there
#include "semantic.h"

Types: SemanticKind, SemanticSymbol, SemanticScope, SemanticModel

Constants: SEMANTIC_NONE, SEMANTIC_EXPANSION_BYTES, SEMANTIC_EXPANSIONS_BYTES

Functions: semantic_build, semantic_free, semantic_kind_name

Types

SemanticKind

What a symbol is.

Name Value Description
SEMANTIC_VARIABLE a variable
SEMANTIC_FUNCTION a function (in other languages also a method)
SEMANTIC_PARAMETER a parameter of a function
SEMANTIC_TYPEDEF a typedef name (in other languages a type name)
SEMANTIC_TAG a struct, union or enum name (in other languages a class, a namespace or a module)
SEMANTIC_MEMBER a member of a struct or union (in other languages a field)
SEMANTIC_ENUMERATOR a constant of an enum (in other languages a constant)
SEMANTIC_LABEL a label for goto (in a document language, a heading)
SEMANTIC_MACRO a macro
SEMANTIC_KIND_COUNT how many kinds there are

SemanticSymbol

One symbol the file declares.

Field Type Description
kind uint8_t what it is: a SemanticKind
is_definition uint8_t 1 for a function with its body, a struct with its members, a macro, a variable that is not extern, and every parameter, member, enumerator and label (and a C++ namespace)
at_file_scope uint8_t 1 when it is declared outside every function and block (a macro always; in C++, 0 inside a namespace or class)
name uint32_t the file's token of its name (for a name a macro of the file makes: its token in the define line), or SEMANTIC_NONE (a name made by a header's macro, or by ##)
first uint32_t the file's first token of its whole declaration (for a macro: the # of its define line)
last uint32_t the last token of that declaration
scope int32_t the scope it is declared in (an index into scopes), or -1 for none. In C, scope 0 is the file's own scope; a macro, and a file-level symbol of another language, has -1
comment_first uint32_t its documentation: the comment tokens [comment_first, comment_first + comment_count)
comment_count uint32_t how many comment tokens that is; 0 when it has none
use_count uint32_t how many tokens of the file name it, its declaration's name not counted
type_token uint32_t the file's token that names its type (struct x a: x; Foo *a: Foo), or SEMANTIC_NONE
body uint32_t the { of the struct, union or enum it declares or has as its type written in place (in C++ also of a class or namespace; in another language, the first token of its block), or SEMANTIC_NONE
owner_body uint32_t for a member: the { of the struct that holds it (past any anonymous struct in between); for a C++ declaration with an owner: the owner's {; or SEMANTIC_NONE

SemanticScope

One scope of the file.

Field Type Description
parent int32_t the scope around it, or -1 for none
first uint32_t the file's token where it begins, as ParseScope.first (for a macro's scope, its { in the define line or the macro's use); SEMANTIC_NONE when no token of the file stands for it
last uint32_t the file's token where it ends, as ParseScope.last
block_only uint8_t 1 for a block that folds but declares nothing of its own (a block of a grammar, such as Python's if)

SemanticModel

The model of one file: its symbols, its scopes, and what each token names.

Field Type Description
symbols SemanticSymbol * every symbol the file declares
symbol_count size_t how many symbols there are
scopes SemanticScope * every scope
scope_count size_t how many scopes there are
symbol_of_token int32_t * one per token of the file: the symbol it names (an index into symbols), or -1
declares uint8_t * one per token: 1 where it is the name in a symbol's declaration
member_base int32_t * one per token: for a member's name, the token its type comes from (in a->b, the a), else -1
token_count size_t how many tokens the three arrays above are for
valid int 1 when the whole file preprocessed without a fault and parsed (for C++: its brackets pair)
problem[160] char what stopped it, for people ("line 12: not C at ..."), when it is not valid
expansion_tokens uint32_t * What each use of a macro in the file becomes, in full (the macros inside it expanded too), for a program that shows it. expansion_tokens lists the file's tokens that are such uses, in ascending order. A text longer than SEMANTIC_EXPANSION_BYTES is cut and ends with "..."; once SEMANTIC_EXPANSIONS_BYTES are used in all, the later uses have no text.
expansion_offsets uint32_t * where each use's text starts in expansion_text
expansion_count size_t how many uses have a text
expansion_text char * the texts, each followed by a NUL
expansion_text_length size_t the length of expansion_text in bytes

Constants

SEMANTIC_NONE

#define SEMANTIC_NONE UINT32_MAX

No token: the value of a field that names none.

SEMANTIC_EXPANSION_BYTES

#define SEMANTIC_EXPANSION_BYTES 16384

The most bytes the text of one macro use has.

SEMANTIC_EXPANSIONS_BYTES

#define SEMANTIC_EXPANSIONS_BYTES (2u << 20)

The most bytes the texts of all macro uses of one file have together.

Functions

semantic_build

int semantic_build(
    const Token *tokens,
    size_t token_count,
    const char *source,
    const PreprocessorOptions *options,
    SemanticModel *model);

Build the model of a C or C++ file as the compiler would see it.

The file is preprocessed with options, then read by parse_c_record(), or by parse_cpp_record() when options->cplusplus is set. The headers the file includes add their macros only: their declarations are not symbols here.

Parameter Description
tokens The file's tokens (lexer_run())
token_count How many tokens there are
source The file's text
options What is known of the build, or NULL for nothing known (a C file)
model Filled in; free it with semantic_free(). On -1 it is left empty. When the preprocessor stops at one of its limits, the model has no symbols, is not valid, and its problem says which limit ("preprocessing stopped: ...")

Returns: 0 (model->valid tells whether the file parses), or -1 when out of memory

semantic_free

void semantic_free(SemanticModel *model);

Free what semantic_build() or language_symbols_build() made, and clear the model.

Parameter Description
model The model, or NULL (nothing is done); an empty model may be freed too

semantic_kind_name

const char *semantic_kind_name(SemanticKind kind);

A kind's name, for people.

Parameter Description
kind The kind

Returns: "variable", "function", "parameter", "type", "struct, union or enum", "member", "enumerator", "label" or "macro"; "symbol" for a value that is no kind