lingua-franca 0.10.1
Lingua Franca code generator
Loading...
Searching...
No Matches
org.lflang.ast.ASTUtils Class Reference

A helper class for modifying and analyzing the AST. More...

Static Public Member Functions

static void addReactionAttribute (Reaction reaction, String name)
 Add an attribute to the given reaction.
static String addZeroToLeadingDot (String literal)
 If the given string can be recognized as a floating-point number that has a leading decimal point, prepend the string with a zero and return it.
static List< Action > allActions (Reactor definition)
 Given a reactor class, return a list of all its actions, which includes actions of base classes that it extends.
static List< Connection > allConnections (Reactor definition)
 Given a reactor class, return a list of all its connections, which includes connections of base classes that it extends.
static< T extends EObject > Iterable< T > allElementsOfClass (Resource resource, Class< T > elementClass)
static LinkedHashSet< Preamble > allFileLevelPreambles (Reactor reactor)
 Return all the file-level preambles in the files that define the specified class and its superclasses in deepest-first order.
static List< Input > allInputs (Reactor definition)
 Given a reactor class, return a list of all its inputs, which includes inputs of base classes that it extends.
static List< Instantiation > allInstantiations (Reactor definition)
 Given a reactor class, return a list of all its instantiations, which includes instantiations of base classes that it extends.
static List< Method > allMethods (Reactor definition)
 Given a reactor class, return a list of all its methods, which includes methods of base classes that it extends.
static List< Mode > allModes (Reactor definition)
 Given a reactor class, returns a list of all its modes, which includes modes of base classes that it extends.
static Stream< Reactor > allNestedClasses (Reactor definition)
static List< Output > allOutputs (Reactor definition)
 Given a reactor class, return a list of all its outputs, which includes outputs of base classes that it extends.
static List< Parameter > allParameters (Reactor definition)
 Given a reactor class, return a list of all its parameters, which includes parameters of base classes that it extends.
static List< Port > allPorts (Reactor definition)
 A list of all ports of definition, in an unspecified order.
static List< Preamble > allPreambles (Reactor definition)
 Given a reactor class, return a list of all its preambles, which includes preambles of base classes that it extends.
static List< Reaction > allReactions (Reactor definition)
 Given a reactor class, return a list of all its reactions, which includes reactions of base classes that it extends.
static List< StateVar > allStateVars (Reactor definition)
 Given a reactor class, return a list of all its state variables, which includes state variables of base classes that it extends.
static List< Timer > allTimers (Reactor definition)
 Given a reactor class, return a list of all its timers, which includes timers of base classes that it extends.
static List< Watchdog > allWatchdogs (Reactor definition)
 Given a reactor class, return a list of all its watchdogs.
static String baseType (Type type)
 Translate the given type into its textual representation, but do not append any array specifications or type arguments.
static boolean belongsTo (EObject eobject, Instantiation instantiation)
 Return true if the specified object (a Parameter, Port, Action, or Timer) belongs to the specified instantiation, meaning that it is defined in the reactor class being instantiated or one of its base classes.
static boolean belongsTo (EObject eobject, Reactor reactor)
 Return true if the specified object (a Parameter, Port, Action, or Timer) belongs to the specified reactor, meaning that it is defined in reactor class or one of its base classes.
static boolean changeTargetName (Resource resource, String newTargetName)
 Change the target name to 'newTargetName'.
static< T extends EObject > List< T > collectElements (Reactor definition, EStructuralFeature feature)
 Collect elements of type T from the class hierarchy and modes defined by a given reactor definition.
static< T extends EObject > List< T > collectElements (Reactor definition, EStructuralFeature feature, boolean includeSuperClasses, boolean includeModes)
 Collect elements of type T contained in given reactor definition, including modes and the class hierarchy defined depending on configuration.
static< T > List< T > convertToEmptyListIfNull (List< T > list)
 Returns the list if it is not null.
static Instantiation createInstantiation (Reactor reactor)
 Create a new instantiation node with the given reactor as its defining class.
static ReactorInstance createMainReactorInstance (Instantiation mainDef, List< Reactor > reactors, MessageReporter messageReporter, TargetConfig targetConfig)
 If a main or federated reactor has been declared, create a ReactorInstance for this top level.
static List< String > elementToListOfStrings (Element value)
 Given the right-hand side of a target property, return a list with all the strings that the property lists.
static String elementToSingleString (Element e)
 Given the right-hand side of a target property, return a string that represents the given value/.
static Map< String, String > elementToStringMaps (Element value)
 Convert key-value pairs in an Element to a map, assuming that both the key and the value are strings.
static Collection< Connection > findConflictingConnectionsInModalReactors (Resource resource)
 Find connections in the given resource that would be conflicting writes if they were not located in mutually exclusive modes.
static Reactor findMainReactor (Resource resource)
 Return the main reactor in the given resource if there is one, null otherwise.
static String generateVarRef (VarRef reference)
 Generate code for referencing a port, action, or timer.
static List< Reactor > getAllReactors (Resource resource)
 Get all reactors defined in the given resource.
static TimeValue getDefaultAsTimeValue (Parameter p)
 If the parameter is of time type, return its default value.
static Long getDelay (Expression delay)
 Return the delay (in nanoseconds) denoted by delay, or null if the delay cannot be determined.
static TimeValue getDelayAsTimeValue (Expression delay)
 Return the delay denoted by delay or null if the delay cannot be determined.
