// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
testq %rdi, %rdi
je 0x9fa6
movq %rdi, %rbx
cmpq $0x0, 0x10(%rdi)
jne 0x9fa6
movq 0x18(%rbx), %rdi
callq 0x9eef
movq 0x20(%rbx), %rdi
callq 0x9eef
movq 0x38(%rbx), %rdi
callq 0x9eef
movq 0x40(%rbx), %rdi
callq 0x9eef
movq 0x48(%rbx), %rdi
callq 0x9eef
movq 0x58(%rbx), %rdi
callq 0x9eef
movq 0x60(%rbx), %rdi
callq 0x9eef
movq 0x28(%rbx), %r14
testq %r14, %r14
je 0x9f61
movq %r14, %rdi
callq 0x9eef
movq 0x30(%r14), %r14
jmp 0x9f4e
movq 0x70(%rbx), %r14
testq %r14, %r14
je 0x9f78
movq %r14, %rdi
callq 0x9eef
movq 0x30(%r14), %r14
jmp 0x9f65
movl (%rbx), %eax
decl %eax
cmpl $0x2f, %eax
ja 0x9fa6
leaq 0x307c(%rip), %rcx      # 0xd004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x6438(%rip), %r14      # 0x103d0
jmp 0x9fa2
movq 0x18(%rbx), %rax
movq 0x10(%rax), %r14
movq %r14, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r14
testq %r14, %r14
jne 0x9fdf
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x30f8(%rip), %rsi      # 0xd0c4
xorl %eax, %eax
callq 0x487c
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r14
movl (%r14), %eax
cmpl $0x1, %eax
jne 0xa000
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x30eb(%rip), %rsi      # 0xd0e1
xorl %eax, %eax
callq 0x487c
movl (%r14), %eax
cmpl $0x9, %eax
jne 0x9fa2
cmpb $0x1, 0xc(%r14)
jne 0x9fa2
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x3100(%rip), %rsi      # 0xd11b
xorl %eax, %eax
callq 0x487c
jmp 0x9fa2
movq 0x78(%rbx), %rax
movq (%rax), %r14
jmp 0x9fa2
movq 0x40(%rbx), %rax
jmp 0x9f9e
movq 0x98(%rbx), %rax
movq 0x8(%rax), %r14
jmp 0x9fa2
movq 0x18(%rbx), %rax
movq 0x10(%rax), %r15
cmpl $0x8, (%r15)
jne 0xa05e
movq 0x10(%r15), %r15
movl $0x1, %edi
movl $0x30, %esi
callq 0x2100
movq %rax, %r14
movabsq $0x800000007, %rax      # imm = 0x800000007
movq %rax, (%r14)
movl $0x8, 0x8(%r14)
movq %r15, 0x10(%r14)
jmp 0x9fa2
nop