Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Compiler/CompilationModelFeatureTest.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,9 @@ open Verity.EVM.Uint256
verity_contract MacroEventTrace where
storage

event_defs
event Transfer(@indexed amount : Uint256, total : Uint256)

function emitNamed (amount : Uint256, bonus : Uint256) : Unit := do
emit "Transfer" [amount, add amount bonus]

Expand Down Expand Up @@ -1909,6 +1912,17 @@ def emitDynamicLogModelUsesStmtRawLog : Bool :=

example : emitDynamicLogModelUsesStmtRawLog = true := by native_decide

def eventTraceSpecCarriesEventMetadata : Bool :=
match MacroEventTrace.spec.events with
| [{ name := "Transfer",
params := [
{ name := "amount", ty := ParamType.uint256, kind := EventParamKind.indexed },
{ name := "total", ty := ParamType.uint256, kind := EventParamKind.unindexed }
] }] => true
| _ => false

example : eventTraceSpecCarriesEventMetadata = true := by native_decide

def emitNamedExecutableAppendsNamedEvent : Bool :=
match MacroEventTrace.emitNamed 7 5 Verity.defaultState with
| .success () state =>
Expand Down Expand Up @@ -4082,6 +4096,8 @@ set_option maxRecDepth 4096 in
MacroImmutableSmoke.typedImmutableExecutableReadsConvertedValues
expectTrue "macro emit lowers to Stmt.emit"
MacroEventTraceSmoke.emitNamedModelUsesStmtEmit
expectTrue "macro event declarations populate CompilationModel event metadata"
MacroEventTraceSmoke.eventTraceSpecCarriesEventMetadata
expectTrue "macro rawLog lowers to Stmt.rawLog with dynamic topic expressions"
MacroEventTraceSmoke.emitDynamicLogModelUsesStmtRawLog
expectTrue "macro emit executable path appends the named event trace"
Expand Down
4 changes: 2 additions & 2 deletions Verity/Macro/Elaborate.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set_option hygiene false

@[command_elab verityContractCmd]
def elabVerityContract : CommandElab := fun stx => do
let (contractName, _newtypeDecls, structDecls, adtDecls, fields, errorDecls, constDecls, immutableDecls, externalDecls, ctor, functions, storageNamespace) ← parseContractSyntax stx
let (contractName, _newtypeDecls, structDecls, adtDecls, fields, errorDecls, eventDecls, constDecls, immutableDecls, externalDecls, ctor, functions, storageNamespace) ← parseContractSyntax stx

validateGeneratedDefNamesPublic fields constDecls immutableDecls functions
validateConstantDeclsPublic constDecls
Expand Down Expand Up @@ -48,7 +48,7 @@ def elabVerityContract : CommandElab := fun stx => do
elabCommand cmd
elabCommand (← mkBridgeCommand fn.ident)

elabCommand (← mkSpecCommandPublic (toString contractName.getId) fields errorDecls constDecls immutableDecls externalDecls ctor functions adtDecls storageNamespace)
elabCommand (← mkSpecCommandPublic (toString contractName.getId) fields errorDecls eventDecls constDecls immutableDecls externalDecls ctor functions adtDecls storageNamespace)

let findIdxSimpCmds ← mkFindIdxFieldSimpCommandsPublic contractName fields
for cmd in findIdxSimpCmds do
Expand Down
6 changes: 6 additions & 0 deletions Verity/Macro/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ declare_syntax_cat verityStorageField
declare_syntax_cat verityStructMember
declare_syntax_cat verityParam
declare_syntax_cat verityError
declare_syntax_cat verityEventParam
declare_syntax_cat verityEvent
declare_syntax_cat verityConstant
declare_syntax_cat verityImmutable
declare_syntax_cat verityExternal
Expand All @@ -33,6 +35,9 @@ syntax "MappingStruct(" term "," "[" sepBy(verityStructMember, ",") "]" ")" : te
syntax "MappingStruct2(" term "," term "," "[" sepBy(verityStructMember, ",") "]" ")" : term
syntax ident " : " term : verityParam
syntax "error " ident "(" sepBy(term, ",") ")" : verityError
syntax ident " : " term : verityEventParam
syntax "@indexed " ident " : " term : verityEventParam
syntax "event " ident "(" sepBy(verityEventParam, ",") ")" : verityEvent
syntax ident " : " term:max " := " term:max : verityConstant
syntax ident " : " term:max " := " term:max : verityImmutable
syntax "external " ident "(" sepBy(term, ",") ")" (" -> " "(" sepBy(term, ",") ")")? : verityExternal
Expand Down Expand Up @@ -82,6 +87,7 @@ syntax (name := verityContractCmd)
"storage " verityStorageField*
(verityStructDecl)*
("errors " verityError+)?
("event_defs " verityEvent+)?
("constants " verityConstant+)?
("immutables " verityImmutable+)?
("linked_externals " verityExternal+)?
Expand Down
80 changes: 77 additions & 3 deletions Verity/Macro/Translate.lean
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ structure ErrorDecl where
name : String
params : Array ValueType

