AddOverload ensures that the new overload does not collide with an existing overload signature; however, if the function signatures are identical, the implementation may be rewritten as its difficult to compare functions by object identity.
(overload *OverloadDecl)
| 266 | // however, if the function signatures are identical, the implementation may be rewritten as its |
| 267 | // difficult to compare functions by object identity. |
| 268 | func (f *FunctionDecl) AddOverload(overload *OverloadDecl) error { |
| 269 | if f == nil { |
| 270 | return fmt.Errorf("nil function cannot add overload: %s", overload.ID()) |
| 271 | } |
| 272 | if overload == nil { |
| 273 | return fmt.Errorf("cannot add nil overload to funciton: %s", f.Name()) |
| 274 | } |
| 275 | for oID, o := range f.overloads { |
| 276 | if oID != overload.ID() && o.SignatureOverlaps(overload) { |
| 277 | return fmt.Errorf("overload signature collision in function %s: %s collides with %s", f.Name(), oID, overload.ID()) |
| 278 | } |
| 279 | if oID == overload.ID() { |
| 280 | if o.SignatureEquals(overload) && o.IsNonStrict() == overload.IsNonStrict() { |
| 281 | // Allow redefinition of an overload implementation so long as the signatures match. |
| 282 | if overload.HasBinding() { |
| 283 | f.overloads[oID] = overload |
| 284 | for i, decl := range f.overloadDecls { |
| 285 | if decl.ID() == oID { |
| 286 | f.overloadDecls[i] = overload |
| 287 | break |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | // Allow redefinition of the doc string. |
| 292 | if len(overload.doc) != 0 && o.doc != overload.doc { |
| 293 | o.doc = overload.doc |
| 294 | } |
| 295 | return nil |
| 296 | } |
| 297 | return fmt.Errorf("overload redefinition in function. %s: %s has multiple definitions", f.Name(), oID) |
| 298 | } |
| 299 | if overload.HasLateBinding() != o.HasLateBinding() { |
| 300 | return fmt.Errorf("overload with late binding cannot be added to function %s: cannot mix late and non-late bindings", f.Name()) |
| 301 | } |
| 302 | } |
| 303 | f.overloadOrdinals = append(f.overloadOrdinals, overload.ID()) |
| 304 | f.overloads[overload.ID()] = overload |
| 305 | f.overloadDecls = append(f.overloadDecls, overload) |
| 306 | return nil |
| 307 | } |
| 308 | |
| 309 | // OverloadDecls returns the overload declarations in the order in which they were declared. |
| 310 | func (f *FunctionDecl) OverloadDecls() []*OverloadDecl { |