Skip to content

Commit

Permalink
Merge pull request #957 from Einzich/master
Browse files Browse the repository at this point in the history
micro-optimizations for das infer
  • Loading branch information
borisbat authored Feb 9, 2024
2 parents 32b7038 + ebe36c3 commit 828a876
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 33 deletions.
6 changes: 6 additions & 0 deletions include/daScript/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,17 @@ namespace das
bool addModule ( Module * module );
void foreach ( const callable<bool (Module * module)> & func, const string & name ) const;
void foreach_in_order ( const callable<bool (Module * module)> & func, Module * thisM ) const;

void findWithCallback ( const string & name, Module * inWhichModule, const callable<void (Module * pm, const string &name, Module * inWhichModule)> & func ) const;
void findAlias ( vector<TypeDeclPtr> & ptr, Module * pm, const string & aliasName, Module * inWhichModule ) const;
vector<TypeDeclPtr> findAlias ( const string & name, Module * inWhichModule ) const;
void findAnnotation ( vector<AnnotationPtr> & ptr, Module * pm, const string & annotationName, Module * inWhichModule ) const;
vector<AnnotationPtr> findAnnotation ( const string & name, Module * inWhichModule ) const;
vector<AnnotationPtr> findStaticAnnotation ( const string & name ) const;
vector<TypeInfoMacroPtr> findTypeInfoMacro ( const string & name, Module * inWhichModule ) const;
void findEnum ( vector<EnumerationPtr> & ptr, Module * pm, const string & enumName, Module * inWhichModule ) const;
vector<EnumerationPtr> findEnum ( const string & name, Module * inWhichModule ) const;
void findStructure ( vector<StructurePtr> & ptr, Module * pm, const string & funcName, Module * inWhichModule ) const;
vector<StructurePtr> findStructure ( const string & name, Module * inWhichModule ) const;
Module * findModule ( const string & name ) const;
TypeDeclPtr makeStructureType ( const string & name ) const;
Expand Down
2 changes: 1 addition & 1 deletion include/daScript/ast/ast_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace das
virtual void aotVisitGetField(TextWriter & ss, const string & fieldName) override;
virtual void aotVisitGetFieldPtr(TextWriter & ss, const string & fieldName) override;
virtual bool canSubstitute(TypeAnnotation * ann) const override;
StructureField & addFieldEx(const string & na, const string & cppNa, off_t offset, TypeDeclPtr pT);
StructureField & addFieldEx(const string & na, const string & cppNa, off_t offset, const TypeDeclPtr & pT);
virtual void walk(DataWalker & walker, void * data) override;
void updateTypeInfo() const;
int32_t fieldCount() const { return int32_t(fields.size()); }
Expand Down
2 changes: 1 addition & 1 deletion include/daScript/ast/ast_typedecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ namespace das {

bool isCircularType ( const TypeDeclPtr & type );
bool hasImplicit ( const TypeDeclPtr & type );
bool isMatchingArgumentType (TypeDeclPtr argType, TypeDeclPtr passType);
bool isMatchingArgumentType ( const TypeDeclPtr & argType, const TypeDeclPtr & passType );

enum class CpptSubstitureRef { no, yes };
enum class CpptSkipRef { no, yes };
Expand Down
16 changes: 12 additions & 4 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2740,10 +2740,18 @@ namespace das {
}

TypeDecl * Program::makeTypeDeclaration(const LineInfo &at, const string &name) {
auto structs = library.findStructure(name,thisModule.get());
auto handles = library.findAnnotation(name,thisModule.get());
auto enums = library.findEnum(name,thisModule.get());
auto aliases = library.findAlias(name,thisModule.get());

das::vector<das::StructurePtr> structs;
das::vector<das::AnnotationPtr> handles;
das::vector<das::EnumerationPtr> enums;
das::vector<das::TypeDeclPtr> aliases;
library.findWithCallback(name, thisModule.get(), [&](Module * pm, const string &name, Module * inWhichModule) {
library.findStructure(structs, pm, name, inWhichModule);
library.findAnnotation(handles, pm, name, inWhichModule);
library.findEnum(enums, pm, name, inWhichModule);
library.findAlias(aliases, pm, name, inWhichModule);
});

if ( ((structs.size()!=0)+(handles.size()!=0)+(enums.size()!=0)+(aliases.size()!=0)) > 1 ) {
string candidates = describeCandidates(structs);
candidates += describeCandidates(handles, false);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ast_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace das {
}
}

BasicStructureAnnotation::StructureField & BasicStructureAnnotation::addFieldEx ( const string & na, const string & cppNa, off_t offset, TypeDeclPtr pT ) {
BasicStructureAnnotation::StructureField & BasicStructureAnnotation::addFieldEx ( const string & na, const string & cppNa, off_t offset, const TypeDeclPtr & pT ) {
auto & field = fields[na];
if ( field.decl ) {
DAS_FATAL_ERROR("structure field %s already exist in structure %s\n", na.c_str(), name.c_str() );
Expand Down
77 changes: 52 additions & 25 deletions src/ast/ast_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,38 +759,57 @@ namespace das {
return it!=modules.end() ? *it : nullptr;
}

void ModuleLibrary::findWithCallback ( const string & name, Module * inWhichModule, const callable<void (Module * pm, const string &name, Module * inWhichModule)> & func ) const {
string moduleName, funcName;
splitTypeName(name, moduleName, funcName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
func(pm, funcName, inWhichModule);
}
return true;
}, moduleName);
}

void ModuleLibrary::findAlias ( vector<TypeDeclPtr> & ptr, Module * pm, const string & aliasName, Module * inWhichModule ) const {
if ( auto pp = pm->findAlias(aliasName) ) {
if ( pp->isEnumT() ) {
if ( !pp->enumType->isPrivate || pp->enumType->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
} else if ( pp->baseType==Type::tStructure ) {
if ( !pp->structType->privateStructure || pp->structType->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
} else {
ptr.push_back(das::move(pp));
}
}
}

vector<TypeDeclPtr> ModuleLibrary::findAlias ( const string & name, Module * inWhichModule ) const {
vector<TypeDeclPtr> ptr;
string moduleName, aliasName;
splitTypeName(name, moduleName, aliasName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) )
if ( auto pp = pm->findAlias(aliasName) ) {
if ( pp->isEnumT() ) {
if ( !pp->enumType->isPrivate || pp->enumType->module==inWhichModule ) {
ptr.push_back(pp);
}
} else if ( pp->baseType==Type::tStructure ) {
if ( !pp->structType->privateStructure || pp->structType->module==inWhichModule ) {
ptr.push_back(pp);
}
} else {
ptr.push_back(pp);
}
}
findAlias(ptr, pm, aliasName, inWhichModule);
return true;
}, moduleName);
return ptr;
}

void ModuleLibrary::findAnnotation ( vector<AnnotationPtr> & ptr, Module * pm, const string & annotationName, Module * ) const {
if ( auto pp = pm->findAnnotation(annotationName) )
ptr.push_back(das::move(pp));
}

vector<AnnotationPtr> ModuleLibrary::findAnnotation ( const string & name, Module * inWhichModule ) const {
vector<AnnotationPtr> ptr;
string moduleName, annName;
splitTypeName(name, moduleName, annName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) )
if ( auto pp = pm->findAnnotation(annName) )
ptr.push_back(pp);
findAnnotation(ptr, pm, annName, inWhichModule);
return true;
}, moduleName);
return ptr;
Expand All @@ -809,34 +828,42 @@ namespace das {
return ptr;
}

