gcc的AST是如何表示结构体类型和结构体变量的

gcc版本12.2.0
写一段示例代码struct.c

typedef struct s{
    int x;
    int y;
} s;
int main()
{
    s s1;
    s1.x = 2;
    s1.y = 3;
    int sum = s1.x + s1.y;
}

使用 gcc -fdump-tree-all-raw struct.c -o struct.out 命令编译并生成中间文件
生成的以.original结尾的AST文件如下:


;; Function main (null)
;; enabled by -tree-original

@1      statement_list   0   : @2       1   : @3      
@2      bind_expr        type: @4       vars: @5       body: @6      
@3      return_expr      type: @4       expr: @7      
@4      void_type        name: @8       algn: 8       
@5      var_decl         name: @9       type: @10      scpe: @11     
                         srcp: struct.c:7              size: @12     
                         algn: 32       used: 1       
@6      statement_list   0   : @13      1   : @14      2   : @15     
                         3   : @16     
@7      modify_expr      type: @17      op 0: @18      op 1: @19     
@8      type_decl        name: @20      type: @4      
@9      identifier_node  strg: s1       lngt: 2       
@10     record_type      name: @21      unql: @22      size: @12     
                         algn: 32       tag : struct   flds: @23     
@11     function_decl    name: @24      type: @25      srcp: struct.c:5      
                         link: extern  
@12     integer_cst      type: @26     int: 64
@13     decl_expr        type: @4      
@14     modify_expr      type: @17      op 0: @27      op 1: @28     
@15     modify_expr      type: @17      op 0: @29      op 1: @30     
@16     decl_expr        type: @4      
@17     integer_type     name: @31      size: @32      algn: 32      
                         prec: 32       sign: signed   min : @33     
                         max : @34     
@18     result_decl      type: @17      scpe: @11      srcp: struct.c:5      
                         note: artificial              size: @32     
                         algn: 32      
@19     integer_cst      type: @17     int: 0
@20     identifier_node  strg: void     lngt: 4       
@21     type_decl        name: @35      type: @10      srcp: struct.c:4      
@22     record_type      name: @35      size: @12      algn: 32      
                         tag : struct   flds: @23     
@23     field_decl       name: @36      type: @17      scpe: @22     
                         srcp: struct.c:2              size: @32     
                         algn: 32       bpos: @37     
@24     identifier_node  strg: main     lngt: 4       
@25     function_type    unql: @38      size: @39      algn: 8       
                         retn: @17     
@26     integer_type     name: @40      size: @41      algn: 128     
                         prec: 128      sign: unsigned min : @37     
                         max : @42     
@27     component_ref    type: @17      op 0: @5       op 1: @23     
@28     integer_cst      type: @17     int: 2
@29     component_ref    type: @17      op 0: @5       op 1: @43     
@30     integer_cst      type: @17     int: 3
@31     type_decl        name: @44      type: @17     
@32     integer_cst      type: @26     int: 32
@33     integer_cst      type: @17     int: -2147483648
@34     integer_cst      type: @17     int: 2147483647
@35     identifier_node  strg: s        lngt: 1       
@36     identifier_node  strg: x        lngt: 1       
@37     integer_cst      type: @26     int: 0
@38     function_type    size: @39      algn: 8        retn: @17     
@39     integer_cst      type: @26     int: 8
@40     identifier_node  strg: bitsizetype             lngt: 11      
@41     integer_cst      type: @26     int: 128
@42     integer_cst      type: @26     int: -1
@43     field_decl       name: @45      type: @17      scpe: @22     
                         srcp: struct.c:3              size: @32     
                         algn: 32       bpos: @32     
@44     identifier_node  strg: int      lngt: 3       
@45     identifier_node  strg: y        lngt: 1       

其中@5 var_decl 表示结构体变量s1的声明节点,其type字段指向表示结构体类型s的@10 record_type节点。
在GCC的AST中,record_type是表示结构体类型的节点类型。结构体类型节点通常包含以下子节点:

  • name:表示结构体类型的名称。
  • unql:表示结构体类型的未命名字段(unqualified fields)的数量。
  • size:表示结构体类型的大小。
  • algn:表示结构体类型的对齐方式。
  • tag:表示结构体类型的标签(tag),包括struct、union、enum等。
  • flds:表示结构体类型的字段(fields),包括每个字段的类型、名称、偏移量等信息。

@10的flds字段指向@23 field_decl节点。
在GCC的AST中,field_decl是表示结构体字段的节点类型。结构体字段节点通常包含以下子节点:

  • name:表示结构体字段的名称。
  • type:表示结构体字段的类型。
  • scpe:表示结构体字段的作用域,即包含此字段的结构体类型节点。
  • srcp:表示结构体字段在源代码中的位置。
  • size:表示结构体字段的大小。
  • algn:表示结构体字段的对齐方式。
  • bpos:表示结构体字段在结构体中的偏移量。

本例中结构体类型s有两个字段x和y,分别对应@23和@43两个field_decl节点。

在GCC的AST中,component_ref是表示结构体或联合体成员访问的节点类型。component_ref节点通常包含以下子节点:

  • type:表示成员的类型。
  • op 0:表示成员访问的对象,通常是一个指向结构体或联合体的指针。
  • op 1:表示成员的名称或索引。

以@27节点为例,节点包括type、op 0和op 1。其中,@17是表示成员类型的节点,@5是表示成员访问的对象(即结构体或联合体指针)的节点,@23应该是表示成员名称或索引的节点。

需要注意的是,在某些情况下,component_ref节点也可以用于访问数组元素。此时,op 1节点通常表示数组元素的下标。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值