static Set< Instantiation > getEnclaves (Reactor top)
 Return the set of enclave instantiations in the given reactor definition.
static Reactor getEnclosingReactor (EObject obj)
 Return the enclosing reactor of an LF EObject in a reactor or mode.
static Optional< Reactor > getFederatedReactor (Resource resource)
 Get the federated reactor defined in the given resource, if there is one.
static InferredType getInferredType (Action a)
 Construct an inferred type from an "action" AST node based on its declared type.
static InferredType getInferredType (Parameter p)
 Given a parameter, return an inferred type.
static InferredType getInferredType (Port p)
 Construct an inferred type from a "port" AST node based on its declared type.
static InferredType getInferredType (StateVar s)
 Given a state variable, return an inferred type.
static InferredType getInferredType (Type type, Initializer init)
 Return the type of a declaration with the given (nullable) explicit type, and the given (nullable) initializer.
static TimeValue getLiteralTimeValue (Expression expr)
 Assuming that the given expression denotes a valid time value, return a time value.
static Optional< Reactor > getMainReactor (Resource resource)
 Get the main reactor defined in the given resource, if there is one.
static Stream< INode > getPrecedingCommentNodes (ICompositeNode compNode, Predicate< INode > filter)
 Return all single-line or multi-line comments immediately preceding the given EObject.
static Stream< String > getPrecedingComments (ICompositeNode compNode, Predicate< INode > filter)
 Return all single-line or multi-line comments immediately preceding the given EObject.
static Target getTarget (EObject object)
 Return the target of the file in which the given node lives.
static String getUniqueIdentifier (Reactor reactor, String name)
 Produce a unique identifier within a reactor based on a given based name.
static boolean hasMultipleConnections (Connection connection)
 Return true if the connection involves multiple ports on the left or right side of the connection, or if the port on the left or right of the connection involves a bank of reactors or a multiport.
static int inferPortWidth (VarRef reference, Connection connection, List< Instantiation > instantiations)
 Infer the width of a port reference in a connection.
static Expression initialValue (Parameter parameter, List< Instantiation > instantiations)
 Given a parameter, return its initial value.
static Integer initialValueInt (Parameter parameter, List< Instantiation > instantiations)
 Given a parameter return its integer value or null if it does not have an integer value.
static boolean isBigInteger (String literal)
 Report whether the given string literal is an integer number or not.
static boolean isBoolean (String literal)
 Report whether the given string literal is a boolean value or not.
static boolean isComment (INode node)
 Return whether node is a comment.
static boolean isFloat (String literal)
 Report whether the given string literal is a float value or not.
static boolean isGeneric (Reactor r)
 Check if the reactor class uses generics.
static boolean isInCode (INode node)
static boolean isInitialized (StateVar v)
 Report whether a state variable has been initialized or not.
static boolean isInteger (String literal)
 Report whether the given string literal is an integer number or not.
static boolean isMultilineComment (INode node)
 Return whether node is a multiline comment.
static boolean isMultiport (Port port)
 Return true if the specified port is a multiport.
static boolean isOfTimeType (Parameter param)
 Return whether the given parameter is inferred to a time type.
static boolean isOfTimeType (StateVar state)
 Return whether the given state variable is inferred to a time type.
static boolean isParameterized (StateVar s)
 Report whether the given time state variable is initialized using a parameter or not.
static boolean isSingleLineComment (INode node)
 Return whether node is a multiline comment.
static boolean isTopLevel (NamedInstance<?> instance)
 Return true if the given instance is top-level, i.e., its parent is null.
static boolean isValidTime (Time t)
 Report whether the given time denotes a valid time or not.
static boolean isZero (Expression expr)
 Report whether the given expression is zero or not.
static boolean isZero (String literal)
 Report whether the given literal is zero or not.
static boolean makeFederated (Resource resource)
 Find the main reactor and change it to a federated reactor.
static void overrideParameter (ParameterInstance param, CodeExpr expr)
 Override the parameter initializer with a code expression.
static List< ReactorInstancerecursiveChildren (ReactorInstance r)
static void rerouteViaInstance (List< Pair< Connection, Instantiation > > conns)
 Reroute the given connection to go through the given instantiation.
static Predicate< INode > sameLine (ICompositeNode compNode)
 Return true if the given node starts on the same line as the given other node.
static void setMainName (Resource resource, String name)
 Find the main reactor and set its name if none was defined.
static LinkedHashSet< Reactor > superClasses (Reactor reactor)
 Return all the superclasses of the specified reactor in deepest-first order.
static TargetDecl targetDecl (Model model)
 Returns the target declaration in the given model.
static TargetDecl targetDecl (Resource model)
 Returns the target declaration in the given resource.
static boolean toBoolean (Element e)
 Return a boolean based on the given element.
static Reactor toDefinition (ReactorDecl r)
 If the specified reactor declaration is an import, then return the imported reactor class definition.
static Element toElement (boolean val)
static Element toElement (int val)
static Element toElement (List< String > list)
 Given a list of strings, convert it into its AST representation.
static Element toElement (Map< String, String > map)
 Convert a <String, String> map to key-value pairs in an Element.
static Element toElement (String str)
 Given a single string, convert it into its AST representation.
static Element toElement (TimeValue tv)
 Convert a TimeValue to its AST representation.
