// File: /workspace/github_c_makefile_star2_O2/pocari[P]compilerbook-9cc/type.c
// Function: add_type
void add_type(Node *node) {
  if (!node || node->ty)
    return;

  assert(node);
  add_type(node->lhs);
  add_type(node->rhs);
  add_type(node->cond);
  add_type(node->then);
  add_type(node->els);
  add_type(node->init);
  add_type(node->inc);

  for (Node *n = node->body; n; n = n->next) {
    add_type(n);
  }
  for (Node *n = node->arg; n; n = n->next) {
    add_type(n);
  }
  #pragma clang diagnostic ignored "-Wswitch"
  switch (node->kind) {
    case ND_ADD:
    case ND_SUB:
    case ND_PTR_DIFF:
    case ND_MUL:
    case ND_DIV:
    case ND_EQL:
    case ND_NOT_EQL:
    case ND_LT:
    case ND_LTE:
    case ND_BIT_AND:
    case ND_BIT_OR:
    case ND_BIT_XOR:
    case ND_A_LSHIFT:
    case ND_A_RSHIFT:
    case ND_NUM:
      node->ty = long_type;
      return;
    case ND_PTR_ADD:
    case ND_PTR_SUB:
    case ND_ASSIGN:
    case ND_POST_INC:
    case ND_POST_DEC:
    case ND_PRE_INC:
    case ND_PRE_DEC:
      node->ty = node->lhs->ty;
      return;
    case ND_VAR:
      node->ty = node->var->type;
      return;
    case ND_MEMBER:
      node->ty = node->member->ty;
      return;
    case ND_TERNARY:
      node->ty = node->then->ty;
      return;
    case ND_ADDR:
      if (node->lhs->ty->kind == TY_ARRAY) {
        node->ty = pointer_to(node->lhs->ty->ptr_to);
      } else {
        node->ty = pointer_to(node->lhs->ty);
      }
      return;
    case ND_DEREF:
      {
        if (!node->lhs->ty->ptr_to) {
          error_at(node->tok->str, "invalid pointer dereferrence");
        }
        Type *ty = node->lhs->ty->ptr_to;
        if (ty->kind == TY_VOID) {
          error_at(node->tok->str, "void *ポインタをデリファレンスできません");
        }
        if (ty->kind == TY_STRUCT && ty->is_incomplete) {
          error_at(node->tok->str, "不完全な構造体のデリファレンスはできません");
        }
        node->ty = ty;
      }
      return;
  }

}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x8b09
movq %rdi, %rbx
cmpq $0x0, 0x10(%rdi)
jne 0x8b09
movq 0x18(%rbx), %rdi
callq 0x8a4d
movq 0x20(%rbx), %rdi
callq 0x8a4d
movq 0x38(%rbx), %rdi
callq 0x8a4d
movq 0x40(%rbx), %rdi
callq 0x8a4d
movq 0x48(%rbx), %rdi
callq 0x8a4d
movq 0x58(%rbx), %rdi
callq 0x8a4d
movq 0x60(%rbx), %rdi
callq 0x8a4d
leaq 0x28(%rbx), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x8ac1
movq %r14, %rdi
callq 0x8a4d
addq $0x30, %r14
jmp 0x8aab
leaq 0x70(%rbx), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x8adb
movq %r14, %rdi
callq 0x8a4d
addq $0x30, %r14
jmp 0x8ac5
movl (%rbx), %eax
decl %eax
cmpl $0x2f, %eax
ja 0x8b09
leaq 0x2541(%rip), %rcx      # 0xb02c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x68d5(%rip), %r14      # 0xf3d0
jmp 0x8b05
movq 0x18(%rbx), %rax
movq 0x10(%rax), %r14
movq %r14, 0x10(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r14
testq %r14, %r14
jne 0x8b44
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x25bb(%rip), %rsi      # 0xb0ec
xorl %eax, %eax
callq 0x46f0
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r14
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x8b65
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x25ae(%rip), %rsi      # 0xb109
xorl %eax, %eax
callq 0x46f0
movl (%r14), %eax
cmpl $0x9, %eax
jne 0x8b05
cmpb $0x1, 0xc(%r14)
jne 0x8b05
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x25c3(%rip), %rsi      # 0xb143
xorl %eax, %eax
callq 0x46f0
jmp 0x8b05
movq 0x78(%rbx), %rax
movq (%rax), %r14
jmp 0x8b05
movq 0x40(%rbx), %rax
jmp 0x8b01
movq 0x98(%rbx), %rax
movq 0x8(%rax), %r14
jmp 0x8b05
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rdi
cmpl $0x8, (%rdi)
jne 0x8bc2
movq 0x10(%rdi), %rdi
callq 0x8990
movq %rax, %r14
jmp 0x8b05
nop