Add check to enforce base pragmas.

This commit is contained in:
Jacques Wagener 2019-07-29 17:02:15 +02:00
parent 07536ee4c6
commit 82a8208595
No known key found for this signature in database
GPG Key ID: C294D1025DA0E923
6 changed files with 42 additions and 24 deletions

View File

@ -19,7 +19,7 @@ contract("KingOfTheHill"):
proc BecameKing2(name {.indexed.}: bytes32, value: uint128) {.event.}
# Methods
proc becomeKing*(name: bytes32) {.payable,self,log,msg.} =
proc becomeKing*(name: bytes32) {.payable,self,msg,log.} =
if msg.value > self.king_value:
self.king_name = name
self.king_addr = msg.sender
@ -27,11 +27,11 @@ contract("KingOfTheHill"):
# log.BecameKing(name, msg.value)
log.BecameKing2(name, msg.value)
# proc getKing*(): bytes32 =
# self.king_name
proc getKing*(): bytes32 {.self.} =
self.king_name
# proc getKingAddr*(): address =
# self.king_addr
proc getKingAddr*(): address {.self.} =
self.king_addr
# proc getKingValue*(): wei_value =
# self.king_value
proc getKingValue*(): wei_value {.self.} =
self.king_value

View File

@ -44,11 +44,11 @@ contract("MyContract"):
# proc ret_bytes32*(in_a: bytes32): bytes32 =
# return in_a
proc set_bytes32*(in_a: bytes32) =
proc set_bytes32*(in_a: bytes32) {.self.} =
self.name = in_a
proc get_bytes32*(): bytes32 =
return self.name
proc get_bytes32*(): bytes32 {.self.} =
self.name
# proc get_value*(): uint128 {.payable.} =
# return msg.value

View File

@ -148,16 +148,28 @@ proc generate_defines(keywords: seq[string], global_ctx: GlobalContext): (NimNod
return (stmts, tmp_vars)
proc check_keyword_defines(keywords_used: seq[string], local_ctx: LocalContext) =
for keyword in keywords_used:
var base = keyword.replace("set_", "")
if "." in base:
base = base.split(".")[0]
if not (base in local_ctx.sig.pragma_base_keywords):
raiseParserError(
fmt"Base Keyword {{.{base}.}} needs to be placed in the pragma of function '{local_ctx.sig.name}'.",
local_ctx.sig.line_info
)
proc get_keyword_defines*(proc_def: NimNode, global_ctx: GlobalContext): (NimNode, Table[string, string]) =
var keywords_used: seq[string]
proc get_keyword_defines*(proc_def: NimNode, global_ctx: GlobalContext, local_ctx: LocalContext): (NimNode, Table[string, string]) =
var
keywords_used: seq[string]
find_builtin_keywords(proc_def, keywords_used, global_ctx)
keywords_used = deduplicate(keywords_used)
echo keywords_used
check_keyword_defines(keywords_used, local_ctx)
let (global_define_stmts, global_keyword_map) = generate_defines(keywords_used, global_ctx)
return (global_define_stmts, global_keyword_map)
proc get_next_storage_node(kw_key_name: string, global_keyword_map: Table[string, string], current_node: NimNode): NimNode =
if kw_key_name.startsWith("self."):
return nnkCall.newTree(

View File

@ -1,6 +1,4 @@
import macros
import strutils
import stint
import macros, strutils, stint, strformat
import ./utils, ./types
@ -66,12 +64,18 @@ proc generate_function_signature*(proc_def: NimNode, global_ctx: GlobalContext):
else:
raise newException(Exception, "unknown param type" & treeRepr(child))
of nnkPragma:
if child[0].kind == nnkIdent:
var pragma_name = strVal(child[0])
if not (pragma_name in @["payable", "event", "self", "msg"]):
raiseParserError("Unsupported pragma: " & pragma_name, child)
if pragma_name == "payable":
func_sig.payable = true
for pragma_child in child:
if pragma_child.kind == nnkIdent:
var pragma_name = strVal(pragma_child)
# Add pragma to list of pragmas
if not (pragma_name in ALLOWED_PRAGMAS):
raiseParserError(
fmt"Unsupported pragma: {pragma_name}, must be one of " & ALLOWED_PRAGMAS.join(","),
child
)
func_sig.pragma_base_keywords.add(pragma_name)
if pragma_name == "payable":
func_sig.payable = true
else:
discard
# raise newException(Exception, "unknown func type" & treeRepr(child))

View File

@ -109,7 +109,7 @@ proc generate_context(proc_def: NimNode, global_ctx: GlobalContext): LocalContex
var ctx = LocalContext()
ctx.name = get_func_name(proc_def)
ctx.sig = generate_function_signature(proc_def, global_ctx)
(ctx.keyword_define_stmts, ctx.global_keyword_map) = get_keyword_defines(proc_def, global_ctx)
(ctx.keyword_define_stmts, ctx.global_keyword_map) = get_keyword_defines(proc_def, global_ctx, ctx)
return ctx

View File

@ -39,6 +39,7 @@ type
method_sig*: string
is_private*: bool
line_info*: LineInfo
pragma_base_keywords*: seq[string] # list of pragmas
type
EventSignature* = object
@ -73,3 +74,4 @@ let
KEYWORDS* {.compileTime.} = @["contract", "self", "log"]
TYPE_NAMES* {.compileTime.} = @["address", "uint256", "bytes32", "int128", "uint128"]
ALL_KEYWORDS* {.compileTime.} = concat(TYPE_NAMES, KEYWORDS)
ALLOWED_PRAGMAS* {.compileTime.} = @["payable", "event", "self", "msg", "log"]