summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ast-synthesis.h
blob: 7613a6fae73cc41358299d4d4cde6e1682d42988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// slang-ast-synthesis.h

#pragma once

#include "slang-syntax.h"

namespace Slang
{

struct ASTEmitScope
{
    ContainerDecl* m_parent = nullptr;
    SeqStmt* m_parentSeqStmt = nullptr;
    Scope* m_scope = nullptr;
};
class ASTSynthesizer
{
private:
    ASTBuilder* m_builder;
    NamePool* m_namePool;
    List<ASTEmitScope> m_scopeStack;

public:
    ASTSynthesizer(ASTBuilder* builder, NamePool* namePool)
        : m_builder(builder), m_namePool(namePool)
    {
    }

    ASTBuilder* getBuilder() { return m_builder; }

    Scope* getScope(ContainerDecl* decl)
    {
        for (auto container = decl; container; container = container->parentDecl)
        {
            if (container->ownedScope)
            {
                return container->ownedScope;
            }
        }
        return nullptr;
    }

    // Create a scope for `decl` and push it to scope stack
    void pushScopeForContainer(ContainerDecl* decl)
    {
        if (decl->ownedScope)
        {
            // if decl already owns a scope, don't create a new one.
            pushContainerScope(decl);
            return;
        }

        auto parentScope = getScope(decl);
        decl->ownedScope = m_builder->create<Scope>();
        decl->ownedScope->parent = parentScope;
        decl->ownedScope->containerDecl = decl;
        pushContainerScope(decl);
    }

    // Push `decl` and its associated scope to scope stack
    void pushContainerScope(ContainerDecl* decl)
    {
        ASTEmitScope scope = getCurrentScope();
        scope.m_parent = decl;
        scope.m_scope = getScope(decl);
        m_scopeStack.add(scope);
    }

    Scope* pushVarScope()
    {
        ASTEmitScope scope = getCurrentScope();
        auto scopeDecl = m_builder->create<ScopeDecl>();
        auto newScope = m_builder->create<Scope>();

        if (scope.m_parent)
            scope.m_parent->addDirectMemberDecl(scopeDecl);

        newScope->parent = scope.m_scope;
        newScope->containerDecl = scopeDecl;
        scope.m_scope = newScope;
        m_scopeStack.add(scope);
        return newScope;
    }

    void _addStmtToScope(Stmt* stmt)
    {
        auto scope = getCurrentScope();
        if (scope.m_parentSeqStmt)
        {
            scope.m_parentSeqStmt->stmts.add(stmt);
        }
    }

    SeqStmt* pushSeqStmtScope()
    {
        ASTEmitScope scope = getCurrentScope();
        scope.m_parentSeqStmt = m_builder->create<SeqStmt>();
        m_scopeStack.add(scope);
        return scope.m_parentSeqStmt;
    }

    void popScope() { m_scopeStack.removeLast(); }

    ASTEmitScope getCurrentScope()
    {
        if (m_scopeStack.getCount())
            return m_scopeStack.getLast();
        return ASTEmitScope();
    }

    ForStmt* emitFor(Expr* initVal, Expr* finalVal, VarDecl*& outIndexVar);

    Expr* emitBinaryExpr(UnownedStringSlice operatorToken, Expr* left, Expr* right);

    Expr* emitPrefixExpr(UnownedStringSlice operatorToken, Expr* base);

    Expr* emitPostfixExpr(UnownedStringSlice operatorToken, Expr* base);

    Expr* emitThisExpr();
    Expr* emitVarExpr(Name* name);
    Expr* emitVarExpr(VarDeclBase* var);
    Expr* emitVarExpr(VarDeclBase* var, Type* type);
    Expr* emitVarExpr(DeclStmt* varStmt, Type* type);
    Expr* emitStaticTypeExpr(Type* type);

    Expr* emitIntConst(int value);

    Expr* emitGetArrayLengthExpr(Expr* arrayExpr);

    Expr* emitMemberExpr(Expr* base, Name* name);
    Expr* emitMemberExpr(Type* base, Name* name);
    Expr* emitMemberExpr(QualType exprType, Expr* base, DeclRef<Decl> declRef);

    Expr* emitIndexExpr(Expr* base, Expr* index);

    Expr* emitAssignExpr(Expr* left, Expr* right);
    ExpressionStmt* emitAssignStmt(Expr* left, Expr* right)
    {
        return emitExprStmt(emitAssignExpr(left, right));
    }

    Expr* emitInvokeExpr(Expr* callee, List<Expr*>&& args);
    Expr* emitCtorInvokeExpr(Expr* callee, List<Expr*>&& args);

    Expr* emitGenericAppExpr(Expr* genericExpr, List<Expr*>&& args);

    DeclStmt* emitVarDeclStmt(Type* type, Name* name = nullptr, Expr* initVal = nullptr);

    ExpressionStmt* emitExprStmt(Expr* expr);

    ReturnStmt* emitReturnStmt(Expr* expr);
};

} // namespace Slang