跳转到内容

Compiler 2014: Types

来自ACM Class Wiki

Here is some Java codes of type definition.

 abstract class Type {}
 final class Void extends Type {}
 final class Char extends Type {}
 final class Int extends Type {}
 final class Name extends Type {
     String name;
     // maybe you need a reference to an environment here
 }
 class Pointer extends Type {
     Type elementType;
 }
 final class Array extends Type {
     Type elementType;
     int capacity;
 }

We need a separate type for Array because there're typedef's like typedef int T[10];

 abstract class Record extends Type {
     class RecordField {
         Type type;
         String name;
     }
     List<RecordField> fields;
 }
 final class Struct extends Record {}
 final class Union extends Record {}

The Function type is composed of the argument type (Void if no arguments) and the return type, where the return type can be a Function type for a function with two or more arguments. This design also allows first-class functions.

 final class Function extends Type {
     Type argumentType;
     Type returnType;
 }

Hierarchy

 data Type = Void
           | Char
           | Int
           | Name     String
           | Pointer  Type
           | Array    Type  Int
           | Struct [(Type, String)]
           | Union  [(Type, String)]
           | Function Type  Type