structure EventParamDecl where
ident : Ident
name : String
ty : ValueType
isIndexed : Bool

structure EventDecl where
ident : Ident
name : String
params : Array EventParamDecl

structure ConstantDecl where
ident : Ident
name : String
Expand Down Expand Up @@ -788,6 +799,42 @@ private def parseError (newtypes : Array NewtypeDecl) (structDecls : Array Struc
}
| _ => throwErrorAt stx "invalid custom error declaration"

private def parseEventParam
(newtypes : Array NewtypeDecl)
(structDecls : Array StructDecl)
(adtDecls : Array AdtDecl)
(stx : Syntax) : CommandElabM EventParamDecl := do
match stx with
| `(verityEventParam| $name:ident : $ty:term) =>
pure {
ident := name
name := toString name.getId
ty := ← valueTypeFromSyntax newtypes structDecls adtDecls ty
isIndexed := false
}
| `(verityEventParam| @indexed $name:ident : $ty:term) =>
pure {
ident := name
name := toString name.getId
ty := ← valueTypeFromSyntax newtypes structDecls adtDecls ty
isIndexed := true
}
| _ => throwErrorAt stx "invalid event parameter declaration"

private def parseEvent
(newtypes : Array NewtypeDecl)
(structDecls : Array StructDecl)
(adtDecls : Array AdtDecl)
(stx : Syntax) : CommandElabM EventDecl := do
match stx with
| `(verityEvent| event $name:ident ($[$params:verityEventParam],*)) =>
pure {
ident := name
name := toString name.getId
params := ← params.mapM (parseEventParam newtypes structDecls adtDecls)
}
| _ => throwErrorAt stx "invalid event declaration"

private def parseConstant (newtypes : Array NewtypeDecl) (stx : Syntax) : CommandElabM ConstantDecl := do
match stx with
| `(verityConstant| $name:ident : $ty:term := $body:term) =>
Expand Down Expand Up @@ -6321,6 +6368,24 @@ private def mkModelErrorTerm (err : ErrorDecl) : CommandElabM Term := do
$(strTerm err.name)
[ $[$paramTerms],* ])

private def mkModelEventParamTerm (param : EventParamDecl) : CommandElabM Term := do
let tyTerm ← modelParamTypeTerm param.ty
let kindTerm ←
if param.isIndexed then
`(Compiler.CompilationModel.EventParamKind.indexed)
else
`(Compiler.CompilationModel.EventParamKind.unindexed)
`(Compiler.CompilationModel.EventParam.mk
$(strTerm param.name)
$tyTerm
$kindTerm)

private def mkModelEventTerm (ev : EventDecl) : CommandElabM Term := do
let paramTerms ← ev.params.mapM mkModelEventParamTerm
`(Compiler.CompilationModel.EventDef.mk
$(strTerm ev.name)
[ $[$paramTerms],* ])

