1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/sksl/DSLSymbols.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/sksl/SkSLCompiler.h"
11cb93a386Sopenharmony_ci#include "src/sksl/SkSLThreadContext.h"
12cb93a386Sopenharmony_ci#include "src/sksl/dsl/priv/DSLWriter.h"
13cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLVariable.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cinamespace SkSL {
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_cinamespace dsl {
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cistatic bool is_type_in_symbol_table(skstd::string_view name, SkSL::SymbolTable* symbols) {
20cb93a386Sopenharmony_ci    const SkSL::Symbol* s = (*symbols)[name];
21cb93a386Sopenharmony_ci    return s && s->is<Type>();
22cb93a386Sopenharmony_ci}
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_civoid PushSymbolTable() {
25cb93a386Sopenharmony_ci    SymbolTable::Push(&ThreadContext::SymbolTable());
26cb93a386Sopenharmony_ci}
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_civoid PopSymbolTable() {
29cb93a386Sopenharmony_ci    SymbolTable::Pop(&ThreadContext::SymbolTable());
30cb93a386Sopenharmony_ci}
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_cistd::shared_ptr<SymbolTable> CurrentSymbolTable() {
33cb93a386Sopenharmony_ci    return ThreadContext::SymbolTable();
34cb93a386Sopenharmony_ci}
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ciDSLPossibleExpression Symbol(skstd::string_view name, PositionInfo pos) {
37cb93a386Sopenharmony_ci    return ThreadContext::Compiler().convertIdentifier(pos.line(), name);
38cb93a386Sopenharmony_ci}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_cibool IsType(skstd::string_view name) {
41cb93a386Sopenharmony_ci    return is_type_in_symbol_table(name, CurrentSymbolTable().get());
42cb93a386Sopenharmony_ci}
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_cibool IsBuiltinType(skstd::string_view name) {
45cb93a386Sopenharmony_ci    return is_type_in_symbol_table(name, CurrentSymbolTable()->builtinParent());
46cb93a386Sopenharmony_ci}
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_civoid AddToSymbolTable(DSLVarBase& var, PositionInfo pos) {
49cb93a386Sopenharmony_ci    const SkSL::Variable* skslVar = DSLWriter::Var(var);
50cb93a386Sopenharmony_ci    if (skslVar) {
51cb93a386Sopenharmony_ci        CurrentSymbolTable()->addWithoutOwnership(skslVar);
52cb93a386Sopenharmony_ci    }
53cb93a386Sopenharmony_ci    ThreadContext::ReportErrors(pos);
54cb93a386Sopenharmony_ci}
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ciconst String* Retain(String string) {
57cb93a386Sopenharmony_ci    return CurrentSymbolTable()->takeOwnershipOfString(std::move(string));
58cb93a386Sopenharmony_ci}
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci} // namespace dsl
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci} // namespace SkSL
63