1.1.3. 树节点的内存分配
1.1.3.1. 节点大小的确定
树节点由下面的make_node函数来分配。函数中的参数code,在上面的表中给出,注意它也是由.def文件定义的。
202 tree
203 make_node (enum tree_code code) in tree.c
204 {
205 tree t;
206 int type = TREE_CODE_CLASS (code);
207 size_t length;
208 #ifdef GATHER_STATISTICS
209 tree_node_kind kind;
210 #endif
211 struct tree_common ttmp;
212
213 /* We can't allocate a TREE_VEC without knowing how many elements
214 it will have. */
215 if (code == TREE_VEC)
216 abort ();
217
218 TREE_SET_CODE ((tree)&ttmp, code);
219 length = tree_size ((tree)&ttmp);
在上面TREE_CODE_CLASS被定义如下,它获取要创建的树节点的类识别码。
54 #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)] in tree.h
对于C++,tree_code_type包含以下文件的内容:其中文件tree.def通用于所有的语言,c-common.def由C/C++使用,cp_tree.def则专用于C++。注意,DEFTREECODE在tree_code_type前被重新定义,获取对应的域。
197 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
199 const char tree_code_type[] = { in cp-lang.c
200 #include "tree.def"
201 'x',
202 #include "c-common.def"
203 'x',
204 #include "cp-tree.def"
205 };
下面,我们补上来自c-common.def的树节点的定义。它关注C/C++中的表达式和语句
Tree Code
|
Type |
Description |
|
SIZEOF_EXPR |
1 |
|
|
ARROW_EXPR |
1 |
|
|
ALIGNOF_EXPR |
1 |
|
|
EXPR_STMT |
e |
用于表达式语句 |
|
COMPOUND_STMT |
e |
用于大括号对围起的代码块。节点中的操作数是COMPOUND_BODY节点 |
|
DECL_STMT |
e |
用于表示局部声明(local declaration) |
|
IF_STMT |
e |
表示if语句 |
|
FOR_STMT |
e |
表示for语句 |
|
WHILE_STMT |
e |
表示while语句 |
|
DO_STMT |
e |
表示do语句 |
|
RETURN_STMT |
e |
表示return语句 |
|
BREAK_STMT |
e |
表示break语句 |
|
CONTINUE_STMT |
e |
表示continue语句 |
|
SWITCH_STMT |
e |
表示switch语句 |
|
GOTO_STMT |
e |
表示goto语句 |
|
LABEL_STMT |
e |
表示label语句 |
|
ASM_STMT |
e |
表示内联的汇编代码 |
|
SCOPE_STMT |
e |
用于表示代码块的起始与结尾 |
|
FILE_STMT |
e |
表示当前源文件改变(换为另一文件)的点 |
|
CASE_LABEL |
e |
用于表示case语句中的标识(label) |
|
STMT_EXPR |
|