Exemplo n.º 1
0
 public override void CaseASharpCastExp(ASharpCastExp node)
 {
     InASharpCastExp(node);
     if (node.GetExp() != null)
     {
         node.GetExp().Apply(this);
     }
     if (node.GetType() != null)
     {
         node.GetType().Apply(this);
     }
     if (node.GetToken() != null)
     {
         node.GetToken().Apply(this);
     }
     OutASharpCastExp(node);
 }
        public override void OutASharpCastExp(ASharpCastExp node)
        {
            PType fromType = data.ExpTypes[node.GetExp()];
            PType toType = node.GetType();
            //Valid from pointer to int/string and from int/string to pointer
            if (fromType is APointerType)
            {
                APointerType aFromType = (APointerType) fromType;

                if (toType != null && !(toType is ANamedType))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText115")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }
                ANamedType aToType = toType == null ? null : (ANamedType) toType;
                if (toType != null && !aToType.IsPrimitive("int") && !aToType.IsPrimitive("string"))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText114")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }
                if (aFromType.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType) aFromType.GetType()))
                {
                    AStructDecl str = data.StructTypeLinks[(ANamedType) aFromType.GetType()];
                    if (toType != null && (str.GetDimention() == null) != (aToType.IsPrimitive("string")))
                    {
                        errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                             LocRM.GetString("ErrorText116") +
                                                             (str.GetDimention() == null ? "string." : "int.")));
                        throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                    }
                    data.ExpTypes[node.GetExp()] = new ANamedType(new TIdentifier(str.GetDimention() == null ? "string" : "int"), null);
                    node.ReplaceBy(node.GetExp());
                    return;
                }
                //Look for enrichments
                List<IList> visibleDecls = Util.GetVisibleDecls(node, true);
                foreach (IList declList in visibleDecls)
                {
                    foreach (PDecl decl in declList)
                    {
                        if (decl is AEnrichmentDecl)
                        {
                            AEnrichmentDecl enrichment = (AEnrichmentDecl)decl;
                            if (!Util.TypesEqual(aFromType.GetType(), enrichment.GetType(), data))
                                continue;
                            if (toType != null && (enrichment.GetDimention() == null) != (aToType.IsPrimitive("string")))
                            {
                                errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                     LocRM.GetString("ErrorText116") +
                                                                     (enrichment.GetDimention() == null
                                                                          ? "string."
                                                                          : "int.")));
                                throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                            }
                            data.ExpTypes[node.GetExp()] = new ANamedType(new TIdentifier(enrichment.GetDimention() == null ? "string" : "int"), null);
                            node.ReplaceBy(node.GetExp());
                            return;
                        }
                    }
                }

                if (toType != null && aToType.IsPrimitive("int"))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                         LocRM.GetString("ErrorText117")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }

                data.ExpTypes[node.GetExp()] = new ANamedType(new TIdentifier("string"), null);
                node.ReplaceBy(node.GetExp());
                return;
            }
            if (fromType is ANamedType)
            {
                ANamedType aFromType = (ANamedType) fromType;
                if (!(toType is APointerType))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText118")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }
                APointerType aToType = (APointerType) toType;
                if (!aFromType.IsPrimitive("int") && !aFromType.IsPrimitive("string"))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText119")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }
                if (aToType.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)aToType.GetType()))
                {
                    AStructDecl str = data.StructTypeLinks[(ANamedType)aToType.GetType()];
                    if ((str.GetDimention() == null) != (aFromType.IsPrimitive("string")))
                    {
                        errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                             LocRM.GetString("ErrorText120") +
                                                             (str.GetDimention() == null ? "string." : "int.")));
                        throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                    }
                    data.ExpTypes[node.GetExp()] = aToType;
                    node.ReplaceBy(node.GetExp());
                    return;
                }
                //Look for enrichments
                List<IList> visibleDecls = Util.GetVisibleDecls(node, true);
                foreach (IList declList in visibleDecls)
                {
                    foreach (PDecl decl in declList)
                    {
                        if (decl is AEnrichmentDecl)
                        {
                            AEnrichmentDecl enrichment = (AEnrichmentDecl)decl;
                            if (!Util.TypesEqual(aToType.GetType(), enrichment.GetType(), data))
                                continue;
                            if ((enrichment.GetDimention() == null) != (aFromType.IsPrimitive("string")))
                            {
                                errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                     LocRM.GetString("ErrorText120") +
                                                                     (enrichment.GetDimention() == null
                                                                          ? "string."
                                                                          : "int.")));
                                throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                            }
                            data.ExpTypes[node.GetExp()] = aToType;
                            node.ReplaceBy(node.GetExp());
                            return;
                        }
                    }
                }
                if (aFromType.IsPrimitive("int"))
                {
                    errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                         LocRM.GetString("ErrorText121")));
                    throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
                }
                data.ExpTypes[node.GetExp()] = aToType;
                node.ReplaceBy(node.GetExp());
                return;
            }
            errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText118")));
            throw new ParserException(node.GetToken(), "TypeChecking.OutASharpCastExp");
        }