From e1649657fd80947598d1fd230ab1911867c2c980 Mon Sep 17 00:00:00 2001 From: servostar Date: Mon, 5 Aug 2024 22:47:12 +0200 Subject: [PATCH] formatted source with clangd --- .clang-format | 44 +- run-clang-format.sh | 19 + src/ast/ast.c | 153 +-- src/ast/ast.h | 99 +- src/cfg/opt.c | 154 ++-- src/cfg/opt.h | 16 +- src/codegen/backend.c | 31 +- src/codegen/backend.h | 40 +- src/compiler.c | 149 +-- src/compiler.h | 2 +- src/io/files.c | 116 +-- src/io/files.h | 42 +- src/lex/util.c | 125 ++- src/lex/util.h | 6 +- src/link/clang/driver.c | 9 +- src/link/driver.h | 2 +- src/link/gcc/driver.c | 9 +- src/link/lib.c | 26 +- src/link/lib.h | 2 +- src/llvm/backend.c | 26 +- src/llvm/backend.h | 6 +- src/llvm/link/lld.c | 47 +- src/llvm/link/lld.h | 5 +- src/llvm/llvm-ir/expr.c | 257 +++--- src/llvm/llvm-ir/expr.h | 11 +- src/llvm/llvm-ir/func.c | 141 +-- src/llvm/llvm-ir/func.h | 15 +- src/llvm/llvm-ir/stmt.c | 295 +++--- src/llvm/llvm-ir/stmt.h | 13 +- src/llvm/llvm-ir/types.c | 82 +- src/llvm/llvm-ir/types.h | 2 +- src/llvm/llvm-ir/variables.c | 21 +- src/llvm/llvm-ir/variables.h | 2 +- src/llvm/parser.c | 53 +- src/llvm/parser.h | 4 +- src/main.c | 18 +- src/mem/cache.c | 136 +-- src/mem/cache.h | 31 +- src/set/set.c | 1685 +++++++++++++++++++--------------- src/set/set.h | 6 +- src/set/types.h | 131 ++- src/sys/col.c | 104 ++- src/sys/col.h | 27 +- src/sys/log.c | 83 +- src/sys/log.h | 122 +-- 45 files changed, 2431 insertions(+), 1936 deletions(-) create mode 100755 run-clang-format.sh diff --git a/.clang-format b/.clang-format index 1076ca2..754fea5 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,45 @@ -BasedOnStyle: Google IndentWidth: 4 PointerAlignment: Left +BreakAfterAttributes: Always +BreakStringLiterals: true +SpaceAfterCStyleCast: true +SortIncludes: CaseSensitive +UseTab: Never +ReflowComments: false +EmptyLineAfterAccessModifier: Never +TabWidth: 4 +SpaceAroundPointerQualifiers: Default +InsertNewlineAtEOF: true +InsertBraces: true +IndentCaseLabels: true +IncludeBlocks: Regroup +ContinuationIndentWidth: 2 +ColumnLimit: 80 +BreakBeforeBraces: Attach +BinPackParameters: true +BinPackArguments: true +AllowAllArgumentsOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AlignOperands: Align +BreakBeforeBinaryOperators: NonAssignment +AlignConsecutiveMacros: Consecutive +AlignEscapedNewlines: Left +AlignConsecutiveDeclarations: false +AlignConsecutiveAssignments: Consecutive +IndentCaseBlocks: true +AlignArrayOfStructures: Left +AlignAfterOpenBracket: Align +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortLoopsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AlwaysBreakAfterReturnType: None +BracedInitializerIndentWidth: 4 +BreakConstructorInitializers: AfterColon +DerivePointerAlignment: false +IndentWrappedFunctionNames: true +Language: Cpp +SpaceAfterLogicalNot: false +LineEnding: LF diff --git a/run-clang-format.sh b/run-clang-format.sh new file mode 100755 index 0000000..6b04b79 --- /dev/null +++ b/run-clang-format.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# Define the target directory +directory="./src" + +# Check if the target is not a directory +if [ ! -d "$directory" ]; then + echo "Error: $directory is not a valid directory" + exit 1 +fi + +# Use find to iterate over all files in the directory and subdirectories +find "$directory" -type f | while read -r file; do + if [[ "${file##*/}" =~ ^[a-z_]+\.c|h$ ]]; then + echo "Formatting file: $file..." + + clang-format -i "$file" + fi +done diff --git a/src/ast/ast.c b/src/ast/ast.c index aa88e1d..b6e59f6 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -1,15 +1,18 @@ +#include #include +#include #include #include -#include -#include -struct AST_Node_t *AST_new_node(TokenLocation location, enum AST_SyntaxElement_t kind, const char* value) { +struct AST_Node_t* AST_new_node(TokenLocation location, + enum AST_SyntaxElement_t kind, + const char* value) { DEBUG("creating new AST node: %d \"%s\"", kind, value); assert(kind < AST_ELEMENT_COUNT); - struct AST_Node_t *node = mem_alloc(MemoryNamespaceAst, sizeof(struct AST_Node_t)); + struct AST_Node_t* node = + mem_alloc(MemoryNamespaceAst, sizeof(struct AST_Node_t)); if (node == NULL) { PANIC("failed to allocate AST node"); @@ -18,24 +21,24 @@ struct AST_Node_t *AST_new_node(TokenLocation location, enum AST_SyntaxElement_t assert(node != NULL); // init to discrete state - node->parent = NULL; + node->parent = NULL; node->children = mem_new_g_array(MemoryNamespaceAst, sizeof(AST_NODE_PTR)); - node->kind = kind; - node->value = value; + node->kind = kind; + node->value = value; node->location = location; return node; } -static const char* lookup_table[AST_ELEMENT_COUNT] = { "__UNINIT__" }; +static const char* lookup_table[AST_ELEMENT_COUNT] = {"__UNINIT__"}; void AST_init() { DEBUG("initializing global syntax tree..."); INFO("filling lookup table..."); - lookup_table[AST_Stmt] = "stmt"; + lookup_table[AST_Stmt] = "stmt"; lookup_table[AST_Module] = "module"; - lookup_table[AST_Expr] = "expr"; + lookup_table[AST_Expr] = "expr"; lookup_table[AST_Add] = "+"; lookup_table[AST_Sub] = "-"; @@ -43,53 +46,53 @@ void AST_init() { lookup_table[AST_Div] = "/"; lookup_table[AST_BitAnd] = "&"; - lookup_table[AST_BitOr] = "|"; + lookup_table[AST_BitOr] = "|"; lookup_table[AST_BitXor] = "^"; lookup_table[AST_BitNot] = "!"; - lookup_table[AST_Eq] = "=="; - lookup_table[AST_Less] = "<"; + lookup_table[AST_Eq] = "=="; + lookup_table[AST_Less] = "<"; lookup_table[AST_Greater] = ">"; lookup_table[AST_BoolAnd] = "&&"; - lookup_table[AST_BoolOr] = "||"; + lookup_table[AST_BoolOr] = "||"; lookup_table[AST_BoolXor] = "^^"; lookup_table[AST_BoolNot] = "!!"; - lookup_table[AST_While] = "while"; - lookup_table[AST_If] = "if"; + lookup_table[AST_While] = "while"; + lookup_table[AST_If] = "if"; lookup_table[AST_IfElse] = "else if"; - lookup_table[AST_Else] = "else"; + lookup_table[AST_Else] = "else"; - lookup_table[AST_Decl] = "decl"; + lookup_table[AST_Decl] = "decl"; lookup_table[AST_Assign] = "assign"; - lookup_table[AST_Def] = "def"; + lookup_table[AST_Def] = "def"; - lookup_table[AST_Typedef] = "typedef"; - lookup_table[AST_Box] = "box"; - lookup_table[AST_FunDecl] = "fun"; - lookup_table[AST_FunDef] = "fun"; + lookup_table[AST_Typedef] = "typedef"; + lookup_table[AST_Box] = "box"; + lookup_table[AST_FunDecl] = "fun"; + lookup_table[AST_FunDef] = "fun"; lookup_table[AST_ProcDecl] = "fun"; - lookup_table[AST_ProcDef] = "fun"; + lookup_table[AST_ProcDef] = "fun"; - lookup_table[AST_Call] = "funcall"; - lookup_table[AST_Typecast] = "typecast"; - lookup_table[AST_Transmute] = "transmute"; - lookup_table[AST_Condition] = "condition"; - lookup_table[AST_List] = "list"; - lookup_table[AST_ExprList] = "expr list"; - lookup_table[AST_ArgList] = "arg list"; - lookup_table[AST_ParamList] = "param list"; - lookup_table[AST_StmtList] = "stmt list"; - lookup_table[AST_IdentList] = "ident list"; - lookup_table[AST_Type] = "type"; - lookup_table[AST_Negate] = "-"; - lookup_table[AST_Parameter] = "parameter"; - lookup_table[AST_ParamDecl] = "parameter-declaration"; - lookup_table[AST_AddressOf] = "address of"; + lookup_table[AST_Call] = "funcall"; + lookup_table[AST_Typecast] = "typecast"; + lookup_table[AST_Transmute] = "transmute"; + lookup_table[AST_Condition] = "condition"; + lookup_table[AST_List] = "list"; + lookup_table[AST_ExprList] = "expr list"; + lookup_table[AST_ArgList] = "arg list"; + lookup_table[AST_ParamList] = "param list"; + lookup_table[AST_StmtList] = "stmt list"; + lookup_table[AST_IdentList] = "ident list"; + lookup_table[AST_Type] = "type"; + lookup_table[AST_Negate] = "-"; + lookup_table[AST_Parameter] = "parameter"; + lookup_table[AST_ParamDecl] = "parameter-declaration"; + lookup_table[AST_AddressOf] = "address of"; lookup_table[AST_Dereference] = "deref"; - lookup_table[AST_Reference] = "ref"; - lookup_table[AST_Return] = "ret"; + lookup_table[AST_Reference] = "ref"; + lookup_table[AST_Return] = "ret"; } const char* AST_node_to_string(const struct AST_Node_t* node) { @@ -98,7 +101,7 @@ const char* AST_node_to_string(const struct AST_Node_t* node) { const char* string; - switch(node->kind) { + switch (node->kind) { case AST_Int: case AST_Char: case AST_Float: @@ -131,7 +134,7 @@ static inline unsigned long int max(unsigned long int a, unsigned long int b) { return a > b ? a : b; } -void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) { +void AST_push_node(struct AST_Node_t* owner, struct AST_Node_t* child) { DEBUG("Adding new node %p to %p", child, owner); assert(owner != NULL); assert(child != NULL); @@ -140,11 +143,15 @@ void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) { PANIC("failed to allocate children array of AST node"); } - owner->location.col_end = max(owner->location.col_end, child->location.col_end); - owner->location.line_end = max(owner->location.line_end, child->location.line_end); + owner->location.col_end = + max(owner->location.col_end, child->location.col_end); + owner->location.line_end = + max(owner->location.line_end, child->location.line_end); - owner->location.col_start = min(owner->location.col_start, child->location.col_start); - owner->location.line_start = min(owner->location.line_start, child->location.line_start); + owner->location.col_start = + min(owner->location.col_start, child->location.col_start); + owner->location.line_start = + min(owner->location.line_start, child->location.line_start); if (owner->location.file == NULL) { owner->location.file = child->location.file; @@ -155,7 +162,7 @@ void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) { g_array_append_val(owner->children, child); } -struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, const size_t idx) { +struct AST_Node_t* AST_get_node(struct AST_Node_t* owner, const size_t idx) { DEBUG("retrvieng node %d from %p", idx, owner); assert(owner != NULL); assert(owner->children != NULL); @@ -174,7 +181,8 @@ struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, const size_t idx) { return child; } -struct AST_Node_t* AST_remove_child(struct AST_Node_t* owner, const size_t idx) { +struct AST_Node_t* AST_remove_child(struct AST_Node_t* owner, + const size_t idx) { assert(owner != NULL); assert(owner->children != NULL); assert(idx < owner->children->len); @@ -187,7 +195,8 @@ struct AST_Node_t* AST_remove_child(struct AST_Node_t* owner, const size_t idx) return child; } -struct AST_Node_t* AST_detach_child(struct AST_Node_t* owner, const struct AST_Node_t* child) { +struct AST_Node_t* AST_detach_child(struct AST_Node_t* owner, + const struct AST_Node_t* child) { assert(owner != NULL); assert(child != NULL); assert(owner->children != NULL); @@ -201,14 +210,14 @@ struct AST_Node_t* AST_detach_child(struct AST_Node_t* owner, const struct AST_N PANIC("Child to detach not a child of parent"); } -void AST_delete_node(struct AST_Node_t *node) { +void AST_delete_node(struct AST_Node_t* node) { assert(node != NULL); DEBUG("Deleting AST node: %p", node); if (node->parent != NULL) { - [[maybe_unused]] - const struct AST_Node_t* child = AST_detach_child(node->parent, node); + [[maybe_unused]] const struct AST_Node_t* child = + AST_detach_child(node->parent, node); assert(child == node); } @@ -224,8 +233,8 @@ void AST_delete_node(struct AST_Node_t *node) { mem_free(node); } -static void AST_visit_nodes_recurse2(struct AST_Node_t *root, - void (*for_each)(struct AST_Node_t *node, +static void AST_visit_nodes_recurse2(struct AST_Node_t* root, + void (*for_each)(struct AST_Node_t* node, size_t depth), const size_t depth) { DEBUG("Recursive visit of %p at %d with %p", root, depth, for_each); @@ -235,12 +244,13 @@ static void AST_visit_nodes_recurse2(struct AST_Node_t *root, (for_each)(root, depth); for (size_t i = 0; i < root->children->len; i++) { - AST_visit_nodes_recurse2(g_array_index(root->children, AST_NODE_PTR, i), for_each, depth + 1); + AST_visit_nodes_recurse2(g_array_index(root->children, AST_NODE_PTR, i), + for_each, depth + 1); } } -void AST_visit_nodes_recurse(struct AST_Node_t *root, - void (*for_each)(struct AST_Node_t *node, +void AST_visit_nodes_recurse(struct AST_Node_t* root, + void (*for_each)(struct AST_Node_t* node, size_t depth)) { DEBUG("Starting recursive visit of %p with %p", root, for_each); @@ -250,24 +260,28 @@ void AST_visit_nodes_recurse(struct AST_Node_t *root, AST_visit_nodes_recurse2(root, for_each, 0); } -static void AST_fprint_graphviz_node_definition(FILE* stream, const struct AST_Node_t* node) { +static void AST_fprint_graphviz_node_definition(FILE* stream, + const struct AST_Node_t* node) { DEBUG("Printing graphviz definition of %p", node); assert(stream != NULL); assert(node != NULL); - fprintf(stream, "\tnode%p [label=\"%s\"]\n", (void*) node, AST_node_to_string(node)); + fprintf(stream, "\tnode%p [label=\"%s\"]\n", (void*) node, + AST_node_to_string(node)); if (node->children == NULL) { return; } for (size_t i = 0; i < node->children->len; i++) { - AST_fprint_graphviz_node_definition(stream, g_array_index(node->children, AST_NODE_PTR, i)); + AST_fprint_graphviz_node_definition( + stream, g_array_index(node->children, AST_NODE_PTR, i)); } } -static void AST_fprint_graphviz_node_connection(FILE* stream, const struct AST_Node_t* node) { +static void AST_fprint_graphviz_node_connection(FILE* stream, + const struct AST_Node_t* node) { DEBUG("Printing graphviz connection of %p", node); assert(stream != NULL); @@ -298,16 +312,17 @@ void AST_fprint_graphviz(FILE* stream, const struct AST_Node_t* root) { fprintf(stream, "}\n"); } -AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, enum AST_SyntaxElement_t kind) { - for (size_t i = 0; i < owner->children->len; i++) { - AST_NODE_PTR child = AST_get_node(owner, i); +AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, + enum AST_SyntaxElement_t kind) { + for (size_t i = 0; i < owner->children->len; i++) { + AST_NODE_PTR child = AST_get_node(owner, i); - if (child->kind == kind) { - return child; + if (child->kind == kind) { + return child; + } } - } - return NULL; + return NULL; } void AST_merge_modules(AST_NODE_PTR dst, size_t k, AST_NODE_PTR src) { diff --git a/src/ast/ast.h b/src/ast/ast.h index 256eb96..be836e2 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -2,8 +2,8 @@ #ifndef _AST_H_ #define _AST_H_ -#include #include +#include /** * @brief The type of a AST node @@ -50,10 +50,10 @@ enum AST_SyntaxElement_t { AST_Greater, AST_Less, // Casts - AST_Typecast, // type cast - AST_Transmute, // reinterpret cast - AST_Call, // function call - AST_Macro, // builtin functions: lineno(), filename(), ... + AST_Typecast, // type cast + AST_Transmute, // reinterpret cast + AST_Call, // function call + AST_Macro, // builtin functions: lineno(), filename(), ... // Defintions AST_Typedef, AST_Box, @@ -93,11 +93,12 @@ enum AST_SyntaxElement_t { * Every node can have one ancestor (parent) but multiple (also none) children. * Nodes have two properties: * - kind: The type of the node. Such as AST_Expr, AST_Add, ... - * - value: A string representing an optional value. Can be a integer literal for kind AST_int + * - value: A string representing an optional value. Can be a integer literal + * for kind AST_int */ typedef struct AST_Node_t { // parent node that owns this node - struct AST_Node_t *parent; + struct AST_Node_t* parent; // type of AST node: if, declaration, ... enum AST_SyntaxElement_t kind; @@ -116,43 +117,44 @@ typedef struct AST_Node_t { typedef struct AST_Node_t* AST_NODE_PTR; /** - * @brief Initalize the global state of this module. Required for some functionality to work correctly. + * @brief Initalize the global state of this module. Required for some + * functionality to work correctly. */ void AST_init(void); /** * @brief Returns the string representation of the supplied node - * @attention The retuned pointer is not to be freed as it may either be a statically stored string or - * used by the node after this function call. + * @attention The retuned pointer is not to be freed as it may either be a + * statically stored string or used by the node after this function call. * @param node to return string representation of * @return string represenation of the node */ -[[maybe_unused]] -[[gnu::nonnull(1)]] +[[maybe_unused]] [[gnu::nonnull(1)]] const char* AST_node_to_string(const struct AST_Node_t* node); /** - * @brief Create a new node struct on the system heap. Initializes the struct with the given values. - * All other fields are set to either NULL or 0. No allocation for children array is preformed. -* @attention parameter value can be NULL in case no value can be provided for the node + * @brief Create a new node struct on the system heap. Initializes the struct + * with the given values. All other fields are set to either NULL or 0. No + * allocation for children array is preformed. + * @attention parameter value can be NULL in case no value can be provided for + * the node * @param kind the type of this node * @param value an optional value for this node * @return */ -[[maybe_unused]] -[[nodiscard("pointer must be freed")]] -[[gnu::returns_nonnull]] -struct AST_Node_t *AST_new_node(TokenLocation location, enum AST_SyntaxElement_t kind, const char* value); +[[maybe_unused]] [[nodiscard("pointer must be freed")]] [[gnu::returns_nonnull]] +struct AST_Node_t* AST_new_node(TokenLocation location, + enum AST_SyntaxElement_t kind, + const char* value); /** * @brief Deallocate this node and all of its children. - * @attention This function will detach this node from its parent if one is present - * Use of the supplied node after this call is undefined behavior + * @attention This function will detach this node from its parent if one is + * present Use of the supplied node after this call is undefined behavior * @param node The node to deallocate */ -[[maybe_unused]] -[[gnu::nonnull(1)]] -void AST_delete_node(struct AST_Node_t * node); +[[maybe_unused]] [[gnu::nonnull(1)]] +void AST_delete_node(struct AST_Node_t* node); /** * @brief Add a new child node to a parent node @@ -160,59 +162,58 @@ void AST_delete_node(struct AST_Node_t * node); * @param owner node to add a child to * @param child node to be added as a child */ -[[maybe_unused]] -[[gnu::nonnull(1), gnu::nonnull(2)]] -void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child); +[[maybe_unused]] [[gnu::nonnull(1), gnu::nonnull(2)]] +void AST_push_node(struct AST_Node_t* owner, struct AST_Node_t* child); /** * @brief Remove the specified child from the owner. * @attention The parent of the removed node is set to NULL. - * The returned pointer is still valid. It must be freed at some pointer later. + * The returned pointer is still valid. It must be freed at some + * pointer later. * @param owner Node to remove the child from * @param idx the index of the child to remove * @return a pointer to the child which was removed */ -[[maybe_unused]] -[[nodiscard("pointer must be freed")]] -[[gnu::nonnull(1)]] +[[maybe_unused]] [[nodiscard("pointer must be freed")]] [[gnu::nonnull(1)]] struct AST_Node_t* AST_remove_child(struct AST_Node_t* owner, size_t idx); /** - * @brief Detach a child from its parent. This involves removing the child from its parent - * and marking the parent of the child as NULL. - * @attention The returned pointer is still valid. It must be freed at some pointer later. + * @brief Detach a child from its parent. This involves removing the child from + * its parent and marking the parent of the child as NULL. + * @attention The returned pointer is still valid. It must be freed at some + * pointer later. * @param owner the owner to remove the child from * @param child the child to detach * @return a pointer to child detached */ -[[nodiscard("pointer must be freed")]] -[[gnu::nonnull(1), gnu::nonnull(1)]] -struct AST_Node_t* AST_detach_child(struct AST_Node_t* owner, const struct AST_Node_t* child); +[[nodiscard("pointer must be freed")]] [[gnu::nonnull(1), gnu::nonnull(1)]] +struct AST_Node_t* AST_detach_child(struct AST_Node_t* owner, + const struct AST_Node_t* child); /** * @brief Return a pointer to the n-th child of a node * @attention Pointer to childen nodes will never change. * However, the index a node is stored within a parent can change - * if a child of lower index is removed, thus reducing the childrens index by one. + * if a child of lower index is removed, thus reducing the childrens + * index by one. * @param owner the parent node which owns the children * @param idx the index of the child to get a pointer to * @return a pointer to the n-th child of the owner node */ -[[maybe_unused]] -[[gnu::nonnull(1)]] -struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, size_t idx); +[[maybe_unused]] [[gnu::nonnull(1)]] +struct AST_Node_t* AST_get_node(struct AST_Node_t* owner, size_t idx); AST_NODE_PTR AST_get_last_node(AST_NODE_PTR node); /** - * @brief Execute a function for every child, grandchild, ... and the supplied node as topmost ancestor + * @brief Execute a function for every child, grandchild, ... and the supplied + * node as topmost ancestor * @param root the root to recursively execute a function for * @param for_each the function to execute */ -[[maybe_unused]] -[[gnu::nonnull(1), gnu::nonnull(2)]] -void AST_visit_nodes_recurse(struct AST_Node_t *root, - void (*for_each)(struct AST_Node_t *node, +[[maybe_unused]] [[gnu::nonnull(1), gnu::nonnull(2)]] +void AST_visit_nodes_recurse(struct AST_Node_t* root, + void (*for_each)(struct AST_Node_t* node, size_t depth)); /** @@ -220,11 +221,11 @@ void AST_visit_nodes_recurse(struct AST_Node_t *root, * @param stream The stream to print to. Can be a file, stdout, ... * @param node the topmost ancestor */ -[[maybe_unused]] -[[gnu::nonnull(1), gnu::nonnull(2)]] +[[maybe_unused]] [[gnu::nonnull(1), gnu::nonnull(2)]] void AST_fprint_graphviz(FILE* stream, const struct AST_Node_t* node); -AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, enum AST_SyntaxElement_t kind); +AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, + enum AST_SyntaxElement_t kind); [[gnu::nonnull(1), gnu::nonnull(3)]] void AST_merge_modules(AST_NODE_PTR dst, size_t i, AST_NODE_PTR src); diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 8a946b6..00c8043 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -2,14 +2,14 @@ // Created by servostar on 5/31/24. // +#include #include +#include +#include +#include #include #include -#include -#include #include -#include -#include static GHashTable* args = NULL; @@ -34,14 +34,15 @@ void parse_options(int argc, char* argv[]) { for (int i = 0; i < argc; i++) { Option* option = mem_alloc(MemoryNamespaceOpt, sizeof(Option)); option->is_opt = g_str_has_prefix(argv[i], "--"); - option->string = mem_strdup(MemoryNamespaceOpt, argv[i] + (option->is_opt ? 2 : 0)); + option->string = + mem_strdup(MemoryNamespaceOpt, argv[i] + (option->is_opt ? 2 : 0)); option->index = i; option->value = NULL; char* equals = strchr(option->string, '='); if (equals != NULL) { option->value = equals + 1; - *equals = 0; + *equals = 0; } g_hash_table_insert(args, (gpointer) option->string, (gpointer) option); @@ -94,17 +95,18 @@ TargetConfig* default_target_config() { TargetConfig* config = mem_alloc(MemoryNamespaceOpt, sizeof(TargetConfig)); - config->name = mem_strdup(MemoryNamespaceOpt, "out"); - config->print_ast = false; - config->print_asm = false; - config->print_ir = false; - config->driver = mem_strdup(MemoryNamespaceOpt, DEFAULT_DRIVER); - config->mode = Application; - config->archive_directory = mem_strdup(MemoryNamespaceOpt, "archive"); - config->output_directory = mem_strdup(MemoryNamespaceOpt, "bin"); + config->name = mem_strdup(MemoryNamespaceOpt, "out"); + config->print_ast = false; + config->print_asm = false; + config->print_ir = false; + config->driver = mem_strdup(MemoryNamespaceOpt, DEFAULT_DRIVER); + config->mode = Application; + config->archive_directory = mem_strdup(MemoryNamespaceOpt, "archive"); + config->output_directory = mem_strdup(MemoryNamespaceOpt, "bin"); config->optimization_level = 1; - config->root_module = NULL; - config->link_search_paths = mem_new_g_array(MemoryNamespaceOpt, sizeof(char*)); + config->root_module = NULL; + config->link_search_paths = + mem_new_g_array(MemoryNamespaceOpt, sizeof(char*)); config->lld_fatal_warnings = FALSE; config->gsc_fatal_warnings = FALSE; config->import_paths = mem_new_g_array(MemoryNamespaceOpt, sizeof(char*)); @@ -148,7 +150,8 @@ TargetConfig* default_target_config_from_args() { } else if (strcmp(opt->value, "lib") == 0) { config->mode = Library; } else { - print_message(Warning, "Invalid compilation mode: %s", opt->value); + print_message(Warning, "Invalid compilation mode: %s", + opt->value); } } } @@ -169,7 +172,7 @@ TargetConfig* default_target_config_from_args() { } } - char* cwd = g_get_current_dir(); + char* cwd = g_get_current_dir(); char* cached_cwd = mem_strdup(MemoryNamespaceOpt, cwd); g_array_append_val(config->link_search_paths, cached_cwd); free(cwd); @@ -180,10 +183,10 @@ TargetConfig* default_target_config_from_args() { if (opt->value != NULL) { const char* start = opt->value; - const char* end = NULL; - while((end = strchr(start, ',')) != NULL) { + const char* end = NULL; + while ((end = strchr(start, ',')) != NULL) { - const int len = end - start; + const int len = end - start; char* link_path = mem_alloc(MemoryNamespaceOpt, len + 1); memcpy(link_path, start, len); link_path[len] = 0; @@ -211,10 +214,12 @@ TargetConfig* default_target_config_from_args() { } else { if (files->len > 1) { - print_message(Warning, "Got more than one file to compile, using first, ignoring others."); + print_message(Warning, "Got more than one file to compile, using " + "first, ignoring others."); } - config->root_module = mem_strdup(MemoryNamespaceOpt, g_array_index(files, char*, 0)); + config->root_module = + mem_strdup(MemoryNamespaceOpt, g_array_index(files, char*, 0)); } char* default_import_path = mem_strdup(MemoryNamespaceOpt, "."); @@ -226,10 +231,10 @@ TargetConfig* default_target_config_from_args() { if (opt->value != NULL) { const char* start = opt->value; - const char* end = NULL; - while((end = strchr(start, ',')) != NULL) { + const char* end = NULL; + while ((end = strchr(start, ',')) != NULL) { - const int len = end - start; + const int len = end - start; char* import_path = mem_alloc(MemoryNamespaceOpt, len + 1); memcpy(import_path, start, len); import_path[len] = 0; @@ -256,39 +261,43 @@ TargetConfig* default_target_config_from_args() { void print_help(void) { DEBUG("printing help dialog..."); - const char *lines[] = { + const char* lines[] = { "Gemstone Compiler (c) GPL-2.0", "Build a project target: gsc build [target]|all", "Compile non-project file: gsc compile [file]", "Output information: gsc