// File: /workspace/github_c_makefile_star2_O3/pocari[P]compilerbook-9cc/parser.c
// Function: basetype
static Type *basetype(StorageClass *sclass) {
  if (!is_type(token)) {
    error_at(token->str, "typename expected");
  }

  // typedef x
  // なども合法なのでデフォルトの型をintにしておく
  Type *ty = int_type;

  // 各型が何回出てきたかを数えるcounter
  int counter = 0;

  // counterに加えていく値。
  // 適当に2回足してもかぶらないようにシフトさせているんだと思う
  enum {
    VOID  = 1 << 0,
    BOOL  = 1 << 2,
    CHAR  = 1 << 4,
    SHORT = 1 << 6,
    INT   = 1 << 8,
    LONG  = 1 << 10,
    OTHER = 1 << 12,
  };

  // storage class が指定可能な場所はまず未定義で初期化
  if (sclass) {
    *sclass = 0;
  }

  while (is_type(token)) {
    Token *tk = token;

    if (peek_kind(TK_TYPEDEF) || peek_kind(TK_STATIC) || peek_kind(TK_EXTERN)) {
      if (!sclass) {
        error_at(tk->str, "ストレージクラス指定子はここでは使えません");
      }
      // 一度でもどこかにtypedef/staticがついていたら覚えておく
      if (consume_kind(TK_TYPEDEF)) {
        *sclass |= TYPEDEF;
      } else if (consume_kind(TK_STATIC)) {
        *sclass |= STATIC;
      } else if (consume_kind(TK_EXTERN)) {
        *sclass |= EXTERN;
      }

      // typedef と static と extern を同時に使っていたらエラーにする
      // 001        010       100
      // 3bitのうちどこか1bitのみ立っている場合だけOKでそれ以外はNGにしたい。
      // ある値xがあったとして
      // x & (x - 1) が 0 になる場合は どこかの1bitしか立っていないということに鳴るらしい
      // 例
      // x = 0b0010 の場合
      // x & (x - 1) = 0b0010 & 0b0001 = 0
      //
      // x = 0b0011 の場合
      // x & (x - 1) = 0b0011 & 0b0001 = 0b0001
      //
      // となり、複数のbitが立っているときのみ0以外になる
      if (*sclass & (*sclass - 1)) {
        // どこかの複数のbitが立っていた場合は複数のsclassが使われていることになるので
        error_at(tk->str, "static、typedefとexternは同時に指定できません");
      }

      continue;
    }

    if (!is_builtin_type(tk)) {
      if (counter) {
        //ビルトインの型名じゃないものがきて、かつ、一度でも何かの型を読んでいたら
        //変数宣言(またはtypedef)の後の識別子にきてるので、終了
        break;
      }
      //まだ何も型名を読んでいなくて、ビルトインの方じゃない場合は、構造体か、typedefされた型名になる。
      if (tk->kind == TK_STRUCT) {
        ty = struct_decl();
      } else if (tk->kind == TK_ENUM) {
        ty = enum_decl();
      } else {
        ty = find_typedef(tk);
        // fprintf(stderr, "found typedef: %s\n", my_strndup(tk->str, tk->len));
        // fprintf(stderr, "found is_incomplete: %d\n", ty->is_incomplete);
        assert(ty);
        token = token->next;
      }

      // ビルトイン以外の型を読んだのでカウンターを上げる
      counter += OTHER;
      continue;
    }

    if (consume_kind(TK_VOID)) {
      counter += VOID;
    } else if (consume_kind(TK_BOOL)) {
      counter += BOOL;
    } else if (consume_kind(TK_CHAR)) {
      counter += CHAR;
    } else if (consume_kind(TK_SHORT)) {
      counter += SHORT;
    } else if (consume_kind(TK_INT)) {
      counter += INT;
    } else if (consume_kind(TK_LONG)) {
      counter += LONG;
    }

    // counterがとり得る値のみ許可して他はエラー
    switch (counter) {
      case VOID:
        ty = void_type;
        break;
      case BOOL:
        ty = bool_type;
        break;
      case CHAR:
        ty = char_type;
        break;
      case SHORT:
      case SHORT + INT: // これで short int, int short どっちも判断できる
        ty = short_type;
        break;
      case INT:
        ty = int_type;
        break;
      case LONG:
      case LONG + INT:
      case LONG + LONG:
      case LONG + LONG + INT:
        ty = long_type;
        break;
      default:
        error_at(tk->str, "不正な型です");
    }
  }

  return ty;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0xb23c(%rip), %r15      # 0x10480
movq (%r15), %r14
movl (%r14), %eax
leal -0xa(%rax), %ecx
cmpl $0x6, %ecx
movq %rdi, (%rsp)
jb 0x526d
cmpl $0x17, %eax
ja 0x57b2
movl $0xe30000, %ecx         # imm = 0xE30000
btl %eax, %ecx
jae 0x57b2
leaq 0xb0ec(%rip), %rax      # 0x10360
movq (%rax), %rbp
testq %rdi, %rdi
je 0x5282
movl $0x0, (%rdi)
xorl %r12d, %r12d
movl (%r14), %ebx
leal -0xa(%rbx), %eax
cmpl $0x6, %eax
jae 0x52ef
jae 0x52a6
movl %eax, %eax
movq 0x8(%r14), %rcx
movq %rcx, (%r15)
leaq 0x73e2(%rip), %rcx      # 0xc684
addl (%rcx,%rax,4), %r12d
cmpl $0x13f, %r12d           # imm = 0x13F
jg 0x5325
cmpl $0xf, %r12d
jle 0x5404
cmpl $0x10, %r12d
je 0x5545
cmpl $0x40, %r12d
je 0x544c
cmpl $0x100, %r12d           # imm = 0x100
jne 0x5820
leaq 0xb07f(%rip), %rax      # 0x10360
movq (%rax), %rbp
movl $0x100, %r12d           # imm = 0x100
jmp 0x5798
leal -0x15(%rbx), %r13d
cmpl $0x3, %r13d
jae 0x535e
testq %rdi, %rdi
je 0x57ef
cmpl $0x16, %ebx
je 0x545b
cmpl $0x15, %ebx
jne 0x5469
movq 0x8(%r14), %rax
movq %rax, (%r15)
movl (%rdi), %eax
orl $0x1, %eax
jmp 0x5475
cmpl $0x4ff, %r12d           # imm = 0x4FF
jle 0x542d
cmpl $0x500, %r12d           # imm = 0x500
je 0x5443
cmpl $0x800, %r12d           # imm = 0x800
je 0x5443
cmpl $0x900, %r12d           # imm = 0x900
je 0x5443
jmp 0x5820
leal -0x10(%rbx), %eax
cmpl $0x2, %eax
jae 0x5487
testl %r12d, %r12d
jne 0x57a0
cmpl $0x11, %ebx
je 0x54ab
cmpl $0x10, %ebx
jne 0x5510
movl $0x10, %edi
callq 0x5a49
movq (%r15), %r14
cmpl $0x2, (%r14)
leaq 0x6f79(%rip), %r12      # 0xc316
jne 0x555a
movq 0x8(%r14), %rbx
movq %rbx, (%r15)
movq %r12, %rdi
callq 0x51eb
testb %al, %al
je 0x5651
movq %r12, %rdi
callq 0x51eb
testb %al, %al
je 0x563e
movq 0x8(%rbx), %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x5a7d
testq %rax, %rax
je 0x5698
movl 0x18(%rax), %ecx
cmpl 0xb045(%rip), %ecx      # 0x10430
jne 0x5698
movq 0x10(%rax), %rbp
cmpl $0x9, (%rbp)
je 0x56b0
jmp 0x5889
cmpl $0x1, %r12d
je 0x5530
cmpl $0x4, %r12d
jne 0x5820
leaq 0xaf09(%rip), %rax      # 0x10328
movq (%rax), %rbp
movl $0x4, %r12d
jmp 0x5798
cmpl $0x140, %r12d           # imm = 0x140
je 0x544c
cmpl $0x400, %r12d           # imm = 0x400
jne 0x5820
leaq 0xaf86(%rip), %rax      # 0x103d0
jmp 0x5453
leaq 0xafb5(%rip), %rax      # 0x10408
movq (%rax), %rbp
jmp 0x5798
movq 0x8(%r14), %rax
movq %rax, (%r15)
movl (%rdi), %eax
orl $0x2, %eax
jmp 0x5475
movl (%rdi), %eax
movq 0x8(%r14), %rcx
movq %rcx, (%r15)
orl $0x4, %eax
movl %eax, (%rdi)
leal -0x1(%rax), %ecx
testl %ecx, %eax
je 0x5798
jmp 0x57e2
movq %r14, %rdi
callq 0x59dc
movq (%rsp), %rdi
testq %rax, %rax
je 0x57a0
cmpl $0x2, %r13d
jbe 0x52f9
jmp 0x536a
movl $0x11, %edi
callq 0x5a49
movq (%r15), %r14
movl (%r14), %ebx
cmpl $0x2, %ebx
leaq 0x6e51(%rip), %r12      # 0xc316
jne 0x557e
movq 0x8(%r14), %rax
movq %rax, (%r15)
movq %r12, %rdi
callq 0x51eb
testb %al, %al
jne 0x5581
movq %r14, %rdi
callq 0x5a7d
testq %rax, %rax
je 0x582d
movq 0x10(%rax), %rbp
movl $0x1000, %r12d          # imm = 0x1000
cmpl $0xa, (%rbp)
movq (%rsp), %rdi
je 0x5798
jmp 0x585b
movq %r14, %rdi
callq 0x59dc
testq %rax, %rax
je 0x5801
movq %rax, %rbp
movq 0x8(%r14), %rax
movq %rax, (%r15)
jmp 0x578e
leaq 0xadb9(%rip), %rax      # 0x102f0
movq (%rax), %rbp
movl $0x1, %r12d
jmp 0x5798
leaq 0xae4c(%rip), %rax      # 0x10398
movq (%rax), %rbp
movl $0x10, %r12d
jmp 0x5798
movq %r12, %rdi
callq 0x51eb
testb %al, %al
je 0x563e
movq 0x8(%r14), %rax
movq %rax, (%r15)
callq 0x9eac
movq %rax, %rbp
jmp 0x56b0
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x5b5c
xorl %r13d, %r13d
callq 0x9e86
movq %rax, %rbp
callq 0x5bde
movq %rax, %r12
leaq 0x735e(%rip), %rdi      # 0xc901
callq 0x51eb
testb %al, %al
je 0x55c8
movq (%r15), %rax
movq 0x8(%rax), %rax
movq %rax, (%r15)
callq 0x5fef
movq %rax, %rdi
xorl %esi, %esi
callq 0x5c54
movq %rax, %r13
movl $0x1, %edi
movl $0x30, %esi
callq 0x2100
movq %r12, 0x8(%rax)
movq 0xae5e(%rip), %rcx      # 0x10440
movq %rcx, (%rax)
movl 0xae45(%rip), %ecx      # 0x10430
movl %ecx, 0x2c(%rax)
movq %rax, 0xae4b(%rip)      # 0x10440
movq %rbp, 0x20(%rax)
movl %r13d, 0x28(%rax)
callq 0x4b2b
testb %al, %al
jne 0x561a
incl %r13d
leaq 0x709f(%rip), %rdi      # 0xc6af
callq 0x5b5c
jmp 0x5594
movl $0x1000, %r12d          # imm = 0x1000
cmpl $0x2, %ebx
jne 0x5794
movq 0x18(%r14), %rdi
movl 0x20(%r14), %esi
movq %rbp, %rdx
callq 0x5ad5
jmp 0x5794
movl $0x1000, %r12d          # imm = 0x1000
callq 0x9eac
movq %rax, %rbp
jmp 0x5794
movq %r14, %rdi
callq 0x5a7d
testq %rax, %rax
je 0x567b
movq 0x10(%rax), %rbp
movl $0x1000, %r12d          # imm = 0x1000
cmpl $0x9, (%rbp)
movq (%rsp), %rdi
je 0x5798
jmp 0x5889
callq 0x9eac
movq %rax, %rbp
movq 0x18(%r14), %rdi
movl 0x20(%r14), %esi
movq %rax, %rdx
callq 0x5ad5
jmp 0x578e
callq 0x9eac
movq %rax, %rbp
movq 0x18(%r14), %rdi
movl 0x20(%r14), %esi
movq %rax, %rdx
callq 0x5ad5
movq $0x0, 0x8(%rsp)
leaq 0x6fed(%rip), %rdi      # 0xc6ad
callq 0x51eb
testb %al, %al
jne 0x5735
leaq 0x8(%rsp), %rbx
xorl %edi, %edi
callq 0x522f
movq (%r15), %r12
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
callq 0x58f5
cmpb $0x1, 0xc(%rax)
je 0x57d4
movq %rax, %r14
leaq 0x6fc1(%rip), %rdi      # 0xc6ba
callq 0x5b5c
movl $0x1, %edi
movl $0x20, %esi
callq 0x2100
movq %rax, %r12
movq 0x10(%rsp), %rax
movq %rax, 0x10(%r12)
movq %r14, 0x8(%r12)
movq %r12, (%rbx)
leaq 0x6f84(%rip), %rdi      # 0xc6ad
callq 0x51eb
movq %r12, %rbx
testb %al, %al
je 0x56ce
movq (%r15), %rax
movq 0x8(%rax), %rax
movq %rax, (%r15)
movq 0x8(%rsp), %rsi
movq %rsi, 0x20(%rbp)
movl 0x8(%rbp), %ecx
xorl %eax, %eax
testq %rsi, %rsi
je 0x577b
movq 0x8(%rsi), %r8
movl 0x8(%r8), %edi
cltd
idivl %edi
cmpl $0x1, %edx
sbbl $-0x1, %eax
imull %edi, %eax
movl %eax, 0x18(%rsi)
addl 0x4(%r8), %eax
cmpl %edi, %ecx
jge 0x5776
movl %edi, 0x8(%rbp)
movl %edi, %ecx
movq (%rsi), %rsi
jmp 0x574d
cltd
idivl %ecx
cmpl $0x1, %edx
sbbl $-0x1, %eax
imull %ecx, %eax
movl %eax, 0x4(%rbp)
movb $0x0, 0xc(%rbp)
movl $0x1000, %r12d          # imm = 0x1000
movq (%rsp), %rdi
movq (%r15), %r14
jmp 0x5285
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x59dc
movq (%rsp), %rdi
testq %rax, %rax
jne 0x526d
movq 0x18(%r14), %rdi
leaq 0x6eea(%rip), %rsi      # 0xc6bc
jmp 0x57fa
movq 0x18(%r12), %rdi
leaq 0x6fef(%rip), %rsi      # 0xc7cf
jmp 0x57fa
movq 0x18(%r14), %rdi
leaq 0x6f21(%rip), %rsi      # 0xc70e
jmp 0x57fa
movq 0x18(%r14), %rdi
leaq 0x6ed4(%rip), %rsi      # 0xc6ce
xorl %eax, %eax
callq 0x487c
leaq 0x6f41(%rip), %rdi      # 0xc749
leaq 0x6ea2(%rip), %rsi      # 0xc6b1
leaq 0x6f36(%rip), %rcx      # 0xc74c
movl $0x41f, %edx            # imm = 0x41F
callq 0x20d0
movq 0x18(%r14), %rdi
leaq 0x6f40(%rip), %rsi      # 0xc76b
jmp 0x57fa
movq 0x18(%r14), %rbx
movslq 0x20(%r14), %r15
leaq 0x1(%r15), %rdi
movl $0x1, %esi
callq 0x2100
movq %rax, %r14
testq %r15, %r15
jle 0x58b7
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2150
jmp 0x58ba
movq 0x18(%r14), %rbx
movslq 0x20(%r14), %r15
leaq 0x1(%r15), %rdi
movl $0x1, %esi
callq 0x2100
movq %rax, %r14
testq %r15, %r15
jle 0x58c8
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2150
jmp 0x58cb
movq 0x18(%r14), %rbx
movslq 0x20(%r14), %r15
leaq 0x1(%r15), %rdi
movl $0x1, %esi
callq 0x2100
movq %rax, %r14
testq %r15, %r15
jle 0x58d9
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2150
jmp 0x58dc
xorl %r15d, %r15d
movb $0x0, (%r14,%r15)
leaq 0x6f73(%rip), %rsi      # 0xc839
jmp 0x58e8
xorl %r15d, %r15d
movb $0x0, (%r14,%r15)
leaq 0x6f84(%rip), %rsi      # 0xc85b
jmp 0x58e8
xorl %r15d, %r15d
movb $0x0, (%r14,%r15)
leaq 0x6e96(%rip), %rsi      # 0xc77e
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x487c