From 8c0a940bfee53f1b3bd73cd5528bd7b6006ed23f Mon Sep 17 00:00:00 2001 From: Tim Docker Date: Thu, 8 Dec 2022 16:49:59 +1300 Subject: [PATCH] include local references in emitted ast --- adl/stdlib/sys/adlast2.adl | 46 +++++++-- rust/compiler/src/adlgen/sys/adlast2.rs | 78 +++++++++++--- rust/compiler/src/processing/primitives.rs | 113 ++++++++------------- rust/compiler/src/processing/resolver.rs | 24 ++--- 4 files changed, 157 insertions(+), 104 deletions(-) diff --git a/adl/stdlib/sys/adlast2.adl b/adl/stdlib/sys/adlast2.adl index c7e32a60..fe959840 100644 --- a/adl/stdlib/sys/adlast2.adl +++ b/adl/stdlib/sys/adlast2.adl @@ -16,13 +16,6 @@ struct ScopedName Ident name; }; -union TypeRef -{ - Ident primitive; - Ident typeParam; - ScopedName reference; -}; - struct TypeExpr { R typeRef; @@ -112,4 +105,43 @@ struct Spanned { Span span; }; +union PrimitiveType { + Void Void; + Void Bool; + Void Int8; + Void Int16; + Void Int32; + Void Int64; + Void Word8; + Void Word16; + Void Word32; + Void Word64; + Void Float; + Void Double; + Void Json; + Void ByteVector; + Void String; + Void Vector; + Void StringMap; + Void Nullable; + Void TypeToken; +}; + +union TypeRef { + ScopedName scopedName; + Ident localName; + PrimitiveType primitive; + Ident typeParam; +}; + +// AST after initial parsing +type TypeExpr0 = TypeExpr; +type Module0 = Module; + + +// AST after name resolution +type TypeExpr1 = TypeExpr; +type Decl1 = Decl; +type Module1 = Module; + }; diff --git a/rust/compiler/src/adlgen/sys/adlast2.rs b/rust/compiler/src/adlgen/sys/adlast2.rs index 28fb7c85..960d9d85 100644 --- a/rust/compiler/src/adlgen/sys/adlast2.rs +++ b/rust/compiler/src/adlgen/sys/adlast2.rs @@ -28,18 +28,6 @@ impl ScopedName { } } -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub enum TypeRef { - #[serde(rename="primitive")] - Primitive(Ident), - - #[serde(rename="typeParam")] - TypeParam(Ident), - - #[serde(rename="reference")] - Reference(ScopedName), -} - #[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] pub struct TypeExpr { #[serde(rename="typeRef")] @@ -279,3 +267,69 @@ impl Spanned { } } } + +#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] +pub enum PrimitiveType { + Void, + + Bool, + + Int8, + + Int16, + + Int32, + + Int64, + + Word8, + + Word16, + + Word32, + + Word64, + + Float, + + Double, + + Json, + + ByteVector, + + String, + + Vector, + + StringMap, + + Nullable, + + TypeToken, +} + +#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] +pub enum TypeRef { + #[serde(rename="scopedName")] + ScopedName(ScopedName), + + #[serde(rename="localName")] + LocalName(Ident), + + #[serde(rename="primitive")] + Primitive(PrimitiveType), + + #[serde(rename="typeParam")] + TypeParam(Ident), +} + +pub type TypeExpr0 = TypeExpr; + +pub type Module0 = Module; + +pub type TypeExpr1 = TypeExpr; + +pub type Decl1 = Decl; + +pub type Module1 = Module; diff --git a/rust/compiler/src/processing/primitives.rs b/rust/compiler/src/processing/primitives.rs index 629f76a0..146c413b 100644 --- a/rust/compiler/src/processing/primitives.rs +++ b/rust/compiler/src/processing/primitives.rs @@ -1,73 +1,50 @@ -#[derive(serde::Serialize)] -pub enum PrimitiveType { - Void, - Bool, - Int8, - Int16, - Int32, - Int64, - Word8, - Word16, - Word32, - Word64, - Float, - Double, - Json, - ByteVector, - String, - Vector, - StringMap, - Nullable, - TypeToken, -} +use crate::adlgen::sys::adlast2::PrimitiveType; -impl PrimitiveType { - pub fn to_str(&self) -> &str { - match self { - PrimitiveType::Void => "Void", - PrimitiveType::Bool => "Bool", - PrimitiveType::Int8 => "Int8", - PrimitiveType::Int16 => "Int16", - PrimitiveType::Int32 => "Int32", - PrimitiveType::Int64 => "Int64", - PrimitiveType::Word8 => "Word8", - PrimitiveType::Word16 => "Word16", - PrimitiveType::Word32 => "Word32", - PrimitiveType::Word64 => "Word64", - PrimitiveType::Float => "Float", - PrimitiveType::Double => "Double", - PrimitiveType::Json => "Json", - PrimitiveType::ByteVector => "ByteVector", - PrimitiveType::String => "String", - PrimitiveType::Vector => "Vector", - PrimitiveType::StringMap => "StringMap", - PrimitiveType::Nullable => "Nullable", - PrimitiveType::TypeToken => "TypeToken", - } +pub fn str_from_prim(prim: PrimitiveType) -> &'static str { + match prim { + PrimitiveType::Void => "Void", + PrimitiveType::Bool => "Bool", + PrimitiveType::Int8 => "Int8", + PrimitiveType::Int16 => "Int16", + PrimitiveType::Int32 => "Int32", + PrimitiveType::Int64 => "Int64", + PrimitiveType::Word8 => "Word8", + PrimitiveType::Word16 => "Word16", + PrimitiveType::Word32 => "Word32", + PrimitiveType::Word64 => "Word64", + PrimitiveType::Float => "Float", + PrimitiveType::Double => "Double", + PrimitiveType::Json => "Json", + PrimitiveType::ByteVector => "ByteVector", + PrimitiveType::String => "String", + PrimitiveType::Vector => "Vector", + PrimitiveType::StringMap => "StringMap", + PrimitiveType::Nullable => "Nullable", + PrimitiveType::TypeToken => "TypeToken", } +} - pub fn from_str(s: &str) -> Option { - match s { - "Void" => Some(Self::Void), - "Bool" => Some(Self::Bool), - "Int8" => Some(Self::Int8), - "Int16" => Some(Self::Int16), - "Int32" => Some(Self::Int32), - "Int64" => Some(Self::Int64), - "Word8" => Some(Self::Word8), - "Word16" => Some(Self::Word16), - "Word32" => Some(Self::Word32), - "Word64" => Some(Self::Word64), - "Float" => Some(Self::Float), - "Double" => Some(Self::Double), - "Json" => Some(Self::Json), - "ByteVector" => Some(Self::ByteVector), - "String" => Some(Self::String), - "Vector" => Some(Self::Vector), - "StringMap" => Some(Self::StringMap), - "Nullable" => Some(Self::Nullable), - "TypeToken" => Some(Self::TypeToken), - _ => None, - } +pub fn prim_from_str(s: &str) -> Option { + match s { + "Void" => Some(PrimitiveType::Void), + "Bool" => Some(PrimitiveType::Bool), + "Int8" => Some(PrimitiveType::Int8), + "Int16" => Some(PrimitiveType::Int16), + "Int32" => Some(PrimitiveType::Int32), + "Int64" => Some(PrimitiveType::Int64), + "Word8" => Some(PrimitiveType::Word8), + "Word16" => Some(PrimitiveType::Word16), + "Word32" => Some(PrimitiveType::Word32), + "Word64" => Some(PrimitiveType::Word64), + "Float" => Some(PrimitiveType::Float), + "Double" => Some(PrimitiveType::Double), + "Json" => Some(PrimitiveType::Json), + "ByteVector" => Some(PrimitiveType::ByteVector), + "String" => Some(PrimitiveType::String), + "Vector" => Some(PrimitiveType::Vector), + "StringMap" => Some(PrimitiveType::StringMap), + "Nullable" => Some(PrimitiveType::Nullable), + "TypeToken" => Some(PrimitiveType::TypeToken), + _ => None, } } diff --git a/rust/compiler/src/processing/resolver.rs b/rust/compiler/src/processing/resolver.rs index 128b777e..5eb17818 100644 --- a/rust/compiler/src/processing/resolver.rs +++ b/rust/compiler/src/processing/resolver.rs @@ -1,11 +1,10 @@ -use serde::Serialize; use std::collections::{HashMap, HashSet}; use crate::adlgen::sys::adlast2 as adlast; use crate::adlrt::custom::sys::types::map::Map; use super::loader::AdlLoader; -use super::primitives::PrimitiveType; +use super::primitives::{prim_from_str, str_from_prim}; use super::{Module0, TypeExpr0}; type Result = std::result::Result; @@ -20,18 +19,12 @@ pub enum ResolveError { LoadFailed, } -pub type TypeExpr1 = adlast::TypeExpr; -pub type Decl1 = adlast::Decl; -pub type Module1 = adlast::Module; +pub type TypeRef = adlast::TypeRef; +pub type TypeExpr1 = adlast::TypeExpr1; +pub type Decl1 = adlast::Decl1; +pub type Module1 = adlast::Module1; pub type ModuleName = adlast::ModuleName; -#[derive(Serialize)] -pub enum TypeRef { - ScopedName(adlast::ScopedName), - Primitive(PrimitiveType), - TypeParam(adlast::Ident), -} - pub struct Resolver { loader: Box, modules: HashMap, @@ -299,14 +292,11 @@ impl<'a> ResolveCtx<'a> { if self.type_params.contains(name.as_str()) { return Ok(TypeRef::TypeParam(name.clone())); } - if let Some(ptype) = PrimitiveType::from_str(&name) { + if let Some(ptype) = prim_from_str(&name) { return Ok(TypeRef::Primitive(ptype)); } if self.module0.decls.contains_key(name) { - return Ok(TypeRef::ScopedName(adlast::ScopedName::new( - self.module0.name.value.clone(), - name.clone(), - ))); + return Ok(TypeRef::LocalName(name.clone())); } if let Some(scoped_name) = self.expanded_imports.get(name) { return Ok(TypeRef::ScopedName(scoped_name.clone()));