// 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.

// The Cangjie API is in Beta. For details on its capabilities and limitations, please refer to the README file.

// This file discribes the format of a cangjie package used in incremental compilation.
namespace CachedASTFormat;

// Module
file_identifier "CAST";

// CHIR Opt infos
table DeclDep{
  decl:string;
  dependency:[string];
}

// describes the change of srcDecl is to be perceived by effectedDecls
table EffectMap{
  srcDecl:string;
  effectedDecls:[string];
}

table VirtualDep{
  raw: string;  // 'rawMangledName' of decl
  wrapper: string; // 'mangledName' of virtual func wrapper for CodeGen 
}

// Semantic usage infos
table Usage {
  definition: string;   // 'rawMangledName' of context decl, only be toplevel or member decl.
  apiUsage: UseInfo;    // Use info for API part.
  bodyUsage: UseInfo;   // Use info for implementation part.
  boxedTypes: [string]; // 'rawMangledName' of the types which have been boxed in current 'definition'.
}

table UseInfo {
  usedDecls: [string];   // 'rawMangledName' of decls that directly used decl
  usedNames: [NameInfo]; // identifiers that used as type annotation or decl name
}

table NameInfo {
  name: string;           // Used identifier.
  conditions: [bool];     // Conditions: 1. whether the name has been used as unqualified.
                          //             2. whether the name was resolved as unqualified imported decl
                          //             etc
  parentDecls: [string];    // 'rawMangledName' or builtin type's name of 'name''s qualified parent decl.
  qualifiers: [string];   // qualifier prefix to access package's decl name.
}

table Relation {
  definition: string;   // 'rawMangledname' of the declaration or name of extended built-in type.
  inherited: [string];  // 'rawMangledname' of the inherited types.
  extends: [string];    // 'rawMangledname' of the extened decls.
  extendInterfaces: [string]; // 'rawMangledname' of the extend interfaces.
}

table CompilerAddedUsage {
  definition: string;   // 'rawMangledname' of the declaration
  related: [string];    // 'mangledName' of compiler added decls for CodeGen
}

table SemanticInfo {
  usages: [Usage];
  relations: [Relation];
  builtInTypeRelations: [Relation];
  compilerAddedUsages: [CompilerAddedUsage];
}

// incr ast begins here

table GlobalVarIndex {
  file: string;
  id: int;
}

table MemberDecl {
  mangle:string; // raw mangled name
  sig:uint64; // sig hash
  srcUse:uint64; // srcuse hash
  body:uint64; // body hash
  type:uint8;
  isGV:bool; // is global or static var
  gvid:GlobalVarIndex;
  members:[MemberDecl]; // member decls of member decl, e.g. property can have getters & setters
  cgMangle:string; // mangled name for CodeGen
}

table TopDecl {
  mangle:string; // raw mangled name
  sig:uint64; // signature hash
  srcUse:uint64; // source usage hash
  body:uint64; // body hash
  type:uint8; // astKind
  isGV:bool; // is global var
  instVar:uint64; // instance variable hash
  virt:uint64; // virtual function hash
  gvid:GlobalVarIndex; // see struct GVID
  members:[MemberDecl];
  extends:[string]; // mangled name of extended decls
  cgMangle:string; // mangled name for CodeGen
}

table HashedPackage{
  version:string;             // cjc version
  packageName:string;         // package name.
  specs:uint64;               // package spec reference
  compileOptionArgs:[string];
  topDecls:[TopDecl];      // src decls
  importedDecls:[TopDecl]; // imported decls
  varAndFunc:[DeclDep];    // dependencies among global vars and funcs
  chirOptInfo:[EffectMap];
  virtualDep:[VirtualDep];
  varInitDep:[VirtualDep];
  ccOutFuncs:[string]; // raw mangled name of top or mem funcs had closure convert
  lambdaCounter:uint64;
  envClassCounter:uint64;
  strLitCounter:uint64;
  semanticInfo:SemanticInfo;
  bitcodeFilesName:[string];
}

root_type HashedPackage;