예제 #1
0
        public override bool VisitTopLevelStructDef([NotNull] GamaParser.TopLevelStructDefContext context)
        {
            var symtype = context.symbolTypePairList();

            if (symtype == null)
            {
                GlobalContext.AddError(new ErrorEmptyStruct(context));
                return(false);
            }
            var structname = context.Symbol().GetText();

            if (NamespaceContext.FindTypeRef(structname) != null)
            {
                GlobalContext.AddError(new ErrorDuplicateType(context));
                return(false);
            }

            var pairs  = symtype.symbolTypePair();
            var fields = new Dictionary <string, GamaFieldRef>(pairs.Length);
            var types  = new LLVMTypeRef[pairs.Length];
            var packed = context.Packed() != null;

            for (int i = 0; i < pairs.Length; i++)
            {
                var p    = pairs[i];
                var name = p.Symbol().GetText();
                if (fields.ContainsKey(name))
                {
                    GlobalContext.AddError(new ErrorDuplicateField(p));
                    return(false);
                }
                var ty = NamespaceContext.FindTypeRefGlobal(p.typeName());
                if (ty == null)
                {
                    GlobalContext.AddError(new ErrorTypeNotFound(p.typeName()));
                    return(false);
                }
                fields[name] = new GamaFieldRef(name, i, ty);
                types[i]     = ty.UnderlyingType;
            }

            LLVMTypeRef nativety;

            unsafe {
                nativety = LLVM.StructCreateNamed(GlobalContext.Context, structname.GetSbytePtr());
                nativety.StructSetBody(types, packed);
            }
            var structty = new GamaTypeRef(structname, nativety);

            structty.Meta.Fields.AddRange(fields.Values);
            NamespaceContext.This.Types.Add(structty);

            return(true);
        }
예제 #2
0
        public override bool VisitTopLevelDelegate([NotNull] GamaParser.TopLevelDelegateContext context)
        {
            var name = context.Symbol().GetText();

            if (NamespaceContext.FindTypeRef(name) != null)
            {
                GlobalContext.AddError(new ErrorDuplicateType(context));
                return(false);
            }

            var retty = InstanceTypes.Void;

            var rettyfqtn = context.fqtn();

            if (rettyfqtn != null)
            {
                retty = NamespaceContext.FindTypeRefGlobal(rettyfqtn);
                if (retty == null)
                {
                    GlobalContext.AddError(new ErrorTypeNotFound(context.fqtn()));
                    return(false);
                }
            }

            var parms       = new GamaTypeRef[0];
            var parmsnative = new LLVMTypeRef[0];
            var parmsctx    = context.fqtnList();

            if (parmsctx != null)
            {
                var fqtns = parmsctx.fqtn();
                parms       = new GamaTypeRef[fqtns.Length];
                parmsnative = new LLVMTypeRef[fqtns.Length];
                for (int i = 0; i < fqtns.Length; i++)
                {
                    var ty = NamespaceContext.FindTypeRefGlobal(fqtns[i]);
                    if (ty == null)
                    {
                        GlobalContext.AddError(new ErrorTypeNotFound(fqtns[i]));
                        return(false);
                    }
                    parms[i]       = ty;
                    parmsnative[i] = ty.UnderlyingType;
                }
            }
            NamespaceContext.This.Types.Add(new GamaFunction(name, retty, parms, LLVMTypeRef.CreateFunction(retty.UnderlyingType, parmsnative)));
            return(true);
        }