Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions ql/lib/codeql/bicep/ast/Calls.qll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ private import Misc
private import internal.Calls
private import internal.CallExpression
private import internal.LambdaExpression
private import internal.UserDefinedFunction
private import internal.Type

/**
* Represents a callable expression in the AST, such as a function or method call.
Expand Down Expand Up @@ -105,8 +107,8 @@ class CallExpression extends Call instanceof CallExpressionImpl {
* filters, or other functional programming patterns. A lambda expression
* consists of parameters and a body that defines the computation to be performed.
*/
class LambdaExpression extends Expr instanceof LambdaExpressionImpl {
Idents getIdentifier() { none() }
class LambdaExpression extends Callable instanceof LambdaExpressionImpl {
override Idents getIdentifier() { none() }

/**
* Gets the parameters of this lambda expression.
Expand Down Expand Up @@ -137,3 +139,55 @@ class LambdaExpression extends Expr instanceof LambdaExpressionImpl {
*/
int getNumberOfParameters() { result = LambdaExpressionImpl.super.getNumberOfParameters() }
}

/**
* A user-defined function in the AST.
*
* Represents a function declaration in Bicep, which can be called by name
* from other parts of the code. User-defined functions consist of a name,
* parameters, return type, and a body that defines the computation.
*/
class UserDefinedFunction extends Callable instanceof UserDefinedFunctionImpl {
/**
* Gets the identifier of this user-defined function.
*
* @return The identifier node of the function
*/
override Idents getIdentifier() { result = UserDefinedFunctionImpl.super.getIdentifier() }

/**
* Gets the parameters of this user-defined function.
*
* @return The parameters node of the function
*/
Expr getParameters() { result = UserDefinedFunctionImpl.super.getParameters() }

/**
* Gets the parameter at the specified index.
*
* @param n The index of the parameter to retrieve
* @return The parameter at the specified index
*/
Expr getParameter(int n) { result = UserDefinedFunctionImpl.super.getParameter(n) }

/**
* Gets the return type of this user-defined function.
*
* @return The return type node of the function
*/
Type getReturnType() { result = UserDefinedFunctionImpl.super.getReturnType() }

/**
* Gets the body of this user-defined function.
*
* @return The body node of the function
*/
Stmts getBody() { result = UserDefinedFunctionImpl.super.getBody() }

/**
* Gets the number of parameters in this user-defined function.
*
* @return The number of parameters
*/
int getNumberOfParameters() { result = UserDefinedFunctionImpl.super.getNumberOfParameters() }
}
73 changes: 0 additions & 73 deletions ql/lib/codeql/bicep/ast/Stmts.qll
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ private import internal.Parameter
private import internal.Parameters
private import internal.ParameterDeclaration
private import internal.OutputDeclaration
private import internal.UserDefinedFunction
// CFG
private import codeql.bicep.CFG
private import codeql.bicep.controlflow.internal.ControlFlowGraphImpl as CfgImpl
Expand Down Expand Up @@ -262,78 +261,6 @@ class OutputDeclaration extends Stmts instanceof OutputDeclarationImpl {
Expr getValue() { result = OutputDeclarationImpl.super.getValue() }
}

/**
* Represents a user-defined function in the AST.
*
* User-defined functions allow creating reusable pieces of logic in Bicep templates.
* They encapsulate calculations or transformations that can be called from multiple
* places in the template, promoting code reuse and maintainability.
*/
class UserDefinedFunction extends Stmts instanceof UserDefinedFunctionImpl {
/**
* Gets the identifier of the user-defined function.
*
* This is the name token of the function as it appears in the source code.
*
* @return The identifier node of the function
*/
Identifier getIdentifier() { result = UserDefinedFunctionImpl.super.getName() }

/**
* Gets the name of the user-defined function as a string.
*
* This is a convenience method that returns the name from the identifier.
*
* @return The name of the function
*/
string getName() { result = this.getIdentifier().getName() }

/**
* Gets the return type of the user-defined function.
*
* This specifies what kind of value the function returns,
* such as 'string', 'int', 'bool', or more complex types.
*
* @return The return type node of the function
*/
Type getReturnType() { result = UserDefinedFunctionImpl.super.getReturnType() }

/**
* Gets the declared parameters node of the user-defined function.
*
* This contains all the parameter declarations for the function.
*
* @return The parameters node of the function
*/
Parameters getDeclaredParameters() { result = UserDefinedFunctionImpl.super.getParameters() }

/**
* Gets all individual parameters of the user-defined function.
*
* @return All parameter nodes of the function
*/
Parameter getParameters() { result = this.getDeclaredParameters().getParameter(_) }

/**
* Gets the parameter at the specified index.
*
* @param index The index of the parameter to retrieve
* @return The parameter at the specified index
*/
Parameter getParameter(int index) { result = this.getDeclaredParameters().getParameter(index) }

/**
* Gets the body of the user-defined function.
*
* This is the expression that defines the computation performed by the function.
* When the function is called, this expression is evaluated with the provided
* argument values bound to the function parameters.
*
* @return The body expression of the function
*/
Expr getBody() { result = UserDefinedFunctionImpl.super.getBody() }
}

/**
* Represents a function parameter node in the AST.
*
Expand Down
22 changes: 15 additions & 7 deletions ql/lib/codeql/bicep/ast/internal/UserDefinedFunction.qll
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/**
* Internal implementation for UserDefinedFunction
*
* WARNING: this file is generated, do not edit manually
*/

private import AstNodes
private import TreeSitter
private import codeql.bicep.ast.AstNodes
private import Calls
private import Stmts
private import Identifier
private import Stmts
private import Type
private import Parameter
private import Parameters
private import Expr

/**
* A UserDefinedFunction AST Node.
*/
class UserDefinedFunctionImpl extends TUserDefinedFunction, StmtsImpl {
class UserDefinedFunctionImpl extends TUserDefinedFunction, CallableImpl {
private BICEP::UserDefinedFunction ast;

override string getAPrimaryQlClass() { result = "UserDefinedFunction" }
Expand All @@ -26,11 +25,20 @@ class UserDefinedFunctionImpl extends TUserDefinedFunction, StmtsImpl {

override string toString() { result = ast.toString() }

IdentifierImpl getName() { toTreeSitter(result) = ast.getName() }
override IdentifierImpl getIdentifier() { toTreeSitter(result) = ast.getName() }

override ParametersImpl getParameters() { toTreeSitter(result) = ast.getChild(0) }

ParametersImpl getParameters() { toTreeSitter(result) = ast.getChild(0) }
override ParameterImpl getParameter(int n) {
exists(ParametersImpl params |
params = this.getParameters() and
result = params.getParameter(n)
)
}

TypeImpl getReturnType() { toTreeSitter(result) = ast.getReturns() }

StmtsImpl getBody() { toTreeSitter(result) = ast.getChild(1) }
override StmtSequenceImpl getBody() { toTreeSitter(result) = ast.getChild(1) }

override TypeImpl getType() { result = this.getReturnType() }
}
Loading
Loading