2016-10-08 06:35:16 +00:00
|
|
|
/*
|
|
|
|
** zcc_compile.cpp
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright -2016 Randy Heit
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
#include "dobject.h"
|
|
|
|
#include "sc_man.h"
|
|
|
|
#include "c_console.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "m_alloc.h"
|
|
|
|
#include "zcc_parser.h"
|
|
|
|
#include "zcc_compile.h"
|
|
|
|
#include "v_text.h"
|
|
|
|
#include "gdtoa.h"
|
|
|
|
|
2013-10-26 03:01:18 +00:00
|
|
|
#define DEFINING_CONST ((PSymbolConst *)(void *)1)
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols)
|
2016-03-13 03:22:29 +00:00
|
|
|
: Outer(_outer), Symbols(&_symbols), AST(ast), ErrorCount(0), WarnCount(0)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
|
|
|
// Group top-level nodes by type
|
|
|
|
if (ast.TopNode != NULL)
|
|
|
|
{
|
|
|
|
ZCC_TreeNode *node = ast.TopNode;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
switch (node->NodeType)
|
|
|
|
{
|
2013-10-26 03:01:18 +00:00
|
|
|
case AST_Class:
|
|
|
|
case AST_Struct:
|
2016-03-13 03:22:29 +00:00
|
|
|
case AST_ConstantDef:
|
|
|
|
if (AddNamedNode(static_cast<ZCC_NamedNode *>(node)))
|
2013-10-26 03:01:18 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
switch (node->NodeType)
|
|
|
|
{
|
|
|
|
case AST_Class: Classes.Push(static_cast<ZCC_Class *>(node)); break;
|
|
|
|
case AST_Struct: Structs.Push(static_cast<ZCC_Struct *>(node)); break;
|
|
|
|
case AST_ConstantDef: Constants.Push(static_cast<ZCC_ConstantDef *>(node)); break;
|
2016-03-20 16:34:41 +00:00
|
|
|
default: assert(0 && "Default case is just here to make GCC happy. It should never be reached");
|
2016-03-13 03:22:29 +00:00
|
|
|
}
|
2013-10-26 03:01:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
case AST_Enum: break;
|
|
|
|
case AST_EnumTerminator:break;
|
2013-10-26 03:01:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0 && "Unhandled AST node type");
|
|
|
|
break;
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
node = node->SiblingNext;
|
|
|
|
}
|
|
|
|
while (node != ast.TopNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 03:01:18 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: AddNamedNode
|
|
|
|
//
|
|
|
|
// Keeps track of definition nodes by their names. Ensures that all names
|
|
|
|
// in this scope are unique.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
bool ZCCCompiler::AddNamedNode(ZCC_NamedNode *node)
|
2013-10-26 03:01:18 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
FName name = node->NodeName;
|
|
|
|
PSymbol *check = Symbols->FindSymbol(name, false);
|
|
|
|
if (check != NULL)
|
2013-10-26 03:01:18 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
assert(check->IsA(RUNTIME_CLASS(PSymbolTreeNode)));
|
|
|
|
Error(node, "Attempt to redefine '%s'", name.GetChars());
|
|
|
|
Error(static_cast<PSymbolTreeNode *>(check)->Node, " Original definition is here");
|
2013-10-26 03:01:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Symbols->AddSymbol(new PSymbolTreeNode(name, node));
|
2013-10-26 03:01:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-03-13 03:22:29 +00:00
|
|
|
// ZCCCompiler :: Warn
|
2013-10-24 04:06:32 +00:00
|
|
|
//
|
2016-03-13 03:22:29 +00:00
|
|
|
// Prints a warning message, and increments WarnCount.
|
2013-10-24 04:06:32 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
void ZCCCompiler::Warn(ZCC_TreeNode *node, const char *msg, ...)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, msg);
|
|
|
|
MessageV(node, TEXTCOLOR_ORANGE, msg, argptr);
|
|
|
|
va_end(argptr);
|
2013-10-24 04:06:32 +00:00
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
WarnCount++;
|
|
|
|
}
|
2013-10-24 04:06:32 +00:00
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: Error
|
|
|
|
//
|
|
|
|
// Prints an error message, and increments ErrorCount.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void ZCCCompiler::Error(ZCC_TreeNode *node, const char *msg, ...)
|
|
|
|
{
|
2013-10-24 04:06:32 +00:00
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, msg);
|
2016-03-13 03:22:29 +00:00
|
|
|
MessageV(node, TEXTCOLOR_RED, msg, argptr);
|
2013-10-24 04:06:32 +00:00
|
|
|
va_end(argptr);
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
ErrorCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: MessageV
|
|
|
|
//
|
|
|
|
// Prints a message, annotated with the source location for the tree node.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void ZCCCompiler::MessageV(ZCC_TreeNode *node, const char *txtcolor, const char *msg, va_list argptr)
|
|
|
|
{
|
|
|
|
FString composed;
|
|
|
|
|
|
|
|
composed.Format("%s%s, line %d: ", txtcolor, node->SourceName->GetChars(), node->SourceLoc);
|
|
|
|
composed.VAppendFormat(msg, argptr);
|
2013-10-24 04:06:32 +00:00
|
|
|
composed += '\n';
|
|
|
|
PrintString(PRINT_HIGH, composed);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: Compile
|
|
|
|
//
|
|
|
|
// Compile everything defined at this level.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int ZCCCompiler::Compile()
|
|
|
|
{
|
2016-10-08 09:54:33 +00:00
|
|
|
CreateClasses();
|
2016-03-13 03:22:29 +00:00
|
|
|
CompileConstants(Constants);
|
2013-10-24 04:06:32 +00:00
|
|
|
return ErrorCount;
|
|
|
|
}
|
|
|
|
|
2016-10-08 09:54:33 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: CreateClassTypes
|
|
|
|
//
|
|
|
|
// Creates a PClass for every class so that we get access to the symbol table
|
|
|
|
// These will be created with unknown size because for that we need to
|
|
|
|
// process all fields first, but to do that we need the PClass and some
|
|
|
|
// other info depending on the PClass.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void ZCCCompiler::CreateClasses()
|
|
|
|
{
|
|
|
|
auto OrigClasses = std::move(Classes);
|
|
|
|
Classes.Clear();
|
|
|
|
bool donesomething = true;
|
|
|
|
while (donesomething)
|
|
|
|
{
|
|
|
|
for (unsigned i=0;i<OrigClasses.Size();i++)
|
|
|
|
{
|
|
|
|
donesomething = false;
|
|
|
|
auto c = OrigClasses[i];
|
|
|
|
// Check if we got the parent already defined.
|
|
|
|
PClass *parent;
|
|
|
|
|
|
|
|
if (c->ParentName != nullptr && c->ParentName->SiblingNext == c->ParentName) parent = PClass::FindClass(c->ParentName->Id);
|
|
|
|
else if (c->ParentName == nullptr) parent = RUNTIME_CLASS(DObject);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The parent is a dotted name which the type system currently does not handle.
|
|
|
|
// Once it does this needs to be implemented here.
|
|
|
|
auto p = c->ParentName;
|
|
|
|
FString build;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (build.IsNotEmpty()) build += '.';
|
|
|
|
build += FName(p->Id);
|
|
|
|
p = static_cast<decltype(p)>(p->SiblingNext);
|
|
|
|
} while (p != c->ParentName);
|
|
|
|
Error(c, "Qualified name '%s' for base class not supported in '%s'", build.GetChars(), FName(c->NodeName).GetChars());
|
|
|
|
parent = RUNTIME_CLASS(DObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
|
|
|
// The parent exists, we may create a type for this class
|
|
|
|
if (c->Flags & ZCC_Native)
|
|
|
|
{
|
|
|
|
// If this is a native class, its own type must also already exist.
|
|
|
|
auto me = PClass::FindClass(c->NodeName);
|
|
|
|
if (me == nullptr)
|
|
|
|
{
|
|
|
|
Error(c, "Unknown native class %s", FName(c->NodeName).GetChars());
|
|
|
|
me = parent->FindClassTentative(c->NodeName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPrintf(DMSG_SPAMMY, "Registered %s as native with parent %s\n", me->TypeName.GetChars(), parent->TypeName.GetChars());
|
|
|
|
}
|
|
|
|
c->Type = me;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto me = PClass::FindClass(c->NodeName);
|
|
|
|
if (me != nullptr)
|
|
|
|
{
|
|
|
|
Error(c, "Redefining class %s", FName(c->NodeName).GetChars());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
me = parent->FindClassTentative(c->NodeName);
|
|
|
|
DPrintf(DMSG_SPAMMY, "Created %s with parent %s\n", me->TypeName.GetChars(), parent->TypeName.GetChars());
|
|
|
|
}
|
|
|
|
c->Type = me;
|
|
|
|
}
|
|
|
|
Classes.Push(c);
|
|
|
|
OrigClasses.Delete(i);
|
|
|
|
i--;
|
|
|
|
donesomething = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No base class found. Now check if something in the unprocessed classes matches.
|
|
|
|
// If not, print an error. If something is found let's retry again in the next iteration.
|
|
|
|
bool found = false;
|
|
|
|
for (auto d : OrigClasses)
|
|
|
|
{
|
|
|
|
if (d->NodeName == c->ParentName->Id)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
Error(c, "Class %s has unknown base class %s", FName(c->NodeName).GetChars(), FName(c->ParentName->Id).GetChars());
|
|
|
|
// create a placeholder so that the compiler can continue looking for errors.
|
|
|
|
c->Type = RUNTIME_CLASS(DObject)->FindClassTentative(c->NodeName);
|
|
|
|
Classes.Push(c);
|
|
|
|
OrigClasses.Delete(i);
|
|
|
|
donesomething = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// What's left refers to some other class in the list but could not be resolved.
|
|
|
|
// This normally means a circular reference.
|
|
|
|
for (auto c : OrigClasses)
|
|
|
|
{
|
|
|
|
Error(c, "Class %s has circular inheritance", FName(c->NodeName).GetChars());
|
|
|
|
c->Type = RUNTIME_CLASS(DObject)->FindClassTentative(c->NodeName);
|
|
|
|
Classes.Push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: CompileConstants
|
|
|
|
//
|
|
|
|
// Make symbols from every constant defined at this level.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
void ZCCCompiler::CompileConstants(const TArray<ZCC_ConstantDef *> &defs)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
for (unsigned i = 0; i < defs.Size(); ++i)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
ZCC_ConstantDef *def = defs[i];
|
2013-10-24 04:06:32 +00:00
|
|
|
if (def->Symbol == NULL)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
PSymbolConst *sym = CompileConstant(def);
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: CompileConstant
|
|
|
|
//
|
|
|
|
// For every constant definition, evaluate its value (which should result
|
|
|
|
// in a constant), and create a symbol for it. Simplify() uses recursion
|
|
|
|
// to resolve constants used before their declarations.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
PSymbolConst *ZCCCompiler::CompileConstant(ZCC_ConstantDef *def)
|
|
|
|
{
|
|
|
|
assert(def->Symbol == NULL);
|
|
|
|
|
2013-10-26 03:01:18 +00:00
|
|
|
def->Symbol = DEFINING_CONST; // avoid recursion
|
2013-10-24 04:06:32 +00:00
|
|
|
ZCC_Expression *val = Simplify(def->Value);
|
|
|
|
def->Value = val;
|
|
|
|
PSymbolConst *sym = NULL;
|
|
|
|
if (val->NodeType == AST_ExprConstant)
|
|
|
|
{
|
|
|
|
ZCC_ExprConstant *cval = static_cast<ZCC_ExprConstant *>(val);
|
|
|
|
if (cval->Type == TypeString)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
sym = new PSymbolConstString(def->NodeName, *(cval->StringVal));
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
else if (cval->Type->IsA(RUNTIME_CLASS(PInt)))
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
sym = new PSymbolConstNumeric(def->NodeName, cval->Type, cval->IntVal);
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
else if (cval->Type->IsA(RUNTIME_CLASS(PFloat)))
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
sym = new PSymbolConstNumeric(def->NodeName, cval->Type, cval->DoubleVal);
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Error(def->Value, "Bad type for constant definiton");
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Error(def->Value, "Constant definition requires a constant value");
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
if (sym == NULL)
|
|
|
|
{
|
|
|
|
// Create a dummy constant so we don't make any undefined value warnings.
|
2016-03-13 03:22:29 +00:00
|
|
|
sym = new PSymbolConstNumeric(def->NodeName, TypeError, 0);
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
def->Symbol = sym;
|
2016-03-13 03:22:29 +00:00
|
|
|
Symbols->ReplaceSymbol(sym);
|
2013-10-24 04:06:32 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: Simplify
|
|
|
|
//
|
|
|
|
// For an expression,
|
|
|
|
// Evaluate operators whose arguments are both constants, replacing it
|
|
|
|
// with a new constant.
|
|
|
|
// For a binary operator with one constant argument, put it on the right-
|
|
|
|
// hand operand, where permitted.
|
|
|
|
// Perform automatic type promotion.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::Simplify(ZCC_Expression *root)
|
|
|
|
{
|
2013-11-02 02:28:00 +00:00
|
|
|
if (root->NodeType == AST_ExprUnary)
|
2013-10-31 01:53:02 +00:00
|
|
|
{
|
|
|
|
return SimplifyUnary(static_cast<ZCC_ExprUnary *>(root));
|
|
|
|
}
|
2013-11-02 02:28:00 +00:00
|
|
|
else if (root->NodeType == AST_ExprBinary)
|
2013-10-31 01:53:02 +00:00
|
|
|
{
|
|
|
|
return SimplifyBinary(static_cast<ZCC_ExprBinary *>(root));
|
|
|
|
}
|
|
|
|
else if (root->Operation == PEX_ID)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
|
|
|
return IdentifyIdentifier(static_cast<ZCC_ExprID *>(root));
|
|
|
|
}
|
2013-10-30 02:55:32 +00:00
|
|
|
else if (root->Operation == PEX_MemberAccess)
|
|
|
|
{
|
|
|
|
return SimplifyMemberAccess(static_cast<ZCC_ExprMemberAccess *>(root));
|
|
|
|
}
|
2013-10-31 01:53:02 +00:00
|
|
|
else if (root->Operation == PEX_FuncCall)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2013-10-31 01:53:02 +00:00
|
|
|
return SimplifyFunctionCall(static_cast<ZCC_ExprFuncCall *>(root));
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: SimplifyUnary
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::SimplifyUnary(ZCC_ExprUnary *unary)
|
|
|
|
{
|
|
|
|
unary->Operand = Simplify(unary->Operand);
|
|
|
|
ZCC_OpProto *op = PromoteUnary(unary->Operation, unary->Operand);
|
|
|
|
if (op == NULL)
|
|
|
|
{ // Oh, poo!
|
|
|
|
unary->Type = TypeError;
|
|
|
|
}
|
|
|
|
else if (unary->Operand->Operation == PEX_ConstValue)
|
|
|
|
{
|
|
|
|
return op->EvalConst1(static_cast<ZCC_ExprConstant *>(unary->Operand));
|
|
|
|
}
|
|
|
|
return unary;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: SimplifyBinary
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::SimplifyBinary(ZCC_ExprBinary *binary)
|
|
|
|
{
|
|
|
|
binary->Left = Simplify(binary->Left);
|
|
|
|
binary->Right = Simplify(binary->Right);
|
|
|
|
ZCC_OpProto *op = PromoteBinary(binary->Operation, binary->Left, binary->Right);
|
|
|
|
if (op == NULL)
|
|
|
|
{
|
|
|
|
binary->Type = TypeError;
|
|
|
|
}
|
|
|
|
else if (binary->Left->Operation == PEX_ConstValue &&
|
|
|
|
binary->Right->Operation == PEX_ConstValue)
|
|
|
|
{
|
|
|
|
return op->EvalConst2(static_cast<ZCC_ExprConstant *>(binary->Left),
|
|
|
|
static_cast<ZCC_ExprConstant *>(binary->Right), AST.Strings);
|
|
|
|
}
|
|
|
|
return binary;
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:55:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: SimplifyMemberAccess
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::SimplifyMemberAccess(ZCC_ExprMemberAccess *dotop)
|
|
|
|
{
|
|
|
|
dotop->Left = Simplify(dotop->Left);
|
|
|
|
|
|
|
|
if (dotop->Left->Operation == PEX_TypeRef)
|
|
|
|
{ // Type refs can be evaluated now.
|
|
|
|
PType *ref = static_cast<ZCC_ExprTypeRef *>(dotop->Left)->RefType;
|
2016-03-13 03:22:29 +00:00
|
|
|
PSymbolTable *symtable;
|
|
|
|
PSymbol *sym = ref->Symbols.FindSymbolInTable(dotop->Right, symtable);
|
2013-10-30 02:55:32 +00:00
|
|
|
if (sym == NULL)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Error(dotop, "'%s' is not a valid member", FName(dotop->Right).GetChars());
|
2013-10-30 02:55:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
ZCC_Expression *expr = NodeFromSymbol(sym, dotop, symtable);
|
2013-10-30 02:55:32 +00:00
|
|
|
if (expr == NULL)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Error(dotop, "Unhandled symbol type encountered");
|
2013-10-30 02:55:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dotop;
|
|
|
|
}
|
|
|
|
|
2013-10-31 01:53:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: SimplifyFunctionCall
|
|
|
|
//
|
|
|
|
// This may replace a function call with cast(s), since they look like the
|
|
|
|
// same thing to the parser.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::SimplifyFunctionCall(ZCC_ExprFuncCall *callop)
|
|
|
|
{
|
|
|
|
ZCC_FuncParm *parm;
|
|
|
|
int parmcount = 0;
|
|
|
|
|
|
|
|
callop->Function = Simplify(callop->Function);
|
|
|
|
parm = callop->Parameters;
|
|
|
|
if (parm != NULL)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
parmcount++;
|
|
|
|
assert(parm->NodeType == AST_FuncParm);
|
|
|
|
parm->Value = Simplify(parm->Value);
|
|
|
|
parm = static_cast<ZCC_FuncParm *>(parm->SiblingNext);
|
|
|
|
}
|
|
|
|
while (parm != callop->Parameters);
|
|
|
|
}
|
|
|
|
// If the left side is a type ref, then this is actually a cast
|
|
|
|
// and not a function call.
|
|
|
|
if (callop->Function->Operation == PEX_TypeRef)
|
|
|
|
{
|
|
|
|
if (parmcount != 1)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
Error(callop, "Type cast requires one parameter");
|
2013-10-31 01:53:02 +00:00
|
|
|
callop->ToErrorNode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PType *dest = static_cast<ZCC_ExprTypeRef *>(callop->Function)->RefType;
|
|
|
|
const PType::Conversion *route[CONVERSION_ROUTE_SIZE];
|
|
|
|
int routelen = parm->Value->Type->FindConversion(dest, route, countof(route));
|
|
|
|
if (routelen < 0)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
///FIXME: Need real type names
|
|
|
|
Error(callop, "Cannot convert type 1 to type 2");
|
2013-10-31 01:53:02 +00:00
|
|
|
callop->ToErrorNode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ZCC_Expression *val = ApplyConversion(parm->Value, route, routelen);
|
|
|
|
assert(val->Type == dest);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callop;
|
|
|
|
}
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: PromoteUnary
|
|
|
|
//
|
|
|
|
// Converts the operand into a format preferred by the operator.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_OpProto *ZCCCompiler::PromoteUnary(EZCCExprType op, ZCC_Expression *&expr)
|
|
|
|
{
|
|
|
|
if (expr->Type == TypeError)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const PType::Conversion *route[CONVERSION_ROUTE_SIZE];
|
|
|
|
int routelen = countof(route);
|
|
|
|
ZCC_OpProto *proto = ZCC_OpInfo[op].FindBestProto(expr->Type, route, routelen);
|
|
|
|
|
|
|
|
if (proto != NULL)
|
|
|
|
{
|
|
|
|
expr = ApplyConversion(expr, route, routelen);
|
|
|
|
}
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: PromoteBinary
|
|
|
|
//
|
|
|
|
// Converts the operands into a format (hopefully) compatible with the
|
|
|
|
// operator.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_OpProto *ZCCCompiler::PromoteBinary(EZCCExprType op, ZCC_Expression *&left, ZCC_Expression *&right)
|
|
|
|
{
|
|
|
|
// If either operand is of type 'error', the result is also 'error'
|
|
|
|
if (left->Type == TypeError || right->Type == TypeError)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const PType::Conversion *route1[CONVERSION_ROUTE_SIZE], *route2[CONVERSION_ROUTE_SIZE];
|
|
|
|
int route1len = countof(route1), route2len = countof(route2);
|
|
|
|
ZCC_OpProto *proto = ZCC_OpInfo[op].FindBestProto(left->Type, route1, route1len, right->Type, route2, route2len);
|
|
|
|
if (proto != NULL)
|
|
|
|
{
|
|
|
|
left = ApplyConversion(left, route1, route1len);
|
|
|
|
right = ApplyConversion(right, route2, route2len);
|
|
|
|
}
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: ApplyConversion
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::ApplyConversion(ZCC_Expression *expr, const PType::Conversion **route, int routelen)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < routelen; ++i)
|
|
|
|
{
|
|
|
|
if (expr->Operation != PEX_ConstValue)
|
|
|
|
{
|
|
|
|
expr = AddCastNode(route[i]->TargetType, expr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
route[i]->ConvertConstant(static_cast<ZCC_ExprConstant *>(expr), AST.Strings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: AddCastNode
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::AddCastNode(PType *type, ZCC_Expression *expr)
|
|
|
|
{
|
|
|
|
assert(expr->Operation != PEX_ConstValue && "Expression must not be constant");
|
|
|
|
// TODO: add a node here
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: IdentifyIdentifier
|
|
|
|
//
|
|
|
|
// Returns a node that represents what the identifer stands for.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_Expression *ZCCCompiler::IdentifyIdentifier(ZCC_ExprID *idnode)
|
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
// Check the symbol table for the identifier.
|
|
|
|
PSymbolTable *table;
|
|
|
|
PSymbol *sym = Symbols->FindSymbolInTable(idnode->Identifier, table);
|
|
|
|
if (sym != NULL)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
ZCC_Expression *node = NodeFromSymbol(sym, idnode, table);
|
2013-10-30 02:55:32 +00:00
|
|
|
if (node != NULL)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
2013-10-30 02:55:32 +00:00
|
|
|
return node;
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-03-13 03:22:29 +00:00
|
|
|
{
|
|
|
|
Error(idnode, "Unknown identifier '%s'", FName(idnode->Identifier).GetChars());
|
2013-10-24 04:06:32 +00:00
|
|
|
}
|
|
|
|
// Identifier didn't refer to anything good, so type error it.
|
2013-10-31 01:53:02 +00:00
|
|
|
idnode->ToErrorNode();
|
2013-10-24 04:06:32 +00:00
|
|
|
return idnode;
|
|
|
|
}
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: CompileNode
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
PSymbol *ZCCCompiler::CompileNode(ZCC_NamedNode *node)
|
|
|
|
{
|
|
|
|
assert(node != NULL);
|
|
|
|
if (node->NodeType == AST_ConstantDef)
|
|
|
|
{
|
|
|
|
ZCC_ConstantDef *def = static_cast<ZCC_ConstantDef *>(node);
|
|
|
|
PSymbolConst *sym = def->Symbol;
|
|
|
|
|
|
|
|
if (sym == DEFINING_CONST)
|
|
|
|
{
|
|
|
|
Error(node, "Definition of '%s' is infinitely recursive", FName(node->NodeName).GetChars());
|
|
|
|
sym = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(sym == NULL);
|
|
|
|
sym = CompileConstant(def);
|
|
|
|
}
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
else if (node->NodeType == AST_Struct)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-24 04:06:32 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2013-10-30 02:55:32 +00:00
|
|
|
// ZCCCompiler :: NodeFromSymbol
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-03-13 03:22:29 +00:00
|
|
|
ZCC_Expression *ZCCCompiler::NodeFromSymbol(PSymbol *sym, ZCC_Expression *source, PSymbolTable *table)
|
2013-10-30 02:55:32 +00:00
|
|
|
{
|
2016-03-13 03:22:29 +00:00
|
|
|
assert(sym != NULL);
|
|
|
|
if (sym->IsA(RUNTIME_CLASS(PSymbolTreeNode)))
|
|
|
|
{
|
|
|
|
PSymbolTable *prevtable = Symbols;
|
|
|
|
Symbols = table;
|
|
|
|
sym = CompileNode(static_cast<PSymbolTreeNode *>(sym)->Node);
|
|
|
|
Symbols = prevtable;
|
|
|
|
if (sym == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 02:55:32 +00:00
|
|
|
if (sym->IsKindOf(RUNTIME_CLASS(PSymbolConst)))
|
|
|
|
{
|
|
|
|
return NodeFromSymbolConst(static_cast<PSymbolConst *>(sym), source);
|
|
|
|
}
|
|
|
|
else if (sym->IsKindOf(RUNTIME_CLASS(PSymbolType)))
|
|
|
|
{
|
|
|
|
return NodeFromSymbolType(static_cast<PSymbolType *>(sym), source);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: NodeFromSymbolConst
|
2013-10-24 04:06:32 +00:00
|
|
|
//
|
|
|
|
// Returns a new AST constant node with the symbol's content.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2013-10-30 02:55:32 +00:00
|
|
|
ZCC_ExprConstant *ZCCCompiler::NodeFromSymbolConst(PSymbolConst *sym, ZCC_Expression *idnode)
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
|
|
|
ZCC_ExprConstant *val = static_cast<ZCC_ExprConstant *>(AST.InitNode(sizeof(*val), AST_ExprConstant, idnode));
|
|
|
|
val->Operation = PEX_ConstValue;
|
2013-10-26 03:01:18 +00:00
|
|
|
if (sym == NULL)
|
|
|
|
{
|
|
|
|
val->Type = TypeError;
|
|
|
|
val->IntVal = 0;
|
|
|
|
}
|
|
|
|
else if (sym->IsKindOf(RUNTIME_CLASS(PSymbolConstString)))
|
2013-10-24 04:06:32 +00:00
|
|
|
{
|
|
|
|
val->StringVal = AST.Strings.Alloc(static_cast<PSymbolConstString *>(sym)->Str);
|
|
|
|
val->Type = TypeString;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val->Type = sym->ValueType;
|
|
|
|
if (val->Type != TypeError)
|
|
|
|
{
|
|
|
|
assert(sym->IsKindOf(RUNTIME_CLASS(PSymbolConstNumeric)));
|
|
|
|
if (sym->ValueType->IsKindOf(RUNTIME_CLASS(PInt)))
|
|
|
|
{
|
|
|
|
val->IntVal = static_cast<PSymbolConstNumeric *>(sym)->Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(sym->ValueType->IsKindOf(RUNTIME_CLASS(PFloat)));
|
|
|
|
val->DoubleVal = static_cast<PSymbolConstNumeric *>(sym)->Float;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2013-10-30 02:55:32 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// ZCCCompiler :: NodeFromSymbolType
|
|
|
|
//
|
|
|
|
// Returns a new AST type ref node with the symbol's content.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ZCC_ExprTypeRef *ZCCCompiler::NodeFromSymbolType(PSymbolType *sym, ZCC_Expression *idnode)
|
|
|
|
{
|
|
|
|
ZCC_ExprTypeRef *ref = static_cast<ZCC_ExprTypeRef *>(AST.InitNode(sizeof(*ref), AST_ExprTypeRef, idnode));
|
|
|
|
ref->Operation = PEX_TypeRef;
|
|
|
|
ref->RefType = sym->Type;
|
|
|
|
ref->Type = NewClassPointer(RUNTIME_CLASS(PType));
|
|
|
|
return ref;
|
|
|
|
}
|