private def mkModelExternalTerm (ext : ExternalDecl) : CommandElabM Term := do
let paramTerms ← ext.params.mapM modelParamTypeTerm
let returnTerms ← ext.returnTys.mapM modelParamTypeTerm
Expand Down Expand Up @@ -6405,6 +6470,7 @@ private def mkSpecCommand
(contractName : String)
(fields : Array StorageFieldDecl)
(errorDecls : Array ErrorDecl)
(eventDecls : Array EventDecl)
(constDecls : Array ConstantDecl)
(immutableDecls : Array ImmutableDecl)
(externalDecls : Array ExternalDecl)
Expand All @@ -6416,6 +6482,7 @@ private def mkSpecCommand
let allFields := fields ++ immutableFields
let fieldTerms ← allFields.mapM mkModelFieldTerm
let errorTerms ← errorDecls.mapM mkModelErrorTerm
let eventTerms ← eventDecls.mapM mkModelEventTerm
let externalTerms ← externalDecls.mapM mkModelExternalTerm
let constructorTerm ←
match ctor, immutableDecls.isEmpty with
Expand Down Expand Up @@ -6499,6 +6566,7 @@ private def mkSpecCommand
name := $(strTerm contractName)
fields := [ $[$fieldTerms],* ]
«errors» := [ $[$errorTerms],* ]
«events» := [ $[$eventTerms],* ]
«constructor» := $constructorTerm
functions := [ $[$allFunctionTerms],* ]
«externals» := [ $[$externalTerms],* ]
Expand Down Expand Up @@ -6599,9 +6667,9 @@ def computeStorageNamespaceKey (key : String) : Nat :=
def parseContractSyntax
(stx : Syntax)
: CommandElabM
(Ident × Array NewtypeDecl × Array StructDecl × Array AdtDecl × Array StorageFieldDecl × Array ErrorDecl × Array ConstantDecl × Array ImmutableDecl × Array ExternalDecl × Option ConstructorDecl × Array FunctionDecl × Option Nat) := do
(Ident × Array NewtypeDecl × Array StructDecl × Array AdtDecl × Array StorageFieldDecl × Array ErrorDecl × Array EventDecl × Array ConstantDecl × Array ImmutableDecl × Array ExternalDecl × Option ConstructorDecl × Array FunctionDecl × Option Nat) := do
match stx with
| `(command| verity_contract $contractName:ident where $[types $[$newtypeDecls:verityNewtype]*]? $[inductive $[$adtDecls:verityAdtDecl]*]? $[$nsSpec:verityNamespaceSpec]? storage $[$storageFields:verityStorageField]* $[$structDecls:verityStructDecl]* $[errors $[$errorDecls:verityError]*]? $[constants $[$constantDecls:verityConstant]*]? $[immutables $[$immutableDecls:verityImmutable]*]? $[linked_externals $[$externalDecls:verityExternal]*]? $[$ctor:verityConstructor]? $[$entrypoints:veritySpecialEntrypoint]* $[$functions:verityFunction]*) =>
| `(command| verity_contract $contractName:ident where $[types $[$newtypeDecls:verityNewtype]*]? $[inductive $[$adtDecls:verityAdtDecl]*]? $[$nsSpec:verityNamespaceSpec]? storage $[$storageFields:verityStorageField]* $[$structDecls:verityStructDecl]* $[errors $[$errorDecls:verityError]*]? $[event_defs $[$eventDecls:verityEvent]*]? $[constants $[$constantDecls:verityConstant]*]? $[immutables $[$immutableDecls:verityImmutable]*]? $[linked_externals $[$externalDecls:verityExternal]*]? $[$ctor:verityConstructor]? $[$entrypoints:veritySpecialEntrypoint]* $[$functions:verityFunction]*) =>
-- Parse newtypes first — they are needed by all downstream type resolution
let parsedNewtypes ←
match newtypeDecls with
Expand Down Expand Up @@ -6665,6 +6733,10 @@ def parseContractSyntax
match errorDecls with
| some decls => decls.mapM (parseError parsedNewtypes parsedStructs parsedAdts)
| none => pure #[]
let parsedEvents ←
match eventDecls with
| some decls => decls.mapM (parseEvent parsedNewtypes parsedStructs parsedAdts)
| none => pure #[]
let parsedConstants ←
match constantDecls with
| some decls => decls.mapM (parseConstant parsedNewtypes)
Expand All @@ -6691,6 +6763,7 @@ def parseContractSyntax
, parsedAdts
, parsedFields
, parsedErrors
, parsedEvents
, parsedConstants
, parsedImmutables
, parsedExternals
Expand Down Expand Up @@ -7016,14 +7089,15 @@ def mkSpecCommandPublic
(contractName : String)
(fields : Array StorageFieldDecl)
(errorDecls : Array ErrorDecl)
(eventDecls : Array EventDecl)
(constDecls : Array ConstantDecl)
(immutableDecls : Array ImmutableDecl)
(externalDecls : Array ExternalDecl)
(ctor : Option ConstructorDecl)
(functions : Array FunctionDecl)
(adtDecls : Array AdtDecl)
(storageNamespace : Option Nat) : CommandElabM Cmd :=
mkSpecCommand contractName fields errorDecls constDecls immutableDecls externalDecls ctor functions adtDecls storageNamespace
mkSpecCommand contractName fields errorDecls eventDecls constDecls immutableDecls externalDecls ctor functions adtDecls storageNamespace

def mkFindIdxFieldSimpCommandsPublic
(contractIdent : Ident)
Expand Down
Loading