Mana
SemanticAnalyzer.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "../runner/common/Setup.h"
10 #include "Symbol.h"
11 #include "SymbolFactory.h"
12 #include "SyntaxNode.h"
13 #include "TypeDescriptor.h"
14 
15 namespace mana
16 {
17  class StringPool;
18 
20  {
21  public:
22  explicit SemanticAnalyzer(
23  const std::shared_ptr<SymbolFactory>& symbolFactory,
24  const std::shared_ptr<TypeDescriptorFactory>& typeDescriptorFactory,
25  const std::shared_ptr<StringPool>& stringPool);
26 
27  virtual ~SemanticAnalyzer() = default;
28 
29  static void SetCurrentFileInformation(const std::shared_ptr<SyntaxNode>& node);
30 
35  bool SearchSymbolFromName(const std::shared_ptr<SyntaxNode>& node);
36 
41  void ResolveTypeDescription(const std::shared_ptr<SyntaxNode>& node);
42 
48  std::shared_ptr<TypeDescriptor> ResolveVariableSize(const std::shared_ptr<SyntaxNode>& node);
49 
56  void ResolveDeclarator(const std::shared_ptr<SyntaxNode>& node, const bool isStaticVariable);
57 
65  void ResolveVariableDescription(const std::shared_ptr<SyntaxNode>& node, const Symbol::MemoryTypeId memoryTypeId, const bool isStaticVariable);
66 
71  static void ResolveTypeFromChildNode(const std::shared_ptr<SyntaxNode>& node);
72 
73  protected:
74  struct ConstValue
75  {
76  std::shared_ptr<TypeDescriptor> type;
77  int32_t intValue = 0;
78  float floatValue = 0.0f;
79  std::string_view stringValue;
80  };
81 
82  [[nodiscard]] std::shared_ptr<Symbol> Lookup(const std::string_view name) const;
83  bool EvaluateConstExpression(const std::shared_ptr<SyntaxNode>& node, ConstValue& value);
84  bool IsConstTypeCompatible(const std::shared_ptr<TypeDescriptor>& declaredType, const std::shared_ptr<TypeDescriptor>& expressionType) const;
85  std::shared_ptr<Symbol> CreateConstSymbol(const std::string_view name, const std::shared_ptr<TypeDescriptor>& declaredType, const ConstValue& value);
86 
87  const std::shared_ptr<SymbolFactory>& GetSymbolFactory() const;
88  const std::shared_ptr<StringPool>& GetStringPool() const;
89  const std::shared_ptr<TypeDescriptorFactory>& GetTypeDescriptorFactory() const;
90 
91  private:
92  std::shared_ptr<SymbolFactory> mSymbolFactory;
93  std::shared_ptr<StringPool> mStringPool;
94  std::shared_ptr<TypeDescriptorFactory> mTypeDescriptorFactory;
95  };
96 }
Definition: SemanticAnalyzer.h:20
std::shared_ptr< Symbol > CreateConstSymbol(const std::string_view name, const std::shared_ptr< TypeDescriptor > &declaredType, const ConstValue &value)
Definition: SemanticAnalyzer.cpp:512
const std::shared_ptr< TypeDescriptorFactory > & GetTypeDescriptorFactory() const
Definition: SemanticAnalyzer.cpp:565
SemanticAnalyzer(const std::shared_ptr< SymbolFactory > &symbolFactory, const std::shared_ptr< TypeDescriptorFactory > &typeDescriptorFactory, const std::shared_ptr< StringPool > &stringPool)
Definition: SemanticAnalyzer.cpp:15
static void SetCurrentFileInformation(const std::shared_ptr< SyntaxNode > &node)
Definition: SemanticAnalyzer.cpp:25
void ResolveTypeDescription(const std::shared_ptr< SyntaxNode > &node)
Definition: SemanticAnalyzer.cpp:55
void ResolveDeclarator(const std::shared_ptr< SyntaxNode > &node, const bool isStaticVariable)
Definition: SemanticAnalyzer.cpp:74
std::shared_ptr< Symbol > Lookup(const std::string_view name) const
Definition: SemanticAnalyzer.cpp:177
static void ResolveTypeFromChildNode(const std::shared_ptr< SyntaxNode > &node)
Definition: SemanticAnalyzer.cpp:149
bool EvaluateConstExpression(const std::shared_ptr< SyntaxNode > &node, ConstValue &value)
Definition: SemanticAnalyzer.cpp:182
const std::shared_ptr< StringPool > & GetStringPool() const
Definition: SemanticAnalyzer.cpp:560
bool SearchSymbolFromName(const std::shared_ptr< SyntaxNode > &node)
Definition: SemanticAnalyzer.cpp:32
void ResolveVariableDescription(const std::shared_ptr< SyntaxNode > &node, const Symbol::MemoryTypeId memoryTypeId, const bool isStaticVariable)
Definition: SemanticAnalyzer.cpp:136
std::shared_ptr< TypeDescriptor > ResolveVariableSize(const std::shared_ptr< SyntaxNode > &node)
Definition: SemanticAnalyzer.cpp:94
virtual ~SemanticAnalyzer()=default
bool IsConstTypeCompatible(const std::shared_ptr< TypeDescriptor > &declaredType, const std::shared_ptr< TypeDescriptor > &expressionType) const
Definition: SemanticAnalyzer.cpp:500
const std::shared_ptr< SymbolFactory > & GetSymbolFactory() const
Definition: SemanticAnalyzer.cpp:555
MemoryTypeId
Definition: Symbol.h:62
Definition: CodeBuffer.cpp:12
Definition: Noncopyable.h:18
Definition: SemanticAnalyzer.h:75
float floatValue
Definition: SemanticAnalyzer.h:78
std::shared_ptr< TypeDescriptor > type
Definition: SemanticAnalyzer.h:76
std::string_view stringValue
Definition: SemanticAnalyzer.h:79
int32_t intValue
Definition: SemanticAnalyzer.h:77