Skip to content

Commit

Permalink
support_thandle_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Aug 31, 2023
1 parent 654cf58 commit 644a370
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
13 changes: 13 additions & 0 deletions daslib/debug_eval.das
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ def getVectorOffset ( var st:TokenStream; ident:string; var result:Result ) : tu
]]
return [[auto ofs*fieldSize,tinfo]]

def getHandleFieldOffset ( var st:TokenStream; ident:string; var result:Result ) : tuple<int;TypeInfo>
var ann <- unsafe(reinterpret<smart_ptr<TypeAnnotation>> result.tinfo.annotation)
if ann == null
return [[auto -1, [[TypeInfo()]]]]
let ofs = int(get_handled_type_field_offset(ann, ident))
if ofs == -1
return [[auto -1, [[TypeInfo()]]]]
let tinfo = get_handled_type_field_type(ann, ident)
print("tinfo [{ident}] is {tinfo}\n")
return [[auto ofs, *tinfo]]

def expr_field ( var st:TokenStream ) : Result
var result = expr_value(st)
let token = token(st)
Expand All @@ -257,6 +268,8 @@ def expr_field ( var st:TokenStream ) : Result
oti = getTupleFieldOffset(st, field as ident, result)
if [[TypeInfo basicType=$v(bt)]] && (getVectorElementCount(bt)!=0)
oti = getVectorOffset(st, field as ident, result)
if [[TypeInfo basicType=Type tHandle]]
oti = getHandleFieldOffset(st, field as ident, result)
if oti.offset < 0
return st |> error("unknown field {field} in type {result.tinfo.basicType}")
let newData = getPD(st, result,oti.offset)
Expand Down
5 changes: 5 additions & 0 deletions examples/test/misc/test_debug_eval.das
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require daslib/debug_eval
require UnitTest

def print ( result:Result )
if !empty(result.error)
Expand Down Expand Up @@ -45,6 +46,7 @@ def main
var temp_float4 = float4(1,2,3,4)
var temp_tuple = [[auto 3, 0.14f]]
var temp_tab <- {{1 => "one"; 2 => "two"; 3 => "three"}}
var temp_foo = [[TestObjectFoo() fooData=13]]
unsafe
context["temp_int"] = [[Result data = addr(temp_int), tinfo = typeinfo(rtti_typeinfo temp_int)]]
context["temp_str"] = [[Result data = addr(temp_str), tinfo = typeinfo(rtti_typeinfo temp_str)]]
Expand All @@ -54,7 +56,9 @@ def main
context["temp_float4"] = [[Result data = addr(temp_float4), tinfo = typeinfo(rtti_typeinfo temp_float4)]]
context["temp_tuple"] = [[Result data = addr(temp_tuple), tinfo = typeinfo(rtti_typeinfo temp_tuple)]]
context["temp_tab"] = [[Result data = addr(temp_tab), tinfo = typeinfo(rtti_typeinfo temp_tab)]]
context["temp_foo"] = [[Result data = addr(temp_foo), tinfo = typeinfo(rtti_typeinfo temp_foo)]]

context |> debug_eval("temp_foo.fooData") |> print
context |> debug_eval("temp_tab") |> print
context |> debug_eval("temp_tab[1]") |> print
context |> debug_eval("temp_tab[2]") |> print
Expand Down Expand Up @@ -114,6 +118,7 @@ def main
verify(context|>eval_test("123)))")=="unexpected token 41")
verify(context|>eval_test("foo+1")=="unknown variable foo")
verify(context|>eval_test("0x12+0x1f")=="49")
verify(context|>eval_test("temp_foo.fooData+1")=="14")



1 change: 1 addition & 0 deletions include/daScript/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ namespace das
virtual size_t getSizeOf() const { return sizeof(void *); }
virtual size_t getAlignOf() const { return 1; }
virtual uint32_t getFieldOffset ( const string & ) const { return -1U; }
virtual TypeInfo * getFieldType ( const string & ) const { return nullptr; }
virtual TypeDeclPtr makeValueType() const { return nullptr; }
virtual TypeDeclPtr makeFieldType ( const string &, bool ) const { return nullptr; }
virtual TypeDeclPtr makeSafeFieldType ( const string &, bool ) const { return nullptr; }
Expand Down
8 changes: 5 additions & 3 deletions include/daScript/ast/ast_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace das
virtual bool isRefType() const override { return true; }
virtual int32_t getGcFlags(das_set<Structure *> &, das_set<Annotation *> &) const override;
virtual uint32_t getFieldOffset ( const string & ) const override;
virtual TypeInfo * getFieldType ( const string & ) const override;
virtual TypeDeclPtr makeFieldType(const string & na, bool isConst) const override;
virtual TypeDeclPtr makeSafeFieldType(const string & na, bool isConst) const override;
virtual void aotPreVisitGetField ( TextWriter &, const string & ) override;
Expand All @@ -99,17 +100,18 @@ namespace das
virtual bool canSubstitute(TypeAnnotation * ann) const override;
StructureField & addFieldEx(const string & na, const string & cppNa, off_t offset, TypeDeclPtr pT);
virtual void walk(DataWalker & walker, void * data) override;
void updateTypeInfo() const;
int32_t fieldCount() const { return int32_t(fields.size()); }
void from(BasicStructureAnnotation * ann);
void from(const char* parentName);
das_map<string,StructureField> fields;
vector<string> fieldsInOrder;
DebugInfoHelper helpA;
StructInfo * sti = nullptr;
mutable DebugInfoHelper helpA;
mutable StructInfo * sti = nullptr;
ModuleLibrary * mlib = nullptr;
vector<TypeAnnotation*> parents;
bool validationNeverFails = false;
recursive_mutex walkMutex;
mutable recursive_mutex walkMutex;
};

