Commit d4a9b8dd by Nicolas Capens Committed by Shannon Woods

Detect redefinition of functions and variables.

TRAC #21840 Signed-off-by: Jamie Madill Signed-off-by: Shannon Woods Author: Nicolas Capens
parent 1b45214a
...@@ -1008,8 +1008,9 @@ const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* ...@@ -1008,8 +1008,9 @@ const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction*
{ {
// First find by unmangled name to check whether the function name has been // First find by unmangled name to check whether the function name has been
// hidden by a variable name or struct typename. // hidden by a variable name or struct typename.
// If a function is found, check for one with a matching argument list.
const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn); const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
if (symbol == 0) { if (symbol == 0 || symbol->isFunction()) {
symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn); symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
} }
......
...@@ -197,7 +197,7 @@ public: ...@@ -197,7 +197,7 @@ public:
TSymbolTableLevel() { } TSymbolTableLevel() { }
~TSymbolTableLevel(); ~TSymbolTableLevel();
bool insert(TSymbol &symbol) bool insert(const TString &name, TSymbol &symbol)
{ {
symbol.setUniqueId(++uniqueId); symbol.setUniqueId(++uniqueId);
...@@ -205,11 +205,16 @@ public: ...@@ -205,11 +205,16 @@ public:
// returning true means symbol was added to the table // returning true means symbol was added to the table
// //
tInsertResult result; tInsertResult result;
result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); result = level.insert(tLevelPair(name, &symbol));
return result.second; return result.second;
} }
bool insert(TSymbol &symbol)
{
return insert(symbol.getMangledName(), symbol);
}
TSymbol* find(const TString& name) const TSymbol* find(const TString& name) const
{ {
tLevel::const_iterator it = level.find(name); tLevel::const_iterator it = level.find(name);
......
...@@ -859,6 +859,24 @@ function_prototype ...@@ -859,6 +859,24 @@ function_prototype
} }
// //
// Check for previously declared variables using the same name.
//
TSymbol *prevSym = context->symbolTable.find($1->getName(), context->shaderVersion);
if (prevSym)
{
if (!prevSym->isFunction())
{
context->error(@2, "redefinition", $1->getName().c_str(), "function");
context->recover();
}
}
else
{
// Insert the unmangled name to detect potential future redefinition as a variable.
context->symbolTable.getOuterLevel()->insert($1->getName(), *$1);
}
//
// If this is a redeclaration, it could also be a definition, // If this is a redeclaration, it could also be a definition,
// in which case, we want to use the variable names from this one, and not the one that's // in which case, we want to use the variable names from this one, and not the one that's
// being redeclared. So, pass back up this declaration, not the one in the symbol table. // being redeclared. So, pass back up this declaration, not the one in the symbol table.
......
/* A Bison parser, made by GNU Bison 2.7.12-4996. */ /* A Bison parser, made by GNU Bison 2.7. */
/* Bison implementation for Yacc-like parsers in C /* Bison implementation for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc. Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
#define YYBISON 1 #define YYBISON 1
/* Bison version. */ /* Bison version. */
#define YYBISON_VERSION "2.7.12-4996" #define YYBISON_VERSION "2.7"
/* Skeleton name. */ /* Skeleton name. */
#define YYSKELETON_NAME "yacc.c" #define YYSKELETON_NAME "yacc.c"
...@@ -448,14 +448,6 @@ typedef short int yytype_int16; ...@@ -448,14 +448,6 @@ typedef short int yytype_int16;
# endif # endif
#endif #endif
#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later. */
# if (! defined __GNUC__ || __GNUC__ < 2 \
|| (__GNUC__ == 2 && __GNUC_MINOR__ < 5))
# define __attribute__(Spec) /* empty */
# endif
#endif
/* Suppress unused-variable warnings by "using" E. */ /* Suppress unused-variable warnings by "using" E. */
#if ! defined lint || defined __GNUC__ #if ! defined lint || defined __GNUC__
# define YYUSE(E) ((void) (E)) # define YYUSE(E) ((void) (E))
...@@ -463,7 +455,6 @@ typedef short int yytype_int16; ...@@ -463,7 +455,6 @@ typedef short int yytype_int16;
# define YYUSE(E) /* empty */ # define YYUSE(E) /* empty */
#endif #endif
/* Identity function, used to suppress warnings about constant conditions. */ /* Identity function, used to suppress warnings about constant conditions. */
#ifndef lint #ifndef lint
# define YYID(N) (N) # define YYID(N) (N)
...@@ -798,23 +789,23 @@ static const yytype_uint16 yyrline[] = ...@@ -798,23 +789,23 @@ static const yytype_uint16 yyrline[] =
629, 639, 652, 656, 660, 664, 665, 678, 679, 692, 629, 639, 652, 656, 660, 664, 665, 678, 679, 692,
693, 706, 707, 724, 725, 738, 739, 740, 741, 742, 693, 706, 707, 724, 725, 738, 739, 740, 741, 742,
746, 749, 760, 768, 776, 803, 808, 819, 823, 827, 746, 749, 760, 768, 776, 803, 808, 819, 823, 827,
831, 838, 875, 878, 885, 893, 914, 935, 945, 973, 831, 838, 893, 896, 903, 911, 932, 953, 963, 991,
978, 988, 993, 1003, 1006, 1009, 1012, 1018, 1025, 1028, 996, 1006, 1011, 1021, 1024, 1027, 1030, 1036, 1043, 1046,
1032, 1036, 1040, 1047, 1051, 1055, 1062, 1066, 1070, 1091, 1050, 1054, 1058, 1065, 1069, 1073, 1080, 1084, 1088, 1109,
1100, 1106, 1109, 1115, 1121, 1128, 1137, 1146, 1149, 1185, 1118, 1124, 1127, 1133, 1139, 1146, 1155, 1164, 1167, 1203,
1192, 1196, 1203, 1206, 1210, 1214, 1223, 1232, 1240, 1250, 1210, 1214, 1221, 1224, 1228, 1232, 1241, 1250, 1258, 1268,
1257, 1260, 1263, 1269, 1276, 1279, 1285, 1288, 1291, 1297, 1275, 1278, 1281, 1287, 1294, 1297, 1303, 1306, 1309, 1315,
1300, 1315, 1319, 1323, 1327, 1331, 1335, 1340, 1345, 1350, 1318, 1333, 1337, 1341, 1345, 1349, 1353, 1358, 1363, 1368,
1355, 1360, 1365, 1370, 1375, 1380, 1385, 1390, 1395, 1400, 1373, 1378, 1383, 1388, 1393, 1398, 1403, 1408, 1413, 1418,
1405, 1410, 1415, 1420, 1425, 1430, 1435, 1440, 1444, 1448, 1423, 1428, 1433, 1438, 1443, 1448, 1453, 1458, 1462, 1466,
1452, 1456, 1460, 1464, 1468, 1472, 1476, 1480, 1484, 1488, 1470, 1474, 1478, 1482, 1486, 1490, 1494, 1498, 1502, 1506,
1496, 1504, 1508, 1521, 1521, 1524, 1524, 1530, 1533, 1549, 1514, 1522, 1526, 1539, 1539, 1542, 1542, 1548, 1551, 1567,
1552, 1561, 1565, 1571, 1578, 1593, 1597, 1601, 1602, 1608, 1570, 1579, 1583, 1589, 1596, 1611, 1615, 1619, 1620, 1626,
1609, 1610, 1611, 1612, 1616, 1617, 1617, 1617, 1627, 1628, 1627, 1628, 1629, 1630, 1634, 1635, 1635, 1635, 1645, 1646,
1632, 1632, 1633, 1633, 1638, 1641, 1651, 1654, 1660, 1661, 1650, 1650, 1651, 1651, 1656, 1659, 1669, 1672, 1678, 1679,
1665, 1673, 1677, 1687, 1692, 1709, 1709, 1714, 1714, 1721, 1683, 1691, 1695, 1705, 1710, 1727, 1727, 1732, 1732, 1739,
1721, 1729, 1732, 1738, 1741, 1747, 1751, 1758, 1765, 1772, 1739, 1747, 1750, 1756, 1759, 1765, 1769, 1776, 1783, 1790,
1779, 1790, 1799, 1803, 1810, 1813, 1819, 1819 1797, 1808, 1817, 1821, 1828, 1831, 1837, 1837
}; };
#endif #endif
...@@ -1798,7 +1789,11 @@ yy_symbol_value_print (yyoutput, yytype, yyvaluep, yylocationp, context) ...@@ -1798,7 +1789,11 @@ yy_symbol_value_print (yyoutput, yytype, yyvaluep, yylocationp, context)
# else # else
YYUSE (yyoutput); YYUSE (yyoutput);
# endif # endif
YYUSE (yytype); switch (yytype)
{
default:
break;
}
} }
...@@ -2198,7 +2193,12 @@ yydestruct (yymsg, yytype, yyvaluep, yylocationp, context) ...@@ -2198,7 +2193,12 @@ yydestruct (yymsg, yytype, yyvaluep, yylocationp, context)
yymsg = "Deleting"; yymsg = "Deleting";
YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
YYUSE (yytype); switch (yytype)
{
default:
break;
}
} }
...@@ -3387,6 +3387,24 @@ yyreduce: ...@@ -3387,6 +3387,24 @@ yyreduce:
} }
// //
// Check for previously declared variables using the same name.
//
TSymbol *prevSym = context->symbolTable.find((yyvsp[(1) - (2)].interm.function)->getName(), context->shaderVersion);
if (prevSym)
{
if (!prevSym->isFunction())
{
context->error((yylsp[(2) - (2)]), "redefinition", (yyvsp[(1) - (2)].interm.function)->getName().c_str(), "function");
context->recover();
}
}
else
{
// Insert the unmangled name to detect potential future redefinition as a variable.
context->symbolTable.getOuterLevel()->insert((yyvsp[(1) - (2)].interm.function)->getName(), *(yyvsp[(1) - (2)].interm.function));
}
//
// If this is a redeclaration, it could also be a definition, // If this is a redeclaration, it could also be a definition,
// in which case, we want to use the variable names from this one, and not the one that's // in which case, we want to use the variable names from this one, and not the one that's
// being redeclared. So, pass back up this declaration, not the one in the symbol table. // being redeclared. So, pass back up this declaration, not the one in the symbol table.
......
/* A Bison parser, made by GNU Bison 2.7.12-4996. */ /* A Bison parser, made by GNU Bison 2.7. */
/* Bison interface for Yacc-like parsers in C /* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc. Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment