public override AstNode Visit(EnumDefinition node) { // Prevent redefinition. ScopeMember old = currentContainer.FindType(node.GetName(), GenericPrototype.Empty); if(old != null) Error(node, "trying to redefine a enum."); // Create the structure Structure building = new Structure(node.GetName(), node.GetFlags(), currentContainer); building.Position = node.GetPosition(); node.SetStructure(building); // Add into the current scope. if(currentContainer.IsNamespace()) { Namespace space = (Namespace)currentContainer; space.AddType(building); } else if(currentContainer.IsStructure() || currentContainer.IsInterface() || currentContainer.IsClass()) { Structure parent = (Structure)currentContainer; parent.AddType(building); } else { Error(node, "unexpected place for a structure."); } return node; }
public override AstNode Visit(EnumDefinition node) { // Push the scope. PushScope(node.GetScope()); // Use a default type of const int. IChelaType baseType = ChelaType.GetIntType(); IChelaType enumType = ConstantType.Create(baseType); // Declare the value field. Structure building = node.GetStructure(); FieldVariable valueField = new FieldVariable("m_value", MemberFlags.Public, baseType, building); building.AddField(valueField); // Used for implicit prev + 1. IChelaType enumConst = ConstantType.Create(building); TypeNode enumIntExpr = new TypeNode(TypeKind.Other, node.GetPosition()); enumIntExpr.SetOtherType(enumType); // Visit the constant defininitions. EnumConstantDefinition prev = null; AstNode child = node.GetChildren(); while(child != null) { // Cast the child. EnumConstantDefinition constDef = (EnumConstantDefinition)child; // Set the child type. constDef.SetCoercionType(enumType); constDef.SetNodeType(enumConst); // If there isn't a constant expression, use previous + 1 or 0. if(constDef.GetValue() == null) { TokenPosition constPos = constDef.GetPosition(); if(prev != null) { // Previous + 1 Expression prevExpr = new CastOperation(enumIntExpr, new VariableReference(prev.GetName(), constPos), constPos); Expression implicitVal = new BinaryOperation(BinaryOperation.OpAdd, prevExpr, new ByteConstant(1, constPos), constPos); constDef.SetValue(implicitVal); } else { // First element is 0 constDef.SetValue(new ByteConstant(0, constPos)); } } // Visit the constant definition. constDef.Accept(this); // Process the next constant. prev = constDef; child = child.GetNext(); } // Restore the scope. PopScope(); return node; }