From 27dd95c8f9d9ac51680398574e6301d0bac3267b Mon Sep 17 00:00:00 2001 From: yt2b Date: Sun, 27 Nov 2022 23:36:25 +0900 Subject: [PATCH] Add SymbolFlags --- codegen/src/symboltable.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/codegen/src/symboltable.rs b/codegen/src/symboltable.rs index b100a9e..24ee5ea 100644 --- a/codegen/src/symboltable.rs +++ b/codegen/src/symboltable.rs @@ -11,6 +11,7 @@ use crate::{ error::{CodegenError, CodegenErrorType}, IndexMap, }; +use bitflags::bitflags; use rustpython_ast as ast; use rustpython_compiler_core::Location; use std::{borrow::Cow, fmt}; @@ -94,6 +95,33 @@ pub enum SymbolScope { Cell, } +bitflags! { + pub struct SymbolFlags: u16 { + const REFERENCED = 0x001; + const ASSIGNED = 0x002; + const PARAMETER = 0x004; + const ANNOTATED = 0x008; + const IMPORTED = 0x010; + const NONLOCAL = 0x020; + // indicates if the symbol gets a value assigned by a named expression in a comprehension + // this is required to correct the scope in the analysis. + const ASSIGNED_IN_COMPREHENSION = 0x040; + // indicates that the symbol is used a bound iterator variable. We distinguish this case + // from normal assignment to detect unallowed re-assignment to iterator variables. + const ITER = 0x080; + /// indicates that the symbol is a free variable in a class method from the scope that the + /// class is defined in, e.g.: + /// ```python + /// def foo(x): + /// class A: + /// def method(self): + /// return x // is_free_class + /// ``` + const FREE_CLASS = 0x100; + const BOUND = Self::ASSIGNED.bits | Self::PARAMETER.bits | Self::IMPORTED.bits | Self::ITER.bits; + } +} + /// A single symbol in a table. Has various properties such as the scope /// of the symbol, and also the various uses of the symbol. #[derive(Debug, Clone)]