Ejemplo n.º 1
0
/*
Returns the fully qualified name of the type.
If the type spec is of a collection type, then ensures that the composite type exists in the runtime,
and if not, creates it.
TODO Handle Maps
TODO Handle nested collection types, like [] [] String

TypeSpec struct {
	Doc            *CommentGroup       // associated documentation; or nil
	Name           *Ident              // type name (or type variable name)
	Type           Expr                // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
	Comment        *CommentGroup       // line comments; or nil
	Params         []*TypeSpec         // Type parameters (egh)
	SuperTypes     []*TypeSpec         // Only valid if this is a type variable (egh)
	CollectionSpec *CollectionTypeSpec // nil or a collection specification
	typ interface{}  // The actual *RType - associated at generation time.
}

type CollectionTypeSpec struct {
	Kind        token.Token
	LDelim      token.Pos
	RDelim      token.Pos
	IsSorting   bool
	IsAscending bool
	OrderFunc   string
}
*/
func (g *Generator) ensureTypeName(typeSpec *ast.TypeSpec) string {

	typ, err := g.Interp.EnsureType(g.packagePath, typeSpec)
	if err != nil {
		// rterr.Stopf("Error in file %s: %s", fileNameRoot +".rel", err.Error())
		rterr.Stop1(g, typeSpec, err)
	}
	if typ.IsPrivate && typ.Package != g.pkg {
		rterr.Stopf1(g, typeSpec, "Type %s is private to its package. Not visible.", typ.Name)
	}
	return typ.Name

	/*
	      var typ *data.RType
	      var err error
	      baseTypeName := g.qualifyTypeName(typeSpec.Name.Name)
	      baseType, baseTypeFound := data.RT.Types[baseTypeName]

	      if ! baseTypeFound {
	         rterr.Stopf("Error in file %s: Type %s not found.", fileNameRoot +".rel", baseTypeName)
	         // panic(fmt.Sprintf("Error in file %s: Type %s not found.", fileNameRoot +".rel", baseTypeName))
	      }

	      if typeSpec.CollectionSpec == nil {
	   	  typ = baseType
	      } else {
	   	    switch typeSpec.CollectionSpec.Kind {
	   	    case token.LIST:
	   			typ, err = data.RT.GetListType(baseType)
	   			if err != nil {
	   			   rterr.Stopf("Error in file %s: %s", fileNameRoot +".rel", err.Error())
	   			}
	   	    case token.SET:
	   			typ, err = data.RT.GetSetType(baseType)
	   			if err != nil {
	   			   rterr.Stopf("Error in file %s: %s", fileNameRoot +".rel", err.Error())
	   			}
	   	    case token.MAP:
	   	       panic("I don't handle Map types yet.")
	   	    }
	   	}
	       return typ.Name
	*/
}
Ejemplo n.º 2
0
func (g *Generator) generateConstants() {

	var packageInitMethod *data.RMethod = nil

	var privateConstPackage *data.RPackage
	for file, fileNameRoot := range g.files {
		g.SetCodeFile(file)

		privateCode := g.isPrivateCodeFile(fileNameRoot)
		if privateCode {
			privateConstPackage = g.pkg
		} else {
			privateConstPackage = nil
		}

		for _, constDeclaration := range file.ConstantDecls {

			if packageInitMethod == nil {
				packageInitMethod = g.generatePackageInitMethod()
				g.th.Push(packageInitMethod)
				g.th.ExecutingMethod = packageInitMethod
				g.th.ExecutingPackage = g.pkg
			}
			constantName := g.qualifyConstName(constDeclaration.Name.Name)

			g.Interp.EvalExpr(g.th, constDeclaration.Value)
			obj := g.th.Pop()

			err := data.RT.CreateConstant(constantName, obj, privateConstPackage)
			if err != nil {
				// panic(err)
				rterr.Stop1(g, constDeclaration, err.Error())
			}
		}
	}
}