Allow returning something of type void in a function that returns void
[delight/core.git] / dmd2 / builtin.c
blobf71e14027c43159e4201396244ba4435aa5370ca
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
11 #include <stdio.h>
12 #include <assert.h>
13 #include <math.h>
15 #include "mars.h"
16 #include "declaration.h"
17 #include "attrib.h"
18 #include "expression.h"
19 #include "scope.h"
20 #include "mtype.h"
21 #include "aggregate.h"
22 #include "identifier.h"
23 #include "id.h"
24 #include "module.h"
26 /**********************************
27 * Determine if function is a builtin one.
29 enum BUILTIN FuncDeclaration::isBuiltin()
31 static const char FeZe[] = "FeZe"; // real function(real)
33 //printf("FuncDeclaration::isBuiltin() %s\n", toChars());
34 if (builtin == BUILTINunknown)
36 builtin = BUILTINnot;
37 #ifndef IN_GCC
38 if (parent && parent->isModule())
40 if (parent->ident == Id::math &&
41 parent->parent && parent->parent->ident == Id::std &&
42 !parent->parent->parent)
44 if (strcmp(type->deco, FeZe) == 0)
46 if (ident == Id::sin)
47 builtin = BUILTINsin;
48 else if (ident == Id::cos)
49 builtin = BUILTINcos;
50 else if (ident == Id::tan)
51 builtin = BUILTINtan;
52 else if (ident == Id::_sqrt)
53 builtin = BUILTINsqrt;
54 else if (ident == Id::fabs)
55 builtin = BUILTINfabs;
56 //printf("builtin = %d\n", builtin);
60 #else
61 /* Maybe in GCC 4.3... */
62 #endif
64 return builtin;
68 /**************************************
69 * Evaluate builtin function.
70 * Return result; NULL if cannot evaluate it.
73 Expression *eval_builtin(enum BUILTIN builtin, Expressions *arguments)
75 assert(arguments && arguments->dim);
76 Expression *arg0 = (Expression *)arguments->data[0];
77 Expression *e = NULL;
78 switch (builtin)
80 #ifndef IN_GCC
81 case BUILTINsin:
82 if (arg0->op == TOKfloat64)
83 e = new RealExp(0, sinl(arg0->toReal()), Type::tfloat80);
84 break;
86 case BUILTINcos:
87 if (arg0->op == TOKfloat64)
88 e = new RealExp(0, cosl(arg0->toReal()), Type::tfloat80);
89 break;
91 case BUILTINtan:
92 if (arg0->op == TOKfloat64)
93 e = new RealExp(0, tanl(arg0->toReal()), Type::tfloat80);
94 break;
96 case BUILTINsqrt:
97 if (arg0->op == TOKfloat64)
98 e = new RealExp(0, sqrtl(arg0->toReal()), Type::tfloat80);
99 break;
101 case BUILTINfabs:
102 if (arg0->op == TOKfloat64)
103 e = new RealExp(0, fabsl(arg0->toReal()), Type::tfloat80);
104 break;
105 #endif
107 return e;