template <typename TT, bool canCopy = isCloneable<TT>::value>
Expand Down
1 change: 1 addition & 0 deletions include/daScript/simulate/aot_builtin_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ namespace das {
void addModuleOption ( Module * mod, char * option, Type type, Context * context, LineInfoArg * at );
TypeDeclPtr getUnderlyingValueType ( smart_ptr_raw<TypeDecl> type, Context * context, LineInfoArg * at );
uint32_t getHandledTypeFieldOffset ( smart_ptr_raw<TypeAnnotation> type, char * name, Context * context, LineInfoArg * at );
TypeInfo * getHandledTypeFieldType ( smart_ptr_raw<TypeAnnotation> annotation, char * name, Context * context, LineInfoArg * at );
void addModuleRequrie ( Module * module, Module * reqModule, bool publ );

template <>
Expand Down
61 changes: 39 additions & 22 deletions src/ast/ast_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ namespace das {
return gcf;
}

TypeInfo * BasicStructureAnnotation::getFieldType ( const string & na ) const {
updateTypeInfo();
for ( uint32_t n=0; n!=sti->count; ++n ) {
auto & fi = sti->fields[n];
if ( fi->name == na ) {
return fi;
}
}
return nullptr;
}

uint32_t BasicStructureAnnotation::getFieldOffset ( const string & na ) const {
auto it = fields.find(na);
if ( it!=fields.end() ) {
Expand Down Expand Up @@ -126,29 +137,30 @@ namespace das {
return field;
}

void BasicStructureAnnotation::walk ( DataWalker & walker, void * data ) {
{
lock_guard<recursive_mutex> guard(walkMutex);
if ( !sti ) {
auto debugInfo = helpA.debugInfo;
sti = debugInfo->template makeNode<StructInfo>();
sti->name = debugInfo->allocateName(name);
// flags
sti->flags = 0;
// count fields
sti->count = 0;
for ( auto & fi : fields ) {
auto & var = fi.second;
if ( var.offset != -1U ) {
sti->count ++;
}
void BasicStructureAnnotation::updateTypeInfo() const {
lock_guard<recursive_mutex> guard(walkMutex);
if ( !sti ) {
auto debugInfo = helpA.debugInfo;
sti = debugInfo->template makeNode<StructInfo>();
sti->name = debugInfo->allocateName(name);
// flags
sti->flags = 0;
// count fields
sti->count = 0;
for ( auto & fi : fields ) {
auto & var = fi.second;
if ( var.offset != -1U ) {
sti->count ++;
}
// and allocate
sti->size = (uint32_t) getSizeOf();
sti->fields = (VarInfo **) debugInfo->allocate( sizeof(VarInfo *) * sti->count );
int i = 0;
for ( const auto & fn : fieldsInOrder ) {
auto & var = fields[fn];
}
// and allocate
sti->size = (uint32_t) getSizeOf();
sti->fields = (VarInfo **) debugInfo->allocate( sizeof(VarInfo *) * sti->count );
int i = 0;
for ( const auto & fn : fieldsInOrder ) {
auto itvar = fields.find(fn);
if ( itvar != fields.end() ) {
auto & var = itvar->second;
if ( var.offset != -1U ) {
VarInfo * vi = debugInfo->template makeNode<VarInfo>();
helpA.makeTypeInfo(vi, var.decl);
Expand All @@ -159,6 +171,11 @@ namespace das {
}
}
}

}

void BasicStructureAnnotation::walk ( DataWalker & walker, void * data ) {
updateTypeInfo();
walker.walk_struct((char *)data, sti);
}

Expand Down
9 changes: 9 additions & 0 deletions src/builtin/module_builtin_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ namespace das {
return type->annotation->makeValueType();
}

TypeInfo * getHandledTypeFieldType ( smart_ptr_raw<TypeAnnotation> annotation, char * name, Context * context, LineInfoArg * at ) {
if ( !name ) context->throw_error_at(at, "expecting field name");
if ( !annotation ) context->throw_error_at(at, "expecting type annotation");
return annotation->getFieldType(name);
}

uint32_t getHandledTypeFieldOffset ( smart_ptr_raw<TypeAnnotation> annotation, char * name, Context * context, LineInfoArg * at ) {
if ( !name ) context->throw_error_at(at, "expecting field name");
if ( !annotation ) context->throw_error_at(at, "expecting type annotation");
Expand Down Expand Up @@ -731,6 +737,9 @@ namespace das {
addExtern<DAS_BIND_FUN(getHandledTypeFieldOffset)>(*this, lib, "get_handled_type_field_offset",
SideEffects::none, "getHandledTypeFieldOffset")
->args({"type","field","context","line"});
addExtern<DAS_BIND_FUN(getHandledTypeFieldType)>(*this, lib, "get_handled_type_field_type",
SideEffects::none, "getHandledTypeFieldType")
->args({"type","field","context","line"});
// module
addExtern<DAS_BIND_FUN(for_each_typedef)>(*this, lib, "for_each_typedef",
SideEffects::modifyExternal, "for_each_typedef")
Expand Down

0 comments on commit 644a370

Please sign in to comment.