static Integer toInteger (Element e)
 Return an integer representation of the given element.
static String toOriginalText (EObject node)
 Translate the given code into its textual representation without CodeMap.Correspondence tags, or return the empty string if node is null.
static String toText (EObject node)
 Translate the given code into its textual representation with CodeMap.Correspondence tags inserted, or return the empty string if node is null.
static TimeValue toTimeValue (Element e)
 Return a time value based on the given element.
static TimeValue toTimeValue (Time e)
 Returns the time value represented by the given AST node.
static int width (WidthSpec spec, List< Instantiation > instantiations)
 Given the width specification of port or instantiation and an (optional) list of nested instantiations, return the width if it can be determined and -1 if not.
static int widthSpecification (Instantiation instantiation)
 Given an instantiation of a reactor or bank of reactors, return the width.

Static Public Attributes

static final LfFactory factory = LfFactory.eINSTANCE
 The Lingua Franca factory for creating new AST nodes.
static final LfPackage featurePackage = LfPackage.eINSTANCE
 The Lingua Franca feature package.

Detailed Description

A helper class for modifying and analyzing the AST.

Author
Marten Lohstroh
Edward A. Lee
Christian Menard

Member Function Documentation

◆ addReactionAttribute()

void org.lflang.ast.ASTUtils.addReactionAttribute ( Reaction reaction,
String name )
static

Add an attribute to the given reaction.

Parameters
reactionThe reaction to add the attribute to.
nameThe name of the attribute to add.

◆ addZeroToLeadingDot()

String org.lflang.ast.ASTUtils.addZeroToLeadingDot ( String literal)
static

If the given string can be recognized as a floating-point number that has a leading decimal point, prepend the string with a zero and return it.

Otherwise, return the original string.

Parameters
literalA string might be recognizable as a floating point number with a leading decimal point.
Returns
an equivalent representation of literal

◆ allActions()

List< Action > org.lflang.ast.ASTUtils.allActions ( Reactor definition)
static

Given a reactor class, return a list of all its actions, which includes actions of base classes that it extends.

This also includes actions in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allConnections()

List< Connection > org.lflang.ast.ASTUtils.allConnections ( Reactor definition)
static

Given a reactor class, return a list of all its connections, which includes connections of base classes that it extends.

This also includes connections in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allElementsOfClass()

static< T extends EObject > Iterable< T > org.lflang.ast.ASTUtils.allElementsOfClass ( Resource resource,
Class< T > elementClass )
static

◆ allFileLevelPreambles()

LinkedHashSet< Preamble > org.lflang.ast.ASTUtils.allFileLevelPreambles ( Reactor reactor)
static

Return all the file-level preambles in the files that define the specified class and its superclasses in deepest-first order.

Duplicates are removed. If there are no file-level preambles, then return an empty list. If a cycle is found, where X extends Y and Y extends X, or if a superclass is declared that is not found, then return null.

Parameters
reactorThe specified reactor.

◆ allInputs()

List< Input > org.lflang.ast.ASTUtils.allInputs ( Reactor definition)
static

Given a reactor class, return a list of all its inputs, which includes inputs of base classes that it extends.

If the base classes include a cycle, where X extends Y and Y extends X, then return only the input defined in the base class. The returned list may be empty.

Parameters
definitionReactor class definition.

◆ allInstantiations()

List< Instantiation > org.lflang.ast.ASTUtils.allInstantiations ( Reactor definition)
static

Given a reactor class, return a list of all its instantiations, which includes instantiations of base classes that it extends.

This also includes instantiations in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allMethods()

List< Method > org.lflang.ast.ASTUtils.allMethods ( Reactor definition)
static

Given a reactor class, return a list of all its methods, which includes methods of base classes that it extends.

Parameters
definitionReactor class definition.

◆ allModes()

List< Mode > org.lflang.ast.ASTUtils.allModes ( Reactor definition)
static

Given a reactor class, returns a list of all its modes, which includes modes of base classes that it extends.

Parameters
definitionReactor class definition.

◆ allNestedClasses()

Stream< Reactor > org.lflang.ast.ASTUtils.allNestedClasses ( Reactor definition)
static

◆ allOutputs()

List< Output > org.lflang.ast.ASTUtils.allOutputs ( Reactor definition)
static

Given a reactor class, return a list of all its outputs, which includes outputs of base classes that it extends.

Parameters
definitionReactor class definition.

◆ allParameters()

List< Parameter > org.lflang.ast.ASTUtils.allParameters ( Reactor definition)
static

Given a reactor class, return a list of all its parameters, which includes parameters of base classes that it extends.

Parameters
definitionReactor class definition.

◆ allPorts()

List< Port > org.lflang.ast.ASTUtils.allPorts ( Reactor definition)
static

A list of all ports of definition, in an unspecified order.

◆ allPreambles()

List< Preamble > org.lflang.ast.ASTUtils.allPreambles ( Reactor definition)
static

Given a reactor class, return a list of all its preambles, which includes preambles of base classes that it extends.

If the base classes include a cycle, where X extends Y and Y extends X, then return only the input defined in the base class. The returned list may be empty.

Parameters
definitionReactor class definition.

◆ allReactions()

List< Reaction > org.lflang.ast.ASTUtils.allReactions ( Reactor definition)
static

Given a reactor class, return a list of all its reactions, which includes reactions of base classes that it extends.

