Skip to content

Refactor/ast decl#27

Merged
broccolingual merged 6 commits into
mainfrom
refactor/ast-decl
Jan 11, 2026
Merged

Refactor/ast decl#27
broccolingual merged 6 commits into
mainfrom
refactor/ast-decl

Conversation

@broccolingual
Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @broccolingual, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request undertakes a significant refactoring of the declaration parsing components within the compiler's Abstract Syntax Tree (AST) module. The primary goal is to enhance the clarity, maintainability, and correctness of how the compiler interprets C-style declarations, particularly those involving complex types like function pointers and arrays. By modularizing parsing logic and improving the handling of type qualifiers and parenthesized constructs, the changes contribute to a more robust and accurate front-end for the compiler.

Highlights

  • Declaration Parsing Refactoring: The core logic for parsing declarations, especially function definitions and prototypes, has been extracted into dedicated helper methods (func_def, validate_func_prototype) in src/ast/decl.rs for improved modularity and readability.
  • Enhanced Type Qualifier Handling: The parser now correctly processes type qualifiers (e.g., const, volatile) for pointer and array types, ensuring they are properly associated with the TypeAttr during type construction. This includes a new TypeAttr::from_type_quals helper.
  • Robust Parenthesized Declarator Support: A significant refactoring of direct_declarator and direct_abst_declarator enables the correct parsing of complex declarators involving parentheses, such as function pointers and arrays of pointers, by introducing a placeholder type and a recursive TypeRef::replace_type mechanism.
  • Performance Improvements: Methods like TypeRef::from_ds and TypeRef::from_tsq now accept slices (&[]) instead of Vec for declaration specifications, reducing unnecessary allocations and improving parsing efficiency.
  • New Function Pointer Test Cases: Several new test cases have been added to test/inline.sh to validate the parser's ability to handle various complex function pointer declarations, confirming the robustness of the refactored declaration logic.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

このプルリクエストは、宣言のパース処理、特に複雑な宣言子(関数ポインタなど)の解釈を改善するための大規模なリファクタリングです。主な変更点は以下の通りです。

  • external_decl から関数定義のパースロジックを func_def 関数に抽出し、見通しを良くしました。
  • ptr 関数の実装を再帰からループに変更し、型修飾子 (const など) を正しく扱えるように修正しました。
  • direct_declarator をリファクタリングし、括弧を含む複雑な宣言子(例: int (*p)())を正しくパースできるように、プレースホルダー型を用いた手法に変更しました。
  • replace_type ヘルパーを TypeRef に追加し、上記のプレースホルダー置換を実現しました。
  • 変数名をより具体的に(例: specs -> decl_specs)変更し、コードの可読性を向上させました。
  • 関数ポインタに関する新しいテストケースを追加し、リファクタリングの正当性を担保しています。

全体として、コードの正確性と保守性を大幅に向上させる素晴らしいリファクタリングです。1点、抽象宣言子のパース処理に declarator と同様の修正が適用されておらず、バグが残っている可能性があるため、修正を提案します。

Comment thread src/ast/decl.rs
Comment on lines 769 to 778
fn direct_abst_declarator(&mut self, base_ty: &TypeRef) -> Result<TypeRef, CompileError> {
let mut current_ty = if self.consume_punct("(").is_some() {
let current_ty = if self.consume_punct("(").is_some() {
let inner_ty = self.abst_declarator(base_ty)?;
self.expect_punct(")")?;
inner_ty
} else {
*base_ty
};
current_ty = self.parse_abst_postfix_declarators(&current_ty)?;
Ok(current_ty)
self.direct_abst_declarator_suffix(&current_ty)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

抽象宣言子 direct_abst_declarator のパース処理に、通常の宣言子 direct_declarator で行われたようなプレースホルダーを用いた修正が適用されていないため、括弧でグループ化された抽象宣言子(例: int (*)[5])を正しく解釈できません。

現在の実装では、(D)S という構造(Dが内側の宣言子、Sが後置修飾子)を (DS) のように誤って結合してしまいます。例えば sizeof(int (*)[5]) のようなキャスト式で int *[5](intへのポインタの配列)と誤解釈されるバグに繋がります。

direct_declarator の修正(600-620行目)と同様に、プレースホルダーを使って型を正しく合成するロジックに修正すべきです。

Suggested change
fn direct_abst_declarator(&mut self, base_ty: &TypeRef) -> Result<TypeRef, CompileError> {
let mut current_ty = if self.consume_punct("(").is_some() {
let current_ty = if self.consume_punct("(").is_some() {
let inner_ty = self.abst_declarator(base_ty)?;
self.expect_punct(")")?;
inner_ty
} else {
*base_ty
};
current_ty = self.parse_abst_postfix_declarators(&current_ty)?;
Ok(current_ty)
self.direct_abst_declarator_suffix(&current_ty)
}
fn direct_abst_declarator(&mut self, base_ty: &TypeRef) -> Result<TypeRef, CompileError> {
if self.consume_punct("(").is_some() {
let placeholder = TypeRef::default();
let inner_ty = self.abst_declarator(&placeholder)?;
self.expect_punct(")")?;
let suffix_ty = self.direct_abst_declarator_suffix(base_ty)?;
Ok(inner_ty.replace_type(placeholder, suffix_ty))
} else {
self.direct_abst_declarator_suffix(base_ty)
}
}

@broccolingual
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

宣言子のパースに関する大規模なリファクタリング、お疲れ様です。素晴らしい改善だと感じました。
特に、関数ポインタのような複雑な宣言子を正しく解析するための direct_declarator の再実装は、C言語の厄介な仕様を見事に解決しています。プレースホルダー型と replace_type を使うアプローチは非常にスマートです。
また、これまで無視されていたポインタや配列の型修飾子 (const など) に対応した点も、コンパイラの堅牢性を高める重要な改善です。
全体的にコードのモジュール性が向上し、可読性も高まっています。

いくつか軽微な修正点を提案させていただきました。主に、不要な可変参照の削除や、リファクタリングによって生じたと思われる到達不能コード(デッドコード)の削除に関するものです。これら修正により、コードの保守性がさらに向上するかと思います。
素晴らしいプルリクエストをありがとうございます。

Comment thread src/ast/decl.rs Outdated
Comment on lines +119 to +120
fn validate_func_prototype(&mut self, decl: &Decl) -> Result<(), CompileError> {
if let Some(symbol) = self.find_symbol_mut(&decl.name) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

この関数はシンボルテーブルを変更しないため、&mut selffind_symbol_mutは不要です。借用規則をより厳密にするために、&selffind_symbolを使用することを推奨します。

これにより、この関数が状態を変更しないことがシグネチャから明確になります。

Suggested change
fn validate_func_prototype(&mut self, decl: &Decl) -> Result<(), CompileError> {
if let Some(symbol) = self.find_symbol_mut(&decl.name) {
fn validate_func_prototype(&self, decl: &Decl) -> Result<(), CompileError> {
if let Some(symbol) = self.find_symbol(&decl.name) {

@broccolingual broccolingual merged commit 33a0451 into main Jan 11, 2026
1 check passed
@broccolingual broccolingual deleted the refactor/ast-decl branch January 11, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant