コード例 #1
0
ファイル: FunctionSemantic.cs プロジェクト: ronsaldo/chela
        public override AstNode Visit(AttributeInstance node)
        {
            // Read the attribute type.
            Expression attrExpr = node.GetAttributeExpr();
            if(attrExpr is ObjectReferenceExpression)
            {
                ObjectReferenceExpression refExpr = (ObjectReferenceExpression)attrExpr;
                refExpr.SetAttributeName(true);
            }

            attrExpr.Accept(this);

            // Get his type.
            IChelaType attrType = attrExpr.GetNodeType();
            attrType = ExtractActualType(attrExpr, attrType);
            if(!attrType.IsClass())
                Error(node, "attributes must be classes.");

            // Make sure is derived from the Attribute class.
            Class attributeClass = currentModule.GetAttributeClass();
            Class attrBuilding = (Class)attrType;
            if(!attrBuilding.IsDerivedFrom(attributeClass))
                Error(node, "invalid attribute class.");

            // Store the attribute class.
            node.SetAttributeClass(attrBuilding);

            // Visit the attribute arguments.
            AstNode arg = node.GetArguments();
            List<object> arguments = new List<object> ();
            bool readingProperties = false;
            while(arg != null)
            {
                // Store the attribute type.
                AttributeArgument attrArg = (AttributeArgument)arg;
                attrArg.SetAttributeClass(attrBuilding);

                // Visit the argument.
                arg.Accept(this);

                // Check property/vs constructor argument.
                bool propertyArgument = !string.IsNullOrEmpty(arg.GetName());
                if(propertyArgument)
                {
                    // Start/started reading properties values.
                    readingProperties = true;
                }
                else
                {
                    // Check for unnamed value.
                    if(readingProperties)
                        Error(arg, "expected attribute property value.");

                    // Store the argument.
                    arguments.Add(arg.GetNodeType());
                }

                // Visit the next argument.
                arg = arg.GetNext();
            }

            // Pick the correct attribute constructor.
            FunctionGroup ctorGroup = attrBuilding.GetConstructor();
            Function ctor = PickFunction(node, ctorGroup, false, null, arguments, false);
            if(!ctor.IsMethod())
                Error(node, "invalid constructor."); // Shouldn't reach here.

            // Store the picked constructor.
            node.SetAttributeConstructor((Method)ctor);

            return node;
        }