This also includes reactions in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allStateVars()

List< StateVar > org.lflang.ast.ASTUtils.allStateVars ( Reactor definition)
static

Given a reactor class, return a list of all its state variables, which includes state variables of base classes that it extends.

This also includes reactions in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allTimers()

List< Timer > org.lflang.ast.ASTUtils.allTimers ( Reactor definition)
static

Given a reactor class, return a list of all its timers, which includes timers of base classes that it extends.

This also includes reactions in modes, returning a flattened view over all modes.

Parameters
definitionReactor class definition.

◆ allWatchdogs()

List< Watchdog > org.lflang.ast.ASTUtils.allWatchdogs ( Reactor definition)
static

Given a reactor class, return a list of all its watchdogs.

Parameters
definitionReactor class definition
Returns
List<Watchdog>

◆ baseType()

String org.lflang.ast.ASTUtils.baseType ( Type type)
static

Translate the given type into its textual representation, but do not append any array specifications or type arguments.

Parameters
typeAST node to render as string.
Returns
Textual representation of the given argument.

◆ belongsTo() [1/2]

boolean org.lflang.ast.ASTUtils.belongsTo ( EObject eobject,
Instantiation instantiation )
static

Return true if the specified object (a Parameter, Port, Action, or Timer) belongs to the specified instantiation, meaning that it is defined in the reactor class being instantiated or one of its base classes.

Parameters
eobjectThe object.
instantiationThe instantiation.

◆ belongsTo() [2/2]

boolean org.lflang.ast.ASTUtils.belongsTo ( EObject eobject,
Reactor reactor )
static

Return true if the specified object (a Parameter, Port, Action, or Timer) belongs to the specified reactor, meaning that it is defined in reactor class or one of its base classes.

Parameters
eobjectThe object.
reactorThe reactor.

◆ changeTargetName()

boolean org.lflang.ast.ASTUtils.changeTargetName ( Resource resource,
String newTargetName )
static

Change the target name to 'newTargetName'.

For example, change C to CCpp.

◆ collectElements() [1/2]

static< T extends EObject > List< T > org.lflang.ast.ASTUtils.collectElements ( Reactor definition,
EStructuralFeature feature )
static

Collect elements of type T from the class hierarchy and modes defined by a given reactor definition.

Parameters
definitionThe reactor definition.
featureThe structural feature to collect.
<T>The type of elements to collect (e.g., Port, Timer, etc.)
Returns
A list of all elements of type T found

◆ collectElements() [2/2]

static< T extends EObject > List< T > org.lflang.ast.ASTUtils.collectElements ( Reactor definition,
EStructuralFeature feature,
boolean includeSuperClasses,
boolean includeModes )
static

Collect elements of type T contained in given reactor definition, including modes and the class hierarchy defined depending on configuration.

Parameters
definitionThe reactor definition.
featureThe structual model elements to collect.
includeSuperClassesWhether to include elements in super classes.
includeModesWhether to include elements in modes.
<T>The type of elements to collect (e.g., Port, Timer, etc.)
Returns
A list of all elements of type T found

◆ convertToEmptyListIfNull()

static< T > List< T > org.lflang.ast.ASTUtils.convertToEmptyListIfNull ( List< T > list)
static

Returns the list if it is not null.

Otherwise, return an empty list.

◆ createInstantiation()

Instantiation org.lflang.ast.ASTUtils.createInstantiation ( Reactor reactor)
static

Create a new instantiation node with the given reactor as its defining class.

Parameters
reactorThe reactor class to create an instantiation of.

◆ createMainReactorInstance()

ReactorInstance org.lflang.ast.ASTUtils.createMainReactorInstance ( Instantiation mainDef,
List< Reactor > reactors,
MessageReporter messageReporter,
TargetConfig targetConfig )
static

If a main or federated reactor has been declared, create a ReactorInstance for this top level.

This will also assign levels to reactions, then, if the program is federated, perform an AST transformation to disconnect connections between federates.

◆ elementToListOfStrings()

List< String > org.lflang.ast.ASTUtils.elementToListOfStrings ( Element value)
static

Given the right-hand side of a target property, return a list with all the strings that the property lists.

Arrays are traversed, so strings are collected recursively. Empty strings are ignored; they are not added to the list.

Parameters
valueThe right-hand side of a target property.

◆ elementToSingleString()

String org.lflang.ast.ASTUtils.elementToSingleString ( Element e)
static

Given the right-hand side of a target property, return a string that represents the given value/.

If the given value is not a literal or and id (but for instance and array or dict), an empty string is returned. If the element is a string, any quotes are removed.

Parameters
eThe right-hand side of a target property.

◆ elementToStringMaps()

Map< String, String > org.lflang.ast.ASTUtils.elementToStringMaps ( Element value)
static

Convert key-value pairs in an Element to a map, assuming that both the key and the value are strings.

◆ findConflictingConnectionsInModalReactors()

Collection< Connection > org.lflang.ast.ASTUtils.findConflictingConnectionsInModalReactors ( Resource resource)
static

Find connections in the given resource that would be conflicting writes if they were not located in mutually exclusive modes.

Parameters
resourceThe AST.
Returns
a list of connections being able to be transformed

◆ findMainReactor()

Reactor org.lflang.ast.ASTUtils.findMainReactor ( Resource resource)
static

Return the main reactor in the given resource if there is one, null otherwise.

