// File: /workspace/github_c_makefile_star2_O1/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 0xa3bf
movq %rdi, %rbx
cmpq $0x0, 0x10(%rdi)
jne 0xa3bf
movq 0x18(%rbx), %rdi
callq 0xa308
movq 0x20(%rbx), %rdi
callq 0xa308
movq 0x38(%rbx), %rdi
callq 0xa308
movq 0x40(%rbx), %rdi
callq 0xa308
movq 0x48(%rbx), %rdi
callq 0xa308
movq 0x58(%rbx), %rdi
callq 0xa308
movq 0x60(%rbx), %rdi
callq 0xa308
movq 0x28(%rbx), %r14
testq %r14, %r14
je 0xa37a
movq %r14, %rdi
callq 0xa308
movq 0x30(%r14), %r14
jmp 0xa367
movq 0x70(%rbx), %r14
testq %r14, %r14
je 0xa391
movq %r14, %rdi
callq 0xa308
movq 0x30(%r14), %r14
jmp 0xa37e
movl (%rbx), %eax
decl %eax
cmpl $0x2f, %eax
ja 0xa3bf
leaq 0x2c33(%rip), %rcx      # 0xcfd4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x601f(%rip), %r14      # 0x103d0
jmp 0xa3bb
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
cmpq $0x0, 0x10(%rax)
jne 0xa3ea
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x2cb1(%rip), %rsi      # 0xd094
xorl %eax, %eax
callq 0x487c
movq 0x18(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r14
cmpl $0x1, (%r14)
jne 0xa412
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x2ca6(%rip), %rsi      # 0xd0b1
xorl %eax, %eax
callq 0x487c
cmpl $0x9, (%r14)
jne 0xa3bb
cmpb $0x1, 0xc(%r14)
jne 0xa3bb
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x2cbd(%rip), %rsi      # 0xd0eb
xorl %eax, %eax
callq 0x487c
jmp 0xa3bb
movq 0x78(%rbx), %rax
movq (%rax), %r14
jmp 0xa3bb
movq 0x40(%rbx), %rax
jmp 0xa3b7
movq 0x98(%rbx), %rax
movq 0x8(%rax), %r14
jmp 0xa3bb
movq 0x18(%rbx), %rax
movq 0x10(%rax), %r15
cmpl $0x8, (%r15)
jne 0xa46e
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 0xa3bb
nop