Merge Kai's D1 removal commits.

This commit is contained in:
David Nadlinger
2013-03-06 16:41:13 +01:00
143 changed files with 11 additions and 91595 deletions

View File

@@ -4,7 +4,6 @@ install:
- sudo apt-get update -qq
- sudo apt-get install -qq $LLVM_PACKAGE libconfig++8-dev
env:
- DVER=1 LLVM_PACKAGE=llvm-3.1 LLVM_VERSION=3.1
- DVER=2 LLVM_PACKAGE=llvm-3.0-dev LLVM_VERSION=3.0 TESTSUITE_FILTER=debug
- DVER=2 LLVM_PACKAGE=llvm-3.0-dev LLVM_VERSION=3.0 TESTSUITE_FILTER=release
- DVER=2 LLVM_PACKAGE=llvm-3.1 LLVM_VERSION=3.1 TESTSUITE_FILTER=debug

View File

@@ -50,11 +50,8 @@ option(USE_METADATA "use metadata and related custom optimization passes" ON)
mark_as_advanced(USE_BOEHM_GC GENERATE_OFFTI USE_METADATA)
if(D_VERSION EQUAL 1)
set(DMDFE_PATH dmd)
set(LDC_EXE ldc)
set(LDMD_EXE ldmd)
set(RUNTIME runtime)
add_definitions(-DDMDV1)
message(FATAL_ERROR "D version 1 is no longer supported.
Please consider using D version 2 or checkout the 'd1' git branch for the last version supporting D version 1.")
elseif(D_VERSION EQUAL 2)
set(DMDFE_PATH dmd2)
set(LDC_EXE ldc2)

View File

@@ -1,418 +0,0 @@
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "root.h"
#include "rmem.h"
#include "enum.h"
#include "aggregate.h"
#include "init.h"
#include "attrib.h"
#include "scope.h"
#include "id.h"
#include "mtype.h"
#include "declaration.h"
#include "aggregate.h"
#include "expression.h"
#include "module.h"
#define LOG 0
/* Code to do access checks
*/
int hasPackageAccess(Scope *sc, Dsymbol *s);
/****************************************
* Return PROT access for Dsymbol smember in this declaration.
*/
enum PROT AggregateDeclaration::getAccess(Dsymbol *smember)
{
return PROTpublic;
}
enum PROT StructDeclaration::getAccess(Dsymbol *smember)
{
enum PROT access_ret = PROTnone;
#if LOG
printf("+StructDeclaration::getAccess(this = '%s', smember = '%s')\n",
toChars(), smember->toChars());
#endif
if (smember->toParent() == this)
{
access_ret = smember->prot();
}
else if (smember->isDeclaration()->isStatic())
{
access_ret = smember->prot();
}
return access_ret;
}
enum PROT ClassDeclaration::getAccess(Dsymbol *smember)
{
enum PROT access_ret = PROTnone;
#if LOG
printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n",
toChars(), smember->toChars());
#endif
if (smember->toParent() == this)
{
access_ret = smember->prot();
}
else
{
if (smember->isDeclaration()->isStatic())
{
access_ret = smember->prot();
}
for (size_t i = 0; i < baseclasses->dim; i++)
{ BaseClass *b = (*baseclasses)[i];
enum PROT access = b->base->getAccess(smember);
switch (access)
{
case PROTnone:
break;
case PROTprivate:
access_ret = PROTnone; // private members of base class not accessible
break;
case PROTpackage:
case PROTprotected:
case PROTpublic:
case PROTexport:
// If access is to be tightened
if (b->protection < access)
access = b->protection;
// Pick path with loosest access
if (access > access_ret)
access_ret = access;
break;
default:
assert(0);
}
}
}
#if LOG
printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n",
toChars(), smember->toChars(), access_ret);
#endif
return access_ret;
}
/********************************************************
* Helper function for ClassDeclaration::accessCheck()
* Returns:
* 0 no access
* 1 access
*/
static int accessCheckX(
Dsymbol *smember,
Dsymbol *sfunc,
AggregateDeclaration *dthis,
AggregateDeclaration *cdscope)
{
assert(dthis);
#if 0
printf("accessCheckX for %s.%s in function %s() in scope %s\n",
dthis->toChars(), smember->toChars(),
sfunc ? sfunc->toChars() : "NULL",
cdscope ? cdscope->toChars() : "NULL");
#endif
if (dthis->hasPrivateAccess(sfunc) ||
dthis->isFriendOf(cdscope))
{
if (smember->toParent() == dthis)
return 1;
else
{
ClassDeclaration *cdthis = dthis->isClassDeclaration();
if (cdthis)
{
for (size_t i = 0; i < cdthis->baseclasses->dim; i++)
{ BaseClass *b = (*cdthis->baseclasses)[i];
enum PROT access = b->base->getAccess(smember);
if (access >= PROTprotected ||
accessCheckX(smember, sfunc, b->base, cdscope)
)
return 1;
}
}
}
}
else
{
if (smember->toParent() != dthis)
{
ClassDeclaration *cdthis = dthis->isClassDeclaration();
if (cdthis)
{
for (size_t i = 0; i < cdthis->baseclasses->dim; i++)
{ BaseClass *b = (*cdthis->baseclasses)[i];
if (accessCheckX(smember, sfunc, b->base, cdscope))
return 1;
}
}
}
}
return 0;
}
/*******************************
* Do access check for member of this class, this class being the
* type of the 'this' pointer used to access smember.
*/
void AggregateDeclaration::accessCheck(Loc loc, Scope *sc, Dsymbol *smember)
{
int result;
FuncDeclaration *f = sc->func;
AggregateDeclaration *cdscope = sc->getStructClassScope();
enum PROT access;
#if LOG
printf("AggregateDeclaration::accessCheck() for %s.%s in function %s() in scope %s\n",
toChars(), smember->toChars(),
f ? f->toChars() : NULL,
cdscope ? cdscope->toChars() : NULL);
#endif
Dsymbol *smemberparent = smember->toParent();
if (!smemberparent || !smemberparent->isAggregateDeclaration())
{
#if LOG
printf("not an aggregate member\n");
#endif
return; // then it is accessible
}
// BUG: should enable this check
//assert(smember->parent->isBaseOf(this, NULL));
if (smemberparent == this)
{ enum PROT access2 = smember->prot();
result = access2 >= PROTpublic ||
hasPrivateAccess(f) ||
isFriendOf(cdscope) ||
(access2 == PROTpackage && hasPackageAccess(sc, this));
#if LOG
printf("result1 = %d\n", result);
#endif
}
else if ((access = this->getAccess(smember)) >= PROTpublic)
{
result = 1;
#if LOG
printf("result2 = %d\n", result);
#endif
}
else if (access == PROTpackage && hasPackageAccess(sc, this))
{
result = 1;
#if LOG
printf("result3 = %d\n", result);
#endif
}
else
{
result = accessCheckX(smember, f, this, cdscope);
#if LOG
printf("result4 = %d\n", result);
#endif
}
if (!result)
{
error(loc, "member %s is not accessible", smember->toChars());
}
}
/****************************************
* Determine if this is the same or friend of cd.
*/
int AggregateDeclaration::isFriendOf(AggregateDeclaration *cd)
{
#if LOG
printf("AggregateDeclaration::isFriendOf(this = '%s', cd = '%s')\n", toChars(), cd ? cd->toChars() : "null");
#endif
if (this == cd)
return 1;
// Friends if both are in the same module
//if (toParent() == cd->toParent())
if (cd && getModule() == cd->getModule())
{
#if LOG
printf("\tin same module\n");
#endif
return 1;
}
#if LOG
printf("\tnot friend\n");
#endif
return 0;
}
/****************************************
* Determine if scope sc has package level access to s.
*/
int hasPackageAccess(Scope *sc, Dsymbol *s)
{
#if LOG
printf("hasPackageAccess(s = '%s', sc = '%p')\n", s->toChars(), sc);
#endif
for (; s; s = s->parent)
{
if (s->isPackage() && !s->isModule())
break;
}
#if LOG
if (s)
printf("\tthis is in package '%s'\n", s->toChars());
#endif
if (s && s == sc->module->parent)
{
#if LOG
printf("\ts is in same package as sc\n");
#endif
return 1;
}
#if LOG
printf("\tno package access\n");
#endif
return 0;
}
/**********************************
* Determine if smember has access to private members of this declaration.
*/
int AggregateDeclaration::hasPrivateAccess(Dsymbol *smember)
{
if (smember)
{ AggregateDeclaration *cd = NULL;
Dsymbol *smemberparent = smember->toParent();
if (smemberparent)
cd = smemberparent->isAggregateDeclaration();
#if LOG
printf("AggregateDeclaration::hasPrivateAccess(class %s, member %s)\n",
toChars(), smember->toChars());
#endif
if (this == cd) // smember is a member of this class
{
#if LOG
printf("\tyes 1\n");
#endif
return 1; // so we get private access
}
// If both are members of the same module, grant access
while (1)
{ Dsymbol *sp = smember->toParent();
if (sp->isFuncDeclaration() && smember->isFuncDeclaration())
smember = sp;
else
break;
}
if (!cd && toParent() == smember->toParent())
{
#if LOG
printf("\tyes 2\n");
#endif
return 1;
}
if (!cd && getModule() == smember->getModule())
{
#if LOG
printf("\tyes 3\n");
#endif
return 1;
}
}
#if LOG
printf("\tno\n");
#endif
return 0;
}
/****************************************
* Check access to d for expression e.d
*/
void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d)
{
#if LOG
if (e)
{ printf("accessCheck(%s . %s)\n", e->toChars(), d->toChars());
printf("\te->type = %s\n", e->type->toChars());
}
else
{
//printf("accessCheck(%s)\n", d->toChars());
}
#endif
if (!e)
{
if (d->prot() == PROTprivate && d->getModule() != sc->module ||
d->prot() == PROTpackage && !hasPackageAccess(sc, d))
error(loc, "%s %s.%s is not accessible from %s",
d->kind(), d->getModule()->toChars(), d->toChars(), sc->module->toChars());
}
else if (e->type->ty == Tclass)
{ // Do access check
ClassDeclaration *cd;
cd = (ClassDeclaration *)(((TypeClass *)e->type)->sym);
#if 1
if (e->op == TOKsuper)
{ ClassDeclaration *cd2;
cd2 = sc->func->toParent()->isClassDeclaration();
if (cd2)
cd = cd2;
}
#endif
cd->accessCheck(loc, sc, d);
}
else if (e->type->ty == Tstruct)
{ // Do access check
StructDeclaration *cd;
cd = (StructDeclaration *)(((TypeStruct *)e->type)->sym);
cd->accessCheck(loc, sc, d);
}
}

View File

@@ -1,373 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_AGGREGATE_H
#define DMD_AGGREGATE_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "dsymbol.h"
#if IN_LLVM
#include <vector>
#include <set>
#include <map>
#endif
struct Identifier;
struct Type;
struct TypeFunction;
struct Expression;
struct FuncDeclaration;
struct CtorDeclaration;
struct DtorDeclaration;
struct InvariantDeclaration;
struct NewDeclaration;
struct DeleteDeclaration;
struct InterfaceDeclaration;
struct ClassInfoDeclaration;
struct VarDeclaration;
struct dt_t;
#if IN_LLVM
namespace llvm
{
class Type;
class Value;
class Constant;
class ConstantStruct;
class GlobalVariable;
}
#endif
enum Sizeok
{
SIZEOKnone, // size of aggregate is not computed yet
SIZEOKdone, // size of aggregate is set correctly
SIZEOKfwd, // error in computing size of aggregate
};
struct AggregateDeclaration : ScopeDsymbol
{
Type *type;
StorageClass storage_class;
enum PROT protection;
Type *handle; // 'this' type
unsigned structsize; // size of struct
unsigned alignsize; // size of struct for alignment purposes
structalign_t structalign; // struct member alignment in effect
int hasUnions; // set if aggregate has overlapping fields
VarDeclarations fields; // VarDeclaration fields
enum Sizeok sizeok; // set when structsize contains valid data
int isdeprecated; // !=0 if deprecated
#if DMDV2
int isnested; // !=0 if is nested
VarDeclaration *vthis; // 'this' parameter if this aggregate is nested
#endif
// Special member functions
InvariantDeclaration *inv; // invariant
NewDeclaration *aggNew; // allocator
DeleteDeclaration *aggDelete; // deallocator
#if DMDV2
//CtorDeclaration *ctor;
Dsymbol *ctor; // CtorDeclaration or TemplateDeclaration
CtorDeclaration *defaultCtor; // default constructor
Dsymbol *aliasthis; // forward unresolved lookups to aliasthis
bool noDefaultCtor; // no default construction
#endif
FuncDeclarations dtors; // Array of destructors
FuncDeclaration *dtor; // aggregate destructor
#ifdef IN_GCC
Expressions *attributes; // GCC decl/type attributes
FuncDeclarations methods; // flat list of all methods for debug information
#endif
AggregateDeclaration(Loc loc, Identifier *id);
void semantic2(Scope *sc);
void semantic3(Scope *sc);
void inlineScan();
unsigned size(Loc loc);
static void alignmember(structalign_t salign, unsigned size, unsigned *poffset);
static unsigned placeField(unsigned *nextoffset,
unsigned memsize, unsigned memalignsize, structalign_t memalign,
unsigned *paggsize, unsigned *paggalignsize, bool isunion);
Type *getType();
int firstFieldInUnion(int indx); // first field in union that includes indx
int numFieldsInUnion(int firstIndex); // #fields in union starting at index
int isDeprecated(); // is aggregate deprecated?
FuncDeclaration *buildDtor(Scope *sc);
int isNested();
int isExport();
void emitComment(Scope *sc);
void toJsonBuffer(OutBuffer *buf);
void toDocBuffer(OutBuffer *buf);
// For access checking
virtual PROT getAccess(Dsymbol *smember); // determine access to smember
int isFriendOf(AggregateDeclaration *cd);
int hasPrivateAccess(Dsymbol *smember); // does smember have private access to members of this class?
void accessCheck(Loc loc, Scope *sc, Dsymbol *smember);
enum PROT prot();
#if IN_DMD
// Back end
Symbol *stag; // tag symbol for debug data
Symbol *sinit;
Symbol *toInitializer();
#endif
AggregateDeclaration *isAggregateDeclaration() { return this; }
#if IN_LLVM
// Aggregates that wouldn't have gotten semantic3'ed if we weren't inlining set this flag.
bool availableExternally;
#endif
};
struct AnonymousAggregateDeclaration : AggregateDeclaration
{
AnonymousAggregateDeclaration()
: AggregateDeclaration(0, NULL)
{
}
AnonymousAggregateDeclaration *isAnonymousAggregateDeclaration() { return this; }
};
struct StructDeclaration : AggregateDeclaration
{
int zeroInit; // !=0 if initialize with 0 fill
#if DMDV2
int hasIdentityAssign; // !=0 if has identity opAssign
int hasIdentityEquals; // !=0 if has identity opEquals
FuncDeclaration *cpctor; // generated copy-constructor, if any
FuncDeclarations postblits; // Array of postblit functions
FuncDeclaration *postblit; // aggregate postblit
FuncDeclaration *xeq; // TypeInfo_Struct.xopEquals
static FuncDeclaration *xerreq; // object.xopEquals
#endif
// For 64 bit Efl function call/return ABI
Type *arg1type;
Type *arg2type;
StructDeclaration(Loc loc, Identifier *id);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
Dsymbol *search(Loc, Identifier *ident, int flags);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
char *mangle();
const char *kind();
void finalizeSize(Scope *sc);
bool isPOD();
#if DMDV1
Expression *cloneMembers();
#endif
#if DMDV2
int needOpAssign();
int needOpEquals();
FuncDeclaration *buildOpAssign(Scope *sc);
FuncDeclaration *buildOpEquals(Scope *sc);
FuncDeclaration *buildPostBlit(Scope *sc);
FuncDeclaration *buildCpCtor(Scope *sc);
FuncDeclaration *buildXopEquals(Scope *sc);
#endif
void toDocBuffer(OutBuffer *buf);
PROT getAccess(Dsymbol *smember); // determine access to smember
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
void toDt(dt_t **pdt);
void toDebug(); // to symbolic debug info
#endif
StructDeclaration *isStructDeclaration() { return this; }
#if IN_LLVM
void codegen(Ir*);
#endif
};
struct UnionDeclaration : StructDeclaration
{
UnionDeclaration(Loc loc, Identifier *id);
Dsymbol *syntaxCopy(Dsymbol *s);
const char *kind();
UnionDeclaration *isUnionDeclaration() { return this; }
};
// warning: two classes with the same base class share the same
// BaseClass instance.
struct BaseClass
{
Type *type; // (before semantic processing)
enum PROT protection; // protection for the base interface
ClassDeclaration *base;
int offset; // 'this' pointer offset
FuncDeclarations vtbl; // for interfaces: Array of FuncDeclaration's
// making up the vtbl[]
size_t baseInterfaces_dim;
BaseClass *baseInterfaces; // if BaseClass is an interface, these
// are a copy of the InterfaceDeclaration::interfaces
BaseClass();
BaseClass(Type *type, enum PROT protection);
int fillVtbl(ClassDeclaration *cd, FuncDeclarations *vtbl, int newinstance);
void copyBaseInterfaces(BaseClasses *);
};
#if DMDV2
#define CLASSINFO_SIZE_64 0x98 // value of ClassInfo.size
#define CLASSINFO_SIZE (0x3C+12+4) // value of ClassInfo.size
#else
#define CLASSINFO_SIZE (0x3C+12+4) // value of ClassInfo.size
#define CLASSINFO_SIZE_64 (0x98) // value of ClassInfo.size
#endif
struct ClassDeclaration : AggregateDeclaration
{
static ClassDeclaration *object;
static ClassDeclaration *classinfo;
static ClassDeclaration *errorException;
ClassDeclaration *baseClass; // NULL only if this is Object
#if DMDV1
CtorDeclaration *ctor;
CtorDeclaration *defaultCtor; // default constructor
#endif
FuncDeclaration *staticCtor;
FuncDeclaration *staticDtor;
Dsymbols vtbl; // Array of FuncDeclaration's making up the vtbl[]
Dsymbols vtblFinal; // More FuncDeclaration's that aren't in vtbl[]
BaseClasses *baseclasses; // Array of BaseClass's; first is super,
// rest are Interface's
size_t interfaces_dim;
BaseClass **interfaces; // interfaces[interfaces_dim] for this class
// (does not include baseClass)
BaseClasses *vtblInterfaces; // array of base interfaces that have
// their own vtbl[]
ClassInfoDeclaration *vclassinfo; // the ClassInfo object for this ClassDeclaration
int com; // !=0 if this is a COM class (meaning
// it derives from IUnknown)
int isscope; // !=0 if this is an auto class
int isabstract; // !=0 if abstract class
#if DMDV1
int isnested; // !=0 if is nested
VarDeclaration *vthis; // 'this' parameter if this class is nested
#endif
int inuse; // to prevent recursive attempts
ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int isBaseOf2(ClassDeclaration *cd);
#define OFFSET_RUNTIME 0x76543210
virtual int isBaseOf(ClassDeclaration *cd, int *poffset);
virtual int isBaseInfoComplete();
Dsymbol *search(Loc, Identifier *ident, int flags);
Dsymbol *searchBase(Loc, Identifier *ident);
#if DMDV2
int isFuncHidden(FuncDeclaration *fd);
#endif
FuncDeclaration *findFunc(Identifier *ident, TypeFunction *tf);
void interfaceSemantic(Scope *sc);
#if DMDV1
int isNested();
#endif
int isCOMclass();
virtual int isCOMinterface();
#if DMDV2
virtual int isCPPinterface();
#endif
int isAbstract();
virtual int vtblOffset();
const char *kind();
char *mangle();
void toDocBuffer(OutBuffer *buf);
PROT getAccess(Dsymbol *smember); // determine access to smember
void addLocalClass(ClassDeclarations *);
#if IN_DMD
// Back end
void toObjFile(int multiobj); // compile to .obj file
void toDebug();
unsigned baseVtblOffset(BaseClass *bc);
Symbol *toSymbol();
Symbol *toVtblSymbol();
void toDt(dt_t **pdt);
void toDt2(dt_t **pdt, ClassDeclaration *cd);
Symbol *vtblsym;
#endif
ClassDeclaration *isClassDeclaration() { return (ClassDeclaration *)this; }
#if IN_LLVM
virtual void codegen(Ir*);
#endif
};
struct InterfaceDeclaration : ClassDeclaration
{
#if DMDV2
int cpp; // !=0 if this is a C++ interface
#endif
InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
int isBaseOf(ClassDeclaration *cd, int *poffset);
int isBaseOf(BaseClass *bc, int *poffset);
const char *kind();
int isBaseInfoComplete();
int vtblOffset();
#if DMDV2
int isCPPinterface();
#endif
virtual int isCOMinterface();
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
Symbol *toSymbol();
#endif
InterfaceDeclaration *isInterfaceDeclaration() { return this; }
#if IN_LLVM
void codegen(Ir*);
#endif
};
#endif /* DMD_AGGREGATE_H */

View File

@@ -1,165 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "mars.h"
#include "expression.h"
/**************************************
* An Expression tree walker that will visit each Expression e in the tree,
* in depth-first evaluation order, and call fp(e,param) on it.
* fp() signals whether the walking continues with its return value:
* Returns:
* 0 continue
* 1 done
* It's a bit slower than using virtual functions, but more encapsulated and less brittle.
* Creating an iterator for this would be much more complex.
*/
typedef int (*fp_t)(Expression *, void *);
int Expression::apply(fp_t fp, void *param)
{
return (*fp)(this, param);
}
/******************************
* Perform apply() on an array of Expressions.
*/
int arrayExpressionApply(Expressions *a, fp_t fp, void *param)
{
//printf("arrayExpressionApply(%p)\n", a);
if (a)
{
for (size_t i = 0; i < a->dim; i++)
{ Expression *e = (*a)[i];
if (e)
{
if (e->apply(fp, param))
return 1;
}
}
}
return 0;
}
int NewExp::apply(int (*fp)(Expression *, void *), void *param)
{
//printf("NewExp::apply(): %s\n", toChars());
return ((thisexp ? thisexp->apply(fp, param) : 0) ||
arrayExpressionApply(newargs, fp, param) ||
arrayExpressionApply(arguments, fp, param) ||
(*fp)(this, param));
}
int NewAnonClassExp::apply(int (*fp)(Expression *, void *), void *param)
{
//printf("NewAnonClassExp::apply(): %s\n", toChars());
return ((thisexp ? thisexp->apply(fp, param) : 0) ||
arrayExpressionApply(newargs, fp, param) ||
arrayExpressionApply(arguments, fp, param) ||
(*fp)(this, param));
}
int UnaExp::apply(fp_t fp, void *param)
{
return e1->apply(fp, param) ||
(*fp)(this, param);
}
int BinExp::apply(fp_t fp, void *param)
{
return e1->apply(fp, param) ||
e2->apply(fp, param) ||
(*fp)(this, param);
}
int AssertExp::apply(fp_t fp, void *param)
{
//printf("CallExp::apply(fp_t fp, void *param): %s\n", toChars());
return e1->apply(fp, param) ||
(msg ? msg->apply(fp, param) : 0) ||
(*fp)(this, param);
}
int CallExp::apply(fp_t fp, void *param)
{
//printf("CallExp::apply(fp_t fp, void *param): %s\n", toChars());
return e1->apply(fp, param) ||
arrayExpressionApply(arguments, fp, param) ||
(*fp)(this, param);
}
int ArrayExp::apply(fp_t fp, void *param)
{
//printf("ArrayExp::apply(fp_t fp, void *param): %s\n", toChars());
return e1->apply(fp, param) ||
arrayExpressionApply(arguments, fp, param) ||
(*fp)(this, param);
}
int SliceExp::apply(fp_t fp, void *param)
{
return e1->apply(fp, param) ||
(lwr ? lwr->apply(fp, param) : 0) ||
(upr ? upr->apply(fp, param) : 0) ||
(*fp)(this, param);
}
int ArrayLiteralExp::apply(fp_t fp, void *param)
{
return arrayExpressionApply(elements, fp, param) ||
(*fp)(this, param);
}
int AssocArrayLiteralExp::apply(fp_t fp, void *param)
{
return arrayExpressionApply(keys, fp, param) ||
arrayExpressionApply(values, fp, param) ||
(*fp)(this, param);
}
int StructLiteralExp::apply(fp_t fp, void *param)
{
return arrayExpressionApply(elements, fp, param) ||
(*fp)(this, param);
}
int TupleExp::apply(fp_t fp, void *param)
{
return arrayExpressionApply(exps, fp, param) ||
(*fp)(this, param);
}
int CondExp::apply(fp_t fp, void *param)
{
return econd->apply(fp, param) ||
e1->apply(fp, param) ||
e2->apply(fp, param) ||
(*fp)(this, param);
}

View File

@@ -1,441 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 2010-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// http://www.dsource.org/projects/dmd/browser/branches/dmd-1.x/src/argtypes.c
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "mars.h"
#include "dsymbol.h"
#include "mtype.h"
#include "scope.h"
#include "init.h"
#include "expression.h"
#include "attrib.h"
#include "declaration.h"
#include "template.h"
#include "id.h"
#include "enum.h"
#include "import.h"
#include "aggregate.h"
#include "hdrgen.h"
#define tfloat2 tfloat64
//#define tfloat2 tcomplex32
/****************************************************
* This breaks a type down into 'simpler' types that can be passed to a function
* in registers, and returned in registers.
* It's highly platform dependent.
* Returning a tuple of zero length means the type cannot be passed/returned in registers.
*/
TypeTuple *Type::toArgTypes()
{
return NULL; // not valid for a parameter
}
TypeTuple *TypeError::toArgTypes()
{
return new TypeTuple(Type::terror);
}
TypeTuple *TypeBasic::toArgTypes()
{ Type *t1 = NULL;
Type *t2 = NULL;
switch (ty)
{
case Tvoid:
return NULL;
case Tbool:
case Tint8:
case Tuns8:
case Tint16:
case Tuns16:
case Tint32:
case Tuns32:
case Tfloat32:
case Tint64:
case Tuns64:
case Tfloat64:
case Tfloat80:
t1 = this;
break;
case Timaginary32:
t1 = Type::tfloat32;
break;
case Timaginary64:
t1 = Type::tfloat64;
break;
case Timaginary80:
t1 = Type::tfloat80;
break;
case Tcomplex32:
if (global.params.is64bit)
t1 = Type::tfloat2;
else
{
t1 = Type::tfloat64;
t2 = Type::tfloat64;
}
break;
case Tcomplex64:
t1 = Type::tfloat64;
t2 = Type::tfloat64;
break;
case Tcomplex80:
t1 = Type::tfloat80;
t2 = Type::tfloat80;
break;
case Tascii:
t1 = Type::tuns8;
break;
case Twchar:
t1 = Type::tuns16;
break;
case Tdchar:
t1 = Type::tuns32;
break;
default: assert(0);
}
TypeTuple *t;
if (t1)
{
if (t2)
t = new TypeTuple(t1, t2);
else
t = new TypeTuple(t1);
}
else
t = new TypeTuple();
return t;
}
#if DMDV2
TypeTuple *TypeVector::toArgTypes()
{
return new TypeTuple(this);
}
#endif
TypeTuple *TypeSArray::toArgTypes()
{
#if DMDV2
if (dim)
{
/* Should really be done as if it were a struct with dim members
* of the array's elements.
* I.e. int[2] should be done like struct S { int a; int b; }
*/
dinteger_t sz = dim->toInteger();
if (sz == 1)
// T[1] should be passed like T
return next->toArgTypes();
}
return new TypeTuple(); // pass on the stack for efficiency
#else
return new TypeTuple(); // pass on the stack for efficiency
#endif
}
TypeTuple *TypeDArray::toArgTypes()
{
/* Should be done as if it were:
* struct S { size_t length; void* ptr; }
*/
return new TypeTuple(Type::tsize_t, Type::tvoidptr);
}
TypeTuple *TypeAArray::toArgTypes()
{
return new TypeTuple(Type::tvoidptr);
}
TypeTuple *TypePointer::toArgTypes()
{
return new TypeTuple(Type::tvoidptr);
}
TypeTuple *TypeDelegate::toArgTypes()
{
/* Should be done as if it were:
* struct S { void* ptr; void* funcptr; }
*/
return new TypeTuple(Type::tvoidptr, Type::tvoidptr);
}
/*************************************
* Convert a floating point type into the equivalent integral type.
*/
Type *mergeFloatToInt(Type *t)
{
switch (t->ty)
{
case Tfloat32:
case Timaginary32:
t = Type::tint32;
break;
case Tfloat64:
case Timaginary64:
case Tcomplex32:
t = Type::tint64;
break;
default:
#ifdef DEBUG
printf("mergeFloatToInt() %s\n", t->toChars());
#endif
assert(0);
}
return t;
}
/*************************************
* This merges two types into an 8byte type.
*/
Type *argtypemerge(Type *t1, Type *t2, unsigned offset2)
{
//printf("argtypemerge(%s, %s, %d)\n", t1 ? t1->toChars() : "", t2 ? t2->toChars() : "", offset2);
if (!t1)
{ assert(!t2 || offset2 == 0);
return t2;
}
if (!t2)
return t1;
unsigned sz1 = t1->size(0);
unsigned sz2 = t2->size(0);
if (t1->ty != t2->ty &&
(t1->ty == Tfloat80 || t2->ty == Tfloat80))
return NULL;
// [float,float] => [cfloat]
if (t1->ty == Tfloat32 && t2->ty == Tfloat32 && offset2 == 4)
return Type::tfloat2;
// Merging floating and non-floating types produces the non-floating type
if (t1->isfloating())
{
if (!t2->isfloating())
t1 = mergeFloatToInt(t1);
}
else if (t2->isfloating())
t2 = mergeFloatToInt(t2);
Type *t;
// Pick type with larger size
if (sz1 < sz2)
t = t2;
else
t = t1;
// If t2 does not lie within t1, need to increase the size of t to enclose both
if (offset2 && sz1 < offset2 + sz2)
{
switch (offset2 + sz2)
{
case 2:
t = Type::tint16;
break;
case 3:
case 4:
t = Type::tint32;
break;
case 5:
case 6:
case 7:
case 8:
t = Type::tint64;
break;
default:
assert(0);
}
}
return t;
}
TypeTuple *TypeStruct::toArgTypes()
{
//printf("TypeStruct::toArgTypes() %s\n", toChars());
if (!sym->isPOD())
{
Lmemory:
//printf("\ttoArgTypes() %s => [ ]\n", toChars());
return new TypeTuple(); // pass on the stack
}
Type *t1 = NULL;
Type *t2 = NULL;
d_uns64 sz = size(0);
assert(sz < 0xFFFFFFFF);
switch ((unsigned)sz)
{
case 1:
t1 = Type::tint8;
break;
case 2:
t1 = Type::tint16;
break;
case 4:
t1 = Type::tint32;
break;
case 8:
t1 = Type::tint64;
break;
case 16:
t1 = NULL; // could be a TypeVector
break;
default:
goto Lmemory;
}
if (global.params.is64bit && sym->fields.dim)
{
#if 1
unsigned sz1 = 0;
unsigned sz2 = 0;
t1 = NULL;
for (size_t i = 0; i < sym->fields.dim; i++)
{ VarDeclaration *f = sym->fields[i];
//printf("f->type = %s\n", f->type->toChars());
TypeTuple *tup = f->type->toArgTypes();
if (!tup)
goto Lmemory;
size_t dim = tup->arguments->dim;
Type *ft1 = NULL;
Type *ft2 = NULL;
switch (dim)
{
case 2:
ft1 = (*tup->arguments)[0]->type;
ft2 = (*tup->arguments)[1]->type;
break;
case 1:
if (f->offset < 8)
ft1 = (*tup->arguments)[0]->type;
else
ft2 = (*tup->arguments)[0]->type;
break;
default:
goto Lmemory;
}
if (f->offset & 7)
{
// Misaligned fields goto Lmemory
unsigned alignsz = f->type->alignsize();
if (f->offset & (alignsz - 1))
goto Lmemory;
// Fields that overlap the 8byte boundary goto Lmemory
unsigned fieldsz = f->type->size(0);
if (f->offset < 8 && (f->offset + fieldsz) > 8)
goto Lmemory;
}
// First field in 8byte must be at start of 8byte
assert(t1 || f->offset == 0);
if (ft1)
{
t1 = argtypemerge(t1, ft1, f->offset);
if (!t1)
goto Lmemory;
}
if (ft2)
{
unsigned off2 = f->offset;
if (ft1)
off2 = 8;
assert(t2 || off2 == 8);
t2 = argtypemerge(t2, ft2, off2 - 8);
if (!t2)
goto Lmemory;
}
}
if (t2)
{
if (t1->isfloating() && t2->isfloating())
{
if (t1->ty == Tfloat64 && t2->ty == Tfloat64)
;
else
goto Lmemory;
}
else if (t1->isfloating())
goto Lmemory;
else if (t2->isfloating())
goto Lmemory;
else
;
}
#else
if (sym->fields.dim == 1)
{ VarDeclaration *f = sym->fields[0];
//printf("f->type = %s\n", f->type->toChars());
TypeTuple *tup = f->type->toArgTypes();
if (tup)
{
size_t dim = tup->arguments->dim;
if (dim == 1)
t1 = (*tup->arguments)[0]->type;
}
}
#endif
}
//printf("\ttoArgTypes() %s => [%s,%s]\n", toChars(), t1 ? t1->toChars() : "", t2 ? t2->toChars() : "");
TypeTuple *t;
if (t1)
{
//if (t1) printf("test1: %s => %s\n", toChars(), t1->toChars());
if (t2)
t = new TypeTuple(t1, t2);
else
t = new TypeTuple(t1);
}
else
goto Lmemory;
return t;
}
TypeTuple *TypeEnum::toArgTypes()
{
return toBasetype()->toArgTypes();
}
TypeTuple *TypeTypedef::toArgTypes()
{
return sym->basetype->toArgTypes();
}
TypeTuple *TypeClass::toArgTypes()
{
return new TypeTuple(Type::tvoidptr);
}

View File

@@ -1,660 +0,0 @@
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "rmem.h"
#include "aav.h"
#include "expression.h"
#include "statement.h"
#include "mtype.h"
#include "declaration.h"
#include "scope.h"
#include "id.h"
#include "module.h"
#include "init.h"
#if IN_DMD
extern int binary(const char *p , const char **tab, int high);
/**************************************
* Hash table of array op functions already generated or known about.
*/
AA *arrayfuncs;
#endif
/**********************************************
* Check that there are no uses of arrays without [].
*/
bool isArrayOpValid(Expression *e)
{
if (e->op == TOKslice)
return true;
Type *tb = e->type->toBasetype();
if ( (tb->ty == Tarray) || (tb->ty == Tsarray) )
{
switch (e->op)
{
case TOKadd:
case TOKmin:
case TOKmul:
case TOKdiv:
case TOKmod:
case TOKxor:
case TOKand:
case TOKor:
case TOKassign:
case TOKaddass:
case TOKminass:
case TOKmulass:
case TOKdivass:
case TOKmodass:
case TOKxorass:
case TOKandass:
case TOKorass:
#if DMDV2
case TOKpow:
case TOKpowass:
#endif
return isArrayOpValid(((BinExp *)e)->e1) && isArrayOpValid(((BinExp *)e)->e2);
case TOKcall:
return false; // TODO: Decide if [] is required after arrayop calls.
case TOKneg:
case TOKtilde:
return isArrayOpValid(((UnaExp *)e)->e1);
default:
return false;
}
}
return true;
}
/***********************************
* Construct the array operation expression.
*/
Expression *BinExp::arrayOp(Scope *sc)
{
//printf("BinExp::arrayOp() %s\n", toChars());
Type *tb = type->toBasetype();
assert(tb->ty == Tarray || tb->ty == Tsarray);
if (tb->nextOf()->toBasetype()->ty == Tvoid)
{
error("Cannot perform array operations on void[] arrays");
return new ErrorExp();
}
if (!isArrayOpValid(e2))
{
e2->error("invalid array operation %s (did you forget a [] ?)", toChars());
return new ErrorExp();
}
Expressions *arguments = new Expressions();
/* The expression to generate an array operation for is mangled
* into a name to use as the array operation function name.
* Mangle in the operands and operators in RPN order, and type.
*/
OutBuffer buf;
buf.writestring("_array");
buildArrayIdent(&buf, arguments);
buf.writeByte('_');
/* Append deco of array element type
*/
#if DMDV2
buf.writestring(type->toBasetype()->nextOf()->toBasetype()->mutableOf()->deco);
#else
buf.writestring(type->toBasetype()->nextOf()->toBasetype()->deco);
#endif
#if IN_LLVM
const size_t namelen = buf.offset;
#endif
buf.writeByte(0);
char *name = buf.toChars();
Identifier *ident = Lexer::idPool(name);
/* Look up name in hash table
*/
#if IN_DMD
FuncDeclaration **pfd = (FuncDeclaration **)_aaGet(&arrayfuncs, ident);
FuncDeclaration *fd = (FuncDeclaration *)*pfd;
#elif IN_LLVM
StringValue *sv = sc->module->arrayfuncs.update(name, namelen);
FuncDeclaration *fd = (FuncDeclaration *)sv->ptrvalue;
#endif
if (!fd)
{
#if IN_DMD
static const char *libArrayopFuncs[] =
{
"_arrayExpSliceAddass_a",
"_arrayExpSliceAddass_d", // T[]+=T
"_arrayExpSliceAddass_f", // T[]+=T
"_arrayExpSliceAddass_g",
"_arrayExpSliceAddass_h",
"_arrayExpSliceAddass_i",
"_arrayExpSliceAddass_k",
"_arrayExpSliceAddass_s",
"_arrayExpSliceAddass_t",
"_arrayExpSliceAddass_u",
"_arrayExpSliceAddass_w",
"_arrayExpSliceDivass_d", // T[]/=T
"_arrayExpSliceDivass_f", // T[]/=T
"_arrayExpSliceMinSliceAssign_a",
"_arrayExpSliceMinSliceAssign_d", // T[]=T-T[]
"_arrayExpSliceMinSliceAssign_f", // T[]=T-T[]
"_arrayExpSliceMinSliceAssign_g",
"_arrayExpSliceMinSliceAssign_h",
"_arrayExpSliceMinSliceAssign_i",
"_arrayExpSliceMinSliceAssign_k",
"_arrayExpSliceMinSliceAssign_s",
"_arrayExpSliceMinSliceAssign_t",
"_arrayExpSliceMinSliceAssign_u",
"_arrayExpSliceMinSliceAssign_w",
"_arrayExpSliceMinass_a",
"_arrayExpSliceMinass_d", // T[]-=T
"_arrayExpSliceMinass_f", // T[]-=T
"_arrayExpSliceMinass_g",
"_arrayExpSliceMinass_h",
"_arrayExpSliceMinass_i",
"_arrayExpSliceMinass_k",
"_arrayExpSliceMinass_s",
"_arrayExpSliceMinass_t",
"_arrayExpSliceMinass_u",
"_arrayExpSliceMinass_w",
"_arrayExpSliceMulass_d", // T[]*=T
"_arrayExpSliceMulass_f", // T[]*=T
"_arrayExpSliceMulass_i",
"_arrayExpSliceMulass_k",
"_arrayExpSliceMulass_s",
"_arrayExpSliceMulass_t",
"_arrayExpSliceMulass_u",
"_arrayExpSliceMulass_w",
"_arraySliceExpAddSliceAssign_a",
"_arraySliceExpAddSliceAssign_d", // T[]=T[]+T
"_arraySliceExpAddSliceAssign_f", // T[]=T[]+T
"_arraySliceExpAddSliceAssign_g",
"_arraySliceExpAddSliceAssign_h",
"_arraySliceExpAddSliceAssign_i",
"_arraySliceExpAddSliceAssign_k",
"_arraySliceExpAddSliceAssign_s",
"_arraySliceExpAddSliceAssign_t",
"_arraySliceExpAddSliceAssign_u",
"_arraySliceExpAddSliceAssign_w",
"_arraySliceExpDivSliceAssign_d", // T[]=T[]/T
"_arraySliceExpDivSliceAssign_f", // T[]=T[]/T
"_arraySliceExpMinSliceAssign_a",
"_arraySliceExpMinSliceAssign_d", // T[]=T[]-T
"_arraySliceExpMinSliceAssign_f", // T[]=T[]-T
"_arraySliceExpMinSliceAssign_g",
"_arraySliceExpMinSliceAssign_h",
"_arraySliceExpMinSliceAssign_i",
"_arraySliceExpMinSliceAssign_k",
"_arraySliceExpMinSliceAssign_s",
"_arraySliceExpMinSliceAssign_t",
"_arraySliceExpMinSliceAssign_u",
"_arraySliceExpMinSliceAssign_w",
"_arraySliceExpMulSliceAddass_d", // T[] += T[]*T
"_arraySliceExpMulSliceAddass_f",
"_arraySliceExpMulSliceAddass_r",
"_arraySliceExpMulSliceAssign_d", // T[]=T[]*T
"_arraySliceExpMulSliceAssign_f", // T[]=T[]*T
"_arraySliceExpMulSliceAssign_i",
"_arraySliceExpMulSliceAssign_k",
"_arraySliceExpMulSliceAssign_s",
"_arraySliceExpMulSliceAssign_t",
"_arraySliceExpMulSliceAssign_u",
"_arraySliceExpMulSliceAssign_w",
"_arraySliceExpMulSliceMinass_d", // T[] -= T[]*T
"_arraySliceExpMulSliceMinass_f",
"_arraySliceExpMulSliceMinass_r",
"_arraySliceSliceAddSliceAssign_a",
"_arraySliceSliceAddSliceAssign_d", // T[]=T[]+T[]
"_arraySliceSliceAddSliceAssign_f", // T[]=T[]+T[]
"_arraySliceSliceAddSliceAssign_g",
"_arraySliceSliceAddSliceAssign_h",
"_arraySliceSliceAddSliceAssign_i",
"_arraySliceSliceAddSliceAssign_k",
"_arraySliceSliceAddSliceAssign_r", // T[]=T[]+T[]
"_arraySliceSliceAddSliceAssign_s",
"_arraySliceSliceAddSliceAssign_t",
"_arraySliceSliceAddSliceAssign_u",
"_arraySliceSliceAddSliceAssign_w",
"_arraySliceSliceAddass_a",
"_arraySliceSliceAddass_d", // T[]+=T[]
"_arraySliceSliceAddass_f", // T[]+=T[]
"_arraySliceSliceAddass_g",
"_arraySliceSliceAddass_h",
"_arraySliceSliceAddass_i",
"_arraySliceSliceAddass_k",
"_arraySliceSliceAddass_s",
"_arraySliceSliceAddass_t",
"_arraySliceSliceAddass_u",
"_arraySliceSliceAddass_w",
"_arraySliceSliceMinSliceAssign_a",
"_arraySliceSliceMinSliceAssign_d", // T[]=T[]-T[]
"_arraySliceSliceMinSliceAssign_f", // T[]=T[]-T[]
"_arraySliceSliceMinSliceAssign_g",
"_arraySliceSliceMinSliceAssign_h",
"_arraySliceSliceMinSliceAssign_i",
"_arraySliceSliceMinSliceAssign_k",
"_arraySliceSliceMinSliceAssign_r", // T[]=T[]-T[]
"_arraySliceSliceMinSliceAssign_s",
"_arraySliceSliceMinSliceAssign_t",
"_arraySliceSliceMinSliceAssign_u",
"_arraySliceSliceMinSliceAssign_w",
"_arraySliceSliceMinass_a",
"_arraySliceSliceMinass_d", // T[]-=T[]
"_arraySliceSliceMinass_f", // T[]-=T[]
"_arraySliceSliceMinass_g",
"_arraySliceSliceMinass_h",
"_arraySliceSliceMinass_i",
"_arraySliceSliceMinass_k",
"_arraySliceSliceMinass_s",
"_arraySliceSliceMinass_t",
"_arraySliceSliceMinass_u",
"_arraySliceSliceMinass_w",
"_arraySliceSliceMulSliceAssign_d", // T[]=T[]*T[]
"_arraySliceSliceMulSliceAssign_f", // T[]=T[]*T[]
"_arraySliceSliceMulSliceAssign_i",
"_arraySliceSliceMulSliceAssign_k",
"_arraySliceSliceMulSliceAssign_s",
"_arraySliceSliceMulSliceAssign_t",
"_arraySliceSliceMulSliceAssign_u",
"_arraySliceSliceMulSliceAssign_w",
"_arraySliceSliceMulass_d", // T[]*=T[]
"_arraySliceSliceMulass_f", // T[]*=T[]
"_arraySliceSliceMulass_i",
"_arraySliceSliceMulass_k",
"_arraySliceSliceMulass_s",
"_arraySliceSliceMulass_t",
"_arraySliceSliceMulass_u",
"_arraySliceSliceMulass_w",
};
int i = binary(name, libArrayopFuncs, sizeof(libArrayopFuncs) / sizeof(char *));
if (i == -1)
{
#ifdef DEBUG // Make sure our array is alphabetized
for (i = 0; i < sizeof(libArrayopFuncs) / sizeof(char *); i++)
{
if (strcmp(name, libArrayopFuncs[i]) == 0)
assert(0);
}
#endif
#endif
/* Not in library, so generate it.
* Construct the function body:
* foreach (i; 0 .. p.length) for (size_t i = 0; i < p.length; i++)
* loopbody;
* return p;
*/
Parameters *fparams = new Parameters();
Expression *loopbody = buildArrayLoop(fparams);
Parameter *p = (*fparams)[0 /*fparams->dim - 1*/];
#if DMDV1
// for (size_t i = 0; i < p.length; i++)
Initializer *init = new ExpInitializer(0, new IntegerExp(0, 0, Type::tsize_t));
Dsymbol *d = new VarDeclaration(0, Type::tsize_t, Id::p, init);
Statement *s1 = new ForStatement(0,
new ExpStatement(0, d),
new CmpExp(TOKlt, 0, new IdentifierExp(0, Id::p), new ArrayLengthExp(0, new IdentifierExp(0, p->ident))),
new PostExp(TOKplusplus, 0, new IdentifierExp(0, Id::p)),
new ExpStatement(0, loopbody));
#else
// foreach (i; 0 .. p.length)
Statement *s1 = new ForeachRangeStatement(0, TOKforeach,
new Parameter(0, NULL, Id::p, NULL),
new IntegerExp(0, 0, Type::tsize_t),
new ArrayLengthExp(0, new IdentifierExp(0, p->ident)),
new ExpStatement(0, loopbody));
#endif
Statement *s2 = new ReturnStatement(0, new IdentifierExp(0, p->ident));
//printf("s2: %s\n", s2->toChars());
Statement *fbody = new CompoundStatement(0, s1, s2);
/* Construct the function
*/
TypeFunction *ftype = new TypeFunction(fparams, type, 0, LINKc);
//printf("ftype: %s\n", ftype->toChars());
fd = new FuncDeclaration(loc, 0, ident, STCundefined, ftype);
fd->fbody = fbody;
fd->protection = PROTpublic;
fd->linkage = LINKc;
fd->isArrayOp = 1;
sc->module->importedFrom->members->push(fd);
sc = sc->push();
sc->parent = sc->module->importedFrom;
sc->stc = 0;
sc->linkage = LINKc;
fd->semantic(sc);
fd->semantic2(sc);
fd->semantic3(sc);
sc->pop();
#if IN_DMD
}
else
{ /* In library, refer to it.
*/
fd = FuncDeclaration::genCfunc(type, ident);
}
*pfd = fd; // cache symbol in hash table
#elif IN_LLVM
sv->ptrvalue = fd; // cache symbol in hash table
#endif
}
/* Call the function fd(arguments)
*/
Expression *ec = new VarExp(0, fd);
Expression *e = new CallExp(loc, ec, arguments);
e->type = type;
return e;
}
/******************************************
* Construct the identifier for the array operation function,
* and build the argument list to pass to it.
*/
void Expression::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
buf->writestring("Exp");
arguments->shift(this);
}
void CastExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
Type *tb = type->toBasetype();
if (tb->ty == Tarray || tb->ty == Tsarray)
{
e1->buildArrayIdent(buf, arguments);
}
else
Expression::buildArrayIdent(buf, arguments);
}
void SliceExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
buf->writestring("Slice");
arguments->shift(this);
}
void AssignExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
/* Evaluate assign expressions right to left
*/
e2->buildArrayIdent(buf, arguments);
e1->buildArrayIdent(buf, arguments);
buf->writestring("Assign");
}
#define X(Str) \
void Str##AssignExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments) \
{ \
/* Evaluate assign expressions right to left \
*/ \
e2->buildArrayIdent(buf, arguments); \
e1->buildArrayIdent(buf, arguments); \
buf->writestring(#Str); \
buf->writestring("ass"); \
}
X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
#if DMDV2
X(Pow)
#endif
#undef X
void NegExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
e1->buildArrayIdent(buf, arguments);
buf->writestring("Neg");
}
void ComExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
e1->buildArrayIdent(buf, arguments);
buf->writestring("Com");
}
#define X(Str) \
void Str##Exp::buildArrayIdent(OutBuffer *buf, Expressions *arguments) \
{ \
/* Evaluate assign expressions left to right \
*/ \
e1->buildArrayIdent(buf, arguments); \
e2->buildArrayIdent(buf, arguments); \
buf->writestring(#Str); \
}
X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
#if DMDV2
X(Pow)
#endif
#undef X
/******************************************
* Construct the inner loop for the array operation function,
* and build the parameter list.
*/
Expression *Expression::buildArrayLoop(Parameters *fparams)
{
Identifier *id = Identifier::generateId("c", fparams->dim);
Parameter *param = new Parameter(0, type, id, NULL);
fparams->shift(param);
Expression *e = new IdentifierExp(0, id);
return e;
}
Expression *CastExp::buildArrayLoop(Parameters *fparams)
{
Type *tb = type->toBasetype();
if (tb->ty == Tarray || tb->ty == Tsarray)
{
return e1->buildArrayLoop(fparams);
}
else
return Expression::buildArrayLoop(fparams);
}
Expression *SliceExp::buildArrayLoop(Parameters *fparams)
{
Identifier *id = Identifier::generateId("p", fparams->dim);
Parameter *param = new Parameter(STCconst, type, id, NULL);
fparams->shift(param);
Expression *e = new IdentifierExp(0, id);
Expressions *arguments = new Expressions();
Expression *index = new IdentifierExp(0, Id::p);
arguments->push(index);
e = new ArrayExp(0, e, arguments);
return e;
}
Expression *AssignExp::buildArrayLoop(Parameters *fparams)
{
/* Evaluate assign expressions right to left
*/
Expression *ex2 = e2->buildArrayLoop(fparams);
#if DMDV2
/* Need the cast because:
* b = c + p[i];
* where b is a byte fails because (c + p[i]) is an int
* which cannot be implicitly cast to byte.
*/
ex2 = new CastExp(0, ex2, e1->type->nextOf());
#endif
Expression *ex1 = e1->buildArrayLoop(fparams);
Parameter *param = (*fparams)[0];
param->storageClass = 0;
Expression *e = new AssignExp(0, ex1, ex2);
return e;
}
#define X(Str) \
Expression *Str##AssignExp::buildArrayLoop(Parameters *fparams) \
{ \
/* Evaluate assign expressions right to left \
*/ \
Expression *ex2 = e2->buildArrayLoop(fparams); \
Expression *ex1 = e1->buildArrayLoop(fparams); \
Parameter *param = (*fparams)[0]; \
param->storageClass = 0; \
Expression *e = new Str##AssignExp(loc, ex1, ex2); \
return e; \
}
X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
#if DMDV2
X(Pow)
#endif
#undef X
Expression *NegExp::buildArrayLoop(Parameters *fparams)
{
Expression *ex1 = e1->buildArrayLoop(fparams);
Expression *e = new NegExp(0, ex1);
return e;
}
Expression *ComExp::buildArrayLoop(Parameters *fparams)
{
Expression *ex1 = e1->buildArrayLoop(fparams);
Expression *e = new ComExp(0, ex1);
return e;
}
#define X(Str) \
Expression *Str##Exp::buildArrayLoop(Parameters *fparams) \
{ \
/* Evaluate assign expressions left to right \
*/ \
Expression *ex1 = e1->buildArrayLoop(fparams); \
Expression *ex2 = e2->buildArrayLoop(fparams); \
Expression *e = new Str##Exp(0, ex1, ex2); \
return e; \
}
X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
#if DMDV2
X(Pow)
#endif
#undef X
/***********************************************
* Test if operand is a valid array op operand.
*/
int Expression::isArrayOperand()
{
//printf("Expression::isArrayOperand() %s\n", toChars());
if (op == TOKslice)
return 1;
if (type->toBasetype()->ty == Tarray)
{
switch (op)
{
case TOKadd:
case TOKmin:
case TOKmul:
case TOKdiv:
case TOKmod:
case TOKxor:
case TOKand:
case TOKor:
case TOKassign:
case TOKaddass:
case TOKminass:
case TOKmulass:
case TOKdivass:
case TOKmodass:
case TOKxorass:
case TOKandass:
case TOKorass:
#if DMDV2
case TOKpow:
case TOKpowass:
#endif
case TOKneg:
case TOKtilde:
return 1;
default:
break;
}
}
return 0;
}

View File

@@ -1,74 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 2006-2007 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_ARRAYTYPES_H
#define DMD_ARRAYTYPES_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
struct Expression;
struct Statement;
struct BaseClass;
struct TemplateParameter;
struct FuncDeclaration;
struct Identifier;
struct Initializer;
typedef ArrayBase<struct TemplateParameter> TemplateParameters;
typedef ArrayBase<struct Expression> Expressions;
typedef ArrayBase<struct Statement> Statements;
typedef ArrayBase<struct BaseClass> BaseClasses;
typedef ArrayBase<struct ClassDeclaration> ClassDeclarations;
typedef ArrayBase<struct Dsymbol> Dsymbols;
typedef ArrayBase<struct Object> Objects;
typedef ArrayBase<struct FuncDeclaration> FuncDeclarations;
typedef ArrayBase<struct Parameter> Parameters;
typedef ArrayBase<struct Identifier> Identifiers;
typedef ArrayBase<struct Initializer> Initializers;
typedef ArrayBase<struct VarDeclaration> VarDeclarations;
typedef ArrayBase<struct Type> Types;
typedef ArrayBase<struct ScopeDsymbol> ScopeDsymbols;
typedef ArrayBase<struct Catch> Catches;
typedef ArrayBase<struct StaticDtorDeclaration> StaticDtorDeclarations;
typedef ArrayBase<struct SharedStaticDtorDeclaration> SharedStaticDtorDeclarations;
typedef ArrayBase<struct Module> Modules;
typedef ArrayBase<struct CaseStatement> CaseStatements;
typedef ArrayBase<struct CompoundStatement> CompoundStatements;
typedef ArrayBase<struct GotoCaseStatement> GotoCaseStatements;
typedef ArrayBase<struct TemplateInstance> TemplateInstances;
typedef ArrayBase<struct block> Blocks;
typedef ArrayBase<struct Symbol> Symbols;
#endif

View File

@@ -1,117 +0,0 @@
The "Artistic License"
Preamble
The intent of this document is to state the conditions under which a
Package may be copied, such that the Copyright Holder maintains some
semblance of artistic control over the development of the package,
while giving the users of the package the right to use and distribute
the Package in a more-or-less customary fashion, plus the right to make
reasonable modifications.
Definitions:
"Package" refers to the collection of files distributed by the
Copyright Holder, and derivatives of that collection of files
created through textual modification.
"Standard Version" refers to such a Package if it has not been
modified, or has been modified in accordance with the wishes
of the Copyright Holder as specified below.
"Copyright Holder" is whoever is named in the copyright or
copyrights for the package.
"You" is you, if you're thinking about copying or distributing
this Package.
"Reasonable copying fee" is whatever you can justify on the
basis of media cost, duplication charges, time of people involved,
and so on. (You will not be required to justify it to the
Copyright Holder, but only to the computing community at large
as a market that must bear the fee.)
"Freely Available" means that no fee is charged for the item
itself, though there may be fees involved in handling the item.
It also means that recipients of the item may redistribute it
under the same conditions they received it.
1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.
2. You may apply bug fixes, portability fixes and other modifications
derived from the Public Domain or from the Copyright Holder. A Package
modified in such a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided
that you insert a prominent notice in each changed file stating how and
when you changed that file, and provided that you do at least ONE of the
following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or
an equivalent medium, or placing the modifications on a major archive
site such as uunet.uu.net, or by allowing the Copyright Holder to include
your modifications in the Standard Version of the Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict
with standard executables, which must also be provided, and provide
a separate manual page for each non-standard executable that clearly
documents how it differs from the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
4. You may distribute the programs of this Package in object code or
executable form, provided that you do at least ONE of the following:
a) distribute a Standard Version of the executables and library files,
together with instructions (in the manual page or equivalent) on where
to get the Standard Version.
b) accompany the distribution with the machine-readable source of
the Package with your modifications.
c) give non-standard executables non-standard names, and clearly
document the differences in manual pages (or equivalent), together
with instructions on where to get the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
5. You may charge a reasonable copying fee for any distribution of this
Package. You may charge any fee you choose for support of this
Package. You may not charge a fee for this Package itself. However,
you may distribute this Package in aggregate with other (possibly
commercial) programs as part of a larger (possibly commercial) software
distribution provided that you do not advertise this Package as a
product of your own. You may embed this Package's interpreter within
an executable of yours (by linking); this shall be construed as a mere
form of aggregation, provided that the complete Standard Version of the
interpreter is so embedded.
6. The source code and object code supplied as input to or produced as
output from the programs of this Package do not automatically fall
under the copyright of this Package, but belong to whoever generated
them, and may be sold commercially, and may be aggregated with this
Package.
7. Aggregation of this Package with a commercial distribution is always
permitted provided that the use of this Package is embedded; that is,
when no overt attempt is made to make this Package's interfaces visible
to the end user of the commercial distribution. Such use shall not be
construed as a distribution of this Package.
8. The name of the Copyright Holder may not be used to endorse or promote
products derived from this software without specific prior written permission.
9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The End

File diff suppressed because it is too large Load Diff

View File

@@ -1,204 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_ATTRIB_H
#define DMD_ATTRIB_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "dsymbol.h"
struct Expression;
struct Statement;
struct LabelDsymbol;
struct Initializer;
struct Module;
struct Condition;
struct HdrGenState;
/**************************************************************/
struct AttribDeclaration : Dsymbol
{
Dsymbols *decl; // array of Dsymbol's
AttribDeclaration(Dsymbols *decl);
virtual Dsymbols *include(Scope *sc, ScopeDsymbol *s);
int apply(Dsymbol_apply_ft_t fp, void *param);
int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
void setScopeNewSc(Scope *sc,
StorageClass newstc, enum LINK linkage, enum PROT protection, int explictProtection,
structalign_t structalign);
void semanticNewSc(Scope *sc,
StorageClass newstc, enum LINK linkage, enum PROT protection, int explictProtection,
structalign_t structalign);
void semantic(Scope *sc);
void semantic2(Scope *sc);
void semantic3(Scope *sc);
void inlineScan();
void addComment(unsigned char *comment);
void emitComment(Scope *sc);
const char *kind();
int oneMember(Dsymbol **ps);
void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
int hasPointers();
bool hasStaticCtorOrDtor();
void checkCtorConstInit();
void addLocalClass(ClassDeclarations *);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toJsonBuffer(OutBuffer *buf);
AttribDeclaration *isAttribDeclaration() { return this; }
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
#endif
#if IN_LLVM
virtual void codegen(Ir*);
#endif
};
struct StorageClassDeclaration: AttribDeclaration
{
StorageClass stc;
StorageClassDeclaration(StorageClass stc, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void setScope(Scope *sc);
void semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
static void stcToCBuffer(OutBuffer *buf, StorageClass stc);
};
struct LinkDeclaration : AttribDeclaration
{
enum LINK linkage;
LinkDeclaration(enum LINK p, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void setScope(Scope *sc);
void semantic(Scope *sc);
void semantic3(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
char *toChars();
};
struct ProtDeclaration : AttribDeclaration
{
enum PROT protection;
ProtDeclaration(enum PROT p, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void importAll(Scope *sc);
void setScope(Scope *sc);
void semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
static void protectionToCBuffer(OutBuffer *buf, enum PROT protection);
};
struct AlignDeclaration : AttribDeclaration
{
unsigned salign;
AlignDeclaration(Loc loc, unsigned sa, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void setScope(Scope *sc);
void semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct AnonDeclaration : AttribDeclaration
{
bool isunion;
structalign_t alignment;
int sem; // 1 if successful semantic()
AnonDeclaration(Loc loc, int isunion, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
const char *kind();
};
struct PragmaDeclaration : AttribDeclaration
{
Expressions *args; // array of Expression's
PragmaDeclaration(Loc loc, Identifier *ident, Expressions *args, Dsymbols *decl);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
void setScope(Scope *sc);
int oneMember(Dsymbol **ps);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
const char *kind();
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
#endif
#if IN_LLVM
void codegen(Ir*);
#endif
};
struct ConditionalDeclaration : AttribDeclaration
{
Condition *condition;
Dsymbols *elsedecl; // array of Dsymbol's for else block
ConditionalDeclaration(Condition *condition, Dsymbols *decl, Dsymbols *elsedecl);
Dsymbol *syntaxCopy(Dsymbol *s);
int oneMember(Dsymbol **ps);
void emitComment(Scope *sc);
Dsymbols *include(Scope *sc, ScopeDsymbol *s);
void addComment(unsigned char *comment);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toJsonBuffer(OutBuffer *buf);
void importAll(Scope *sc);
void setScope(Scope *sc);
};
struct StaticIfDeclaration : ConditionalDeclaration
{
ScopeDsymbol *sd;
int addisdone;
StaticIfDeclaration(Condition *condition, Dsymbols *decl, Dsymbols *elsedecl);
Dsymbol *syntaxCopy(Dsymbol *s);
int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
void semantic(Scope *sc);
void importAll(Scope *sc);
void setScope(Scope *sc);
const char *kind();
};
// Mixin declarations
struct CompileDeclaration : AttribDeclaration
{
Expression *exp;
ScopeDsymbol *sd;
int compiled;
CompileDeclaration(Loc loc, Expression *exp);
Dsymbol *syntaxCopy(Dsymbol *s);
int addMember(Scope *sc, ScopeDsymbol *sd, int memnum);
void compileIt(Scope *sc);
void semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
#endif /* DMD_ATTRIB_H */

1676
dmd/cast.c

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,175 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "root.h"
#include "aggregate.h"
#include "scope.h"
#include "mtype.h"
#include "declaration.h"
#include "module.h"
#include "id.h"
#include "expression.h"
#include "statement.h"
/*********************************
* Generate expression that calls opClone()
* for each member of the struct
* (can be NULL for members that don't need one)
*/
#if DMDV2
Expression *StructDeclaration::cloneMembers()
{
Expression *e = NULL;
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = (Dsymbol *)fields.data[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->storage_class & STCfield);
Type *tv = v->type->toBasetype();
dinteger_t dim = (tv->ty == Tsarray ? 1 : 0);
while (tv->ty == Tsarray)
{ TypeSArray *ta = (TypeSArray *)tv;
dim *= ((TypeSArray *)tv)->dim->toInteger();
tv = tv->nextOf()->toBasetype();
}
if (tv->ty == Tstruct)
{ TypeStruct *ts = (TypeStruct *)tv;
StructDeclaration *sd = ts->sym;
if (sd->opclone)
{
// this.v
Expression *ex = new ThisExp(0);
ex = new DotVarExp(0, ex, v, 0);
if (dim == 1)
{ // this.v.opClone()
ex = new DotVarExp(0, ex, sd->opclone, 0);
ex = new CallExp(0, ex);
}
else
{
// _callOpClones(&this.v, opclone, dim)
Expressions *args = new Expressions();
args->push(new AddrExp(0, ex));
args->push(new SymOffExp(0, sd->opclone, 0));
args->push(new IntegerExp(dim));
FuncDeclaration *ec = FuncDeclaration::genCfunc(Type::tvoid, "_callOpClones");
ex = new CallExp(0, new VarExp(0, ec), args);
}
e = Expression::combine(e, ex);
}
}
}
return e;
}
#endif
/*****************************************
* Create inclusive destructor for struct by aggregating
* all the destructors in dtors[] with the destructors for
* all the members.
*/
FuncDeclaration *AggregateDeclaration::buildDtor(Scope *sc)
{
//printf("StructDeclaration::buildDtor() %s\n", toChars());
Expression *e = NULL;
#if DMDV2
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = (Dsymbol *)fields.data[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->storage_class & STCfield);
Type *tv = v->type->toBasetype();
dinteger_t dim = (tv->ty == Tsarray ? 1 : 0);
while (tv->ty == Tsarray)
{ TypeSArray *ta = (TypeSArray *)tv;
dim *= ((TypeSArray *)tv)->dim->toInteger();
tv = tv->nextOf()->toBasetype();
}
if (tv->ty == Tstruct)
{ TypeStruct *ts = (TypeStruct *)tv;
StructDeclaration *sd = ts->sym;
if (sd->dtor)
{ Expression *ex;
// this.v
ex = new ThisExp(0);
ex = new DotVarExp(0, ex, v, 0);
if (dim == 1)
{ // this.v.dtor()
ex = new DotVarExp(0, ex, sd->dtor, 0);
ex = new CallExp(0, ex);
}
else
{
// Typeinfo.destroy(cast(void*)&this.v);
Expression *ea = new AddrExp(0, ex);
ea = new CastExp(0, ea, Type::tvoid->pointerTo());
Expressions *args = new Expressions();
args->push(ea);
Expression *et = v->type->getTypeInfo(sc);
et = new DotIdExp(0, et, Id::destroy);
ex = new CallExp(0, et, args);
}
e = Expression::combine(ex, e); // combine in reverse order
}
}
}
/* Build our own "destructor" which executes e
*/
if (e)
{ //printf("Building __fieldDtor()\n");
DtorDeclaration *dd = new DtorDeclaration(loc, 0, Lexer::idPool("__fieldDtor"));
dd->fbody = new ExpStatement(0, e);
dtors.shift(dd);
members->push(dd);
dd->semantic(sc);
}
#endif
switch (dtors.dim)
{
case 0:
return NULL;
case 1:
return (FuncDeclaration *)dtors.data[0];
default:
e = NULL;
for (size_t i = 0; i < dtors.dim; i++)
{ FuncDeclaration *fd = (FuncDeclaration *)dtors.data[i];
Expression *ex = new ThisExp(0);
ex = new DotVarExp(0, ex, fd);
ex = new CallExp(0, ex);
e = Expression::combine(ex, e);
}
DtorDeclaration *dd = new DtorDeclaration(loc, 0, Lexer::idPool("__aggrDtor"));
dd->fbody = new ExpStatement(0, e);
members->push(dd);
dd->semantic(sc);
return dd;
}
}

View File

@@ -1,76 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright and Burton Radons
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_COMPLEX_T_H
#define DMD_COMPLEX_T_H
/* Roll our own complex type for compilers that don't support complex
*/
struct complex_t
{
longdouble re;
longdouble im;
complex_t() { this->re = 0; this->im = 0; }
complex_t(longdouble re) { this->re = re; this->im = 0; }
complex_t(double re) { this->re = re; this->im = 0; }
complex_t(longdouble re, longdouble im) { this->re = re; this->im = im; }
complex_t(double re, double im) { this->re = re; this->im = im; }
complex_t operator + (complex_t y) { complex_t r; r.re = re + y.re; r.im = im + y.im; return r; }
complex_t operator - (complex_t y) { complex_t r; r.re = re - y.re; r.im = im - y.im; return r; }
complex_t operator - () { complex_t r; r.re = -re; r.im = -im; return r; }
complex_t operator * (complex_t y) { return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); }
complex_t operator / (complex_t y)
{
longdouble abs_y_re = y.re < 0 ? -y.re : y.re;
longdouble abs_y_im = y.im < 0 ? -y.im : y.im;
longdouble r, den;
if (abs_y_re < abs_y_im)
{
r = y.re / y.im;
den = y.im + r * y.re;
return complex_t((re * r + im) / den,
(im * r - re) / den);
}
else
{
r = y.im / y.re;
den = y.re + r * y.im;
return complex_t((re + r * im) / den,
(im - r * re) / den);
}
}
operator bool () { return re || im; }
int operator == (complex_t y) { return re == y.re && im == y.im; }
int operator != (complex_t y) { return re != y.re || im != y.im; }
};
inline complex_t operator * (longdouble x, complex_t y) { return complex_t(x) * y; }
inline complex_t operator * (complex_t x, longdouble y) { return x * complex_t(y); }
inline complex_t operator / (complex_t x, longdouble y) { return x / complex_t(y); }
inline longdouble creall(complex_t x)
{
return x.re;
}
inline longdouble cimagl(complex_t x)
{
return x.im;
}
#endif

View File

@@ -1,430 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include <string.h> // strcmp()
#include "id.h"
#include "init.h"
#include "declaration.h"
#include "identifier.h"
#include "expression.h"
#include "cond.h"
#include "module.h"
#include "template.h"
#include "lexer.h"
#include "mtype.h"
#include "scope.h"
#include "arraytypes.h"
int findCondition(Strings *ids, Identifier *ident)
{
if (ids)
{
for (size_t i = 0; i < ids->dim; i++)
{
const char *id = (*ids)[i];
if (strcmp(id, ident->toChars()) == 0)
return TRUE;
}
}
return FALSE;
}
/* ============================================================ */
Condition::Condition(Loc loc)
{
this->loc = loc;
inc = 0;
}
/* ============================================================ */
DVCondition::DVCondition(Module *mod, unsigned level, Identifier *ident)
: Condition(0)
{
this->mod = mod;
this->level = level;
this->ident = ident;
}
Condition *DVCondition::syntaxCopy()
{
return this; // don't need to copy
}
/* ============================================================ */
void DebugCondition::setGlobalLevel(unsigned level)
{
global.params.debuglevel = level;
}
void DebugCondition::addGlobalIdent(const char *ident)
{
if (!global.params.debugids)
global.params.debugids = new Strings();
global.params.debugids->push((char *)ident);
}
DebugCondition::DebugCondition(Module *mod, unsigned level, Identifier *ident)
: DVCondition(mod, level, ident)
{
}
int DebugCondition::include(Scope *sc, ScopeDsymbol *s)
{
//printf("DebugCondition::include() level = %d, debuglevel = %d\n", level, global.params.debuglevel);
if (inc == 0)
{
inc = 2;
if (ident)
{
if (findCondition(mod->debugids, ident))
inc = 1;
else if (findCondition(global.params.debugids, ident))
inc = 1;
else
{ if (!mod->debugidsNot)
mod->debugidsNot = new Strings();
mod->debugidsNot->push(ident->toChars());
}
}
else if (level <= global.params.debuglevel || level <= mod->debuglevel)
inc = 1;
}
return (inc == 1);
}
void DebugCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
if (ident)
buf->printf("debug (%s)", ident->toChars());
else
buf->printf("debug (%u)", level);
}
/* ============================================================ */
void VersionCondition::setGlobalLevel(unsigned level)
{
global.params.versionlevel = level;
}
void VersionCondition::checkPredefined(Loc loc, const char *ident)
{
static const char* reserved[] =
{
"DigitalMars", "X86", "X86_64",
"Windows", "Win32", "Win64",
"linux",
#if DMDV2
/* Although Posix is predefined by D1, disallowing its
* redefinition breaks makefiles and older builds.
*/
"Posix",
"D_NET",
#endif
"OSX", "FreeBSD",
"OpenBSD",
"Solaris",
"LittleEndian", "BigEndian",
"all",
"none",
#if IN_LLVM
"LLVM", "LDC", "LLVM64",
"PPC", "PPC64",
"darwin","solaris","freebsd"
#endif
};
for (unsigned i = 0; i < sizeof(reserved) / sizeof(reserved[0]); i++)
{
if (strcmp(ident, reserved[i]) == 0)
goto Lerror;
}
if (ident[0] == 'D' && ident[1] == '_')
goto Lerror;
return;
Lerror:
error(loc, "version identifier '%s' is reserved and cannot be set", ident);
}
void VersionCondition::addGlobalIdent(const char *ident)
{
checkPredefined(0, ident);
addPredefinedGlobalIdent(ident);
}
void VersionCondition::addPredefinedGlobalIdent(const char *ident)
{
if (!global.params.versionids)
global.params.versionids = new Strings();
global.params.versionids->push((char *)ident);
}
VersionCondition::VersionCondition(Module *mod, unsigned level, Identifier *ident)
: DVCondition(mod, level, ident)
{
}
int VersionCondition::include(Scope *sc, ScopeDsymbol *s)
{
//printf("VersionCondition::include() level = %d, versionlevel = %d\n", level, global.params.versionlevel);
//if (ident) printf("\tident = '%s'\n", ident->toChars());
if (inc == 0)
{
inc = 2;
if (ident)
{
if (findCondition(mod->versionids, ident))
inc = 1;
else if (findCondition(global.params.versionids, ident))
inc = 1;
else
{
if (!mod->versionidsNot)
mod->versionidsNot = new Strings();
mod->versionidsNot->push(ident->toChars());
}
}
else if (level <= global.params.versionlevel || level <= mod->versionlevel)
inc = 1;
}
return (inc == 1);
}
void VersionCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
if (ident)
buf->printf("version (%s)", ident->toChars());
else
buf->printf("version (%u)", level);
}
/**************************** StaticIfCondition *******************************/
StaticIfCondition::StaticIfCondition(Loc loc, Expression *exp)
: Condition(loc)
{
this->exp = exp;
this->nest = 0;
}
Condition *StaticIfCondition::syntaxCopy()
{
return new StaticIfCondition(loc, exp->syntaxCopy());
}
int StaticIfCondition::include(Scope *sc, ScopeDsymbol *s)
{
#if 0
printf("StaticIfCondition::include(sc = %p, s = %p) this=%p inc = %d\n", sc, s, this, inc);
if (s)
{
printf("\ts = '%s', kind = %s\n", s->toChars(), s->kind());
}
#endif
if (inc == 0)
{
if (exp->op == TOKerror || nest > 100)
{
error(loc, (nest > 1000) ? "unresolvable circular static if expression"
: "error evaluating static if expression");
if (!global.gag)
inc = 2; // so we don't see the error message again
return 0;
}
if (!sc)
{
error(loc, "static if conditional cannot be at global scope");
inc = 2;
return 0;
}
++nest;
sc = sc->push(sc->scopesym);
sc->sd = s; // s gets any addMember()
sc->flags |= SCOPEstaticif;
Expression *e = exp->semantic(sc);
sc->pop();
if (!e->type->checkBoolean())
{
if (e->type->toBasetype() != Type::terror)
exp->error("expression %s of type %s does not have a boolean value", exp->toChars(), e->type->toChars());
inc = 0;
return 0;
}
e = e->ctfeInterpret();
--nest;
if (e->op == TOKerror)
{ exp = e;
inc = 0;
}
else if (e->isBool(TRUE))
inc = 1;
else if (e->isBool(FALSE))
inc = 2;
else
{
e->error("expression %s is not constant or does not evaluate to a bool", e->toChars());
inc = 2;
}
}
return (inc == 1);
}
void StaticIfCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("static if(");
exp->toCBuffer(buf, hgs);
buf->writeByte(')');
}
/**************************** IftypeCondition *******************************/
IftypeCondition::IftypeCondition(Loc loc, Type *targ, Identifier *id, enum TOK tok, Type *tspec)
: Condition(loc)
{
this->targ = targ;
this->id = id;
this->tok = tok;
this->tspec = tspec;
}
Condition *IftypeCondition::syntaxCopy()
{
return new IftypeCondition(loc,
targ->syntaxCopy(),
id,
tok,
tspec ? tspec->syntaxCopy() : NULL);
}
int IftypeCondition::include(Scope *sc, ScopeDsymbol *sd)
{
//printf("IftypeCondition::include()\n");
if (inc == 0)
{
if (!sc)
{
error(loc, "iftype conditional cannot be at global scope");
inc = 2;
return 0;
}
Type *t = targ->trySemantic(loc, sc);
if (t)
targ = t;
else
inc = 2; // condition is false
if (!t)
{
}
else if (id && tspec)
{
/* Evaluate to TRUE if targ matches tspec.
* If TRUE, declare id as an alias for the specialized type.
*/
MATCH m;
TemplateTypeParameter tp(loc, id, NULL, NULL);
TemplateParameters parameters;
parameters.setDim(1);
parameters[0] = &tp;
Objects dedtypes;
dedtypes.setDim(1);
m = targ->deduceType(sc, tspec, &parameters, &dedtypes);
if (m == MATCHnomatch ||
(m != MATCHexact && tok == TOKequal))
inc = 2;
else
{
inc = 1;
Type *tded = (Type *)dedtypes[0];
if (!tded)
tded = targ;
Dsymbol *s = new AliasDeclaration(loc, id, tded);
s->semantic(sc);
sc->insert(s);
if (sd)
s->addMember(sc, sd, 1);
}
}
else if (id)
{
/* Declare id as an alias for type targ. Evaluate to TRUE
*/
Dsymbol *s = new AliasDeclaration(loc, id, targ);
s->semantic(sc);
sc->insert(s);
if (sd)
s->addMember(sc, sd, 1);
inc = 1;
}
else if (tspec)
{
/* Evaluate to TRUE if targ matches tspec
*/
tspec = tspec->semantic(loc, sc);
//printf("targ = %s\n", targ->toChars());
//printf("tspec = %s\n", tspec->toChars());
if (tok == TOKcolon)
{ if (targ->implicitConvTo(tspec))
inc = 1;
else
inc = 2;
}
else /* == */
{ if (targ->equals(tspec))
inc = 1;
else
inc = 2;
}
}
else
inc = 1;
//printf("inc = %d\n", inc);
}
return (inc == 1);
}
void IftypeCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("iftype(");
targ->toCBuffer(buf, id, hgs);
if (tspec)
{
if (tok == TOKcolon)
buf->writestring(" : ");
else
buf->writestring(" == ");
tspec->toCBuffer(buf, NULL, hgs);
}
buf->writeByte(')');
}

View File

@@ -1,106 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_DEBCOND_H
#define DMD_DEBCOND_H
struct Expression;
struct Identifier;
struct OutBuffer;
struct Module;
struct Scope;
struct ScopeDsymbol;
struct DebugCondition;
#include "lexer.h" // dmdhg
enum TOK;
struct HdrGenState;
int findCondition(Strings *ids, Identifier *ident);
struct Condition
{
Loc loc;
int inc; // 0: not computed yet
// 1: include
// 2: do not include
Condition(Loc loc);
virtual Condition *syntaxCopy() = 0;
virtual int include(Scope *sc, ScopeDsymbol *s) = 0;
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs) = 0;
virtual DebugCondition *isDebugCondition() { return NULL; }
};
struct DVCondition : Condition
{
unsigned level;
Identifier *ident;
Module *mod;
DVCondition(Module *mod, unsigned level, Identifier *ident);
Condition *syntaxCopy();
};
struct DebugCondition : DVCondition
{
static void setGlobalLevel(unsigned level);
static void addGlobalIdent(const char *ident);
static void addPredefinedGlobalIdent(const char *ident);
DebugCondition(Module *mod, unsigned level, Identifier *ident);
int include(Scope *sc, ScopeDsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
DebugCondition *isDebugCondition() { return this; }
};
struct VersionCondition : DVCondition
{
static void setGlobalLevel(unsigned level);
static void checkPredefined(Loc loc, const char *ident);
static void addGlobalIdent(const char *ident);
static void addPredefinedGlobalIdent(const char *ident);
VersionCondition(Module *mod, unsigned level, Identifier *ident);
int include(Scope *sc, ScopeDsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct StaticIfCondition : Condition
{
Expression *exp;
int nest; // limit circular dependencies
StaticIfCondition(Loc loc, Expression *exp);
Condition *syntaxCopy();
int include(Scope *sc, ScopeDsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct IftypeCondition : Condition
{
/* iftype (targ id tok tspec)
*/
Type *targ;
Identifier *id; // can be NULL
enum TOK tok; // ':' or '=='
Type *tspec; // can be NULL
IftypeCondition(Loc loc, Type *targ, Identifier *id, enum TOK tok, Type *tspec);
Condition *syntaxCopy();
int include(Scope *sc, ScopeDsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,140 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "mars.h"
#include "expression.h"
#include "statement.h"
#include "mtype.h"
#include "utf.h"
#include "declaration.h"
#include "aggregate.h"
#include "scope.h"
/********************************************
* Convert from expression to delegate that returns the expression,
* i.e. convert:
* expr
* to:
* t delegate() { return expr; }
*/
int lambdaSetParent(Expression *e, void *param);
int lambdaCheckForNestedRef(Expression *e, void *param);
Expression *Expression::toDelegate(Scope *sc, Type *t)
{
//printf("Expression::toDelegate(t = %s) %s\n", t->toChars(), toChars());
TypeFunction *tf = new TypeFunction(NULL, t, 0, LINKd);
FuncLiteralDeclaration *fld =
new FuncLiteralDeclaration(loc, loc, tf, TOKdelegate, NULL);
Expression *e;
sc = sc->push();
sc->parent = fld; // set current function to be the delegate
e = this;
e->apply(&lambdaSetParent, sc);
e->apply(&lambdaCheckForNestedRef, sc);
sc = sc->pop();
Statement *s;
if (t->ty == Tvoid)
s = new ExpStatement(loc, e);
else
s = new ReturnStatement(loc, e);
fld->fbody = s;
e = new FuncExp(loc, fld);
e = e->semantic(sc);
return e;
}
/******************************************
* Patch the parent of declarations to be the new function literal.
*/
int lambdaSetParent(Expression *e, void *param)
{
Scope *sc = (Scope *)param;
/* We could use virtual functions instead of a switch,
* but it doesn't seem worth the bother.
*/
switch (e->op)
{
case TOKdeclaration:
{ DeclarationExp *de = (DeclarationExp *)e;
de->declaration->parent = sc->parent;
break;
}
case TOKindex:
{ IndexExp *de = (IndexExp *)e;
if (de->lengthVar)
{ //printf("lengthVar\n");
de->lengthVar->parent = sc->parent;
}
break;
}
case TOKslice:
{ SliceExp *se = (SliceExp *)e;
if (se->lengthVar)
{ //printf("lengthVar\n");
se->lengthVar->parent = sc->parent;
}
break;
}
default:
break;
}
return 0;
}
/*******************************************
* Look for references to variables in a scope enclosing the new function literal.
*/
int lambdaCheckForNestedRef(Expression *e, void *param)
{
Scope *sc = (Scope *)param;
/* We could use virtual functions instead of a switch,
* but it doesn't seem worth the bother.
*/
switch (e->op)
{
case TOKsymoff:
{ SymOffExp *se = (SymOffExp *)e;
VarDeclaration *v = se->var->isVarDeclaration();
if (v)
v->checkNestedReference(sc, 0);
break;
}
case TOKvar:
{ VarExp *ve = (VarExp *)e;
VarDeclaration *v = ve->var->isVarDeclaration();
if (v)
v->checkNestedReference(sc, 0);
break;
}
case TOKthis:
case TOKsuper:
{ ThisExp *te = (ThisExp *)e;
VarDeclaration *v = te->var->isVarDeclaration();
if (v)
v->checkNestedReference(sc, 0);
break;
}
default:
break;
}
return 0;
}

2230
dmd/doc.c

File diff suppressed because it is too large Load Diff

View File

@@ -1,20 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_DOC_H
#define DMD_DOC_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
void escapeDdocString(OutBuffer *buf, unsigned start);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,387 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_DSYMBOL_H
#define DMD_DSYMBOL_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "stringtable.h"
#include "mars.h"
#include "arraytypes.h"
#if IN_LLVM
#if defined(_MSC_VER)
#undef min
#undef max
#endif
#include "../ir/irdsymbol.h"
#endif
struct Identifier;
struct Scope;
struct DsymbolTable;
struct Declaration;
struct ThisDeclaration;
struct TupleDeclaration;
struct TypedefDeclaration;
struct AliasDeclaration;
struct AggregateDeclaration;
struct EnumDeclaration;
struct ClassDeclaration;
struct InterfaceDeclaration;
struct StructDeclaration;
struct UnionDeclaration;
struct FuncDeclaration;
struct FuncAliasDeclaration;
struct FuncLiteralDeclaration;
struct CtorDeclaration;
struct PostBlitDeclaration;
struct DtorDeclaration;
struct StaticCtorDeclaration;
struct StaticDtorDeclaration;
struct InvariantDeclaration;
struct UnitTestDeclaration;
struct NewDeclaration;
struct VarDeclaration;
struct AttribDeclaration;
#if IN_DMD
struct Symbol;
#endif
struct Package;
struct Module;
struct Import;
struct Type;
struct TypeTuple;
struct WithStatement;
struct LabelDsymbol;
struct ScopeDsymbol;
struct TemplateDeclaration;
struct TemplateInstance;
struct TemplateMixin;
struct EnumMember;
struct ScopeDsymbol;
struct WithScopeSymbol;
struct ArrayScopeSymbol;
struct StaticStructInitDeclaration;
struct Expression;
struct DeleteDeclaration;
struct HdrGenState;
struct TypeInfoDeclaration;
struct ClassInfoDeclaration;
struct OverloadSet;
struct AA;
#if TARGET_NET
struct PragmaScope;
#endif
#if IN_GCC
union tree_node;
typedef union tree_node TYPE;
#else
struct TYPE;
#endif
// Back end
struct Classsym;
#if IN_LLVM
class Ir;
namespace llvm
{
class Value;
}
#endif
enum PROT
{
PROTundefined,
PROTnone, // no access
PROTprivate,
PROTpackage,
PROTprotected,
PROTpublic,
PROTexport,
};
/* State of symbol in winding its way through the passes of the compiler
*/
enum PASS
{
PASSinit, // initial state
PASSsemantic, // semantic() started
PASSsemanticdone, // semantic() done
PASSsemantic2, // semantic2() run
PASSsemantic3, // semantic3() started
PASSsemantic3done, // semantic3() done
PASSobj, // toObjFile() run
};
typedef int (*Dsymbol_apply_ft_t)(Dsymbol *, void *);
struct Dsymbol : Object
{
Identifier *ident;
Identifier *c_ident;
Dsymbol *parent;
#if IN_DMD
Symbol *csym; // symbol for code generator
Symbol *isym; // import version of csym
#endif
unsigned char *comment; // documentation comment for this Dsymbol
Loc loc; // where defined
Scope *scope; // !=NULL means context to use for semantic()
bool errors; // this symbol failed to pass semantic()
Dsymbol();
Dsymbol(Identifier *);
char *toChars();
char *locToChars();
int equals(Object *o);
int isAnonymous();
void error(Loc loc, const char *format, ...) IS_PRINTF(3);
void error(const char *format, ...) IS_PRINTF(2);
void checkDeprecated(Loc loc, Scope *sc);
Module *getModule(); // module where declared
Module *getCompilationModule(); // possibly different for templates
Dsymbol *pastMixin();
Dsymbol *toParent();
Dsymbol *toParent2();
TemplateInstance *inTemplateInstance();
TemplateInstance *isSpeculative();
int dyncast() { return DYNCAST_DSYMBOL; } // kludge for template.isSymbol()
static Dsymbols *arraySyntaxCopy(Dsymbols *a);
virtual const char *toPrettyChars();
virtual const char *kind();
virtual Dsymbol *toAlias(); // resolve real symbol
virtual int apply(Dsymbol_apply_ft_t fp, void *param);
virtual int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
virtual void setScope(Scope *sc);
virtual void importAll(Scope *sc);
virtual void semantic0(Scope *sc);
virtual void semantic(Scope *sc);
virtual void semantic2(Scope *sc);
virtual void semantic3(Scope *sc);
virtual void inlineScan();
virtual Dsymbol *search(Loc loc, Identifier *ident, int flags);
Dsymbol *search_correct(Identifier *id);
Dsymbol *searchX(Loc loc, Scope *sc, Identifier *id);
virtual int overloadInsert(Dsymbol *s);
char *toHChars();
virtual void toHBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual void toDocBuffer(OutBuffer *buf);
virtual void toJsonBuffer(OutBuffer *buf);
virtual unsigned size(Loc loc);
virtual int isforwardRef();
virtual void defineRef(Dsymbol *s);
virtual AggregateDeclaration *isThis(); // is a 'this' required to access the member
virtual ClassDeclaration *isClassMember(); // are we a member of a class?
virtual int isExport(); // is Dsymbol exported?
virtual int isImportedSymbol(); // is Dsymbol imported?
virtual int isDeprecated(); // is Dsymbol deprecated?
#if DMDV2
virtual int isOverloadable();
#endif
virtual int hasOverloads();
virtual LabelDsymbol *isLabel(); // is this a LabelDsymbol?
virtual AggregateDeclaration *isMember(); // is this symbol a member of an AggregateDeclaration?
virtual Type *getType(); // is this a type?
virtual char *mangle();
virtual int needThis(); // need a 'this' pointer?
virtual enum PROT prot();
virtual Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees
virtual int oneMember(Dsymbol **ps);
static int oneMembers(Dsymbols *members, Dsymbol **ps);
virtual void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
virtual int hasPointers();
virtual bool hasStaticCtorOrDtor();
virtual void addLocalClass(ClassDeclarations *) { }
virtual void checkCtorConstInit() { }
virtual void addComment(unsigned char *comment);
virtual void emitComment(Scope *sc);
void emitDitto(Scope *sc);
#if IN_DMD
// Backend
virtual Symbol *toSymbol(); // to backend symbol
virtual void toObjFile(int multiobj); // compile to .obj file
virtual int cvMember(unsigned char *p); // emit cv debug info for member
Symbol *toImport(); // to backend import symbol
static Symbol *toImport(Symbol *s); // to backend import symbol
Symbol *toSymbolX(const char *prefix, int sclass, TYPE *t, const char *suffix); // helper
#endif
// Eliminate need for dynamic_cast
virtual Package *isPackage() { return NULL; }
virtual Module *isModule() { return NULL; }
virtual EnumMember *isEnumMember() { return NULL; }
virtual TemplateDeclaration *isTemplateDeclaration() { return NULL; }
virtual TemplateInstance *isTemplateInstance() { return NULL; }
virtual TemplateMixin *isTemplateMixin() { return NULL; }
virtual Declaration *isDeclaration() { return NULL; }
virtual ThisDeclaration *isThisDeclaration() { return NULL; }
virtual TupleDeclaration *isTupleDeclaration() { return NULL; }
virtual TypedefDeclaration *isTypedefDeclaration() { return NULL; }
virtual AliasDeclaration *isAliasDeclaration() { return NULL; }
virtual AggregateDeclaration *isAggregateDeclaration() { return NULL; }
virtual FuncDeclaration *isFuncDeclaration() { return NULL; }
virtual FuncAliasDeclaration *isFuncAliasDeclaration() { return NULL; }
virtual FuncLiteralDeclaration *isFuncLiteralDeclaration() { return NULL; }
virtual CtorDeclaration *isCtorDeclaration() { return NULL; }
virtual PostBlitDeclaration *isPostBlitDeclaration() { return NULL; }
virtual DtorDeclaration *isDtorDeclaration() { return NULL; }
virtual StaticCtorDeclaration *isStaticCtorDeclaration() { return NULL; }
virtual StaticDtorDeclaration *isStaticDtorDeclaration() { return NULL; }
virtual InvariantDeclaration *isInvariantDeclaration() { return NULL; }
virtual UnitTestDeclaration *isUnitTestDeclaration() { return NULL; }
virtual NewDeclaration *isNewDeclaration() { return NULL; }
virtual VarDeclaration *isVarDeclaration() { return NULL; }
virtual ClassDeclaration *isClassDeclaration() { return NULL; }
virtual StructDeclaration *isStructDeclaration() { return NULL; }
virtual UnionDeclaration *isUnionDeclaration() { return NULL; }
virtual InterfaceDeclaration *isInterfaceDeclaration() { return NULL; }
virtual ScopeDsymbol *isScopeDsymbol() { return NULL; }
virtual WithScopeSymbol *isWithScopeSymbol() { return NULL; }
virtual ArrayScopeSymbol *isArrayScopeSymbol() { return NULL; }
virtual Import *isImport() { return NULL; }
virtual EnumDeclaration *isEnumDeclaration() { return NULL; }
virtual DeleteDeclaration *isDeleteDeclaration() { return NULL; }
//virtual SymbolDeclaration *isSymbolDeclaration() { return NULL; }
virtual StaticStructInitDeclaration *isStaticStructInitDeclaration() { return NULL; }
virtual AttribDeclaration *isAttribDeclaration() { return NULL; }
virtual TypeInfoDeclaration* isTypeInfoDeclaration() { return NULL; }
virtual ClassInfoDeclaration* isClassInfoDeclaration() { return NULL; }
virtual OverloadSet *isOverloadSet() { return NULL; }
#if TARGET_NET
virtual PragmaScope* isPragmaScope() { return NULL; }
#endif
#if IN_LLVM
/// Codegen traversal
virtual void codegen(Ir* ir);
// llvm stuff
int llvmInternal;
IrDsymbol ir;
#endif
};
// Dsymbol that generates a scope
struct ScopeDsymbol : Dsymbol
{
Dsymbols *members; // all Dsymbol's in this scope
DsymbolTable *symtab; // members[] sorted into table
Dsymbols *imports; // imported Dsymbol's
unsigned char *prots; // array of PROT, one for each import
ScopeDsymbol();
ScopeDsymbol(Identifier *id);
Dsymbol *syntaxCopy(Dsymbol *s);
Dsymbol *search(Loc loc, Identifier *ident, int flags);
void importScope(Dsymbol *s, enum PROT protection);
int isforwardRef();
void defineRef(Dsymbol *s);
static void multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2);
Dsymbol *nameCollision(Dsymbol *s);
const char *kind();
FuncDeclaration *findGetMembers();
virtual Dsymbol *symtabInsert(Dsymbol *s);
bool hasStaticCtorOrDtor();
void emitMemberComments(Scope *sc);
static size_t dim(Dsymbols *members);
static Dsymbol *getNth(Dsymbols *members, size_t nth, size_t *pn = NULL);
typedef int (*ForeachDg)(void *ctx, size_t idx, Dsymbol *s);
static int foreach(Dsymbols *members, ForeachDg dg, void *ctx, size_t *pn=NULL);
ScopeDsymbol *isScopeDsymbol() { return this; }
};
// With statement scope
struct WithScopeSymbol : ScopeDsymbol
{
WithStatement *withstate;
WithScopeSymbol(WithStatement *withstate);
Dsymbol *search(Loc loc, Identifier *ident, int flags);
WithScopeSymbol *isWithScopeSymbol() { return this; }
};
// Array Index/Slice scope
struct ArrayScopeSymbol : ScopeDsymbol
{
Expression *exp; // IndexExp or SliceExp
TypeTuple *type; // for tuple[length]
TupleDeclaration *td; // for tuples of objects
Scope *sc;
ArrayScopeSymbol(Expression *e);
ArrayScopeSymbol(TypeTuple *t);
ArrayScopeSymbol(TupleDeclaration *td);
Dsymbol *search(Loc loc, Identifier *ident, int flags);
ArrayScopeSymbol *isArrayScopeSymbol() { return this; }
};
// Overload Sets
#if DMDV2
struct OverloadSet : Dsymbol
{
Dsymbols a; // array of Dsymbols
OverloadSet();
void push(Dsymbol *s);
OverloadSet *isOverloadSet() { return this; }
const char *kind();
};
#endif
// Table of Dsymbol's
struct DsymbolTable : Object
{
AA *tab;
DsymbolTable();
~DsymbolTable();
// Look up Identifier. Return Dsymbol if found, NULL if not.
Dsymbol *lookup(Identifier *ident);
// Insert Dsymbol in table. Return NULL if already there.
Dsymbol *insert(Dsymbol *s);
// Look for Dsymbol in table. If there, return it. If not, insert s and return that.
Dsymbol *update(Dsymbol *s);
Dsymbol *insert(Identifier *ident, Dsymbol *s); // when ident and s are not the same
};
#endif /* DMD_DSYMBOL_H */

View File

@@ -1,144 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include "mars.h"
#include "mtype.h"
#include "declaration.h"
#include "expression.h"
#include "template.h"
static void indent(int indent)
{
int i;
for (i = 0; i < indent; i++)
printf(" ");
}
static char *type_print(Type *type)
{
return type ? type->toChars() : (char *) "null";
}
void dumpExpressions(int i, Expressions *exps)
{
for (size_t j = 0; j < exps->dim; j++)
{ Expression *e = (Expression *)exps->data[j];
indent(i);
printf("(\n");
e->dump(i + 2);
indent(i);
printf(")\n");
}
}
void Expression::dump(int i)
{
indent(i);
printf("%p %s type=%s\n", this, Token::toChars(op), type_print(type));
}
void IntegerExp::dump(int i)
{
indent(i);
printf("%p %jd type=%s\n", this, (intmax_t)value, type_print(type));
}
void IdentifierExp::dump(int i)
{
indent(i);
printf("%p ident '%s' type=%s\n", this, ident->toChars(), type_print(type));
}
void DsymbolExp::dump(int i)
{
indent(i);
printf("%p %s type=%s\n", this, s->toChars(), type_print(type));
}
void VarExp::dump(int i)
{
indent(i);
printf("%p %s var=%s type=%s\n", this, Token::toChars(op), var->toChars(), type_print(type));
}
void UnaExp::dump(int i)
{
indent(i);
printf("%p %s type=%s e1=%p\n", this, Token::toChars(op), type_print(type), e1);
if (e1)
e1->dump(i + 2);
}
void CallExp::dump(int i)
{
UnaExp::dump(i);
dumpExpressions(i, arguments);
}
void SliceExp::dump(int i)
{
indent(i);
printf("%p %s type=%s e1=%p\n", this, Token::toChars(op), type_print(type), e1);
if (e1)
e1->dump(i + 2);
if (lwr)
lwr->dump(i + 2);
if (upr)
upr->dump(i + 2);
}
void DotIdExp::dump(int i)
{
indent(i);
printf("%p %s type=%s ident=%s e1=%p\n", this, Token::toChars(op), type_print(type), ident->toChars(), e1);
if (e1)
e1->dump(i + 2);
}
void DotVarExp::dump(int i)
{
indent(i);
printf("%p %s type=%s var='%s' e1=%p\n", this, Token::toChars(op), type_print(type), var->toChars(), e1);
if (e1)
e1->dump(i + 2);
}
void DotTemplateInstanceExp::dump(int i)
{
indent(i);
printf("%p %s type=%s ti='%s' e1=%p\n", this, Token::toChars(op), type_print(type), ti->toChars(), e1);
if (e1)
e1->dump(i + 2);
}
void DelegateExp::dump(int i)
{
indent(i);
printf("%p %s func=%s type=%s e1=%p\n", this, Token::toChars(op), func->toChars(), type_print(type), e1);
if (e1)
e1->dump(i + 2);
}
void BinExp::dump(int i)
{
indent(i);
printf("%p %s type=%s e1=%p e2=%p\n", this, Token::toChars(op), type_print(type), e1, e2);
if (e1)
e1->dump(i + 2);
if (e2)
e2->dump(i + 2);
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,373 +0,0 @@
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "root.h"
#include "enum.h"
#include "mtype.h"
#include "scope.h"
#include "module.h"
#include "declaration.h"
/********************************* EnumDeclaration ****************************/
EnumDeclaration::EnumDeclaration(Loc loc, Identifier *id, Type *memtype)
: ScopeDsymbol(id)
{
this->loc = loc;
type = new TypeEnum(this);
this->memtype = memtype;
maxval = 0;
minval = 0;
defaultval = 0;
#if IN_DMD
sinit = NULL;
#endif
isdeprecated = 0;
isdone = 0;
}
Dsymbol *EnumDeclaration::syntaxCopy(Dsymbol *s)
{
Type *t = NULL;
if (memtype)
t = memtype->syntaxCopy();
EnumDeclaration *ed;
if (s)
{ ed = (EnumDeclaration *)s;
ed->memtype = t;
}
else
ed = new EnumDeclaration(loc, ident, t);
ScopeDsymbol::syntaxCopy(ed);
return ed;
}
void EnumDeclaration::semantic0(Scope *sc)
{
/* This function is a hack to get around a significant problem.
* The members of anonymous enums, like:
* enum { A, B, C }
* don't get installed into the symbol table until after they are
* semantically analyzed, yet they're supposed to go into the enclosing
* scope's table. Hence, when forward referenced, they come out as
* 'undefined'. The real fix is to add them in at addSymbol() time.
* But to get code to compile, we'll just do this quick hack at the moment
* to compile it if it doesn't depend on anything else.
*/
if (isdone || !scope)
return;
if (!isAnonymous() || memtype)
return;
for (size_t i = 0; i < members->dim; i++)
{
EnumMember *em = ((Dsymbol *)members->data[i])->isEnumMember();
if (em && em->value)
return;
}
// Can do it
semantic(sc);
}
void EnumDeclaration::semantic(Scope *sc)
{
uinteger_t number;
Type *t;
Scope *sce;
//printf("EnumDeclaration::semantic(sd = %p, '%s')\n", sc->scopesym, sc->scopesym->toChars());
if (!memtype)
memtype = Type::tint32;
if (symtab) // if already done
{ if (isdone || !scope)
return; // semantic() already completed
}
else
symtab = new DsymbolTable();
Scope *scx = NULL;
if (scope)
{ sc = scope;
scx = scope; // save so we don't make redundant copies
scope = NULL;
}
unsigned dprogress_save = Module::dprogress;
if (sc->stc & STCdeprecated)
isdeprecated = 1;
parent = sc->scopesym;
memtype = memtype->semantic(loc, sc);
/* Check to see if memtype is forward referenced
*/
if (memtype->ty == Tenum)
{ EnumDeclaration *sym = (EnumDeclaration *)memtype->toDsymbol(sc);
if (!sym->memtype)
{
error("base enum %s is forward referenced", sym->toChars());
memtype = Type::tint32;
}
}
if (!memtype->isintegral())
{ error("base type must be of integral type, not %s", memtype->toChars());
memtype = Type::tint32;
}
isdone = 1;
Module::dprogress++;
t = isAnonymous() ? memtype : type;
symtab = new DsymbolTable();
sce = sc->push(this);
sce->parent = this;
number = 0;
if (!members) // enum ident;
return;
if (members->dim == 0)
error("enum %s must have at least one member", toChars());
int first = 1;
for (size_t i = 0; i < members->dim; i++)
{
EnumMember *em = ((Dsymbol *)members->data[i])->isEnumMember();
Expression *e;
if (!em)
/* The e->semantic(sce) can insert other symbols, such as
* template instances and function literals.
*/
continue;
//printf("Enum member '%s'\n",em->toChars());
e = em->value;
if (e)
{
assert(e->dyncast() == DYNCAST_EXPRESSION);
e = e->semantic(sce);
e = e->optimize(WANTvalue);
// Need to copy it because we're going to change the type
e = e->copy();
e = e->implicitCastTo(sc, memtype);
e = e->optimize(WANTvalue);
number = e->toInteger();
e->type = t;
}
else
{ // Default is the previous number plus 1
// Check for overflow
if (!first)
{
switch (t->toBasetype()->ty)
{
case Tbool:
if (number == 2) goto Loverflow;
break;
case Tint8:
if (number == 128) goto Loverflow;
break;
case Tchar:
case Tuns8:
if (number == 256) goto Loverflow;
break;
case Tint16:
if (number == 0x8000) goto Loverflow;
break;
case Twchar:
case Tuns16:
if (number == 0x10000) goto Loverflow;
break;
case Tint32:
if (number == 0x80000000) goto Loverflow;
break;
case Tdchar:
case Tuns32:
if (number == 0x100000000LL) goto Loverflow;
break;
case Tint64:
if (number == 0x8000000000000000LL) goto Loverflow;
break;
case Tuns64:
if (number == 0) goto Loverflow;
break;
Loverflow:
error("overflow of enum value");
break;
default:
assert(0);
}
}
e = new IntegerExp(em->loc, number, t);
}
em->value = e;
// Add to symbol table only after evaluating 'value'
if (isAnonymous())
{
//sce->enclosing->insert(em);
for (Scope *sct = sce->enclosing; sct; sct = sct->enclosing)
{
if (sct->scopesym)
{
if (!sct->scopesym->symtab)
sct->scopesym->symtab = new DsymbolTable();
em->addMember(sce, sct->scopesym, 1);
break;
}
}
}
else
em->addMember(sc, this, 1);
if (first)
{ first = 0;
defaultval = number;
minval = number;
maxval = number;
}
else if (memtype->isunsigned())
{
if (number < minval)
minval = number;
if (number > maxval)
maxval = number;
}
else
{
if ((sinteger_t)number < (sinteger_t)minval)
minval = number;
if ((sinteger_t)number > (sinteger_t)maxval)
maxval = number;
}
number++;
}
//printf("defaultval = %lld\n", defaultval);
sce->pop();
//members->print();
}
int EnumDeclaration::oneMember(Dsymbol **ps)
{
if (isAnonymous())
return Dsymbol::oneMembers(members, ps);
return Dsymbol::oneMember(ps);
}
void EnumDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("enum ");
if (ident)
{ buf->writestring(ident->toChars());
buf->writeByte(' ');
}
if (memtype)
{
buf->writestring(": ");
memtype->toCBuffer(buf, NULL, hgs);
}
if (!members)
{
buf->writeByte(';');
buf->writenl();
return;
}
buf->writenl();
buf->writeByte('{');
buf->writenl();
for (size_t i = 0; i < members->dim; i++)
{
EnumMember *em = ((Dsymbol *)members->data[i])->isEnumMember();
if (!em)
continue;
//buf->writestring(" ");
em->toCBuffer(buf, hgs);
buf->writeByte(',');
buf->writenl();
}
buf->writeByte('}');
buf->writenl();
}
Type *EnumDeclaration::getType()
{
return type;
}
const char *EnumDeclaration::kind()
{
return "enum";
}
int EnumDeclaration::isDeprecated()
{
return isdeprecated;
}
/********************************* EnumMember ****************************/
EnumMember::EnumMember(Loc loc, Identifier *id, Expression *value)
: Dsymbol(id)
{
this->value = value;
this->loc = loc;
}
Dsymbol *EnumMember::syntaxCopy(Dsymbol *s)
{
Expression *e = NULL;
if (value)
e = value->syntaxCopy();
EnumMember *em;
if (s)
{ em = (EnumMember *)s;
em->loc = loc;
em->value = e;
}
else
em = new EnumMember(loc, ident, e);
return em;
}
void EnumMember::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring(ident->toChars());
if (value)
{
buf->writestring(" = ");
value->toCBuffer(buf, hgs);
}
}
const char *EnumMember::kind()
{
return "enum member";
}

View File

@@ -1,96 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2008 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_ENUM_H
#define DMD_ENUM_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "dsymbol.h"
struct Identifier;
struct Type;
struct Expression;
struct HdrGenState;
struct EnumDeclaration : ScopeDsymbol
{ /* enum ident : memtype { ... }
*/
Type *type; // the TypeEnum
Type *memtype; // type of the members
#if DMDV1
dinteger_t maxval;
dinteger_t minval;
dinteger_t defaultval; // default initializer
#else
Expression *maxval;
Expression *minval;
Expression *defaultval; // default initializer
#endif
int isdeprecated;
int isdone; // 0: not done
// 1: semantic() successfully completed
EnumDeclaration(Loc loc, Identifier *id, Type *memtype);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic0(Scope *sc);
void semantic(Scope *sc);
int oneMember(Dsymbol **ps);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Type *getType();
const char *kind();
#if DMDV2
Dsymbol *search(Loc, Identifier *ident, int flags);
#endif
int isDeprecated(); // is Dsymbol deprecated?
void emitComment(Scope *sc);
void toJsonBuffer(OutBuffer *buf);
void toDocBuffer(OutBuffer *buf);
EnumDeclaration *isEnumDeclaration() { return this; }
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
void toDebug();
int cvMember(unsigned char *p);
Symbol *sinit;
Symbol *toInitializer();
#endif
#if IN_LLVM
void codegen(Ir*);
#endif
};
struct EnumMember : Dsymbol
{
Expression *value;
EnumMember(Loc loc, Identifier *id, Expression *value);
Dsymbol *syntaxCopy(Dsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
const char *kind();
void emitComment(Scope *sc);
void toJsonBuffer(OutBuffer *buf);
void toDocBuffer(OutBuffer *buf);
EnumMember *isEnumMember() { return this; }
};
#endif /* DMD_ENUM_H */

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

3653
dmd/func.c

File diff suppressed because it is too large Load Diff

View File

@@ -1,248 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 1, February 1989
Copyright (C) 1989 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The license agreements of most software companies try to keep users
at the mercy of those companies. By contrast, our General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. The
General Public License applies to the Free Software Foundation's
software and to any other program whose authors commit to using it.
You can use it for your programs, too.
When we speak of free software, we are referring to freedom, not
price. Specifically, the General Public License is designed to make
sure that you have the freedom to give away or sell copies of free
software, that you receive source code or can get it if you want it,
that you can change the software or use pieces of it in new free
programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of a such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must tell them their rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any program or other work which
contains a notice placed by the copyright holder saying it may be
distributed under the terms of this General Public License. The
"Program", below, refers to any such program or work, and a "work based
on the Program" means either the Program or any work containing the
Program or a portion of it, either verbatim or with modifications. Each
licensee is addressed as "you".
1. You may copy and distribute verbatim copies of the Program's source
code as you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this
General Public License and to the absence of any warranty; and give any
other recipients of the Program a copy of this General Public License
along with the Program. You may charge a fee for the physical act of
transferring a copy.
2. You may modify your copy or copies of the Program or any portion of
it, and copy and distribute such modifications under the terms of Paragraph
1 above, provided that you also do the following:
a) cause the modified files to carry prominent notices stating that
you changed the files and the date of any change; and
b) cause the whole of any work that you distribute or publish, that
in whole or in part contains the Program or any part thereof, either
with or without modifications, to be licensed at no charge to all
third parties under the terms of this General Public License (except
that you may choose to grant warranty protection to some or all
third parties, at your option).
c) If the modified program normally reads commands interactively when
run, you must cause it, when started running for such interactive use
in the simplest and most usual way, to print or display an
announcement including an appropriate copyright notice and a notice
that there is no warranty (or else, saying that you provide a
warranty) and that users may redistribute the program under these
conditions, and telling the user how to view a copy of this General
Public License.
d) You may charge a fee for the physical act of transferring a
copy, and you may at your option offer warranty protection in
exchange for a fee.
Mere aggregation of another independent work with the Program (or its
derivative) on a volume of a storage or distribution medium does not bring
the other work under the scope of these terms.
3. You may copy and distribute the Program (or a portion or derivative of
it, under Paragraph 2) in object code or executable form under the terms of
Paragraphs 1 and 2 above provided that you also do one of the following:
a) accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of
Paragraphs 1 and 2 above; or,
b) accompany it with a written offer, valid for at least three
years, to give any third party free (except for a nominal charge
for the cost of distribution) a complete machine-readable copy of the
corresponding source code, to be distributed under the terms of
Paragraphs 1 and 2 above; or,
c) accompany it with the information you received as to where the
corresponding source code may be obtained. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form alone.)
Source code for a work means the preferred form of the work for making
modifications to it. For an executable file, complete source code means
all the source code for all modules it contains; but, as a special
exception, it need not include source code for modules which are standard
libraries that accompany the operating system on which the executable
file runs, or for standard header files or definitions files that
accompany that operating system.
4. You may not copy, modify, sublicense, distribute or transfer the
Program except as expressly provided under this General Public License.
Any attempt otherwise to copy, modify, sublicense, distribute or transfer
the Program is void, and will automatically terminate your rights to use
the Program under this License. However, parties who have received
copies, or rights to use copies, from you under this General Public
License will not have their licenses terminated so long as such parties
remain in full compliance.
5. By copying, distributing or modifying the Program (or any work based
on the Program) you indicate your acceptance of this license to do so,
and all its terms and conditions.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these
terms and conditions. You may not impose any further restrictions on the
recipients' exercise of the rights granted herein.
7. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of the license which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
the license, you may choose any version ever published by the Free Software
Foundation.
8. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19xx name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License. Of course, the
commands you use may be called something other than `show w' and `show
c'; they could even be mouse-clicks or menu items--whatever suits your
program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
program `Gnomovision' (a program to direct compilers to make passes
at assemblers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
That's all there is to it!

View File

@@ -1,100 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// Initial header generation implementation by Dave Fladebo
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// Routines to emit header files
#define PRETTY_PRINT
#define TEST_EMIT_ALL 0 // For Testing
#define LOG 0
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#if __DMC__
#include <complex.h>
#endif
#include "rmem.h"
#include "id.h"
#include "init.h"
#include "attrib.h"
#include "cond.h"
#include "enum.h"
#include "import.h"
#include "module.h"
#include "mtype.h"
#include "scope.h"
#include "staticassert.h"
#include "template.h"
#include "utf.h"
#include "version.h"
#include "declaration.h"
#include "aggregate.h"
#include "expression.h"
#include "statement.h"
#include "mtype.h"
#include "hdrgen.h"
void argsToCBuffer(OutBuffer *buf, Array *arguments, HdrGenState *hgs);
void Module::genhdrfile()
{
OutBuffer hdrbufr;
hdrbufr.printf("// D import file generated from '%s'", srcfile->toChars());
hdrbufr.writenl();
HdrGenState hgs;
memset(&hgs, 0, sizeof(hgs));
hgs.hdrgen = 1;
toCBuffer(&hdrbufr, &hgs);
// Transfer image to file
hdrfile->setbuffer(hdrbufr.data, hdrbufr.offset);
hdrbufr.data = NULL;
char *pt = FileName::path(hdrfile->toChars());
if (*pt)
FileName::ensurePathExists(pt);
mem.free(pt);
hdrfile->writev();
}
void Module::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
if (md)
{
buf->writestring("module ");
buf->writestring(md->toChars());
buf->writebyte(';');
buf->writenl();
}
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = (Dsymbol *)members->data[i];
s->toHBuffer(buf, hgs);
}
}
void Dsymbol::toHBuffer(OutBuffer *buf, HdrGenState *hgs)
{
toCBuffer(buf, hgs);
}
/*************************************/

View File

@@ -1,35 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// initial header generation implementation by Dave Fladebo
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <string.h> // memset()
struct HdrGenState
{
int hdrgen; // 1 if generating header file
int ddoc; // 1 if generating Ddoc file
int console; // 1 if writing to console
int tpltMember;
int inCallExp;
int inPtrExp;
int inSlcExp;
int inDotExp;
int inBinExp;
int inArrExp;
int emitInst;
struct
{
int init;
int decl;
} FLinit;
HdrGenState() { memset(this, 0, sizeof(HdrGenState)); }
};

View File

@@ -1,757 +0,0 @@
// Copyright (c) 1999-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gpl.txt.
// See the included readme.txt for details.
/* HTML parser
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <wchar.h>
#include "mars.h"
#include "html.h"
#if MARS || IN_LLVM
#include <assert.h>
#include "root.h"
//#include "../mars/mars.h"
#else
#include "outbuf.h"
#include "msgs2.h"
extern void html_err(const char *, unsigned, unsigned, ...);
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
#endif
#if __GNUC__
int memicmp(const char *s1, const char *s2, int n);
#if 0
{
int result = 0;
for (int i = 0; i < n; i++)
{ char c1 = s1[i];
char c2 = s2[i];
result = c1 - c2;
if (result)
{
if ('A' <= c1 && c1 <= 'Z')
c1 += 'a' - 'A';
if ('A' <= c2 && c2 <= 'Z')
c2 += 'a' - 'A';
result = c1 - c2;
if (result)
break;
}
}
return result;
}
#endif
#endif
extern int HtmlNamedEntity(unsigned char *p, int length);
static int isLineSeparator(const unsigned char* p);
/**********************************
* Determine if beginning of tag identifier
* or a continuation of a tag identifier.
*/
inline int istagstart(int c)
{
return (isalpha(c) || c == '_');
}
inline int istag(int c)
{
return (isalnum(c) || c == '_');
}
/**********************************************
*/
Html::Html(const char *sourcename, unsigned char *base, unsigned length)
{
//printf("Html::Html()\n");
this->sourcename = sourcename;
this->base = base;
p = base;
end = base + length;
linnum = 1;
dbuf = NULL;
inCode = 0;
}
/**********************************************
* Print error & quit.
*/
void Html::error(const char *format, ...)
{
if (!global.gag)
{
fprintf(stderr, "%s(%d) : HTML Error: ", sourcename, linnum);
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
fflush(stderr);
}
global.errors++;
}
/**********************************************
* Extract all the code from an HTML file,
* concatenate it all together, and store in buf.
*/
void Html::extractCode(OutBuffer *buf)
{
//printf("Html::extractCode()\n");
dbuf = buf; // save for other routines
buf->reserve(end - p);
inCode = 0;
while (1)
{
//printf("p = %p, *p = x%x\n", p, *p);
switch (*p)
{
#if 0 // strings are not recognized outside of tags
case '"':
case '\'':
skipString();
continue;
#endif
case '<':
if (p[1] == '!' && isCommentStart())
{ // Comments start with <!--
scanComment();
}
else if(p[1] == '!' && isCDATAStart())
{
scanCDATA();
}
else if (p[1] == '/' && istagstart(*skipWhite(p + 2)))
skipTag();
else if (istagstart(*skipWhite(p + 1)))
skipTag();
else
goto Ldefault;
continue;
case 0:
case 0x1a:
break; // end of file
case '&':
if (inCode)
{ // Translate character entity into ascii for D parser
int c;
c = charEntity();
buf->writeUTF8(c);
}
else
p++;
continue;
case '\r':
if (p[1] == '\n')
goto Ldefault;
case '\n':
linnum++;
// Always extract new lines, so that D lexer counts the
// lines right.
buf->writeByte(*p);
p++;
continue;
default:
Ldefault:
if (inCode)
buf->writeByte(*p);
p++;
continue;
}
break;
}
buf->writeByte(0); // ending sentinel
//printf("D code is: '%s'\n", (char *)buf->data);
}
/***********************************************
* Scan to end of <> tag.
* Look for <code> and </code> tags to start/stop D processing.
* Input:
* p is on opening '<' of tag; it's already verified that
* it's a tag by lookahead
* Output:
* p is past closing '>' of tag
*/
void Html::skipTag()
{
enum TagState // what parsing state we're in
{
TStagstart, // start of tag name
TStag, // in a tag name
TSrest, // following tag name
};
enum TagState state = TStagstart;
int inot;
unsigned char *tagstart = NULL;
int taglen = 0;
p++;
inot = 0;
if (*p == '/')
{ inot = 1;
p++;
}
while (1)
{
switch (*p)
{
case '>': // found end of tag
p++;
break;
case '"':
case '\'':
state = TSrest;
skipString();
continue;
case '<':
if (p[1] == '!' && isCommentStart())
{ // Comments start with <!--
scanComment();
}
else if (p[1] == '/' && istagstart(*skipWhite(p + 2)))
{ error("nested tag");
skipTag();
}
else if (istagstart(*skipWhite(p + 1)))
{ error("nested tag");
skipTag();
}
// Treat comments as if they were whitespace
state = TSrest;
continue;
case 0:
case 0x1a:
error("end of file before end of tag");
break; // end of file
case '\r':
if (p[1] == '\n')
goto Ldefault;
case '\n':
linnum++;
// Always extract new lines, so that code lexer counts the
// lines right.
dbuf->writeByte(*p);
state = TSrest; // end of tag
p++;
continue;
case ' ':
case '\t':
case '\f':
case '\v':
if (state == TStagstart)
{ p++;
continue;
}
default:
Ldefault:
switch (state)
{
case TStagstart: // start of tag name
assert(istagstart(*p));
state = TStag;
tagstart = p;
taglen = 0;
break;
case TStag:
if (istag(*p))
{ // Continuing tag name
taglen++;
}
else
{ // End of tag name
state = TSrest;
}
break;
case TSrest:
break;
}
p++;
continue;
}
break;
}
// See if we parsed a <code> or </code> tag
if (taglen && memicmp((char *) tagstart, (char *) "CODE", taglen) == 0
&& *(p - 2) != '/') // ignore "<code />" (XHTML)
{
if (inot)
{ inCode--;
if (inCode < 0)
inCode = 0; // ignore extra </code>'s
}
else
inCode++;
}
}
/***********************************************
* Scan to end of attribute string.
*/
void Html::skipString()
{
int tc = *p;
while (1)
{
p++;
switch (*p)
{
case '"':
case '\'':
if (*p == tc)
{ p++;
break;
}
continue;
case '\r':
if (p[1] == '\n')
goto Ldefault;
case '\n':
linnum++;
// Always extract new lines, so that D lexer counts the
// lines right.
dbuf->writeByte(*p);
continue;
case 0:
case 0x1a:
Leof:
error("end of file before closing %c of string", tc);
break;
default:
Ldefault:
continue;
}
break;
}
}
/*********************************
* If p points to any white space, skip it
* and return pointer just past it.
*/
unsigned char *Html::skipWhite(unsigned char *q)
{
for (; 1; q++)
{
switch (*q)
{
case ' ':
case '\t':
case '\f':
case '\v':
case '\r':
case '\n':
continue;
default:
break;
}
break;
}
return q;
}
/***************************************************
* Scan to end of comment.
* Comments are defined any of a number of ways.
* IE 5.0: <!-- followed by >
* "HTML The Definitive Guide": <!-- text with at least one space in it -->
* Netscape: <!-- --> comments nest
* w3c: whitespace can appear between -- and > of comment close
*/
void Html::scanComment()
{
// Most of the complexity is dealing with the case that
// an arbitrary amount of whitespace can appear between
// the -- and the > of a comment close.
int scangt = 0;
//printf("scanComment()\n");
if (*p == '\n')
{ linnum++;
// Always extract new lines, so that D lexer counts the
// lines right.
dbuf->writeByte(*p);
}
while (1)
{
//scangt = 1; // IE 5.0 compatibility
p++;
switch (*p)
{
case '-':
if (p[1] == '-')
{
if (p[2] == '>') // optimize for most common case
{
p += 3;
break;
}
p++;
scangt = 1;
}
else
scangt = 0;
continue;
case '>':
if (scangt)
{ // found -->
p++;
break;
}
continue;
case ' ':
case '\t':
case '\f':
case '\v':
// skip white space
continue;
case '\r':
if (p[1] == '\n')
goto Ldefault;
case '\n':
linnum++; // remember to count lines
// Always extract new lines, so that D lexer counts the
// lines right.
dbuf->writeByte(*p);
continue;
case 0:
case 0x1a:
error("end of file before closing --> of comment");
break;
default:
Ldefault:
scangt = 0; // it's not -->
continue;
}
break;
}
//printf("*p = '%c'\n", *p);
}
/********************************************
* Determine if we are at the start of a comment.
* Input:
* p is on the opening '<'
* Returns:
* 0 if not start of a comment
* 1 if start of a comment, p is adjusted to point past --
*/
int Html::isCommentStart()
#ifdef __DMC__
__out(result)
{
if (result == 0)
;
else if (result == 1)
{
assert(p[-2] == '-' && p[-1] == '-');
}
else
assert(0);
}
__body
#endif /* __DMC__ */
{ unsigned char *s;
if (p[0] == '<' && p[1] == '!')
{
for (s = p + 2; 1; s++)
{
switch (*s)
{
case ' ':
case '\t':
case '\r':
case '\f':
case '\v':
// skip white space, even though spec says no
// white space is allowed
continue;
case '-':
if (s[1] == '-')
{
p = s + 2;
return 1;
}
goto No;
default:
goto No;
}
}
}
No:
return 0;
}
int Html::isCDATAStart()
{
const char * CDATA_START_MARKER = "<![CDATA[";
size_t len = strlen(CDATA_START_MARKER);
if (strncmp((char*)p, CDATA_START_MARKER, len) == 0)
{
p += len;
return 1;
}
else
{
return 0;
}
}
void Html::scanCDATA()
{
while(*p && *p != 0x1A)
{
int lineSepLength = isLineSeparator(p);
if (lineSepLength>0)
{
/* Always extract new lines, so that D lexer counts the lines
* right.
*/
linnum++;
dbuf->writeByte('\n');
p += lineSepLength;
continue;
}
else if (p[0] == ']' && p[1] == ']' && p[2] == '>')
{
/* end of CDATA section */
p += 3;
return;
}
else if (inCode)
{
/* this CDATA section contains D code */
dbuf->writeByte(*p);
}
p++;
}
}
/********************************************
* Convert an HTML character entity into a character.
* Forms are:
* &name; named entity
* &#ddd; decimal
* &#xhhhh; hex
* Input:
* p is on the &
*/
int Html::charEntity()
{ int c = 0;
int v;
int hex;
unsigned char *pstart = p;
//printf("Html::charEntity('%c')\n", *p);
if (p[1] == '#')
{
p++;
if (p[1] == 'x' || p[1] == 'X')
{ p++;
hex = 1;
}
else
hex = 0;
if (p[1] == ';')
goto Linvalid;
while (1)
{
p++;
switch (*p)
{
case 0:
case 0x1a:
error("end of file before end of character entity");
goto Lignore;
case '\n':
case '\r':
case '<': // tag start
// Termination is assumed
break;
case ';':
// Termination is explicit
p++;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
v = *p - '0';
goto Lvalue;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
if (!hex)
goto Linvalid;
v = (*p - 'a') + 10;
goto Lvalue;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
if (!hex)
goto Linvalid;
v = (*p - 'A') + 10;
goto Lvalue;
Lvalue:
if (hex)
c = (c << 4) + v;
else
c = (c * 10) + v;
if (c > 0x10FFFF)
{
error("character entity out of range");
goto Lignore;
}
continue;
default:
Linvalid:
error("invalid numeric character reference");
goto Lignore;
}
break;
}
}
else
{
// It's a named entity; gather all characters until ;
unsigned char *idstart = p + 1;
while (1)
{
p++;
switch (*p)
{
case 0:
case 0x1a:
error("end of file before end of character entity");
break;
case '\n':
case '\r':
case '<': // tag start
// Termination is assumed
c = HtmlNamedEntity(idstart, p - idstart);
if (c == -1)
goto Lignore;
break;
case ';':
// Termination is explicit
c = HtmlNamedEntity(idstart, p - idstart);
if (c == -1)
goto Lignore;
p++;
break;
default:
continue;
}
break;
}
}
// Kludge to convert non-breaking space to ascii space
if (c == 160)
c = ' ';
return c;
Lignore:
//printf("Lignore\n");
p = pstart + 1;
return '&';
}
/**
* identify DOS, Linux, Mac, Next and Unicode line endings
* 0 if this is no line separator
* >0 the length of the separator
* Note: input has to be UTF-8
*/
static int isLineSeparator(const unsigned char* p)
{
// Linux
if( p[0]=='\n')
return 1;
// Mac & Dos
if( p[0]=='\r')
return (p[1]=='\n') ? 2 : 1;
// Unicode (line || paragraph sep.)
if( p[0]==0xE2 && p[1]==0x80 && (p[2]==0xA8 || p[2]==0xA9))
return 3;
// Next
if( p[0]==0xC2 && p[1]==0x85)
return 2;
return 0;
}

View File

@@ -1,42 +0,0 @@
// Copyright (c) 1999-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gpl.txt.
// See the included readme.txt for details.
#ifndef DMD_HTML_H
#define DMD_HTML_H 1
struct OutBuffer;
struct Html
{
const char *sourcename;
unsigned char *base; // pointer to start of buffer
unsigned char *end; // past end of buffer
unsigned char *p; // current character
unsigned linnum; // current line number
OutBuffer *dbuf; // code source buffer
int inCode; // !=0 if in code
Html(const char *sourcename, unsigned char *base, unsigned length);
void error(const char *format, ...) IS_PRINTF(2);
void extractCode(OutBuffer *buf);
void skipTag();
void skipString();
unsigned char *skipWhite(unsigned char *q);
void scanComment();
int isCommentStart();
void scanCDATA();
int isCDATAStart();
int charEntity();
static int namedEntity(unsigned char *p, int length);
};
#endif

View File

@@ -1,101 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include "root.h"
#include "identifier.h"
#include "mars.h"
#include "lexer.h"
#include "id.h"
Identifier::Identifier(const char *string, int value)
{
//printf("Identifier('%s', %d)\n", string, value);
this->string = string;
this->value = value;
this->len = strlen(string);
}
hash_t Identifier::hashCode()
{
return String::calcHash(string);
}
int Identifier::equals(Object *o)
{
return this == o || memcmp(string,o->toChars(),len+1) == 0;
}
int Identifier::compare(Object *o)
{
return memcmp(string, o->toChars(), len + 1);
}
char *Identifier::toChars()
{
return (char *)string;
}
const char *Identifier::toHChars2()
{
const char *p = NULL;
if (this == Id::ctor) p = "this";
else if (this == Id::dtor) p = "~this";
else if (this == Id::classInvariant) p = "invariant";
else if (this == Id::unitTest) p = "unittest";
else if (this == Id::dollar) p = "$";
else if (this == Id::withSym) p = "with";
else if (this == Id::result) p = "result";
else if (this == Id::returnLabel) p = "return";
else
{ p = toChars();
if (*p == '_')
{
if (memcmp(p, "_staticCtor", 11) == 0)
p = "static this";
else if (memcmp(p, "_staticDtor", 11) == 0)
p = "static ~this";
}
}
return p;
}
void Identifier::print()
{
fprintf(stdmsg, "%s",string);
}
int Identifier::dyncast()
{
return DYNCAST_IDENTIFIER;
}
Identifier *Identifier::generateId(const char *prefix)
{
static size_t i;
return generateId(prefix, ++i);
}
Identifier *Identifier::generateId(const char *prefix, size_t i)
{ OutBuffer buf;
buf.writestring(prefix);
buf.printf("%zu", i);
char *id = buf.toChars();
buf.data = NULL;
return Lexer::idPool(id);
}

View File

@@ -1,46 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_IDENTIFIER_H
#define DMD_IDENTIFIER_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#if IN_LLVM
namespace llvm
{
class Value;
}
#endif
struct Identifier : Object
{
int value;
const char *string;
unsigned len;
Identifier(const char *string, int value);
int equals(Object *o);
hash_t hashCode();
int compare(Object *o);
void print();
char *toChars();
char *toHChars();
const char *toHChars2();
int dyncast();
static Identifier *generateId(const char *prefix);
static Identifier *generateId(const char *prefix, size_t i);
};
#endif /* DMD_IDENTIFIER_H */

View File

@@ -1,378 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// http://www.dsource.org/projects/dmd/browser/branches/dmd-1.x/src/idgen.c
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// Program to generate string files in d data structures.
// Saves much tedious typing, and eliminates typo problems.
// Generates:
// id.h
// id.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
struct Msgtable
{
const char *ident; // name to use in DMD source
const char *name; // name in D executable
};
Msgtable msgtable[] =
{
{ "IUnknown" },
{ "Object" },
{ "object" },
{ "max" },
{ "min" },
{ "This", "this" },
{ "ctor", "_ctor" },
{ "dtor", "_dtor" },
{ "classInvariant", "__invariant" },
{ "unitTest", "_unitTest" },
{ "require", "__require" },
{ "ensure", "__ensure" },
{ "init" },
{ "size" },
{ "__sizeof", "sizeof" },
{ "__xalignof", "alignof" },
{ "mangleof" },
{ "stringof" },
{ "tupleof" },
{ "length" },
{ "remove" },
{ "ptr" },
{ "funcptr" },
{ "dollar", "__dollar" },
{ "offset" },
{ "offsetof" },
{ "ModuleInfo" },
{ "ClassInfo" },
{ "classinfo" },
{ "typeinfo" },
{ "outer" },
{ "Exception" },
{ "Error" },
{ "withSym", "__withSym" },
{ "result", "__result" },
{ "returnLabel", "__returnLabel" },
{ "delegate" },
{ "line" },
{ "empty", "" },
{ "p" },
{ "coverage", "__coverage" },
{ "__vptr" },
{ "__monitor" },
{ "TypeInfo" },
{ "TypeInfo_Class" },
{ "TypeInfo_Interface" },
{ "TypeInfo_Struct" },
{ "TypeInfo_Enum" },
{ "TypeInfo_Typedef" },
{ "TypeInfo_Pointer" },
{ "TypeInfo_Array" },
{ "TypeInfo_StaticArray" },
{ "TypeInfo_AssociativeArray" },
{ "TypeInfo_Function" },
{ "TypeInfo_Delegate" },
{ "TypeInfo_Tuple" },
{ "TypeInfo_Const" },
{ "TypeInfo_Invariant" },
{ "elements" },
{ "_arguments_typeinfo" },
{ "_arguments" },
{ "_argptr" },
{ "_match" },
{ "m_align" },
{ "LINE", "__LINE__" },
{ "FILE", "__FILE__" },
{ "DATE", "__DATE__" },
{ "TIME", "__TIME__" },
{ "TIMESTAMP", "__TIMESTAMP__" },
{ "VENDOR", "__VENDOR__" },
{ "VERSIONX", "__VERSION__" },
{ "nan" },
{ "infinity" },
{ "dig" },
{ "epsilon" },
{ "mant_dig" },
{ "max_10_exp" },
{ "max_exp" },
{ "min_10_exp" },
{ "min_exp" },
{ "re" },
{ "im" },
{ "C" },
{ "D" },
{ "Windows" },
{ "Pascal" },
{ "System" },
{ "exit" },
{ "success" },
{ "failure" },
{ "keys" },
{ "values" },
{ "rehash" },
{ "sort" },
{ "reverse" },
{ "dup" },
{ "idup" },
// For inline assembler
{ "___out", "out" },
{ "___in", "in" },
{ "__int", "int" },
{ "__dollar", "$" },
{ "__LOCAL_SIZE" },
// For operator overloads
{ "uadd", "opPos" },
{ "neg", "opNeg" },
{ "com", "opCom" },
{ "add", "opAdd" },
{ "add_r", "opAdd_r" },
{ "sub", "opSub" },
{ "sub_r", "opSub_r" },
{ "mul", "opMul" },
{ "mul_r", "opMul_r" },
{ "div", "opDiv" },
{ "div_r", "opDiv_r" },
{ "mod", "opMod" },
{ "mod_r", "opMod_r" },
{ "eq", "opEquals" },
{ "cmp", "opCmp" },
{ "iand", "opAnd" },
{ "iand_r", "opAnd_r" },
{ "ior", "opOr" },
{ "ior_r", "opOr_r" },
{ "ixor", "opXor" },
{ "ixor_r", "opXor_r" },
{ "shl", "opShl" },
{ "shl_r", "opShl_r" },
{ "shr", "opShr" },
{ "shr_r", "opShr_r" },
{ "ushr", "opUShr" },
{ "ushr_r", "opUShr_r" },
{ "cat", "opCat" },
{ "cat_r", "opCat_r" },
{ "assign", "opAssign" },
{ "addass", "opAddAssign" },
{ "subass", "opSubAssign" },
{ "mulass", "opMulAssign" },
{ "divass", "opDivAssign" },
{ "modass", "opModAssign" },
{ "andass", "opAndAssign" },
{ "orass", "opOrAssign" },
{ "xorass", "opXorAssign" },
{ "shlass", "opShlAssign" },
{ "shrass", "opShrAssign" },
{ "ushrass", "opUShrAssign" },
{ "catass", "opCatAssign" },
{ "postinc", "opPostInc" },
{ "postdec", "opPostDec" },
{ "index", "opIndex" },
{ "indexass", "opIndexAssign" },
{ "slice", "opSlice" },
{ "sliceass", "opSliceAssign" },
{ "call", "opCall" },
{ "cast", "opCast" },
{ "match", "opMatch" },
{ "next", "opNext" },
{ "opIn" },
{ "opIn_r" },
{ "classNew", "new" },
{ "classDelete", "delete" },
// For foreach
{ "apply", "opApply" },
{ "applyReverse", "opApplyReverse" },
{ "adDup", "_adDupT" },
{ "adReverse", "_adReverse" },
// For internal functions
{ "aaLen", "_aaLen" },
{ "aaKeys", "_aaKeys" },
{ "aaValues", "_aaValues" },
{ "aaRehash", "_aaRehash" },
{ "monitorenter", "_d_monitorenter" },
{ "monitorexit", "_d_monitorexit" },
{ "criticalenter", "_d_criticalenter" },
{ "criticalexit", "_d_criticalexit" },
// For pragma's
{ "GNU_asm" },
{ "lib" },
{ "msg" },
#if IN_LLVM
// LDC-specific pragmas.
{ "LDC_intrinsic" },
{ "LDC_no_typeinfo" },
{ "LDC_no_moduleinfo" },
{ "LDC_alloca" },
{ "LDC_va_start" },
{ "LDC_va_copy" },
{ "LDC_va_end" },
{ "LDC_va_arg" },
{ "LDC_verbose" },
{ "LDC_allow_inline" },
{ "LDC_inline_asm" },
{ "LDC_inline_ir" },
{ "LDC_fence" },
{ "LDC_atomic_load" },
{ "LDC_atomic_store" },
{ "LDC_atomic_cmp_xchg" },
{ "LDC_atomic_rmw" },
{ "LDC_global_crt_ctor" },
{ "LDC_global_crt_dtor" },
// Deprecated LDC pragmas lacking the vendor prefix.
{ "intrinsic" },
{ "no_typeinfo" },
{ "no_moduleinfo" },
{ "Alloca", "alloca" },
{ "vastart", "va_start" },
{ "vacopy", "va_copy" },
{ "vaend", "va_end" },
{ "vaarg", "va_arg" },
{ "ldc" },
{ "allow_inline" },
{ "llvm_inline_asm" },
{ "llvm_inline_ir" },
{ "fence" },
{ "atomic_load" },
{ "atomic_store" },
{ "atomic_cmp_xchg" },
{ "atomic_rmw" },
#endif
// For special functions
{ "tohash", "toHash" },
{ "tostring", "toString" },
// Special functions
//{ "alloca" },
{ "main" },
{ "WinMain" },
{ "DllMain" },
// varargs implementation
{ "va_argsave_t", "__va_argsave_t" },
{ "va_argsave", "__va_argsave" },
// Builtin functions
{ "std" },
{ "core" },
{ "math" },
{ "sin" },
{ "cos" },
{ "tan" },
{ "_sqrt", "sqrt" },
{ "_pow", "pow" },
{ "atan2" },
{ "rndtol" },
{ "expm1" },
{ "exp2" },
{ "yl2x" },
{ "yl2xp1" },
{ "fabs" },
{ "bitop" },
{ "bsf" },
{ "bsr" },
{ "bswap" },
};
int main()
{
FILE *fp;
unsigned i;
{
fp = fopen("id.h","w");
if (!fp)
{ printf("can't open id.h\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "// File generated by idgen.c\n");
#if __DMC__
fprintf(fp, "#pragma once\n");
#endif
fprintf(fp, "#ifndef DMD_ID_H\n");
fprintf(fp, "#define DMD_ID_H 1\n");
fprintf(fp, "struct Identifier;\n");
fprintf(fp, "struct Id\n");
fprintf(fp, "{\n");
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
{ const char *id = msgtable[i].ident;
fprintf(fp," static Identifier *%s;\n", id);
}
fprintf(fp, " static void initialize();\n");
fprintf(fp, "};\n");
fprintf(fp, "#endif\n");
fclose(fp);
}
{
fp = fopen("id.c","w");
if (!fp)
{ printf("can't open id.c\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "// File generated by idgen.c\n");
fprintf(fp, "#include \"id.h\"\n");
fprintf(fp, "#include \"identifier.h\"\n");
fprintf(fp, "#include \"lexer.h\"\n");
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
{ const char *id = msgtable[i].ident;
const char *p = msgtable[i].name;
if (!p)
p = id;
fprintf(fp,"Identifier *Id::%s;\n", id);
}
fprintf(fp, "void Id::initialize()\n");
fprintf(fp, "{\n");
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
{ const char *id = msgtable[i].ident;
const char *p = msgtable[i].name;
if (!p)
p = id;
fprintf(fp," %s = Lexer::idPool(\"%s\");\n", id, p);
}
fprintf(fp, "}\n");
fclose(fp);
}
return EXIT_SUCCESS;
}

View File

@@ -1,457 +0,0 @@
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include "mtype.h"
enum TY impcnvResult[TMAX][TMAX];
enum TY impcnvType1[TMAX][TMAX];
enum TY impcnvType2[TMAX][TMAX];
int impcnvWarn[TMAX][TMAX];
int integral_promotion(int t)
{
switch (t)
{
case Tchar:
case Twchar:
case Tbit:
case Tbool:
case Tint8:
case Tuns8:
case Tint16:
case Tuns16: return Tint32;
case Tdchar: return Tuns32;
default: return t;
}
}
void init()
{ int i, j;
// Set conversion tables
for (i = 0; i < TMAX; i++)
for (j = 0; j < TMAX; j++)
{ impcnvResult[i][j] = Terror;
impcnvType1[i][j] = Terror;
impcnvType2[i][j] = Terror;
impcnvWarn[i][j] = 0;
}
#define X(t1,t2, nt1,nt2, rt) \
impcnvResult[t1][t2] = rt; \
impcnvType1[t1][t2] = nt1; \
impcnvType2[t1][t2] = nt2;
/* ======================= */
X(Tbit,Tbit, Tint32,Tint32, Tint32)
X(Tbit,Tint8, Tint32,Tint32, Tint32)
X(Tbit,Tuns8, Tint32,Tint32, Tint32)
X(Tbit,Tint16, Tint32,Tint32, Tint32)
X(Tbit,Tuns16, Tint32,Tint32, Tint32)
X(Tbit,Tint32, Tint32,Tint32, Tint32)
X(Tbit,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tbit,Tint64, Tint64,Tint64, Tint64)
X(Tbit,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tbit,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tbit,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tbit,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tbit,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tbit,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tbit,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tbit,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tbit,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tbit,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tbool,Tbool, Tbool,Tbool, Tbool)
X(Tbool,Tint8, Tint32,Tint32, Tint32)
X(Tbool,Tuns8, Tint32,Tint32, Tint32)
X(Tbool,Tint16, Tint32,Tint32, Tint32)
X(Tbool,Tuns16, Tint32,Tint32, Tint32)
X(Tbool,Tint32, Tint32,Tint32, Tint32)
X(Tbool,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tbool,Tint64, Tint64,Tint64, Tint64)
X(Tbool,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tbool,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tbool,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tbool,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tbool,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tbool,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tbool,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tbool,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tbool,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tbool,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tint8,Tint8, Tint32,Tint32, Tint32)
X(Tint8,Tuns8, Tint32,Tint32, Tint32)
X(Tint8,Tint16, Tint32,Tint32, Tint32)
X(Tint8,Tuns16, Tint32,Tint32, Tint32)
X(Tint8,Tint32, Tint32,Tint32, Tint32)
X(Tint8,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tint8,Tint64, Tint64,Tint64, Tint64)
X(Tint8,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tint8,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tint8,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tint8,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tint8,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tint8,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tint8,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tint8,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tint8,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tint8,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tuns8,Tuns8, Tint32,Tint32, Tint32)
X(Tuns8,Tint16, Tint32,Tint32, Tint32)
X(Tuns8,Tuns16, Tint32,Tint32, Tint32)
X(Tuns8,Tint32, Tint32,Tint32, Tint32)
X(Tuns8,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tuns8,Tint64, Tint64,Tint64, Tint64)
X(Tuns8,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tuns8,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tuns8,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tuns8,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tuns8,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tuns8,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tuns8,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tuns8,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tuns8,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tuns8,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tint16,Tint16, Tint32,Tint32, Tint32)
X(Tint16,Tuns16, Tint32,Tint32, Tint32)
X(Tint16,Tint32, Tint32,Tint32, Tint32)
X(Tint16,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tint16,Tint64, Tint64,Tint64, Tint64)
X(Tint16,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tint16,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tint16,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tint16,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tint16,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tint16,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tint16,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tint16,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tint16,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tint16,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tuns16,Tuns16, Tint32,Tint32, Tint32)
X(Tuns16,Tint32, Tint32,Tint32, Tint32)
X(Tuns16,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tuns16,Tint64, Tint64,Tint64, Tint64)
X(Tuns16,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tuns16,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tuns16,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tuns16,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tuns16,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tuns16,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tuns16,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tuns16,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tuns16,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tuns16,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tint32,Tint32, Tint32,Tint32, Tint32)
X(Tint32,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tint32,Tint64, Tint64,Tint64, Tint64)
X(Tint32,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tint32,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tint32,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tint32,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tint32,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tint32,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tint32,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tint32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tint32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tint32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tuns32,Tuns32, Tuns32,Tuns32, Tuns32)
X(Tuns32,Tint64, Tint64,Tint64, Tint64)
X(Tuns32,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tuns32,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tuns32,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tuns32,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tuns32,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tuns32,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tuns32,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tuns32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tuns32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tuns32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tint64,Tint64, Tint64,Tint64, Tint64)
X(Tint64,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tint64,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tint64,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tint64,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tint64,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tint64,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tint64,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tint64,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tint64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tint64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tuns64,Tuns64, Tuns64,Tuns64, Tuns64)
X(Tuns64,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tuns64,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tuns64,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tuns64,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tuns64,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tuns64,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tuns64,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tuns64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tuns64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tfloat32,Tfloat32, Tfloat32,Tfloat32, Tfloat32)
X(Tfloat32,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tfloat32,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tfloat32,Timaginary32, Tfloat32,Timaginary32, Tfloat32)
X(Tfloat32,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tfloat32,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tfloat32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32)
X(Tfloat32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tfloat32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tfloat64,Tfloat64, Tfloat64,Tfloat64, Tfloat64)
X(Tfloat64,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tfloat64,Timaginary32, Tfloat64,Timaginary64, Tfloat64)
X(Tfloat64,Timaginary64, Tfloat64,Timaginary64, Tfloat64)
X(Tfloat64,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tfloat64,Tcomplex32, Tfloat64,Tcomplex64, Tcomplex64)
X(Tfloat64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64)
X(Tfloat64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tfloat80,Tfloat80, Tfloat80,Tfloat80, Tfloat80)
X(Tfloat80,Timaginary32, Tfloat80,Timaginary80, Tfloat80)
X(Tfloat80,Timaginary64, Tfloat80,Timaginary80, Tfloat80)
X(Tfloat80,Timaginary80, Tfloat80,Timaginary80, Tfloat80)
X(Tfloat80,Tcomplex32, Tfloat80,Tcomplex80, Tcomplex80)
X(Tfloat80,Tcomplex64, Tfloat80,Tcomplex80, Tcomplex80)
X(Tfloat80,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Timaginary32,Timaginary32, Timaginary32,Timaginary32, Timaginary32)
X(Timaginary32,Timaginary64, Timaginary64,Timaginary64, Timaginary64)
X(Timaginary32,Timaginary80, Timaginary80,Timaginary80, Timaginary80)
X(Timaginary32,Tcomplex32, Timaginary32,Tcomplex32, Tcomplex32)
X(Timaginary32,Tcomplex64, Timaginary64,Tcomplex64, Tcomplex64)
X(Timaginary32,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Timaginary64,Timaginary64, Timaginary64,Timaginary64, Timaginary64)
X(Timaginary64,Timaginary80, Timaginary80,Timaginary80, Timaginary80)
X(Timaginary64,Tcomplex32, Timaginary64,Tcomplex64, Tcomplex64)
X(Timaginary64,Tcomplex64, Timaginary64,Tcomplex64, Tcomplex64)
X(Timaginary64,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Timaginary80,Timaginary80, Timaginary80,Timaginary80, Timaginary80)
X(Timaginary80,Tcomplex32, Timaginary80,Tcomplex80, Tcomplex80)
X(Timaginary80,Tcomplex64, Timaginary80,Tcomplex80, Tcomplex80)
X(Timaginary80,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tcomplex32,Tcomplex32, Tcomplex32,Tcomplex32, Tcomplex32)
X(Tcomplex32,Tcomplex64, Tcomplex64,Tcomplex64, Tcomplex64)
X(Tcomplex32,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tcomplex64,Tcomplex64, Tcomplex64,Tcomplex64, Tcomplex64)
X(Tcomplex64,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80)
/* ======================= */
X(Tcomplex80,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80)
#undef X
#define Y(t1,t2) impcnvWarn[t1][t2] = 1;
Y(Tint8, Tbit)
Y(Tuns8, Tbit)
Y(Tint16, Tbit)
Y(Tuns16, Tbit)
Y(Tint32, Tbit)
Y(Tuns32, Tbit)
Y(Tint64, Tbit)
Y(Tuns64, Tbit)
Y(Tuns8, Tint8)
Y(Tint16, Tint8)
Y(Tuns16, Tint8)
Y(Tint32, Tint8)
Y(Tuns32, Tint8)
Y(Tint64, Tint8)
Y(Tuns64, Tint8)
Y(Tint8, Tuns8)
Y(Tint16, Tuns8)
Y(Tuns16, Tuns8)
Y(Tint32, Tuns8)
Y(Tuns32, Tuns8)
Y(Tint64, Tuns8)
Y(Tuns64, Tuns8)
Y(Tint8, Tchar)
Y(Tint16, Tchar)
Y(Tuns16, Tchar)
Y(Tint32, Tchar)
Y(Tuns32, Tchar)
Y(Tint64, Tchar)
Y(Tuns64, Tchar)
Y(Tuns16, Tint16)
Y(Tint32, Tint16)
Y(Tuns32, Tint16)
Y(Tint64, Tint16)
Y(Tuns64, Tint16)
Y(Tint16, Tuns16)
Y(Tint32, Tuns16)
Y(Tuns32, Tuns16)
Y(Tint64, Tuns16)
Y(Tuns64, Tuns16)
Y(Tint16, Twchar)
Y(Tint32, Twchar)
Y(Tuns32, Twchar)
Y(Tint64, Twchar)
Y(Tuns64, Twchar)
// Y(Tuns32, Tint32)
Y(Tint64, Tint32)
Y(Tuns64, Tint32)
// Y(Tint32, Tuns32)
Y(Tint64, Tuns32)
Y(Tuns64, Tuns32)
Y(Tint64, Tdchar)
Y(Tuns64, Tdchar)
// Y(Tint64, Tuns64)
// Y(Tuns64, Tint64)
for (i = 0; i < TMAX; i++)
for (j = 0; j < TMAX; j++)
{
if (impcnvResult[i][j] == Terror)
{
impcnvResult[i][j] = impcnvResult[j][i];
impcnvType1[i][j] = impcnvType2[j][i];
impcnvType2[i][j] = impcnvType1[j][i];
}
}
}
int main()
{ FILE *fp;
int i;
int j;
init();
fp = fopen("impcnvtab.c","w");
fprintf(fp,"// This file is generated by impcnvgen.c\n");
fprintf(fp,"#include \"mtype.h\"\n");
fprintf(fp,"unsigned char Type::impcnvResult[TMAX][TMAX] =\n{\n");
for (i = 0; i < TMAX; i++)
{
for (j = 0; j < TMAX; j++)
{
fprintf(fp, "%d,",impcnvResult[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp,"};\n");
fprintf(fp,"unsigned char Type::impcnvType1[TMAX][TMAX] =\n{\n");
for (i = 0; i < TMAX; i++)
{
for (j = 0; j < TMAX; j++)
{
fprintf(fp, "%d,",impcnvType1[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp,"};\n");
fprintf(fp,"unsigned char Type::impcnvType2[TMAX][TMAX] =\n{\n");
for (i = 0; i < TMAX; i++)
{
for (j = 0; j < TMAX; j++)
{
fprintf(fp, "%d,",impcnvType2[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp,"};\n");
fprintf(fp,"unsigned char Type::impcnvWarn[TMAX][TMAX] =\n{\n");
for (i = 0; i < TMAX; i++)
{
for (j = 0; j < TMAX; j++)
{
fprintf(fp, "%d,",impcnvWarn[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp,"};\n");
fclose(fp);
return EXIT_SUCCESS;
}

View File

@@ -1,84 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 2010 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include "mars.h"
/******************************************
* Looks for undefined identifier s to see
* if it might be undefined because an import
* was not specified.
* Not meant to be a comprehensive list of names in each module,
* just the most common ones.
*/
const char *importHint(const char *s)
{
#if DMDV1
static const char *modules[] =
{ "tango.stdc.stdio",
"tango.io.Stdout",
"tango.math.Math",
};
static const char *names[] =
{
"printf", NULL,
"Stdout", NULL,
"sin", "cos", "sqrt", "abs", NULL,
};
#else
static const char *modules[] =
{ "core.stdc.stdio",
"std.stdio",
"std.math",
};
static const char *names[] =
{
"printf", NULL,
"writeln", NULL,
"sin", "cos", "sqrt", "fabs", NULL,
};
#endif
int m = 0;
for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++)
{
const char *p = names[n];
if (p == NULL)
{ m++;
continue;
}
assert(m < sizeof(modules)/sizeof(modules[0]));
if (strcmp(s, p) == 0)
return modules[m];
}
return NULL; // didn't find it
}
#if UNITTEST
void unittest_importHint()
{
const char *p;
p = importHint("printf");
assert(p);
p = importHint("fabs");
assert(p);
p = importHint("xxxxx");
assert(!p);
}
#endif

View File

@@ -1,411 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "root.h"
#include "dsymbol.h"
#include "import.h"
#include "identifier.h"
#include "module.h"
#include "scope.h"
#include "hdrgen.h"
#include "mtype.h"
#include "declaration.h"
#include "id.h"
#include "attrib.h"
/********************************* Import ****************************/
Import::Import(Loc loc, Identifiers *packages, Identifier *id, Identifier *aliasId,
int isstatic)
: Dsymbol()
{
assert(id);
this->loc = loc;
this->packages = packages;
this->id = id;
this->aliasId = aliasId;
this->isstatic = isstatic;
protection = PROTundefined;
pkg = NULL;
mod = NULL;
}
void Import::addAlias(Identifier *name, Identifier *alias)
{
if (isstatic)
error("cannot have an import bind list");
names.push(name);
aliases.push(alias);
}
const char *Import::kind()
{
return isstatic ? (char *)"static import" : (char *)"import";
}
enum PROT Import::prot()
{
return protection;
}
Dsymbol *Import::syntaxCopy(Dsymbol *s)
{
assert(!s);
Import *si = new Import(loc, packages, id, aliasId, isstatic);
for (size_t i = 0; i < names.dim; i++)
{
si->addAlias(names[i], aliases[i]);
}
return si;
}
void Import::load(Scope *sc)
{
//printf("Import::load('%s')\n", toChars());
// See if existing module
DsymbolTable *dst = Package::resolve(packages, NULL, &pkg);
#if TARGET_NET //dot net needs modules and packages with same name
#else
if (pkg && pkg->isModule())
{
::error(loc, "can only import from a module, not from a member of module %s. Did you mean `import %s : %s`?",
pkg->toChars(), pkg->toPrettyChars(), id->toChars());
mod = pkg->isModule(); // Error recovery - treat as import of that module
return;
}
#endif
Dsymbol *s = dst->lookup(id);
if (s)
{
#if TARGET_NET
mod = (Module *)s;
#else
if (s->isModule())
mod = (Module *)s;
else
{
if (pkg)
{
::error(loc, "can only import from a module, not from package %s.%s",
pkg->toPrettyChars(), id->toChars());
}
else
{
::error(loc, "can only import from a module, not from package %s",
id->toChars());
}
}
#endif
}
if (!mod)
{
// Load module
mod = Module::load(loc, packages, id);
if (mod)
{
dst->insert(id, mod); // id may be different from mod->ident,
// if so then insert alias
if (!mod->importedFrom)
mod->importedFrom = sc ? sc->module->importedFrom : Module::rootModule;
}
}
if (!pkg)
pkg = mod;
//printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
}
void escapePath(OutBuffer *buf, const char *fname)
{
while (1)
{
switch (*fname)
{
case 0:
return;
case '(':
case ')':
case '\\':
buf->writebyte('\\');
default:
buf->writebyte(*fname);
break;
}
fname++;
}
}
void Import::importAll(Scope *sc)
{
if (!mod)
{
load(sc);
mod->importAll(0);
/* Default to private importing
*/
enum PROT prot = sc->protection;
if (!sc->explicitProtection)
prot = PROTprivate;
sc->scopesym->importScope(this, prot);
}
}
void Import::semantic(Scope *sc)
{
//printf("Import::semantic('%s')\n", toChars());
// Load if not already done so
if (!mod)
{ load(sc);
if (mod)
mod->importAll(0);
}
if (mod)
{
#if 0
if (mod->loc.linnum != 0)
{ /* If the line number is not 0, then this is not
* a 'root' module, i.e. it was not specified on the command line.
*/
mod->importedFrom = sc->module->importedFrom;
assert(mod->importedFrom);
}
#endif
// Modules need a list of each imported module
//printf("%s imports %s\n", sc->module->toChars(), mod->toChars());
sc->module->aimports.push(mod);
/* Default to private importing
*/
enum PROT prot = sc->protection;
if (!sc->explicitProtection)
prot = PROTprivate;
for (Scope *scd = sc; scd; scd = scd->enclosing)
{
if (scd->scopesym)
{
scd->scopesym->importScope(this, prot);
break;
}
}
mod->semantic();
if (mod->needmoduleinfo)
{ //printf("module4 %s because of %s\n", sc->module->toChars(), mod->toChars());
sc->module->needmoduleinfo = 1;
}
if (aliasId)
{
AliasDeclaration *ad = new AliasDeclaration(loc, aliasId, mod);
sc->insert(ad);
ad->semantic(sc);
}
}
//printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
if (global.params.moduleDeps != NULL &&
// object self-imports itself, so skip that (Bugzilla 7547)
!(id == Id::object && sc->module->ident == Id::object))
{
/* The grammar of the file is:
* ImportDeclaration
* ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> "
* ModuleAliasIdentifier ] "\n"
*
* BasicImportDeclaration
* ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection
* " [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")"
*
* FilePath
* - any string with '(', ')' and '\' escaped with the '\' character
*/
OutBuffer *ob = global.params.moduleDeps;
ob->writestring(sc->module->toPrettyChars());
ob->writestring(" (");
escapePath(ob, sc->module->srcfile->toChars());
ob->writestring(") : ");
ProtDeclaration::protectionToCBuffer(ob, sc->protection);
if (isstatic)
StorageClassDeclaration::stcToCBuffer(ob, STCstatic);
ob->writestring(": ");
if (packages)
{
for (size_t i = 0; i < packages->dim; i++)
{
Identifier *pid = (*packages)[i];
ob->printf("%s.", pid->toChars());
}
}
ob->writestring(id->toChars());
ob->writestring(" (");
if (mod)
escapePath(ob, mod->srcfile->toChars());
else
ob->writestring("???");
ob->writebyte(')');
for (size_t i = 0; i < names.dim; i++)
{
if (i == 0)
ob->writebyte(':');
else
ob->writebyte(',');
Identifier *name = names[i];
Identifier *alias = aliases[i];
if (!alias)
{
ob->printf("%s", name->toChars());
alias = name;
}
else
ob->printf("%s=%s", alias->toChars(), name->toChars());
}
if (aliasId)
ob->printf(" -> %s", aliasId->toChars());
ob->writenl();
}
//printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
}
void Import::semantic2(Scope *sc)
{
//printf("Import::semantic2('%s')\n", toChars());
mod->semantic2();
if (mod->needmoduleinfo)
{ //printf("module5 %s because of %s\n", sc->module->toChars(), mod->toChars());
sc->module->needmoduleinfo = 1;
}
}
Dsymbol *Import::search(Loc loc, Identifier *ident, int flags)
{
//printf("%s.Import::search(ident = '%s', flags = x%x)\n", toChars(), ident->toChars(), flags);
if (!pkg)
{ load(NULL);
mod->semantic();
}
if (names.dim) // selective import
{
for (size_t i = 0; i < names.dim; i++)
{
Identifier *name = (Identifier *)names[i];
Identifier *alias = (Identifier *)aliases[i];
if (!alias)
alias = name;
if (alias->equals(ident))
return mod->search(loc, name, flags);
}
// What should happen when renamed and selective imports are mixed?
// This makes the whole module available with the renamed id.
if (aliasId && aliasId->equals(ident))
return mod;
}
else // non-selective import
{
// For renamed imports, only the alias name is visible.
if (aliasId)
{
if (aliasId->equals(ident))
return mod;
return 0;
}
// For non-static imports, prefer symbols in the module over the module name.
if (!isstatic)
{
Dsymbol *s = mod->search(loc, ident, flags);
if (s)
return s;
}
// Make the start of the package name available.
if (pkg->ident->equals(ident))
{
return pkg;
}
}
return 0;
}
void Import::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
if (hgs->hdrgen && id == Id::object)
return; // object is imported by default
if (isstatic)
buf->writestring("static ");
buf->writestring("import ");
if (aliasId)
{
buf->printf("%s = ", aliasId->toChars());
}
if (packages && packages->dim)
{
for (size_t i = 0; i < packages->dim; i++)
{ Identifier *pid = (*packages)[i];
buf->printf("%s.", pid->toChars());
}
}
buf->printf("%s", id->toChars());
if (names.dim)
{
buf->writestring(" : ");
for (size_t i = 0; i < names.dim; i++)
{
Identifier *name = names[i];
Identifier *alias = aliases[i];
if (alias)
buf->printf("%s = %s", alias->toChars(), name->toChars());
else
buf->printf("%s", name->toChars());
if (i < names.dim - 1)
buf->writestring(", ");
}
}
buf->printf(";");
buf->writenl();
}
char *Import::toChars()
{
return id->toChars();
}

View File

@@ -1,63 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_IMPORT_H
#define DMD_IMPORT_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "dsymbol.h"
struct Identifier;
struct Scope;
struct OutBuffer;
struct Module;
struct Package;
struct AliasDeclaration;
struct HdrGenState;
struct Import : Dsymbol
{
// isstatic import aliasId = packages.id;
Identifiers *packages; // array of Identifier's representing packages
Identifier *id; // module Identifier
Identifier *aliasId;
int isstatic; // !=0 if static import
enum PROT protection;
// Pairs of alias=name to bind into current namespace
Identifiers names;
Identifiers aliases;
Module *mod;
Package *pkg; // leftmost package/module
Import(Loc loc, Identifiers *packages, Identifier *id, Identifier *aliasId,
int isstatic);
void addAlias(Identifier *name, Identifier *alias);
const char *kind();
enum PROT prot();
Dsymbol *syntaxCopy(Dsymbol *s); // copy only syntax trees
void load(Scope *sc);
void importAll(Scope *sc);
void semantic(Scope *sc);
void semantic2(Scope *sc);
Dsymbol *search(Loc loc, Identifier *ident, int flags);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
char *toChars();
Import *isImport() { return this; }
};
#endif /* DMD_IMPORT_H */

View File

@@ -1,845 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "mars.h"
#include "init.h"
#include "expression.h"
#include "statement.h"
#include "identifier.h"
#include "declaration.h"
#include "aggregate.h"
#include "scope.h"
#include "mtype.h"
#include "hdrgen.h"
/********************************** Initializer *******************************/
Initializer::Initializer(Loc loc)
{
this->loc = loc;
}
Initializer *Initializer::syntaxCopy()
{
return this;
}
Initializer *Initializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
return this;
}
Type *Initializer::inferType(Scope *sc)
{
error(loc, "cannot infer type from initializer");
return Type::terror;
}
Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
{ Initializers *a = NULL;
if (ai)
{
a = new Initializers();
a->setDim(ai->dim);
for (size_t i = 0; i < a->dim; i++)
{ Initializer *e = (*ai)[i];
e = e->syntaxCopy();
(*a)[i] = e;
}
}
return a;
}
char *Initializer::toChars()
{ OutBuffer *buf;
HdrGenState hgs;
memset(&hgs, 0, sizeof(hgs));
buf = new OutBuffer();
toCBuffer(buf, &hgs);
return buf->toChars();
}
/********************************** VoidInitializer ***************************/
VoidInitializer::VoidInitializer(Loc loc)
: Initializer(loc)
{
type = NULL;
}
Initializer *VoidInitializer::syntaxCopy()
{
return new VoidInitializer(loc);
}
Initializer *VoidInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
//printf("VoidInitializer::semantic(t = %p)\n", t);
type = t;
return this;
}
Expression *VoidInitializer::toExpression()
{
error(loc, "void initializer has no value");
return new IntegerExp(0);
}
void VoidInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("void");
}
/********************************** StructInitializer *************************/
StructInitializer::StructInitializer(Loc loc)
: Initializer(loc)
{
ad = NULL;
#if IN_LLVM
ltype = NULL;
#endif
}
Initializer *StructInitializer::syntaxCopy()
{
StructInitializer *ai = new StructInitializer(loc);
assert(field.dim == value.dim);
ai->field.setDim(field.dim);
ai->value.setDim(value.dim);
for (size_t i = 0; i < field.dim; i++)
{
ai->field[i] = field[i];
Initializer *init = value[i];
init = init->syntaxCopy();
ai->value[i] = init;
}
return ai;
}
void StructInitializer::addInit(Identifier *field, Initializer *value)
{
//printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
this->field.push(field);
this->value.push(value);
}
Initializer *StructInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
int errors = 0;
//printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
vars.setDim(field.dim);
t = t->toBasetype();
if (t->ty == Tstruct)
{
unsigned fieldi = 0;
TypeStruct *ts = (TypeStruct *)t;
ad = ts->sym;
size_t nfields = ad->fields.dim;
#if DMDV2
if (((StructDeclaration *)ad)->isnested)
nfields--; // don't count pointer to outer
#endif
for (size_t i = 0; i < field.dim; i++)
{
Identifier *id = field[i];
Initializer *val = value[i];
Dsymbol *s;
VarDeclaration *v;
if (id == NULL)
{
if (fieldi >= nfields)
{ error(loc, "too many initializers for %s", ad->toChars());
errors = 1;
field.remove(i);
i--;
continue;
}
else
{
s = ad->fields[fieldi];
}
}
else
{
//s = ad->symtab->lookup(id);
s = ad->search(loc, id, 0);
if (!s)
{
s = ad->search_correct(id);
if (s)
error(loc, "'%s' is not a member of '%s', did you mean '%s %s'?",
id->toChars(), t->toChars(), s->kind(), s->toChars());
else
error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
errors = 1;
continue;
}
s = s->toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
error(loc, "%s.%s is not a per-instance initializable field",
t->toChars(), s->toChars());
errors = 1;
break;
}
if (s == ad->fields[fieldi])
break;
}
}
if (s && (v = s->isVarDeclaration()) != NULL)
{
val = val->semantic(sc, v->type, needInterpret);
value[i] = val;
vars[i] = v;
}
else
{ error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
errors = 1;
}
fieldi++;
}
}
else if (t->ty == Tdelegate && value.dim == 0)
{ /* Rewrite as empty delegate literal { }
*/
Parameters *arguments = new Parameters;
Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
fd->fbody = new CompoundStatement(loc, new Statements());
fd->endloc = loc;
Expression *e = new FuncExp(loc, fd);
ExpInitializer *ie = new ExpInitializer(loc, e);
return ie->semantic(sc, t, needInterpret);
}
else
{
error(loc, "a struct is not a valid initializer for a %s", t->toChars());
errors = 1;
}
if (errors)
{
field.setDim(0);
value.setDim(0);
vars.setDim(0);
}
return this;
}
/***************************************
* This works by transforming a struct initializer into
* a struct literal. In the future, the two should be the
* same thing.
*/
Expression *StructInitializer::toExpression()
{ Expression *e;
//printf("StructInitializer::toExpression() %s\n", toChars());
if (!ad) // if fwd referenced
{
return NULL;
}
StructDeclaration *sd = ad->isStructDeclaration();
if (!sd)
return NULL;
Expressions *elements = new Expressions();
size_t nfields = ad->fields.dim;
#if DMDV2
if (sd->isnested)
nfields--;
#endif
elements->setDim(nfields);
for (size_t i = 0; i < elements->dim; i++)
{
(*elements)[i] = NULL;
}
unsigned fieldi = 0;
for (size_t i = 0; i < value.dim; i++)
{
Identifier *id = field[i];
if (id)
{
Dsymbol * s = ad->search(loc, id, 0);
if (!s)
{
error(loc, "'%s' is not a member of '%s'", id->toChars(), sd->toChars());
goto Lno;
}
s = s->toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
s->error("is not a per-instance initializable field");
goto Lno;
}
if (s == ad->fields[fieldi])
break;
}
}
else if (fieldi >= nfields)
{ error(loc, "too many initializers for '%s'", ad->toChars());
goto Lno;
}
Initializer *iz = value[i];
if (!iz)
goto Lno;
Expression *ex = iz->toExpression();
if (!ex)
goto Lno;
if ((*elements)[fieldi])
{ error(loc, "duplicate initializer for field '%s'",
ad->fields[fieldi]->toChars());
goto Lno;
}
(*elements)[fieldi] = ex;
++fieldi;
}
// Now, fill in any missing elements with default initializers.
// We also need to validate any anonymous unions
for (size_t i = 0; i < elements->dim; )
{
VarDeclaration * vd = ad->fields[i]->isVarDeclaration();
int unionSize = ad->numFieldsInUnion(i);
if (unionSize == 1)
{ // Not a union -- default initialize if missing
if (!(*elements)[i])
{ // Default initialize
if (vd->init)
(*elements)[i] = vd->init->toExpression();
else
(*elements)[i] = vd->type->defaultInit();
}
}
else
{ // anonymous union -- check for errors
int found = -1; // index of the first field with an initializer
for (size_t j = i; j < i + unionSize; ++j)
{
if (!(*elements)[j])
continue;
if (found >= 0)
{
VarDeclaration * v1 = ((Dsymbol *)ad->fields.data[found])->isVarDeclaration();
VarDeclaration * v = ((Dsymbol *)ad->fields.data[j])->isVarDeclaration();
error(loc, "%s cannot have initializers for fields %s and %s in same union",
ad->toChars(),
v1->toChars(), v->toChars());
goto Lno;
}
found = j;
}
if (found == -1)
{
error(loc, "no initializer for union that contains field %s",
vd->toChars());
goto Lno;
}
}
i += unionSize;
}
e = new StructLiteralExp(loc, sd, elements);
e->type = sd->type;
return e;
Lno:
delete elements;
return NULL;
}
void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
//printf("StructInitializer::toCBuffer()\n");
buf->writebyte('{');
for (size_t i = 0; i < field.dim; i++)
{
if (i > 0)
buf->writebyte(',');
Identifier *id = field[i];
if (id)
{
buf->writestring(id->toChars());
buf->writebyte(':');
}
Initializer *iz = value[i];
if (iz)
iz->toCBuffer(buf, hgs);
}
buf->writebyte('}');
}
/********************************** ArrayInitializer ************************************/
ArrayInitializer::ArrayInitializer(Loc loc)
: Initializer(loc)
{
dim = 0;
type = NULL;
sem = 0;
}
Initializer *ArrayInitializer::syntaxCopy()
{
//printf("ArrayInitializer::syntaxCopy()\n");
ArrayInitializer *ai = new ArrayInitializer(loc);
assert(index.dim == value.dim);
ai->index.setDim(index.dim);
ai->value.setDim(value.dim);
for (size_t i = 0; i < ai->value.dim; i++)
{ Expression *e = index[i];
if (e)
e = e->syntaxCopy();
ai->index[i] = e;
Initializer *init = value[i];
init = init->syntaxCopy();
ai->value[i] = init;
}
return ai;
}
void ArrayInitializer::addInit(Expression *index, Initializer *value)
{
this->index.push(index);
this->value.push(value);
dim = 0;
type = NULL;
}
Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{ unsigned i;
unsigned length;
const unsigned amax = 0x80000000;
//printf("ArrayInitializer::semantic(%s)\n", t->toChars());
if (sem) // if semantic() already run
return this;
sem = 1;
type = t;
t = t->toBasetype();
switch (t->ty)
{
case Tpointer:
case Tsarray:
case Tarray:
break;
default:
error(loc, "cannot use array to initialize %s", type->toChars());
goto Lerr;
}
length = 0;
for (i = 0; i < index.dim; i++)
{
Expression *idx = index[i];
if (idx)
{ idx = idx->semantic(sc);
idx = idx->ctfeInterpret();
index[i] = idx;
length = idx->toInteger();
}
Initializer *val = value[i];
ExpInitializer *ei = val->isExpInitializer();
if (ei && !idx)
ei->expandTuples = 1;
val = val->semantic(sc, t->nextOf(), needInterpret);
ei = val->isExpInitializer();
// found a tuple, expand it
if (ei && ei->exp->op == TOKtuple)
{
TupleExp *te = (TupleExp *)ei->exp;
index.remove(i);
value.remove(i);
for (size_t j = 0; j < te->exps->dim; ++j)
{
Expression *e = (*te->exps)[j];
index.insert(i + j, (Expression *)NULL);
value.insert(i + j, new ExpInitializer(e->loc, e));
}
i--;
continue;
}
else
{
value[i] = val;
}
length++;
if (length == 0)
{ error(loc, "array dimension overflow");
goto Lerr;
}
if (length > dim)
dim = length;
}
if (t->ty == Tsarray)
{
dinteger_t edim = ((TypeSArray *)t)->dim->toInteger();
if (dim > edim)
{
error(loc, "array initializer has %u elements, but array length is %jd", dim, edim);
goto Lerr;
}
}
if ((unsigned long) dim * t->nextOf()->size() >= amax)
{ error(loc, "array dimension %u exceeds max of %u", dim, amax / t->nextOf()->size());
goto Lerr;
}
return this;
Lerr:
return new ExpInitializer(loc, new ErrorExp());
}
/********************************
* If possible, convert array initializer to array literal.
*/
Expression *ArrayInitializer::toExpression()
{ Expressions *elements;
//printf("ArrayInitializer::toExpression(), dim = %d\n", dim);
//static int i; if (++i == 2) halt();
size_t edim;
Type *t = NULL;
if (type)
{
if (type == Type::terror)
return new ErrorExp();
t = type->toBasetype();
switch (t->ty)
{
case Tsarray:
edim = ((TypeSArray *)t)->dim->toInteger();
break;
case Tpointer:
case Tarray:
edim = dim;
break;
default:
assert(0);
}
}
else
{
edim = value.dim;
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
{
if (index[i])
j = index[i]->toInteger();
if (j >= edim)
edim = j + 1;
}
}
elements = new Expressions();
elements->setDim(edim);
elements->zero();
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
{
if (index[i])
j = (index[i])->toInteger();
assert(j < edim);
Initializer *iz = value[i];
if (!iz)
goto Lno;
Expression *ex = iz->toExpression();
if (!ex)
{
goto Lno;
}
(*elements)[j] = ex;
}
/* Fill in any missing elements with the default initializer
*/
{
Expression *init = NULL;
for (size_t i = 0; i < edim; i++)
{
if (!(*elements)[i])
{
if (!type)
goto Lno;
if (!init)
init = t->next->defaultInit();
(*elements)[i] = init;
}
}
Expression *e = new ArrayLiteralExp(loc, elements);
e->type = type;
return e;
}
Lno:
delete elements;
error(loc, "array initializers as expressions are not allowed");
return new ErrorExp();
}
/********************************
* If possible, convert array initializer to associative array initializer.
*/
Initializer *ArrayInitializer::toAssocArrayInitializer()
{
Expression *e;
//printf("ArrayInitializer::toAssocArrayInitializer()\n");
//static int i; if (++i == 2) halt();
Expressions *keys = new Expressions();
keys->setDim(value.dim);
Expressions *values = new Expressions();
values->setDim(value.dim);
for (size_t i = 0; i < value.dim; i++)
{
e = index[i];
if (!e)
goto Lno;
(*keys)[i] = e;
Initializer *iz = value[i];
if (!iz)
goto Lno;
e = iz->toExpression();
if (!e)
goto Lno;
(*values)[i] = e;
}
e = new AssocArrayLiteralExp(loc, keys, values);
return new ExpInitializer(loc, e);
Lno:
delete keys;
delete values;
error(loc, "not an associative array initializer");
return this;
}
Type *ArrayInitializer::inferType(Scope *sc)
{
for (size_t i = 0; i < value.dim; i++)
{
if (index.data[i])
goto Laa;
}
if (value.dim)
{
Initializer *iz = (Initializer *)value.data[0];
if (iz)
{ Type *t = iz->inferType(sc);
t = new TypeSArray(t, new IntegerExp(value.dim));
t = t->semantic(loc, sc);
return t;
}
}
Laa:
/* It's possibly an associative array initializer
*/
Initializer *iz = (Initializer *)value.data[0];
Expression *indexinit = (Expression *)index.data[0];
if (iz && indexinit)
{ Type *t = iz->inferType(sc);
indexinit = indexinit->semantic(sc);
Type *indext = indexinit->type;
t = new TypeAArray(t, indext);
type = t->semantic(loc, sc);
}
else
error(loc, "cannot infer type from this array initializer");
return type;
}
void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writebyte('[');
for (size_t i = 0; i < index.dim; i++)
{
if (i > 0)
buf->writebyte(',');
Expression *ex = index[i];
if (ex)
{
ex->toCBuffer(buf, hgs);
buf->writebyte(':');
}
Initializer *iz = value[i];
if (iz)
iz->toCBuffer(buf, hgs);
}
buf->writebyte(']');
}
/********************************** ExpInitializer ************************************/
ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
: Initializer(loc)
{
this->exp = exp;
this->expandTuples = 0;
}
Initializer *ExpInitializer::syntaxCopy()
{
return new ExpInitializer(loc, exp->syntaxCopy());
}
Initializer *ExpInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
//printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
exp = exp->semantic(sc);
if (exp->op == TOKerror)
return this;
int olderrors = global.errors;
if (needInterpret)
exp = exp->ctfeInterpret();
else
exp = exp->optimize(WANTvalue);
if (!global.gag && olderrors != global.errors)
return this; // Failed, suppress duplicate error messages
if (exp->op == TOKtype)
exp->error("initializer must be an expression, not '%s'", exp->toChars());
Type *tb = t->toBasetype();
if (exp->op == TOKtuple &&
expandTuples &&
!exp->implicitConvTo(t))
return new ExpInitializer(loc, exp);
/* Look for case of initializing a static array with a too-short
* string literal, such as:
* char[5] foo = "abc";
* Allow this by doing an explicit cast, which will lengthen the string
* literal.
*/
if (exp->op == TOKstring && tb->ty == Tsarray && exp->type->ty == Tsarray)
{ StringExp *se = (StringExp *)exp;
if (!se->committed && se->type->ty == Tsarray &&
((TypeSArray *)se->type)->dim->toInteger() <
((TypeSArray *)t)->dim->toInteger())
{
exp = se->castTo(sc, t);
goto L1;
}
}
// Look for the case of statically initializing an array
// with a single member.
if (tb->ty == Tsarray &&
!tb->nextOf()->equals(exp->type->toBasetype()->nextOf()) &&
exp->implicitConvTo(tb->nextOf())
)
{
t = tb->nextOf();
}
exp = exp->implicitCastTo(sc, t);
if (exp->op == TOKerror)
return this;
L1:
if (needInterpret)
exp = exp->ctfeInterpret();
else
exp = exp->optimize(WANTvalue);
//printf("-ExpInitializer::semantic(): "); exp->print();
return this;
}
Type *ExpInitializer::inferType(Scope *sc)
{
//printf("ExpInitializer::inferType() %s\n", toChars());
exp = exp->semantic(sc);
exp = resolveProperties(sc, exp);
// Give error for overloaded function addresses
if (exp->op == TOKsymoff)
{ SymOffExp *se = (SymOffExp *)exp;
if (
#if DMDV2
se->hasOverloads &&
#else
se->var->isFuncDeclaration() &&
#endif
!se->var->isFuncDeclaration()->isUnique())
exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
}
// Give error for overloaded function addresses
if (exp->op == TOKdelegate)
{ DelegateExp *se = (DelegateExp *)exp;
if (se->hasOverloads &&
se->func->isFuncDeclaration() &&
!se->func->isFuncDeclaration()->isUnique())
exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
}
Type *t = exp->type;
if (!t)
t = Initializer::inferType(sc);
return t;
}
Expression *ExpInitializer::toExpression()
{
return exp;
}
void ExpInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
exp->toCBuffer(buf, hgs);
}

View File

@@ -1,149 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef INIT_H
#define INIT_H
#include "root.h"
#include "mars.h"
#include "arraytypes.h"
struct Identifier;
struct Expression;
struct Scope;
struct Type;
struct dt_t;
struct AggregateDeclaration;
struct VoidInitializer;
struct StructInitializer;
struct ArrayInitializer;
struct ExpInitializer;
struct HdrGenState;
enum NeedInterpret { INITnointerpret, INITinterpret };
#if IN_LLVM
namespace llvm {
class StructType;
}
#endif
struct Initializer : Object
{
Loc loc;
Initializer(Loc loc);
virtual Initializer *syntaxCopy();
virtual Initializer *semantic(Scope *sc, Type *t, NeedInterpret needInterpret);
virtual Type *inferType(Scope *sc);
virtual Expression *toExpression() = 0;
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs) = 0;
char *toChars();
static Initializers *arraySyntaxCopy(Initializers *ai);
#if IN_DMD
virtual dt_t *toDt();
#endif
virtual VoidInitializer *isVoidInitializer() { return NULL; }
virtual StructInitializer *isStructInitializer() { return NULL; }
virtual ArrayInitializer *isArrayInitializer() { return NULL; }
virtual ExpInitializer *isExpInitializer() { return NULL; }
};
struct VoidInitializer : Initializer
{
Type *type; // type that this will initialize to
VoidInitializer(Loc loc);
Initializer *syntaxCopy();
Initializer *semantic(Scope *sc, Type *t, NeedInterpret needInterpret);
Expression *toExpression();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
#if IN_DMD
dt_t *toDt();
#endif
virtual VoidInitializer *isVoidInitializer() { return this; }
};
struct StructInitializer : Initializer
{
Identifiers field; // of Identifier *'s
Initializers value; // parallel array of Initializer *'s
VarDeclarations vars; // parallel array of VarDeclaration *'s
AggregateDeclaration *ad; // which aggregate this is for
StructInitializer(Loc loc);
Initializer *syntaxCopy();
void addInit(Identifier *field, Initializer *value);
Initializer *semantic(Scope *sc, Type *t, NeedInterpret needInterpret);
Expression *toExpression();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
#if IN_DMD
dt_t *toDt();
#endif
StructInitializer *isStructInitializer() { return this; }
#if IN_LLVM
llvm::StructType *ltype;
#endif
};
struct ArrayInitializer : Initializer
{
Expressions index; // indices
Initializers value; // of Initializer *'s
unsigned dim; // length of array being initialized
Type *type; // type that array will be used to initialize
int sem; // !=0 if semantic() is run
ArrayInitializer(Loc loc);
Initializer *syntaxCopy();
void addInit(Expression *index, Initializer *value);
Initializer *semantic(Scope *sc, Type *t, NeedInterpret needInterpret);
Type *inferType(Scope *sc);
Expression *toExpression();
Initializer *toAssocArrayInitializer();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
#if IN_DMD
dt_t *toDt();
dt_t *toDtBit(); // for bit arrays
#endif
ArrayInitializer *isArrayInitializer() { return this; }
};
struct ExpInitializer : Initializer
{
Expression *exp;
int expandTuples;
ExpInitializer(Loc loc, Expression *exp);
Initializer *syntaxCopy();
Initializer *semantic(Scope *sc, Type *t, NeedInterpret needInterpret);
Type *inferType(Scope *sc);
Expression *toExpression();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
#if IN_DMD
dt_t *toDt();
#endif
virtual ExpInitializer *isExpInitializer() { return this; }
};
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,462 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// This implements the JSON capability.
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <assert.h>
#include "rmem.h"
#include "root.h"
#include "mars.h"
#include "dsymbol.h"
#include "macro.h"
#include "template.h"
#include "lexer.h"
#include "aggregate.h"
#include "declaration.h"
#include "enum.h"
#include "id.h"
#include "module.h"
#include "scope.h"
#include "hdrgen.h"
#include "json.h"
#include "mtype.h"
#include "attrib.h"
#include "cond.h"
const char Pname[] = "name";
const char Pkind[] = "kind";
const char Pfile[] = "file";
const char Pline[] = "line";
const char Ptype[] = "type";
const char Pcomment[] = "comment";
const char Pmembers[] = "members";
const char Pprotection[] = "protection";
const char* Pprotectionnames[] = {NULL, "none", "private", "package", "protected", "public", "export"};
void JsonRemoveComma(OutBuffer *buf);
void json_generate(Modules *modules)
{ OutBuffer buf;
buf.writestring("[\n");
for (size_t i = 0; i < modules->dim; i++)
{ Module *m = modules->tdata()[i];
if (global.params.verbose)
printf("json gen %s\n", m->toChars());
m->toJsonBuffer(&buf);
buf.writestring(",\n");
}
JsonRemoveComma(&buf);
buf.writestring("]\n");
// Write buf to file
char *arg = global.params.xfilename;
if (!arg || !*arg)
{ // Generate lib file name from first obj name
char *n = global.params.objfiles->tdata()[0];
n = FileName::name(n);
FileName *fn = FileName::forceExt(n, global.json_ext);
arg = fn->toChars();
}
else if (arg[0] == '-' && arg[1] == 0)
{ // Write to stdout; assume it succeeds
int n = fwrite(buf.data, 1, buf.offset, stdout);
assert(n == buf.offset); // keep gcc happy about return values
return;
}
// if (!FileName::absolute(arg))
// arg = FileName::combine(dir, arg);
FileName *jsonfilename = FileName::defaultExt(arg, global.json_ext);
File *jsonfile = new File(jsonfilename);
assert(jsonfile);
jsonfile->setbuffer(buf.data, buf.offset);
jsonfile->ref = 1;
char *pt = FileName::path(jsonfile->toChars());
if (*pt)
FileName::ensurePathExists(pt);
mem.free(pt);
jsonfile->writev();
}
/*********************************
* Encode string into buf, and wrap it in double quotes.
*/
void JsonString(OutBuffer *buf, const char *s)
{
buf->writeByte('\"');
for (; *s; s++)
{
unsigned char c = (unsigned char) *s;
switch (c)
{
case '\n':
buf->writestring("\\n");
break;
case '\r':
buf->writestring("\\r");
break;
case '\t':
buf->writestring("\\t");
break;
case '\"':
buf->writestring("\\\"");
break;
case '\\':
buf->writestring("\\\\");
break;
case '/':
buf->writestring("\\/");
break;
case '\b':
buf->writestring("\\b");
break;
case '\f':
buf->writestring("\\f");
break;
default:
if (c < 0x20)
buf->printf("\\u%04x", c);
else
// Note that UTF-8 chars pass through here just fine
buf->writeByte(c);
break;
}
}
buf->writeByte('\"');
}
void JsonProperty(OutBuffer *buf, const char *name, const char *value)
{
JsonString(buf, name);
buf->writestring(" : ");
JsonString(buf, value);
buf->writestring(",\n");
}
void JsonProperty(OutBuffer *buf, const char *name, int value)
{
JsonString(buf, name);
buf->writestring(" : ");
buf->printf("%d", value);
buf->writestring(",\n");
}
void JsonRemoveComma(OutBuffer *buf)
{
if (buf->offset >= 2 &&
buf->data[buf->offset - 2] == ',' &&
buf->data[buf->offset - 1] == '\n')
buf->offset -= 2;
}
void Dsymbol::toJsonBuffer(OutBuffer *buf)
{
}
void Module::toJsonBuffer(OutBuffer *buf)
{
buf->writestring("{\n");
if (md)
JsonProperty(buf, Pname, md->toChars());
JsonProperty(buf, Pkind, kind());
JsonProperty(buf, Pfile, srcfile->toChars());
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
JsonString(buf, Pmembers);
buf->writestring(" : [\n");
size_t offset = buf->offset;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
s->toJsonBuffer(buf);
}
JsonRemoveComma(buf);
buf->writestring("]\n");
buf->writestring("}\n");
}
void AttribDeclaration::toJsonBuffer(OutBuffer *buf)
{
//printf("AttribDeclaration::toJsonBuffer()\n");
Dsymbols *d = include(NULL, NULL);
if (d)
{
size_t offset = buf->offset;
for (unsigned i = 0; i < d->dim; i++)
{ Dsymbol *s = d->tdata()[i];
//printf("AttribDeclaration::toJsonBuffer %s\n", s->toChars());
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
s->toJsonBuffer(buf);
}
JsonRemoveComma(buf);
}
}
void ConditionalDeclaration::toJsonBuffer(OutBuffer *buf)
{
//printf("ConditionalDeclaration::toJsonBuffer()\n");
if (condition->inc)
{
AttribDeclaration::toJsonBuffer(buf);
}
}
void InvariantDeclaration::toJsonBuffer(OutBuffer *buf) { }
void DtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
void StaticCtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
void StaticDtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
void ClassInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
void ModuleInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
void TypeInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
void UnitTestDeclaration::toJsonBuffer(OutBuffer *buf) { }
#if DMDV2
void PostBlitDeclaration::toJsonBuffer(OutBuffer *buf) { }
#endif
void Declaration::toJsonBuffer(OutBuffer *buf)
{
//printf("Declaration::toJsonBuffer()\n");
buf->writestring("{\n");
JsonProperty(buf, Pname, toChars());
JsonProperty(buf, Pkind, kind());
if (prot())
JsonProperty(buf, Pprotection, Pprotectionnames[prot()]);
if (type)
JsonProperty(buf, Ptype, type->toChars());
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
if (loc.linnum)
JsonProperty(buf, Pline, loc.linnum);
TypedefDeclaration *td = isTypedefDeclaration();
if (td)
{
JsonProperty(buf, "base", td->basetype->toChars());
}
JsonRemoveComma(buf);
buf->writestring("}\n");
}
void AggregateDeclaration::toJsonBuffer(OutBuffer *buf)
{
//printf("AggregateDeclaration::toJsonBuffer()\n");
buf->writestring("{\n");
JsonProperty(buf, Pname, toChars());
JsonProperty(buf, Pkind, kind());
if (prot())
JsonProperty(buf, Pprotection, Pprotectionnames[prot()]);
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
if (loc.linnum)
JsonProperty(buf, Pline, loc.linnum);
ClassDeclaration *cd = isClassDeclaration();
if (cd)
{
if (cd->baseClass)
{
JsonProperty(buf, "base", cd->baseClass->toChars());
}
if (cd->interfaces_dim)
{
JsonString(buf, "interfaces");
buf->writestring(" : [\n");
size_t offset = buf->offset;
for (size_t i = 0; i < cd->interfaces_dim; i++)
{ BaseClass *b = cd->interfaces[i];
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
JsonString(buf, b->base->toChars());
}
JsonRemoveComma(buf);
buf->writestring("],\n");
}
}
if (members)
{
JsonString(buf, Pmembers);
buf->writestring(" : [\n");
size_t offset = buf->offset;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
s->toJsonBuffer(buf);
}
JsonRemoveComma(buf);
buf->writestring("]\n");
}
JsonRemoveComma(buf);
buf->writestring("}\n");
}
void TemplateDeclaration::toJsonBuffer(OutBuffer *buf)
{
//printf("TemplateDeclaration::toJsonBuffer()\n");
buf->writestring("{\n");
JsonProperty(buf, Pname, toChars());
JsonProperty(buf, Pkind, kind());
if (prot())
JsonProperty(buf, Pprotection, Pprotectionnames[prot()]);
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
if (loc.linnum)
JsonProperty(buf, Pline, loc.linnum);
JsonString(buf, Pmembers);
buf->writestring(" : [\n");
size_t offset = buf->offset;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
s->toJsonBuffer(buf);
}
JsonRemoveComma(buf);
buf->writestring("]\n");
buf->writestring("}\n");
}
void EnumDeclaration::toJsonBuffer(OutBuffer *buf)
{
//printf("EnumDeclaration::toJsonBuffer()\n");
if (isAnonymous())
{
if (members)
{
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
s->toJsonBuffer(buf);
buf->writestring(",\n");
}
JsonRemoveComma(buf);
}
return;
}
buf->writestring("{\n");
JsonProperty(buf, Pname, toChars());
JsonProperty(buf, Pkind, kind());
if (prot())
JsonProperty(buf, Pprotection, Pprotectionnames[prot()]);
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
if (loc.linnum)
JsonProperty(buf, Pline, loc.linnum);
if (memtype)
JsonProperty(buf, "base", memtype->toChars());
if (members)
{
JsonString(buf, Pmembers);
buf->writestring(" : [\n");
size_t offset = buf->offset;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
if (offset != buf->offset)
{ buf->writestring(",\n");
offset = buf->offset;
}
s->toJsonBuffer(buf);
}
JsonRemoveComma(buf);
buf->writestring("]\n");
}
JsonRemoveComma(buf);
buf->writestring("}\n");
}
void EnumMember::toJsonBuffer(OutBuffer *buf)
{
//printf("EnumMember::toJsonBuffer()\n");
buf->writestring("{\n");
JsonProperty(buf, Pname, toChars());
JsonProperty(buf, Pkind, kind());
if (prot())
JsonProperty(buf, Pprotection, Pprotectionnames[prot()]);
if (comment)
JsonProperty(buf, Pcomment, (const char *)comment);
if (loc.linnum)
JsonProperty(buf, Pline, loc.linnum);
JsonRemoveComma(buf);
buf->writestring("}\n");
}

View File

@@ -1,24 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2008 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_JSON_H
#define DMD_JSON_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "arraytypes.h"
void json_generate(Modules *);
#endif /* DMD_JSON_H */

File diff suppressed because it is too large Load Diff

View File

@@ -1,321 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_LEXER_H
#define DMD_LEXER_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "mars.h"
struct StringTable;
struct Identifier;
struct Module;
/* Tokens:
( )
[ ]
{ }
< > <= >= == != === !==
<< >> <<= >>= >>> >>>=
+ - += -=
* / % *= /= %=
& | ^ &= |= ^=
= ! ~ @
^^ ^^=
++ --
. -> : ,
? && ||
*/
enum TOK
{
TOKreserved,
// Other
TOKlparen, TOKrparen,
TOKlbracket, TOKrbracket,
TOKlcurly, TOKrcurly,
TOKcolon, TOKneg,
TOKsemicolon, TOKdotdotdot,
TOKeof, TOKcast,
TOKnull, TOKassert,
TOKtrue, TOKfalse,
TOKarray, TOKcall,
TOKaddress,
TOKtype, TOKthrow,
TOKnew, TOKdelete,
TOKstar, TOKsymoff,
TOKvar, TOKdotvar,
TOKdotti, TOKdotexp,
TOKdottype, TOKslice,
TOKarraylength, TOKversion,
TOKmodule, TOKdollar,
TOKtemplate, TOKdottd,
TOKdeclaration, TOKtypeof,
TOKpragma, TOKdsymbol,
TOKtypeid, TOKuadd,
TOKremove,
TOKnewanonclass, TOKcomment,
TOKarrayliteral, TOKassocarrayliteral,
TOKstructliteral,
// Operators
TOKlt, TOKgt,
TOKle, TOKge,
TOKequal, TOKnotequal,
TOKidentity, TOKnotidentity,
TOKindex, TOKis,
TOKtobool,
// 60
// NCEG floating point compares
// !<>= <> <>= !> !>= !< !<= !<>
TOKunord,TOKlg,TOKleg,TOKule,TOKul,TOKuge,TOKug,TOKue,
TOKshl, TOKshr,
TOKshlass, TOKshrass,
TOKushr, TOKushrass,
TOKcat, TOKcatass, // ~ ~=
TOKadd, TOKmin, TOKaddass, TOKminass,
TOKmul, TOKdiv, TOKmod,
TOKmulass, TOKdivass, TOKmodass,
TOKand, TOKor, TOKxor,
TOKandass, TOKorass, TOKxorass,
TOKassign, TOKnot, TOKtilde,
TOKplusplus, TOKminusminus, TOKconstruct, TOKblit,
TOKdot, TOKarrow, TOKcomma,
TOKquestion, TOKandand, TOKoror,
TOKpreplusplus, TOKpreminusminus,
// 106
// Numeric literals
TOKint32v, TOKuns32v,
TOKint64v, TOKuns64v,
TOKfloat32v, TOKfloat64v, TOKfloat80v,
TOKimaginary32v, TOKimaginary64v, TOKimaginary80v,
// Char constants
TOKcharv, TOKwcharv, TOKdcharv,
// Leaf operators
TOKidentifier, TOKstring,
TOKthis, TOKsuper,
TOKhalt, TOKtuple,
TOKerror,
// Basic types
TOKvoid,
TOKint8, TOKuns8,
TOKint16, TOKuns16,
TOKint32, TOKuns32,
TOKint64, TOKuns64,
TOKfloat32, TOKfloat64, TOKfloat80,
TOKimaginary32, TOKimaginary64, TOKimaginary80,
TOKcomplex32, TOKcomplex64, TOKcomplex80,
TOKchar, TOKwchar, TOKdchar, TOKbit, TOKbool,
TOKcent, TOKucent,
// 152
// Aggregates
TOKstruct, TOKclass, TOKinterface, TOKunion, TOKenum, TOKimport,
TOKtypedef, TOKalias, TOKoverride, TOKdelegate, TOKfunction,
TOKmixin,
TOKalign, TOKextern, TOKprivate, TOKprotected, TOKpublic, TOKexport,
TOKstatic, /*TOKvirtual,*/ TOKfinal, TOKconst, TOKabstract, TOKvolatile,
TOKdebug, TOKdeprecated, TOKin, TOKout, TOKinout, TOKlazy,
TOKauto, TOKpackage, TOKmanifest, TOKimmutable,
// Statements
TOKif, TOKelse, TOKwhile, TOKfor, TOKdo, TOKswitch,
TOKcase, TOKdefault, TOKbreak, TOKcontinue, TOKwith,
TOKsynchronized, TOKreturn, TOKgoto, TOKtry, TOKcatch, TOKfinally,
TOKasm, TOKforeach, TOKforeach_reverse,
TOKscope,
TOKon_scope_exit, TOKon_scope_failure, TOKon_scope_success,
// Contracts
TOKbody, TOKinvariant,
// Testing
TOKunittest,
// Added after 1.0
TOKargTypes,
TOKref,
TOKmacro,
TOKat,
#if DMDV2
TOKtraits,
TOKoverloadset,
TOKpure,
TOKnothrow,
TOKtls,
TOKgshared,
TOKline,
TOKfile,
TOKshared,
TOKpow,
TOKpowass,
TOKgoesto,
TOKvector,
TOKpound,
#endif
// LDC specific
#if IN_LLVM
TOKgep,
#endif
TOKMAX
};
#define TOKwild TOKinout
#define BASIC_TYPES \
TOKwchar: case TOKdchar: \
case TOKbit: case TOKbool: case TOKchar: \
case TOKint8: case TOKuns8: \
case TOKint16: case TOKuns16: \
case TOKint32: case TOKuns32: \
case TOKint64: case TOKuns64: \
case TOKfloat32: case TOKfloat64: case TOKfloat80: \
case TOKimaginary32: case TOKimaginary64: case TOKimaginary80: \
case TOKcomplex32: case TOKcomplex64: case TOKcomplex80: \
case TOKvoid
#define BASIC_TYPES_X(t) \
TOKvoid: t = Type::tvoid; goto LabelX; \
case TOKint8: t = Type::tint8; goto LabelX; \
case TOKuns8: t = Type::tuns8; goto LabelX; \
case TOKint16: t = Type::tint16; goto LabelX; \
case TOKuns16: t = Type::tuns16; goto LabelX; \
case TOKint32: t = Type::tint32; goto LabelX; \
case TOKuns32: t = Type::tuns32; goto LabelX; \
case TOKint64: t = Type::tint64; goto LabelX; \
case TOKuns64: t = Type::tuns64; goto LabelX; \
case TOKfloat32: t = Type::tfloat32; goto LabelX; \
case TOKfloat64: t = Type::tfloat64; goto LabelX; \
case TOKfloat80: t = Type::tfloat80; goto LabelX; \
case TOKimaginary32: t = Type::timaginary32; goto LabelX; \
case TOKimaginary64: t = Type::timaginary64; goto LabelX; \
case TOKimaginary80: t = Type::timaginary80; goto LabelX; \
case TOKcomplex32: t = Type::tcomplex32; goto LabelX; \
case TOKcomplex64: t = Type::tcomplex64; goto LabelX; \
case TOKcomplex80: t = Type::tcomplex80; goto LabelX; \
case TOKbool: t = Type::tbool; goto LabelX; \
case TOKchar: t = Type::tchar; goto LabelX; \
case TOKwchar: t = Type::twchar; goto LabelX; \
case TOKdchar: t = Type::tdchar; goto LabelX; \
LabelX
struct Token
{
Token *next;
unsigned char *ptr; // pointer to first character of this token within buffer
enum TOK value;
unsigned char *blockComment; // doc comment string prior to this token
unsigned char *lineComment; // doc comment for previous token
union
{
// Integers
d_int64 int64value;
d_uns64 uns64value;
// Floats
#ifdef IN_GCC
// real_t float80value; // can't use this in a union!
#else
d_float80 float80value;
#endif
struct
{ unsigned char *ustring; // UTF8 string
unsigned len;
unsigned char postfix; // 'c', 'w', 'd'
};
Identifier *ident;
};
#ifdef IN_GCC
real_t float80value; // can't use this in a union!
#endif
static const char *tochars[TOKMAX];
static void *operator new(size_t sz);
int isKeyword();
void print();
const char *toChars();
static const char *toChars(enum TOK);
};
struct Lexer
{
static StringTable stringtable;
static OutBuffer stringbuffer;
static Token *freelist;
Loc loc; // for error messages
unsigned char *base; // pointer to start of buffer
unsigned char *end; // past end of buffer
unsigned char *p; // current character
Token token;
Module *mod;
int doDocComment; // collect doc comment information
int anyToken; // !=0 means seen at least one token
int commentToken; // !=0 means comments are TOKcomment's
Lexer(Module *mod,
unsigned char *base, unsigned begoffset, unsigned endoffset,
int doDocComment, int commentToken);
static void initKeywords();
static Identifier *idPool(const char *s);
static Identifier *uniqueId(const char *s);
static Identifier *uniqueId(const char *s, int num);
TOK nextToken();
TOK peekNext();
TOK peekNext2();
void scan(Token *t);
Token *peek(Token *t);
Token *peekPastParen(Token *t);
unsigned escapeSequence();
TOK wysiwygStringConstant(Token *t, int tc);
TOK hexStringConstant(Token *t);
#if DMDV2
TOK delimitedStringConstant(Token *t);
TOK tokenStringConstant(Token *t);
#endif
TOK escapeStringConstant(Token *t, int wide);
TOK charConstant(Token *t, int wide);
void stringPostfix(Token *t);
unsigned wchar(unsigned u);
TOK number(Token *t);
TOK inreal(Token *t);
void error(const char *format, ...) IS_PRINTF(2);
void error(Loc loc, const char *format, ...) IS_PRINTF(3);
void pragma();
unsigned decodeUTF();
void getDocComment(Token *t, unsigned lineComment);
static int isValidIdentifier(char *p);
static unsigned char *combineComments(unsigned char *c1, unsigned char *c2);
Loc tokenLoc();
};
#endif /* DMD_LEXER_H */

View File

@@ -1,449 +0,0 @@
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
/* Simple macro text processor.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <assert.h>
#include "rmem.h"
#include "root.h"
#include "macro.h"
#define isidstart(c) (isalpha(c) || (c) == '_')
#define isidchar(c) (isalnum(c) || (c) == '_')
unsigned char *memdup(unsigned char *p, size_t len)
{
return (unsigned char *)memcpy(mem.malloc(len), p, len);
}
Macro::Macro(unsigned char *name, size_t namelen, unsigned char *text, size_t textlen)
{
next = NULL;
#if 1
this->name = name;
this->namelen = namelen;
this->text = text;
this->textlen = textlen;
#else
this->name = name;
this->namelen = namelen;
this->text = text;
this->textlen = textlen;
#endif
inuse = 0;
}
Macro *Macro::search(unsigned char *name, size_t namelen)
{ Macro *table;
//printf("Macro::search(%.*s)\n", namelen, name);
for (table = this; table; table = table->next)
{
if (table->namelen == namelen &&
memcmp(table->name, name, namelen) == 0)
{
//printf("\tfound %d\n", table->textlen);
break;
}
}
return table;
}
Macro *Macro::define(Macro **ptable, unsigned char *name, size_t namelen, unsigned char *text, size_t textlen)
{
//printf("Macro::define('%.*s' = '%.*s')\n", namelen, name, textlen, text);
Macro *table;
//assert(ptable);
for (table = *ptable; table; table = table->next)
{
if (table->namelen == namelen &&
memcmp(table->name, name, namelen) == 0)
{
table->text = text;
table->textlen = textlen;
return table;
}
}
table = new Macro(name, namelen, text, textlen);
table->next = *ptable;
*ptable = table;
return table;
}
/**********************************************************
* Given buffer p[0..end], extract argument marg[0..marglen].
* Params:
* n 0: get entire argument
* 1..9: get nth argument
* -1: get 2nd through end
*/
unsigned extractArgN(unsigned char *p, unsigned end, unsigned char **pmarg, unsigned *pmarglen, int n)
{
/* Scan forward for matching right parenthesis.
* Nest parentheses.
* Skip over $( and $)
* Skip over "..." and '...' strings inside HTML tags.
* Skip over <!-- ... --> comments.
* Skip over previous macro insertions
* Set marglen.
*/
unsigned parens = 1;
unsigned char instring = 0;
unsigned incomment = 0;
unsigned intag = 0;
unsigned inexp = 0;
unsigned argn = 0;
unsigned v = 0;
Largstart:
#if 1
// Skip first space, if any, to find the start of the macro argument
if (v < end && isspace(p[v]))
v++;
#else
// Skip past spaces to find the start of the macro argument
for (; v < end && isspace(p[v]); v++)
;
#endif
*pmarg = p + v;
for (; v < end; v++)
{ unsigned char c = p[v];
switch (c)
{
case ',':
if (!inexp && !instring && !incomment && parens == 1)
{
argn++;
if (argn == 1 && n == -1)
{ v++;
goto Largstart;
}
if (argn == n)
break;
if (argn + 1 == n)
{ v++;
goto Largstart;
}
}
continue;
case '(':
if (!inexp && !instring && !incomment)
parens++;
continue;
case ')':
if (!inexp && !instring && !incomment && --parens == 0)
{
break;
}
continue;
case '"':
case '\'':
if (!inexp && !incomment && intag)
{
if (c == instring)
instring = 0;
else if (!instring)
instring = c;
}
continue;
case '<':
if (!inexp && !instring && !incomment)
{
if (v + 6 < end &&
p[v + 1] == '!' &&
p[v + 2] == '-' &&
p[v + 3] == '-')
{
incomment = 1;
v += 3;
}
else if (v + 2 < end &&
isalpha(p[v + 1]))
intag = 1;
}
continue;
case '>':
if (!inexp)
intag = 0;
continue;
case '-':
if (!inexp &&
!instring &&
incomment &&
v + 2 < end &&
p[v + 1] == '-' &&
p[v + 2] == '>')
{
incomment = 0;
v += 2;
}
continue;
case 0xFF:
if (v + 1 < end)
{
if (p[v + 1] == '{')
inexp++;
else if (p[v + 1] == '}')
inexp--;
}
continue;
default:
continue;
}
break;
}
if (argn == 0 && n == -1)
*pmarg = p + v;
*pmarglen = p + v - *pmarg;
//printf("extractArg%d('%.*s') = '%.*s'\n", n, end, p, *pmarglen, *pmarg);
return v;
}
/*****************************************************
* Expand macro in place in buf.
* Only look at the text in buf from start to end.
*/
void Macro::expand(OutBuffer *buf, unsigned start, unsigned *pend,
unsigned char *arg, unsigned arglen)
{
#if 0
printf("Macro::expand(buf[%d..%d], arg = '%.*s')\n", start, *pend, arglen, arg);
printf("Buf is: '%.*s'\n", *pend - start, buf->data + start);
#endif
static int nest;
if (nest > 100) // limit recursive expansion
return;
nest++;
unsigned end = *pend;
assert(start <= end);
assert(end <= buf->offset);
/* First pass - replace $0
*/
arg = memdup(arg, arglen);
for (unsigned u = start; u + 1 < end; )
{
unsigned char *p = buf->data; // buf->data is not loop invariant
/* Look for $0, but not $$0, and replace it with arg.
*/
if (p[u] == '$' && (isdigit(p[u + 1]) || p[u + 1] == '+'))
{
if (u > start && p[u - 1] == '$')
{ // Don't expand $$0, but replace it with $0
buf->remove(u - 1, 1);
end--;
u += 1; // now u is one past the closing '1'
continue;
}
unsigned char c = p[u + 1];
int n = (c == '+') ? -1 : c - '0';
unsigned char *marg;
unsigned marglen;
extractArgN(arg, arglen, &marg, &marglen, n);
if (marglen == 0)
{ // Just remove macro invocation
//printf("Replacing '$%c' with '%.*s'\n", p[u + 1], marglen, marg);
buf->remove(u, 2);
end -= 2;
}
else if (c == '+')
{
// Replace '$+' with 'arg'
//printf("Replacing '$%c' with '%.*s'\n", p[u + 1], marglen, marg);
buf->remove(u, 2);
buf->insert(u, marg, marglen);
end += marglen - 2;
// Scan replaced text for further expansion
unsigned mend = u + marglen;
expand(buf, u, &mend, NULL, 0);
end += mend - (u + marglen);
u = mend;
}
else
{
// Replace '$1' with '\xFF{arg\xFF}'
//printf("Replacing '$%c' with '\xFF{%.*s\xFF}'\n", p[u + 1], marglen, marg);
buf->data[u] = 0xFF;
buf->data[u + 1] = '{';
buf->insert(u + 2, marg, marglen);
buf->insert(u + 2 + marglen, "\xFF}", 2);
end += -2 + 2 + marglen + 2;
// Scan replaced text for further expansion
unsigned mend = u + 2 + marglen;
expand(buf, u + 2, &mend, NULL, 0);
end += mend - (u + 2 + marglen);
u = mend;
}
//printf("u = %d, end = %d\n", u, end);
//printf("#%.*s#\n", end, &buf->data[0]);
continue;
}
u++;
}
/* Second pass - replace other macros
*/
for (unsigned u = start; u + 4 < end; )
{
unsigned char *p = buf->data; // buf->data is not loop invariant
/* A valid start of macro expansion is $(c, where c is
* an id start character, and not $$(c.
*/
if (p[u] == '$' && p[u + 1] == '(' && isidstart(p[u + 2]))
{
//printf("\tfound macro start '%c'\n", p[u + 2]);
unsigned char *name = p + u + 2;
unsigned namelen = 0;
unsigned char *marg;
unsigned marglen;
unsigned v;
/* Scan forward to find end of macro name and
* beginning of macro argument (marg).
*/
for (v = u + 2; v < end; v++)
{ unsigned char c = p[v];
if (!isidchar(c))
{ // We've gone past the end of the macro name.
namelen = v - (u + 2);
break;
}
}
v += extractArgN(p + v, end - v, &marg, &marglen, 0);
assert(v <= end);
if (v < end)
{ // v is on the closing ')'
if (u > start && p[u - 1] == '$')
{ // Don't expand $$(NAME), but replace it with $(NAME)
buf->remove(u - 1, 1);
end--;
u = v; // now u is one past the closing ')'
continue;
}
Macro *m = search(name, namelen);
if (m)
{
#if 0
if (m->textlen && m->text[0] == ' ')
{ m->text++;
m->textlen--;
}
#endif
if (m->inuse && marglen == 0)
{ // Remove macro invocation
buf->remove(u, v + 1 - u);
end -= v + 1 - u;
}
else if (m->inuse && arglen == marglen && memcmp(arg, marg, arglen) == 0)
{ // Recursive expansion; just leave in place
}
else
{
//printf("\tmacro '%.*s'(%.*s) = '%.*s'\n", m->namelen, m->name, marglen, marg, m->textlen, m->text);
#if 1
marg = memdup(marg, marglen);
// Insert replacement text
buf->spread(v + 1, 2 + m->textlen + 2);
buf->data[v + 1] = 0xFF;
buf->data[v + 2] = '{';
memcpy(buf->data + v + 3, m->text, m->textlen);
buf->data[v + 3 + m->textlen] = 0xFF;
buf->data[v + 3 + m->textlen + 1] = '}';
end += 2 + m->textlen + 2;
// Scan replaced text for further expansion
m->inuse++;
unsigned mend = v + 1 + 2+m->textlen+2;
expand(buf, v + 1, &mend, marg, marglen);
end += mend - (v + 1 + 2+m->textlen+2);
m->inuse--;
buf->remove(u, v + 1 - u);
end -= v + 1 - u;
u += mend - (v + 1);
#else
// Insert replacement text
buf->insert(v + 1, m->text, m->textlen);
end += m->textlen;
// Scan replaced text for further expansion
m->inuse++;
unsigned mend = v + 1 + m->textlen;
expand(buf, v + 1, &mend, marg, marglen);
end += mend - (v + 1 + m->textlen);
m->inuse--;
buf->remove(u, v + 1 - u);
end -= v + 1 - u;
u += mend - (v + 1);
#endif
mem.free(marg);
//printf("u = %d, end = %d\n", u, end);
//printf("#%.*s#\n", end - u, &buf->data[u]);
continue;
}
}
else
{
// Replace $(NAME) with nothing
buf->remove(u, v + 1 - u);
end -= (v + 1 - u);
continue;
}
}
}
u++;
}
mem.free(arg);
*pend = end;
nest--;
}

View File

@@ -1,45 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_MACRO_H
#define DMD_MACRO_H 1
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "root.h"
struct Macro
{
private:
Macro *next; // next in list
unsigned char *name; // macro name
size_t namelen; // length of macro name
unsigned char *text; // macro replacement text
size_t textlen; // length of replacement text
int inuse; // macro is in use (don't expand)
Macro(unsigned char *name, size_t namelen, unsigned char *text, size_t textlen);
Macro *search(unsigned char *name, size_t namelen);
public:
static Macro *define(Macro **ptable, unsigned char *name, size_t namelen, unsigned char *text, size_t textlen);
void expand(OutBuffer *buf, unsigned start, unsigned *pend,
unsigned char *arg, unsigned arglen);
};
#endif

View File

@@ -1,300 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "root.h"
#include "init.h"
#include "declaration.h"
#include "aggregate.h"
#include "mtype.h"
#include "attrib.h"
#include "template.h"
#include "id.h"
#include "module.h"
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
char *cpp_mangle(Dsymbol *s);
#endif
char *mangle(Declaration *sthis)
{
OutBuffer buf;
char *id;
Dsymbol *s;
//printf("::mangle(%s), type %s\n", sthis->toChars(), sthis->type->toChars());
s = sthis;
do
{
//printf("mangle: s = %p, '%s', parent = %p\n", s, s->toChars(), s->parent);
if (s->ident)
{
FuncDeclaration *fd = s->isFuncDeclaration();
if (s != sthis && fd)
{
id = mangle(fd);
buf.prependstring(id);
goto L1;
}
else
{
id = s->ident->toChars();
int len = strlen(id);
char tmp[sizeof(len) * 3 + 1];
buf.prependstring(id);
sprintf(tmp, "%d", len);
buf.prependstring(tmp);
}
}
else
buf.prependstring("0");
s = s->parent;
} while (s);
// buf.prependstring("_D");
L1:
//printf("deco = '%s'\n", sthis->type->deco ? sthis->type->deco : "null");
//printf("sthis->type = %s\n", sthis->type->toChars());
FuncDeclaration *fd = sthis->isFuncDeclaration();
if (fd && (fd->needThis() || fd->isNested()))
buf.writeByte(Type::needThisPrefix());
if (sthis->type->deco)
buf.writestring(sthis->type->deco);
else
{
#ifdef DEBUG
if (!fd->inferRetType)
printf("%s\n", fd->toChars());
#endif
assert(fd && fd->inferRetType);
}
id = buf.toChars();
buf.data = NULL;
return id;
}
char *Declaration::mangle()
#if __DMC__
__out(result)
{
int len = strlen(result);
assert(len > 0);
//printf("mangle: '%s' => '%s'\n", toChars(), result);
for (int i = 0; i < len; i++)
{
assert(result[i] == '_' ||
result[i] == '@' ||
isalnum(result[i]) || result[i] & 0x80);
}
}
__body
#endif
{
//printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent->toChars() : "null", linkage);
if (!parent || parent->isModule()) // if at global scope
{
// If it's not a D declaration, no mangling
switch (linkage)
{
case LINKd:
break;
#if IN_LLVM
case LINKintrinsic:
#endif
case LINKc:
case LINKwindows:
case LINKpascal:
return ident->toChars();
case LINKcpp:
#if DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS)
return cpp_mangle(this);
#else
// Windows C++ mangling is done by C++ back end
return ident->toChars();
#endif
case LINKdefault:
error("forward declaration");
return ident->toChars();
default:
fprintf(stdmsg, "'%s', linkage = %d\n", toChars(), linkage);
assert(0);
}
}
char *p = ::mangle(this);
OutBuffer buf;
buf.writestring("_D");
buf.writestring(p);
p = buf.toChars();
buf.data = NULL;
//printf("Declaration::mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent->toChars() : "null", linkage, p);
return p;
}
char *FuncDeclaration::mangle()
#if __DMC__
__out(result)
{
assert(strlen(result) > 0);
}
__body
#endif
{
if (isMain())
return (char *)"_Dmain";
if (isWinMain() || isDllMain())
return ident->toChars();
assert(this);
return Declaration::mangle();
}
char *StructDeclaration::mangle()
{
//printf("StructDeclaration::mangle() '%s'\n", toChars());
return Dsymbol::mangle();
}
char *TypedefDeclaration::mangle()
{
//printf("TypedefDeclaration::mangle() '%s'\n", toChars());
return Dsymbol::mangle();
}
char *ClassDeclaration::mangle()
{
Dsymbol *parentsave = parent;
//printf("ClassDeclaration::mangle() %s.%s\n", parent->toChars(), toChars());
/* These are reserved to the compiler, so keep simple
* names for them.
*/
if (ident == Id::Exception)
{ if (parent->ident == Id::object)
parent = NULL;
}
else if (ident == Id::TypeInfo ||
// ident == Id::Exception ||
ident == Id::TypeInfo_Struct ||
ident == Id::TypeInfo_Class ||
ident == Id::TypeInfo_Typedef ||
ident == Id::TypeInfo_Tuple ||
this == object ||
this == classinfo ||
this == Module::moduleinfo ||
memcmp(ident->toChars(), "TypeInfo_", 9) == 0
)
parent = NULL;
char *id = Dsymbol::mangle();
parent = parentsave;
return id;
}
char *TemplateInstance::mangle()
{
OutBuffer buf;
#if 0
printf("TemplateInstance::mangle() %s", toChars());
if (parent)
printf(" parent = %s %s", parent->kind(), parent->toChars());
printf("\n");
#endif
char *id = ident ? ident->toChars() : toChars();
if (!tempdecl)
error("is not defined");
else
{
Dsymbol *par = isnested || isTemplateMixin() ? parent : tempdecl->parent;
if (par)
{
char *p = par->mangle();
if (p[0] == '_' && p[1] == 'D')
p += 2;
buf.writestring(p);
}
}
buf.printf("%zu%s", strlen(id), id);
id = buf.toChars();
buf.data = NULL;
//printf("TemplateInstance::mangle() %s = %s\n", toChars(), id);
return id;
}
char *TemplateMixin::mangle()
{
OutBuffer buf;
char *id;
#if 0
printf("TemplateMixin::mangle() %s", toChars());
if (parent)
printf(" parent = %s %s", parent->kind(), parent->toChars());
printf("\n");
#endif
id = ident ? ident->toChars() : toChars();
if (parent)
{
char *p = parent->mangle();
if (p[0] == '_' && p[1] == 'D')
p += 2;
buf.writestring(p);
}
buf.printf("%zu%s", strlen(id), id);
id = buf.toChars();
buf.data = NULL;
//printf("TemplateMixin::mangle() %s = %s\n", toChars(), id);
return id;
}
char *Dsymbol::mangle()
{
OutBuffer buf;
char *id;
#if 0
printf("Dsymbol::mangle() '%s'", toChars());
if (parent)
printf(" parent = %s %s", parent->kind(), parent->toChars());
printf("\n");
#endif
id = ident ? ident->toChars() : toChars();
if (parent)
{
char *p = parent->mangle();
if (p[0] == '_' && p[1] == 'D')
p += 2;
buf.writestring(p);
}
buf.printf("%zu%s", strlen(id), id);
id = buf.toChars();
buf.data = NULL;
//printf("Dsymbol::mangle() %s = %s\n", toChars(), id);
return id;
}

View File

@@ -1,396 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#if IN_LLVM
#include <cstdarg>
#endif
#if POSIX
#include <errno.h>
#endif
#include "rmem.h"
#include "root.h"
#include "mars.h"
#include "module.h"
#include "mtype.h"
#include "id.h"
#include "cond.h"
#include "expression.h"
#include "lexer.h"
#include "json.h"
#if WINDOWS_SEH
#include <windows.h>
long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep);
#endif
int response_expand(int *pargc, char ***pargv);
void browse(const char *url);
void getenv_setargv(const char *envvar, int *pargc, char** *pargv);
void obj_start(char *srcfile);
void obj_end(Library *library, File *objfile);
void printCtfePerformanceStats();
Global global;
Global::Global()
{
mars_ext = "d";
sym_ext = "d";
hdr_ext = "di";
doc_ext = "html";
ddoc_ext = "ddoc";
json_ext = "json";
map_ext = "map";
// LDC
ll_ext = "ll";
bc_ext = "bc";
s_ext = "s";
obj_ext = "o";
obj_ext_alt = "obj";
copyright = "Copyright (c) 1999-2012 by Digital Mars and Tomas Lindquist Olsen";
written = "written by Walter Bright and Tomas Lindquist Olsen";
version = "v1.075";
ldc_version = "trunk";
llvm_version = "LLVM "LDC_LLVM_VERSION_STRING;
global.structalign = STRUCTALIGN_DEFAULT;
// This should only be used as a global, so the other fields are
// automatically initialized to zero when the program is loaded.
// In particular, DO NOT zero-initialize .params here (like DMD
// does) because command-line options initialize some of those
// fields to non-zero defaults, and do so from constructors that
// may run before this one.
}
unsigned Global::startGagging()
{
++gag;
return gaggedErrors;
}
bool Global::endGagging(unsigned oldGagged)
{
bool anyErrs = (gaggedErrors != oldGagged);
--gag;
// Restore the original state of gagged errors; set total errors
// to be original errors + new ungagged errors.
errors -= (gaggedErrors - oldGagged);
gaggedErrors = oldGagged;
return anyErrs;
}
bool Global::isSpeculativeGagging()
{
return gag && gag == speculativeGag;
}
char *Loc::toChars() const
{
OutBuffer buf;
if (filename)
{
buf.printf("%s", filename);
}
if (linnum)
buf.printf("(%d)", linnum);
buf.writeByte(0);
return (char *)buf.extractData();
}
Loc::Loc(Module *mod, unsigned linnum)
{
this->linnum = linnum;
this->filename = mod ? mod->srcfile->toChars() : NULL;
}
bool Loc::equals(const Loc& loc)
{
return linnum == loc.linnum && FileName::equals(filename, loc.filename);
}
/**************************************
* Print error message
*/
void error(Loc loc, const char *format, ...)
{
va_list ap;
va_start(ap, format);
verror(loc, format, ap);
va_end( ap );
}
void warning(Loc loc, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vwarning(loc, format, ap);
va_end( ap );
}
/**************************************
* Print supplementary message about the last error
* Used for backtraces, etc
*/
void errorSupplemental(Loc loc, const char *format, ...)
{
va_list ap;
va_start(ap, format);
verrorSupplemental(loc, format, ap);
va_end( ap );
}
void verror(Loc loc, const char *format, va_list ap, const char *p1, const char *p2)
{
if (!global.gag)
{
char *p = loc.toChars();
if (*p)
fprintf(stdmsg, "%s: ", p);
mem.free(p);
fprintf(stdmsg, "Error: ");
if (p1)
fprintf(stdmsg, "%s ", p1);
if (p2)
fprintf(stdmsg, "%s ", p2);
#if _MSC_VER
// MS doesn't recognize %zu format
OutBuffer tmp;
tmp.vprintf(format, ap);
fprintf(stdmsg, "%s", tmp.toChars());
#else
vfprintf(stdmsg, format, ap);
#endif
fprintf(stdmsg, "\n");
fflush(stdmsg);
if (global.errors >= 20) // moderate blizzard of cascading messages
fatal();
//halt();
}
else
{
global.gaggedErrors++;
}
global.errors++;
}
// Doesn't increase error count, doesn't print "Error:".
void verrorSupplemental(Loc loc, const char *format, va_list ap)
{
if (!global.gag)
{
fprintf(stdmsg, "%s: ", loc.toChars());
#if _MSC_VER
// MS doesn't recognize %zu format
OutBuffer tmp;
tmp.vprintf(format, ap);
fprintf(stdmsg, "%s", tmp.toChars());
#else
vfprintf(stdmsg, format, ap);
#endif
fprintf(stdmsg, "\n");
fflush(stdmsg);
}
}
void vwarning(Loc loc, const char *format, va_list ap)
{
if (global.params.warnings && !global.gag)
{
char *p = loc.toChars();
if (*p)
fprintf(stdmsg, "%s: ", p);
mem.free(p);
fprintf(stdmsg, "Warning: ");
#if _MSC_VER
// MS doesn't recognize %zu format
OutBuffer tmp;
tmp.vprintf(format, ap);
fprintf(stdmsg, "%s", tmp.toChars());
#else
vfprintf(stdmsg, format, ap);
#endif
fprintf(stdmsg, "\n");
fflush(stdmsg);
//halt();
if (global.params.warnings == 1)
global.warnings++; // warnings don't count if gagged
}
}
/***************************************
* Call this after printing out fatal error messages to clean up and exit
* the compiler.
*/
void fatal()
{
#if 0
halt();
#endif
exit(EXIT_FAILURE);
}
/**************************************
* Try to stop forgetting to remove the breakpoints from
* release builds.
*/
void halt()
{
#ifdef DEBUG
*(volatile char*)0=0;
#endif
}
/***********************************
* Parse and append contents of environment variable envvar
* to argc and argv[].
* The string is separated into arguments, processing \ and ".
*/
void getenv_setargv(const char *envvar, int *pargc, char** *pargv)
{
char *p;
int instring;
int slash;
char c;
char *env = getenv(envvar);
if (!env)
return;
env = mem.strdup(env); // create our own writable copy
int argc = *pargc;
Strings *argv = new Strings();
argv->setDim(argc);
size_t argc_left = 0;
for (size_t i = 0; i < argc; i++) {
if (!strcmp((*pargv)[i], "-run") || !strcmp((*pargv)[i], "--run")) {
// HACK: set flag to indicate we saw '-run' here
global.params.run = true;
// Don't eat -run yet so the program arguments don't get changed
argc_left = argc - i;
argc = i;
*pargv = &(*pargv)[i];
argv->setDim(i);
break;
} else {
argv->data[i] = (void *)(*pargv)[i];
}
}
// HACK to stop required values from command line being drawn from DFLAGS
argv->push((char*)"");
argc++;
size_t j = 1; // leave argv[0] alone
while (1)
{
int wildcard = 1; // do wildcard expansion
switch (*env)
{
case ' ':
case '\t':
env++;
break;
case 0:
goto Ldone;
case '"':
wildcard = 0;
default:
argv->push(env); // append
//argv->insert(j, env); // insert at position j
j++;
argc++;
p = env;
slash = 0;
instring = 0;
c = 0;
while (1)
{
c = *env++;
switch (c)
{
case '"':
p -= (slash >> 1);
if (slash & 1)
{ p--;
goto Laddc;
}
instring ^= 1;
slash = 0;
continue;
case ' ':
case '\t':
if (instring)
goto Laddc;
*p = 0;
//if (wildcard)
//wildcardexpand(); // not implemented
break;
case '\\':
slash++;
*p++ = c;
continue;
case 0:
*p = 0;
//if (wildcard)
//wildcardexpand(); // not implemented
goto Ldone;
default:
Laddc:
slash = 0;
*p++ = c;
continue;
}
break;
}
}
}
Ldone:
assert(argc == argv->dim);
argv->reserve(argc_left);
for (int i = 0; i < argc_left; i++)
argv->data[argc++] = (void *)(*pargv)[i];
*pargc = argc;
*pargv = argv->tdata();
}

View File

@@ -1,528 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_MARS_H
#define DMD_MARS_H
#ifdef __DMC__
#pragma once
#endif
/*
It is very important to use version control macros correctly - the
idea is that host and target are independent. If these are done
correctly, cross compilers can be built.
The host compiler and host operating system are also different,
and are predefined by the host compiler. The ones used in
dmd are:
Macros defined by the compiler, not the code:
Compiler:
__DMC__ Digital Mars compiler
_MSC_VER Microsoft compiler
__GNUC__ Gnu compiler
__clang__ Clang compiler
Host operating system:
_WIN32 Microsoft NT, Windows 95, Windows 98, Win32s,
Windows 2000, Win XP, Vista
_WIN64 Windows for AMD64
linux Linux
__APPLE__ Mac OSX
__FreeBSD__ FreeBSD
__OpenBSD__ OpenBSD
__sun&&__SVR4 Solaris, OpenSolaris (yes, both macros are necessary)
For the target systems, there are the target operating system and
the target object file format:
Target operating system:
TARGET_WINDOS Covers 32 bit windows and 64 bit windows
TARGET_LINUX Covers 32 and 64 bit linux
TARGET_OSX Covers 32 and 64 bit Mac OSX
TARGET_FREEBSD Covers 32 and 64 bit FreeBSD
TARGET_OPENBSD Covers 32 and 64 bit OpenBSD
TARGET_SOLARIS Covers 32 and 64 bit Solaris
TARGET_NET Covers .Net
It is expected that the compiler for each platform will be able
to generate 32 and 64 bit code from the same compiler binary.
Target object module format:
OMFOBJ Intel Object Module Format, used on Windows
ELFOBJ Elf Object Module Format, used on linux, FreeBSD, OpenBSD and Solaris
MACHOBJ Mach-O Object Module Format, used on Mac OSX
There are currently no macros for byte endianness order.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdarg.h>
#if IN_LLVM
#include "llvm/ADT/Triple.h"
#endif
#ifdef __DMC__
#ifdef DEBUG
#undef assert
#define assert(e) (static_cast<void>((e) || (printf("assert %s(%d) %s\n", __FILE__, __LINE__, #e), halt())))
#endif
#endif
#ifndef IS_PRINTF
# ifdef __GNUC__
# define IS_PRINTF(FMTARG) __attribute__((__format__ (__printf__, (FMTARG), (FMTARG)+1) ))
# else
# define IS_PRINTF(FMTARG)
# endif
#endif
#ifndef IS_VPRINTF
# ifdef __GNUC__
# define IS_VPRINTF(FMTARG) __attribute__((__format__ (__printf__, (FMTARG), 0) ))
# else
# define IS_VPRINTF(FMTARG)
# endif
#endif
#ifdef IN_GCC
/* Changes for the GDC compiler by David Friedman */
#endif
#define DMDV2 0 // Version 2.0 features
#define STRUCTTHISREF DMDV2 // if 'this' for struct is a reference, not a pointer
#define SNAN_DEFAULT_INIT DMDV2 // if floats are default initialized to signalling NaN
#define SARRAYVALUE DMDV2 // static arrays are value types
#define MODULEINFO_IS_STRUCT DMDV2 // if ModuleInfo is a struct rather than a class
// Set if C++ mangling is done by the front end
#define CPP_MANGLE (DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS))
/* Other targets are TARGET_LINUX, TARGET_OSX, TARGET_FREEBSD, TARGET_OPENBSD and
* TARGET_SOLARIS, which are
* set on the command line via the compiler makefile.
*/
#if _WIN32
#ifndef TARGET_WINDOS
#define TARGET_WINDOS 1 // Windows dmd generates Windows targets
#endif
#ifndef OMFOBJ
#define OMFOBJ TARGET_WINDOS
#endif
#endif
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
#ifndef ELFOBJ
#define ELFOBJ 1
#endif
#endif
#if TARGET_OSX
#ifndef MACHOBJ
#define MACHOBJ 1
#endif
#endif
struct OutBuffer;
#if IN_LLVM
enum OUTPUTFLAG
{
OUTPUTFLAGno,
OUTPUTFLAGdefault, // for the .o default
OUTPUTFLAGset // for -output
};
enum OS
{
OSinvalid = llvm::Triple::UnknownOS,
OSLinux = llvm::Triple::Linux,
OSHaiku = llvm::Triple::Haiku,
OSWindows = llvm::Triple::Win32,
OSMacOSX = llvm::Triple::MacOSX,
OSFreeBSD = llvm::Triple::FreeBSD,
OSSolaris = llvm::Triple::Solaris,
};
#endif
typedef unsigned char ubyte;
// Can't include arraytypes.h here, need to declare these directly.
template <typename TYPE> struct ArrayBase;
//typedef ArrayBase<struct Identifier> Identifiers;
typedef ArrayBase<char> Strings;
// Put command line switches in here
struct Param
{
bool obj; // write object file
bool link; // perform link
bool verbose; // verbose compile
ubyte symdebug; // insert debug symbolic information
#if !IN_LLVM
// LDC uses a different mechanism
bool optimize; // run optimizer
char optimizeLevel; // optimization level
#endif
char vtls; // identify thread local variables
// KN Start merge conflict
bool is64bit; // generate 64 bit code
bool useDeprecated; // allow use of deprecated features
bool useAssert; // generate runtime code for assert()'s
bool useInvariants; // generate class invariant checks
bool useIn; // generate precondition checks
bool useOut; // generate postcondition checks
bool useArrayBounds; // generate array bounds checks
bool useSwitchError; // check for switches without a default
bool useUnitTests; // generate unittest code
bool useInline; // inline expand functions
ubyte warnings; // enable warnings
// KN End merge conflict
#if 0
char symdebug; // insert debug symbolic information
char alwaysframe; // always emit standard stack frame
char optimize; // run optimizer
char map; // generate linker .map file
char cpu; // target CPU
char is64bit; // generate 64 bit code
char isLinux; // generate code for linux
char isOSX; // generate code for Mac OSX
char isWindows; // generate code for Windows
char isFreeBSD; // generate code for FreeBSD
char isOPenBSD; // generate code for OpenBSD
char isSolaris; // generate code for Solaris
char scheduler; // which scheduler to use
char useDeprecated; // allow use of deprecated features
char useAssert; // generate runtime code for assert()'s
char useInvariants; // generate class invariant checks
char useIn; // generate precondition checks
char useOut; // generate postcondition checks
char useArrayBounds; // 0: no array bounds checks
// 1: array bounds checks for safe functions only
// 2: array bounds checks for all functions
char noboundscheck; // no array bounds checking at all
char useSwitchError; // check for switches without a default
char useUnitTests; // generate unittest code
char useInline; // inline expand functions
char release; // build release version
char preservePaths; // !=0 means don't strip path from source file
char warnings; // 0: enable warnings
#endif
// 1: warnings as errors
// 2: informational warnings (no errors)
ubyte Dversion; // D version number
bool ignoreUnsupportedPragmas; // rather than error on them
char safe; // enforce safe memory model
char *argv0; // program name
Strings *imppath; // array of char*'s of where to look for import modules
Strings *fileImppath; // array of char*'s of where to look for file import modules
char *objdir; // .obj/.lib file output directory
char *objname; // .obj file output name
bool doDocComments; // process embedded documentation comments
char *docdir; // write documentation file to docdir directory
char *docname; // write documentation file to docname
Strings *ddocfiles; // macro include files for Ddoc
bool doHdrGeneration; // process embedded documentation comments
char *hdrdir; // write 'header' file to docdir directory
char *hdrname; // write 'header' file to docname
bool doXGeneration; // write JSON file
char *xfilename; // write JSON file to xfilename
unsigned debuglevel; // debug level
Strings *debugids; // debug identifiers
unsigned versionlevel; // version level
Strings *versionids; // version identifiers
bool dump_source;
Strings *defaultlibnames; // default libraries for non-debug builds
Strings *debuglibnames; // default libraries for debug builds
char *moduleDepsFile; // filename for deps output
OutBuffer *moduleDeps; // contents to be written to deps file
// Hidden debug switches
bool debuga;
bool debugb;
bool debugc;
bool debugf;
bool debugr;
bool debugw;
bool debugx;
bool debugy;
bool run; // run resulting executable
// Linker stuff
Strings *objfiles;
Strings *linkswitches;
Strings *libfiles;
char *deffile;
char *resfile;
char *exefile;
char *mapfile;
#if IN_LLVM
// LDC stuff
OUTPUTFLAG output_ll;
OUTPUTFLAG output_bc;
OUTPUTFLAG output_s;
OUTPUTFLAG output_o;
bool useInlineAsm;
bool verbose_cg;
bool useAvailableExternally;
// target stuff
llvm::Triple targetTriple;
// Codegen cl options
bool singleObj;
bool disableRedZone;
#endif
};
typedef unsigned structalign_t;
#define STRUCTALIGN_DEFAULT ~0 // magic value means "match whatever the underlying C compiler does"
// other values are all powers of 2
struct Global
{
const char *mars_ext;
const char *sym_ext;
const char *obj_ext;
#if IN_LLVM
const char *obj_ext_alt;
const char *ll_ext;
const char *bc_ext;
const char *s_ext;
#endif
const char *lib_ext;
const char *dll_ext;
const char *doc_ext; // for Ddoc generated files
const char *ddoc_ext; // for Ddoc macro include files
const char *hdr_ext; // for D 'header' import files
const char *json_ext; // for JSON files
const char *map_ext; // for .map files
const char *copyright;
const char *written;
Strings *path; // Array of char*'s which form the import lookup path
Strings *filePath; // Array of char*'s which form the file import lookup path
structalign_t structalign; // default alignment for struct fields
const char *version;
char *ldc_version;
char *llvm_version;
Param params;
unsigned errors; // number of errors reported so far
unsigned warnings; // number of warnings reported so far
unsigned gag; // !=0 means gag reporting of errors & warnings
unsigned gaggedErrors; // number of errors reported while gagged
/* Gagging can either be speculative (is(typeof()), etc)
* or because of forward references
*/
unsigned speculativeGag; // == gag means gagging is for is(typeof);
bool isSpeculativeGagging();
// Start gagging. Return the current number of gagged errors
unsigned startGagging();
/* End gagging, restoring the old gagged state.
* Return true if errors occured while gagged.
*/
bool endGagging(unsigned oldGagged);
Global();
};
extern Global global;
/* Set if Windows Structured Exception Handling C extensions are supported.
* Apparently, VC has dropped support for these?
*/
#define WINDOWS_SEH (_WIN32 && __DMC__)
#include "longdouble.h"
#ifdef __DMC__
#include <complex.h>
typedef _Complex long double complex_t;
#else
#ifndef IN_GCC
#include "complex_t.h"
#endif
#ifdef __APPLE__
//#include "complex.h"//This causes problems with include the c++ <complex> and not the C "complex.h"
#endif
#endif
// Be careful not to care about sign when using dinteger_t
//typedef uint64_t integer_t;
typedef uint64_t dinteger_t; // use this instead of integer_t to
// avoid conflicts with system #include's
// Signed and unsigned variants
typedef int64_t sinteger_t;
typedef uint64_t uinteger_t;
typedef int8_t d_int8;
typedef uint8_t d_uns8;
typedef int16_t d_int16;
typedef uint16_t d_uns16;
typedef int32_t d_int32;
typedef uint32_t d_uns32;
typedef int64_t d_int64;
typedef uint64_t d_uns64;
typedef float d_float32;
typedef double d_float64;
typedef longdouble d_float80;
typedef d_uns8 d_char;
typedef d_uns16 d_wchar;
typedef d_uns32 d_dchar;
#ifdef IN_GCC
#include "d-gcc-real.h"
#else
typedef longdouble real_t;
#endif
// Modify OutBuffer::writewchar to write the correct size of wchar
#if _WIN32
#define writewchar writeword
#else
// This needs a configuration test...
#define writewchar write4
#endif
#ifdef IN_GCC
#include "d-gcc-complex_t.h"
#endif
struct Module;
//typedef unsigned Loc; // file location
struct Loc
{
const char *filename;
unsigned linnum;
Loc()
{
linnum = 0;
filename = NULL;
}
Loc(int x)
{
linnum = x;
filename = NULL;
}
Loc(Module *mod, unsigned linnum);
char *toChars() const;
bool equals(const Loc& loc);
};
#ifndef GCC_SAFE_DMD
#define TRUE 1
#define FALSE 0
#endif
#define INTERFACE_OFFSET 0 // if 1, put classinfo as first entry
// in interface vtbl[]'s
#define INTERFACE_VIRTUAL 0 // 1 means if an interface appears
// in the inheritance graph multiple
// times, only one is used
enum LINK
{
LINKdefault,
LINKd,
LINKc,
LINKcpp,
LINKwindows,
LINKpascal,
#if IN_LLVM
LINKintrinsic,
#endif
};
enum DYNCAST
{
DYNCAST_OBJECT,
DYNCAST_EXPRESSION,
DYNCAST_DSYMBOL,
DYNCAST_TYPE,
DYNCAST_IDENTIFIER,
DYNCAST_TUPLE,
};
enum MATCH
{
MATCHnomatch, // no match
MATCHconvert, // match with conversions
#if DMDV2
MATCHconst, // match with conversion to const
#endif
MATCHexact // exact match
};
typedef uint64_t StorageClass;
void warning(Loc loc, const char *format, ...) IS_PRINTF(2);
void error(Loc loc, const char *format, ...) IS_PRINTF(2);
void errorSupplemental(Loc loc, const char *format, ...);
void verror(Loc loc, const char *format, va_list ap, const char *p1 = NULL, const char *p2 = NULL);
void vwarning(Loc loc, const char *format, va_list);
void verrorSupplemental(Loc loc, const char *format, va_list);
#if defined(__GNUC__) || defined(__clang__)
__attribute__((noreturn))
#endif
void fatal();
void err_nomem();
#if IN_LLVM
void error(const char *format, ...) IS_PRINTF(1);
#else
void inifile(char *argv0, const char *inifile);
#endif
void halt();
const char *importHint(const char *s);
/*** Where to send error messages ***/
#if IN_GCC || IN_LLVM
#define stdmsg stderr
#else
#define stdmsg stderr
#endif
#endif /* DMD_MARS_H */

269
dmd/mem.c
View File

@@ -1,269 +0,0 @@
/* Copyright (c) 2000 Digital Mars */
/* All Rights Reserved */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "rmem.h"
#if USE_BOEHM_GC
// I needed to perfix the dir after upgrading to gc 7.0
#include "gc/gc.h"
#endif
/* This implementation of the storage allocator uses the standard C allocation package.
*/
Mem mem;
#if USE_BOEHM_GC
static bool gc_was_init = false;
void Mem::init()
{
GC_init();
gc_was_init = true;
}
char *Mem::strdup(const char *s)
{
char *p;
if (s)
{
p = GC_strdup(s);
if (p)
return p;
error();
}
return NULL;
}
void *Mem::malloc(size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
p = GC_malloc(size);
if (!p)
error();
}
return p;
}
void *Mem::calloc(size_t size, size_t n)
{ void *p;
if (!size || !n)
p = NULL;
else
{
p = GC_malloc(size * n);
if (!p)
error();
memset(p, 0, size * n);
}
return p;
}
void *Mem::realloc(void *p, size_t size)
{
if (!size)
{ if (p)
{ GC_free(p);
p = NULL;
}
}
else if (!p)
{
p = GC_malloc(size);
if (!p)
error();
}
else
{
p = GC_realloc(p, size);
if (!p)
error();
}
return p;
}
void Mem::free(void *p)
{
if (p)
GC_free(p);
}
void *Mem::mallocdup(void *o, size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
p = GC_malloc(size);
if (!p)
error();
else
memcpy(p,o,size);
}
return p;
}
void Mem::error()
{
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
}
void Mem::fullcollect()
{
GC_gcollect();
}
void Mem::mark(void *pointer)
{
(void) pointer; // necessary for VC /W4
}
/* =================================================== */
void * operator new(size_t m_size)
{
// without this we segfault with gc 7.0
if (!gc_was_init) {
mem.init();
}
void *p = GC_malloc(m_size);
if (p)
return p;
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
return p;
}
void operator delete(void *p)
{
GC_free(p);
}
#elif !USE_BOEHM_GC
void Mem::init()
{
}
char *Mem::strdup(const char *s)
{
char *p;
if (s)
{
p = ::strdup(s);
if (p)
return p;
error();
}
return NULL;
}
void *Mem::malloc(size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
p = ::malloc(size);
if (!p)
error();
}
return p;
}
void *Mem::calloc(size_t size, size_t n)
{ void *p;
if (!size || !n)
p = NULL;
else
{
p = ::malloc(size * n);
if (!p)
error();
memset(p, 0, size * n);
}
return p;
}
void *Mem::realloc(void *p, size_t size)
{
if (!size)
{ if (p)
{ ::free(p);
p = NULL;
}
}
else if (!p)
{
p = ::malloc(size);
if (!p)
error();
}
else
{
p = ::realloc(p, size);
if (!p)
error();
}
return p;
}
void Mem::free(void *p)
{
if (p)
::free(p);
}
void *Mem::mallocdup(void *o, size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
p = ::malloc(size);
if (!p)
error();
else
memcpy(p,o,size);
}
return p;
}
void Mem::error()
{
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
}
void Mem::fullcollect()
{
}
void Mem::mark(void *pointer)
{
}
void Mem::setStackBottom(void */*stackbottom*/)
{
}
#endif // USE_BOEHM_GC

View File

@@ -1,51 +0,0 @@
// Copyright (C) 2000-2001 by Chromium Communications
// All Rights Reserved
#ifndef ROOT_MEM_H
#define ROOT_MEM_H
#include <stddef.h> // for size_t
typedef void (*FINALIZERPROC)(void* pObj, void* pClientData);
struct GC; // thread specific allocator
struct Mem
{
GC *gc; // pointer to our thread specific allocator
Mem() { gc = NULL; }
void init();
// Derive from Mem to get these storage allocators instead of global new/delete
void * operator new(size_t m_size);
void * operator new(size_t m_size, Mem *mem);
void * operator new(size_t m_size, GC *gc);
void operator delete(void *p);
void * operator new[](size_t m_size);
void operator delete[](void *p);
char *strdup(const char *s);
void *malloc(size_t size);
void *malloc_uncollectable(size_t size);
void *calloc(size_t size, size_t n);
void *realloc(void *p, size_t size);
void free(void *p);
void free_uncollectable(void *p);
void *mallocdup(void *o, size_t size);
void error();
void check(void *p); // validate pointer
void fullcollect(); // do full garbage collection
void fullcollectNoStack(); // do full garbage collection, no scan stack
void mark(void *pointer);
void addroots(char* pStart, char* pEnd);
void removeroots(char* pStart);
void setFinalizer(void* pObj, FINALIZERPROC pFn, void* pClientData);
void setStackBottom(void *bottom);
GC *getThreadGC(); // get apartment allocator for this thread
};
extern Mem mem;
#endif /* ROOT_MEM_H */

File diff suppressed because it is too large Load Diff

View File

@@ -1,225 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_MODULE_H
#define DMD_MODULE_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "dsymbol.h"
struct ModuleInfoDeclaration;
struct ClassDeclaration;
struct ModuleDeclaration;
struct Macro;
struct Escape;
struct VarDeclaration;
struct Library;
// Back end
#if IN_LLVM
class Ir;
struct DValue;
typedef DValue elem;
namespace llvm {
class LLVMContext;
class Module;
class GlobalVariable;
class StructType;
}
#else
#ifdef IN_GCC
union tree_node; typedef union tree_node elem;
#else
struct elem;
#endif
#endif
struct Package : ScopeDsymbol
{
Package(Identifier *ident);
const char *kind();
static DsymbolTable *resolve(Identifiers *packages, Dsymbol **pparent, Package **ppkg);
Package *isPackage() { return this; }
virtual void semantic(Scope *sc) { }
};
struct Module : Package
{
static Module *rootModule;
static DsymbolTable *modules; // symbol table of all modules
static Modules amodules; // array of all modules
static Dsymbols deferred; // deferred Dsymbol's needing semantic() run on them
static unsigned dprogress; // progress resolving the deferred list
static void init();
static ClassDeclaration *moduleinfo;
const char *arg; // original argument name
ModuleDeclaration *md; // if !NULL, the contents of the ModuleDeclaration declaration
File *srcfile; // input source file
File *objfile; // output object file
File *docfile; // output doc file
File *hdrfile; // output hdr file
unsigned errors; // if any errors in file
unsigned numlines; // number of lines in source file
int isHtml; // if it is an HTML file
int isDocFile; // if it is a documentation input file, not D source
int needmoduleinfo;
#ifdef IN_GCC
int strictlyneedmoduleinfo;
#endif
int selfimports; // 0: don't know, 1: does not, 2: does
int selfImports(); // returns !=0 if module imports itself
int insearch;
Identifier *searchCacheIdent;
Dsymbol *searchCacheSymbol; // cached value of search
int searchCacheFlags; // cached flags
int semanticstarted; // has semantic() been started?
int semanticRun; // has semantic() been done?
int root; // != 0 if this is a 'root' module,
// i.e. a module that will be taken all the
// way to an object file
Module *importedFrom; // module from command line we're imported from,
// i.e. a module that will be taken all the
// way to an object file
Dsymbols *decldefs; // top level declarations for this Module
Modules aimports; // all imported modules
ModuleInfoDeclaration *vmoduleinfo;
unsigned debuglevel; // debug level
Strings *debugids; // debug identifiers
Strings *debugidsNot; // forward referenced debug identifiers
unsigned versionlevel; // version level
Strings *versionids; // version identifiers
Strings *versionidsNot; // forward referenced version identifiers
Macro *macrotable; // document comment macros
struct Escape *escapetable; // document comment escapes
int doDocComment; // enable generating doc comments for this module
int doHdrGen; // enable generating header file for this module
bool safe; // TRUE if module is marked as 'safe'
Module(char *arg, Identifier *ident, int doDocComment, int doHdrGen);
~Module();
static Module *load(Loc loc, Identifiers *packages, Identifier *ident);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toJsonBuffer(OutBuffer *buf);
const char *kind();
bool read(Loc loc); // read file, returns 'true' if succeed, 'false' otherwise.
#if IN_LLVM
void parse(bool gen_docs = false); // syntactic parse
#else
void setDocfile(); // set docfile member
void parse(); // syntactic parse
#endif
void importAll(Scope *sc);
void semantic(); // semantic analysis
void semantic2(); // pass 2 semantic analysis
void semantic3(); // pass 3 semantic analysis
void inlineScan(); // scan for functions to inline
void setHdrfile(); // set hdrfile member
void genhdrfile(); // generate D import file
void genobjfile(int multiobj);
// void gensymfile();
void gendocfile();
int needModuleInfo();
Dsymbol *search(Loc loc, Identifier *ident, int flags);
Dsymbol *symtabInsert(Dsymbol *s);
void deleteObjFile();
void addDeferredSemantic(Dsymbol *s);
static void runDeferredSemantic();
static void clearCache();
int imports(Module *m);
// Back end
#if IN_DMD
int doppelganger; // sub-module
Symbol *cov; // private uint[] __coverage;
unsigned *covb; // bit array of valid code line numbers
Symbol *sictor; // module order independent constructor
Symbol *sctor; // module constructor
Symbol *sdtor; // module destructor
Symbol *stest; // module unit test
Symbol *sfilename; // symbol for filename
Symbol *massert; // module assert function
Symbol *toModuleAssert(); // get module assert function
Symbol *munittest; // module unittest failure function
Symbol *toModuleUnittest(); // get module unittest failure function
Symbol *marray; // module array bounds function
Symbol *toModuleArray(); // get module array bounds function
static Symbol *gencritsec();
elem *toEfilename();
elem *toEmodulename();
Symbol *toSymbol();
#endif
void genmoduleinfo();
#if IN_LLVM
// LDC
llvm::Module* genLLVMModule(llvm::LLVMContext& context, Ir* sir);
void buildTargetFiles(bool singleObj);
File* buildFilePath(const char* forcename, const char* path, const char* ext);
Module *isModule() { return this; }
llvm::GlobalVariable* moduleInfoSymbol();
bool llvmForceLogging;
llvm::GlobalVariable* moduleInfoVar;
llvm::StructType* moduleInfoType;
// array ops emitted in this module already
StringTable arrayfuncs;
bool isRoot;
#endif
};
struct ModuleDeclaration
{
Identifier *id;
Identifiers *packages; // array of Identifier's representing packages
bool safe;
ModuleDeclaration(Identifiers *packages, Identifier *id);
char *toChars();
};
#endif /* DMD_MODULE_H */

File diff suppressed because it is too large Load Diff

View File

@@ -1,861 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_MTYPE_H
#define DMD_MTYPE_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "stringtable.h"
#include "arraytypes.h"
#include "expression.h"
#if IN_LLVM
#include "../ir/irfuncty.h"
namespace llvm { class Type; }
class Ir;
class IrType;
#endif
struct Scope;
struct Identifier;
struct Expression;
struct StructDeclaration;
struct ClassDeclaration;
struct VarDeclaration;
struct EnumDeclaration;
struct TypedefDeclaration;
struct TypeInfoDeclaration;
struct Dsymbol;
struct TemplateInstance;
struct CppMangleState;
enum LINK;
struct TypeBasic;
struct HdrGenState;
struct Parameter;
// Back end
#if IN_GCC
union tree_node; typedef union tree_node TYPE;
typedef TYPE type;
#else
typedef struct TYPE type;
#endif
struct Symbol;
struct TypeTuple;
enum TY
{
Tarray, // dynamic array
Tsarray, // static array
Taarray, // associative array
Tpointer,
Treference,
Tfunction,
Tident,
Tclass,
Tstruct,
Tenum,
Ttypedef,
Tdelegate,
Tnone,
Tvoid,
Tint8,
Tuns8,
Tint16,
Tuns16,
Tint32,
Tuns32,
Tint64,
Tuns64,
Tfloat32,
Tfloat64,
Tfloat80,
Timaginary32,
Timaginary64,
Timaginary80,
Tcomplex32,
Tcomplex64,
Tcomplex80,
Tbit,
Tbool,
Tchar,
Twchar,
Tdchar,
Terror,
Tinstance,
Ttypeof,
Ttuple,
Tslice,
Treturn,
TMAX,
Tnull
};
#define Tascii Tchar
extern int Tsize_t;
extern int Tptrdiff_t;
struct Type : Object
{
TY ty;
unsigned char mod; // modifiers MODxxxx
/* pick this order of numbers so switch statements work better
*/
#define MODconst 1 // type is const
#define MODimmutable 4 // type is immutable
#define MODshared 2 // type is shared
#define MODwild 8 // type is wild
#define MODmutable 0x10 // type is mutable (only used in wildcard matching)
char *deco;
Type *pto; // merged pointer to this type
Type *rto; // reference to this type
Type *arrayof; // array of this type
TypeInfoDeclaration *vtinfo; // TypeInfo object for this Type
#if IN_DMD
type *ctype; // for back end
#endif
#define tvoid basic[Tvoid]
#define tint8 basic[Tint8]
#define tuns8 basic[Tuns8]
#define tint16 basic[Tint16]
#define tuns16 basic[Tuns16]
#define tint32 basic[Tint32]
#define tuns32 basic[Tuns32]
#define tint64 basic[Tint64]
#define tuns64 basic[Tuns64]
#define tfloat32 basic[Tfloat32]
#define tfloat64 basic[Tfloat64]
#define tfloat80 basic[Tfloat80]
#define timaginary32 basic[Timaginary32]
#define timaginary64 basic[Timaginary64]
#define timaginary80 basic[Timaginary80]
#define tcomplex32 basic[Tcomplex32]
#define tcomplex64 basic[Tcomplex64]
#define tcomplex80 basic[Tcomplex80]
#define tbit basic[Tbit]
#define tbool basic[Tbool]
#define tchar basic[Tchar]
#define twchar basic[Twchar]
#define tdchar basic[Tdchar]
// Some special types
#define tshiftcnt tint32 // right side of shift expression
// #define tboolean tint32 // result of boolean expression
#define tboolean tbool // result of boolean expression
#define tindex tsize_t // array/ptr index
static Type *tvoidptr; // void*
static Type *tstring; // immutable(char)[]
#define terror basic[Terror] // for error recovery
#define tsize_t basic[Tsize_t] // matches size_t alias
#define tptrdiff_t basic[Tptrdiff_t] // matches ptrdiff_t alias
#define thash_t tsize_t // matches hash_t alias
static ClassDeclaration *typeinfo;
static ClassDeclaration *typeinfoclass;
static ClassDeclaration *typeinfointerface;
static ClassDeclaration *typeinfostruct;
static ClassDeclaration *typeinfotypedef;
static ClassDeclaration *typeinfopointer;
static ClassDeclaration *typeinfoarray;
static ClassDeclaration *typeinfostaticarray;
static ClassDeclaration *typeinfoassociativearray;
static ClassDeclaration *typeinfoenum;
static ClassDeclaration *typeinfofunction;
static ClassDeclaration *typeinfodelegate;
static ClassDeclaration *typeinfotypelist;
static Type *basic[TMAX];
static unsigned char mangleChar[TMAX];
static StringTable stringtable;
#if IN_LLVM
static StringTable deco_stringtable;
#endif
// These tables are for implicit conversion of binary ops;
// the indices are the type of operand one, followed by operand two.
static unsigned char impcnvResult[TMAX][TMAX];
static unsigned char impcnvType1[TMAX][TMAX];
static unsigned char impcnvType2[TMAX][TMAX];
// If !=0, give warning on implicit conversion
static unsigned char impcnvWarn[TMAX][TMAX];
Type(TY ty, Type *next);
virtual Type *syntaxCopy();
int equals(Object *o);
int dyncast() { return DYNCAST_TYPE; } // kludge for template.isType()
int covariant(Type *t);
char *toChars();
static char needThisPrefix();
#if IN_LLVM
static void init(Ir*);
#else
static void init();
#endif
d_uns64 size();
virtual d_uns64 size(Loc loc);
virtual unsigned alignsize();
virtual Type *semantic(Loc loc, Scope *sc);
Type *trySemantic(Loc loc, Scope *sc);
// append the mangleof or a string uniquely identifying this type to buf
virtual void toDecoBuffer(OutBuffer *buf, bool mangle);
Type *merge();
Type *merge2();
virtual void toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs);
virtual void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
void toCBuffer3(OutBuffer *buf, HdrGenState *hgs, int mod);
virtual int isbit();
virtual int isintegral();
virtual int isfloating(); // real, imaginary, or complex
virtual int isreal();
virtual int isimaginary();
virtual int iscomplex();
virtual int isscalar();
virtual int isunsigned();
virtual int isscope();
virtual int isString();
virtual int checkBoolean(); // if can be converted to boolean value
void checkDeprecated(Loc loc, Scope *sc);
Type *pointerTo();
Type *referenceTo();
Type *arrayOf();
virtual Dsymbol *toDsymbol(Scope *sc);
virtual Type *toBasetype();
virtual int isBaseOf(Type *t, int *poffset);
virtual MATCH implicitConvTo(Type *to);
virtual ClassDeclaration *isClassHandle();
virtual Expression *getProperty(Loc loc, Identifier *ident);
virtual Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
virtual structalign_t memalign(structalign_t salign);
virtual Expression *defaultInit(Loc loc = 0);
virtual Expression *defaultInitLiteral(Loc loc);
virtual Expression *voidInitLiteral(VarDeclaration *var);
virtual int isZeroInit(Loc loc = 0); // if initializer is 0
#if IN_DMD
virtual dt_t **toDt(dt_t **pdt);
#endif
Identifier *getTypeInfoIdent(int internal);
virtual MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
virtual void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
Expression *getInternalTypeInfo(Scope *sc);
Expression *getTypeInfo(Scope *sc);
virtual TypeInfoDeclaration *getTypeInfoDeclaration();
virtual int builtinTypeInfo();
virtual Type *reliesOnTident();
virtual Expression *toExpression();
virtual int hasPointers();
virtual TypeTuple *toArgTypes();
Type *next;
Type *nextOf() { return next; }
static void error(Loc loc, const char *format, ...) IS_PRINTF(2);
static void warning(Loc loc, const char *format, ...) IS_PRINTF(2);
#if IN_DMD
// For backend
virtual unsigned totym();
virtual type *toCtype();
virtual type *toCParamtype();
virtual Symbol *toSymbol();
#endif
// For eliminating dynamic_cast
virtual TypeBasic *isTypeBasic();
#if IN_LLVM
static Ir* sir;
IrType* irtype;
#endif
};
struct TypeError : Type
{
TypeError();
void toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs);
d_uns64 size(Loc loc);
Expression *getProperty(Loc loc, Identifier *ident);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
Expression *defaultInit(Loc loc);
Expression *defaultInitLiteral(Loc loc);
TypeTuple *toArgTypes();
};
#if DMDV2
struct TypeNext : Type
{
Type *next;
TypeNext(TY ty, Type *next);
void toDecoBuffer(OutBuffer *buf, int flag);
void checkDeprecated(Loc loc, Scope *sc);
Type *reliesOnTident();
int hasWild();
unsigned wildMatch(Type *targ);
Type *nextOf();
Type *makeConst();
Type *makeInvariant();
Type *makeShared();
Type *makeSharedConst();
Type *makeWild();
Type *makeSharedWild();
Type *makeMutable();
MATCH constConv(Type *to);
void transitive();
};
#endif
struct TypeBasic : Type
{
const char *dstring;
const char *cstring;
unsigned flags;
TypeBasic(TY ty);
Type *syntaxCopy();
d_uns64 size(Loc loc);
unsigned alignsize();
#if IN_LLVM
unsigned memalign(unsigned salign);
#endif
Expression *getProperty(Loc loc, Identifier *ident);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
char *toChars();
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
int isintegral();
int isbit();
int isfloating();
int isreal();
int isimaginary();
int iscomplex();
int isscalar();
int isunsigned();
MATCH implicitConvTo(Type *to);
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
int builtinTypeInfo();
TypeTuple *toArgTypes();
// For eliminating dynamic_cast
TypeBasic *isTypeBasic();
};
struct TypeArray : Type
{
TypeArray(TY ty, Type *next);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
};
// Static array, one with a fixed dimension
struct TypeSArray : TypeArray
{
Expression *dim;
TypeSArray(Type *t, Expression *dim);
Type *syntaxCopy();
d_uns64 size(Loc loc);
unsigned alignsize();
Type *semantic(Loc loc, Scope *sc);
void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
int isString();
int isZeroInit(Loc loc);
structalign_t memalign(structalign_t salign);
MATCH implicitConvTo(Type *to);
Expression *defaultInit(Loc loc);
Expression *defaultInitLiteral(Loc loc);
Expression *voidInitLiteral(VarDeclaration *var);
#if IN_DMD
dt_t **toDt(dt_t **pdt);
dt_t **toDtElem(dt_t **pdt, Expression *e);
#endif
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
TypeInfoDeclaration *getTypeInfoDeclaration();
Expression *toExpression();
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
type *toCtype();
type *toCParamtype();
#endif
};
// Dynamic array, no dimension
struct TypeDArray : TypeArray
{
TypeDArray(Type *t);
Type *syntaxCopy();
d_uns64 size(Loc loc);
unsigned alignsize();
Type *semantic(Loc loc, Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
int isString();
int isZeroInit(Loc loc);
int checkBoolean();
MATCH implicitConvTo(Type *to);
Expression *defaultInit(Loc loc);
int builtinTypeInfo();
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
type *toCtype();
#endif
};
struct TypeAArray : TypeArray
{
Type *index; // key type for type checking
Type *key; // actual key type
TypeAArray(Type *t, Type *index);
Type *syntaxCopy();
d_uns64 size(Loc loc);
Type *semantic(Loc loc, Scope *sc);
void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
Expression *defaultInit(Loc loc);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
int isZeroInit(Loc loc);
int checkBoolean();
TypeInfoDeclaration *getTypeInfoDeclaration();
Expression *toExpression();
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
// Back end
Symbol *aaGetSymbol(const char *func, int flags);
type *toCtype();
#endif
};
struct TypePointer : Type
{
TypePointer(Type *t);
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
d_uns64 size(Loc loc);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
MATCH implicitConvTo(Type *to);
int isscalar();
// LDC: pointers are unsigned
int isunsigned() { return TRUE; };
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
type *toCtype();
#endif
};
struct TypeReference : Type
{
TypeReference(Type *t);
Type *syntaxCopy();
d_uns64 size(Loc loc);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
};
enum RET
{
RETregs = 1, // returned in registers
RETstack = 2, // returned on stack
};
struct TypeFunction : Type
{
Parameters *parameters; // function parameters
int varargs; // 1: T t, ...) style for variable number of arguments
// if extern (C) then this is C style va_args
// if extern (D) then D style va_args
// 2: T t ...) style for variable number of arguments
// where the args are stored in a local, and a
// dynamic array is passed to the function
enum LINK linkage; // calling convention
int inuse;
TypeFunction(Parameters *parameters, Type *treturn, int varargs, enum LINK linkage);
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer(OutBuffer *buf, Identifier *ident, HdrGenState *hgs);
void toCBufferWithAttributes(OutBuffer *buf, Identifier *ident, HdrGenState* hgs, TypeFunction *attrs, TemplateDeclaration *td);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
TypeInfoDeclaration *getTypeInfoDeclaration();
Type *reliesOnTident();
int callMatch(Expressions *toargs);
#if IN_DMD
type *toCtype();
#endif
enum RET retStyle();
#if IN_DMD
unsigned totym();
#elif IN_LLVM
// LDC
IrFuncTy fty;
FuncDeclaration* funcdecl;
#endif
Expression *defaultInit(Loc loc);
};
struct TypeDelegate : Type
{
TypeDelegate(Type *t);
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
d_uns64 size(Loc loc);
unsigned alignsize();
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
int checkBoolean();
TypeInfoDeclaration *getTypeInfoDeclaration();
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
type *toCtype();
#endif
};
struct TypeQualified : Type
{
Loc loc;
Identifiers idents; // array of Identifier's representing ident.ident.ident etc.
TypeQualified(TY ty, Loc loc);
void syntaxCopyHelper(TypeQualified *t);
void addIdent(Identifier *ident);
void toCBuffer2Helper(OutBuffer *buf, HdrGenState *hgs);
d_uns64 size(Loc loc);
void resolveHelper(Loc loc, Scope *sc, Dsymbol *s, Dsymbol *scopesym,
Expression **pe, Type **pt, Dsymbol **ps);
};
struct TypeIdentifier : TypeQualified
{
Identifier *ident;
TypeIdentifier(Loc loc, Identifier *ident);
Type *syntaxCopy();
//char *toChars();
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
Dsymbol *toDsymbol(Scope *sc);
Type *semantic(Loc loc, Scope *sc);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
Type *reliesOnTident();
Expression *toExpression();
};
/* Similar to TypeIdentifier, but with a TemplateInstance as the root
*/
struct TypeInstance : TypeQualified
{
TemplateInstance *tempinst;
TypeInstance(Loc loc, TemplateInstance *tempinst);
Type *syntaxCopy();
//char *toChars();
//void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
Type *semantic(Loc loc, Scope *sc);
Dsymbol *toDsymbol(Scope *sc);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
};
struct TypeTypeof : TypeQualified
{
Expression *exp;
int inuse;
TypeTypeof(Loc loc, Expression *exp);
Type *syntaxCopy();
Dsymbol *toDsymbol(Scope *sc);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
void toDecoBuffer(OutBuffer *buf, bool mangle);
Type *semantic(Loc loc, Scope *sc);
d_uns64 size(Loc loc);
};
struct TypeStruct : Type
{
StructDeclaration *sym;
TypeStruct(StructDeclaration *sym);
d_uns64 size(Loc loc);
unsigned alignsize();
char *toChars();
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
Dsymbol *toDsymbol(Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
structalign_t memalign(structalign_t salign);
Expression *defaultInit(Loc loc);
Expression *defaultInitLiteral(Loc loc);
Expression *voidInitLiteral(VarDeclaration *var);
int isZeroInit(Loc loc);
int checkBoolean();
#if IN_DMD
dt_t **toDt(dt_t **pdt);
#endif
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
#if CPP_MANGLE
void toCppMangle(OutBuffer *buf, CppMangleState *cms);
#endif
#if IN_DMD
type *toCtype();
#elif IN_LLVM
// LDC
// cache the hasUnalignedFields check
// 0 = not checked, 1 = aligned, 2 = unaligned
int unaligned;
#endif
};
struct TypeEnum : Type
{
EnumDeclaration *sym;
TypeEnum(EnumDeclaration *sym);
Type *syntaxCopy();
d_uns64 size(Loc loc);
unsigned alignsize();
char *toChars();
Type *semantic(Loc loc, Scope *sc);
Dsymbol *toDsymbol(Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
Expression *getProperty(Loc loc, Identifier *ident);
int isintegral();
int isfloating();
int isscalar();
int isunsigned();
MATCH implicitConvTo(Type *to);
Type *toBasetype();
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
#if CPP_MANGLE
void toCppMangle(OutBuffer *buf, CppMangleState *cms);
#endif
#if IN_DMD
type *toCtype();
#endif
};
struct TypeTypedef : Type
{
TypedefDeclaration *sym;
TypeTypedef(TypedefDeclaration *sym);
Type *syntaxCopy();
d_uns64 size(Loc loc);
unsigned alignsize();
char *toChars();
Type *semantic(Loc loc, Scope *sc);
Dsymbol *toDsymbol(Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
Expression *getProperty(Loc loc, Identifier *ident);
int isbit();
int isintegral();
int isfloating();
int isreal();
int isimaginary();
int iscomplex();
int isscalar();
int isunsigned();
int checkBoolean();
Type *toBasetype();
MATCH implicitConvTo(Type *to);
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
#if IN_DMD
dt_t **toDt(dt_t **pdt);
#endif
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
#if IN_DMD
type *toCtype();
type *toCParamtype();
#endif
};
struct TypeClass : Type
{
ClassDeclaration *sym;
TypeClass(ClassDeclaration *sym);
d_uns64 size(Loc loc);
char *toChars();
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
Dsymbol *toDsymbol(Scope *sc);
void toDecoBuffer(OutBuffer *buf, bool mangle);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
ClassDeclaration *isClassHandle();
int isBaseOf(Type *t, int *poffset);
MATCH implicitConvTo(Type *to);
Expression *defaultInit(Loc loc);
int isZeroInit(Loc loc);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes);
int isscope();
int checkBoolean();
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
TypeTuple *toArgTypes();
int builtinTypeInfo();
#if DMDV2
Type *toHeadMutable();
MATCH constConv(Type *to);
#if CPP_MANGLE
void toCppMangle(OutBuffer *buf, CppMangleState *cms);
#endif
#endif
#if IN_DMD
type *toCtype();
Symbol *toSymbol();
#endif
};
struct TypeTuple : Type
{
Parameters *arguments; // types making up the tuple
TypeTuple(Parameters *arguments);
TypeTuple(Expressions *exps);
TypeTuple();
TypeTuple(Type *t1);
TypeTuple(Type *t1, Type *t2);
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
int equals(Object *o);
Type *reliesOnTident();
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
void toDecoBuffer(OutBuffer *buf, bool mangle);
Expression *getProperty(Loc loc, Identifier *ident);
TypeInfoDeclaration *getTypeInfoDeclaration();
};
struct TypeSlice : Type
{
Expression *lwr;
Expression *upr;
TypeSlice(Type *next, Expression *lwr, Expression *upr);
Type *syntaxCopy();
Type *semantic(Loc loc, Scope *sc);
void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
};
/**************************************************************/
//enum InOut { None, In, Out, InOut, Lazy };
struct Parameter : Object
{
//enum InOut inout;
StorageClass storageClass;
Type *type;
Identifier *ident;
Expression *defaultArg;
Parameter(StorageClass storageClass, Type *type, Identifier *ident, Expression *defaultArg);
Parameter *syntaxCopy();
Type *isLazyArray();
void toDecoBuffer(OutBuffer *buf, bool mangle);
static Parameters *arraySyntaxCopy(Parameters *args);
static char *argsTypesToChars(Parameters *args, int varargs);
static void argsCppMangle(OutBuffer *buf, CppMangleState *cms, Parameters *arguments, int varargs);
static void argsToCBuffer(OutBuffer *buf, HdrGenState *hgs, Parameters *arguments, int varargs);
static void argsToDecoBuffer(OutBuffer *buf, Parameters *arguments, bool mangle);
static int isTPL(Parameters *arguments);
static size_t dim(Parameters *arguments);
static Parameter *getNth(Parameters *arguments, size_t nth, size_t *pn = NULL);
typedef int (*ForeachDg)(void *ctx, size_t paramidx, Parameter *param, int flags);
static int foreach(Parameters *args, ForeachDg dg, void *ctx, size_t *pn=NULL, int flags = 0);
};
extern int PTRSIZE;
extern int REALSIZE;
extern int REALPAD;
extern int Tsize_t;
extern int Tptrdiff_t;
int arrayTypeCompatible(Loc loc, Type *t1, Type *t2);
#endif /* DMD_MTYPE_H */

View File

@@ -1,787 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h> // memset()
#if _MSC_VER
#include <complex>
#else
#include <complex>
#endif
#include "rmem.h"
//#include "port.h"
#include "mtype.h"
#include "init.h"
#include "expression.h"
#include "id.h"
#include "declaration.h"
#include "aggregate.h"
#include "template.h"
#include "scope.h"
static Expression *build_overload(Loc loc, Scope *sc, Expression *ethis, Expression *earg, Identifier *id);
static void inferApplyArgTypesX(Module* from, FuncDeclaration *fstart, Parameters *arguments);
static int inferApplyArgTypesY(TypeFunction *tf, Parameters *arguments);
static void templateResolve(Match *m, TemplateDeclaration *td, Scope *sc, Loc loc, Objects *targsi, Expression *ethis, Expressions *arguments);
/******************************** Expression **************************/
/***********************************
* Determine if operands of binary op can be reversed
* to fit operator overload.
*/
int Expression::isCommutative()
{
return FALSE; // default is no reverse
}
/***********************************
* Get Identifier for operator overload.
*/
Identifier *Expression::opId()
{
assert(0);
return NULL;
}
/***********************************
* Get Identifier for reverse operator overload,
* NULL if not supported for this operator.
*/
Identifier *Expression::opId_r()
{
return NULL;
}
/************************* Operators *****************************/
Identifier *UAddExp::opId() { return Id::uadd; }
Identifier *NegExp::opId() { return Id::neg; }
Identifier *ComExp::opId() { return Id::com; }
Identifier *CastExp::opId() { return Id::cast; }
Identifier *InExp::opId() { return Id::opIn; }
Identifier *InExp::opId_r() { return Id::opIn_r; }
Identifier *PostExp::opId() { return (op == TOKplusplus)
? Id::postinc
: Id::postdec; }
int AddExp::isCommutative() { return TRUE; }
Identifier *AddExp::opId() { return Id::add; }
Identifier *AddExp::opId_r() { return Id::add_r; }
Identifier *MinExp::opId() { return Id::sub; }
Identifier *MinExp::opId_r() { return Id::sub_r; }
int MulExp::isCommutative() { return TRUE; }
Identifier *MulExp::opId() { return Id::mul; }
Identifier *MulExp::opId_r() { return Id::mul_r; }
Identifier *DivExp::opId() { return Id::div; }
Identifier *DivExp::opId_r() { return Id::div_r; }
Identifier *ModExp::opId() { return Id::mod; }
Identifier *ModExp::opId_r() { return Id::mod_r; }
Identifier *ShlExp::opId() { return Id::shl; }
Identifier *ShlExp::opId_r() { return Id::shl_r; }
Identifier *ShrExp::opId() { return Id::shr; }
Identifier *ShrExp::opId_r() { return Id::shr_r; }
Identifier *UshrExp::opId() { return Id::ushr; }
Identifier *UshrExp::opId_r() { return Id::ushr_r; }
int AndExp::isCommutative() { return TRUE; }
Identifier *AndExp::opId() { return Id::iand; }
Identifier *AndExp::opId_r() { return Id::iand_r; }
int OrExp::isCommutative() { return TRUE; }
Identifier *OrExp::opId() { return Id::ior; }
Identifier *OrExp::opId_r() { return Id::ior_r; }
int XorExp::isCommutative() { return TRUE; }
Identifier *XorExp::opId() { return Id::ixor; }
Identifier *XorExp::opId_r() { return Id::ixor_r; }
Identifier *CatExp::opId() { return Id::cat; }
Identifier *CatExp::opId_r() { return Id::cat_r; }
Identifier * AssignExp::opId() { return Id::assign; }
Identifier * AddAssignExp::opId() { return Id::addass; }
Identifier * MinAssignExp::opId() { return Id::subass; }
Identifier * MulAssignExp::opId() { return Id::mulass; }
Identifier * DivAssignExp::opId() { return Id::divass; }
Identifier * ModAssignExp::opId() { return Id::modass; }
Identifier * AndAssignExp::opId() { return Id::andass; }
Identifier * OrAssignExp::opId() { return Id::orass; }
Identifier * XorAssignExp::opId() { return Id::xorass; }
Identifier * ShlAssignExp::opId() { return Id::shlass; }
Identifier * ShrAssignExp::opId() { return Id::shrass; }
Identifier *UshrAssignExp::opId() { return Id::ushrass; }
Identifier * CatAssignExp::opId() { return Id::catass; }
int EqualExp::isCommutative() { return TRUE; }
Identifier *EqualExp::opId() { return Id::eq; }
int CmpExp::isCommutative() { return TRUE; }
Identifier *CmpExp::opId() { return Id::cmp; }
Identifier *ArrayExp::opId() { return Id::index; }
/************************************
* Operator overload.
* Check for operator overload, if so, replace
* with function call.
* Return NULL if not an operator overload.
*/
Expression *UnaExp::op_overload(Scope *sc)
{
//printf("UnaExp::op_overload() (%s)\n", toChars());
AggregateDeclaration *ad;
Dsymbol *fd;
Type *t1 = e1->type->toBasetype();
if (t1->ty == Tclass)
{
ad = ((TypeClass *)t1)->sym;
goto L1;
}
else if (t1->ty == Tstruct)
{
ad = ((TypeStruct *)t1)->sym;
L1:
fd = search_function(ad, opId());
if (fd)
{
if (op == TOKarray)
{
/* Rewrite op e1[arguments] as:
* e1.fd(arguments)
*/
Expression *e = new DotIdExp(loc, e1, fd->ident);
ArrayExp *ae = (ArrayExp *)this;
e = new CallExp(loc, e, ae->arguments);
e = e->semantic(sc);
return e;
}
else
{
// Rewrite +e1 as e1.add()
return build_overload(loc, sc, e1, NULL, fd->ident);
}
}
#if DMDV2
// Didn't find it. Forward to aliasthis
if (ad->aliasthis)
{
/* Rewrite op(e1) as:
* op(e1.aliasthis)
*/
Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
Expression *e = copy();
((UnaExp *)e)->e1 = e1;
e = e->semantic(sc);
return e;
}
#endif
}
return NULL;
}
Expression *BinExp::op_overload(Scope *sc)
{
//printf("BinExp::op_overload() (%s)\n", toChars());
AggregateDeclaration *ad;
Type *t1 = e1->type->toBasetype();
Type *t2 = e2->type->toBasetype();
Identifier *id = opId();
Identifier *id_r = opId_r();
Match m;
Expressions args1;
Expressions args2;
int argsset = 0;
AggregateDeclaration *ad1;
if (t1->ty == Tclass)
ad1 = ((TypeClass *)t1)->sym;
else if (t1->ty == Tstruct)
ad1 = ((TypeStruct *)t1)->sym;
else
ad1 = NULL;
AggregateDeclaration *ad2;
if (t2->ty == Tclass)
ad2 = ((TypeClass *)t2)->sym;
else if (t2->ty == Tstruct)
ad2 = ((TypeStruct *)t2)->sym;
else
ad2 = NULL;
Dsymbol *s = NULL;
Dsymbol *s_r = NULL;
FuncDeclaration *fd = NULL;
TemplateDeclaration *td = NULL;
if (ad1 && id)
{
s = search_function(ad1, id);
}
if (ad2 && id_r)
{
s_r = search_function(ad2, id_r);
}
if (s || s_r)
{
/* Try:
* a.opfunc(b)
* b.opfunc_r(a)
* and see which is better.
*/
Expression *e;
FuncDeclaration *lastf;
args1.setDim(1);
args1.data[0] = (void*) e1;
args2.setDim(1);
args2.data[0] = (void*) e2;
argsset = 1;
memset(&m, 0, sizeof(m));
m.last = MATCHnomatch;
if (s)
{
fd = s->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, NULL, &args2, sc->module);
}
else
{ td = s->isTemplateDeclaration();
templateResolve(&m, td, sc, loc, NULL, NULL, &args2);
}
}
lastf = m.lastf;
if (s_r)
{
fd = s_r->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, NULL, &args1, sc->module);
}
else
{ td = s_r->isTemplateDeclaration();
templateResolve(&m, td, sc, loc, NULL, NULL, &args1);
}
}
if (m.count > 1)
{
// Error, ambiguous
error("overloads %s and %s both match argument list for %s",
m.lastf->type->toChars(),
m.nextf->type->toChars(),
m.lastf->toChars());
}
else if (m.last == MATCHnomatch)
{
m.lastf = m.anyf;
}
if (op == TOKplusplus || op == TOKminusminus)
// Kludge because operator overloading regards e++ and e--
// as unary, but it's implemented as a binary.
// Rewrite (e1 ++ e2) as e1.postinc()
// Rewrite (e1 -- e2) as e1.postdec()
e = build_overload(loc, sc, e1, NULL, id);
else if (lastf && m.lastf == lastf || m.last == MATCHnomatch)
// Rewrite (e1 op e2) as e1.opfunc(e2)
e = build_overload(loc, sc, e1, e2, id);
else
// Rewrite (e1 op e2) as e2.opfunc_r(e1)
e = build_overload(loc, sc, e2, e1, id_r);
return e;
}
if (isCommutative())
{
s = NULL;
s_r = NULL;
if (ad1 && id_r)
{
s_r = search_function(ad1, id_r);
}
if (ad2 && id)
{
s = search_function(ad2, id);
}
if (s || s_r)
{
/* Try:
* a.opfunc_r(b)
* b.opfunc(a)
* and see which is better.
*/
FuncDeclaration *lastf;
if (!argsset)
{ args1.setDim(1);
args1.data[0] = (void*) e1;
args2.setDim(1);
args2.data[0] = (void*) e2;
}
memset(&m, 0, sizeof(m));
m.last = MATCHnomatch;
if (s_r)
{
fd = s_r->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, NULL, &args2, sc->module);
}
else
{ td = s_r->isTemplateDeclaration();
templateResolve(&m, td, sc, loc, NULL, NULL, &args2);
}
}
lastf = m.lastf;
if (s)
{
fd = s->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, NULL, &args1, sc->module);
}
else
{ td = s->isTemplateDeclaration();
templateResolve(&m, td, sc, loc, NULL, NULL, &args1);
}
}
if (m.count > 1)
{
// Error, ambiguous
error("overloads %s and %s both match argument list for %s",
m.lastf->type->toChars(),
m.nextf->type->toChars(),
m.lastf->toChars());
}
else if (m.last == MATCHnomatch)
{
m.lastf = m.anyf;
}
Expression *e;
if (lastf && m.lastf == lastf ||
id_r && m.last == MATCHnomatch)
// Rewrite (e1 op e2) as e1.opfunc_r(e2)
e = build_overload(loc, sc, e1, e2, id_r);
else
// Rewrite (e1 op e2) as e2.opfunc(e1)
e = build_overload(loc, sc, e2, e1, id);
// When reversing operands of comparison operators,
// need to reverse the sense of the op
switch (op)
{
case TOKlt: op = TOKgt; break;
case TOKgt: op = TOKlt; break;
case TOKle: op = TOKge; break;
case TOKge: op = TOKle; break;
// Floating point compares
case TOKule: op = TOKuge; break;
case TOKul: op = TOKug; break;
case TOKuge: op = TOKule; break;
case TOKug: op = TOKul; break;
// These are symmetric
case TOKunord:
case TOKlg:
case TOKleg:
case TOKue:
break;
}
return e;
}
}
#if DMDV2
// Try alias this on first operand
if (ad1 && ad1->aliasthis)
{
/* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2)
*/
Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
Expression *e = copy();
((BinExp *)e)->e1 = e1;
e = e->semantic(sc);
return e;
}
// Try alias this on second operand
if (ad2 && ad2->aliasthis)
{
/* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis)
*/
Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
Expression *e = copy();
((BinExp *)e)->e2 = e2;
e = e->semantic(sc);
return e;
}
#endif
return NULL;
}
/***********************************
* Utility to build a function call out of this reference and argument.
*/
Expression *build_overload(Loc loc, Scope *sc, Expression *ethis, Expression *earg, Identifier *id)
{
Expression *e;
//printf("build_overload(id = '%s')\n", id->toChars());
//earg->print();
//earg->type->print();
e = new DotIdExp(loc, ethis, id);
if (earg)
e = new CallExp(loc, e, earg);
else
e = new CallExp(loc, e);
e = e->semantic(sc);
return e;
}
/***************************************
* Search for function funcid in aggregate ad.
*/
Dsymbol *search_function(AggregateDeclaration *ad, Identifier *funcid)
{
Dsymbol *s;
FuncDeclaration *fd;
TemplateDeclaration *td;
s = ad->search(0, funcid, 0);
if (s)
{ Dsymbol *s2;
//printf("search_function: s = '%s'\n", s->kind());
s2 = s->toAlias();
//printf("search_function: s2 = '%s'\n", s2->kind());
fd = s2->isFuncDeclaration();
if (fd && fd->type->ty == Tfunction)
return fd;
td = s2->isTemplateDeclaration();
if (td)
return td;
}
return NULL;
}
/*****************************************
* Given array of arguments and an aggregate type,
* if any of the argument types are missing, attempt to infer
* them from the aggregate type.
*/
void inferApplyArgTypes(enum TOK op, Parameters *arguments, Expression *aggr, Module* from)
{
if (!arguments || !arguments->dim)
return;
/* Return if no arguments need types.
*/
for (size_t u = 0; 1; u++)
{ if (u == arguments->dim)
return;
Parameter *arg = (Parameter *)arguments->data[u];
if (!arg->type)
break;
}
AggregateDeclaration *ad;
Parameter *arg = (Parameter *)arguments->data[0];
Type *taggr = aggr->type;
if (!taggr)
return;
Type *tab = taggr->toBasetype();
switch (tab->ty)
{
case Tarray:
case Tsarray:
case Ttuple:
if (arguments->dim == 2)
{
if (!arg->type)
arg->type = Type::tsize_t; // key type
arg = (Parameter *)arguments->data[1];
}
if (!arg->type && tab->ty != Ttuple)
arg->type = tab->nextOf(); // value type
break;
case Taarray:
{ TypeAArray *taa = (TypeAArray *)tab;
if (arguments->dim == 2)
{
if (!arg->type)
arg->type = taa->index; // key type
arg = (Parameter *)arguments->data[1];
}
if (!arg->type)
arg->type = taa->next; // value type
break;
}
case Tclass:
ad = ((TypeClass *)tab)->sym;
goto Laggr;
case Tstruct:
ad = ((TypeStruct *)tab)->sym;
goto Laggr;
Laggr:
#if 0
if (arguments->dim == 1)
{
if (!arg->type)
{
/* Look for an opNext() overload
*/
Dsymbol *s = search_function(ad, Id::next);
fd = s ? s->isFuncDeclaration() : NULL;
if (!fd)
goto Lapply;
arg->type = fd->type->next;
}
break;
}
#endif
Lapply:
{ /* Look for an
* int opApply(int delegate(ref Type [, ...]) dg);
* overload
*/
Dsymbol *s = search_function(ad,
(op == TOKforeach_reverse) ? Id::applyReverse
: Id::apply);
if (s)
{
FuncDeclaration *fd = s->isFuncDeclaration();
if (fd)
inferApplyArgTypesX(from, fd, arguments);
}
break;
}
case Tdelegate:
{
if (0 && aggr->op == TOKdelegate)
{ DelegateExp *de = (DelegateExp *)aggr;
FuncDeclaration *fd = de->func->isFuncDeclaration();
if (fd)
inferApplyArgTypesX(from, fd, arguments);
}
else
{
inferApplyArgTypesY((TypeFunction *)tab->nextOf(), arguments);
}
break;
}
default:
break; // ignore error, caught later
}
}
/********************************
* Recursive helper function,
* analogous to func.overloadResolveX().
*/
int fp3(void *param, FuncDeclaration *f)
{
Parameters *arguments = (Parameters *)param;
TypeFunction *tf = (TypeFunction *)f->type;
if (inferApplyArgTypesY(tf, arguments) == 1)
return 0;
if (arguments->dim == 0)
return 1;
return 0;
}
static void inferApplyArgTypesX(Module* from, FuncDeclaration *fstart, Parameters *arguments)
{
overloadApply(from, fstart, &fp3, arguments);
}
#if 0
static void inferApplyArgTypesX(FuncDeclaration *fstart, Parameters *arguments)
{
Declaration *d;
Declaration *next;
for (d = fstart; d; d = next)
{
FuncDeclaration *f;
FuncAliasDeclaration *fa;
AliasDeclaration *a;
fa = d->isFuncAliasDeclaration();
if (fa)
{
inferApplyArgTypesX(fa->funcalias, arguments);
next = fa->overnext;
}
else if ((f = d->isFuncDeclaration()) != NULL)
{
next = f->overnext;
TypeFunction *tf = (TypeFunction *)f->type;
if (inferApplyArgTypesY(tf, arguments) == 1)
continue;
if (arguments->dim == 0)
return;
}
else if ((a = d->isAliasDeclaration()) != NULL)
{
Dsymbol *s = a->toAlias();
next = s->isDeclaration();
if (next == a)
break;
if (next == fstart)
break;
}
else
{ d->error("is aliased to a function");
break;
}
}
}
#endif
/******************************
* Infer arguments from type of function.
* Returns:
* 0 match for this function
* 1 no match for this function
*/
static int inferApplyArgTypesY(TypeFunction *tf, Parameters *arguments)
{ size_t nparams;
Parameter *p;
if (Parameter::dim(tf->parameters) != 1)
goto Lnomatch;
p = Parameter::getNth(tf->parameters, 0);
if (p->type->ty != Tdelegate)
goto Lnomatch;
tf = (TypeFunction *)p->type->nextOf();
assert(tf->ty == Tfunction);
/* We now have tf, the type of the delegate. Match it against
* the arguments, filling in missing argument types.
*/
nparams = Parameter::dim(tf->parameters);
if (nparams == 0 || tf->varargs)
goto Lnomatch; // not enough parameters
if (arguments->dim != nparams)
goto Lnomatch; // not enough parameters
for (size_t u = 0; u < nparams; u++)
{
Parameter *arg = (Parameter *)arguments->data[u];
Parameter *param = Parameter::getNth(tf->parameters, u);
if (arg->type)
{ if (!arg->type->equals(param->type))
{
/* Cannot resolve argument types. Indicate an
* error by setting the number of arguments to 0.
*/
arguments->dim = 0;
goto Lmatch;
}
continue;
}
arg->type = param->type;
}
Lmatch:
return 0;
Lnomatch:
return 1;
}
/**************************************
*/
static void templateResolve(Match *m, TemplateDeclaration *td, Scope *sc, Loc loc, Objects *targsi, Expression *ethis, Expressions *arguments)
{
FuncDeclaration *fd;
assert(td);
fd = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments);
if (!fd)
return;
m->anyf = fd;
if (m->last >= MATCHexact)
{
m->nextf = fd;
m->count++;
}
else
{
m->last = MATCHexact;
m->lastf = fd;
m->count = 1;
}
}

View File

@@ -1,896 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
#if __DMC__
#include <complex.h>
#endif
#include "lexer.h"
#include "mtype.h"
#include "expression.h"
#include "declaration.h"
#include "aggregate.h"
#include "init.h"
#ifdef IN_GCC
#include "d-gcc-real.h"
/* %% fix? */
extern "C" bool real_isnan (const real_t *);
#endif
static real_t zero; // work around DMC bug for now
/*************************************
* If expression is a variable with a const initializer,
* return that initializer.
*/
Expression *fromConstInitializer(int result, Expression *e1)
{
//printf("fromConstInitializer(%s)\n", e1->toChars());
if (e1->op == TOKvar)
{ VarExp *ve = (VarExp *)e1;
VarDeclaration *v = ve->var->isVarDeclaration();
if (v && !v->originalType && v->scope) // semantic() not yet run
v->semantic (v->scope);
if (!v || !v->type)
return e1;
Type * tb = v->type->toBasetype();
if (v->isConst() && v->init
&& (result & WANTinterpret || (tb->isscalar() ||
((result & WANTexpand) && (tb->ty != Tsarray && tb->ty != Tstruct))))
)
{ Expression *ei = v->init->toExpression();
if (ei && ei->type)
e1 = ei;
}
}
return e1;
}
Expression *Expression::optimize(int result)
{
//printf("Expression::optimize(result = x%x) %s\n", result, toChars());
return this;
}
Expression *VarExp::optimize(int result)
{
return fromConstInitializer(result, this);
return this;
}
Expression *TupleExp::optimize(int result)
{
for (size_t i = 0; i < exps->dim; i++)
{ Expression *e = (Expression *)exps->data[i];
e = e->optimize(WANTvalue | (result & WANTinterpret));
exps->data[i] = (void *)e;
}
return this;
}
Expression *ArrayLiteralExp::optimize(int result)
{
if (elements)
{
for (size_t i = 0; i < elements->dim; i++)
{ Expression *e = (Expression *)elements->data[i];
e = e->optimize(WANTvalue | (result & WANTinterpret));
elements->data[i] = (void *)e;
}
}
return this;
}
Expression *AssocArrayLiteralExp::optimize(int result)
{
assert(keys->dim == values->dim);
for (size_t i = 0; i < keys->dim; i++)
{ Expression *e = (Expression *)keys->data[i];
e = e->optimize(WANTvalue | (result & WANTinterpret));
keys->data[i] = (void *)e;
e = (Expression *)values->data[i];
e = e->optimize(WANTvalue | (result & WANTinterpret));
values->data[i] = (void *)e;
}
return this;
}
Expression *StructLiteralExp::optimize(int result)
{
if (elements)
{
for (size_t i = 0; i < elements->dim; i++)
{ Expression *e = (*elements)[i];
if (!e)
continue;
e = e->optimize(WANTvalue | (result & WANTinterpret));
(*elements)[i] = e;
}
}
return this;
}
Expression *TypeExp::optimize(int result)
{
return this;
}
Expression *UnaExp::optimize(int result)
{
e1 = e1->optimize(result);
return this;
}
Expression *NegExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
if (e1->isConst() == 1)
{
e = Neg(type, e1);
}
else
e = this;
return e;
}
Expression *ComExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
if (e1->isConst() == 1)
{
e = Com(type, e1);
}
else
e = this;
return e;
}
Expression *NotExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
if (e1->isConst() == 1)
{
e = Not(type, e1);
}
else
e = this;
return e;
}
Expression *BoolExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
if (e1->isConst() == 1)
{
e = Bool(type, e1);
}
else
e = this;
return e;
}
Expression *AddrExp::optimize(int result)
{ Expression *e;
//printf("AddrExp::optimize(result = %d) %s\n", result, toChars());
// never try to interpret: it could change the semantics by turning
// const p = &s; into an something like const p = &(Struct());
e1 = e1->optimize(result & ~WANTinterpret);
// Convert &*ex to ex
if (e1->op == TOKstar)
{ Expression *ex;
ex = ((PtrExp *)e1)->e1;
if (type->equals(ex->type))
e = ex;
else
{
e = ex->copy();
e->type = type;
}
return e;
}
#if !IN_LLVM
if (e1->op == TOKvar)
{ VarExp *ve = (VarExp *)e1;
if (!ve->var->isOut() && !ve->var->isRef() &&
!ve->var->isImportedSymbol())
{
e = new SymOffExp(loc, ve->var, 0);
e->type = type;
return e;
}
}
if (e1->op == TOKindex)
{ // Convert &array[n] to &array+n
IndexExp *ae = (IndexExp *)e1;
if (ae->e2->op == TOKint64 && ae->e1->op == TOKvar)
{
dinteger_t index = ae->e2->toInteger();
VarExp *ve = (VarExp *)ae->e1;
if (ve->type->ty == Tsarray && ve->type->next->ty != Tbit
&& !ve->var->isImportedSymbol())
{
TypeSArray *ts = (TypeSArray *)ve->type;
dinteger_t dim = ts->dim->toInteger();
if (index < 0 || index >= dim)
error("array index %jd is out of bounds [0..%jd]", index, dim);
e = new SymOffExp(loc, ve->var, index * ts->next->size());
e->type = type;
return e;
}
}
}
#endif
return this;
}
Expression *PtrExp::optimize(int result)
{
//printf("PtrExp::optimize(result = x%x) %s\n", result, toChars());
e1 = e1->optimize(result);
// Convert *&ex to ex
if (e1->op == TOKaddress)
{ Expression *e;
Expression *ex;
ex = ((AddrExp *)e1)->e1;
if (type->equals(ex->type))
e = ex;
else
{
e = ex->copy();
e->type = type;
}
return e;
}
// Constant fold *(&structliteral + offset)
if (e1->op == TOKadd)
{
Expression *e;
e = Ptr(type, e1);
if (e != EXP_CANT_INTERPRET)
return e;
}
return this;
}
Expression *DotVarExp::optimize(int result)
{
//printf("DotVarExp::optimize(result = x%x) %s\n", result, toChars());
e1 = e1->optimize(result);
#if DMDV2
if (e1->op == TOKvar)
{ VarExp *ve = (VarExp *)e1;
VarDeclaration *v = ve->var->isVarDeclaration();
Expression *e = expandVar(result, v);
if (e && e->op == TOKstructliteral)
{ StructLiteralExp *sle = (StructLiteralExp *)e;
VarDeclaration *vf = var->isVarDeclaration();
if (vf)
{
e = sle->getField(type, vf->offset);
if (e && e != EXP_CANT_INTERPRET)
return e;
}
}
}
else
#endif
if (e1->op == TOKstructliteral)
{ StructLiteralExp *sle = (StructLiteralExp *)e1;
VarDeclaration *vf = var->isVarDeclaration();
if (vf)
{
Expression *e = sle->getField(type, vf->offset);
if (e && e != EXP_CANT_INTERPRET)
return e;
}
}
return this;
}
Expression *NewExp::optimize(int result)
{
if (thisexp)
thisexp = thisexp->optimize(WANTvalue);
// Optimize parameters
if (newargs)
{
for (size_t i = 0; i < newargs->dim; i++)
{ Expression *e = newargs->tdata()[i];
e = e->optimize(WANTvalue);
newargs->tdata()[i] = e;
}
}
if (arguments)
{
for (size_t i = 0; i < arguments->dim; i++)
{ Expression *e = arguments->tdata()[i];
e = e->optimize(WANTvalue);
arguments->tdata()[i] = e;
}
}
return this;
}
Expression *CallExp::optimize(int result)
{
//printf("CallExp::optimize(result = %d) %s\n", result, toChars());
Expression *e = this;
// Optimize parameters
if (arguments)
{
for (size_t i = 0; i < arguments->dim; i++)
{ Expression *e = arguments->tdata()[i];
e = e->optimize(WANTvalue);
arguments->tdata()[i] = e;
}
}
e1 = e1->optimize(result);
if (result & WANTinterpret)
{
Expression *eresult = interpret(NULL);
if (eresult == EXP_CANT_INTERPRET)
return e;
if (eresult && eresult != EXP_VOID_INTERPRET)
e = eresult;
else
error("cannot evaluate %s at compile time", toChars());
}
return e;
}
Expression *CastExp::optimize(int result)
{
//printf("CastExp::optimize(result = %d) %s\n", result, toChars());
//printf("from %s to %s\n", type->toChars(), to->toChars());
//printf("from %s\n", type->toChars());
//printf("e1->type %s\n", e1->type->toChars());
//printf("type = %p\n", type);
assert(type);
enum TOK op1 = e1->op;
e1 = e1->optimize(result);
if (result & WANTinterpret)
e1 = fromConstInitializer(result, e1);
if ((e1->op == TOKstring || e1->op == TOKarrayliteral) &&
(type->ty == Tpointer || type->ty == Tarray) &&
type->next->equals(e1->type->next)
)
{
// make a copy before adjusting type to avoid
// messing up the type of an existing initializer
e1 = e1->syntaxCopy();
e1->type = type;
return e1;
}
/* The first test here is to prevent infinite loops
*/
if (op1 != TOKarrayliteral && e1->op == TOKarrayliteral)
return e1->castTo(NULL, to);
if (e1->op == TOKnull &&
(type->ty == Tpointer || type->ty == Tclass))
{
e1->type = type;
return e1;
}
if (result & WANTflags && type->ty == Tclass && e1->type->ty == Tclass)
{
// See if we can remove an unnecessary cast
ClassDeclaration *cdfrom;
ClassDeclaration *cdto;
int offset;
cdfrom = e1->type->isClassHandle();
cdto = type->isClassHandle();
if (cdto->isBaseOf(cdfrom, &offset) && offset == 0)
{
e1->type = type;
return e1;
}
}
Expression *e;
if (e1->isConst())
{
if (e1->op == TOKsymoff)
{
if (type->size() == e1->type->size() &&
type->toBasetype()->ty != Tsarray)
{
e1->type = type;
return e1;
}
return this;
}
if (to->toBasetype()->ty == Tvoid)
e = this;
else
e = Cast(type, to, e1);
}
else
e = this;
return e;
}
Expression *BinExp::optimize(int result)
{
//printf("BinExp::optimize(result = %d) %s\n", result, toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (op == TOKshlass || op == TOKshrass || op == TOKushrass)
{
if (e2->isConst() == 1)
{
dinteger_t i2 = e2->toInteger();
d_uns64 sz = e1->type->size() * 8;
if (i2 < 0 || i2 >= sz)
{ error("shift assign by %jd is outside the range 0..%zu", i2, sz - 1);
e2 = new IntegerExp(0);
}
}
}
return this;
}
Expression *AddExp::optimize(int result)
{ Expression *e;
//printf("AddExp::optimize(%s)\n", toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() && e2->isConst())
{
if (e1->op == TOKsymoff && e2->op == TOKsymoff)
return this;
e = Add(type, e1, e2);
}
else
e = this;
return e;
}
Expression *MinExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() && e2->isConst())
{
if (e2->op == TOKsymoff)
return this;
e = Min(type, e1, e2);
}
else
e = this;
return e;
}
Expression *MulExp::optimize(int result)
{ Expression *e;
//printf("MulExp::optimize(result = %d) %s\n", result, toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
{
e = Mul(type, e1, e2);
}
else
e = this;
return e;
}
Expression *DivExp::optimize(int result)
{ Expression *e;
//printf("DivExp::optimize(%s)\n", toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
{
e = Div(type, e1, e2);
}
else
e = this;
return e;
}
Expression *ModExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
{
e = Mod(type, e1, e2);
}
else
e = this;
return e;
}
Expression *shift_optimize(int result, BinExp *e, Expression *(*shift)(Type *, Expression *, Expression *))
{ Expression *ex = e;
e->e1 = e->e1->optimize(result);
e->e2 = e->e2->optimize(result);
if (e->e2->isConst() == 1)
{
dinteger_t i2 = e->e2->toInteger();
d_uns64 sz = e->e1->type->size() * 8;
if (i2 < 0 || i2 >= sz)
{ e->error("shift by %jd is outside the range 0..%zu", i2, sz - 1);
e->e2 = new IntegerExp(0);
}
if (e->e1->isConst() == 1)
ex = (*shift)(e->type, e->e1, e->e2);
}
return ex;
}
Expression *ShlExp::optimize(int result)
{
//printf("ShlExp::optimize(result = %d) %s\n", result, toChars());
return shift_optimize(result, this, Shl);
}
Expression *ShrExp::optimize(int result)
{
//printf("ShrExp::optimize(result = %d) %s\n", result, toChars());
return shift_optimize(result, this, Shr);
}
Expression *UshrExp::optimize(int result)
{
//printf("UshrExp::optimize(result = %d) %s\n", result, toChars());
return shift_optimize(result, this, Ushr);
}
Expression *AndExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
e = And(type, e1, e2);
else
e = this;
return e;
}
Expression *OrExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
e = Or(type, e1, e2);
else
e = this;
return e;
}
Expression *XorExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
e = Xor(type, e1, e2);
else
e = this;
return e;
}
Expression *CommaExp::optimize(int result)
{ Expression *e;
//printf("CommaExp::optimize(result = %d) %s\n", result, toChars());
// Comma needs special treatment, because it may
// contain compiler-generated declarations. We can interpret them, but
// otherwise we must NOT attempt to constant-fold them.
// In particular, if the comma returns a temporary variable, it needs
// to be an lvalue (this is particularly important for struct constructors)
if (result & WANTinterpret)
{ // Interpreting comma needs special treatment, because it may
// contain compiler-generated declarations.
e = interpret(NULL);
return (e == EXP_CANT_INTERPRET) ? this : e;
}
e1 = e1->optimize(result & WANTinterpret);
e2 = e2->optimize(result);
if (!e1 || e1->op == TOKint64 || e1->op == TOKfloat64 || !e1->hasSideEffect())
{
e = e2;
if (e)
e->type = type;
}
else
e = this;
//printf("-CommaExp::optimize(result = %d) %s\n", result, e->toChars());
return e;
}
Expression *ArrayLengthExp::optimize(int result)
{ Expression *e;
//printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars());
e1 = e1->optimize(WANTvalue | WANTexpand | (result & WANTinterpret));
e = this;
if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral)
{
e = ArrayLength(type, e1);
}
return e;
}
Expression *EqualExp::optimize(int result)
{ Expression *e;
//printf("EqualExp::optimize(result = %x) %s\n", result, toChars());
e1 = e1->optimize(WANTvalue | (result & WANTinterpret));
e2 = e2->optimize(WANTvalue | (result & WANTinterpret));
e = this;
Expression *e1 = fromConstInitializer(result, this->e1);
Expression *e2 = fromConstInitializer(result, this->e2);
e = Equal(op, type, e1, e2);
if (e == EXP_CANT_INTERPRET)
e = this;
return e;
}
Expression *IdentityExp::optimize(int result)
{
//printf("IdentityExp::optimize(result = %d) %s\n", result, toChars());
e1 = e1->optimize(WANTvalue | (result & WANTinterpret));
e2 = e2->optimize(WANTvalue | (result & WANTinterpret));
Expression *e = this;
if ((this->e1->isConst() && this->e2->isConst()) ||
(this->e1->op == TOKnull && this->e2->op == TOKnull))
{
e = Identity(op, type, this->e1, this->e2);
if (e == EXP_CANT_INTERPRET)
e = this;
}
return e;
}
/* It is possible for constant folding to change an array expression of
* unknown length, into one where the length is known.
* If the expression 'arr' is a literal, set lengthVar to be its length.
*/
void setLengthVarIfKnown(VarDeclaration *lengthVar, Expression *arr)
{
if (!lengthVar)
return;
if (lengthVar->init && !lengthVar->init->isVoidInitializer())
return; // we have previously calculated the length
size_t len;
if (arr->op == TOKstring)
len = ((StringExp *)arr)->len;
else if (arr->op == TOKarrayliteral)
len = ((ArrayLiteralExp *)arr)->elements->dim;
else
return; // we don't know the length yet
Expression *dollar = new IntegerExp(0, len, Type::tsize_t);
lengthVar->init = new ExpInitializer(0, dollar);
lengthVar->storage_class |= STCstatic | STCconst;
}
Expression *IndexExp::optimize(int result)
{ Expression *e;
//printf("IndexExp::optimize(result = %d) %s\n", result, toChars());
Expression *e1 = this->e1->optimize(WANTvalue | (result & WANTinterpret));
if (result & WANTinterpret)
e1 = fromConstInitializer(result, e1);
// We might know $ now
setLengthVarIfKnown(lengthVar, e1);
e2 = e2->optimize(WANTvalue | (result & WANTinterpret));
e = Index(type, e1, e2);
if (e == EXP_CANT_INTERPRET)
e = this;
return e;
}
Expression *SliceExp::optimize(int result)
{ Expression *e;
//printf("SliceExp::optimize(result = %d) %s\n", result, toChars());
e = this;
e1 = e1->optimize(WANTvalue | (result & WANTinterpret));
if (!lwr)
{ if (e1->op == TOKstring)
{ // Convert slice of string literal into dynamic array
Type *t = e1->type->toBasetype();
if (t->nextOf())
e = e1->castTo(NULL, t->nextOf()->arrayOf());
}
return e;
}
if (result & WANTinterpret)
e1 = fromConstInitializer(result, e1);
// We might know $ now
setLengthVarIfKnown(lengthVar, e1);
lwr = lwr->optimize(WANTvalue | (result & WANTinterpret));
upr = upr->optimize(WANTvalue | (result & WANTinterpret));
e = Slice(type, e1, lwr, upr);
if (e == EXP_CANT_INTERPRET)
e = this;
//printf("-SliceExp::optimize() %s\n", e->toChars());
return e;
}
Expression *AndAndExp::optimize(int result)
{ Expression *e;
//printf("AndAndExp::optimize(%d) %s\n", result, toChars());
e1 = e1->optimize(WANTflags | (result & WANTinterpret));
e = this;
if (e1->isBool(FALSE))
{
if (type->toBasetype()->ty == Tvoid)
e = e2;
else
{ e = new CommaExp(loc, e1, new IntegerExp(loc, 0, type));
e->type = type;
}
e = e->optimize(result);
}
else
{
e2 = e2->optimize(WANTflags | (result & WANTinterpret));
if (result && e2->type->toBasetype()->ty == Tvoid && !global.errors)
error("void has no value");
if (e1->isConst())
{
if (e2->isConst())
{ int n1 = e1->isBool(1);
int n2 = e2->isBool(1);
e = new IntegerExp(loc, n1 && n2, type);
}
else if (e1->isBool(TRUE))
{
if (type->toBasetype()->ty == Tvoid)
e = e2;
else e = new BoolExp(loc, e2, type);
}
}
}
return e;
}
Expression *OrOrExp::optimize(int result)
{ Expression *e;
e1 = e1->optimize(WANTflags | (result & WANTinterpret));
e = this;
if (e1->isBool(TRUE))
{ // Replace with (e1, 1)
e = new CommaExp(loc, e1, new IntegerExp(loc, 1, type));
e->type = type;
e = e->optimize(result);
}
else
{
e2 = e2->optimize(WANTflags | (result & WANTinterpret));
if (result && e2->type->toBasetype()->ty == Tvoid && !global.errors)
error("void has no value");
if (e1->isConst())
{
if (e2->isConst())
{ int n1 = e1->isBool(1);
int n2 = e2->isBool(1);
e = new IntegerExp(loc, n1 || n2, type);
}
else if (e1->isBool(FALSE))
{
if (type->toBasetype()->ty == Tvoid)
e = e2;
else
e = new BoolExp(loc, e2, type);
}
}
}
return e;
}
Expression *CmpExp::optimize(int result)
{ Expression *e;
//printf("CmpExp::optimize() %s\n", toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
if (e1->isConst() == 1 && e2->isConst() == 1)
{
e = Cmp(op, type, this->e1, this->e2);
}
else
e = this;
return e;
}
Expression *CatExp::optimize(int result)
{ Expression *e;
//printf("CatExp::optimize(%d) %s\n", result, toChars());
e1 = e1->optimize(result);
e2 = e2->optimize(result);
e = Cat(type, e1, e2);
if (e == EXP_CANT_INTERPRET)
e = this;
return e;
}
Expression *CondExp::optimize(int result)
{ Expression *e;
econd = econd->optimize(WANTflags | (result & WANTinterpret));
if (econd->isBool(TRUE))
e = e1->optimize(result);
else if (econd->isBool(FALSE))
e = e2->optimize(result);
else
{ e1 = e1->optimize(result);
e2 = e2->optimize(result);
e = this;
}
return e;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,172 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_PARSE_H
#define DMD_PARSE_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "arraytypes.h"
#include "lexer.h"
#include "enum.h"
struct Type;
struct TypeQualified;
struct Expression;
struct Declaration;
struct Statement;
struct Import;
struct Initializer;
struct FuncDeclaration;
struct CtorDeclaration;
struct PostBlitDeclaration;
struct DtorDeclaration;
struct StaticCtorDeclaration;
struct StaticDtorDeclaration;
struct ConditionalDeclaration;
struct InvariantDeclaration;
struct UnitTestDeclaration;
struct NewDeclaration;
struct DeleteDeclaration;
struct Condition;
struct Module;
struct ModuleDeclaration;
struct TemplateDeclaration;
struct TemplateInstance;
struct StaticAssert;
/************************************
* These control how parseStatement() works.
*/
enum ParseStatementFlags
{
PSsemi = 1, // empty ';' statements are allowed
PSscope = 2, // start a new scope
PScurly = 4, // { } statement is required
PScurlyscope = 8, // { } starts a new scope
};
struct Parser : Lexer
{
ModuleDeclaration *md;
enum LINK linkage;
Loc endloc; // set to location of last right curly
int inBrackets; // inside [] of array index or slice
Parser(Module *module, unsigned char *base, unsigned length, int doDocComment);
Dsymbols *parseModule();
Dsymbols *parseDeclDefs(int once);
Array *parseAutoDeclarations(StorageClass storageClass, unsigned char *comment);
Dsymbols *parseBlock();
void composeStorageClass(StorageClass stc);
Expression *parseConstraint();
TemplateDeclaration *parseTemplateDeclaration();
TemplateParameters *parseTemplateParameterList(int flag = 0);
Dsymbol *parseMixin();
Objects *parseTemplateArgumentList();
Objects *parseTemplateArgumentList2();
Objects *parseTemplateArgument();
StaticAssert *parseStaticAssert();
TypeQualified *parseTypeof();
enum LINK parseLinkage();
Condition *parseDebugCondition();
Condition *parseVersionCondition();
Condition *parseStaticIfCondition();
Dsymbol *parseCtor();
PostBlitDeclaration *parsePostBlit();
DtorDeclaration *parseDtor();
StaticCtorDeclaration *parseStaticCtor();
StaticDtorDeclaration *parseStaticDtor();
InvariantDeclaration *parseInvariant();
UnitTestDeclaration *parseUnitTest();
NewDeclaration *parseNew();
DeleteDeclaration *parseDelete();
Parameters *parseParameters(int *pvarargs);
EnumDeclaration *parseEnum();
Dsymbol *parseAggregate();
BaseClasses *parseBaseClasses();
Import *parseImport(Array *decldefs, int isstatic);
Type *parseType(Identifier **pident = NULL, TemplateParameters **tpl = NULL);
Type *parseBasicType();
Type *parseBasicType2(Type *t);
Type *parseDeclarator(Type *t, Identifier **pident, TemplateParameters **tpl = NULL);
Dsymbols *parseDeclarations();
void parseContracts(FuncDeclaration *f);
Statement *parseStatement(int flags);
Initializer *parseInitializer();
Expression *parseDefaultInitExp();
void check(Loc loc, enum TOK value);
void check(enum TOK value);
void check(enum TOK value, const char *string);
int isDeclaration(Token *t, int needId, enum TOK endtok, Token **pt);
int isBasicType(Token **pt);
int isDeclarator(Token **pt, int *haveId, enum TOK endtok);
int isParameters(Token **pt);
int isExpression(Token **pt);
int isTemplateInstance(Token *t, Token **pt);
int skipParens(Token *t, Token **pt);
Expression *parseExpression();
Expression *parsePrimaryExp();
Expression *parseUnaryExp();
Expression *parsePostExp(Expression *e);
Expression *parseMulExp();
Expression *parseAddExp();
Expression *parseShiftExp();
Expression *parseRelExp();
Expression *parseEqualExp();
Expression *parseCmpExp();
Expression *parseAndExp();
Expression *parseXorExp();
Expression *parseOrExp();
Expression *parseAndAndExp();
Expression *parseOrOrExp();
Expression *parseCondExp();
Expression *parseAssignExp();
Expressions *parseArguments();
Expression *parseNewExp(Expression *thisexp);
void addComment(Dsymbol *s, unsigned char *blockComment);
};
// Operator precedence - greater values are higher precedence
enum PREC
{
PREC_zero,
PREC_expr,
PREC_assign,
PREC_cond,
PREC_oror,
PREC_andand,
PREC_or,
PREC_xor,
PREC_and,
PREC_equal,
PREC_rel,
PREC_shift,
PREC_add,
PREC_mul,
PREC_unary,
PREC_primary,
};
extern enum PREC precedence[TOKMAX];
void initPrecedence();
#endif /* DMD_PARSE_H */

View File

@@ -1,27 +0,0 @@
The D Programming Language
Compiler Front End Source
Copyright (c) 1999-2002, by Digital Mars
www.digitalmars.com
All Rights Reserved
This is the source code to the front end Digital Mars D compiler.
It covers the lexical analysis, parsing, and semantic analysis
of the D Programming Language defined in the documents at
www.digitalmars.com/d/
The optimizer, code generator, and object file generator are not part
of this source, hence the source does not currently constitute a complete,
compilable program. However, many people have expressed a strong interested
in producing a D compiler with the GNU compiler sources. This release should
enable that.
These sources are free, they are redistributable and modifiable
under the terms of the GNU General Public License (attached as gpl.txt),
or the Artistic License (attached as artistic.txt).
It does not apply to anything else distributed by Digital Mars,
including D compiler executables.
-Walter Bright

View File

@@ -1,16 +0,0 @@
#ifndef __RMEM_H__
#define __RMEM_H__
// jam memory stuff here
#include "mem.h"
#if (defined (__SVR4) && defined (__sun))
#include <alloca.h>
#endif
#ifdef __MINGW32__
#include <malloc.h>
#endif
#endif // __RMEM_H__

View File

@@ -1,188 +0,0 @@
/**
* Implementation of associative arrays.
*
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "aav.h"
static const size_t prime_list[] = {
31UL,
97UL, 389UL,
1543UL, 6151UL,
24593UL, 98317UL,
393241UL, 1572869UL,
6291469UL, 25165843UL,
100663319UL, 402653189UL,
1610612741UL, 4294967291UL,
};
struct aaA
{
aaA *next;
Key key;
Value value;
};
struct AA
{
aaA* *b;
size_t b_length;
size_t nodes; // total number of aaA nodes
aaA* binit[4]; // initial value of b[]
};
static const AA bbinit = { NULL, };
/****************************************************
* Determine number of entries in associative array.
*/
size_t _aaLen(AA* aa)
{
return aa ? aa->nodes : 0;
}
/*************************************************
* Get pointer to value in associative array indexed by key.
* Add entry for key if it is not already there.
*/
Value* _aaGet(AA** paa, Key key)
{
//printf("paa = %p\n", paa);
if (!*paa)
{ AA *a = new AA();
*a = bbinit;
a->b = a->binit;
a->b_length = sizeof(a->binit) / sizeof(a->binit[0]);
*paa = a;
assert((*paa)->b_length == 4);
}
//printf("paa = %p, *paa = %p\n", paa, *paa);
assert((*paa)->b_length);
size_t i = (size_t)key % (*paa)->b_length;
aaA** pe = &(*paa)->b[i];
aaA *e;
while ((e = *pe) != NULL)
{
if (key == e->key)
return &e->value;
pe = &e->next;
}
// Not found, create new elem
//printf("create new one\n");
e = new aaA();
e->next = NULL;
e->key = key;
e->value = NULL;
*pe = e;
size_t nodes = ++(*paa)->nodes;
//printf("length = %d, nodes = %d\n", paa.a.b.length, nodes);
if (nodes > (*paa)->b_length * 4)
{
//printf("rehash\n");
_aaRehash(paa);
}
return &e->value;
}
/*************************************************
* Get value in associative array indexed by key.
* Returns NULL if it is not already there.
*/
Value _aaGetRvalue(AA* aa, Key key)
{
//printf("_aaGetRvalue(key = %p)\n", key);
if (!aa)
return NULL;
size_t len = aa->b_length;
if (len)
{
size_t i = (size_t)key % len;
aaA* e = aa->b[i];
while (e)
{
if (key == e->key)
return e->value;
e = e->next;
}
}
return NULL; // not found
}
/********************************************
* Rehash an array.
*/
void _aaRehash(AA** paa)
{
//printf("Rehash\n");
if (*paa)
{
AA newb = bbinit;
AA *aa = *paa;
size_t len = _aaLen(*paa);
if (len)
{ size_t i;
for (i = 0; i < sizeof(prime_list)/sizeof(prime_list[0]) - 1; i++)
{
if (len <= prime_list[i])
break;
}
len = prime_list[i];
newb.b = new aaA*[len];
memset(newb.b, 0, len * sizeof(aaA*));
newb.b_length = len;
for (size_t k = 0; k < aa->b_length; k++)
{ aaA *e = aa->b[k];
while (e)
{ aaA* enext = e->next;
size_t j = (size_t)e->key % len;
e->next = newb.b[j];
newb.b[j] = e;
e = enext;
}
}
if (aa->b != aa->binit)
delete[] aa->b;
newb.nodes = aa->nodes;
}
**paa = newb;
}
}
#if UNITTEST
void unittest_aa()
{
AA* aa = NULL;
Value v = _aaGetRvalue(aa, NULL);
assert(!v);
Value *pv = _aaGet(&aa, NULL);
assert(pv);
*pv = (void *)3;
v = _aaGetRvalue(aa, NULL);
assert(v == (void *)3);
}
#endif

View File

@@ -1,11 +0,0 @@
typedef void* Value;
typedef void* Key;
struct AA;
size_t _aaLen(AA* aa);
Value* _aaGet(AA** aa, Key key);
Value _aaGetRvalue(AA* aa, Key key);
void _aaRehash(AA** paa);

View File

@@ -1,256 +0,0 @@
// Copyright (c) 1999-2010 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#if (defined (__SVR4) && defined (__sun))
#include <alloca.h>
#endif
#if _MSC_VER || __MINGW32__
#include <malloc.h>
#endif
#if IN_GCC
#include "gdc_alloca.h"
#endif
#if _WIN32
#include <windows.h>
#endif
#ifndef _WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <utime.h>
#endif
#include "port.h"
#include "root.h"
#include "rmem.h"
/********************************* Array ****************************/
Array::Array()
{
data = SMALLARRAYCAP ? &smallarray[0] : NULL;
dim = 0;
allocdim = SMALLARRAYCAP;
}
Array::~Array()
{
if (data != &smallarray[0])
mem.free(data);
}
void Array::mark()
{ unsigned u;
mem.mark(data);
for (u = 0; u < dim; u++)
mem.mark(data[u]); // BUG: what if arrays of Object's?
}
void Array::reserve(unsigned nentries)
{
//printf("Array::reserve: dim = %d, allocdim = %d, nentries = %d\n", dim, allocdim, nentries);
if (allocdim - dim < nentries)
{
if (allocdim == 0)
{ // Not properly initialized, someone memset it to zero
if (nentries <= SMALLARRAYCAP)
{ allocdim = SMALLARRAYCAP;
data = SMALLARRAYCAP ? &smallarray[0] : NULL;
}
else
{ allocdim = nentries;
data = (void **)mem.malloc(allocdim * sizeof(*data));
}
}
else if (allocdim == SMALLARRAYCAP)
{
allocdim = dim + nentries;
data = (void **)mem.malloc(allocdim * sizeof(*data));
memcpy(data, &smallarray[0], dim * sizeof(*data));
}
else
{ allocdim = dim + nentries;
data = (void **)mem.realloc(data, allocdim * sizeof(*data));
}
}
}
void Array::setDim(unsigned newdim)
{
if (dim < newdim)
{
reserve(newdim - dim);
}
dim = newdim;
}
void Array::fixDim()
{
if (dim != allocdim)
{
if (allocdim >= SMALLARRAYCAP)
{
if (dim <= SMALLARRAYCAP)
{
memcpy(&smallarray[0], data, dim * sizeof(*data));
mem.free(data);
}
else
data = (void **)mem.realloc(data, dim * sizeof(*data));
}
allocdim = dim;
}
}
void Array::push(void *ptr)
{
reserve(1);
data[dim++] = ptr;
}
void *Array::pop()
{
return data[--dim];
}
void Array::shift(void *ptr)
{
reserve(1);
memmove(data + 1, data, dim * sizeof(*data));
data[0] = ptr;
dim++;
}
void Array::insert(unsigned index, void *ptr)
{
reserve(1);
memmove(data + index + 1, data + index, (dim - index) * sizeof(*data));
data[index] = ptr;
dim++;
}
void Array::insert(unsigned index, Array *a)
{
if (a)
{ unsigned d;
d = a->dim;
reserve(d);
if (dim != index)
memmove(data + index + d, data + index, (dim - index) * sizeof(*data));
memcpy(data + index, a->data, d * sizeof(*data));
dim += d;
}
}
/***********************************
* Append array a to this array.
*/
void Array::append(Array *a)
{
insert(dim, a);
}
void Array::remove(unsigned i)
{
if (dim - i - 1)
memmove(data + i, data + i + 1, (dim - i - 1) * sizeof(data[0]));
dim--;
}
char *Array::toChars()
{
unsigned len;
unsigned u;
char **buf;
char *str;
char *p;
buf = (char **)malloc(dim * sizeof(char *));
assert(buf);
len = 2;
for (u = 0; u < dim; u++)
{
buf[u] = ((Object *)data[u])->toChars();
len += strlen(buf[u]) + 1;
}
str = (char *)mem.malloc(len);
str[0] = '[';
p = str + 1;
for (u = 0; u < dim; u++)
{
if (u)
*p++ = ',';
len = strlen(buf[u]);
memcpy(p,buf[u],len);
p += len;
}
*p++ = ']';
*p = 0;
free(buf);
return str;
}
void Array::zero()
{
memset(data,0,dim * sizeof(data[0]));
}
void *Array::tos()
{
return dim ? data[dim - 1] : NULL;
}
int
#if _WIN32
__cdecl
#endif
Array_sort_compare(const void *x, const void *y)
{
Object *ox = *(Object **)x;
Object *oy = *(Object **)y;
return ox->compare(oy);
}
void Array::sort()
{
if (dim)
{
qsort(data, dim, sizeof(Object *), Array_sort_compare);
}
}
Array *Array::copy()
{
Array *a = new Array();
a->setDim(dim);
memcpy(a->data, data, dim * sizeof(void *));
return a;
}

View File

@@ -1,325 +0,0 @@
#define _MT 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#if _WIN32
#include <windows.h>
#include <errno.h>
#include <process.h>
#include "root.h"
static unsigned __stdcall startthread(void *p);
struct FileData
{
File *file;
int result;
HANDLE event;
};
struct AsyncRead
{
static AsyncRead *create(size_t nfiles);
void addFile(File *file);
void start();
int read(size_t i);
static void dispose(AsyncRead *);
HANDLE hThread;
size_t filesdim;
size_t filesmax;
FileData files[1];
};
AsyncRead *AsyncRead::create(size_t nfiles)
{
AsyncRead *aw = (AsyncRead *)calloc(1, sizeof(AsyncRead) +
(nfiles - 1) * sizeof(FileData));
aw->filesmax = nfiles;
return aw;
}
void AsyncRead::addFile(File *file)
{
//printf("addFile(file = %p)\n", file);
//printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax);
assert(filesdim < filesmax);
files[filesdim].file = file;
files[filesdim].event = CreateEvent(NULL, TRUE, FALSE, NULL);
ResetEvent(files[filesdim].event);
filesdim++;
}
void AsyncRead::start()
{
//printf("aw->filesdim = %p %d\n", this, filesdim);
if (filesdim)
{
unsigned threadaddr;
hThread = (HANDLE) _beginthreadex(NULL,
0,
&startthread,
this,
0,
(unsigned *)&threadaddr);
if (hThread)
{
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
}
else
{
assert(0);
}
}
}
int AsyncRead::read(size_t i)
{
FileData *f = &files[i];
WaitForSingleObject(f->event, INFINITE);
Sleep(0); // give up time slice
return f->result;
}
void AsyncRead::dispose(AsyncRead *aw)
{
free(aw);
}
unsigned __stdcall startthread(void *p)
{
AsyncRead *aw = (AsyncRead *)p;
//printf("aw->filesdim = %p %d\n", aw, aw->filesdim);
for (size_t i = 0; i < aw->filesdim; i++)
{ FileData *f = &aw->files[i];
f->result = f->file->read();
SetEvent(f->event);
}
_endthreadex(EXIT_SUCCESS);
return EXIT_SUCCESS; // if skidding
}
#elif linux // Posix
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include "root.h"
void *startthread(void *arg);
void err_abort(int status, const char *msg)
{
fprintf(stderr, "fatal error = %d, %s\n", status, msg);
exit(EXIT_FAILURE);
}
struct FileData
{
File *file;
int result;
pthread_mutex_t mutex;
pthread_cond_t cond;
int value;
};
struct AsyncRead
{
static AsyncRead *create(size_t nfiles);
void addFile(File *file);
void start();
int read(size_t i);
static void dispose(AsyncRead *);
size_t filesdim;
size_t filesmax;
FileData files[1];
};
AsyncRead *AsyncRead::create(size_t nfiles)
{
AsyncRead *aw = (AsyncRead *)calloc(1, sizeof(AsyncRead) +
(nfiles - 1) * sizeof(FileData));
aw->filesmax = nfiles;
return aw;
}
void AsyncRead::addFile(File *file)
{
//printf("addFile(file = %p)\n", file);
//printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax);
assert(filesdim < filesmax);
FileData *f = &files[filesdim];
f->file = file;
int status = pthread_mutex_init(&f->mutex, NULL);
if (status != 0)
err_abort(status, "init mutex");
status = pthread_cond_init(&f->cond, NULL);
if (status != 0)
err_abort(status, "init cond");
filesdim++;
}
void AsyncRead::start()
{
//printf("aw->filesdim = %p %d\n", this, filesdim);
if (filesdim)
{
pthread_t thread_id;
int status = pthread_create(&thread_id,
NULL,
&startthread,
this);
if (status != 0)
err_abort(status, "create thread");
}
}
int AsyncRead::read(size_t i)
{
FileData *f = &files[i];
// Wait for the event
int status = pthread_mutex_lock(&f->mutex);
if (status != 0)
err_abort(status, "lock mutex");
while (f->value == 0)
{
status = pthread_cond_wait(&f->cond, &f->mutex);
if (status != 0)
err_abort(status, "wait on condition");
}
status = pthread_mutex_unlock(&f->mutex);
if (status != 0)
err_abort(status, "unlock mutex");
return f->result;
}
void AsyncRead::dispose(AsyncRead *aw)
{
//printf("AsyncRead::dispose()\n");
for (int i = 0; i < aw->filesdim; i++)
{
FileData *f = &aw->files[i];
int status = pthread_cond_destroy(&f->cond);
if (status != 0)
err_abort(status, "cond destroy");
status = pthread_mutex_destroy(&f->mutex);
if (status != 0)
err_abort(status, "mutex destroy");
}
free(aw);
}
void *startthread(void *p)
{
AsyncRead *aw = (AsyncRead *)p;
//printf("startthread: aw->filesdim = %p %d\n", aw, aw->filesdim);
size_t dim = aw->filesdim;
for (size_t i = 0; i < dim; i++)
{ FileData *f = &aw->files[i];
f->result = f->file->read();
// Set event
int status = pthread_mutex_lock(&f->mutex);
if (status != 0)
err_abort(status, "lock mutex");
f->value = 1;
status = pthread_cond_signal(&f->cond);
if (status != 0)
err_abort(status, "signal condition");
status = pthread_mutex_unlock(&f->mutex);
if (status != 0)
err_abort(status, "unlock mutex");
}
return NULL; // end thread
}
#else
#include <stdio.h>
#include <errno.h>
#include "root.h"
struct FileData
{
File *file;
int result;
//HANDLE event;
};
struct AsyncRead
{
static AsyncRead *create(size_t nfiles);
void addFile(File *file);
void start();
int read(size_t i);
static void dispose(AsyncRead *);
//HANDLE hThread;
size_t filesdim;
size_t filesmax;
FileData files[1];
};
AsyncRead *AsyncRead::create(size_t nfiles)
{
AsyncRead *aw = (AsyncRead *)calloc(1, sizeof(AsyncRead) +
(nfiles - 1) * sizeof(FileData));
aw->filesmax = nfiles;
return aw;
}
void AsyncRead::addFile(File *file)
{
//printf("addFile(file = %p)\n", file);
//printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax);
assert(filesdim < filesmax);
files[filesdim].file = file;
//files[filesdim].event = CreateEvent(NULL, TRUE, FALSE, NULL);
//ResetEvent(files[filesdim].event);
filesdim++;
}
void AsyncRead::start()
{
}
int AsyncRead::read(size_t i)
{
FileData *f = &files[i];
f->result = f->file->read();
return f->result;
}
void AsyncRead::dispose(AsyncRead *aw)
{
free(aw);
}
#endif

View File

@@ -1,33 +0,0 @@
// Copyright (c) 2009-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef ASYNC_H
#define ASYNC_H
#if __DMC__
#pragma once
#endif
/*******************
* Simple interface to read files asynchronously in another
* thread.
*/
struct AsyncRead
{
static AsyncRead *create(size_t nfiles);
void addFile(File *file);
void start();
int read(size_t i);
static void dispose(AsyncRead *);
};
#endif

View File

@@ -1,55 +0,0 @@
// Put functions in here missing from gnu C
#include "gnuc.h"
int memicmp(const char *s1, const char *s2, int n)
{
int result = 0;
for (int i = 0; i < n; i++)
{ char c1 = s1[i];
char c2 = s2[i];
result = c1 - c2;
if (result)
{
if ('A' <= c1 && c1 <= 'Z')
c1 += 'a' - 'A';
if ('A' <= c2 && c2 <= 'Z')
c2 += 'a' - 'A';
result = c1 - c2;
if (result)
break;
}
}
return result;
}
int stricmp(const char *s1, const char *s2)
{
int result = 0;
for (;;)
{ char c1 = *s1;
char c2 = *s2;
result = c1 - c2;
if (result)
{
if ('A' <= c1 && c1 <= 'Z')
c1 += 'a' - 'A';
if ('A' <= c2 && c2 <= 'Z')
c2 += 'a' - 'A';
result = c1 - c2;
if (result)
break;
}
if (!c1)
break;
s1++;
s2++;
}
return result;
}

View File

@@ -1,8 +0,0 @@
#ifndef _GNUC_H
#define _GNUC_H 1
int memicmp(const char *s1, const char *s2, int n);
int stricmp(const char *s1, const char *s2);
#endif

View File

@@ -1,636 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Rainer Schuetze
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// 80 bit floating point value implementation for Microsoft compiler
#if _MSC_VER
#include "longdouble.h"
#include "assert.h"
#include <float.h>
#include <stdio.h>
#include <string.h>
extern "C"
{
// implemented in ldfpu.asm for _WIN64
int ld_initfpu(int bits, int mask);
void ld_expl(longdouble* ld, int exp);
longdouble ld_add(longdouble ld1, longdouble ld2);
longdouble ld_sub(longdouble ld1, longdouble ld2);
longdouble ld_mul(longdouble ld1, longdouble ld2);
longdouble ld_div(longdouble ld1, longdouble ld2);
longdouble ld_mod(longdouble ld1, longdouble ld2);
bool ld_cmpb(longdouble ld1, longdouble ld2);
bool ld_cmpbe(longdouble ld1, longdouble ld2);
bool ld_cmpa(longdouble ld1, longdouble ld2);
bool ld_cmpae(longdouble ld1, longdouble ld2);
bool ld_cmpe(longdouble ld1, longdouble ld2);
bool ld_cmpne(longdouble ld1, longdouble ld2);
longdouble ld_sqrt(longdouble ld1);
longdouble ld_sin(longdouble ld1);
longdouble ld_cos(longdouble ld1);
longdouble ld_tan(longdouble ld1);
}
bool initFPU()
{
#ifdef _WIN64
// int old_cw = ld_initfpu(_RC_NEAR);
int old_cw = ld_initfpu(0x300 /*_PC_64 | _RC_NEAR*/, // #defines NOT identical to CPU FPU control word!
0xF00 /*_MCW_PC | _MCW_RC*/);
#else
int old_cw = _control87(_MCW_EM | _PC_64 | _RC_NEAR,
_MCW_EM | _MCW_PC | _MCW_RC);
#endif
return true;
}
static bool doInitFPU = initFPU();
#ifndef _WIN64
extern "C"
{
double ld_read(const longdouble* pthis)
{
double res;
__asm
{
mov eax, pthis
fld tbyte ptr [eax]
fstp res
}
return res;
}
long long ld_readll(const longdouble* pthis)
{
#if 1
return ld_readull(pthis);
#elif defined _WIN64
return ld_readll(this);
#else
longdouble* pthis = this;
long long res;
__asm
{
mov eax, pthis
fld tbyte ptr [eax]
fistp qword ptr res
}
return res;
#endif
}
unsigned long long ld_readull(const longdouble* pthis)
{
#if 1
// somehow the FPU does not respect the CHOP mode of the rounding control
// in 64-bit mode
// so we roll our own conversion (it also allows the usual C wrap-around
// instead of the "invalid value" created by the FPU)
int expo = pthis->exponent - 0x3fff;
unsigned long long u;
if(expo < 0 || expo > 127)
return 0;
if(expo < 64)
u = pthis->mantissa >> (63 - expo);
else
u = pthis->mantissa << (expo - 63);
if(pthis->sign)
u = ~u + 1;
return u;
#else
longdouble* pthis = this;
long long res; // cannot use unsigned, VC will not generate "fistp qword"
longdouble twoPow63 = { 1ULL << 63, 0x3fff + 63, 0 };
__asm
{
mov eax, pthis
fld tbyte ptr [eax]
fld tbyte ptr twoPow63
fsubp ST(1),ST(0) // move it into signed range
lea eax, res
fistp qword ptr [eax]
}
res ^= (1LL << 63);
return res;
#endif
}
void ld_set(longdouble* pthis, double d)
{
__asm
{
mov eax, pthis
fld d
fstp tbyte ptr [eax]
}
}
void ld_setll(longdouble* pthis, long long d)
{
__asm
{
fild qword ptr d
mov eax, pthis
fstp tbyte ptr [eax]
}
}
void ld_setull(longdouble* pthis, unsigned long long d)
{
d ^= (1LL << 63);
longdouble twoPow63 = { 1ULL << 63, 0x3fff + 63, 0 };
__asm
{
fild qword ptr d
fld tbyte ptr twoPow63
faddp ST(1),ST(0)
mov eax, pthis
fstp tbyte ptr [eax]
}
}
} // extern "C"
#endif // !_WIN64
longdouble ldexpl(longdouble ld, int exp)
{
#ifdef _WIN64
ld_expl(&ld, exp);
#else
__asm
{
fild dword ptr exp
fld tbyte ptr ld
fscale // ST(0) = ST(0) * (2**ST(1))
fstp ST(1)
fstp tbyte ptr ld
}
#endif
return ld;
}
///////////////////////////////////////////////////////////////////////
longdouble operator+(longdouble ld1, longdouble ld2)
{
#ifdef _WIN64
return ld_add(ld1, ld2);
#else
longdouble res;
__asm
{
fld tbyte ptr ld1
fld tbyte ptr ld2
fadd
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble operator-(longdouble ld1, longdouble ld2)
{
#ifdef _WIN64
return ld_sub(ld1, ld2);
#else
longdouble res;
__asm
{
fld tbyte ptr ld1
fld tbyte ptr ld2
fsub
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble operator*(longdouble ld1, longdouble ld2)
{
#ifdef _WIN64
return ld_mul(ld1, ld2);
#else
longdouble res;
__asm
{
fld tbyte ptr ld1
fld tbyte ptr ld2
fmul
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble operator/(longdouble ld1, longdouble ld2)
{
#ifdef _WIN64
return ld_div(ld1, ld2);
#else
longdouble res;
__asm
{
fld tbyte ptr ld1
fld tbyte ptr ld2
fdiv
fstp tbyte ptr res;
}
return res;
#endif
}
bool operator< (longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpb(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
setb AL
setnp AH
and AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
bool operator<=(longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpbe(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
setbe AL
setnp AH
and AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
bool operator> (longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpa(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
seta AL
setnp AH
and AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
bool operator>=(longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpae(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
setae AL
setnp AH
and AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
bool operator==(longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpe(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
sete AL
setnp AH
and AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
bool operator!=(longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_cmpne(x, y);
#else
short sw;
bool res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
fucomip ST(0),ST(1)
setne AL
setp AH
or AL,AH
mov res,AL
fstp ST(0)
}
return res;
#endif
}
int _isnan(longdouble ld)
{
return (ld.exponent == 0x7fff && ld.mantissa != 0);
}
longdouble fabsl(longdouble ld)
{
ld.sign = 0;
return ld;
}
longdouble sqrtl(longdouble ld)
{
#ifdef _WIN64
return ld_sqrt(ld);
#else
longdouble res;
__asm
{
fld tbyte ptr ld;
fsqrt;
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble sinl (longdouble ld)
{
#ifdef _WIN64
return ld_sin(ld);
#else
longdouble res;
__asm
{
fld tbyte ptr ld;
fsin; // exact for |x|<=PI/4
fstp tbyte ptr res
}
return res;
#endif
}
longdouble cosl (longdouble ld)
{
#ifdef _WIN64
return ld_cos(ld);
#else
longdouble res;
__asm
{
fld tbyte ptr ld;
fcos; // exact for |x|<=PI/4
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble tanl (longdouble ld)
{
#ifdef _WIN64
return ld_tan(ld);
#else
longdouble res;
__asm
{
fld tbyte ptr ld;
fptan;
fstp ST(0); // always 1
fstp tbyte ptr res;
}
return res;
#endif
}
longdouble fmodl(longdouble x, longdouble y)
{
#ifdef _WIN64
return ld_mod(x, y);
#else
short sw;
longdouble res;
__asm
{
fld tbyte ptr y
fld tbyte ptr x // ST = x, ST1 = y
FM1: // We don't use fprem1 because for some inexplicable
// reason we get -5 when we do _modulo(15, 10)
fprem // ST = ST % ST1
fstsw word ptr sw
fwait
mov AH,byte ptr sw+1 // get msb of status word in AH
sahf // transfer to flags
jp FM1 // continue till ST < ST1
fstp ST(1) // leave remainder on stack
fstp tbyte ptr res;
}
return res;
#endif
}
//////////////////////////////////////////////////////////////
longdouble ld_qnan = { 0x8000000000000000ULL, 0x7fff, 0 };
longdouble ld_snan = { 0x0000000000000001ULL, 0x7fff, 0 };
longdouble ld_inf = { 0x0000000000000000ULL, 0x7fff, 0 };
longdouble ld_zero = { 0, 0, 0 };
longdouble ld_one = { 0x8000000000000000ULL, 0x3fff, 0 };
longdouble ld_pi = { 0xc90fdaa22168c235ULL, 0x4000, 0 };
longdouble ld_log2t = { 0xd49a784bcd1b8afeULL, 0x4000, 0 };
longdouble ld_log2e = { 0xb8aa3b295c17f0bcULL, 0x3fff, 0 };
longdouble ld_log2 = { 0x9a209a84fbcff799ULL, 0x3ffd, 0 };
longdouble ld_ln2 = { 0xb17217f7d1cf79acULL, 0x3ffe, 0 };
longdouble ld_pi2 = ld_pi*2;
longdouble ld_piOver2 = ld_pi*0.5;
longdouble ld_piOver4 = ld_pi*0.25;
//////////////////////////////////////////////////////////////
#define LD_TYPE_OTHER 0
#define LD_TYPE_ZERO 1
#define LD_TYPE_INFINITE 2
#define LD_TYPE_SNAN 3
#define LD_TYPE_QNAN 4
int ld_type(longdouble x)
{
if(x.exponent == 0)
return x.mantissa == 0 ? LD_TYPE_ZERO : LD_TYPE_OTHER; // dnormal if not zero
if(x.exponent != 0x7fff)
return LD_TYPE_OTHER;
if(x.mantissa == 0)
return LD_TYPE_INFINITE;
if(x.mantissa & (1LL << 63))
return LD_TYPE_QNAN;
return LD_TYPE_SNAN;
}
int ld_sprint(char* str, int fmt, longdouble x)
{
// fmt is 'a','A','f' or 'g'
if(fmt != 'a' && fmt != 'A')
{
char format[] = { '%', fmt, 0 };
return sprintf(str, format, ld_read(&x));
}
unsigned short exp = x.exponent;
unsigned long long mantissa = x.mantissa;
switch(ld_type(x))
{
case LD_TYPE_ZERO:
return sprintf(str, "0x0.0L");
case LD_TYPE_QNAN:
case LD_TYPE_SNAN:
return sprintf(str, "NAN");
case LD_TYPE_INFINITE:
return sprintf(str, x.sign ? "-INF" : "INF");
}
int len = 0;
if(x.sign)
str[len++] = '-';
len += sprintf(str + len, mantissa & (1LL << 63) ? "0x1." : "0x0.");
mantissa = mantissa << 1;
while(mantissa)
{
int dig = (mantissa >> 60) & 0xf;
dig += dig < 10 ? '0' : fmt - 10;
str[len++] = dig;
mantissa = mantissa << 4;
}
str[len++] = 'p';
if(exp < 0x3fff)
{
str[len++] = '-';
exp = 0x3fff - exp;
}
else
{
str[len++] = '+';
exp = exp - 0x3fff;
}
int exppos = len;
for(int i = 12; i >= 0; i -= 4)
{
int dig = (exp >> i) & 0xf;
if(dig != 0 || len > exppos || i == 0)
str[len++] = dig + (dig < 10 ? '0' : fmt - 10);
}
str[len] = 0;
return len;
}
//////////////////////////////////////////////////////////////
#if UNITTEST
static bool unittest()
{
char buffer[32];
ld_sprint(buffer, 'a', ld_pi);
assert(strcmp(buffer, "0x1.921fb54442d1846ap+1") == 0);
longdouble ldb = ldouble(0.4);
long long b = ldb;
assert(b == 0);
b = ldouble(0.9);
assert(b == 0);
long long x = 0x12345678abcdef78LL;
longdouble ldx = ldouble(x);
assert(ldx > 0);
long long y = ldx;
assert(x == y);
x = -0x12345678abcdef78LL;
ldx = ldouble(x);
assert(ldx < 0);
y = ldx;
assert(x == y);
unsigned long long u = 0x12345678abcdef78LL;
longdouble ldu = ldouble(u);
assert(ldu > 0);
unsigned long long v = ldu;
assert(u == v);
u = 0xf234567812345678ULL;
ldu = ldouble(u);
assert(ldu > 0);
v = ldu;
assert(u == v);
u = 0xf2345678;
ldu = ldouble(u);
ldu = ldu * ldu;
ldu = sqrt(ldu);
v = ldu;
assert(u == v);
u = 0x123456789A;
ldu = ldouble(u);
ldu = ldu * (1LL << 23);
v = ldu;
u = u * (1LL << 23);
assert(u == v);
return true;
}
static bool runUnittest = unittest();
#endif // UNITTEST
#endif // _MSC_VER

View File

@@ -1,254 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Rainer Schuetze
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// 80 bit floating point value implementation for Microsoft compiler
#ifndef __LONG_DOUBLE_H__
#define __LONG_DOUBLE_H__
#if IN_GCC
#include "d-gcc-real.h"
typedef real_t longdouble;
template<typename T> longdouble ldouble(T x) { return (longdouble) x; }
inline int ld_sprint(char* str, int fmt, longdouble x)
{
if(fmt == 'a' || fmt == 'A')
return x.formatHex(buffer, 46); // don't know the size here, but 46 is the max
return x.format(buffer, 46);
}
#elif !_MSC_VER // has native 10 byte doubles
#include <stdio.h>
typedef long double longdouble;
typedef volatile long double volatile_longdouble;
// also used from within C code, so use a #define rather than a template
// template<typename T> longdouble ldouble(T x) { return (longdouble) x; }
#define ldouble(x) ((longdouble)(x))
inline int ld_sprint(char* str, int fmt, longdouble x)
{
char sfmt[4] = "%Lg";
sfmt[2] = fmt;
return sprintf(str, sfmt, x);
}
#else
#include <float.h>
#include <limits>
struct longdouble;
extern "C"
{
// implemented in ldfpu.asm for _WIN64
double ld_read(const longdouble* ld);
long long ld_readll(const longdouble* ld);
unsigned long long ld_readull(const longdouble* ld);
void ld_set(longdouble* ld, double d);
void ld_setll(longdouble* ld, long long d);
void ld_setull(longdouble* ld, unsigned long long d);
}
struct longdouble
{
unsigned long long mantissa;
unsigned short exponent:15; // bias 0x3fff
unsigned short sign:1;
unsigned short fill:16; // for 12 byte alignment
// no constructor to be able to use this class in a union
// use ldouble() to explicitely create a longdouble value
template<typename T> longdouble& operator=(T x) { set(x); return *this; }
void set(longdouble ld) { mantissa = ld.mantissa; exponent = ld.exponent; sign = ld.sign; }
// we need to list all basic types to avoid ambiguities
void set(float d) { ld_set(this, d); }
void set(double d) { ld_set(this, d); }
void set(long double d) { ld_set(this, d); }
void set(signed char d) { ld_set(this, d); }
void set(short d) { ld_set(this, d); }
void set(int d) { ld_set(this, d); }
void set(long d) { ld_set(this, d); }
void set(long long d) { ld_setll(this, d); }
void set(unsigned char d) { ld_set(this, d); }
void set(unsigned short d) { ld_set(this, d); }
void set(unsigned int d) { ld_set(this, d); }
void set(unsigned long d) { ld_set(this, d); }
void set(unsigned long long d) { ld_setull(this, d); }
void set(bool d) { ld_set(this, d); }
operator float () { return ld_read(this); }
operator double () { return ld_read(this); }
operator signed char () { return ld_read(this); }
operator short () { return ld_read(this); }
operator int () { return ld_read(this); }
operator long () { return ld_read(this); }
operator long long () { return ld_readll(this); }
operator unsigned char () { return ld_read(this); }
operator unsigned short () { return ld_read(this); }
operator unsigned int () { return ld_read(this); }
operator unsigned long () { return ld_read(this); }
operator unsigned long long() { return ld_readull(this); }
operator bool () { return mantissa != 0 || exponent != 0; } // correct?
};
// some optimizations are avoided by adding volatile to the longdouble
// type, but this introduces bad ambiguities when using the class implementation above
// as we are going through asm these optimizations won't kick in anyway, so "volatile"
// is not required.
typedef longdouble volatile_longdouble;
inline longdouble ldouble(unsigned long long mantissa, int exp, int sign = 0)
{
longdouble d;
d.mantissa = mantissa;
d.exponent = exp;
d.sign = sign;
return d;
}
template<typename T> inline longdouble ldouble(T x) { longdouble d; d.set(x); return d; }
//template<typename T> inline longdouble ldouble(volatile T x) { longdouble d; d.set(x); return d; }
longdouble operator+(longdouble ld1, longdouble ld2);
longdouble operator-(longdouble ld1, longdouble ld2);
longdouble operator*(longdouble ld1, longdouble ld2);
longdouble operator/(longdouble ld1, longdouble ld2);
bool operator< (longdouble ld1, longdouble ld2);
bool operator<=(longdouble ld1, longdouble ld2);
bool operator> (longdouble ld1, longdouble ld2);
bool operator>=(longdouble ld1, longdouble ld2);
bool operator==(longdouble ld1, longdouble ld2);
bool operator!=(longdouble ld1, longdouble ld2);
inline longdouble operator-(longdouble ld1) { ld1.sign ^= 1; return ld1; }
inline longdouble operator+(longdouble ld1) { return ld1; }
template<typename T> inline longdouble operator+(longdouble ld, T x) { return ld + ldouble(x); }
template<typename T> inline longdouble operator-(longdouble ld, T x) { return ld - ldouble(x); }
template<typename T> inline longdouble operator*(longdouble ld, T x) { return ld * ldouble(x); }
template<typename T> inline longdouble operator/(longdouble ld, T x) { return ld / ldouble(x); }
template<typename T> inline longdouble operator+(T x, longdouble ld) { return ldouble(x) + ld; }
template<typename T> inline longdouble operator-(T x, longdouble ld) { return ldouble(x) - ld; }
template<typename T> inline longdouble operator*(T x, longdouble ld) { return ldouble(x) * ld; }
template<typename T> inline longdouble operator/(T x, longdouble ld) { return ldouble(x) / ld; }
template<typename T> inline longdouble& operator+=(longdouble& ld, T x) { return ld = ld + x; }
template<typename T> inline longdouble& operator-=(longdouble& ld, T x) { return ld = ld - x; }
template<typename T> inline longdouble& operator*=(longdouble& ld, T x) { return ld = ld * x; }
template<typename T> inline longdouble& operator/=(longdouble& ld, T x) { return ld = ld / x; }
template<typename T> inline bool operator< (longdouble ld, T x) { return ld < ldouble(x); }
template<typename T> inline bool operator<=(longdouble ld, T x) { return ld <= ldouble(x); }
template<typename T> inline bool operator> (longdouble ld, T x) { return ld > ldouble(x); }
template<typename T> inline bool operator>=(longdouble ld, T x) { return ld >= ldouble(x); }
template<typename T> inline bool operator==(longdouble ld, T x) { return ld == ldouble(x); }
template<typename T> inline bool operator!=(longdouble ld, T x) { return ld != ldouble(x); }
template<typename T> inline bool operator< (T x, longdouble ld) { return ldouble(x) < ld; }
template<typename T> inline bool operator<=(T x, longdouble ld) { return ldouble(x) <= ld; }
template<typename T> inline bool operator> (T x, longdouble ld) { return ldouble(x) > ld; }
template<typename T> inline bool operator>=(T x, longdouble ld) { return ldouble(x) >= ld; }
template<typename T> inline bool operator==(T x, longdouble ld) { return ldouble(x) == ld; }
template<typename T> inline bool operator!=(T x, longdouble ld) { return ldouble(x) != ld; }
int _isnan(longdouble ld);
longdouble fabsl(longdouble ld);
longdouble sqrtl(longdouble ld);
longdouble sinl (longdouble ld);
longdouble cosl (longdouble ld);
longdouble tanl (longdouble ld);
longdouble fmodl(longdouble x, longdouble y);
longdouble ldexpl(longdouble ldval, int exp); // see strtold
inline longdouble fabs (longdouble ld) { return fabsl(ld); }
inline longdouble sqrt (longdouble ld) { return sqrtl(ld); }
#undef LDBL_DIG
#undef LDBL_MAX
#undef LDBL_MIN
#undef LDBL_EPSILON
#undef LDBL_MANT_DIG
#undef LDBL_MAX_EXP
#undef LDBL_MIN_EXP
#undef LDBL_MAX_10_EXP
#undef LDBL_MIN_10_EXP
#define LDBL_DIG 18
#define LDBL_MAX ldouble(0xffffffffffffffffULL, 0x7ffe)
#define LDBL_MIN ldouble(0x8000000000000000ULL, 1)
#define LDBL_EPSILON ldouble(0x8000000000000000ULL, 0x3fff - 63) // allow denormal?
#define LDBL_MANT_DIG 64
#define LDBL_MAX_EXP 16384
#define LDBL_MIN_EXP (-16381)
#define LDBL_MAX_10_EXP 4932
#define LDBL_MIN_10_EXP (-4932)
extern longdouble ld_zero;
extern longdouble ld_one;
extern longdouble ld_pi;
extern longdouble ld_log2t;
extern longdouble ld_log2e;
extern longdouble ld_log2;
extern longdouble ld_ln2;
extern longdouble ld_inf;
extern longdouble ld_qnan;
extern longdouble ld_snan;
///////////////////////////////////////////////////////////////////////
// CLASS numeric_limits<longdouble>
template<> class _CRTIMP2_PURE std::numeric_limits<longdouble>
: public _Num_float_base
{ // limits for type long double
public:
typedef longdouble _Ty;
static _Ty (__CRTDECL min)() _THROW0() { return LDBL_MIN; }
static _Ty (__CRTDECL max)() _THROW0() { return LDBL_MAX; }
static _Ty __CRTDECL epsilon() _THROW0() { return LDBL_EPSILON; }
static _Ty __CRTDECL round_error() _THROW0() { return ldouble(0.5); }
static _Ty __CRTDECL denorm_min() _THROW0() { return ldouble(0x0000000000000001ULL, 1); }
static _Ty __CRTDECL infinity() _THROW0() { return ld_inf; }
static _Ty __CRTDECL quiet_NaN() _THROW0() { return ld_qnan; }
static _Ty __CRTDECL signaling_NaN() _THROW0() { return ld_snan; }
_STCONS(int, digits, LDBL_MANT_DIG);
_STCONS(int, digits10, LDBL_DIG);
_STCONS(int, max_exponent, (int)LDBL_MAX_EXP);
_STCONS(int, max_exponent10, (int)LDBL_MAX_10_EXP);
_STCONS(int, min_exponent, (int)LDBL_MIN_EXP);
_STCONS(int, min_exponent10, (int)LDBL_MIN_10_EXP);
};
//_STCONSDEF(numeric_limits<longdouble>, int, digits)
//_STCONSDEF(numeric_limits<longdouble>, int, digits10)
//_STCONSDEF(numeric_limits<longdouble>, int, max_exponent)
//_STCONSDEF(numeric_limits<longdouble>, int, max_exponent10)
//_STCONSDEF(numeric_limits<longdouble>, int, min_exponent)
//_STCONSDEF(numeric_limits<longdouble>, int, min_exponent10)
int ld_sprint(char* str, int fmt, longdouble x);
#endif // !_MSC_VER
#endif // __LONG_DOUBLE_H__

View File

@@ -1,100 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 2008-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#if _WIN32
#include <windows.h>
#pragma comment(lib,"shell32.lib")
void browse(const char *url)
{
ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
}
#endif
#if linux || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void browse(const char *url)
{
pid_t childpid;
const char *args[3];
const char *browser = getenv("BROWSER");
if (browser)
browser = strdup(browser);
else
browser = "x-www-browser";
args[0] = browser;
args[1] = url;
args[2] = NULL;
childpid = fork();
if (childpid == 0)
{
execvp(args[0], (char**)args);
perror(args[0]); // failed to execute
return;
}
}
#endif
#if __APPLE__
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void browse(const char *url)
{
pid_t childpid;
const char *args[5];
char *browser = getenv("BROWSER");
if (browser)
{ browser = strdup(browser);
args[0] = browser;
args[1] = url;
args[2] = NULL;
}
else
{
//browser = "/Applications/Safari.app/Contents/MacOS/Safari";
args[0] = "open";
args[1] = "-a";
args[2] = "/Applications/Safari.app";
args[3] = url;
args[4] = NULL;
}
childpid = fork();
if (childpid == 0)
{
execvp(args[0], (char**)args);
perror(args[0]); // failed to execute
return;
}
}
#endif

View File

@@ -1,663 +0,0 @@
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
#include "port.h"
#if __DMC__
#include <math.h>
#include <float.h>
#include <fp.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
double Port::nan = NAN;
double Port::infinity = INFINITY;
double Port::dbl_max = DBL_MAX;
double Port::dbl_min = DBL_MIN;
longdouble Port::ldbl_max = LDBL_MAX;
int Port::isNan(double r)
{
return ::isnan(r);
}
int Port::isNan(longdouble r)
{
return ::isnan(r);
}
int Port::isSignallingNan(double r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 51 of 0..63 for 64 bit doubles.
*/
return isNan(r) && !((((unsigned char*)&r)[6]) & 8);
}
int Port::isSignallingNan(longdouble r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 62 of 0..79 for 80 bit reals.
*/
return isNan(r) && !((((unsigned char*)&r)[7]) & 0x40);
}
int Port::isFinite(double r)
{
return ::isfinite(r);
}
int Port::isInfinity(double r)
{
return (::fpclassify(r) == FP_INFINITE);
}
int Port::Signbit(double r)
{
return ::signbit(r);
}
double Port::floor(double d)
{
return ::floor(d);
}
double Port::pow(double x, double y)
{
return ::pow(x, y);
}
longdouble Port::fmodl(longdouble x, longdouble y)
{
return ::fmodl(x, y);
}
unsigned long long Port::strtoull(const char *p, char **pend, int base)
{
return ::strtoull(p, pend, base);
}
char *Port::ull_to_string(char *buffer, ulonglong ull)
{
sprintf(buffer, "%llu", ull);
return buffer;
}
wchar_t *Port::ull_to_string(wchar_t *buffer, ulonglong ull)
{
swprintf(buffer, sizeof(ulonglong) * 3 + 1, L"%llu", ull);
return buffer;
}
double Port::ull_to_double(ulonglong ull)
{
return (double) ull;
}
const char *Port::list_separator()
{
// LOCALE_SLIST for Windows
return ",";
}
const wchar_t *Port::wlist_separator()
{
// LOCALE_SLIST for Windows
return L",";
}
char *Port::strupr(char *s)
{
return ::strupr(s);
}
#endif
#if _MSC_VER
// Disable useless warnings about unreferenced functions
#pragma warning (disable : 4514)
#include <math.h>
#include <float.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits> // for std::numeric_limits
static unsigned long nanarray[2]= { 0xFFFFFFFF, 0x7FFFFFFF };
//static unsigned long nanarray[2] = {0,0x7FF80000 };
double Port::nan = (*(double *)nanarray);
//static unsigned long infinityarray[2] = {0,0x7FF00000 };
static double zero = 0;
double Port::infinity = 1 / zero;
double Port::dbl_max = DBL_MAX;
double Port::dbl_min = DBL_MIN;
longdouble Port::ldbl_max = LDBL_MAX;
struct PortInitializer
{
PortInitializer();
};
static PortInitializer portinitializer;
PortInitializer::PortInitializer()
{
Port::infinity = std::numeric_limits<double>::infinity();
}
int Port::isNan(double r)
{
return ::_isnan(r);
}
int Port::isNan(longdouble r)
{
return ::_isnan(r);
}
int Port::isSignallingNan(double r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 51 of 0..63 for 64 bit doubles.
*/
return isNan(r) && !((((unsigned char*)&r)[6]) & 8);
}
int Port::isSignallingNan(longdouble r)
{
/* MSVC doesn't have 80 bit long doubles
*/
return isSignallingNan((double) r);
}
int Port::isFinite(double r)
{
return ::_finite(r);
}
int Port::isInfinity(double r)
{
return (::_fpclass(r) & (_FPCLASS_NINF | _FPCLASS_PINF));
}
int Port::Signbit(double r)
{
return (long)(((long *)&(r))[1] & 0x80000000);
}
double Port::floor(double d)
{
return ::floor(d);
}
double Port::pow(double x, double y)
{
if (y == 0)
return 1; // even if x is NAN
return ::pow(x, y);
}
longdouble Port::fmodl(longdouble x, longdouble y)
{
return ::fmodl(x, y);
}
unsigned _int64 Port::strtoull(const char *p, char **pend, int base)
{
unsigned _int64 number = 0;
int c;
int error;
#ifndef ULLONG_MAX
#define ULLONG_MAX ((unsigned _int64)~0I64)
#endif
while (isspace((unsigned char)*p)) /* skip leading white space */
p++;
if (*p == '+')
p++;
switch (base)
{ case 0:
base = 10; /* assume decimal base */
if (*p == '0')
{ base = 8; /* could be octal */
p++;
switch (*p)
{ case 'x':
case 'X':
base = 16; /* hex */
p++;
break;
#if BINARY
case 'b':
case 'B':
base = 2; /* binary */
p++;
break;
#endif
}
}
break;
case 16: /* skip over '0x' and '0X' */
if (*p == '0' && (p[1] == 'x' || p[1] == 'X'))
p += 2;
break;
#if BINARY
case 2: /* skip over '0b' and '0B' */
if (*p == '0' && (p[1] == 'b' || p[1] == 'B'))
p += 2;
break;
#endif
}
error = 0;
for (;;)
{ c = *p;
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c = (c & ~0x20) - ('A' - 10);
else /* unrecognized character */
break;
if (c >= base) /* not in number base */
break;
if ((ULLONG_MAX - c) / base < number)
error = 1;
number = number * base + c;
p++;
}
if (pend)
*pend = (char *)p;
if (error)
{ number = ULLONG_MAX;
errno = ERANGE;
}
return number;
}
char *Port::ull_to_string(char *buffer, ulonglong ull)
{
_ui64toa(ull, buffer, 10);
return buffer;
}
wchar_t *Port::ull_to_string(wchar_t *buffer, ulonglong ull)
{
_ui64tow(ull, buffer, 10);
return buffer;
}
double Port::ull_to_double(ulonglong ull)
{ double d;
if ((__int64) ull < 0)
{
// MSVC doesn't implement the conversion
d = (double) (__int64)(ull - 0x8000000000000000i64);
d += (double)(signed __int64)(0x7FFFFFFFFFFFFFFFi64) + 1.0;
}
else
d = (double)(__int64)ull;
return d;
}
const char *Port::list_separator()
{
// LOCALE_SLIST for Windows
return ",";
}
const wchar_t *Port::wlist_separator()
{
// LOCALE_SLIST for Windows
return L",";
}
char *Port::strupr(char *s)
{
return ::strupr(s);
}
#endif
#if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __MINGW32__ || __HAIKU__
#include <math.h>
#if linux
#include <bits/nan.h>
#include <bits/mathdef.h>
#endif
#if __FreeBSD__ && __i386__
#include <ieeefp.h>
#endif
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <assert.h>
static double zero = 0;
double Port::nan = copysign(NAN, 1.0);
double Port::infinity = 1 / zero;
double Port::dbl_max = 1.7976931348623157e308;
double Port::dbl_min = 5e-324;
longdouble Port::ldbl_max = LDBL_MAX;
struct PortInitializer
{
PortInitializer();
};
static PortInitializer portinitializer;
PortInitializer::PortInitializer()
{
assert(!signbit(Port::nan));
#if __FreeBSD__ && __i386__
// LDBL_MAX comes out as infinity. Fix.
static unsigned char x[sizeof(longdouble)] =
{ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x7F };
Port::ldbl_max = *(longdouble *)&x[0];
// FreeBSD defaults to double precision. Switch to extended precision.
fpsetprec(FP_PE);
#endif
}
int Port::isNan(double r)
{
#if __APPLE__
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
return __inline_isnand(r);
#else
return __inline_isnan(r);
#endif
#elif __OpenBSD__ || __MINGW32__ || __HAIKU__
return isnan(r);
#else
#undef isnan
return ::isnan(r);
#endif
}
int Port::isNan(longdouble r)
{
#if __APPLE__
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
return __inline_isnanl(r);
#else
return __inline_isnan(r);
#endif
#elif __OpenBSD__ || __MINGW32__ || __HAIKU__
return isnan(r);
#else
#undef isnan
return ::isnan(r);
#endif
}
int Port::isSignallingNan(double r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 51 of 0..63 for 64 bit doubles.
*/
return isNan(r) && !((((unsigned char*)&r)[6]) & 8);
}
int Port::isSignallingNan(longdouble r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 62 of 0..79 for 80 bit reals.
*/
return isNan(r) && !((((unsigned char*)&r)[7]) & 0x40);
}
#undef isfinite
int Port::isFinite(double r)
{
return ::finite(r);
}
int Port::isInfinity(double r)
{
#if __APPLE__
return fpclassify(r) == FP_INFINITE;
#elif __OpenBSD__ || __MINGW32__ || __HAIKU__
return isinf(r);
#else
#undef isinf
return ::isinf(r);
#endif
}
#undef signbit
int Port::Signbit(double r)
{
union { double d; long long ll; } u;
u.d = r;
return u.ll < 0;
}
double Port::floor(double d)
{
return ::floor(d);
}
double Port::pow(double x, double y)
{
return ::pow(x, y);
}
longdouble Port::fmodl(longdouble x, longdouble y)
{
#if __FreeBSD__ || __OpenBSD__
return ::fmod(x, y); // hack for now, fix later
#else
return ::fmodl(x, y);
#endif
}
unsigned long long Port::strtoull(const char *p, char **pend, int base)
{
return ::strtoull(p, pend, base);
}
char *Port::ull_to_string(char *buffer, ulonglong ull)
{
sprintf(buffer, "%llu", ull);
return buffer;
}
wchar_t *Port::ull_to_string(wchar_t *buffer, ulonglong ull)
{
#if __OpenBSD__
assert(0);
#else
#ifndef __MINGW32__
swprintf(buffer, sizeof(ulonglong) * 3 + 1, L"%llu", ull);
#else
_snwprintf(buffer, sizeof(ulonglong) * 3 + 1, L"%llu", ull);
#endif
#endif
return buffer;
}
double Port::ull_to_double(ulonglong ull)
{
return (double) ull;
}
const char *Port::list_separator()
{
return ",";
}
const wchar_t *Port::wlist_separator()
{
return L",";
}
char *Port::strupr(char *s)
{
char *t = s;
while (*s)
{
*s = toupper(*s);
s++;
}
return t;
}
#endif
#if __sun&&__SVR4
#define __C99FEATURES__ 1 // Needed on Solaris for NaN and more
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <ieeefp.h>
static double zero = 0;
double Port::nan = NAN;
double Port::infinity = 1 / zero;
double Port::dbl_max = 1.7976931348623157e308;
double Port::dbl_min = 5e-324;
longdouble Port::ldbl_max = LDBL_MAX;
struct PortInitializer
{
PortInitializer();
};
static PortInitializer portinitializer;
PortInitializer::PortInitializer()
{
// gcc nan's have the sign bit set by default, so turn it off
// Need the volatile to prevent gcc from doing incorrect
// constant folding.
volatile longdouble foo;
foo = NAN;
if (signbit(foo)) // signbit sometimes, not always, set
foo = -foo; // turn off sign bit
Port::nan = foo;
}
int Port::isNan(double r)
{
return isnan(r);
}
int Port::isNan(longdouble r)
{
return isnan(r);
}
int Port::isSignallingNan(double r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 51 of 0..63 for 64 bit doubles.
*/
return isNan(r) && !((((unsigned char*)&r)[6]) & 8);
}
int Port::isSignallingNan(longdouble r)
{
/* A signalling NaN is a NaN with 0 as the most significant bit of
* its significand, which is bit 62 of 0..79 for 80 bit reals.
*/
return isNan(r) && !((((unsigned char*)&r)[7]) & 0x40);
}
#undef isfinite
int Port::isFinite(double r)
{
return finite(r);
}
int Port::isInfinity(double r)
{
return isinf(r);
}
#undef signbit
int Port::Signbit(double r)
{
return (long)(((long *)&r)[1] & 0x80000000);
}
double Port::floor(double d)
{
return ::floor(d);
}
double Port::pow(double x, double y)
{
return ::pow(x, y);
}
unsigned long long Port::strtoull(const char *p, char **pend, int base)
{
return ::strtoull(p, pend, base);
}
char *Port::ull_to_string(char *buffer, ulonglong ull)
{
sprintf(buffer, "%llu", ull);
return buffer;
}
wchar_t *Port::ull_to_string(wchar_t *buffer, ulonglong ull)
{
swprintf(buffer, sizeof(ulonglong) * 3 + 1, L"%llu", ull);
return buffer;
}
double Port::ull_to_double(ulonglong ull)
{
return (double) ull;
}
const char *Port::list_separator()
{
return ",";
}
const wchar_t *Port::wlist_separator()
{
return L",";
}
char *Port::strupr(char *s)
{
char *t = s;
while (*s)
{
*s = toupper(*s);
s++;
}
return t;
}
#endif

View File

@@ -1,83 +0,0 @@
// Copyright (c) 1999-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
#ifndef PORT_H
#define PORT_H
// Portable wrapper around compiler/system specific things.
// The idea is to minimize #ifdef's in the app code.
#include "longdouble.h"
#ifndef TYPEDEFS
#define TYPEDEFS
#include <wchar.h>
#if _MSC_VER
typedef __int64 longlong;
typedef unsigned __int64 ulonglong;
// According to VC 8.0 docs, long double is the same as double
longdouble strtold(const char *p,char **endp);
#define strtof strtod
#else
typedef long long longlong;
typedef unsigned long long ulonglong;
#endif
#endif
typedef double d_time;
struct Port
{
static double nan;
static double infinity;
static double dbl_max;
static double dbl_min;
static longdouble ldbl_max;
#if __OpenBSD__
#elif __GNUC__ && !defined __HAIKU__
// These conflict with macros in math.h, should rename them
#undef isnan
#undef isfinite
#undef isinfinity
#undef signbit
#endif
static int isNan(double);
static int isNan(longdouble);
static int isSignallingNan(double);
static int isSignallingNan(longdouble);
static int isFinite(double);
static int isInfinity(double);
static int Signbit(double);
static double floor(double);
static double pow(double x, double y);
static longdouble fmodl(longdouble x, longdouble y);
static ulonglong strtoull(const char *p, char **pend, int base);
static char *ull_to_string(char *buffer, ulonglong ull);
static wchar_t *ull_to_string(wchar_t *buffer, ulonglong ull);
// Convert ulonglong to double
static double ull_to_double(ulonglong ull);
// Get locale-dependent list separator
static const char *list_separator();
static const wchar_t *wlist_separator();
static char *strupr(char *);
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,422 +0,0 @@
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef ROOT_H
#define ROOT_H
#include <stdlib.h>
#include <stdarg.h>
#ifdef DEBUG
#include <assert.h>
#endif
#if __DMC__
#pragma once
#endif
#ifndef IS_PRINTF
# ifdef __GNUC__
# define IS_PRINTF(FMTARG) __attribute((__format__ (__printf__, (FMTARG), (FMTARG)+1) ))
# else
# define IS_PRINTF(FMTARG)
# endif
#endif
typedef size_t hash_t;
#include "longdouble.h"
char *wchar2ascii(wchar_t *);
int wcharIsAscii(wchar_t *);
char *wchar2ascii(wchar_t *, unsigned len);
int wcharIsAscii(wchar_t *, unsigned len);
int bstrcmp(unsigned char *s1, unsigned char *s2);
char *bstr2str(unsigned char *b);
void error(const char *format, ...) IS_PRINTF(1);
#if M_UNICODE
void error(const dchar *format, ...);
#endif
void warning(const char *format, ...) IS_PRINTF(1);
#ifndef TYPEDEFS
#define TYPEDEFS
#if _MSC_VER
#include <float.h> // for _isnan
#include <malloc.h> // for alloca
// According to VC 8.0 docs, long double is the same as double
longdouble strtold(const char *p,char **endp);
#define strtof strtod
#define isnan _isnan
typedef __int64 longlong;
typedef unsigned __int64 ulonglong;
#else
typedef long long longlong;
typedef unsigned long long ulonglong;
#endif
#endif
longlong randomx();
/*
* Root of our class library.
*/
struct OutBuffer;
// Can't include arraytypes.h here, need to declare these directly.
template <typename TYPE> struct ArrayBase;
typedef ArrayBase<struct File> Files;
typedef ArrayBase<char> Strings;
struct Object
{
Object() { }
virtual ~Object() { }
virtual int equals(Object *o);
/**
* Returns a hash code, useful for things like building hash tables of Objects.
*/
virtual hash_t hashCode();
/**
* Return <0, ==0, or >0 if this is less than, equal to, or greater than obj.
* Useful for sorting Objects.
*/
virtual int compare(Object *obj);
/**
* Pretty-print an Object. Useful for debugging the old-fashioned way.
*/
virtual void print();
virtual char *toChars();
virtual void toBuffer(OutBuffer *buf);
/**
* Used as a replacement for dynamic_cast. Returns a unique number
* defined by the library user. For Object, the return value is 0.
*/
virtual int dyncast();
/**
* Marks pointers for garbage collector by calling mem.mark() for all pointers into heap.
*/
/*virtual*/ // not used, disable for now
void mark();
};
struct String : Object
{
int ref; // != 0 if this is a reference to someone else's string
char *str; // the string itself
String(char *str, int ref = 1);
~String();
static hash_t calcHash(const char *str, size_t len);
static hash_t calcHash(const char *str);
hash_t hashCode();
unsigned len();
int equals(Object *obj);
int compare(Object *obj);
char *toChars();
void print();
void mark();
};
struct FileName : String
{
FileName(char *str, int ref);
FileName(char *path, char *name);
hash_t hashCode();
int equals(Object *obj);
static int equals(const char *name1, const char *name2);
int compare(Object *obj);
static int compare(const char *name1, const char *name2);
static int absolute(const char *name);
static char *ext(const char *);
char *ext();
static char *removeExt(const char *str);
static char *name(const char *);
char *name();
static char *path(const char *);
static const char *replaceName(const char *path, const char *name);
static char *combine(const char *path, const char *name);
static Strings *splitPath(const char *path);
static FileName *defaultExt(const char *name, const char *ext);
static FileName *forceExt(const char *name, const char *ext);
int equalsExt(const char *ext);
void CopyTo(FileName *to);
static char *searchPath(Strings *path, const char *name, int cwd);
static char *safeSearchPath(Strings *path, const char *name);
static int exists(const char *name);
static void ensurePathExists(const char *path);
static char *canonicalName(const char *name);
};
struct File : Object
{
int ref; // != 0 if this is a reference to someone else's buffer
unsigned char *buffer; // data for our file
unsigned len; // amount of data in buffer[]
void *touchtime; // system time to use for file
FileName *name; // name of our file
File(char *);
File(FileName *);
~File();
void mark();
char *toChars();
/* Read file, return !=0 if error
*/
int read();
/* Write file, either succeed or fail
* with error message & exit.
*/
void readv();
/* Read file, return !=0 if error
*/
int mmread();
/* Write file, either succeed or fail
* with error message & exit.
*/
void mmreadv();
/* Write file, return !=0 if error
*/
int write();
/* Write file, either succeed or fail
* with error message & exit.
*/
void writev();
/* Return !=0 if file exists.
* 0: file doesn't exist
* 1: normal file
* 2: directory
*/
/* Append to file, return !=0 if error
*/
int append();
/* Append to file, either succeed or fail
* with error message & exit.
*/
void appendv();
/* Return !=0 if file exists.
* 0: file doesn't exist
* 1: normal file
* 2: directory
*/
int exists();
/* Given wildcard filespec, return an array of
* matching File's.
*/
static Files *match(char *);
static Files *match(FileName *);
// Compare file times.
// Return <0 this < f
// =0 this == f
// >0 this > f
int compareTime(File *f);
// Read system file statistics
void stat();
/* Set buffer
*/
void setbuffer(void *buffer, unsigned len)
{
this->buffer = (unsigned char *)buffer;
this->len = len;
}
void checkoffset(size_t offset, size_t nbytes);
void remove(); // delete file
};
struct OutBuffer : Object
{
unsigned char *data;
unsigned offset;
unsigned size;
OutBuffer();
~OutBuffer();
char *extractData();
void mark();
void reserve(unsigned nbytes);
void setsize(unsigned size);
void reset();
void write(const void *data, unsigned nbytes);
void writebstring(unsigned char *string);
void writestring(const char *string);
void prependstring(const char *string);
void writenl(); // write newline
void writeByte(unsigned b);
void writebyte(unsigned b) { writeByte(b); }
void writeUTF8(unsigned b);
void prependbyte(unsigned b);
void writeword(unsigned w);
void writeUTF16(unsigned w);
void write4(unsigned w);
void write(OutBuffer *buf);
void write(Object *obj);
void fill0(unsigned nbytes);
void align(unsigned size);
void vprintf(const char *format, va_list args);
void printf(const char *format, ...) IS_PRINTF(2);
#if M_UNICODE
void vprintf(const unsigned short *format, va_list args);
void printf(const unsigned short *format, ...);
#endif
void bracket(char left, char right);
unsigned bracket(unsigned i, const char *left, unsigned j, const char *right);
void spread(unsigned offset, unsigned nbytes);
unsigned insert(unsigned offset, const void *data, unsigned nbytes);
void remove(unsigned offset, unsigned nbytes);
char *toChars();
char *extractString();
};
struct Array : Object
{
unsigned dim;
void **data;
private:
unsigned allocdim;
#define SMALLARRAYCAP 1
void *smallarray[SMALLARRAYCAP]; // inline storage for small arrays
public:
Array();
~Array();
//Array(const Array&);
void mark();
char *toChars();
void reserve(unsigned nentries);
void setDim(unsigned newdim);
void fixDim();
void push(void *ptr);
void *pop();
void shift(void *ptr);
void insert(unsigned index, void *ptr);
void insert(unsigned index, Array *a);
void append(Array *a);
void remove(unsigned i);
void zero();
void *tos();
void sort();
Array *copy();
};
template <typename TYPE>
struct ArrayBase : Array
{
TYPE **tdata()
{
return (TYPE **)data;
}
TYPE*& operator[] (size_t index)
{
#ifdef DEBUG
assert(index < dim);
#endif
return ((TYPE **)data)[index];
}
void insert(size_t index, TYPE *v)
{
Array::insert(index, (void *)v);
}
void insert(size_t index, ArrayBase *a)
{
Array::insert(index, (Array *)a);
}
void append(ArrayBase *a)
{
Array::append((Array *)a);
}
void push(TYPE *a)
{
Array::push((void *)a);
}
ArrayBase *copy()
{
return (ArrayBase *)Array::copy();
}
};
struct Bits : Object
{
unsigned bitdim;
unsigned allocdim;
unsigned *data;
Bits();
~Bits();
void mark();
void resize(unsigned bitdim);
void set(unsigned bitnum);
void clear(unsigned bitnum);
int test(unsigned bitnum);
void set();
void clear();
void copy(Bits *from);
Bits *clone();
void sub(Bits *b);
};
#endif

View File

@@ -1,189 +0,0 @@
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdint.h> // uint{8|16|32}_t
#include <string.h> // memcpy()
#include <stdlib.h>
#include "root.h"
#include "rmem.h" // mem
#include "stringtable.h"
hash_t calcHash(const char *str, size_t len)
{
hash_t hash = 0;
while (1)
{
switch (len)
{
case 0:
return hash;
case 1:
hash *= 37;
hash += *(const uint8_t *)str;
return hash;
case 2:
hash *= 37;
#if LITTLE_ENDIAN
hash += *(const uint16_t *)str;
#else
hash += str[0] * 256 + str[1];
#endif
return hash;
case 3:
hash *= 37;
#if LITTLE_ENDIAN
hash += (*(const uint16_t *)str << 8) +
((const uint8_t *)str)[2];
#else
hash += (str[0] * 256 + str[1]) * 256 + str[2];
#endif
return hash;
default:
hash *= 37;
#if LITTLE_ENDIAN
hash += *(const uint32_t *)str;
#else
hash += ((str[0] * 256 + str[1]) * 256 + str[2]) * 256 + str[3];
#endif
str += 4;
len -= 4;
break;
}
}
}
void StringValue::ctor(const char *p, unsigned length)
{
this->length = length;
this->lstring[length] = 0;
memcpy(this->lstring, p, length * sizeof(char));
}
void StringTable::init(unsigned size)
{
table = (void **)mem.calloc(size, sizeof(void *));
tabledim = size;
count = 0;
}
StringTable::~StringTable()
{
unsigned i;
// Zero out dangling pointers to help garbage collector.
// Should zero out StringEntry's too.
for (i = 0; i < count; i++)
table[i] = NULL;
mem.free(table);
table = NULL;
}
struct StringEntry
{
StringEntry *left;
StringEntry *right;
hash_t hash;
StringValue value;
static StringEntry *alloc(const char *s, unsigned len);
};
StringEntry *StringEntry::alloc(const char *s, unsigned len)
{
StringEntry *se;
se = (StringEntry *) mem.calloc(1,sizeof(StringEntry) + len + 1);
se->value.ctor(s, len);
se->hash = calcHash(s,len);
return se;
}
void **StringTable::search(const char *s, unsigned len)
{
hash_t hash;
unsigned u;
int cmp;
StringEntry **se;
//printf("StringTable::search(%p,%d)\n",s,len);
hash = calcHash(s,len);
u = hash % tabledim;
se = (StringEntry **)&table[u];
//printf("\thash = %d, u = %d\n",hash,u);
while (*se)
{
cmp = (*se)->hash - hash;
if (cmp == 0)
{
cmp = (*se)->value.len() - len;
if (cmp == 0)
{
cmp = ::memcmp(s,(*se)->value.toDchars(),len);
if (cmp == 0)
break;
}
}
if (cmp < 0)
se = &(*se)->left;
else
se = &(*se)->right;
}
//printf("\treturn %p, %p\n",se, (*se));
return (void **)se;
}
StringValue *StringTable::lookup(const char *s, unsigned len)
{ StringEntry *se;
se = *(StringEntry **)search(s,len);
if (se)
return &se->value;
else
return NULL;
}
StringValue *StringTable::update(const char *s, unsigned len)
{ StringEntry **pse;
StringEntry *se;
pse = (StringEntry **)search(s,len);
se = *pse;
if (!se) // not in table: so create new entry
{
se = StringEntry::alloc(s, len);
*pse = se;
}
return &se->value;
}
StringValue *StringTable::insert(const char *s, unsigned len)
{ StringEntry **pse;
StringEntry *se;
pse = (StringEntry **)search(s,len);
se = *pse;
if (se)
return NULL; // error: already in table
else
{
se = StringEntry::alloc(s, len);
*pse = se;
}
return &se->value;
}

View File

@@ -1,71 +0,0 @@
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef STRINGTABLE_H
#define STRINGTABLE_H
#if __SC__
#pragma once
#endif
#include "root.h"
struct StringEntry;
// StringValue is a variable-length structure as indicated by the last array
// member with unspecified size. It has neither proper c'tors nor a factory
// method because the only thing which should be creating these is StringTable.
struct StringValue
{
union
{
void *ptrvalue;
char *string;
};
private:
unsigned length;
#ifndef IN_GCC
// Disable warning about nonstandard extension
#pragma warning (disable : 4200)
#endif
char lstring[];
public:
unsigned len() const { return length; }
const char *toDchars() const { return lstring; }
private:
friend struct StringEntry;
StringValue(); // not constructible
// This is more like a placement new c'tor
void ctor(const char *p, unsigned length);
};
struct StringTable
{
private:
void **table;
unsigned count;
unsigned tabledim;
public:
void init(unsigned size = 37);
~StringTable();
StringValue *lookup(const char *s, unsigned len);
StringValue *insert(const char *s, unsigned len);
StringValue *update(const char *s, unsigned len);
private:
void **search(const char *s, unsigned len);
};
#endif

View File

@@ -1,400 +0,0 @@
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include <string.h> // strlen()
#include "root.h"
#include "speller.h"
#include "mars.h"
#include "init.h"
#include "identifier.h"
#include "scope.h"
#include "attrib.h"
#include "dsymbol.h"
#include "declaration.h"
#include "aggregate.h"
#include "module.h"
#include "id.h"
#include "lexer.h"
Scope *Scope::freelist = NULL;
void *Scope::operator new(size_t size)
{
if (freelist)
{
Scope *s = freelist;
freelist = s->enclosing;
//printf("freelist %p\n", s);
assert(s->flags & SCOPEfree);
s->flags &= ~SCOPEfree;
return s;
}
void *p = ::operator new(size);
//printf("new %p\n", p);
return p;
}
Scope::Scope()
{ // Create root scope
//printf("Scope::Scope() %p\n", this);
this->module = NULL;
this->scopesym = NULL;
this->sd = NULL;
this->enclosing = NULL;
this->parent = NULL;
this->sw = NULL;
this->enclosingFinally = NULL;
this->enclosingScopeExit = NULL;
this->tinst = NULL;
this->sbreak = NULL;
this->scontinue = NULL;
this->fes = NULL;
this->structalign = global.structalign;
this->func = NULL;
this->slabel = NULL;
this->linkage = LINKd;
this->protection = PROTpublic;
this->explicitProtection = 0;
this->stc = 0;
this->offset = 0;
this->inunion = 0;
this->incontract = 0;
this->nofree = 0;
this->noctor = 0;
this->noaccesscheck = 0;
this->mustsemantic = 0;
this->intypeof = 0;
this->parameterSpecialization = 0;
this->callSuper = 0;
this->flags = 0;
this->lastdc = NULL;
this->lastoffset = 0;
this->docbuf = NULL;
}
Scope::Scope(Scope *enclosing)
{
//printf("Scope::Scope(enclosing = %p) %p\n", enclosing, this);
assert(!(enclosing->flags & SCOPEfree));
this->module = enclosing->module;
this->func = enclosing->func;
this->parent = enclosing->parent;
this->scopesym = NULL;
this->sd = NULL;
this->sw = enclosing->sw;
this->enclosingFinally = enclosing->enclosingFinally;
this->enclosingScopeExit = enclosing->enclosingScopeExit;
this->tinst = enclosing->tinst;
this->sbreak = enclosing->sbreak;
this->scontinue = enclosing->scontinue;
this->fes = enclosing->fes;
this->structalign = enclosing->structalign;
this->enclosing = enclosing;
#ifdef DEBUG
if (enclosing->enclosing)
assert(!(enclosing->enclosing->flags & SCOPEfree));
if (this == enclosing->enclosing)
{
printf("this = %p, enclosing = %p, enclosing->enclosing = %p\n", this, enclosing, enclosing->enclosing);
}
assert(this != enclosing->enclosing);
#endif
this->slabel = NULL;
this->linkage = enclosing->linkage;
this->protection = enclosing->protection;
this->explicitProtection = enclosing->explicitProtection;
this->stc = enclosing->stc;
this->offset = 0;
this->inunion = enclosing->inunion;
this->incontract = enclosing->incontract;
this->nofree = 0;
this->noctor = enclosing->noctor;
this->noaccesscheck = enclosing->noaccesscheck;
this->mustsemantic = enclosing->mustsemantic;
this->intypeof = enclosing->intypeof;
this->parameterSpecialization = enclosing->parameterSpecialization;
this->callSuper = enclosing->callSuper;
this->flags = 0;
this->lastdc = NULL;
this->lastoffset = 0;
this->docbuf = enclosing->docbuf;
assert(this != enclosing);
}
Scope *Scope::createGlobal(Module *module)
{
Scope *sc;
sc = new Scope();
sc->module = module;
sc->scopesym = new ScopeDsymbol();
sc->scopesym->symtab = new DsymbolTable();
// Add top level package as member of this global scope
Dsymbol *m = module;
while (m->parent)
m = m->parent;
m->addMember(NULL, sc->scopesym, 1);
m->parent = NULL; // got changed by addMember()
// Create the module scope underneath the global scope
sc = sc->push(module);
sc->parent = module;
return sc;
}
Scope *Scope::push()
{
//printf("Scope::push()\n");
Scope *s = new Scope(this);
assert(this != s);
return s;
}
Scope *Scope::push(ScopeDsymbol *ss)
{
//printf("Scope::push(%s)\n", ss->toChars());
Scope *s = push();
s->scopesym = ss;
return s;
}
Scope *Scope::pop()
{
//printf("Scope::pop() %p nofree = %d\n", this, nofree);
Scope *enc = enclosing;
if (enclosing)
enclosing->callSuper |= callSuper;
if (!nofree)
{ enclosing = freelist;
freelist = this;
flags |= SCOPEfree;
}
return enc;
}
void Scope::mergeCallSuper(Loc loc, unsigned cs)
{
// This does a primitive flow analysis to support the restrictions
// regarding when and how constructors can appear.
// It merges the results of two paths.
// The two paths are callSuper and cs; the result is merged into callSuper.
if (cs != callSuper)
{ int a;
int b;
callSuper |= cs & (CSXany_ctor | CSXlabel);
if (cs & CSXreturn)
{
}
else if (callSuper & CSXreturn)
{
callSuper = cs | (callSuper & (CSXany_ctor | CSXlabel));
}
else
{
a = (cs & (CSXthis_ctor | CSXsuper_ctor)) != 0;
b = (callSuper & (CSXthis_ctor | CSXsuper_ctor)) != 0;
if (a != b)
error(loc, "one path skips constructor");
callSuper |= cs;
}
}
}
Dsymbol *Scope::search(Loc loc, Identifier *ident, Dsymbol **pscopesym)
{ Dsymbol *s;
Scope *sc;
//printf("Scope::search(%p, '%s')\n", this, ident->toChars());
if (ident == Id::empty)
{
// Look for module scope
for (sc = this; sc; sc = sc->enclosing)
{
assert(sc != sc->enclosing);
if (sc->scopesym)
{
s = sc->scopesym->isModule();
if (s)
{
//printf("\tfound %s.%s\n", s->parent ? s->parent->toChars() : "", s->toChars());
if (pscopesym)
*pscopesym = sc->scopesym;
return s;
}
}
}
return NULL;
}
for (sc = this; sc; sc = sc->enclosing)
{
assert(sc != sc->enclosing);
if (sc->scopesym)
{
//printf("\tlooking in scopesym '%s', kind = '%s'\n", sc->scopesym->toChars(), sc->scopesym->kind());
s = sc->scopesym->search(loc, ident, 0);
if (s)
{
if ((global.params.warnings ||
global.params.Dversion > 1) &&
ident == Id::length &&
sc->scopesym->isArrayScopeSymbol() &&
sc->enclosing &&
sc->enclosing->search(loc, ident, NULL))
{
warning(s->loc, "array 'length' hides other 'length' name in outer scope");
}
//printf("\tfound %s.%s, kind = '%s'\n", s->parent ? s->parent->toChars() : "", s->toChars(), s->kind());
if (pscopesym)
*pscopesym = sc->scopesym;
return s;
}
}
}
return NULL;
}
Dsymbol *Scope::insert(Dsymbol *s)
{ Scope *sc;
for (sc = this; sc; sc = sc->enclosing)
{
//printf("\tsc = %p\n", sc);
if (sc->scopesym)
{
//printf("\t\tsc->scopesym = %p\n", sc->scopesym);
if (!sc->scopesym->symtab)
sc->scopesym->symtab = new DsymbolTable();
return sc->scopesym->symtabInsert(s);
}
}
assert(0);
return NULL;
}
/********************************************
* Search enclosing scopes for ClassDeclaration.
*/
ClassDeclaration *Scope::getClassScope()
{ Scope *sc;
for (sc = this; sc; sc = sc->enclosing)
{
ClassDeclaration *cd;
if (sc->scopesym)
{
cd = sc->scopesym->isClassDeclaration();
if (cd)
return cd;
}
}
return NULL;
}
/********************************************
* Search enclosing scopes for ClassDeclaration.
*/
AggregateDeclaration *Scope::getStructClassScope()
{ Scope *sc;
for (sc = this; sc; sc = sc->enclosing)
{
AggregateDeclaration *ad;
if (sc->scopesym)
{
ad = sc->scopesym->isClassDeclaration();
if (ad)
return ad;
else
{ ad = sc->scopesym->isStructDeclaration();
if (ad)
return ad;
}
}
}
return NULL;
}
/*******************************************
* For TemplateDeclarations, we need to remember the Scope
* where it was declared. So mark the Scope as not
* to be free'd.
*/
void Scope::setNoFree()
{ Scope *sc;
//int i = 0;
//printf("Scope::setNoFree(this = %p)\n", this);
for (sc = this; sc; sc = sc->enclosing)
{
//printf("\tsc = %p\n", sc);
sc->nofree = 1;
assert(!(flags & SCOPEfree));
//assert(sc != sc->enclosing);
//assert(!sc->enclosing || sc != sc->enclosing->enclosing);
//if (++i == 10)
//assert(0);
}
}
/************************************************
* Given the failed search attempt, try to find
* one with a close spelling.
*/
void *scope_search_fp(void *arg, const char *seed)
{
//printf("scope_search_fp('%s')\n", seed);
/* If not in the lexer's string table, it certainly isn't in the symbol table.
* Doing this first is a lot faster.
*/
size_t len = strlen(seed);
if (!len)
return NULL;
StringValue *sv = Lexer::stringtable.lookup(seed, len);
if (!sv)
return NULL;
Identifier *id = (Identifier *)sv->ptrvalue;
assert(id);
Scope *sc = (Scope *)arg;
Module::clearCache();
Dsymbol *s = sc->search(0, id, NULL);
return s;
}
Dsymbol *Scope::search_correct(Identifier *ident)
{
if (global.gag)
return NULL; // don't do it for speculative compiles; too time consuming
return (Dsymbol *)speller(ident->toChars(), &scope_search_fp, this, idchars);
}

View File

@@ -1,130 +0,0 @@
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_SCOPE_H
#define DMD_SCOPE_H
#ifdef __DMC__
#pragma once
#endif
struct Dsymbol;
struct ScopeDsymbol;
struct Array;
struct Identifier;
struct Module;
struct Statement;
struct SwitchStatement;
struct TryFinallyStatement;
struct LabelStatement;
struct ForeachStatement;
struct ClassDeclaration;
struct AggregateDeclaration;
struct AnonymousAggregateDeclaration;
struct FuncDeclaration;
struct DocComment;
struct TemplateInstance;
#if IN_LLVM
struct EnclosingHandler;
struct AnonDeclaration;
#endif
#if __GNUC__
// Requires a full definition for PROT and LINK
#include "dsymbol.h" // PROT
#include "mars.h" // LINK
#else
enum LINK;
enum PROT;
#endif
struct Scope
{
Scope *enclosing; // enclosing Scope
Module *module; // Root module
ScopeDsymbol *scopesym; // current symbol
ScopeDsymbol *sd; // if in static if, and declaring new symbols,
// sd gets the addMember()
FuncDeclaration *func; // function we are in
Dsymbol *parent; // parent to use
LabelStatement *slabel; // enclosing labelled statement
SwitchStatement *sw; // enclosing switch statement
TryFinallyStatement *enclosingFinally; // enclosing try finally statement; set inside its finally block
TemplateInstance *tinst; // enclosing template instance
Statement *enclosingScopeExit; // enclosing statement that wants to do something on scope exit
Statement *sbreak; // enclosing statement that supports "break"
Statement *scontinue; // enclosing statement that supports "continue"
ForeachStatement *fes; // if nested function for ForeachStatement, this is it
unsigned offset; // next offset to use in aggregate
// This really shouldn't be a part of Scope, because it requires
// semantic() to be done in the lexical field order. It should be
// set in a pass after semantic() on all fields so they can be
// semantic'd in any order.
int inunion; // we're processing members of a union
int incontract; // we're inside contract code
int nofree; // set if shouldn't free it
int noctor; // set if constructor calls aren't allowed
int intypeof; // in typeof(exp)
int parameterSpecialization; // if in template parameter specialization
int noaccesscheck; // don't do access checks
int mustsemantic; // cannot defer semantic()
unsigned callSuper; // primitive flow analysis for constructors
#define CSXthis_ctor 1 // called this()
#define CSXsuper_ctor 2 // called super()
#define CSXthis 4 // referenced this
#define CSXsuper 8 // referenced super
#define CSXlabel 0x10 // seen a label
#define CSXreturn 0x20 // seen a return statement
#define CSXany_ctor 0x40 // either this() or super() was called
structalign_t structalign; // alignment for struct members
enum LINK linkage; // linkage for external functions
enum PROT protection; // protection for class members
int explicitProtection; // set if in an explicit protection attribute
StorageClass stc; // storage class
unsigned flags;
#define SCOPEctor 1 // constructor type
#define SCOPEstaticif 2 // inside static if
#define SCOPEfree 4 // is on free list
DocComment *lastdc; // documentation comment for last symbol at this scope
unsigned lastoffset; // offset in docbuf of where to insert next dec
OutBuffer *docbuf; // buffer for documentation output
static Scope *freelist;
static void *operator new(size_t sz);
static Scope *createGlobal(Module *module);
Scope();
Scope(Module *module);
Scope(Scope *enclosing);
Scope *push();
Scope *push(ScopeDsymbol *ss);
Scope *pop();
void mergeCallSuper(Loc loc, unsigned cs);
Dsymbol *search(Loc loc, Identifier *ident, Dsymbol **pscopesym);
Dsymbol *search_correct(Identifier *ident);
Dsymbol *insert(Dsymbol *s);
ClassDeclaration *getClassScope();
AggregateDeclaration *getStructClassScope();
void setNoFree();
};
#endif /* DMD_SCOPE_H */

View File

@@ -1,34 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "mars.h"
#include "init.h"
#include "expression.h"
#include "template.h"
#include "statement.h"
#include "mtype.h"
#include "utf.h"
#include "declaration.h"
#include "aggregate.h"
#include "scope.h"
#include "attrib.h"
/********************************************
* Determine if Expression has any side effects.
*/
bool Expression::hasSideEffect()
{
return checkSideEffect(2);
}

View File

@@ -1,262 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#if __sun&&__SVR4 || _MSC_VER
#include <alloca.h>
#endif
#if _MSC_VER
#include <malloc.h>
#endif
#include "speller.h"
const char idchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
/**************************************************
* Looks for correct spelling.
* Currently only looks a 'distance' of one from the seed[].
* This does an exhaustive search, so can potentially be very slow.
* Input:
* seed wrongly spelled word
* fp search function
* fparg argument to search function
* charset character set
* Returns:
* NULL no correct spellings found
* void* value returned by fp() for first possible correct spelling
*/
void *spellerY(const char *seed, size_t seedlen, fp_speller_t fp, void *fparg,
const char *charset, size_t index)
{
if (!seedlen)
return NULL;
assert(seed[seedlen] == 0);
char tmp[30];
char *buf;
if (seedlen <= sizeof(tmp) - 2)
buf = tmp;
else
{
buf = (char *)alloca(seedlen + 2); // leave space for extra char
if (!buf)
return NULL; // no matches
}
memcpy(buf, seed, index);
/* Delete at seed[index] */
if (index < seedlen)
{
memcpy(buf + index, seed + index + 1, seedlen - index);
assert(buf[seedlen - 1] == 0);
void *p = (*fp)(fparg, buf);
if (p)
return p;
}
if (charset && *charset)
{
/* Substitutions */
if (index < seedlen)
{
memcpy(buf, seed, seedlen + 1);
for (const char *s = charset; *s; s++)
{
buf[index] = *s;
//printf("sub buf = '%s'\n", buf);
void *p = (*fp)(fparg, buf);
if (p)
return p;
}
assert(buf[seedlen] == 0);
}
/* Insertions */
memcpy (buf + index + 1, seed + index, seedlen + 1 - index);
for (const char *s = charset; *s; s++)
{
buf[index] = *s;
//printf("ins buf = '%s'\n", buf);
void *p = (*fp)(fparg, buf);
if (p)
return p;
}
assert(buf[seedlen + 1] == 0);
}
return NULL; // didn't find any corrections
}
void *spellerX(const char *seed, size_t seedlen, fp_speller_t fp, void *fparg,
const char *charset, int flag)
{
if (!seedlen)
return NULL;
char tmp[30];
char *buf;
if (seedlen <= sizeof(tmp) - 2)
buf = tmp;
else
{
buf = (char *)alloca(seedlen + 2); // leave space for extra char
if (!buf)
return NULL; // no matches
}
/* Deletions */
memcpy(buf, seed + 1, seedlen);
for (size_t i = 0; i < seedlen; i++)
{
//printf("del buf = '%s'\n", buf);
void *p;
if (flag)
p = spellerY(buf, seedlen - 1, fp, fparg, charset, i);
else
p = (*fp)(fparg, buf);
if (p)
return p;
buf[i] = seed[i];
}
/* Transpositions */
if (!flag)
{
memcpy(buf, seed, seedlen + 1);
for (size_t i = 0; i + 1 < seedlen; i++)
{
// swap [i] and [i + 1]
buf[i] = seed[i + 1];
buf[i + 1] = seed[i];
//printf("tra buf = '%s'\n", buf);
void *p = (*fp)(fparg, buf);
if (p)
return p;
buf[i] = seed[i];
}
}
if (charset && *charset)
{
/* Substitutions */
memcpy(buf, seed, seedlen + 1);
for (size_t i = 0; i < seedlen; i++)
{
for (const char *s = charset; *s; s++)
{
buf[i] = *s;
//printf("sub buf = '%s'\n", buf);
void *p;
if (flag)
p = spellerY(buf, seedlen, fp, fparg, charset, i + 1);
else
p = (*fp)(fparg, buf);
if (p)
return p;
}
buf[i] = seed[i];
}
/* Insertions */
memcpy(buf + 1, seed, seedlen + 1);
for (size_t i = 0; i <= seedlen; i++) // yes, do seedlen+1 iterations
{
for (const char *s = charset; *s; s++)
{
buf[i] = *s;
//printf("ins buf = '%s'\n", buf);
void *p;
if (flag)
p = spellerY(buf, seedlen + 1, fp, fparg, charset, i + 1);
else
p = (*fp)(fparg, buf);
if (p)
return p;
}
buf[i] = seed[i]; // going past end of seed[] is ok, as we hit the 0
}
}
return NULL; // didn't find any corrections
}
void *speller(const char *seed, fp_speller_t fp, void *fparg, const char *charset)
{
size_t seedlen = strlen(seed);
for (int distance = 0; distance < 2; distance++)
{ void *p = spellerX(seed, seedlen, fp, fparg, charset, distance);
if (p)
return p;
// if (seedlen > 10)
// break;
}
return NULL; // didn't find it
}
#if UNITTEST
#include <stdio.h>
#include <string.h>
#include <assert.h>
void *speller_test(void *fparg, const char *s)
{
//printf("speller_test(%s, %s)\n", fparg, s);
if (strcmp((char *)fparg, s) == 0)
return fparg;
return NULL;
}
void unittest_speller()
{
static const char *cases[][3] =
{
{ "hello", "hell", "y" },
{ "hello", "hel", "y" },
{ "hello", "ello", "y" },
{ "hello", "llo", "y" },
{ "hello", "hellox", "y" },
{ "hello", "helloxy", "y" },
{ "hello", "xhello", "y" },
{ "hello", "xyhello", "y" },
{ "hello", "ehllo", "y" },
{ "hello", "helol", "y" },
{ "hello", "abcd", "n" },
//{ "ehllo", "helol", "y" },
{ "hello", "helxxlo", "y" },
{ "hello", "ehlxxlo", "n" },
{ "hello", "heaao", "y" },
{ "_123456789_123456789_123456789_123456789", "_123456789_123456789_123456789_12345678", "y" },
};
//printf("unittest_speller()\n");
const void *p = speller("hello", &speller_test, (void *)"hell", idchars);
assert(p != NULL);
for (int i = 0; i < sizeof(cases)/sizeof(cases[0]); i++)
{
//printf("case [%d]\n", i);
void *p = speller(cases[i][0], &speller_test, (void *)cases[i][1], idchars);
if (p)
assert(cases[i][2][0] == 'y');
else
assert(cases[i][2][0] == 'n');
}
//printf("unittest_speller() success\n");
}
#endif

View File

@@ -1,7 +0,0 @@
typedef void *(fp_speller_t)(void *, const char *);
extern const char idchars[];
void *speller(const char *seed, fp_speller_t fp, void *fparg, const char *charset);

File diff suppressed because it is too large Load Diff

View File

@@ -1,943 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_STATEMENT_H
#define DMD_STATEMENT_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "root.h"
#include "arraytypes.h"
#include "dsymbol.h"
#include "lexer.h"
struct OutBuffer;
struct Scope;
struct Expression;
struct LabelDsymbol;
struct Identifier;
struct IfStatement;
struct ExpStatement;
struct DefaultStatement;
struct VarDeclaration;
struct Condition;
struct Module;
struct Token;
struct InlineCostState;
struct InlineDoState;
struct InlineScanState;
struct ReturnStatement;
struct CompoundStatement;
struct Parameter;
struct StaticAssert;
struct AsmStatement;
struct AsmBlockStatement;
struct GotoStatement;
struct ScopeStatement;
struct TryCatchStatement;
struct TryFinallyStatement;
struct CaseStatement;
struct DefaultStatement;
struct LabelStatement;
struct HdrGenState;
struct InterState;
struct CaseStatement;
struct LabelStatement;
struct VolatileStatement;
struct SynchronizedStatement;
enum TOK;
#if IN_LLVM
namespace llvm
{
class Value;
class BasicBlock;
class ConstantInt;
}
#endif
// Back end
struct IRState;
struct Blockx;
#if IN_LLVM
struct DValue;
typedef DValue elem;
#endif
#if IN_GCC
union tree_node; typedef union tree_node block;
//union tree_node; typedef union tree_node elem;
#else
struct block;
//struct elem;
#endif
struct code;
/* How a statement exits; this is returned by blockExit()
*/
enum BE
{
BEnone = 0,
BEfallthru = 1,
BEthrow = 2,
BEreturn = 4,
BEgoto = 8,
BEhalt = 0x10,
BEbreak = 0x20,
BEcontinue = 0x40,
BEany = (BEfallthru | BEthrow | BEreturn | BEgoto | BEhalt),
};
struct Statement : Object
{
Loc loc;
Statement(Loc loc);
virtual Statement *syntaxCopy();
void print();
char *toChars();
void error(const char *format, ...) IS_PRINTF(2);
void warning(const char *format, ...) IS_PRINTF(2);
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual TryCatchStatement *isTryCatchStatement() { return NULL; }
virtual GotoStatement *isGotoStatement() { return NULL; }
virtual AsmStatement *isAsmStatement() { return NULL; }
virtual AsmBlockStatement *isAsmBlockStatement() { return NULL; }
int incontract;
virtual ScopeStatement *isScopeStatement() { return NULL; }
virtual Statement *semantic(Scope *sc);
Statement *semanticScope(Scope *sc, Statement *sbreak, Statement *scontinue);
Statement *semanticNoScope(Scope *sc);
virtual int hasBreak();
virtual int hasContinue();
virtual int usesEH();
virtual int blockExit(bool mustNotThrow);
virtual int comeFrom();
virtual int isEmpty();
virtual void scopeCode(Scope *sc, Statement **sentry, Statement **sexit, Statement **sfinally);
virtual Statements *flatten(Scope *sc);
virtual Expression *interpret(InterState *istate);
virtual int inlineCost(InlineCostState *ics);
virtual Expression *doInline(InlineDoState *ids);
virtual Statement *doInlineStatement(InlineDoState *ids);
virtual Statement *inlineScan(InlineScanState *iss);
// Back end
virtual void toIR(IRState *irs);
// Avoid dynamic_cast
virtual ExpStatement *isExpStatement() { return NULL; }
virtual CompoundStatement *isCompoundStatement() { return NULL; }
virtual ReturnStatement *isReturnStatement() { return NULL; }
virtual IfStatement *isIfStatement() { return NULL; }
virtual CaseStatement *isCaseStatement() { return NULL; }
virtual DefaultStatement *isDefaultStatement() { return NULL; }
virtual LabelStatement* isLabelStatement() { return NULL; }
#if IN_LLVM
virtual void toNakedIR(IRState *irs);
virtual AsmBlockStatement* endsWithAsm();
#endif
};
struct PeelStatement : Statement
{
Statement *s;
PeelStatement(Statement *s);
Statement *semantic(Scope *sc);
};
struct ExpStatement : Statement
{
Expression *exp;
ExpStatement(Loc loc, Expression *exp);
ExpStatement(Loc loc, Dsymbol *s);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
int blockExit(bool mustNotThrow);
int isEmpty();
void scopeCode(Scope *sc, Statement **sentry, Statement **sexit, Statement **sfinally);
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
#if IN_LLVM
void toNakedIR(IRState *irs);
#endif
ExpStatement *isExpStatement() { return this; }
};
struct CompileStatement : Statement
{
Expression *exp;
CompileStatement(Loc loc, Expression *exp);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statements *flatten(Scope *sc);
Statement *semantic(Scope *sc);
};
struct CompoundStatement : Statement
{
Statements *statements;
CompoundStatement(Loc loc, Statements *s);
CompoundStatement(Loc loc, Statement *s1);
CompoundStatement(Loc loc, Statement *s1, Statement *s2);
virtual Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual Statement *semantic(Scope *sc);
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
int isEmpty();
virtual Statements *flatten(Scope *sc);
ReturnStatement *isReturnStatement();
Expression *interpret(InterState *istate);
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
virtual void toIR(IRState *irs);
// LDC
virtual void toNakedIR(IRState *irs);
virtual AsmBlockStatement* endsWithAsm();
virtual CompoundStatement *isCompoundStatement() { return this; }
};
struct CompoundDeclarationStatement : CompoundStatement
{
CompoundDeclarationStatement(Loc loc, Statements *s);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
/* The purpose of this is so that continue will go to the next
* of the statements, and break will go to the end of the statements.
*/
struct UnrolledLoopStatement : Statement
{
Statements *statements;
UnrolledLoopStatement(Loc loc, Statements *statements);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct ScopeStatement : Statement
{
Statement *statement;
ScopeStatement(Loc loc, Statement *s);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
ScopeStatement *isScopeStatement() { return this; }
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
int isEmpty();
Expression *interpret(InterState *istate);
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct WhileStatement : Statement
{
Expression *condition;
Statement *body;
WhileStatement(Loc loc, Expression *c, Statement *b);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct DoStatement : Statement
{
Statement *body;
Expression *condition;
DoStatement(Loc loc, Statement *b, Expression *c);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct ForStatement : Statement
{
Statement *init;
Expression *condition;
Expression *increment;
Statement *body;
ForStatement(Loc loc, Statement *init, Expression *condition, Expression *increment, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
void scopeCode(Scope *sc, Statement **sentry, Statement **sexit, Statement **sfinally);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int inlineCost(InlineCostState *ics);
Statement *inlineScan(InlineScanState *iss);
Statement *doInlineStatement(InlineDoState *ids);
void toIR(IRState *irs);
};
struct ForeachStatement : Statement
{
enum TOK op; // TOKforeach or TOKforeach_reverse
Parameters *arguments; // array of Parameter*'s
Expression *aggr;
Statement *body;
VarDeclaration *key;
VarDeclaration *value;
FuncDeclaration *func; // function we're lexically in
Statements *cases; // put breaks, continues, gotos and returns here
CompoundStatements *gotos; // forward referenced goto's go here
ForeachStatement(Loc loc, enum TOK op, Parameters *arguments, Expression *aggr, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
bool checkForArgTypes();
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
#if DMDV2
struct ForeachRangeStatement : Statement
{
enum TOK op; // TOKforeach or TOKforeach_reverse
Parameter *arg; // loop index variable
Expression *lwr;
Expression *upr;
Statement *body;
VarDeclaration *key;
ForeachRangeStatement(Loc loc, enum TOK op, Parameter *arg,
Expression *lwr, Expression *upr, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
#endif
struct IfStatement : Statement
{
Parameter *arg;
Expression *condition;
Statement *ifbody;
Statement *elsebody;
VarDeclaration *match; // for MatchExpression results
IfStatement(Loc loc, Parameter *arg, Expression *condition, Statement *ifbody, Statement *elsebody);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int usesEH();
int blockExit(bool mustNotThrow);
IfStatement *isIfStatement() { return this; }
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct ConditionalStatement : Statement
{
Condition *condition;
Statement *ifbody;
Statement *elsebody;
ConditionalStatement(Loc loc, Condition *condition, Statement *ifbody, Statement *elsebody);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Statements *flatten(Scope *sc);
int usesEH();
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct PragmaStatement : Statement
{
Identifier *ident;
Expressions *args; // array of Expression's
Statement *body;
PragmaStatement(Loc loc, Identifier *ident, Expressions *args, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int usesEH();
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct StaticAssertStatement : Statement
{
StaticAssert *sa;
StaticAssertStatement(StaticAssert *sa);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct SwitchStatement : Statement
{
Expression *condition;
Statement *body;
DefaultStatement *sdefault;
GotoCaseStatements gotoCases; // array of unresolved GotoCaseStatement's
CaseStatements *cases; // array of CaseStatement's
int hasNoDefault; // !=0 if no default statement
// LDC
Statement *enclosingScopeExit;
SwitchStatement(Loc loc, Expression *c, Statement *b);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int usesEH();
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct CaseStatement : Statement
{
Expression *exp;
Statement *statement;
int index; // which case it is (since we sort this)
block *cblock; // back end: label for the block
// LDC
Statement *enclosingScopeExit;
CaseStatement(Loc loc, Expression *exp, Statement *s);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int compare(Object *obj);
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
CaseStatement *isCaseStatement() { return this; }
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
#if IN_LLVM
llvm::BasicBlock* bodyBB;
llvm::Value* llvmIdx;
#endif
};
#if DMDV2
struct CaseRangeStatement : Statement
{
Expression *first;
Expression *last;
Statement *statement;
CaseRangeStatement(Loc loc, Expression *first, Expression *last, Statement *s);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
#endif
struct DefaultStatement : Statement
{
Statement *statement;
#if IN_GCC
block *cblock; // back end: label for the block
#endif
// LDC
Statement *enclosingScopeExit;
DefaultStatement(Loc loc, Statement *s);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
DefaultStatement *isDefaultStatement() { return this; }
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
// LDC
llvm::BasicBlock* bodyBB;
};
struct GotoDefaultStatement : Statement
{
SwitchStatement *sw;
GotoDefaultStatement(Loc loc);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toIR(IRState *irs);
};
struct GotoCaseStatement : Statement
{
Expression *exp; // NULL, or which case to goto
CaseStatement *cs; // case statement it resolves to
SwitchStatement *sw;
GotoCaseStatement(Loc loc, Expression *exp);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toIR(IRState *irs);
};
struct SwitchErrorStatement : Statement
{
SwitchErrorStatement(Loc loc);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toIR(IRState *irs);
};
struct ReturnStatement : Statement
{
Expression *exp;
int implicit0;
ReturnStatement(Loc loc, Expression *exp);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *semantic(Scope *sc);
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
int inlineCost(InlineCostState *ics);
Expression *doInline(InlineDoState *ids);
Statement *doInlineStatement(InlineDoState *ids);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
ReturnStatement *isReturnStatement() { return this; }
};
struct BreakStatement : Statement
{
Identifier *ident;
BreakStatement(Loc loc, Identifier *ident);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toIR(IRState *irs);
// LDC: only set if ident is set: label statement to jump to
LabelStatement *target;
};
struct ContinueStatement : Statement
{
Identifier *ident;
ContinueStatement(Loc loc, Identifier *ident);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Expression *interpret(InterState *istate);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toIR(IRState *irs);
// LDC: only set if ident is set: label statement to jump to
LabelStatement *target;
};
struct SynchronizedStatement : Statement
{
Expression *exp;
Statement *body;
SynchronizedStatement(Loc loc, Expression *exp, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
// Back end
elem *esync;
SynchronizedStatement(Loc loc, elem *esync, Statement *body);
void toIR(IRState *irs);
llvm::Value* llsync;
};
struct WithStatement : Statement
{
Expression *exp;
Statement *body;
VarDeclaration *wthis;
WithStatement(Loc loc, Expression *exp, Statement *body);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int usesEH();
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct TryCatchStatement : Statement
{
Statement *body;
Catches *catches;
TryCatchStatement(Loc loc, Statement *body, Catches *catches);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int hasBreak();
int usesEH();
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
TryCatchStatement *isTryCatchStatement() { return this; }
};
struct Catch : Object
{
Loc loc;
Type *type;
Identifier *ident;
VarDeclaration *var;
Statement *handler;
Catch(Loc loc, Type *t, Identifier *id, Statement *handler);
Catch *syntaxCopy();
void semantic(Scope *sc);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
struct TryFinallyStatement : Statement
{
Statement *body;
Statement *finalbody;
TryFinallyStatement(Loc loc, Statement *body, Statement *finalbody);
Statement *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *semantic(Scope *sc);
int hasBreak();
int hasContinue();
int usesEH();
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct OnScopeStatement : Statement
{
TOK tok;
Statement *statement;
OnScopeStatement(Loc loc, TOK tok, Statement *statement);
Statement *syntaxCopy();
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *semantic(Scope *sc);
int usesEH();
void scopeCode(Scope *sc, Statement **sentry, Statement **sexit, Statement **sfinally);
Expression *interpret(InterState *istate);
void toIR(IRState *irs);
};
struct ThrowStatement : Statement
{
Expression *exp;
ThrowStatement(Loc loc, Expression *exp);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct VolatileStatement : Statement
{
Statement *statement;
VolatileStatement(Loc loc, Statement *statement);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Statements *flatten(Scope *sc);
int blockExit(bool mustNotThrow);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
void toIR(IRState *irs);
};
struct GotoStatement : Statement
{
Identifier *ident;
LabelDsymbol *label;
TryFinallyStatement *enclosingFinally;
Statement* enclosingScopeExit;
GotoStatement(Loc loc, Identifier *ident);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int blockExit(bool mustNotThrow);
Expression *interpret(InterState *istate);
void toIR(IRState *irs);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
GotoStatement *isGotoStatement() { return this; }
};
struct LabelStatement : Statement
{
Identifier *ident;
Statement *statement;
TryFinallyStatement *enclosingFinally;
Statement* enclosingScopeExit;
block *lblock; // back end
Blocks *fwdrefs; // forward references to this LabelStatement
LabelStatement(Loc loc, Identifier *ident, Statement *statement);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
Statements *flatten(Scope *sc);
int usesEH();
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Statement *inlineScan(InlineScanState *iss);
LabelStatement *isLabelStatement() { return this; }
void toIR(IRState *irs);
// LDC
bool asmLabel; // for labels inside inline assembler
void toNakedIR(IRState *irs);
};
struct LabelDsymbol : Dsymbol
{
LabelStatement *statement;
#if IN_GCC
unsigned asmLabelNum; // GCC-specific
#endif
LabelDsymbol(Identifier *ident);
LabelDsymbol *isLabel();
};
struct AsmStatement : Statement
{
Token *tokens;
code *asmcode;
unsigned asmalign; // alignment of this statement
unsigned regs; // mask of registers modified (must match regm_t in back end)
unsigned char refparam; // !=0 if function parameter is referenced
unsigned char naked; // !=0 if function is to be naked
AsmStatement(Loc loc, Token *tokens);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
int blockExit(bool mustNotThrow);
int comeFrom();
Expression *interpret(InterState *istate);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
virtual AsmStatement *isAsmStatement() { return this; }
void toIR(IRState *irs);
// LDC
// non-zero if this is a branch, contains the target labels identifier
Identifier* isBranchToLabel;
void toNakedIR(IRState *irs);
};
struct AsmBlockStatement : CompoundStatement
{
TryFinallyStatement* enclosingFinally;
Statement* enclosingScopeExit;
AsmBlockStatement(Loc loc, Statements *s);
Statements *flatten(Scope *sc);
Statement *syntaxCopy();
Statement *semantic(Scope *sc);
CompoundStatement *isCompoundStatement() { return NULL; }
AsmBlockStatement *isAsmBlockStatement() { return this; }
void toIR(IRState *irs);
void toNakedIR(IRState *irs);
AsmBlockStatement* endsWithAsm();
llvm::Value* abiret;
};
#endif /* DMD_STATEMENT_H */

View File

@@ -1,126 +0,0 @@
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// http://www.dsource.org/projects/dmd/browser/branches/dmd-1.x/src/staticassert.c
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "dsymbol.h"
#include "staticassert.h"
#include "expression.h"
#include "id.h"
#include "hdrgen.h"
#include "scope.h"
#include "template.h"
#include "declaration.h"
/********************************* AttribDeclaration ****************************/
StaticAssert::StaticAssert(Loc loc, Expression *exp, Expression *msg)
: Dsymbol(Id::empty)
{
this->loc = loc;
this->exp = exp;
this->msg = msg;
}
Dsymbol *StaticAssert::syntaxCopy(Dsymbol *s)
{
StaticAssert *sa;
assert(!s);
sa = new StaticAssert(loc, exp->syntaxCopy(), msg ? msg->syntaxCopy() : NULL);
return sa;
}
int StaticAssert::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
{
return 0; // we didn't add anything
}
void StaticAssert::semantic(Scope *sc)
{
}
void StaticAssert::semantic2(Scope *sc)
{
//printf("StaticAssert::semantic2() %s\n", toChars());
Expression *e = exp->semantic(sc);
if (!e->type->checkBoolean())
{
if (e->type->toBasetype() != Type::terror)
exp->error("expression %s of type %s does not have a boolean value", exp->toChars(), e->type->toChars());
return;
}
unsigned olderrs = global.errors;
e = e->ctfeInterpret();
if (global.errors != olderrs)
{
errorSupplemental(loc, "while evaluating: static assert(%s)", exp->toChars());
}
else if (e->isBool(FALSE))
{
if (msg)
{ HdrGenState hgs;
OutBuffer buf;
msg = msg->semantic(sc);
msg = msg->ctfeInterpret();
hgs.console = 1;
msg->toCBuffer(&buf, &hgs);
error("%s", buf.toChars());
}
else
error("(%s) is false", exp->toChars());
if (sc->tinst)
sc->tinst->printInstantiationTrace();
if (!global.gag)
fatal();
}
else if (!e->isBool(TRUE))
{
error("(%s) is not evaluatable at compile time", exp->toChars());
}
}
int StaticAssert::oneMember(Dsymbol **ps)
{
//printf("StaticAssert::oneMember())\n");
*ps = NULL;
return TRUE;
}
void StaticAssert::inlineScan()
{
}
void StaticAssert::toObjFile(int multiobj)
{
}
const char *StaticAssert::kind()
{
return "static assert";
}
void StaticAssert::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring(kind());
buf->writeByte('(');
exp->toCBuffer(buf, hgs);
if (msg)
{
buf->writeByte(',');
msg->toCBuffer(buf, hgs);
}
buf->writestring(");");
buf->writenl();
}

View File

@@ -1,41 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_STATICASSERT_H
#define DMD_STATICASSERT_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include "dsymbol.h"
struct Expression;
struct HdrGenState;
struct StaticAssert : Dsymbol
{
Expression *exp;
Expression *msg;
StaticAssert(Loc loc, Expression *exp, Expression *msg);
Dsymbol *syntaxCopy(Dsymbol *s);
int addMember(Scope *sc, ScopeDsymbol *sd, int memnum);
void semantic(Scope *sc);
void semantic2(Scope *sc);
void inlineScan();
int oneMember(Dsymbol **ps);
void toObjFile(int multiobj);
const char *kind();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
#endif

View File

@@ -1,683 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <assert.h>
#include "root.h"
#include "aggregate.h"
#include "scope.h"
#include "mtype.h"
#include "declaration.h"
#include "module.h"
#include "id.h"
#include "statement.h"
#include "template.h"
/********************************* AggregateDeclaration ****************************/
AggregateDeclaration::AggregateDeclaration(Loc loc, Identifier *id)
: ScopeDsymbol(id)
{
this->loc = loc;
storage_class = 0;
protection = PROTpublic;
type = NULL;
handle = NULL;
structsize = 0; // size of struct
alignsize = 0; // size of struct for alignment purposes
structalign = 0; // struct member alignment in effect
hasUnions = 0;
sizeok = SIZEOKnone; // size not determined yet
isdeprecated = 0;
inv = NULL;
aggNew = NULL;
aggDelete = NULL;
#if IN_DMD
stag = NULL;
sinit = NULL;
#endif
#if DMDV2
dtor = NULL;
ctor = NULL;
defaultCtor = NULL;
aliasthis = NULL;
noDefaultCtor = FALSE;
#endif
dtor = NULL;
#if IN_LLVM
availableExternally = true; // assume this unless proven otherwise
#endif
}
enum PROT AggregateDeclaration::prot()
{
return protection;
}
void AggregateDeclaration::semantic2(Scope *sc)
{
//printf("AggregateDeclaration::semantic2(%s)\n", toChars());
if (scope && members)
{ error("has forward references");
return;
}
if (members)
{
sc = sc->push(this);
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = members->tdata()[i];
s->semantic2(sc);
}
sc->pop();
}
}
void AggregateDeclaration::semantic3(Scope *sc)
{
// LDC
if (!global.params.useAvailableExternally)
availableExternally = false;
//printf("AggregateDeclaration::semantic3(%s)\n", toChars());
if (members)
{
sc = sc->push(this);
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = (*members)[i];
s->semantic3(sc);
}
sc->pop();
}
}
void AggregateDeclaration::inlineScan()
{
//printf("AggregateDeclaration::inlineScan(%s)\n", toChars());
if (members)
{
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = (*members)[i];
//printf("inline scan aggregate symbol '%s'\n", s->toChars());
s->inlineScan();
}
}
}
unsigned AggregateDeclaration::size(Loc loc)
{
//printf("AggregateDeclaration::size() %s, scope = %p\n", toChars(), scope);
if (loc.linnum == 0)
loc = this->loc;
if (!members)
error(loc, "unknown size");
if (sizeok != SIZEOKdone && scope)
semantic(NULL);
if (sizeok != SIZEOKdone)
{ error(loc, "no size yet for forward reference");
//*(char*)0=0;
}
return structsize;
}
Type *AggregateDeclaration::getType()
{
return type;
}
int AggregateDeclaration::isDeprecated()
{
return isdeprecated;
}
int AggregateDeclaration::isExport()
{
return protection == PROTexport;
}
/****************************
* Do byte or word alignment as necessary.
* Align sizes of 0, as we may not know array sizes yet.
*/
void AggregateDeclaration::alignmember(
structalign_t salign, // struct alignment that is in effect
unsigned size, // alignment requirement of field
unsigned *poffset)
{
//printf("salign = %d, size = %d, offset = %d\n",salign,size,offset);
if (salign == STRUCTALIGN_DEFAULT)
salign = 8;
if (salign > 1)
{
assert(size != 3);
unsigned sa = size;
if (sa == 0 || salign < sa)
sa = salign;
*poffset = (*poffset + sa - 1) & ~(sa - 1);
}
//printf("result = %d\n", *poffset);
}
/****************************************
* Place a member (mem) into an aggregate (agg), which can be a struct, union or class
* Returns:
* offset to place field at
*/
unsigned AggregateDeclaration::placeField(
unsigned *nextoffset, // next location in aggregate
unsigned memsize, // size of member
unsigned memalignsize, // size of member for alignment purposes
structalign_t memalign, // alignment in effect for this member
unsigned *paggsize, // size of aggregate (updated)
unsigned *paggalignsize, // size of aggregate for alignment purposes (updated)
bool isunion // the aggregate is a union
)
{
unsigned ofs = *nextoffset;
alignmember(memalign, memalignsize, &ofs);
unsigned memoffset = ofs;
ofs += memsize;
if (ofs > *paggsize)
*paggsize = ofs;
if (!isunion)
*nextoffset = ofs;
if (global.params.is64bit && memalign == 8 && memalignsize == 16)
/* Not sure how to handle this */
;
else if (memalign == STRUCTALIGN_DEFAULT && 8 < memalignsize)
memalignsize = 8;
else if (memalign < memalignsize)
memalignsize = memalign;
if (*paggalignsize < memalignsize)
*paggalignsize = memalignsize;
return memoffset;
}
/****************************************
* If field[indx] is not part of a union, return indx.
* Otherwise, return the lowest field index of the union.
*/
int AggregateDeclaration::firstFieldInUnion(int indx)
{
if (isUnionDeclaration())
return 0;
VarDeclaration * vd = fields[indx];
int firstNonZero = indx; // first index in the union with non-zero size
for (; ;)
{
if (indx == 0)
return firstNonZero;
VarDeclaration * v = fields[indx - 1];
if (v->offset != vd->offset)
return firstNonZero;
--indx;
/* If it is a zero-length field, it's ambiguous: we don't know if it is
* in the union unless we find an earlier non-zero sized field with the
* same offset.
*/
if (v->size(loc) != 0)
firstNonZero = indx;
}
}
/****************************************
* Count the number of fields starting at firstIndex which are part of the
* same union as field[firstIndex]. If not a union, return 1.
*/
int AggregateDeclaration::numFieldsInUnion(int firstIndex)
{
VarDeclaration * vd = fields[firstIndex];
/* If it is a zero-length field, AND we can't find an earlier non-zero
* sized field with the same offset, we assume it's not part of a union.
*/
if (vd->size(loc) == 0 && !isUnionDeclaration() &&
firstFieldInUnion(firstIndex) == firstIndex)
return 1;
int count = 1;
for (size_t i = firstIndex+1; i < fields.dim; ++i)
{
VarDeclaration * v = fields[i];
// If offsets are different, they are not in the same union
if (v->offset != vd->offset)
break;
++count;
}
return count;
}
/********************************* StructDeclaration ****************************/
StructDeclaration::StructDeclaration(Loc loc, Identifier *id)
: AggregateDeclaration(loc, id)
{
zeroInit = 0; // assume false until we do semantic processing
#if DMDV2
hasIdentityAssign = 0;
cpctor = NULL;
postblit = NULL;
eq = NULL;
#endif
arg1type = NULL;
arg2type = NULL;
// For forward references
type = new TypeStruct(this);
}
Dsymbol *StructDeclaration::syntaxCopy(Dsymbol *s)
{
StructDeclaration *sd;
if (s)
sd = (StructDeclaration *)s;
else
sd = new StructDeclaration(loc, ident);
ScopeDsymbol::syntaxCopy(sd);
return sd;
}
void StructDeclaration::semantic(Scope *sc)
{
Scope *sc2;
//printf("+StructDeclaration::semantic(this=%p, %s '%s', sizeok = %d)\n", this, parent->toChars(), toChars(), sizeok);
//static int count; if (++count == 20) halt();
assert(type);
if (!members) // if forward reference
return;
if (symtab)
{ if (sizeok == SIZEOKdone || !scope)
{ //printf("already completed\n");
scope = NULL;
return; // semantic() already completed
}
}
else
symtab = new DsymbolTable();
Scope *scx = NULL;
if (scope)
{ sc = scope;
scx = scope; // save so we don't make redundant copies
scope = NULL;
}
unsigned dprogress_save = Module::dprogress;
parent = sc->parent;
type = type->semantic(loc, sc);
#if STRUCTTHISREF
handle = type;
#else
handle = type->pointerTo();
#endif
structalign = sc->structalign;
protection = sc->protection;
if (sc->stc & STCdeprecated)
isdeprecated = 1;
assert(!isAnonymous());
if (sc->stc & STCabstract)
error("structs, unions cannot be abstract");
if (sizeok == SIZEOKnone) // if not already done the addMember step
{
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = (*members)[i];
//printf("adding member '%s' to '%s'\n", s->toChars(), this->toChars());
s->addMember(sc, this, 1);
}
}
sizeok = SIZEOKnone;
sc2 = sc->push(this);
sc2->stc = 0;
sc2->parent = this;
if (isUnionDeclaration())
sc2->inunion = 1;
sc2->protection = PROTpublic;
sc2->explicitProtection = 0;
size_t members_dim = members->dim;
/* Set scope so if there are forward references, we still might be able to
* resolve individual members like enums.
*/
for (size_t i = 0; i < members_dim; i++)
{ Dsymbol *s = (*members)[i];
/* There are problems doing this in the general case because
* Scope keeps track of things like 'offset'
*/
if (s->isEnumDeclaration() || (s->isAggregateDeclaration() && s->ident))
{
//printf("setScope %s %s\n", s->kind(), s->toChars());
s->setScope(sc2);
}
}
for (size_t i = 0; i < members_dim; i++)
{
Dsymbol *s = (*members)[i];
// Ungag errors when not speculative
unsigned oldgag = global.gag;
if (global.isSpeculativeGagging() && !isSpeculative())
{
global.gag = 0;
}
s->semantic(sc2);
global.gag = oldgag;
#if 0
if (sizeok == 2)
{ //printf("forward reference\n");
break;
}
#endif
}
finalizeSize(sc2);
#if DMDV1
/* This doesn't work for DMDV2 because (ref S) and (S) parameter
* lists will overload the same.
*/
/* The TypeInfo_Struct is expecting an opEquals and opCmp with
* a parameter that is a pointer to the struct. But if there
* isn't one, but is an opEquals or opCmp with a value, write
* another that is a shell around the value:
* int opCmp(struct *p) { return opCmp(*p); }
*/
TypeFunction *tfeqptr;
{
Parameters *arguments = new Parameters;
Parameter *arg = new Parameter(STCin, handle, Id::p, NULL);
arguments->push(arg);
tfeqptr = new TypeFunction(arguments, Type::tint32, 0, LINKd);
tfeqptr = (TypeFunction *)tfeqptr->semantic(0, sc);
}
TypeFunction *tfeq;
{
Parameters *arguments = new Parameters;
Parameter *arg = new Parameter(STCin, type, NULL, NULL);
arguments->push(arg);
tfeq = new TypeFunction(arguments, Type::tint32, 0, LINKd);
tfeq = (TypeFunction *)tfeq->semantic(0, sc);
}
Identifier *id = Id::eq;
for (int i = 0; i < 2; i++)
{
Dsymbol *s = search_function(this, id);
FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL;
if (fdx)
{ FuncDeclaration *fd = fdx->overloadExactMatch(tfeqptr, getModule());
if (!fd)
{ fd = fdx->overloadExactMatch(tfeq, getModule());
if (fd)
{ // Create the thunk, fdptr
FuncDeclaration *fdptr = new FuncDeclaration(loc, loc, fdx->ident, STCundefined, tfeqptr);
Expression *e = new IdentifierExp(loc, Id::p);
e = new PtrExp(loc, e);
Expressions *args = new Expressions();
args->push(e);
e = new IdentifierExp(loc, id);
e = new CallExp(loc, e, args);
fdptr->fbody = new ReturnStatement(loc, e);
ScopeDsymbol *s = fdx->parent->isScopeDsymbol();
assert(s);
s->members->push(fdptr);
fdptr->addMember(sc, s, 1);
fdptr->semantic(sc2);
}
}
}
id = Id::cmp;
}
#endif
#if DMDV2
dtor = buildDtor(sc2);
postblit = buildPostBlit(sc2);
cpctor = buildCpCtor(sc2);
buildOpAssign(sc2);
hasIdentityEquals = (buildOpEquals(sc2) != NULL);
xeq = buildXopEquals(sc2);
#endif
sc2->pop();
if (sizeok == 2)
{ // semantic() failed because of forward references.
// Unwind what we did, and defer it for later
for (size_t i = 0; i < fields.dim; i++)
{ Dsymbol *s = fields[i];
VarDeclaration *vd = s->isVarDeclaration();
if (vd)
vd->offset = 0;
}
fields.setDim(0);
structsize = 0;
alignsize = 0;
structalign = 0;
scope = scx ? scx : new Scope(*sc);
scope->setNoFree();
scope->module->addDeferredSemantic(this);
Module::dprogress = dprogress_save;
//printf("\tdeferring %s\n", toChars());
return;
}
// 0 sized struct's are set to 1 byte
if (structsize == 0)
{
structsize = 1;
alignsize = 1;
}
// Round struct size up to next alignsize boundary.
// This will ensure that arrays of structs will get their internals
// aligned properly.
structsize = (structsize + alignsize - 1) & ~(alignsize - 1);
sizeok = SIZEOKdone;
Module::dprogress++;
//printf("-StructDeclaration::semantic(this=%p, '%s')\n", this, toChars());
// Determine if struct is all zeros or not
zeroInit = 1;
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = (Dsymbol *)fields.data[i];
VarDeclaration *vd = s->isVarDeclaration();
if (vd && !vd->isDataseg())
{
if (vd->init)
{
// Should examine init to see if it is really all 0's
zeroInit = 0;
break;
}
else
{
if (!vd->type->isZeroInit(loc))
{
zeroInit = 0;
break;
}
}
}
}
/* Look for special member functions.
*/
#if DMDV2
ctor = (CtorDeclaration *)search(0, Id::ctor, 0);
#endif
inv = (InvariantDeclaration *)search(0, Id::classInvariant, 0);
aggNew = (NewDeclaration *)search(0, Id::classNew, 0);
aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0);
TypeTuple *tup = type->toArgTypes();
size_t dim = tup->arguments->dim;
if (dim >= 1)
{ assert(dim <= 2);
arg1type = (*tup->arguments)[0]->type;
if (dim == 2)
arg2type = (*tup->arguments)[1]->type;
}
if (sc->func)
{
semantic2(sc);
semantic3(sc);
}
}
Dsymbol *StructDeclaration::search(Loc loc, Identifier *ident, int flags)
{
//printf("%s.StructDeclaration::search('%s')\n", toChars(), ident->toChars());
if (scope && !symtab)
semantic(scope);
if (!members || !symtab)
{
error("is forward referenced when looking for '%s'", ident->toChars());
return NULL;
}
return ScopeDsymbol::search(loc, ident, flags);
}
void StructDeclaration::finalizeSize(Scope *sc)
{
//printf("StructDeclaration::finalizeSize() %s\n", toChars());
if (sizeok != SIZEOKnone)
return;
// Set the offsets of the fields and determine the size of the struct
unsigned offset = 0;
bool isunion = isUnionDeclaration() != NULL;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = (*members)[i];
s->setFieldOffset(this, &offset, isunion);
}
if (sizeok == SIZEOKfwd)
return;
// 0 sized struct's are set to 1 byte
if (structsize == 0)
{
structsize = 1;
alignsize = 1;
}
// Round struct size up to next alignsize boundary.
// This will ensure that arrays of structs will get their internals
// aligned properly.
structsize = (structsize + alignsize - 1) & ~(alignsize - 1);
sizeok = SIZEOKdone;
}
/***************************************
* Return true if struct is POD (Plain Old Data).
* This is defined as:
* not nested
* no postblits, constructors, destructors, or assignment operators
* no fields with with any of those
* The idea being these are compatible with C structs.
*
* Note that D struct constructors can mean POD, since there is always default
* construction with no ctor, but that interferes with OPstrpar which wants it
* on the stack in memory, not in registers.
*/
bool StructDeclaration::isPOD()
{
return true;
}
void StructDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->printf("%s ", kind());
if (!isAnonymous())
buf->writestring(toChars());
if (!members)
{
buf->writeByte(';');
buf->writenl();
return;
}
buf->writenl();
buf->writeByte('{');
buf->writenl();
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = (*members)[i];
buf->writestring(" ");
s->toCBuffer(buf, hgs);
}
buf->writeByte('}');
buf->writenl();
}
const char *StructDeclaration::kind()
{
return "struct";
}
/********************************* UnionDeclaration ****************************/
UnionDeclaration::UnionDeclaration(Loc loc, Identifier *id)
: StructDeclaration(loc, id)
{
}
Dsymbol *UnionDeclaration::syntaxCopy(Dsymbol *s)
{
UnionDeclaration *ud;
if (s)
ud = (UnionDeclaration *)s;
else
ud = new UnionDeclaration(loc, ident);
StructDeclaration::syntaxCopy(ud);
return ud;
}
const char *UnionDeclaration::kind()
{
return "union";
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,388 +0,0 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_TEMPLATE_H
#define DMD_TEMPLATE_H
#ifdef __DMC__
#pragma once
#endif /* __DMC__ */
#include <string>
#include "root.h"
#include "arraytypes.h"
#include "dsymbol.h"
#include "mtype.h"
struct OutBuffer;
struct Identifier;
struct TemplateInstance;
struct TemplateParameter;
struct TemplateTypeParameter;
struct TemplateThisParameter;
struct TemplateValueParameter;
struct TemplateAliasParameter;
struct TemplateTupleParameter;
struct Type;
struct TypeTypeof;
struct Scope;
struct Expression;
struct AliasDeclaration;
struct FuncDeclaration;
struct HdrGenState;
enum MATCH;
enum PASS;
struct Tuple : Object
{
Objects objects;
int dyncast() { return DYNCAST_TUPLE; } // kludge for template.isType()
};
struct TemplateDeclaration : ScopeDsymbol
{
TemplateParameters *parameters; // array of TemplateParameter's
TemplateParameters *origParameters; // originals for Ddoc
Expression *constraint;
TemplateInstances instances; // array of TemplateInstance's
TemplateDeclaration *overnext; // next overloaded TemplateDeclaration
TemplateDeclaration *overroot; // first in overnext list
enum PASS semanticRun; // 1 semantic() run
Dsymbol *onemember; // if !=NULL then one member of this template
TemplateDeclaration(Loc loc, Identifier *id, TemplateParameters *parameters,
Expression *constraint, Dsymbols *decldefs);
Dsymbol *syntaxCopy(Dsymbol *);
void semantic(Scope *sc);
int overloadInsert(Dsymbol *s);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
bool hasStaticCtorOrDtor();
const char *kind();
char *toChars();
void emitComment(Scope *sc);
void toJsonBuffer(OutBuffer *buf);
// void toDocBuffer(OutBuffer *buf);
MATCH matchWithInstance(TemplateInstance *ti, Objects *atypes, int flag);
MATCH leastAsSpecialized(TemplateDeclaration *td2);
MATCH deduceFunctionTemplateMatch(Loc loc, Objects *targsi, Expression *ethis, Expressions *fargs, Objects *dedargs);
FuncDeclaration *deduceFunctionTemplate(Scope *sc, Loc loc, Objects *targsi, Expression *ethis, Expressions *fargs, int flags = 0);
void declareParameter(Scope *sc, TemplateParameter *tp, Object *o);
TemplateDeclaration *isTemplateDeclaration() { return this; }
TemplateTupleParameter *isVariadic();
int isOverloadable();
void makeParamNamesVisibleInConstraint(Scope *paramscope);
#if IN_LLVM
// LDC
std::string intrinsicName;
#endif
};
struct TemplateParameter
{
/* For type-parameter:
* template Foo(ident) // specType is set to NULL
* template Foo(ident : specType)
* For value-parameter:
* template Foo(valType ident) // specValue is set to NULL
* template Foo(valType ident : specValue)
* For alias-parameter:
* template Foo(alias ident)
* For this-parameter:
* template Foo(this ident)
*/
Loc loc;
Identifier *ident;
Declaration *sparam;
TemplateParameter(Loc loc, Identifier *ident);
virtual TemplateTypeParameter *isTemplateTypeParameter();
virtual TemplateValueParameter *isTemplateValueParameter();
virtual TemplateAliasParameter *isTemplateAliasParameter();
#if DMDV2
virtual TemplateThisParameter *isTemplateThisParameter();
#endif
virtual TemplateTupleParameter *isTemplateTupleParameter();
virtual TemplateParameter *syntaxCopy() = 0;
virtual void declareParameter(Scope *sc) = 0;
virtual void semantic(Scope *) = 0;
virtual void print(Object *oarg, Object *oded) = 0;
virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs) = 0;
virtual Object *specialization() = 0;
virtual Object *defaultArg(Loc loc, Scope *sc) = 0;
/* If TemplateParameter's match as far as overloading goes.
*/
virtual int overloadMatch(TemplateParameter *) = 0;
/* Match actual argument against parameter.
*/
virtual MATCH matchArg(Scope *sc, Objects *tiargs, size_t i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags = 0) = 0;
/* Create dummy argument based on parameter.
*/
virtual void *dummyArg() = 0;
};
struct TemplateTypeParameter : TemplateParameter
{
/* Syntax:
* ident : specType = defaultType
*/
Type *specType; // type parameter: if !=NULL, this is the type specialization
Type *defaultType;
TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
TemplateTypeParameter *isTemplateTypeParameter();
TemplateParameter *syntaxCopy();
void declareParameter(Scope *sc);
void semantic(Scope *);
void print(Object *oarg, Object *oded);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Object *specialization();
Object *defaultArg(Loc loc, Scope *sc);
int overloadMatch(TemplateParameter *);
MATCH matchArg(Scope *sc, Objects *tiargs, size_t i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
void *dummyArg();
};
#if DMDV2
struct TemplateThisParameter : TemplateTypeParameter
{
/* Syntax:
* this ident : specType = defaultType
*/
Type *specType; // type parameter: if !=NULL, this is the type specialization
Type *defaultType;
TemplateThisParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
TemplateThisParameter *isTemplateThisParameter();
TemplateParameter *syntaxCopy();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
};
#endif
struct TemplateValueParameter : TemplateParameter
{
/* Syntax:
* valType ident : specValue = defaultValue
*/
Type *valType;
Expression *specValue;
Expression *defaultValue;
static AA *edummies;
TemplateValueParameter(Loc loc, Identifier *ident, Type *valType, Expression *specValue, Expression *defaultValue);
TemplateValueParameter *isTemplateValueParameter();
TemplateParameter *syntaxCopy();
void declareParameter(Scope *sc);
void semantic(Scope *);
void print(Object *oarg, Object *oded);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Object *specialization();
Object *defaultArg(Loc loc, Scope *sc);
int overloadMatch(TemplateParameter *);
MATCH matchArg(Scope *sc, Objects *tiargs, size_t i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
void *dummyArg();
};
struct TemplateAliasParameter : TemplateParameter
{
/* Syntax:
* specType ident : specAlias = defaultAlias
*/
Type *specAliasT;
Dsymbol *specAlias;
Type *defaultAlias;
static Dsymbol *sdummy;
TemplateAliasParameter(Loc loc, Identifier *ident, Type *specAliasT, Type *defaultAlias);
TemplateAliasParameter *isTemplateAliasParameter();
TemplateParameter *syntaxCopy();
void declareParameter(Scope *sc);
void semantic(Scope *);
void print(Object *oarg, Object *oded);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Object *specialization();
Object *defaultArg(Loc loc, Scope *sc);
int overloadMatch(TemplateParameter *);
MATCH matchArg(Scope *sc, Objects *tiargs, size_t i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
void *dummyArg();
};
struct TemplateTupleParameter : TemplateParameter
{
/* Syntax:
* ident ...
*/
TemplateTupleParameter(Loc loc, Identifier *ident);
TemplateTupleParameter *isTemplateTupleParameter();
TemplateParameter *syntaxCopy();
void declareParameter(Scope *sc);
void semantic(Scope *);
void print(Object *oarg, Object *oded);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Object *specialization();
Object *defaultArg(Loc loc, Scope *sc);
int overloadMatch(TemplateParameter *);
MATCH matchArg(Scope *sc, Objects *tiargs, size_t i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
void *dummyArg();
};
struct TemplateInstance : ScopeDsymbol
{
/* Given:
* foo!(args) =>
* name = foo
* tiargs = args
*/
Identifier *name;
//Identifiers idents;
Objects *tiargs; // Array of Types/Expressions of template
// instance arguments [int*, char, 10*10]
Objects tdtypes; // Array of Types/Expressions corresponding
// to TemplateDeclaration.parameters
// [int, char, 100]
TemplateDeclaration *tempdecl; // referenced by foo.bar.abc
TemplateInstance *inst; // refer to existing instance
TemplateInstance *tinst; // enclosing template instance
ScopeDsymbol *argsym; // argument symbol table
AliasDeclaration *aliasdecl; // !=NULL if instance is an alias for its
// sole member
WithScopeSymbol *withsym; // if a member of a with statement
enum PASS semanticRun; // has semantic() been done?
int semantictiargsdone; // has semanticTiargs() been done?
int nest; // for recursion detection
int havetempdecl; // 1 if used second constructor
Dsymbol *isnested; // if referencing local symbols, this is the context
int speculative; // 1 if only instantiated with errors gagged
#ifdef IN_GCC
/* On some targets, it is necessary to know whether a symbol
will be emitted in the output or not before the symbol
is used. This can be different from getModule(). */
Module * objFileModule;
#endif
TemplateInstance(Loc loc, Identifier *temp_id);
TemplateInstance(Loc loc, TemplateDeclaration *tempdecl, Objects *tiargs);
static Objects *arraySyntaxCopy(Objects *objs);
Dsymbol *syntaxCopy(Dsymbol *);
void semantic(Scope *sc);
void semantic2(Scope *sc);
void semantic3(Scope *sc);
void inlineScan();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
Dsymbol *toAlias(); // resolve real symbol
const char *kind();
int oneMember(Dsymbol **ps);
int needsTypeInference(Scope *sc);
char *toChars();
char *mangle();
void printInstantiationTrace();
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
#endif
// Internal
static void semanticTiargs(Loc loc, Scope *sc, Objects *tiargs, int flags);
void semanticTiargs(Scope *sc);
TemplateDeclaration *findTemplateDeclaration(Scope *sc);
TemplateDeclaration *findBestMatch(Scope *sc);
void declareParameters(Scope *sc);
int hasNestedArgs(Objects *tiargs);
Identifier *genIdent();
TemplateInstance *isTemplateInstance() { return this; }
AliasDeclaration *isAliasDeclaration();
#if IN_LLVM
// LDC
Module* tmodule; // module from outermost enclosing template instantiation
Module* emittedInModule; // which module this template instance has been emitted in
void codegen(Ir*);
#endif
};
struct TemplateMixin : TemplateInstance
{
Identifiers *idents;
Type *tqual;
TemplateMixin(Loc loc, Identifier *ident, Type *tqual, Identifiers *idents, Objects *tiargs);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
void semantic2(Scope *sc);
void semantic3(Scope *sc);
void inlineScan();
const char *kind();
int oneMember(Dsymbol **ps);
int apply(Dsymbol_apply_ft_t fp, void *param);
int hasPointers();
void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
char *toChars();
char *mangle();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
#if IN_DMD
void toObjFile(int multiobj); // compile to .obj file
#endif
TemplateMixin *isTemplateMixin() { return this; }
#if IN_LLVM
void codegen(Ir*);
#endif
};
Expression *isExpression(Object *o);
Dsymbol *isDsymbol(Object *o);
Type *isType(Object *o);
Tuple *isTuple(Object *o);
int arrayObjectIsError(Objects *args);
int isError(Object *o);
Type *getType(Object *o);
Dsymbol *getDsymbol(Object *o);
void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg);
#endif /* DMD_TEMPLATE_H */

View File

@@ -1,323 +0,0 @@
// Copyright (c) 2003 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <assert.h>
/*******************************
* Return !=0 if unicode alpha.
* Use table from C99 Appendix D.
*/
int isUniAlpha(unsigned u)
{
static unsigned short table[][2] =
{
{ 0x00AA, 0x00AA },
{ 0x00B5, 0x00B5 },
{ 0x00B7, 0x00B7 },
{ 0x00BA, 0x00BA },
{ 0x00C0, 0x00D6 },
{ 0x00D8, 0x00F6 },
{ 0x00F8, 0x01F5 },
{ 0x01FA, 0x0217 },
{ 0x0250, 0x02A8 },
{ 0x02B0, 0x02B8 },
{ 0x02BB, 0x02BB },
{ 0x02BD, 0x02C1 },
{ 0x02D0, 0x02D1 },
{ 0x02E0, 0x02E4 },
{ 0x037A, 0x037A },
{ 0x0386, 0x0386 },
{ 0x0388, 0x038A },
{ 0x038C, 0x038C },
{ 0x038E, 0x03A1 },
{ 0x03A3, 0x03CE },
{ 0x03D0, 0x03D6 },
{ 0x03DA, 0x03DA },
{ 0x03DC, 0x03DC },
{ 0x03DE, 0x03DE },
{ 0x03E0, 0x03E0 },
{ 0x03E2, 0x03F3 },
{ 0x0401, 0x040C },
{ 0x040E, 0x044F },
{ 0x0451, 0x045C },
{ 0x045E, 0x0481 },
{ 0x0490, 0x04C4 },
{ 0x04C7, 0x04C8 },
{ 0x04CB, 0x04CC },
{ 0x04D0, 0x04EB },
{ 0x04EE, 0x04F5 },
{ 0x04F8, 0x04F9 },
{ 0x0531, 0x0556 },
{ 0x0559, 0x0559 },
{ 0x0561, 0x0587 },
{ 0x05B0, 0x05B9 },
{ 0x05BB, 0x05BD },
{ 0x05BF, 0x05BF },
{ 0x05C1, 0x05C2 },
{ 0x05D0, 0x05EA },
{ 0x05F0, 0x05F2 },
{ 0x0621, 0x063A },
{ 0x0640, 0x0652 },
{ 0x0660, 0x0669 },
{ 0x0670, 0x06B7 },
{ 0x06BA, 0x06BE },
{ 0x06C0, 0x06CE },
{ 0x06D0, 0x06DC },
{ 0x06E5, 0x06E8 },
{ 0x06EA, 0x06ED },
{ 0x06F0, 0x06F9 },
{ 0x0901, 0x0903 },
{ 0x0905, 0x0939 },
{ 0x093D, 0x093D },
{ 0x093E, 0x094D },
{ 0x0950, 0x0952 },
{ 0x0958, 0x0963 },
{ 0x0966, 0x096F },
{ 0x0981, 0x0983 },
{ 0x0985, 0x098C },
{ 0x098F, 0x0990 },
{ 0x0993, 0x09A8 },
{ 0x09AA, 0x09B0 },
{ 0x09B2, 0x09B2 },
{ 0x09B6, 0x09B9 },
{ 0x09BE, 0x09C4 },
{ 0x09C7, 0x09C8 },
{ 0x09CB, 0x09CD },
{ 0x09DC, 0x09DD },
{ 0x09DF, 0x09E3 },
{ 0x09E6, 0x09EF },
{ 0x09F0, 0x09F1 },
{ 0x0A02, 0x0A02 },
{ 0x0A05, 0x0A0A },
{ 0x0A0F, 0x0A10 },
{ 0x0A13, 0x0A28 },
{ 0x0A2A, 0x0A30 },
{ 0x0A32, 0x0A33 },
{ 0x0A35, 0x0A36 },
{ 0x0A38, 0x0A39 },
{ 0x0A3E, 0x0A42 },
{ 0x0A47, 0x0A48 },
{ 0x0A4B, 0x0A4D },
{ 0x0A59, 0x0A5C },
{ 0x0A5E, 0x0A5E },
{ 0x0A66, 0x0A6F },
{ 0x0A74, 0x0A74 },
{ 0x0A81, 0x0A83 },
{ 0x0A85, 0x0A8B },
{ 0x0A8D, 0x0A8D },
{ 0x0A8F, 0x0A91 },
{ 0x0A93, 0x0AA8 },
{ 0x0AAA, 0x0AB0 },
{ 0x0AB2, 0x0AB3 },
{ 0x0AB5, 0x0AB9 },
{ 0x0ABD, 0x0AC5 },
{ 0x0AC7, 0x0AC9 },
{ 0x0ACB, 0x0ACD },
{ 0x0AD0, 0x0AD0 },
{ 0x0AE0, 0x0AE0 },
{ 0x0AE6, 0x0AEF },
{ 0x0B01, 0x0B03 },
{ 0x0B05, 0x0B0C },
{ 0x0B0F, 0x0B10 },
{ 0x0B13, 0x0B28 },
{ 0x0B2A, 0x0B30 },
{ 0x0B32, 0x0B33 },
{ 0x0B36, 0x0B39 },
{ 0x0B3D, 0x0B3D },
{ 0x0B3E, 0x0B43 },
{ 0x0B47, 0x0B48 },
{ 0x0B4B, 0x0B4D },
{ 0x0B5C, 0x0B5D },
{ 0x0B5F, 0x0B61 },
{ 0x0B66, 0x0B6F },
{ 0x0B82, 0x0B83 },
{ 0x0B85, 0x0B8A },
{ 0x0B8E, 0x0B90 },
{ 0x0B92, 0x0B95 },
{ 0x0B99, 0x0B9A },
{ 0x0B9C, 0x0B9C },
{ 0x0B9E, 0x0B9F },
{ 0x0BA3, 0x0BA4 },
{ 0x0BA8, 0x0BAA },
{ 0x0BAE, 0x0BB5 },
{ 0x0BB7, 0x0BB9 },
{ 0x0BBE, 0x0BC2 },
{ 0x0BC6, 0x0BC8 },
{ 0x0BCA, 0x0BCD },
{ 0x0BE7, 0x0BEF },
{ 0x0C01, 0x0C03 },
{ 0x0C05, 0x0C0C },
{ 0x0C0E, 0x0C10 },
{ 0x0C12, 0x0C28 },
{ 0x0C2A, 0x0C33 },
{ 0x0C35, 0x0C39 },
{ 0x0C3E, 0x0C44 },
{ 0x0C46, 0x0C48 },
{ 0x0C4A, 0x0C4D },
{ 0x0C60, 0x0C61 },
{ 0x0C66, 0x0C6F },
{ 0x0C82, 0x0C83 },
{ 0x0C85, 0x0C8C },
{ 0x0C8E, 0x0C90 },
{ 0x0C92, 0x0CA8 },
{ 0x0CAA, 0x0CB3 },
{ 0x0CB5, 0x0CB9 },
{ 0x0CBE, 0x0CC4 },
{ 0x0CC6, 0x0CC8 },
{ 0x0CCA, 0x0CCD },
{ 0x0CDE, 0x0CDE },
{ 0x0CE0, 0x0CE1 },
{ 0x0CE6, 0x0CEF },
{ 0x0D02, 0x0D03 },
{ 0x0D05, 0x0D0C },
{ 0x0D0E, 0x0D10 },
{ 0x0D12, 0x0D28 },
{ 0x0D2A, 0x0D39 },
{ 0x0D3E, 0x0D43 },
{ 0x0D46, 0x0D48 },
{ 0x0D4A, 0x0D4D },
{ 0x0D60, 0x0D61 },
{ 0x0D66, 0x0D6F },
{ 0x0E01, 0x0E3A },
{ 0x0E40, 0x0E5B },
// { 0x0E50, 0x0E59 },
{ 0x0E81, 0x0E82 },
{ 0x0E84, 0x0E84 },
{ 0x0E87, 0x0E88 },
{ 0x0E8A, 0x0E8A },
{ 0x0E8D, 0x0E8D },
{ 0x0E94, 0x0E97 },
{ 0x0E99, 0x0E9F },
{ 0x0EA1, 0x0EA3 },
{ 0x0EA5, 0x0EA5 },
{ 0x0EA7, 0x0EA7 },
{ 0x0EAA, 0x0EAB },
{ 0x0EAD, 0x0EAE },
{ 0x0EB0, 0x0EB9 },
{ 0x0EBB, 0x0EBD },
{ 0x0EC0, 0x0EC4 },
{ 0x0EC6, 0x0EC6 },
{ 0x0EC8, 0x0ECD },
{ 0x0ED0, 0x0ED9 },
{ 0x0EDC, 0x0EDD },
{ 0x0F00, 0x0F00 },
{ 0x0F18, 0x0F19 },
{ 0x0F20, 0x0F33 },
{ 0x0F35, 0x0F35 },
{ 0x0F37, 0x0F37 },
{ 0x0F39, 0x0F39 },
{ 0x0F3E, 0x0F47 },
{ 0x0F49, 0x0F69 },
{ 0x0F71, 0x0F84 },
{ 0x0F86, 0x0F8B },
{ 0x0F90, 0x0F95 },
{ 0x0F97, 0x0F97 },
{ 0x0F99, 0x0FAD },
{ 0x0FB1, 0x0FB7 },
{ 0x0FB9, 0x0FB9 },
{ 0x10A0, 0x10C5 },
{ 0x10D0, 0x10F6 },
{ 0x1E00, 0x1E9B },
{ 0x1EA0, 0x1EF9 },
{ 0x1F00, 0x1F15 },
{ 0x1F18, 0x1F1D },
{ 0x1F20, 0x1F45 },
{ 0x1F48, 0x1F4D },
{ 0x1F50, 0x1F57 },
{ 0x1F59, 0x1F59 },
{ 0x1F5B, 0x1F5B },
{ 0x1F5D, 0x1F5D },
{ 0x1F5F, 0x1F7D },
{ 0x1F80, 0x1FB4 },
{ 0x1FB6, 0x1FBC },
{ 0x1FBE, 0x1FBE },
{ 0x1FC2, 0x1FC4 },
{ 0x1FC6, 0x1FCC },
{ 0x1FD0, 0x1FD3 },
{ 0x1FD6, 0x1FDB },
{ 0x1FE0, 0x1FEC },
{ 0x1FF2, 0x1FF4 },
{ 0x1FF6, 0x1FFC },
{ 0x203F, 0x2040 },
{ 0x207F, 0x207F },
{ 0x2102, 0x2102 },
{ 0x2107, 0x2107 },
{ 0x210A, 0x2113 },
{ 0x2115, 0x2115 },
{ 0x2118, 0x211D },
{ 0x2124, 0x2124 },
{ 0x2126, 0x2126 },
{ 0x2128, 0x2128 },
{ 0x212A, 0x2131 },
{ 0x2133, 0x2138 },
{ 0x2160, 0x2182 },
{ 0x3005, 0x3007 },
{ 0x3021, 0x3029 },
{ 0x3041, 0x3093 },
{ 0x309B, 0x309C },
{ 0x30A1, 0x30F6 },
{ 0x30FB, 0x30FC },
{ 0x3105, 0x312C },
{ 0x4E00, 0x9FA5 },
{ 0xAC00, 0xD7A3 },
};
#ifdef DEBUG
for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
{
//printf("%x\n", table[i][0]);
assert(table[i][0] <= table[i][1]);
if (i < sizeof(table) / sizeof(table[0]) - 1)
assert(table[i][1] < table[i + 1][0]);
}
#endif
if (u > 0xD7A3)
goto Lisnot;
// Binary search
int mid;
int low;
int high;
low = 0;
high = sizeof(table) / sizeof(table[0]) - 1;
while (low <= high)
{
mid = (low + high) >> 1;
if (u < table[mid][0])
high = mid - 1;
else if (u > table[mid][1])
low = mid + 1;
else
goto Lis;
}
Lisnot:
#ifdef DEBUG
for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
{
assert(u < table[i][0] || u > table[i][1]);
}
#endif
return 0;
Lis:
#ifdef DEBUG
for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
{
if (u >= table[i][0] && u <= table[i][1])
return 1;
}
assert(0); // should have been in table
#endif
return 1;
}

320
dmd/utf.c
View File

@@ -1,320 +0,0 @@
// utf.c
// Copyright (c) 2003-2009 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
// Description of UTF-8 at:
// http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "utf.h"
int utf_isValidDchar(dchar_t c)
{
return c < 0xD800 ||
(c > 0xDFFF && c <= 0x10FFFF && c != 0xFFFE && c != 0xFFFF);
}
static const unsigned char UTF8stride[256] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,5,5,5,5,6,6,0xFF,0xFF,
};
/**
* stride() returns the length of a UTF-8 sequence starting at index i
* in string s.
* Returns:
* The number of bytes in the UTF-8 sequence or
* 0xFF meaning s[i] is not the start of of UTF-8 sequence.
*/
unsigned stride(unsigned char* s, size_t i)
{
unsigned result = UTF8stride[s[i]];
return result;
}
/********************************************
* Decode a single UTF-8 character sequence.
* Returns:
* NULL success
* !=NULL error message string
*/
const char *utf_decodeChar(unsigned char *s, size_t len, size_t *pidx, dchar_t *presult)
{
dchar_t V;
size_t i = *pidx;
unsigned char u = s[i];
//printf("utf_decodeChar(s = %02x, %02x, %02x len = %d)\n", u, s[1], s[2], len);
assert(i >= 0 && i < len);
if (u & 0x80)
{ unsigned n;
unsigned char u2;
/* The following encodings are valid, except for the 5 and 6 byte
* combinations:
* 0xxxxxxx
* 110xxxxx 10xxxxxx
* 1110xxxx 10xxxxxx 10xxxxxx
* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
for (n = 1; ; n++)
{
if (n > 4)
goto Lerr; // only do the first 4 of 6 encodings
if (((u << n) & 0x80) == 0)
{
if (n == 1)
goto Lerr;
break;
}
}
// Pick off (7 - n) significant bits of B from first byte of octet
V = (dchar_t)(u & ((1 << (7 - n)) - 1));
if (i + (n - 1) >= len)
goto Lerr; // off end of string
/* The following combinations are overlong, and illegal:
* 1100000x (10xxxxxx)
* 11100000 100xxxxx (10xxxxxx)
* 11110000 1000xxxx (10xxxxxx 10xxxxxx)
* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx)
* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx)
*/
u2 = s[i + 1];
if ((u & 0xFE) == 0xC0 ||
(u == 0xE0 && (u2 & 0xE0) == 0x80) ||
(u == 0xF0 && (u2 & 0xF0) == 0x80) ||
(u == 0xF8 && (u2 & 0xF8) == 0x80) ||
(u == 0xFC && (u2 & 0xFC) == 0x80))
goto Lerr; // overlong combination
for (unsigned j = 1; j != n; j++)
{
u = s[i + j];
if ((u & 0xC0) != 0x80)
goto Lerr; // trailing bytes are 10xxxxxx
V = (V << 6) | (u & 0x3F);
}
if (!utf_isValidDchar(V))
goto Lerr;
i += n;
}
else
{
V = (dchar_t) u;
i++;
}
assert(utf_isValidDchar(V));
*pidx = i;
*presult = V;
return NULL;
Lerr:
*presult = (dchar_t) s[i];
*pidx = i + 1;
return "invalid UTF-8 sequence";
}
/***************************************************
* Validate a UTF-8 string.
* Returns:
* NULL success
* !=NULL error message string
*/
const char *utf_validateString(unsigned char *s, size_t len)
{
size_t idx;
const char *err = NULL;
dchar_t dc;
for (idx = 0; idx < len; )
{
err = utf_decodeChar(s, len, &idx, &dc);
if (err)
break;
}
return err;
}
/********************************************
* Decode a single UTF-16 character sequence.
* Returns:
* NULL success
* !=NULL error message string
*/
const char *utf_decodeWchar(unsigned short *s, size_t len, size_t *pidx, dchar_t *presult)
{
const char *msg;
size_t i = *pidx;
unsigned u = s[i];
assert(i >= 0 && i < len);
if (u & ~0x7F)
{ if (u >= 0xD800 && u <= 0xDBFF)
{ unsigned u2;
if (i + 1 == len)
{ msg = "surrogate UTF-16 high value past end of string";
goto Lerr;
}
u2 = s[i + 1];
if (u2 < 0xDC00 || u2 > 0xDFFF)
{ msg = "surrogate UTF-16 low value out of range";
goto Lerr;
}
u = ((u - 0xD7C0) << 10) + (u2 - 0xDC00);
i += 2;
}
else if (u >= 0xDC00 && u <= 0xDFFF)
{ msg = "unpaired surrogate UTF-16 value";
goto Lerr;
}
else if (u == 0xFFFE || u == 0xFFFF)
{ msg = "illegal UTF-16 value";
goto Lerr;
}
else
i++;
}
else
{
i++;
}
assert(utf_isValidDchar(u));
*pidx = i;
*presult = (dchar_t)u;
return NULL;
Lerr:
*presult = (dchar_t)s[i];
*pidx = i + 1;
return msg;
}
void utf_encodeChar(unsigned char *s, dchar_t c)
{
if (c <= 0x7F)
{
s[0] = (char) c;
}
else if (c <= 0x7FF)
{
s[0] = (char)(0xC0 | (c >> 6));
s[1] = (char)(0x80 | (c & 0x3F));
}
else if (c <= 0xFFFF)
{
s[0] = (char)(0xE0 | (c >> 12));
s[1] = (char)(0x80 | ((c >> 6) & 0x3F));
s[2] = (char)(0x80 | (c & 0x3F));
}
else if (c <= 0x10FFFF)
{
s[0] = (char)(0xF0 | (c >> 18));
s[1] = (char)(0x80 | ((c >> 12) & 0x3F));
s[2] = (char)(0x80 | ((c >> 6) & 0x3F));
s[3] = (char)(0x80 | (c & 0x3F));
}
else
assert(0);
}
void utf_encodeWchar(unsigned short *s, dchar_t c)
{
if (c <= 0xFFFF)
{
s[0] = (wchar_t) c;
}
else
{
s[0] = (wchar_t) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800);
s[1] = (wchar_t) (((c - 0x10000) & 0x3FF) + 0xDC00);
}
}
/**
* Returns the code length of c in the encoding.
* The code is returned in character count, not in bytes.
*/
int utf_codeLengthChar(dchar_t c)
{
return
c <= 0x7F ? 1
: c <= 0x7FF ? 2
: c <= 0xFFFF ? 3
: c <= 0x10FFFF ? 4
: (assert(false), 6);
}
int utf_codeLengthWchar(dchar_t c)
{
return c <= 0xFFFF ? 1 : 2;
}
/**
* Returns the code length of c in the encoding.
* sz is the encoding: 1 = utf8, 2 = utf16, 4 = utf32.
* The code is returned in character count, not in bytes.
*/
int utf_codeLength(int sz, dchar_t c)
{
if (sz == 1)
return utf_codeLengthChar(c);
if (sz == 2)
return utf_codeLengthWchar(c);
assert(sz == 4);
return 1;
}
void utf_encode(int sz, void *s, dchar_t c)
{
if (sz == 1)
utf_encodeChar((unsigned char *)s, c);
else if (sz == 2)
utf_encodeWchar((unsigned short *)s, c);
else
{
assert(sz == 4);
memcpy((unsigned char *)s, &c, sz);
}
}

View File

@@ -1,35 +0,0 @@
// Compiler implementation of the D programming language
// utf.h
// Copyright (c) 2003-2010 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#ifndef DMD_UTF_H
#define DMD_UTF_H
typedef unsigned dchar_t;
int utf_isValidDchar(dchar_t c);
const char *utf_decodeChar(unsigned char *s, size_t len, size_t *pidx, dchar_t *presult);
const char *utf_decodeWchar(unsigned short *s, size_t len, size_t *pidx, dchar_t *presult);
const char *utf_validateString(unsigned char *s, size_t len);
extern int isUniAlpha(dchar_t);
void utf_encodeChar(unsigned char *s, dchar_t c);
void utf_encodeWchar(unsigned short *s, dchar_t c);
int utf_codeLengthChar(dchar_t c);
int utf_codeLengthWchar(dchar_t c);
int utf_codeLength(int sz, dchar_t c);
void utf_encode(int sz, void *s, dchar_t c);
#endif

Some files were not shown because too many files have changed in this diff Show More