public override void CaseAMethodDecl(AMethodDecl node) { if (node.GetInline() == null) return; //Variables marked as out can be made into ref foreach (AALocalDecl formal in node.GetFormals()) { if (formal.GetOut() != null) { formal.SetRef(new TRef("ref")); formal.SetOut(null); } } assignedToLocals.Clear(); base.CaseAMethodDecl(node); foreach (AALocalDecl formal in node.GetFormals()) { if (!assignedToLocals.Contains(formal)) formal.SetRef(new TRef("ref")); } }
public override void CaseAMethodDecl(AMethodDecl node) { InAMethodDecl(node); if (node.GetBlock() != null) { node.GetBlock().Apply(this); } { Object[] temp = new Object[node.GetFormals().Count]; node.GetFormals().CopyTo(temp, 0); for (int i = temp.Length - 1; i >= 0; i--) { ((PLocalDecl)temp[i]).Apply(this); } } if (node.GetName() != null) { node.GetName().Apply(this); } if (node.GetReturnType() != null) { node.GetReturnType().Apply(this); } if (node.GetDelegate() != null) { node.GetDelegate().Apply(this); } if (node.GetInline() != null) { node.GetInline().Apply(this); } if (node.GetNative() != null) { node.GetNative().Apply(this); } if (node.GetStatic() != null) { node.GetStatic().Apply(this); } if (node.GetTrigger() != null) { node.GetTrigger().Apply(this); } if (node.GetVisibilityModifier() != null) { node.GetVisibilityModifier().Apply(this); } OutAMethodDecl(node); }
public override void OutAMethodDecl(AMethodDecl node) { //If void return is missing, insert it. if (node.GetReturnType() is AVoidType && node.GetBlock() != null) { AABlock block = (AABlock)node.GetBlock(); bool insertReturn = false; while (true) { if (block.GetStatements().Count == 0) { insertReturn = true; break; } PStm lastStm = (PStm)block.GetStatements()[block.GetStatements().Count - 1]; if (lastStm is AVoidReturnStm) break; if (lastStm is ABlockStm) { block = (AABlock)((ABlockStm)block.GetStatements()[block.GetStatements().Count - 1]).GetBlock(); continue; } insertReturn = true; break; } if (insertReturn) { block.GetStatements().Add(new AVoidReturnStm(new TReturn("return", block.GetToken().Line, block.GetToken().Pos))); } } //Check if delegate is valid if (node.GetDelegate() != null) { if (node.GetBlock() != null) errors.Add(new ErrorCollection.Error(node.GetDelegate(), currentSourceFile, LocRM.GetString("ErrorText195"))); if (node.GetInline() != null) errors.Add(new ErrorCollection.Error(node.GetDelegate(), currentSourceFile, LocRM.GetString("ErrorText196"))); if (node.GetTrigger() != null) errors.Add(new ErrorCollection.Error(node.GetDelegate(), currentSourceFile, LocRM.GetString("ErrorText197"))); if (node.GetStatic() != null) errors.Add(new ErrorCollection.Error(node.GetDelegate(), currentSourceFile, LocRM.GetString("ErrorText198"))); if (node.GetNative() != null) errors.Add(new ErrorCollection.Error(node.GetDelegate(), currentSourceFile, LocRM.GetString("ErrorText199"))); } //If it's protected, it must be in a struct if (!Util.HasAncestor<AStructDecl>(node)) { if (node.GetVisibilityModifier() is AProtectedVisibilityModifier) errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText200"))); } base.OutAMethodDecl(node); }
private void CheckInvoke(ASimpleInvokeExp node, AMethodDecl target) { if (target.GetInline() != null) { AMethodDecl pMethod = Util.GetAncestor<AMethodDecl>(node); AConstructorDecl pConstructor = Util.GetAncestor<AConstructorDecl>(node); ADeconstructorDecl pDeconstructor = Util.GetAncestor<ADeconstructorDecl>(node); if (pMethod == null && !Util.HasAncestor<AConstructorDecl>(node) && !Util.HasAncestor<ADeconstructorDecl>(node) && !Util.HasAncestor<APropertyDecl>(node)) { errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText131"))); } else if (pMethod != null && !InlineMethodCalls[pMethod].Contains(target)) InlineMethodCalls[pMethod].Add(target); } //For each formal marked as ref or out, the argument must be a non-const variable for (int i = 0; i < node.GetArgs().Count; i++) { AALocalDecl formal = (AALocalDecl)target.GetFormals()[i]; if (formal.GetRef() != null || formal.GetOut() != null) { PExp exp = (PExp)node.GetArgs()[i]; while (true) { PLvalue lvalue; if (exp is ALvalueExp) { lvalue = ((ALvalueExp)exp).GetLvalue(); } else if (exp is AAssignmentExp) { lvalue = ((AAssignmentExp)exp).GetLvalue(); } else { errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText129") + (i + 1) + LocRM.GetString("ErrorText130"))); break; } if (lvalue is ALocalLvalue) { if (data.LocalLinks[(ALocalLvalue)lvalue].GetConst() != null) errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText129") + (i + 1) + LocRM.GetString("ErrorText132"))); break; } if (lvalue is AFieldLvalue) { if (data.FieldLinks[(AFieldLvalue)lvalue].GetConst() != null) errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText129") + (i + 1) + LocRM.GetString("ErrorText132"))); break; } if (lvalue is AStructFieldLvalue) { if (data.StructMethodFieldLinks[(AStructFieldLvalue)lvalue].GetConst() != null) errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText129") + (i + 1) + LocRM.GetString("ErrorText132"))); break; } if (lvalue is AThisLvalue) break; if (lvalue is AStructLvalue) { exp = ((AStructLvalue)lvalue).GetReceiver(); continue; } if (lvalue is AArrayLvalue) { exp = ((AArrayLvalue)lvalue).GetBase(); continue; } if (lvalue is APointerLvalue) { exp = ((APointerLvalue)lvalue).GetBase(); continue; } throw new Exception("Unexpected lvalue"); } } } }
public override void CaseAMethodDecl(AMethodDecl node) { if (node.GetInline() == null && node.GetTrigger() == null && !(data.ConstructorMap.ContainsValue(node) && !inlineConstructors) && node != finalTrans.mainEntry) { CountStatements counter = new CountStatements(); node.Apply(counter); if (counter.Count <= 2) { //Don't inline if it has a recurssive call to itself FindRecurssiveCall recurssiveCallSearcher = new FindRecurssiveCall(node, data); node.Apply(recurssiveCallSearcher); if (!recurssiveCallSearcher.InlinedCallToItself) { node.SetInline(new TInline("inline")); } } } base.CaseAMethodDecl(node); }
public override void CaseAMethodDecl(AMethodDecl node) { if (node.GetInline() != null) { bool canAlwaysInline = true; foreach (KeyValuePair<ASimpleInvokeExp, AMethodDecl> pair in data.SimpleMethodLinks) { if (pair.Value == node && !Util.HasAncestor<AABlock>(pair.Key)) { canAlwaysInline = false; break; } } if (canAlwaysInline) { node.Parent().RemoveChild(node); if (finalTrans.data.Methods.Any(item => item.Decl == node)) finalTrans.data.Methods.Remove(finalTrans.data.Methods.First(item => item.Decl == node)); } } else base.CaseAMethodDecl(node); }