From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms1.migadu.com with LMTPS id UAAdC35dGGfLoQAAe85BDQ:P1 (envelope-from ) for ; Wed, 23 Oct 2024 04:20:46 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id UAAdC35dGGfLoQAAe85BDQ (envelope-from ) for ; Wed, 23 Oct 2024 04:20:46 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=G8IbkQsd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=GhWPiqkl; dmarc=pass (policy=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1"); spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1729650046; a=rsa-sha256; cv=pass; b=W9Ljj1A0zhtv++Pof+o0GXDpjrmptMC7i6AJDc+rnvsVflCN+9w1BcFtInsebxngmf9L88 ZZeS+/BTueL5F3vlhO7TcM7dHmws6iFt7fPICh4KB7Oe1eGdj4pRMxvZxyIejJi+tgCbok 0UuzHy01rX6L/KG2oS7E2XbzVSpsczOwhuVOKdA8DwqyFq+B+Rw+8dROBmKj+Q7QO6E8h7 dS1iC82sW0wQDZwuqQ6+G8Lzq8fZb5zVIkAYtRknHlrfqJtGS5GZ44OWBNEPdYXnNxV4gp pOR5FyUKLTQBa+TekvPTd9OKiy59hw7ksRlFiMIcgNV/WGY9rT5lS79G/kyPBQ== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=G8IbkQsd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=GhWPiqkl; dmarc=pass (policy=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1"); spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1729650046; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=Xf5XBTWZSBWf7grCGj9CGN4CDeIikB+LdCcyB6gtCIU=; b=QHkvQQiXMhHTtjb0iRzz+T4ClYYRrvoTiNqWCk5XyBl7yKHiEV6Bm5UFQU04Hvj71L6Etr B9RK2QH38Z8YSivcYxDp1mK4+hUR8obaZB35DweZt15z4rMg9z5C3jP7iIJjakDQaHzLK1 34pANH62WwwukYHx1LiI/JN2btFKjv6weHEOslvmXi00FfHJ7f+lQObVuSUAPtuSR9NYxo cZDeDbf1R5k0ld2RJeNXyEejyr8qT7Lot6rB7yWjyN6yHwTn4l4CygjwLfVaJVjl++e8Jg CvUkmf6TFeSFGrpk8ET9WqolKCoKWaiLnpRZM4TxWohVsZ4LGgJ9G+vFJeODzw== Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 0109A73ED6 for ; Wed, 23 Oct 2024 04:20:42 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=1MpqBIwvb1yU46WxAXUtWKoWCxoesYtCmL8S6s+cHBI=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:In-Reply-To:References:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1729650042; v=1; b=G8IbkQsdYYuyUY8+Q5g7rFtALwqSoovaBklEYIZdjBMRCeVY4YmqCFhd9+Uh+SZdyjEMWNPl B73cMOrLwO+1Dikjo96Jn6uNgNcWBOw+JEu43ycGQ7xHz3t1gTmIoaZCWSwM5FzO1CejduuXp+Z qNG2zE4d921wjSTBYYJHCc+/gd+6Dvi5NN3mZ2j+48SL7jKkO5BEGRAe2dczkWZn5bjjTspEOFC deL/yGq7Tvvb9zJOqBnT6mgEzXxfVyvgZ/cFpHSIDd9G5o0qjzdlg5yAeZuesHVV0AUIsKqTi1W R8EM2p0h0PHk51gmye+kHn/mjg6kr50qfuYsua6aV4OFA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 3DE7A202BF for ; Wed, 23 Oct 2024 02:20:42 +0000 (UTC) Received: from rusty.tulip.relay.mailchannels.net (rusty.tulip.relay.mailchannels.net [23.83.218.252]) by mail-a.sr.ht (Postfix) with ESMTPS id 8EC4E202B1 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:40 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1E2AF904C4A for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:39 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (trex-10.trex.outbound.svc.cluster.local [100.103.28.225]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 89EA3904D9D for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:38 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1729650038; a=rsa-sha256; cv=none; b=iFEtiNQv28CICEG/Ds11d5Ezpr7Ip1osWXO8LSPX2DweHy4W9ARKvEPyh/CV/0j0oLt35L M1dba+pDJBKy7LxRGSYlGaPRs+XqwCssTI6aPN2Tzbtf4VlAO1gkkBVUOvIE0plx/Ks4uI xZdsYKf2o1chGQs16ohH/idKynhRD+lkLt+DXUxuVi8BLhRUbg7+E32+EgkISiAA2u2TP0 4+B6VcadduQIjcEnkHZXocLRey2VW/UaVcGWNvQlwy3Zyip3pfbkt/V6VKwIOb4t4Ylvov 3u9fHnymPJ5pAFPB7yimT5rERKAhkSeBceaMi0u1pZT9nci0E+lCkhVOQbCiwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1729650038; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Xf5XBTWZSBWf7grCGj9CGN4CDeIikB+LdCcyB6gtCIU=; b=M/blimeIxBFNPtAmi/jMkdrz520+tFrOiONW7rJxu7ESbHtKvC3k4EMcymuvzIVsUSmk+z hEsqc9QM7nqKDqoe28Yr0q7wX0qxjv08d3F5vSlkNdPZiy/ljwxx+vqFu1EcvKgrd3256J meL3RTEaslZREXCuiIsu7UO3y6raLRi5AvEh6Bifhi3V/tlocE6ktFqVo1RvtWpJ+6huwM cuphL9fBoD1ineoqGhVXA0mCQpZYYSyRGnt/+L0vT0K0iMqe8iSE8RJxJV7kGEsKfleCZE 8wvSPyy0G527cnvhbZZcwQ5H1jS9tas9OXC8B0IxvRNeFxPMLsgzVD7rdHch/A== ARC-Authentication-Results: i=1; rspamd-5bbb596db8-mhnnz; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Duck-Thoughtful: 1c2400a00542f024_1729650039037_1850860051 X-MC-Loop-Signature: 1729650039037:633068343 X-MC-Ingress-Time: 1729650039037 Received: from nl-srv-smtpout1.hostinger.io (nl-srv-smtpout1.hostinger.io [145.14.150.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.28.225 (trex/7.0.2); Wed, 23 Oct 2024 02:20:39 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1729650036; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Xf5XBTWZSBWf7grCGj9CGN4CDeIikB+LdCcyB6gtCIU=; b=GhWPiqklcMnLDLofFydNPyT8TVp3aHpynnrRvBsilQORdWzR6YDogtQGTEuZ9h0F5RGe9v Lm1s73TXIBf7XAUk6WIPalzq40VsbGvz5A5eKCtsu4UxeG8jVeR8QyzWLDt1G/lrwHPA6k 7hHM3ZCSMs+Dr/zZooGAVzzTFUL0Xi1RTuFbtr2dw3JP5+weuCJ5bDD++TWRt9Gp8F7yLQ MSDu3Ktx05BuUyxxyyh8yEePPzbCnwlysii5iBYJlfVp15F3eXGTiNtUC3uL1WXatH55It bf7ysNFw+NCfzNkvuJY6tnJKwONUD2H46Xp38wfSQnJclGnnkgs5kQW+NmOUDA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields Message-ID: <20241023022022.38379-3-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241023022022.38379-1-carlos@maniero.me> References: <20241023022022.38379-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Wed, 23 Oct 2024 02:20:36 +0000 (UTC) X-CM-Analysis: v=2.4 cv=VemlP0p9 c=1 sm=1 tr=0 ts=67185d74 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=sTxBJYKUQiiAcmFu2sIA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfK4VTVUB++eCs24QNNUamegEq89ihuu3+CacPSu3Ng4IrLiGVbWPuE1QAeOaTyx7YwcoSbuRAwqRQec+VnhT9pPV4oqJypoT5EVHyXFpOEzS7rP5lj2+ mD0JhxLUyt7gmV7R7ki8B879LDmyQjO4Ejs3W6PawxvUPx8C0bYu5XHAkW+bY1X2DoQR5KRX1/E806lIZacqovexXG20RWeKtcMBNmBdJyqApqEMFL7k3TKm X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -5.59 X-Spam-Score: -5.59 X-Migadu-Queue-Id: 0109A73ED6 X-Migadu-Scanner: mx10.migadu.com X-TUID: wSaANniu+YPX This solution was inspired on cython PyObject which uses macros to avoid the redundancy. Signed-off-by: Carlos Maniero --- src/ast.h | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/ast.h b/src/ast.h index e7d8ed8..c7a25bf 100644 --- a/src/ast.h +++ b/src/ast.h @@ -26,6 +26,21 @@ #include "string_view.h" #include "type.h" +#define AST_NODE_BASE \ + { \ + ast_node_kind_t kind; \ + token_loc_t loc; \ + } + +/** + * Every node within the *ast_node* union must include the base segment. + * + * This macro must be declared first within any node definition to ensure the + * ast_node base segment's integrity. + */ + +#define AST_NODE_HEAD struct AST_NODE_BASE base + typedef union ast_node ast_node_t; typedef enum @@ -45,21 +60,17 @@ typedef enum AST_NODE_UNKNOWN } ast_node_kind_t; -typedef struct ast_node_meta -{ - ast_node_kind_t kind; - token_loc_t loc; -} ast_node_meta_t; +typedef struct ast_node_base AST_NODE_BASE ast_node_base_t; typedef struct ast_block { - ast_node_meta_t meta; + AST_NODE_HEAD; list_t *nodes; } ast_block_t; typedef struct ast_translation_unit { - ast_node_meta_t meta; + AST_NODE_HEAD; list_t *decls; } ast_translation_unit_t; @@ -71,7 +82,7 @@ typedef struct ast_fn_param typedef struct ast_fn_definition { - ast_node_meta_t meta; + AST_NODE_HEAD; string_view_t id; list_t *params; type_t *return_type; @@ -82,7 +93,7 @@ typedef struct ast_fn_definition typedef struct ast_fn_call { - ast_node_meta_t meta; + AST_NODE_HEAD; string_view_t id; list_t *args; scope_t *scope; @@ -90,7 +101,7 @@ typedef struct ast_fn_call typedef struct ast_var_definition { - ast_node_meta_t meta; + AST_NODE_HEAD; string_view_t id; type_t *type; ast_node_t *value; @@ -104,7 +115,7 @@ typedef enum typedef struct ast_literal { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_literal_kind_t kind; union { @@ -114,7 +125,7 @@ typedef struct ast_literal typedef struct ast_ref { - ast_node_meta_t meta; + AST_NODE_HEAD; string_view_t id; scope_t *scope; } ast_ref_t; @@ -144,7 +155,7 @@ typedef enum ast_binary_op_kind typedef struct ast_binary_op { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_binary_op_kind_t kind; ast_node_t *lhs; ast_node_t *rhs; @@ -162,27 +173,27 @@ typedef enum ast_unary_op_kind typedef struct ast_unary_op { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_unary_op_kind_t kind; ast_node_t *expr; } ast_unary_op_t; typedef struct ast_var_assign_stmt { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_node_t *ref; ast_node_t *expr; } ast_var_assign_stmt_t; typedef struct ast_return_stmt { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_node_t *expr; } ast_return_stmt_t; typedef struct ast_if_stmt { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_node_t *cond; ast_node_t *then; ast_node_t *_else; @@ -190,19 +201,14 @@ typedef struct ast_if_stmt typedef struct ast_while_stmt { - ast_node_meta_t meta; + AST_NODE_HEAD; ast_node_t *cond; ast_node_t *then; } ast_while_stmt_t; typedef union ast_node { - // inlined ast_node_meta_t struct. - struct - { - ast_node_kind_t kind; - token_loc_t loc; - }; + struct AST_NODE_BASE; ast_translation_unit_t as_translation_unit; ast_fn_definition_t as_fn_def; ast_fn_call_t as_fn_call; -- 2.46.1