diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-05-08 14:31:40 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-08 14:31:40 -0400 |
| commit | 798f3bc2236ce81499b05662dc11e7c071e7cde8 (patch) | |
| tree | fbfa2ef90d4cfdace9ce3667cf441e22137792fb /source/slang/slang-syntax.cpp | |
| parent | c16abd4fe1bda5ebcd50dbb22f30c6be43bb885f (diff) | |
AST nodes using C++ Extractor (#1341)
* Extractor builds without any reference to syntax (as it will be helping to produce this!).
* Change macros to include the super class.
* WIP replacing defs files.
* Added indexOf(const UnownedSubString& in) to UnownedSubString.
Refactored extractor
* Output a macro for each type with the extracted info - can be used during injection in class
* Simplify the header file - as can get super type and last from macro now
* Store the 'origin' of a definition
* Some small tidy ups to the extractor.
* Improve comments on the extractor options.
* Made CPPExtractor own SourceOrigins
* Small fixes around SourceOrigin.
* Small tidy up around macroOrign
* WIP Visitor seems now to work correctly.
Split out types used by ast into slang-ast-support-types.h
* Fix remaining problems with C++ extractor being used with AST nodes.
Add CountOf to extractor type ids.
Added ReflectClassInfo::getInfo to turn an ASTNodeType into a ReflectClassInfo
* Fix compiling on linux.
Fix typo in memset.
* Small tidy up around comments/layout.
Moved NodeBase casting to NodeBase.
* Make premake generate project that builds with cpp-extractor for AST.
* Get the source directory from the filter in premake.
* Fix typo in source path
* Explicitly set the source path for premake generation for AST.
* Special case handling of override to apease Clang.
* Use a more general way to find the slang-ast-reflect.h file to run the extractor.
* Appveyor is not triggering slang-cpp-extractor - try putting dependson together.
* Put building slang-cpp-extractor first.
* Disable some project options to stop MSBuild producing internal compiler errors.
* Try reordering the projects in premake5.lua
* Hack to try and make slang-cpp-extractor built on appveyor.
* Disable flags - not required for MSBuild on appveyor.
* Disable flags not required for build on AppVeyor.
* Updated Visual Studio projects with slang-cpp-extractor.
* Added Visual Studio slang-cpp-extractor project.
Diffstat (limited to 'source/slang/slang-syntax.cpp')
| -rw-r--r-- | source/slang/slang-syntax.cpp | 196 |
1 files changed, 19 insertions, 177 deletions
diff --git a/source/slang/slang-syntax.cpp b/source/slang/slang-syntax.cpp index 2055c2422..cb75622bc 100644 --- a/source/slang/slang-syntax.cpp +++ b/source/slang/slang-syntax.cpp @@ -8,182 +8,11 @@ namespace Slang { - // We want this first, before the kClassInfo variables so it is constructed before anything else. - /* static*/ SyntaxClassBase::ClassInfo* SyntaxClassBase::ClassInfo::s_first = nullptr; - // BasicExpressionType - - bool BasicExpressionType::EqualsImpl(Type * type) - { - auto basicType = as<BasicExpressionType>(type); - return basicType && basicType->baseType == this->baseType; - } - - RefPtr<Type> BasicExpressionType::CreateCanonicalType() - { - // A basic type is already canonical, in our setup - return this; - } - - // Generate dispatch logic and other definitions for all syntax classes -#define SYNTAX_CLASS(NAME, BASE) /* empty */ -#include "slang-object-meta-begin.h" - -#include "slang-syntax-base-defs.h" -#undef SYNTAX_CLASS -#undef ABSTRACT_SYNTAX_CLASS - -#define ABSTRACT_SYNTAX_CLASS(NAME, BASE) \ - template<> \ - SyntaxClassBase::ClassInfo const SyntaxClassBase::Impl<NAME>::kClassInfo(#NAME, nullptr, &SyntaxClassBase::Impl<BASE>::kClassInfo); - -#define SYNTAX_CLASS(NAME, BASE) \ - template<> \ - void* SyntaxClassBase::Impl<NAME>::createFunc() { return new NAME(); } \ - template<> \ - SyntaxClassBase::ClassInfo const SyntaxClassBase::Impl<NAME>::kClassInfo( #NAME, &SyntaxClassBase::Impl<NAME>::createFunc, &SyntaxClassBase::Impl<BASE>::kClassInfo); \ - void NAME::accept(NAME::Visitor* visitor, void* extra) \ - { visitor->dispatch_##NAME(this, extra); } \ - const SyntaxClassBase::ClassInfo& NAME::getClassInfo() const { return SyntaxClassBase::Impl<NAME>::kClassInfo; } - -template<> -SyntaxClassBase::ClassInfo const SyntaxClassBase::Impl<RefObject>::kClassInfo("RefObject", nullptr, nullptr); - -ABSTRACT_SYNTAX_CLASS(NodeBase, RefObject); -ABSTRACT_SYNTAX_CLASS(SyntaxNodeBase, NodeBase); -ABSTRACT_SYNTAX_CLASS(SyntaxNode, SyntaxNodeBase); -ABSTRACT_SYNTAX_CLASS(ModifiableSyntaxNode, SyntaxNode); -ABSTRACT_SYNTAX_CLASS(DeclBase, ModifiableSyntaxNode); -ABSTRACT_SYNTAX_CLASS(Decl, DeclBase); -ABSTRACT_SYNTAX_CLASS(Stmt, ModifiableSyntaxNode); -ABSTRACT_SYNTAX_CLASS(Val, NodeBase); -ABSTRACT_SYNTAX_CLASS(Type, Val); -ABSTRACT_SYNTAX_CLASS(Modifier, SyntaxNodeBase); -ABSTRACT_SYNTAX_CLASS(Expr, SyntaxNode); - -ABSTRACT_SYNTAX_CLASS(Substitutions, SyntaxNode); -ABSTRACT_SYNTAX_CLASS(GenericSubstitution, Substitutions); -ABSTRACT_SYNTAX_CLASS(ThisTypeSubstitution, Substitutions); -ABSTRACT_SYNTAX_CLASS(GlobalGenericParamSubstitution, Substitutions); - -#include "slang-expr-defs.h" -#include "slang-decl-defs.h" -#include "slang-modifier-defs.h" -#include "slang-stmt-defs.h" -#include "slang-type-defs.h" -#include "slang-val-defs.h" -#include "slang-object-meta-end.h" - - -SyntaxClassBase::ClassInfo::ClassInfo(const char* name, CreateFunc createFunc, const ClassInfo* superClass): - m_name(name), - m_createFunc(createFunc), - m_superClass(superClass), - m_next(s_first) -{ - m_classId = 0; - m_childrenEndClassId = 0; - - s_first = this; -} - - - -static uint32_t _calcRangeRec(const SyntaxClassBase::ClassInfo* classInfo, const Dictionary<const SyntaxClassBase::ClassInfo*, List<const SyntaxClassBase::ClassInfo*> >& childMap, uint32_t index) -{ - classInfo->m_classId = index++; - // Do the calc range for all the children - auto list = childMap.TryGetValue(classInfo); +#define SLANG_CLASS_ACCEPT_IMPL(NAME, SUPER, ORIGIN, LAST, MARKER, TYPE, param) \ + void NAME::accept(NAME::Visitor* visitor, void* extra) { visitor->dispatch_##NAME(this, extra); } - if (list) - { - for (auto child : *list) - { - index = _calcRangeRec(child, childMap, index); - } - } - - classInfo->m_childrenEndClassId = index; - return index; -} - -bool SyntaxClassBase::ClassInfo::isSubClassOfSlow(const ThisType& super) const -{ - SyntaxClassBase::ClassInfo const* info = this; - while (info) - { - if (info == &super) - return true; - info = info->m_superClass; - } - return false; -} - -static bool _checkSubClassRange() -{ - typedef SyntaxClassBase::ClassInfo ClassInfo; - - List<const ClassInfo*> list; - for (const ClassInfo* type = ClassInfo::s_first; type; type = type->m_next) - { - list.add(type); - } - - for (Index i = 0; i < list.getCount(); ++i) - { - for (Index j = 0; j < list.getCount(); ++j) - { - auto a = list[i]; - auto b = list[j]; - if (a->isSubClassOf(*b) != a->isSubClassOfSlow(*b)) - { - return false; - } - } - } - - return true; -} - - -/* static */SlangResult SyntaxClassBase::ClassInfo::initRanges() -{ - // Remove the warning about not referenced - SLANG_UNUSED(&_checkSubClassRange); - - // TODO(JS): - // Note that the calculating of the ranges could be done more efficiently by adding to an array of struct { super, class }, sorting, by super classs - // and using a dictionary to map from class it's first in list of super class use. This works for now though. - - // We want to produce a map from a node that holds all of it's children - Dictionary<const ThisType*, List<const ThisType*> > childMap; - - const List<const ThisType*> emptyList; - - { - for (const ThisType* type = s_first; type; type = type->m_next) - { - if (type->m_superClass) - { - // Add to that item - List<const ThisType*>* list = childMap.TryGetValueOrAdd(type->m_superClass, emptyList); - if (!list) - { - list = childMap.TryGetValue(type->m_superClass); - } - SLANG_ASSERT(list); - list->add(type); - } - } - } - - // We want to recursively work out a range - _calcRangeRec(&SyntaxClassBase::Impl<RefObject>::kClassInfo, childMap, 1); - - SLANG_ASSERT(_checkSubClassRange()); - - return SLANG_OK; -} +SLANG_ALL_ASTNode_NodeBase(SLANG_CLASS_ONLY, SLANG_CLASS_ACCEPT_IMPL) // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! DiagnosticSink impls !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -225,10 +54,23 @@ SourceLoc const& getDiagnosticPos(TypeExp const& typeExp) return typeExp.exp->loc; } +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! BasicExpressionType !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +bool BasicExpressionType::EqualsImpl(Type * type) +{ + auto basicType = as<BasicExpressionType>(type); + return basicType && basicType->baseType == this->baseType; +} + +RefPtr<Type> BasicExpressionType::CreateCanonicalType() +{ + // A basic type is already canonical, in our setup + return this; +} // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Free functions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -const RefPtr<Decl>* adjustFilterCursorImpl(const SyntaxClassBase::ClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end) +const RefPtr<Decl>* adjustFilterCursorImpl(const ReflectClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end) { switch (filterStyle) { @@ -273,7 +115,7 @@ const RefPtr<Decl>* adjustFilterCursorImpl(const SyntaxClassBase::ClassInfo& cls return end; } -const RefPtr<Decl>* getFilterCursorByIndexImpl(const SyntaxClassBase::ClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end, Index index) +const RefPtr<Decl>* getFilterCursorByIndexImpl(const ReflectClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end, Index index) { switch (filterStyle) { @@ -330,7 +172,7 @@ const RefPtr<Decl>* getFilterCursorByIndexImpl(const SyntaxClassBase::ClassInfo& return nullptr; } -Index getFilterCountImpl(const SyntaxClassBase::ClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end) +Index getFilterCountImpl(const ReflectClassInfo& clsInfo, MemberFilterStyle filterStyle, const RefPtr<Decl>* ptr, const RefPtr<Decl>* end) { Index count = 0; switch (filterStyle) |
