QM  6.1.1
Model-Based Design Tool
Loading...
Searching...
No Matches
Code Generation

Working with Files$declare

The QM™ code generation process consists of replacing the code-generation directives in the file templates in the model with the corresponding pieces of auto-generated code in the files generated on disk. All other code fragments surrounding the code-generation directives in the file-templates are simply copied to the files on disk. The picture below illustrates the process:

QM Code Generation as Expansion of Code-Generation Directives

Declarations versus Definitions

To work effectively with the code-generation directives in QM™, you need to understand the difference between declarations and definitions in C or C++.

Declaration

A declaration introduces a name and describes its type, be it a type name (struct/class/typedef), object (variable), or a function. A declaration is what the C or C++ compiler needs to accept references to that name. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // forward declaration
class bar { bar(); ... }; // no extern allowed for class declarations
Note
The QM™ code generator applies the keyword extern only for declarations of variables.

Definition

A definition actually instantiates/implements the symbol. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
bar::bar() {};
Note
In C and C++ a definition can be used in the place of a declaration, meaning that a definition also provides the type information that the compiler needs to accept references to the defined symbol.
Remarks
Some C or C++ compilers can be configured to go beyond the ISO-C or ISO-C++ Standards and actually require separate declarations (e.g., "Require Prototypes" feature in the IAR compilers).

General Syntax

The general syntax of the code-generating directives in the QM file templates is:

$<directive> ${<model-item>}
Note
$<directive> can be followed by zero or more spaces before the ${<model-item>} specification.

where $<directive> is one of:

  • $declare for recursive declaration (e.g., $declare ${<model-item>})
  • $declare1 for non-recursive declaration (e.g,, $declare1 ${<model-item>})
  • $define for recursive definition (e.g., $define ${<model-item>})
  • $define1 for non-recursive definition (e.g., $define1 ${<model-item>})

and <model-item> denotes a Fully-Qualified Name of the model item to expand.

Remarks
The use of Fully-Qualified Item Names is necessary to unambiguously identify model items for code generation. For example, a class Ship residing inside the package AOs will be specified inside a code-generating directive as AOs::Ship.
Example of $declare ${...} and $define ${...} Directives
Remarks
For backward compatibility, the QM tool supports also the old syntax the code-generating directives:
$<directive>(<model-item>)

Creating Fully-Qualified Names by Drag-n-Drop

The fully-qualified item names can be, of course, typed by hand in the QM file templates. However, to avoid errors, the Model Explorer allows you to drag-n-drop a model item onto a file-template to create a fully-qualified item name, as illustrated in the animation below:

Creating a Fully-Qualified Item Name by Drag-n-Drop
Note
The code-generation directives are recognized by the QM syntax highlighter and are shown in special color and embellishment (see Customizing QM GUI) in the QM Code Editors.

Working with Files$declare