Plan 9 Cコンパイラの型変換処理を読む
This content is a draft and will not be included in production builds.
sametype
Section titled “sametype”int sametype(Type *t1, Type *t2)t1 と t2 が同一かどうかを返す。
void arith(Node *n, int f)一般的な算術演算における型変換を行う。具体的には以下のような式を扱う。
int c;c - '0'; /* int */
char *p;p + 3; /* char* */
FILE *p, *q;q - p; /* int */ここで n は次の状態で渡される。
- n.op … 演算子
- n.left … 左辺値
- n.right … 右辺値
- n.left.type … 左辺値の型
- n.right.type … 右辺値の型
型変換の結果で n.type を更新する。このとき、n.type と左右の型が異なる場合は、明示的なキャスト処理を挿入する。従って演算を厳密に書くと以下のようになる。
p + 3 // char* + int// 演算の結果はchar*となる(char*)(p + 3)// 結果と右辺値の型が異なるので明示的なキャストが挿入される(char*)(p + (char*)3)int tcom(Node *n)int tcomo(Node *n, int f)