⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 declaration.cpp

📁 一个面向对像语言的编译器
💻 CPP
字号:
/* File: declaration.cc
 * --------------------
 * Implementation of Declaration class.
 */

#include "declaration.h"
#include <string.h>
#include <stdio.h>
#include "utility.h"
#include "type.h"
#include "scope.h"
#include "tac.h" 

/* Class constant: errorDecl
 * -------------------------
 * We can use @error as the name for the declaration since we know that
 * it will never conflict with anthing else that the user has typed in.
 * @ is not a valid character in our scanner...
 */
Declaration *Declaration::errorDecl =
           new Declaration(Declaration::Variable, "@error", Type::errorType);


Declaration::Declaration(KindOfDecl kind, const char *n, Type *t,int line)
{
  declCode = kind;
  name = strdup(n);
  type = t;
  lineFound = line;
  scope = NULL;
  offset = stackFrameSize = Unassigned;
  bIsAddr=false;
  bFunIsDefined=false;
}

bool Declaration::IsGlobal()      
{
  return scope && scope->IsGlobalScope();
}


bool Declaration::IsClassField()      
{
  return (IsVarDecl() || IsFunctionDecl())
    && scope && scope->IsClassScope();
}

void Declaration::SetStackFrameSize(int sz)
{
//  Assert(IsFunctionDecl());
  stackFrameSize = sz;
}
 
int Declaration::GetStackFrameSize()      
{
  //Assert(IsFunctionDecl());
  return stackFrameSize;
  
}


/* Method: GetFunctionLabel
 * ------------------------
 * This synthesizes the appropriate label for function in such a way to
 * identify uniquely and without conflicts.  The "main" function in
 * global scope is special and has an unadorned label since it is an
 * externed symbol. For functions, the label is the name prefixed with
 * an underbar. For methods, the label is the name prefixed by
 * the class name followed by a dot.  The string returned is
 * heap-allocated (and thus often leaked :-( )
 */  
const char *Declaration::GetFunctionLabel()      
{
  Assert(IsFunctionDecl());
  Assert(scope); // need to be declared to know how to prepare label
  char buffer[260];// identifiers are at most 128
  if (IsClassField())
    sprintf(buffer, "_%s.%s", scope->GetClassType()->GetClassName(), name);
  else
    sprintf(buffer, "%s%s", strcmp(name, "main") == 0 ? "" : "_", name);
  return strdup(buffer);
}
int Declaration::GetOffset()
{
	if(Tac::toWhat==Tac::ToMips &&  declCode== Variable && 
		(scope->IsFunctionScope()||scope->IsLocalScope())  )
		return offset-4;
	else
		return offset;
}
const char *Declaration::Getx86FunctionLabel()      
{
  Assert(IsFunctionDecl());
  Assert(scope); // need to be declared to know how to prepare label
  char buffer[260];// identifiers are at most 128
  if (IsClassField())
    sprintf(buffer, "_%s@%s", scope->GetClassType()->GetClassName(), name);
  else
    sprintf(buffer, "%s%s", strcmp(name, "main") == 0 ? "" : "_", name);
  return strdup(buffer);
}

bool Declaration::IsAddr()
{
	return bIsAddr; 
}

void Declaration::ValidateAddr()
{
	bIsAddr=true;
}

void Declaration::MakeFunDefined()
{
	bFunIsDefined=true;
}

bool Declaration::FunIsDefined()
{
	return bFunIsDefined;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -