1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* -*- c++ -*- */
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#pragma once
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef GLSL_SYMBOL_TABLE
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GLSL_SYMBOL_TABLE
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <new>
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgextern "C" {
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "program/symbol_table.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass symbol_table_entry;
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Facade class for _mesa_symbol_table
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Wraps the existing \c _mesa_symbol_table data structure to enforce some
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * type safe and some symbol table invariants.
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct glsl_symbol_table {
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprivate:
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static void
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _glsl_symbol_table_destructor (glsl_symbol_table *table)
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      table->~glsl_symbol_table();
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Callers of this ralloc-based new need not call delete. It's
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * easier to just ralloc_free 'ctx' (or any of its ancestors). */
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static void* operator new(size_t size, void *ctx)
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      void *table;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      table = ralloc_size(ctx, size);
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(table != NULL);
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_set_destructor(table, (void (*)(void*)) _glsl_symbol_table_destructor);
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return table;
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* If the user *does* call delete, that's OK, we will just
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * ralloc_free in that case. Here, C++ will have already called the
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * destructor so tell ralloc not to do that again. */
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   static void operator delete(void *table)
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_set_destructor(table, NULL);
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_free(table);
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   glsl_symbol_table();
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ~glsl_symbol_table();
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int language_version;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void push_scope();
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void pop_scope();
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /**
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Determine whether a name was declared at the current scope
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool name_declared_this_scope(const char *name);
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /**
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * \name Methods to add symbols to the table
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * There is some temptation to rename all these functions to \c add_symbol
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * or similar.  However, this breaks symmetry with the getter functions and
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * reduces the clarity of the intention of code that uses these methods.
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*@{*/
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool add_variable(ir_variable *v);
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool add_type(const char *name, const glsl_type *t);
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool add_function(ir_function *f);
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool add_uniform_block(struct gl_uniform_block *u);
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*@}*/
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /**
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Add an function at global scope without checking for scoping conflicts.
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void add_global_function(ir_function *f);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /**
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * \name Methods to get symbols from the table
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*@{*/
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_variable *get_variable(const char *name);
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const glsl_type *get_type(const char *name);
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_function *get_function(const char *name);
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*@}*/
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprivate:
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   symbol_table_entry *get_entry(const char *name);
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct _mesa_symbol_table *table;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void *mem_ctx;
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif /* GLSL_SYMBOL_TABLE */
126