Using the Melange Interface Generator

Robert Stockton

Table of Contents

Introduction
A Concrete Example
Basic Use
Loading and Finding Objects
Importing Header Files
Specifying Object Names
Mapping functions
Prefixes
Explicit Renaming
Anonymous Types
Type Definitions
Implicit class definitions
Specifying class inheritance
Translating Object Representations
Specifying low level transformations
Specifying high level transformations
Other File Options
Function Clauses
Struct and Union Clauses
Pointer Clauses
Constant Clauses
Variable Clauses
Low level support facilities
Predefined types
Locating native C objects
Pointer manipulation operations
Static linking mechanisms
Differences from Creole
Known limitations
Proposed modifications
Enumeration clauses
Inheritance of "map" and "equate" options
Remerging of the "equate:" and "map:" options

Abstract

The Melange interface generator provides a mechanism for providing access to native C code. It is modeled upon Apple Computer's Creole, and shares Creole's goals of automatically providing full support for a foreign interface based upon existing interface descriptions. It also, like Creole, provides mechanisms for explicitly adapting these interfaces to provide a greater match between C and Dylan data models.

Melange, however, differs from Creole in a number of significant ways. This document, therefore, provides a gentle introduction to Melange without attempting to build upon any existing descriptions of Creole.

Introduction

Melange is an automatic interface generator which provides transparent access to both functions and data defined or generated by existing C libraries. It allows users to import "interfaces"[1] from existing C header files, controlled by the contents of a "define interface" top-level form which may be included in the same file as arbitrary Dylan code. The user may use the functions and data specified by this interface as if they were native Dylan objects, and may export them to other modules.

Melange provides reasonable interpretations for the various sorts of C declarations which may appear in a header file, as well as mechanisms for explicitly modifying the default interpretations when necessary. For example, users may:

  • specify rules for the translation of foreign names

  • explicitly specify new names for specific objects or routines

  • specify parameter passing conventions or mutability of foreign objects

  • specify mappings or equivalences between "foreign" data and native equivalents

  • choose to import only a subset of the declarations in the header file

All of these customizations, as well as the name of the C header file, are specified by a "define interface" clause. See the section called “A Concrete Example” for an example.

The basic model for interface importation is based upon that used within Apple Computer's "Creole" interface generator. There are, however, significant differences in some of the details. (For instance, the "equate", "map", and "object-file" directives used in the above example are unique to Melange. Likewise, Creole's "type" directive would not be accepted by Melange) You should, therefore, not expect Creole interface declarations to work within Melange without some modification.



[1] In fact, a C header file may contain arbitrary C code which Melange is unprepared to handle. By convention, however, ".h" files con tain only "interface declarations"—type declarations, function prototypes, global variable declarations, and "preprocessor constants." Since Melange can meaningfully process all of these, it is capable of handling the vast majority of header files which will be encountered in practice.