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

📄 type.cpp

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

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

 


/* Class constants
 * ---------------
 * These are public constants for the built-in base types (int, double, etc.)
 * They can be accessed with the syntax Type::intType. This allows you to
 * directly access the built-in types wherever you need them. These objects
 * should be declared const, but that forces you to respect const-ness
 * everywhere. Although that would be the correct thing to do, const can
 * be trouble to deal with, especially if you don't know C++ well.
 * So to keep things simple, these are not const, but you should treat them
 * as though they aren't (i.e. don't modify them).
 */
Type *Type::intType    = new Type(Type::Base, "int");
Type *Type::doubleType = new Type(Type::Base, "double");
Type *Type::voidType   = new Type(Type::Base, "void");
Type *Type::boolType   = new Type(Type::Base, "bool");
Type *Type::nullType   = new Type(Type::Base, "null");
Type *Type::stringType = new Type(Type::Base, "string");
Type *Type::errorType  = new Type(Type::Base, "error"); 



// Name should be the printable version of the type:
// double, array of int, class Binky, etc. 
Type::Type(KindOfType kind, const char *nameForType)
{
  typeCode = kind;
  typeName = strdup(nameForType);
}


/*
 * Type equivalency
 * ---------------
 * This stripped-down implementation only provides equivalency for
 * the base types which is all we need for code generation.
 */
bool Type::IsEquivalentTo(Type *other)       
{
  return this == other;
}



Type *Type::NewArrayType(Type *elemType)
{
  int i;
  char* buf;
  i = strlen(elemType->ToString())+4;
  buf = new char[i];
  sprintf(buf, "%s[]", elemType->ToString());
  Type *t = new Type(Array, buf);
  t->info.arrayType.elemType = elemType;
  return t;
}

Type *Type::GetArrayElemType()       
{
  Assert(IsArrayType());
  return info.arrayType.elemType;
}

 

// Note: our strategy is to copy all parent fields into subclass
// rather than link the subclass to its parent class
Type *Type::NewClassType(const char *name, Type *super)
{
  int i;
  char* buf;
  i = strlen(name)+8;
  buf = new char[i];
  sprintf(buf, "class %s", name);
  Type *t = new Type(Class, buf);
  t->info.classType.name = strdup(name);
  t->info.classType.fields = new Scope(t);
  if (!super || super->IsEquivalentTo(Type::errorType)) {
    t->info.classType.superclass = NULL;
  } else {
    Assert(super->IsClassType());
    t->info.classType.superclass = super;
    t->GetClassScope()->CopyFieldsFromParent(super->GetClassScope());
  }
  return t;
}

const char *Type::GetClassName()       
{
  Assert(IsClassType());
  return info.classType.name;
}


Scope *Type::GetClassScope()       
{
  Assert(IsClassType());
  return info.classType.fields;
}



Type *Type::NewFunctionType(Type *returnType, DeclList *formals)
{
   Type *t = new Type(Function, "function");
   t->info.fnType.returnType = returnType;
   t->info.fnType.formals = formals;
   return t;
}
 
Type *Type::GetFnReturnType()       
{
  Assert(IsFunctionType());
  return info.fnType.returnType;
}
    
DeclList *Type::GetFnFormals()       
{
  Assert(IsFunctionType());
  return info.fnType.formals;
}


⌨️ 快捷键说明

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