glsl: Use a single shared namespace in the symbol table.
As of 1.20, variable names, function names, and structure type names all share a single namespace, and should conflict with one another in the same scope, or hide each other in nested scopes. However, in 1.10, variables and functions can share the same name in the same scope. Structure types, however, conflict with/hide both. Fixes piglit tests redeclaration-06.vert, redeclaration-11.vert, redeclaration-19.vert, and struct-05.vert.
This commit is contained in:

committed by
Ian Romanick

parent
86ddb356e8
commit
e9c7ceed27
@@ -28,6 +28,7 @@ CXX_SOURCES = \
|
||||
glsl_parser.cpp \
|
||||
glsl_parser_extras.cpp \
|
||||
glsl_types.cpp \
|
||||
glsl_symbol_table.cpp \
|
||||
hir_field_selection.cpp \
|
||||
ir_algebraic.cpp \
|
||||
ir_basic_block.cpp \
|
||||
|
@@ -2630,18 +2630,10 @@ ast_struct_specifier::hir(exec_list *instructions,
|
||||
glsl_type::get_record_instance(fields, decl_count, name);
|
||||
|
||||
YYLTYPE loc = this->get_location();
|
||||
if (!state->symbols->add_type(name, t)) {
|
||||
ir_function *ctor = t->generate_constructor();
|
||||
if (!state->symbols->add_type(name, t, ctor)) {
|
||||
_mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
|
||||
} else {
|
||||
/* This logic is a bit tricky. It is an error to declare a structure at
|
||||
* global scope if there is also a function with the same name.
|
||||
*/
|
||||
if ((state->current_function == NULL)
|
||||
&& (state->symbols->get_function(name) != NULL)) {
|
||||
_mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
|
||||
} else {
|
||||
t->generate_constructor(state->symbols);
|
||||
}
|
||||
|
||||
const glsl_type **s = (const glsl_type **)
|
||||
realloc(state->user_structures,
|
||||
|
@@ -40,6 +40,7 @@ read_builtins(GLenum target, const char *protos, const char **functions, unsigne
|
||||
new(sh) _mesa_glsl_parse_state(NULL, target, sh);
|
||||
|
||||
st->language_version = 130;
|
||||
st->symbols->language_version = 130;
|
||||
st->ARB_texture_rectangle_enable = true;
|
||||
st->EXT_texture_array_enable = true;
|
||||
_mesa_glsl_initialize_types(st);
|
||||
|
@@ -133,6 +133,7 @@ read_builtins(GLenum target, const char *protos, const char **functions, unsigne
|
||||
new(sh) _mesa_glsl_parse_state(NULL, target, sh);
|
||||
|
||||
st->language_version = 130;
|
||||
st->symbols->language_version = 130;
|
||||
st->ARB_texture_rectangle_enable = true;
|
||||
st->EXT_texture_array_enable = true;
|
||||
_mesa_glsl_initialize_types(st);
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,9 @@
|
||||
|
||||
/* A Bison parser, made by GNU Bison 2.4.1. */
|
||||
/* A Bison parser, made by GNU Bison 2.4.3. */
|
||||
|
||||
/* Skeleton interface for Bison's Yacc-like parsers in C
|
||||
|
||||
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
|
||||
Free Software Foundation, Inc.
|
||||
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
||||
2009, 2010 Free Software Foundation, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -233,7 +232,7 @@
|
||||
typedef union YYSTYPE
|
||||
{
|
||||
|
||||
/* Line 1676 of yacc.c */
|
||||
/* Line 1685 of yacc.c */
|
||||
#line 52 "glsl_parser.ypp"
|
||||
|
||||
int n;
|
||||
@@ -264,8 +263,8 @@ typedef union YYSTYPE
|
||||
|
||||
|
||||
|
||||
/* Line 1676 of yacc.c */
|
||||
#line 269 "glsl_parser.h"
|
||||
/* Line 1685 of yacc.c */
|
||||
#line 268 "glsl_parser.h"
|
||||
} YYSTYPE;
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
|
||||
|
@@ -216,6 +216,7 @@ version_statement:
|
||||
/* blank - no #version specified */
|
||||
{
|
||||
state->language_version = 110;
|
||||
state->symbols->language_version = 110;
|
||||
}
|
||||
| VERSION INTCONSTANT EOL
|
||||
{
|
||||
@@ -225,6 +226,7 @@ version_statement:
|
||||
case 130:
|
||||
/* FINISHME: Check against implementation support versions. */
|
||||
state->language_version = $2;
|
||||
state->symbols->language_version = $2;
|
||||
break;
|
||||
default:
|
||||
_mesa_glsl_error(& @2, state, "Shading language version"
|
||||
|
160
src/glsl/glsl_symbol_table.cpp
Normal file
160
src/glsl/glsl_symbol_table.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/* -*- c++ -*- */
|
||||
/*
|
||||
* Copyright © 2010 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "glsl_symbol_table.h"
|
||||
|
||||
class symbol_table_entry {
|
||||
public:
|
||||
/* Callers of this talloc-based new need not call delete. It's
|
||||
* easier to just talloc_free 'ctx' (or any of its ancestors). */
|
||||
static void* operator new(size_t size, void *ctx)
|
||||
{
|
||||
void *entry = talloc_size(ctx, size);
|
||||
assert(entry != NULL);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/* If the user *does* call delete, that's OK, we will just
|
||||
* talloc_free in that case. Here, C++ will have already called the
|
||||
* destructor so tell talloc not to do that again. */
|
||||
static void operator delete(void *table)
|
||||
{
|
||||
talloc_set_destructor(table, NULL);
|
||||
talloc_free(table);
|
||||
}
|
||||
|
||||
symbol_table_entry(ir_variable *v) : v(v), f(0), t(0) {}
|
||||
symbol_table_entry(ir_function *f) : v(0), f(f), t(0) {}
|
||||
symbol_table_entry(const glsl_type *t, ir_function *f) : v(0), f(f), t(t) {}
|
||||
|
||||
ir_variable *v;
|
||||
ir_function *f;
|
||||
const glsl_type *t;
|
||||
};
|
||||
|
||||
glsl_symbol_table::glsl_symbol_table()
|
||||
{
|
||||
this->language_version = 120;
|
||||
this->table = _mesa_symbol_table_ctor();
|
||||
this->mem_ctx = talloc_init("symbol table entries");
|
||||
}
|
||||
|
||||
glsl_symbol_table::~glsl_symbol_table()
|
||||
{
|
||||
_mesa_symbol_table_dtor(table);
|
||||
talloc_free(mem_ctx);
|
||||
}
|
||||
|
||||
void glsl_symbol_table::push_scope()
|
||||
{
|
||||
_mesa_symbol_table_push_scope(table);
|
||||
}
|
||||
|
||||
void glsl_symbol_table::pop_scope()
|
||||
{
|
||||
_mesa_symbol_table_pop_scope(table);
|
||||
}
|
||||
|
||||
bool glsl_symbol_table::name_declared_this_scope(const char *name)
|
||||
{
|
||||
return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
|
||||
}
|
||||
|
||||
bool glsl_symbol_table::add_variable(const char *name, ir_variable *v)
|
||||
{
|
||||
if (this->language_version == 110) {
|
||||
/* In 1.10, functions and variables have separate namespaces. */
|
||||
symbol_table_entry *existing = get_entry(name);
|
||||
if (name_declared_this_scope(name)) {
|
||||
/* If there's already an existing function (not a constructor!) in
|
||||
* the current scope, just update the existing entry to include 'v'.
|
||||
*/
|
||||
if (existing->v == NULL && existing->t == NULL) {
|
||||
existing->v = v;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
/* If not declared at this scope, add a new entry. But if an existing
|
||||
* entry includes a function, propagate that to this block - otherwise
|
||||
* the new variable declaration would shadow the function.
|
||||
*/
|
||||
symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
|
||||
if (existing != NULL)
|
||||
entry->f = existing->f;
|
||||
int added = _mesa_symbol_table_add_symbol(table, -1, name, entry);
|
||||
assert(added == 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 1.20+ rules: */
|
||||
symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
|
||||
return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
|
||||
}
|
||||
|
||||
bool glsl_symbol_table::add_type(const char *name, const glsl_type *t,
|
||||
ir_function *constructor)
|
||||
{
|
||||
symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(t, constructor);
|
||||
return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
|
||||
}
|
||||
|
||||
bool glsl_symbol_table::add_function(const char *name, ir_function *f)
|
||||
{
|
||||
if (this->language_version == 110 && name_declared_this_scope(name)) {
|
||||
/* In 1.10, functions and variables have separate namespaces. */
|
||||
symbol_table_entry *existing = get_entry(name);
|
||||
if (existing->f == NULL) {
|
||||
existing->f = f;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(f);
|
||||
return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
|
||||
}
|
||||
|
||||
ir_variable *glsl_symbol_table::get_variable(const char *name)
|
||||
{
|
||||
symbol_table_entry *entry = get_entry(name);
|
||||
return entry != NULL ? entry->v : NULL;
|
||||
}
|
||||
|
||||
const glsl_type *glsl_symbol_table::get_type(const char *name)
|
||||
{
|
||||
symbol_table_entry *entry = get_entry(name);
|
||||
return entry != NULL ? entry->t : NULL;
|
||||
}
|
||||
|
||||
ir_function *glsl_symbol_table::get_function(const char *name)
|
||||
{
|
||||
symbol_table_entry *entry = get_entry(name);
|
||||
return entry != NULL ? entry->f : NULL;
|
||||
}
|
||||
|
||||
symbol_table_entry *glsl_symbol_table::get_entry(const char *name)
|
||||
{
|
||||
return (symbol_table_entry *)
|
||||
_mesa_symbol_table_find_symbol(table, -1, name);
|
||||
}
|
@@ -34,6 +34,8 @@ extern "C" {
|
||||
#include "ir.h"
|
||||
#include "glsl_types.h"
|
||||
|
||||
class symbol_table_entry;
|
||||
|
||||
/**
|
||||
* Facade class for _mesa_symbol_table
|
||||
*
|
||||
@@ -42,12 +44,6 @@ extern "C" {
|
||||
*/
|
||||
struct glsl_symbol_table {
|
||||
private:
|
||||
enum glsl_symbol_name_space {
|
||||
glsl_variable_name_space = 0,
|
||||
glsl_type_name_space = 1,
|
||||
glsl_function_name_space = 2
|
||||
};
|
||||
|
||||
static int
|
||||
_glsl_symbol_table_destructor (glsl_symbol_table *table)
|
||||
{
|
||||
@@ -80,33 +76,18 @@ public:
|
||||
talloc_free(table);
|
||||
}
|
||||
|
||||
glsl_symbol_table()
|
||||
{
|
||||
table = _mesa_symbol_table_ctor();
|
||||
}
|
||||
glsl_symbol_table();
|
||||
~glsl_symbol_table();
|
||||
|
||||
~glsl_symbol_table()
|
||||
{
|
||||
_mesa_symbol_table_dtor(table);
|
||||
}
|
||||
unsigned int language_version;
|
||||
|
||||
void push_scope()
|
||||
{
|
||||
_mesa_symbol_table_push_scope(table);
|
||||
}
|
||||
|
||||
void pop_scope()
|
||||
{
|
||||
_mesa_symbol_table_pop_scope(table);
|
||||
}
|
||||
void push_scope();
|
||||
void pop_scope();
|
||||
|
||||
/**
|
||||
* Determine whether a name was declared at the current scope
|
||||
*/
|
||||
bool name_declared_this_scope(const char *name)
|
||||
{
|
||||
return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
|
||||
}
|
||||
bool name_declared_this_scope(const char *name);
|
||||
|
||||
/**
|
||||
* \name Methods to add symbols to the table
|
||||
@@ -116,56 +97,26 @@ public:
|
||||
* reduces the clarity of the intention of code that uses these methods.
|
||||
*/
|
||||
/*@{*/
|
||||
bool add_variable(const char *name, ir_variable *v)
|
||||
{
|
||||
return _mesa_symbol_table_add_symbol(table, glsl_variable_name_space,
|
||||
name, v) == 0;
|
||||
}
|
||||
|
||||
bool add_type(const char *name, const glsl_type *t)
|
||||
{
|
||||
return _mesa_symbol_table_add_symbol(table, glsl_type_name_space,
|
||||
name, (void *) t) == 0;
|
||||
}
|
||||
|
||||
bool add_function(const char *name, ir_function *f)
|
||||
{
|
||||
return _mesa_symbol_table_add_symbol(table, glsl_function_name_space,
|
||||
name, f) == 0;
|
||||
}
|
||||
|
||||
bool remove_function(const char *name, ir_function *f)
|
||||
{
|
||||
return _mesa_symbol_table_add_symbol(table, glsl_function_name_space,
|
||||
name, f) == 0;
|
||||
}
|
||||
bool add_variable(const char *name, ir_variable *v);
|
||||
bool add_type(const char *name, const glsl_type *t,
|
||||
ir_function *constructor = NULL);
|
||||
bool add_function(const char *name, ir_function *f);
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* \name Methods to get symbols from the table
|
||||
*/
|
||||
/*@{*/
|
||||
ir_variable *get_variable(const char *name)
|
||||
{
|
||||
return (ir_variable *)
|
||||
_mesa_symbol_table_find_symbol(table, glsl_variable_name_space, name);
|
||||
}
|
||||
|
||||
glsl_type *get_type(const char *name)
|
||||
{
|
||||
return (glsl_type *)
|
||||
_mesa_symbol_table_find_symbol(table, glsl_type_name_space, name);
|
||||
}
|
||||
|
||||
ir_function *get_function(const char *name)
|
||||
{
|
||||
return (ir_function *)
|
||||
_mesa_symbol_table_find_symbol(table, glsl_function_name_space, name);
|
||||
}
|
||||
ir_variable *get_variable(const char *name);
|
||||
const glsl_type *get_type(const char *name);
|
||||
ir_function *get_function(const char *name);
|
||||
/*@}*/
|
||||
|
||||
private:
|
||||
symbol_table_entry *get_entry(const char *name);
|
||||
|
||||
struct _mesa_symbol_table *table;
|
||||
void *mem_ctx;
|
||||
};
|
||||
|
||||
#endif /* GLSL_SYMBOL_TABLE */
|
||||
|
@@ -233,18 +233,11 @@ _mesa_glsl_release_types(void)
|
||||
|
||||
|
||||
ir_function *
|
||||
glsl_type::generate_constructor(glsl_symbol_table *symtab) const
|
||||
glsl_type::generate_constructor() const
|
||||
{
|
||||
void *ctx = symtab;
|
||||
void *ctx = (void *) this;
|
||||
|
||||
/* Generate the function name and add it to the symbol table.
|
||||
*/
|
||||
ir_function *const f = new(ctx) ir_function(name);
|
||||
|
||||
bool added = symtab->add_function(name, f);
|
||||
assert(added);
|
||||
(void) added;
|
||||
|
||||
ir_function_signature *const sig = new(ctx) ir_function_signature(this);
|
||||
f->add_signature(sig);
|
||||
|
||||
|
@@ -208,9 +208,9 @@ struct glsl_type {
|
||||
unsigned num_fields,
|
||||
const char *name);
|
||||
/**
|
||||
* Generate the constructor for this type and add it to the symbol table
|
||||
* Generate the constructor for this type and return it
|
||||
*/
|
||||
class ir_function *generate_constructor(glsl_symbol_table *) const;
|
||||
class ir_function *generate_constructor() const;
|
||||
|
||||
/**
|
||||
* Query the total number of scalars that make up a scalar, vector or matrix
|
||||
|
Reference in New Issue
Block a user