Skip to content

Commit

Permalink
align hackc::FileFacts with HPHP::Facts::FileFacts
Browse files Browse the repository at this point in the history
Summary:
We have [at least] three redundant ADTs for representing Facts:
1. Rust hackc::FileFacts designed for interop with C++ FileFacts (#3)
2. Rust facts::Facts, carefully crafted to produce a specific JSON format
3. C++ HPHP::Facts::FileFacts constructed from folly::dynamic (from #2 JSON)

this diff massages #1 to get ready to replace #2 and #3 along with the
unnecessary representation changes that go with them.

Reviewed By: aorenste

Differential Revision: D46969023

fbshipit-source-id: 95f989774c995f094e216d6dc95f8a88aa0b2f06
  • Loading branch information
edwinsmith authored and facebook-github-bot committed Jun 28, 2023
1 parent 350088c commit bdf0ad8
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 134 deletions.
10 changes: 5 additions & 5 deletions hphp/compiler/package.cpp
Expand Up @@ -1643,18 +1643,18 @@ coro::Task<Package::OndemandInfo> Package::emitGroup(
HPHP_CORO_RETURN(OndemandInfo{});
}

Package::IndexMeta HPHP::summary_of_facts(const hackc::FactsResult& facts) {
Package::IndexMeta HPHP::summary_of_facts(const hackc::FileFacts& facts) {
Package::IndexMeta summary;
for (auto& e : facts.facts.types) {
for (auto& e : facts.types) {
summary.types.emplace_back(makeStaticString(std::string(e.name)));
}
for (auto& e : facts.facts.functions) {
for (auto& e : facts.functions) {
summary.funcs.emplace_back(makeStaticString(std::string(e)));
}
for (auto& e : facts.facts.constants) {
for (auto& e : facts.constants) {
summary.constants.emplace_back(makeStaticString(std::string(e)));
}
for (auto& e : facts.facts.modules) {
for (auto& e : facts.modules) {
summary.modules.emplace_back(makeStaticString(std::string(e.name)));
}
return summary;
Expand Down
4 changes: 2 additions & 2 deletions hphp/compiler/package.h
Expand Up @@ -38,7 +38,7 @@ namespace HPHP {
///////////////////////////////////////////////////////////////////////////////

namespace hackc {
struct FactsResult;
struct FileFacts;
}

struct UnitIndex;
Expand Down Expand Up @@ -401,7 +401,7 @@ struct UnitIndex final {

// Given the result of running `hackc::decls_to_facts()`, create a
// `Package::IndexMeta` containing the names of all decls in `facts`.
Package::IndexMeta summary_of_facts(const hackc::FactsResult& facts);
Package::IndexMeta summary_of_facts(const hackc::FileFacts& facts);

///////////////////////////////////////////////////////////////////////////////
}
12 changes: 6 additions & 6 deletions hphp/hack/src/facts/facts.rs
Expand Up @@ -71,7 +71,7 @@ pub struct TypeFacts {
#[serde(default)]
pub attributes: Attributes,

pub flags: isize,
pub flags: u8,

#[serde(default, skip_serializing_if = "StringSet::is_empty")]
pub require_extends: StringSet,
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Facts {
}
}

pub type Flags = isize;
pub type Flags = u8;
#[derive(Clone, Copy)]
pub enum Flag {
Abstract = 1,
Expand All @@ -210,9 +210,9 @@ pub enum Flag {

impl Flag {
pub fn as_flags(&self) -> Flags {
*self as isize
*self as Flags
}
pub fn default() -> Flags {
pub fn zero() -> Flags {
0
}
pub fn is_set(&self, flags: Flags) -> bool {
Expand Down Expand Up @@ -555,7 +555,7 @@ fn format(original_name: &str) -> String {
}
}

fn modifiers_to_flags(flags: isize, is_final: bool, abstraction: Abstraction) -> isize {
fn modifiers_to_flags(flags: Flags, is_final: bool, abstraction: Abstraction) -> Flags {
let flags = match abstraction {
Abstraction::Abstract => Flag::Abstract.set(flags),
Abstraction::Concrete => flags,
Expand Down Expand Up @@ -833,7 +833,7 @@ mod tests {

#[test]
fn test_flags() {
let flags = Flag::default();
let flags = Flag::zero();
assert!(!Flag::Final.is_set(flags));
let flags = Flag::Final.set(flags);
let flags = Flag::Abstract.set(flags);
Expand Down
73 changes: 34 additions & 39 deletions hphp/hack/src/hackc/ffi_bridge/compiler_ffi.rs
Expand Up @@ -131,58 +131,53 @@ pub mod compile_ffi {
#[derive(Debug, PartialEq)]
struct Attribute {
name: String,

/// Values are Hack values encoded as JSON
args: Vec<String>,
}

#[derive(Debug, PartialEq)]
struct MethodFacts {
struct MethodDetails {
name: String,
attributes: Vec<Attribute>,
}

#[derive(Debug, PartialEq)]
struct Method {
pub struct TypeDetails {
name: String,
methfacts: MethodFacts,
}
kind: TypeKind,
flags: u8,

#[derive(Debug, PartialEq)]
pub struct TypeFacts {
pub base_types: Vec<String>,
pub kind: TypeKind,
pub attributes: Vec<Attribute>,
pub flags: isize,
pub require_extends: Vec<String>,
pub require_implements: Vec<String>,
pub require_class: Vec<String>,
pub methods: Vec<Method>,
}
/// List of types which this `extends`, `implements`, or `use`s
base_types: Vec<String>,

#[derive(Debug, PartialEq)]
struct TypeFactsByName {
name: String,
typefacts: TypeFacts,
// List of attributes and their arguments
attributes: Vec<Attribute>,

/// List of classes which this `require class`
require_class: Vec<String>,

/// List of classes or interfaces which this `require extends`
require_extends: Vec<String>,

/// List of interfaces which this `require implements`
require_implements: Vec<String>,
methods: Vec<MethodDetails>,
}

#[derive(Debug, PartialEq)]
struct ModuleFactsByName {
struct ModuleDetails {
name: String,
// Currently does not have modulefacts, since it would be an empty struct
// modulefacts
}

#[derive(Debug, Default, PartialEq)]
struct Facts {
pub types: Vec<TypeFactsByName>,
pub functions: Vec<String>,
pub constants: Vec<String>,
pub file_attributes: Vec<Attribute>,
pub modules: Vec<ModuleFactsByName>,
}

#[derive(Debug, Default)]
pub struct FactsResult {
facts: Facts,
sha1sum: String,
pub struct FileFacts {
types: Vec<TypeDetails>,
functions: Vec<String>,
constants: Vec<String>,
modules: Vec<ModuleDetails>,
attributes: Vec<Attribute>,
sha1hex: String,
}

extern "Rust" {
Expand Down Expand Up @@ -224,7 +219,7 @@ pub mod compile_ffi {
fn verify_deserialization(decls: &DeclsAndBlob) -> bool;

/// Extract Facts from Decls, passing along the source text hash.
fn decls_to_facts(decls: &DeclsHolder, sha1sum: &CxxString) -> FactsResult;
fn decls_to_facts(decls: &DeclsHolder, sha1sum: &CxxString) -> FileFacts;

/// Extract Facts in condensed JSON format from Decls, including the source text hash.
fn decls_to_facts_json(decls: &DeclsHolder, sha1sum: &CxxString) -> String;
Expand Down Expand Up @@ -479,11 +474,11 @@ fn compile_unit_from_text(
.map_err(|e| e.to_string())
}

fn decls_to_facts(holder: &DeclsHolder, sha1sum: &CxxString) -> compile_ffi::FactsResult {
fn decls_to_facts(holder: &DeclsHolder, sha1sum: &CxxString) -> compile_ffi::FileFacts {
let facts = facts::Facts::from_decls(&holder.parsed_file);
compile_ffi::FactsResult {
facts: compile_ffi::Facts::from(facts),
sha1sum: sha1sum.to_string_lossy().to_string(),
compile_ffi::FileFacts {
sha1hex: sha1sum.to_string_lossy().into_owned(),
..facts.into()
}
}

Expand Down

0 comments on commit bdf0ad8

Please sign in to comment.