◆ generateVarRef()

String org.lflang.ast.ASTUtils.generateVarRef ( VarRef reference)
static

Generate code for referencing a port, action, or timer.

Parameters
referenceThe reference to the variable.

◆ getAllReactors()

List< Reactor > org.lflang.ast.ASTUtils.getAllReactors ( Resource resource)
static

Get all reactors defined in the given resource.

Parameters
resourcethe resource to extract reactors from
Returns
An iterable over all reactors found in the resource

◆ getDefaultAsTimeValue()

TimeValue org.lflang.ast.ASTUtils.getDefaultAsTimeValue ( Parameter p)
static

If the parameter is of time type, return its default value.

Otherwise, return null.

◆ getDelay()

Long org.lflang.ast.ASTUtils.getDelay ( Expression delay)
static

Return the delay (in nanoseconds) denoted by delay, or null if the delay cannot be determined.

Parameters
delayThe delay to get the time value from.
Returns
The delay time value or null if the delay cannot be determined.

◆ getDelayAsTimeValue()

TimeValue org.lflang.ast.ASTUtils.getDelayAsTimeValue ( Expression delay)
static

Return the delay denoted by delay or null if the delay cannot be determined.

Parameters
delayThe delay to get the time value from.
Returns
The delay time value or null if the delay cannot be determined.

◆ getEnclaves()

Set< Instantiation > org.lflang.ast.ASTUtils.getEnclaves ( Reactor top)
static

Return the set of enclave instantiations in the given reactor definition.

Parameters
topThe reactor definition to search in.
Returns
The set of enclavereactor instantiations within top.

◆ getEnclosingReactor()

Reactor org.lflang.ast.ASTUtils.getEnclosingReactor ( EObject obj)
static

Return the enclosing reactor of an LF EObject in a reactor or mode.

Parameters
objthe LF model element
Returns
the reactor or null

◆ getFederatedReactor()

Optional< Reactor > org.lflang.ast.ASTUtils.getFederatedReactor ( Resource resource)
static

Get the federated reactor defined in the given resource, if there is one.

Parameters
resourcethe resource to extract reactors from
Returns
An Optional reactor that may be present or absent.

◆ getInferredType() [1/5]

InferredType org.lflang.ast.ASTUtils.getInferredType ( Action a)
static

Construct an inferred type from an "action" AST node based on its declared type.

If no type is declared, return the "undefined" type.

Parameters
aAn action to construct an inferred type object for.
Returns
The inferred type, or "undefined" if none was declared.

◆ getInferredType() [2/5]

InferredType org.lflang.ast.ASTUtils.getInferredType ( Parameter p)
static

Given a parameter, return an inferred type.

Only two types can be inferred: "time" and "timeList". Return the "undefined" type if neither can be inferred.

Parameters
pA parameter to infer the type of.
Returns
The inferred type, or "undefined" if none could be inferred.

◆ getInferredType() [3/5]

InferredType org.lflang.ast.ASTUtils.getInferredType ( Port p)
static

Construct an inferred type from a "port" AST node based on its declared type.

If no type is declared, return the "undefined" type.

Parameters
pA port to construct an inferred type object for.
Returns
The inferred type, or "undefined" if none was declared.

◆ getInferredType() [4/5]

InferredType org.lflang.ast.ASTUtils.getInferredType ( StateVar s)
static

Given a state variable, return an inferred type.

Only two types can be inferred: "time" and "timeList". Return the "undefined" type if neither can be inferred.

Parameters
sA state variable to infer the type of.
Returns
The inferred type, or "undefined" if none could be inferred.

◆ getInferredType() [5/5]

InferredType org.lflang.ast.ASTUtils.getInferredType ( Type type,
Initializer init )
static

Return the type of a declaration with the given (nullable) explicit type, and the given (nullable) initializer.

If the explicit type is null, then the type is inferred from the initializer. Only two types can be inferred: "time" and "timeList". Return the "undefined" type if neither can be inferred.

Parameters
typeExplicit type declared on the declaration
initThe initializer expression
Returns
The inferred type, or "undefined" if none could be inferred.

◆ getLiteralTimeValue()

TimeValue org.lflang.ast.ASTUtils.getLiteralTimeValue ( Expression expr)
static

Assuming that the given expression denotes a valid time value, return a time value.

Parameters
exprThe expression to inspect.
Returns
The time value, or null if the expression does not denote a valid time value.

◆ getMainReactor()

Optional< Reactor > org.lflang.ast.ASTUtils.getMainReactor ( Resource resource)
static

Get the main reactor defined in the given resource, if there is one.

Parameters
resourcethe resource to extract reactors from
Returns
An Optional reactor that may be present or absent.

◆ getPrecedingCommentNodes()

Stream< INode > org.lflang.ast.ASTUtils.getPrecedingCommentNodes ( ICompositeNode compNode,
Predicate< INode > filter )
static

Return all single-line or multi-line comments immediately preceding the given EObject.

◆ getPrecedingComments()

Stream< String > org.lflang.ast.ASTUtils.getPrecedingComments ( ICompositeNode compNode,
Predicate< INode > filter )
static

Return all single-line or multi-line comments immediately preceding the given EObject.

◆ getTarget()

Target org.lflang.ast.ASTUtils.getTarget ( EObject object)
static

Return the target of the file in which the given node lives.

◆ getUniqueIdentifier()