void ModuleLibrary::findStructure ( vector<StructurePtr> & ptr, Module * pm, const string & structName, Module * inWhichModule ) const {
if ( auto pp = pm->findStructure(structName) ) {
if ( !pp->privateStructure || pp->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
}
}

vector<StructurePtr> ModuleLibrary::findStructure ( const string & name, Module * inWhichModule ) const {
vector<StructurePtr> ptr;
string moduleName, funcName;
splitTypeName(name, moduleName, funcName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
if ( auto pp = pm->findStructure(funcName) ) {
if ( !pp->privateStructure || pp->module==inWhichModule ) {
ptr.push_back(pp);
}
}
findStructure(ptr, pm, funcName, inWhichModule);
}
return true;
}, moduleName);
return ptr;
}

void ModuleLibrary::findEnum ( vector<EnumerationPtr> & ptr, Module * pm, const string & enumName, Module * inWhichModule ) const {
if ( auto pp = pm->findEnum(enumName) ) {
if ( !pp->isPrivate || pp->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
}
}

vector<EnumerationPtr> ModuleLibrary::findEnum ( const string & name, Module * inWhichModule ) const {
vector<EnumerationPtr> ptr;
string moduleName, enumName;
splitTypeName(name, moduleName, enumName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
if ( auto pp = pm->findEnum(enumName) ) {
if ( !pp->isPrivate || pp->module==inWhichModule ) {
ptr.push_back(pp);
}
}
findEnum(ptr, pm, enumName, inWhichModule);
}
return true;
}, moduleName);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ast_typedecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3136,7 +3136,7 @@ namespace das
return false;
}

bool isMatchingArgumentType (TypeDeclPtr argType, TypeDeclPtr passType) {
bool isMatchingArgumentType ( const TypeDeclPtr & argType, const TypeDeclPtr & passType ) {
if (!passType) {
return false;
}
Expand Down

0 comments on commit 828a876

Please sign in to comment.