Skip to content
Carlos Tome edited this page Apr 24, 2014 · 4 revisions

In this stage the compiler only performs pure desugaring without any typechecking, although it checks user defined types for identifier clashes between them.

If is not able to pass through this stage it will prompt a Desugar error.

Desugaring happens in several ways depending on the extension:

Unidimensional and multidimensional arrays
Pointers and Structures

Every type representing a pointer to a structure, is converted into a type which gives explicit information about this.

For example:


typedef struct Node *list

struct Node { ... }
...

list l = New Node;

Is translated into:


struct Node { ... }
...

Node* l = New Node;

As the synonym information is not furthermore required, is thrown away.

Object orientation.

In order to desugar objects into structures the next is done:

For every class C with a1, a2 .. an attributes and m1, m2, mn methods we transform this into a structure C with a1, a2 .. an fields and m1, m2, .. mn top level functions which first argument is a object of the class named self.

For example:


class Foo { 
  int x;
  int getX() { return x; }
}

is desugared into:


struct Foo { 
  int x;
}

int Foo.getX(Foo* self) { 
  return self -> x; 
}

In the case of class inheritance, new attributes and methods overwrite old ones.


class Bar { 
  int y;
  int getY() { return y; }
}

struct Bar { 
  int x;
  int y;
}

int Bar.getX(Bar* self) { 
  return self -> x; 
}

int Bar.getY(Bar* self) { 
  return self -> y; 
}

Here as Bar is a subtype of Foo we keep this information explicit in the type of Bar. So the type of any object of class C is C [| Super Types |] to be able to determine if the subtyping relation holds in the next stage.

Clone this wiki locally