예제 #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
 /// <summary>
 /// Visit a parse tree produced by <see cref="GamaParser.topLevelStructDef"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitTopLevelStructDef([NotNull] GamaParser.TopLevelStructDefContext context)
 {
     return(VisitChildren(context));
 }
예제 #3
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="GamaParser.topLevelStructDef"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitTopLevelStructDef([NotNull] GamaParser.TopLevelStructDefContext context)
 {
 }