// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

include "ModuleFormat.fbs";

namespace BCHIRFormat;

file_identifier "BIR2";

struct CodePosition {
  file: uint32;
  line: uint32; // line
  col: uint32;  // column
}

struct PosAnnotation {
  index: uint32;
  codePosition: CodePosition;
}

// Just used in debug mode.
struct ChirIDAnnotation {
  index: uint32;
  // the original CHIR node ID
  chidNodeId: uint64;
}

table TType {
  //All the different types are flattened
  kind:uint8; // TypeKind static casted to an unsigned int
  // TFunc
  retType:uint32;
  hasVarLenParam:bool; // Func type.
  isCFunc:bool; // Func type.
  isBoxed:bool; // Enum type.
  // TypeArguments, also for funcTy's paramTypes
  typeArgs:[uint32];
  // TypeArguments, also for structdef Ty's paramTypes
  typeLocalMemberArgs:[uint32];
  dims: uint64 = 0; // RawArray
}

table MangledNamePair {
  index: uint32; // index to the vector bytecode in Definition
  mangled: uint32; // index to the vector mangledNames in Model
}

table CodePositionPair {
  index: uint32; // index to the vector bytecode in Definition
  pos: CodePosition;
}

struct VMethod {
  method : uint32; // index to the vector mangledNames in Model
  function : uint32; // index to the vector mangledNames in Model
}

table ClassElem {
  mangledName : uint32; // index to the vector mangledNames in Model
  superClasses : [uint32]; // indexes to the vector mangledNames in Model
  vtable : [VMethod];
  finalizer: uint32 = 0;
}

table Definition {
  mangledIndex: uint32;
  numArgs: uint32;
  numLVars: uint32;
  bytecodes: [uint32];
  mangledNamesMap: [MangledNamePair];
  codePositions: [CodePositionPair];
}

table Model {
  // WORK IN PROGRESS
  packageName: string;        // package name
  allTypes: [TType];          // all saved types
  bchirTypes: [uint32];       // the bchir package types, pointers to allTypes
  mangledNames: [string];
  globalVars: [Definition];
  functions: [Definition];
  classTable: [ClassElem];
  globalInitFunc: string;
  fileNames: [string];
  strings: [string];
}

root_type Model;