Frontend API¶
Frontend analysis components for AST analysis, type inference, and optimization.
AST Analyzer¶
multigen.frontend.ast_analyzer
¶
AST Analysis Framework for Static Python Code.
This module provides comprehensive AST analysis capabilities for the frontend layer, focusing on static analysis of Python code that can be converted to C.
ASTAnalyzer
¶
Bases: NodeVisitor
Enhanced AST analyzer for static Python code analysis.
Source code in src/multigen/frontend/ast_analyzer.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
analyze(source_code)
¶
Analyze Python source code and return analysis results.
Source code in src/multigen/frontend/ast_analyzer.py
visit_AnnAssign(node)
¶
Analyze annotated assignments (variable declarations).
Source code in src/multigen/frontend/ast_analyzer.py
visit_Assign(node)
¶
Analyze regular assignments.
Source code in src/multigen/frontend/ast_analyzer.py
visit_Call(node)
¶
Analyze function calls.
Source code in src/multigen/frontend/ast_analyzer.py
visit_For(node)
¶
Analyze for loops.
Source code in src/multigen/frontend/ast_analyzer.py
visit_FunctionDef(node)
¶
Analyze function definitions.
Source code in src/multigen/frontend/ast_analyzer.py
visit_If(node)
¶
visit_Name(node)
¶
Analyze name references.
Source code in src/multigen/frontend/ast_analyzer.py
visit_Return(node)
¶
AnalysisResult
dataclass
¶
Result of AST analysis.
Source code in src/multigen/frontend/ast_analyzer.py
FunctionInfo
dataclass
¶
Information about a function in static analysis.
Source code in src/multigen/frontend/ast_analyzer.py
NodeType
¶
Bases: Enum
Types of AST nodes we can analyze.
Source code in src/multigen/frontend/ast_analyzer.py
StaticComplexity
¶
Bases: Enum
Complexity levels for static analysis.
Source code in src/multigen/frontend/ast_analyzer.py
TypeInfo
dataclass
¶
Information about a type in the static analysis.
Source code in src/multigen/frontend/ast_analyzer.py
VariableInfo
dataclass
¶
Information about a variable in static analysis.
Source code in src/multigen/frontend/ast_analyzer.py
analyze_python_code(source_code)
¶
analyze_python_file(file_path)
¶
Type Inference¶
multigen.frontend.type_inference
¶
Type Inference System for Static Python Analysis.
This module provides advanced type inference capabilities that go beyond simple type annotations to infer types from context, usage patterns, and static analysis.
InferenceMethod
¶
Bases: Enum
Methods used for type inference.
Source code in src/multigen/frontend/type_inference.py
InferenceResult
dataclass
¶
Result of type inference for a single expression.
Source code in src/multigen/frontend/type_inference.py
TypeConstraint
dataclass
¶
A constraint on a type based on usage.
Source code in src/multigen/frontend/type_inference.py
TypeInferenceEngine
¶
Advanced type inference engine for static Python code.
Source code in src/multigen/frontend/type_inference.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
analyze_function_signature(func_node)
¶
Analyze and infer types for a complete function signature.
Source code in src/multigen/frontend/type_inference.py
analyze_function_signature_enhanced(func_node)
¶
Enhanced function analysis with optional flow-sensitive inference.
Source code in src/multigen/frontend/type_inference.py
infer_expression_type(node, context)
¶
Infer the type of an expression.
Source code in src/multigen/frontend/type_inference.py
Intelligence Layer¶
Base Classes¶
multigen.frontend.base
¶
Base classes for the Intelligence Layer analyzers, optimizers, and verifiers.
AnalysisContext
dataclass
¶
Context information for analysis operations.
Source code in src/multigen/frontend/base.py
AnalysisLevel
¶
AnalysisReport
dataclass
¶
Base class for analysis reports.
Source code in src/multigen/frontend/base.py
BaseAnalyzer
¶
Bases: ABC
Base class for all analyzers in the intelligence layer.
Source code in src/multigen/frontend/base.py
analyze(context)
abstractmethod
¶
Perform analysis on the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The analysis context containing code and metadata |
required |
Returns:
| Type | Description |
|---|---|
AnalysisReport
|
AnalysisReport containing the results of the analysis |
Source code in src/multigen/frontend/base.py
can_analyze(context)
¶
Check if this analyzer can handle the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The analysis context to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this analyzer can process the context |
Source code in src/multigen/frontend/base.py
BaseOptimizer
¶
Bases: ABC
Base class for all optimizers in the intelligence layer.
Source code in src/multigen/frontend/base.py
can_optimize(context)
¶
Check if this optimizer can handle the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The analysis context to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this optimizer can process the context |
Source code in src/multigen/frontend/base.py
disable()
¶
enable()
¶
optimize(context)
abstractmethod
¶
Perform optimization on the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The analysis context to optimize |
required |
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult containing the optimized representation |
Source code in src/multigen/frontend/base.py
BaseVerifier
¶
Bases: ABC
Base class for all verifiers in the intelligence layer.
Source code in src/multigen/frontend/base.py
verify(context, optimized_result)
abstractmethod
¶
Verify the correctness of an optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The original analysis context |
required |
optimized_result
|
OptimizationResult
|
The result of optimization to verify |
required |
Returns:
| Type | Description |
|---|---|
VerificationResult
|
VerificationResult containing verification status |
Source code in src/multigen/frontend/base.py
IntelligencePipeline
¶
Main pipeline for coordinating analyzers, optimizers, and verifiers.
Source code in src/multigen/frontend/base.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
add_analyzer(analyzer)
¶
add_optimizer(optimizer)
¶
add_verifier(verifier)
¶
process(context)
¶
Process the context through the entire intelligence pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
The analysis context to process |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing all analysis, optimization, and verification results |
Source code in src/multigen/frontend/base.py
OptimizationLevel
¶
OptimizationResult
dataclass
¶
Result of an optimization operation.
Source code in src/multigen/frontend/base.py
is_safe()
¶
VerificationResult
dataclass
¶
Result of a verification operation.
Source code in src/multigen/frontend/base.py
Example Usage¶
Using the AST analyzer:
from multigen.frontend.ast_analyzer import ASTAnalyzer
import ast
code = "def foo(x: int) -> int: return x * 2"
tree = ast.parse(code)
analyzer = ASTAnalyzer()
result = analyzer.analyze(tree, code)
print(f"Functions: {result.functions}")
print(f"Type annotations: {result.type_annotations}")
Using the intelligence pipeline:
from multigen.frontend.base import (
IntelligencePipeline,
AnalysisContext,
)
import ast
pipeline = IntelligencePipeline()
code = "def example(): pass"
tree = ast.parse(code)
context = AnalysisContext(source_code=code, ast_node=tree)
results = pipeline.process(context)
See Also¶
- Verification API -- Formal verification APIs
- Pipeline API -- Main pipeline API