view ir/irstruct.h @ 1117:4c20fcc4252b

Fun with parameter attributes: For several of the "synthetic" parameters added to D functions, we can apply noalias and nocapture. They are sret parameters, 'nest' pointers passed to nested functions, and _argptr: Nocapture: - Sret and nest are nocapture because they don't represent D-level variables, and thus the callee can't (validly) obtain a pointer to them, let alone keep it around after it returns. - _argptr is nocapture because although the callee has access to it as a pointer, that pointer is invalidated when it returns. All three are noalias because they're function-local variables - Sret and _argptr are noalias because they're freshly alloca'd memory only used for a single function call that's not allowed to keep an aliasing pointer to it around (since the parameter is nocapture). - 'Nest' is noalias because the callee only ever has access to one such pointer per parent function, and every parent function has a different one. This commit also ensures attributes set on sret, _arguments and _argptr are propagated to calls to such functions. It also adds one exception to the general rule that attributes on function types should propagate to calls: the type of a delegate's function pointer has a 'nest' parameter, but this can either be a true 'nest' (for delegates to nested functions) or a 'this' (for delegates to member functions). Since 'this' is neither noalias nor nocapture, and there's generally no way to tell which one it is, we remove these attributes at the call site if the callee is a delegate.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 14 Mar 2009 22:15:31 +0100
parents 1714836f2c0b
children 5ebe8224988b
line wrap: on
line source

#ifndef LDC_IR_IRSTRUCT_H
#define LDC_IR_IRSTRUCT_H

#include "ir/ir.h"

#include <vector>
#include <map>

struct IrInterface;

void addZeros(std::vector<const llvm::Type*>& inits, size_t pos, size_t offset);
void addZeros(std::vector<llvm::Constant*>& inits, size_t pos, size_t offset);
void addZeros(std::vector<llvm::Value*>& inits, size_t pos, size_t offset);

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

// represents a struct or class
// it is used during codegen to hold all the vital info we need
struct IrStruct : IrBase
{
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    typedef std::vector<VarDeclaration*> VarDeclVector;

    typedef std::map<ClassDeclaration*, IrInterface*>   InterfaceMap;
    typedef InterfaceMap::iterator                      InterfaceMapIter;

    typedef std::vector<IrInterface*> InterfaceVector;
    typedef InterfaceVector::iterator InterfaceVectorIter;

    // vector of LLVM types
    typedef std::vector<const llvm::Type*> TypeVector;

    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    // Anon represents an anonymous struct union block inside an aggregate
    // during LLVM type construction.
    struct Anon
    {
        bool isunion;
        Anon* parent;

        TypeVector types;

        Anon(bool IsUnion, Anon* par) : isunion(IsUnion), parent(par) {}
    };

    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    /// ctor
    IrStruct(AggregateDeclaration* agg);

    /// dtor
    virtual ~IrStruct();

    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    /// push an anonymous struct/union
    void pushAnon(bool isunion);

    /// pops an anonymous struct/union
    void popAnon();

    /// adds field
    void addVar(VarDeclaration* var);

    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    /// build the aggr type
    const LLType* build();

    /// put the aggr initializers in a vector
    void buildDefaultConstInit(std::vector<llvm::Constant*>& inits);

    /// ditto - but also builds the constant struct, for convenience
    LLConstant* buildDefaultConstInit();

    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////

    // the D aggregate
    AggregateDeclaration* aggrdecl;

    // vector of VarDeclarations in this aggregate
    VarDeclVector varDecls;

    // vector of VarDeclarations that contribute to the default initializer
    VarDeclVector defVars;

    // true if the default initializer has been built
    bool defaultFound;

    // top element
    Anon* anon;

    // toplevel types in this aggr
    TypeVector types;

    // current index
    // always the same as types.size()
    size_t index; 

    // aggregate D type
    Type* type;

    // class vtable type
    llvm::PATypeHolder vtblTy;
    llvm::PATypeHolder vtblInitTy;

    // initializer type opaque (type of global matches initializer, not formal type)
    llvm::PATypeHolder initOpaque;
    llvm::PATypeHolder classInfoOpaque;

    // map/vector of interfaces implemented
    InterfaceMap interfaceMap;
    InterfaceVector interfaceVec;

    // interface info array global
    LLGlobalVariable* interfaceInfos;

    // ...
    bool defined;
    bool constinited;

    // vtbl global and initializer
    LLGlobalVariable* vtbl;
    LLConstant* constVtbl;

    // static initializers global and constant
    LLGlobalVariable* init;
    LLConstant* constInit;

    // classinfo global and initializer constant
    LLGlobalVariable* classInfo;
    LLConstant* constClassInfo;
    bool classInfoDeclared;
    bool classInfoDefined;
    // vector of interfaces that should be put in ClassInfo.interfaces
    InterfaceVector classInfoInterfaces;

    // align(1) struct S { ... }
    bool packed;

    // composite type debug description
    llvm::DICompositeType diCompositeType;
};

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

// represents interface implemented by a class
struct IrInterface : IrBase
{
    BaseClass* base;
    ClassDeclaration* decl;

    llvm::PATypeHolder vtblInitTy;

    LLConstant* vtblInit;
    LLGlobalVariable* vtbl;
    Array vtblDecls; // array of FuncDecls that make up the vtbl

    const LLStructType* infoTy;
    LLConstant* infoInit;
    LLConstant* info;

    size_t index;

    IrInterface(BaseClass* b);
};

#endif