String org.lflang.ast.ASTUtils.getUniqueIdentifier ( Reactor reactor,
String name )
static

Produce a unique identifier within a reactor based on a given based name.

If the name has not been used before, it is returned; if has been used, an index is appended that makes the name unique.

Parameters
reactorThe reactor to find a unique identifier within.
nameThe name to base the returned identifier on.

◆ hasMultipleConnections()

boolean org.lflang.ast.ASTUtils.hasMultipleConnections ( Connection connection)
static

Return true if the connection involves multiple ports on the left or right side of the connection, or if the port on the left or right of the connection involves a bank of reactors or a multiport.

Parameters
connectionThe connection.

◆ inferPortWidth()

int org.lflang.ast.ASTUtils.inferPortWidth ( VarRef reference,
Connection connection,
List< Instantiation > instantiations )
static

Infer the width of a port reference in a connection.

The port reference one or two parts, a port and an (optional) container which is an Instantiation that may refer to a bank of reactors. The width will be the product of the bank width and the port width. The returned value will be 1 if the port is not in a bank and is not a multiport.

If the width cannot be determined, this will return -1. The width cannot be determined if the list of instantiations is missing or incomplete.

The instantiations list is as in initialValue. The first element on this list should be the instantiation that contains the specified connection.

Parameters
referenceA port reference.
connectionA connection, or null if not in the context of a connection.
instantiationsThe (optional) list of instantiations.
Returns
The width or -1 if it could not be determined.
Exceptions
IllegalArgumentExceptionIf an instantiation provided is not as given above or if the chain of instantiations is not nested.

◆ initialValue()

Expression org.lflang.ast.ASTUtils.initialValue ( Parameter parameter,
List< Instantiation > instantiations )
static

Given a parameter, return its initial value.

If the instantiations argument is null or an empty list, then the value returned is simply the default value given when the parameter is defined.

If a list of instantiations is given, then the first instantiation is required to be an instantiation of the reactor class that is parameterized by the parameter. I.e.,

     parameter.eContainer == instantiations.get(0).reactorClass

If a second instantiation is given, then it is required to be an instantiation of a reactor class that contains the first instantiation. That is,

     instantiations.get(0).eContainer == instantiations.get(1).reactorClass

More generally, for all 0 <= i < instantiations.size - 1,

     instantiations.get(i).eContainer == instantiations.get(i + 1).reactorClass

If any of these conditions is not satisfied, then an IllegalArgumentException will be thrown.

Note that this chain of reactions cannot be inferred from the parameter because in each of the predicates above, there may be more than one instantiation that can appear on the right hand side of the predicate.

For example, consider the following program:

     reactor A(x:int(1)) {}
     reactor B(y:int(2)) {
         a1 = new A(x = y);
         a2 = new A(x = -1);
     }
     reactor C(z:int(3)) {
         b1 = new B(y = z);
         b2 = new B(y = -2);
     }

Notice that there are a total of four instances of reactor class A. Then

     initialValue(x, null) returns 1
     initialValue(x, [a1]) returns 2
     initialValue(x, [a2]) returns -1
     initialValue(x, [a1, b1]) returns 3
     initialValue(x, [a2, b1]) returns -1
     initialValue(x, [a1, b2]) returns -2
     initialValue(x, [a2, b2]) returns -1

(Actually, in each of the above cases, the returned value is a list with one entry, a Literal, e.g. ["1"]).

There are two instances of reactor class B.

     initialValue(y, null) returns 2
     initialValue(y, [a1]) throws an IllegalArgumentException
     initialValue(y, [b1]) returns 3
     initialValue(y, [b2]) returns -2
Parameters
parameterThe parameter.
instantiationsThe (optional) list of instantiations.
Returns
The value of the parameter.
Exceptions
IllegalArgumentExceptionIf an instantiation provided is not an instantiation of the reactor class that is parameterized by the respective parameter or if the chain of instantiations is not nested.

◆ initialValueInt()

Integer org.lflang.ast.ASTUtils.initialValueInt ( Parameter parameter,
List< Instantiation > instantiations )
static

Given a parameter return its integer value or null if it does not have an integer value.

If the value of the parameter is a list of integers, return the sum of value in the list. The instantiations parameter is as in initialValue.

Parameters
parameterThe parameter.
instantiationsThe (optional) list of instantiations.
Returns
The integer value of the parameter, or null if it does not have an integer value.
Exceptions
IllegalArgumentExceptionIf an instantiation provided is not an instantiation of the reactor class that is parameterized by the respective parameter or if the chain of instantiations is not nested.

◆ isBigInteger()

boolean org.lflang.ast.ASTUtils.isBigInteger ( String literal)
static

Report whether the given string literal is an integer number or not.

Parameters
literalAST node to inspect.
Returns
True if the given value is an integer, false otherwise.

◆ isBoolean()

boolean org.lflang.ast.ASTUtils.isBoolean ( String literal)
static

Report whether the given string literal is a boolean value or not.

Parameters
literalAST node to inspect.
Returns
True if the given value is a boolean, false otherwise.

◆ isComment()

boolean org.lflang.ast.ASTUtils.isComment ( INode node)
static

Return whether node is a comment.

◆ isFloat()

boolean org.lflang.ast.ASTUtils.isFloat ( String literal)
static

Report whether the given string literal is a float value or not.

