当前位置: 首页>>代码示例>>C#>>正文


C# TypeNode.SetOtherType方法代码示例

本文整理汇总了C#中TypeNode.SetOtherType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeNode.SetOtherType方法的具体用法?C# TypeNode.SetOtherType怎么用?C# TypeNode.SetOtherType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TypeNode的用法示例。


在下文中一共展示了TypeNode.SetOtherType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Visit

        public override AstNode Visit(FunctionPrototype node)
        {
            MemberFlags callingConvention = node.GetFlags() & MemberFlags.LanguageMask;
            bool isCdecl = callingConvention == MemberFlags.Cdecl;
            bool isUnsafe = (node.GetFlags() & MemberFlags.SecurityMask) == MemberFlags.Unsafe;

            // Use the unsafe scope.
            if(isUnsafe)
                PushUnsafe();

            // Generate a name for explicit contracts.
            if(node.GetNameExpression() != null)
                node.SetName(GenSym());

            // Find an existing group.
            ScopeMember oldGroup = currentContainer.FindMember(node.GetName());
            if(oldGroup != null && (oldGroup.IsType() || !oldGroup.IsFunctionGroup()))
            {
                if(oldGroup.IsFunction() && isCdecl)
                {
                    // TODO: Add additional checks.
                    Function oldDefinition = (Function)oldGroup;
                    node.SetFunction(oldDefinition);
                    node.SetNodeType(oldDefinition.GetFunctionType());
                    oldGroup = null;
                    return node;
                }
                else
                    Error(node, "cannot override something without a function group.");
            }

            FunctionGroup functionGroup = null;
            if(oldGroup != null) // Cast the group member.
                functionGroup = (FunctionGroup)oldGroup;

            // Make sure the destructor name is correct.
            if(node.GetDestructorName() != null)
            {
                // Get the building scope.
                Scope buildingScope = currentScope;
                while(buildingScope != null && (buildingScope.IsFunction() || buildingScope.IsPseudoScope() || buildingScope.IsLexicalScope()))
                    buildingScope = buildingScope.GetParentScope();

                // Make sure we are in a class.
                if(buildingScope == null || !buildingScope.IsClass())
                    Error(node, "only classes can have finalizers.");

                // Make sure the destructor and class name are the same.
                if(node.GetDestructorName() != buildingScope.GetName())
                    Error(node, "finalizers must have the same name as their class.");
            }

            // Read the instance flag.
            MemberFlags instanceFlags = node.GetFlags() & MemberFlags.InstanceMask;

            // Don't allow overloading with cdecl.
            if(isCdecl && functionGroup != null)
                Error(node, "overloading and cdecl are incompatible.");

            // Make sure static constructor doesn't have modifiers.
            if(instanceFlags == MemberFlags.StaticConstructor &&
               node.GetFlags() != MemberFlags.StaticConstructor)
                Error(node, "static constructors cannot have modifiers.");

            // Static constructors cannot have paraemeters.
            if(instanceFlags == MemberFlags.StaticConstructor &&
               node.GetArguments() != null)
                Error(node, "static constructors cannot have parameters.");

            // Add the this argument.
            if(currentContainer.IsStructure() || currentContainer.IsClass() || currentContainer.IsInterface())
            {
                // Cast the scope.
                Structure building = (Structure)currentContainer;

                // Add the this argument.
                if(instanceFlags != MemberFlags.Static &&
                   instanceFlags != MemberFlags.StaticConstructor)
                {
                    // Instance the building.
                    building = building.GetSelfInstance();

                    // Use the structure type.
                    TypeNode thisType = new TypeNode(TypeKind.Reference, node.GetPosition());
                    thisType.SetOtherType(building);

                    // Create the this argument.
                    FunctionArgument thisArg = new FunctionArgument(thisType, "this",
                                                                    node.GetPosition());

                    // Add to the begin of the argument list.
                    thisArg.SetNext(node.GetArguments());
                    node.SetArguments(thisArg);
                }
            }

            // Parse the generic signature.
            GenericSignature genericSign = node.GetGenericSignature();
            GenericPrototype genProto = null;
            PseudoScope protoScope = null;
//.........这里部分代码省略.........
开发者ID:ronsaldo,项目名称:chela,代码行数:101,代码来源:ModuleObjectDeclarator.cs

示例2: Visit

        public override AstNode Visit(ForEachStatement node)
        {
            // Create the foreach lexical scope.
            LexicalScope scope = CreateLexicalScope(node);
            node.SetScope(scope);

            // Push the lexical scope.
            PushScope(scope);

            // Get the element type expression.
            Expression typeExpr = node.GetTypeExpression();

            // Visit the container expression.
            Expression containerExpr = node.GetContainerExpression();
            containerExpr.Accept(this);

            // Get the container expression type.
            IChelaType containerType = containerExpr.GetNodeType();
            if(!containerType.IsReference())
                Error(containerExpr, "expected an object reference.");

            // Remove type references.
            containerType = DeReferenceType(containerType);
            if(containerType.IsReference())
                containerType = DeReferenceType(containerType);

            // Use primitives associated types.
            if(!containerType.IsStructure() && !containerType.IsClass() && !containerType.IsInterface())
            {
                IChelaType assoc = currentModule.GetAssociatedClass(containerType);
                if(assoc == null)
                    Error(containerExpr, "cannot iterate a container of type {0}", assoc.GetFullName());
                containerType = assoc;
            }

            // Get the GetEnumerator method group.
            Structure building = (Structure)containerType;
            FunctionGroup getEnumeratorGroup = building.FindMemberRecursive("GetEnumerator") as FunctionGroup;
            if(getEnumeratorGroup == null)
                Error(containerExpr, "cannot find GetEnumerator method in {0}", containerType.GetName());

            // Use the first no static member.
            Function getEnumeratorFunction = null;
            foreach(FunctionGroupName gname in getEnumeratorGroup.GetFunctions())
            {
                // Ignore static functions.
                if(gname.IsStatic())
                    continue;

                // Only one argument is supported.
                if(gname.GetFunctionType().GetArgumentCount() == 1)
                {
                    getEnumeratorFunction = gname.GetFunction();
                    break;
                }
            }

            // Make sure the function was found.
            if(getEnumeratorFunction == null)
                Error(containerExpr, "{0} doesn't have a no static GetEnumerator() method.", containerType.GetName());

            // Get the enumerator type.
            TokenPosition position = node.GetPosition();
            TypeNode enumeratorTypeNode = new TypeNode(TypeKind.Other, position);
            enumeratorTypeNode.SetOtherType(getEnumeratorFunction.GetFunctionType().GetReturnType());

            // Get the numerator.
            Expression enumeratorExpr = new CallExpression(
                new MemberAccess(node.GetContainerExpression(), "GetEnumerator", position),
                null, position);

            // Create a variable for the enumerator.
            string enumeratorName = GenSym();
            LocalVariablesDeclaration declEnum = new LocalVariablesDeclaration(enumeratorTypeNode,
                    new VariableDeclaration(enumeratorName, enumeratorExpr, position), position);

            // Create a variable for the current object.
            LocalVariablesDeclaration currentDecl =
                new LocalVariablesDeclaration(node.GetTypeExpression(),
                    new VariableDeclaration(node.GetName(), null, position), position);
            declEnum.SetNext(currentDecl);

            // Get the current member.
            Expression currentProp = new MemberAccess(new VariableReference(enumeratorName, position),
                    "Current", position);

            // Create the while job.
            AstNode whileJob = new ExpressionStatement(
                new AssignmentExpression(new VariableReference(node.GetName(), position),
                    new CastOperation(typeExpr, currentProp, position), position),
                position);
            whileJob.SetNext(node.GetChildren());

            // Create the loop with MoveNext condition.
            Expression moveNext = new CallExpression(
                new MemberAccess(new VariableReference(enumeratorName, position), "MoveNext", position),
                null, position);
            WhileStatement whileStmnt = new WhileStatement(moveNext, whileJob, position);
            currentDecl.SetNext(whileStmnt);

//.........这里部分代码省略.........
开发者ID:ronsaldo,项目名称:chela,代码行数:101,代码来源:FunctionSemantic.cs


注:本文中的TypeNode.SetOtherType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。