10. Interface to other languages

10.1 Overview

The Perfect compiler produces implementation code from the Perfect source. This code may be expressed either in object module format or as source code in a target programming language.

It is often required to mix program elements written in Perfect with program elements written in a traditional programming language. This section describes some facilities available to achieve this. The exact behavior may be implementation-dependent and may depend on the target language.

10.2 Local pragmas

Where an identifier or operator declared in the Perfect source causes a corresponding identifier to be declared in the output file, the compiler has default rules for generating the name of this external identifier from the original Perfect identifier. A developer may override these rules by placing a Pragma after the identifier or operator symbol being declared, or after the build keyword in a constructor declaration. The syntax is:

Pragma:
        pragma '(' PragmaName '=' NonEmptyStringLiteral [ ',' PragmaName '=' NonEmptyStringLiteral ]* ')'.

A pragma may immediately follow any identifier or operator being declared.

The most useful PragmaName in a local pragma is codename. The effect of using this pragma is that for all instances of the identifier which bind to the declaration, the corresponding name in the generated code will be as specified in the string literal. If you wish to change the name in the generated code for just one output language, or to use different names for different output languages, then you can use pragma names adaname, cppname, javaname and csharpname instead of codename.

Normally, the Perfect compiler performs any name-mangling necessary to avoid Perfect identifiers clashing with reserved words in the output language or external names used in the run-time system or environment interface. It is the developer's responsibility to avoid clashes when using pragmas to define the names used in the generated code.

Local pragmas can also be used to change the generated code in other ways, for example to suppress reference counting when generating C++.

10.2 Global pragmas

A global pragma has the same syntax as a local pragma, but is placed in the outermost declaration list of a source file instead of being attached to an identifier, and is followed by a semicolon to separate it from the following declaration. The global pragma will be applied to all subsequent identifiers declared at the outermost level. A later global pragma will override an earlier one with the same PragmaName.

The most useful PragmaName in a global pragma is adapackage. This pragma affects Ada (including SPARK) code generation only. It causes subsequent declarations to be placed in a package of the specified name. By this means, the declarations generated from a single Perfect source file can be split up into several Ada packages, to avoid the circular with-dependencies that might otherwise occur in the generated code.

 

Perfect Language Reference Manual, Version 7.0, February 2017.
© 2017 Escher Technologies Limited. All rights reserved.