Parameters
literalAST node to inspect.
Returns
True if the given value is a float, false otherwise.

◆ isGeneric()

boolean org.lflang.ast.ASTUtils.isGeneric ( Reactor r)
static

Check if the reactor class uses generics.

Parameters
rthe reactor to check
Returns
true if the reactor uses generics

◆ isInCode()

boolean org.lflang.ast.ASTUtils.isInCode ( INode node)
static

◆ isInitialized()

boolean org.lflang.ast.ASTUtils.isInitialized ( StateVar v)
static

Report whether a state variable has been initialized or not.

Parameters
vThe state variable to be checked.
Returns
True if the variable was initialized, false otherwise.

◆ isInteger()

boolean org.lflang.ast.ASTUtils.isInteger ( String literal)
static

Report whether the given string literal is an integer number or not.

Parameters
literalAST node to inspect.
Returns
True if the given value is an integer, false otherwise.

◆ isMultilineComment()

boolean org.lflang.ast.ASTUtils.isMultilineComment ( INode node)
static

Return whether node is a multiline comment.

◆ isMultiport()

boolean org.lflang.ast.ASTUtils.isMultiport ( Port port)
static

Return true if the specified port is a multiport.

Parameters
portThe port.
Returns
True if the port is a multiport.

◆ isOfTimeType() [1/2]

boolean org.lflang.ast.ASTUtils.isOfTimeType ( Parameter param)
static

Return whether the given parameter is inferred to a time type.

◆ isOfTimeType() [2/2]

boolean org.lflang.ast.ASTUtils.isOfTimeType ( StateVar state)
static

Return whether the given state variable is inferred to a time type.

◆ isParameterized()

boolean org.lflang.ast.ASTUtils.isParameterized ( StateVar s)
static

Report whether the given time state variable is initialized using a parameter or not.

Parameters
sA state variable.
Returns
True if the argument is initialized using a parameter, false otherwise.

◆ isSingleLineComment()

boolean org.lflang.ast.ASTUtils.isSingleLineComment ( INode node)
static

Return whether node is a multiline comment.

◆ isTopLevel()

boolean org.lflang.ast.ASTUtils.isTopLevel ( NamedInstance<?> instance)
static

Return true if the given instance is top-level, i.e., its parent is null.

Parameters
instanceThe instance to check.

◆ isValidTime()

boolean org.lflang.ast.ASTUtils.isValidTime ( Time t)
static

Report whether the given time denotes a valid time or not.

Parameters
tAST node to inspect.
Returns
True if the argument denotes a valid time, false otherwise.

◆ isZero() [1/2]

boolean org.lflang.ast.ASTUtils.isZero ( Expression expr)
static

Report whether the given expression is zero or not.

Parameters
exprAST node to inspect.
Returns
True if the given value denotes the constant 0, false otherwise.

◆ isZero() [2/2]

boolean org.lflang.ast.ASTUtils.isZero ( String literal)
static

Report whether the given literal is zero or not.

Parameters
literalAST node to inspect.
Returns
True if the given literal denotes the constant 0, false otherwise.

◆ makeFederated()

boolean org.lflang.ast.ASTUtils.makeFederated ( Resource resource)
static

Find the main reactor and change it to a federated reactor.

Return true if the transformation was successful (or the given resource already had a federated reactor); return false otherwise.

◆ overrideParameter()

void org.lflang.ast.ASTUtils.overrideParameter ( ParameterInstance param,
CodeExpr expr )
static

Override the parameter initializer with a code expression.

Parameters
paramThe parameter to override.
exprThe code expression to use as the initializer.

◆ recursiveChildren()

List< ReactorInstance > org.lflang.ast.ASTUtils.recursiveChildren ( ReactorInstance r)
static

◆ rerouteViaInstance()

void org.lflang.ast.ASTUtils.rerouteViaInstance ( List< Pair< Connection, Instantiation > > conns)
static

Reroute the given connection to go through the given instantiation.

For each given pair of a connection and a newly created instantiation, create two connections to reroute specified connection to instead go through the specified instantiation. This is used when code-generating after-delay reactors and enclave connections. This assumes that the specified instantiation has at least one input port and at least one output port and uses the first of such ports. The old connection is removed.

If a connection is iterated, then the downstream new connection is iterated as well.

Parameters
connsThe list of pairs to reroute.

◆ sameLine()

Predicate< INode > org.lflang.ast.ASTUtils.sameLine ( ICompositeNode compNode)
static

Return true if the given node starts on the same line as the given other node.

◆ setMainName()

void org.lflang.ast.ASTUtils.setMainName ( Resource resource,
String name )
static

Find the main reactor and set its name if none was defined.

Parameters
resourceThe resource to find the main reactor in.
nameThe name to set for the main reactor.

◆ superClasses()

LinkedHashSet< Reactor > org.lflang.ast.ASTUtils.superClasses ( Reactor reactor)
static

Return all the superclasses of the specified reactor in deepest-first order.

For example, if A extends B and C, and B and C both extend D, this will return the list [D, B, C, A]. Duplicates are removed. If the specified reactor does not extend any other reactor, then return an empty list. If a cycle is found, where X extends Y and Y extends X, or if a superclass is declared that is not found, then return null.

Parameters
reactorThe specified reactor.

◆ targetDecl() [1/2]

TargetDecl org.lflang.ast.ASTUtils.targetDecl ( Model model)
static

