yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
eaa8dcfcc
master
1#include "node-tree.h" 2 3#include "compiler-core/slang-name-convention-util.h" 4#include "core/slang-io.h" 5#include "identifier-lookup.h" 6#include "options.h" 7 8namespace CppParse 9{ 10using namespace Slang ; 11 12/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! NodeTree !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 13 14NodeTree ::NodeTree ( 15StringSlicePool * typePool , 16NamePool * namePool , 17IdentifierLookup * identifierLookup ) 18 :m_typePool (typePool ) 19 ,m_namePool (namePool ) 20 ,m_identifierLookup (identifierLookup ) 21 ,m_typeSetPool (StringSlicePool ::Style ::Empty ) 22{ 23m_rootNode = new ScopeNode (Node ::Kind ::Namespace ); 24m_rootNode -> m_reflectionType = ReflectionType ::Reflected ; 25} 26 27TypeSet * NodeTree ::getTypeSet (const UnownedStringSlice & slice ) 28{ 29Index index = m_typeSetPool .findIndex (slice ); 30if (index < 0 ) 31 { 32return nullptr ; 33 } 34return m_typeSets [index ]; 35} 36 37TypeSet * NodeTree ::getOrAddTypeSet (const UnownedStringSlice & slice ) 38{ 39const Index index = Index (m_typeSetPool .add (slice )); 40if (index >=m_typeSets .getCount ()) 41 { 42SLANG_ASSERT (m_typeSets .getCount ()== index ); 43TypeSet * typeSet = new TypeSet ; 44 45m_typeSets .add (typeSet ); 46typeSet -> m_macroName = m_typeSetPool .getSlice (StringSlicePool ::Handle (index )); 47return typeSet ; 48 } 49else 50 { 51return m_typeSets [index ]; 52 } 53} 54 55SourceOrigin * NodeTree ::addSourceOrigin (SourceFile * sourceFile ,const Options & options ) 56{ 57// Calculate from the path, a 'macro origin' name. 58const String macroOrigin = calcMacroOrigin (sourceFile -> getPathInfo ().foundPath ,options ); 59 60SourceOrigin * origin = new SourceOrigin (sourceFile ,macroOrigin ); 61m_sourceOrigins .add (origin ); 62return origin ; 63} 64 65/* static */ String NodeTree ::calcMacroOrigin (const String & filePath ,const Options & options ) 66{ 67// Get the filename without extension 68String fileName = Path ::getFileNameWithoutExt (filePath ); 69 70// We can work on just the slice 71UnownedStringSlice slice = fileName .getUnownedSlice (); 72 73// Filename prefix 74if (options .m_stripFilePrefix .getLength ()&& 75slice .startsWith (options .m_stripFilePrefix .getUnownedSlice ())) 76 { 77const Index len = options .m_stripFilePrefix .getLength (); 78slice = UnownedStringSlice (slice .begin ()+ len ,slice .end ()); 79 } 80 81// Trim - 82slice = slice .trim ('-' ); 83 84StringBuilder out ; 85NameConventionUtil ::convert (slice ,NameConvention ::UpperSnake ,out ); 86return out ; 87} 88 89SlangResult NodeTree ::_calcDerivedTypesRec (ScopeNode * inScopeNode ,DiagnosticSink * sink ) 90{ 91if (inScopeNode -> isClassLike ()) 92 { 93ClassLikeNode * classLikeNode = static_cast < ClassLikeNode *> (inScopeNode ); 94 95if (classLikeNode -> m_super .hasContent ()) 96 { 97ScopeNode * parentScope = classLikeNode -> m_parentScope ; 98if (parentScope == nullptr ) 99 { 100sink -> diagnoseRaw ( 101Severity ::Error , 102UnownedStringSlice ::fromLiteral ("Can't lookup in scope if there is none!" )); 103return SLANG_FAIL ; 104 } 105 106Node * superNode = Node ::lookup (parentScope ,classLikeNode -> m_super .getContent ()); 107 108if (!superNode ) 109 { 110if (classLikeNode -> isReflected ()) 111 { 112sink -> diagnose ( 113classLikeNode -> m_name , 114CPPDiagnostics ::superTypeNotFound , 115classLikeNode -> getAbsoluteName ()); 116return SLANG_FAIL ; 117 } 118 } 119else 120 { 121ClassLikeNode * superType = as < ClassLikeNode > (superNode ); 122 123if (!superType ) 124 { 125sink -> diagnose ( 126classLikeNode -> m_name , 127CPPDiagnostics ::superTypeNotAType , 128classLikeNode -> getAbsoluteName ()); 129return SLANG_FAIL ; 130 } 131 132if (superType -> m_typeSet != classLikeNode -> m_typeSet ) 133 { 134sink -> diagnose ( 135classLikeNode -> m_name , 136CPPDiagnostics ::typeInDifferentTypeSet , 137classLikeNode -> m_name .getContent (), 138classLikeNode -> m_typeSet -> m_macroName , 139superType -> m_typeSet -> m_macroName ); 140return SLANG_FAIL ; 141 } 142 143// The base class must be defined in same scope (as we didn't allow different scopes 144// for base classes) 145superType -> addDerived (classLikeNode ); 146 } 147 } 148else 149 { 150// Add to it's own typeset 151if (classLikeNode -> isReflected ()&& classLikeNode -> m_typeSet ) 152 { 153classLikeNode -> m_typeSet -> m_baseTypes .add (classLikeNode ); 154 } 155 } 156 } 157 158for (Node * child :inScopeNode -> m_children ) 159 { 160ScopeNode * childScope = as < ScopeNode > (child ); 161if (childScope ) 162 { 163SLANG_RETURN_ON_FAIL (_calcDerivedTypesRec (childScope ,sink )); 164 } 165 } 166 167return SLANG_OK ; 168} 169 170SlangResult NodeTree ::calcDerivedTypes (DiagnosticSink * sink ) 171{ 172return _calcDerivedTypesRec (m_rootNode ,sink ); 173} 174 175 176}// namespace CppParse