Returns the target declaration in the given model.

Non-null because it would cause a parse error.

◆ targetDecl() [2/2]

TargetDecl org.lflang.ast.ASTUtils.targetDecl ( Resource model)
static

Returns the target declaration in the given resource.

Non-null because it would cause a parse error.

◆ toBoolean()

boolean org.lflang.ast.ASTUtils.toBoolean ( Element e)
static

Return a boolean based on the given element.

Parameters
eThe element to be rendered as a boolean.

◆ toDefinition()

Reactor org.lflang.ast.ASTUtils.toDefinition ( ReactorDecl r)
static

If the specified reactor declaration is an import, then return the imported reactor class definition.

Otherwise, just return the argument.

Parameters
rA Reactor or an ImportedReactor.
Returns
The Reactor class definition or null if no definition is found.

◆ toElement() [1/6]

Element org.lflang.ast.ASTUtils.toElement ( boolean val)
static

◆ toElement() [2/6]

Element org.lflang.ast.ASTUtils.toElement ( int val)
static

◆ toElement() [3/6]

Element org.lflang.ast.ASTUtils.toElement ( List< String > list)
static

Given a list of strings, convert it into its AST representation.

Stores the list in the Array field of the element, unless the list only has one string, in which case it is stored in the Literal field. Returns null if the provided list is empty.

◆ toElement() [4/6]

Element org.lflang.ast.ASTUtils.toElement ( Map< String, String > map)
static

Convert a <String, String> map to key-value pairs in an Element.

◆ toElement() [5/6]

Element org.lflang.ast.ASTUtils.toElement ( String str)
static

Given a single string, convert it into its AST representation.

◆ toElement() [6/6]

Element org.lflang.ast.ASTUtils.toElement ( TimeValue tv)
static

Convert a TimeValue to its AST representation.

The value is type-cast to int in order to fit inside an Element.

◆ toInteger()

Integer org.lflang.ast.ASTUtils.toInteger ( Element e)
static

Return an integer representation of the given element.

Internally, this method uses Integer.decode, so it will also understand hexadecimal, binary, etc.

Parameters
eThe element to be rendered as an integer.

◆ toOriginalText()

String org.lflang.ast.ASTUtils.toOriginalText ( EObject node)
static

Translate the given code into its textual representation without CodeMap.Correspondence tags, or return the empty string if node is null.

This method should be used for analyzing AST nodes in cases where they are easiest to analyze as strings.

Parameters
nodeAST node to render as string.
Returns
Textual representation of the given argument.

◆ toText()

String org.lflang.ast.ASTUtils.toText ( EObject node)
static

Translate the given code into its textual representation with CodeMap.Correspondence tags inserted, or return the empty string if node is null.

This method should be used to generate code.

Parameters
nodeAST node to render as string.
Returns
Textual representation of the given argument.

◆ toTimeValue() [1/2]

TimeValue org.lflang.ast.ASTUtils.toTimeValue ( Element e)
static

Return a time value based on the given element.

Parameters
eThe element to be rendered as a time value.

◆ toTimeValue() [2/2]

TimeValue org.lflang.ast.ASTUtils.toTimeValue ( Time e)
static

Returns the time value represented by the given AST node.

◆ width()

int org.lflang.ast.ASTUtils.width ( WidthSpec spec,
List< Instantiation > instantiations )
static

Given the width specification of port or instantiation and an (optional) list of nested instantiations, return the width if it can be determined and -1 if not.

This will not be able to be determined if either the width is variable (in which case you should use inferPortWidth ) or the list of instantiations is incomplete or missing. If there are parameter references in the width, they are evaluated to the extent possible given the instantiations list.

The instantiations list is as in initialValue. If the spec belongs to an instantiation (for a bank of reactors), then the first element on this list should be the instantiation that contains this instantiation. If the spec belongs to a port, then the first element on the list should be the instantiation of the reactor that contains the port.

Parameters
specThe width specification or null (to return 1).
instantiationsThe (optional) list of instantiations.
Returns
The width, or -1 if the width could not be determined.
Exceptions
IllegalArgumentExceptionIf an instantiation provided is not as given above or if the chain of instantiations is not nested.

◆ widthSpecification()

int org.lflang.ast.ASTUtils.widthSpecification ( Instantiation instantiation)
static

Given an instantiation of a reactor or bank of reactors, return the width.

This will be 1 if this is not a reactor bank. Otherwise, this will attempt to determine the width. If the width is declared as a literal constant, it will return that constant. If the width is specified as a reference to a parameter, this will throw an exception. If the width is variable, this will find connections in the enclosing reactor and attempt to infer the width. If the width cannot be determined, it will throw an exception.

IMPORTANT: This method should not be used you really need to determine the width! It will not evaluate parameter values.

See also
width
Parameters
instantiationA reactor instantiation.
Returns
The width, if it can be determined.

Member Data Documentation

◆ factory

final LfFactory org.lflang.ast.ASTUtils.factory = LfFactory.eINSTANCE
static

The Lingua Franca factory for creating new AST nodes.

◆ featurePackage

final LfPackage org.lflang.ast.ASTUtils.featurePackage = LfPackage.eINSTANCE
static

The Lingua Franca feature package.


The documentation for this class was generated from the following file:
  • /Users/runner/work/lingua-franca/lingua-franca/core/src/main/java/org/lflang/ast/ASTUtils.java