glsl: Implement `error' preprocessor directive.
authorMichal Krol <michal@vmware.com>
Fri, 4 Sep 2009 06:14:48 +0000 (08:14 +0200)
committerMichal Krol <michal@vmware.com>
Mon, 7 Sep 2009 08:11:55 +0000 (10:11 +0200)
src/glsl/pp/SConscript
src/glsl/pp/sl_pp_context.c
src/glsl/pp/sl_pp_context.h
src/glsl/pp/sl_pp_error.c [new file with mode: 0644]
src/glsl/pp/sl_pp_process.c
src/glsl/pp/sl_pp_process.h

index c7718d1d8fdeb3d1a4d97d8dfd29de0e4217421f..13fc230b96ea7c0b6d09badc945ac3c87937bc34 100644 (file)
@@ -10,6 +10,7 @@ glsl = env.StaticLibrary(
     source = [
         'sl_pp_context.c',
         'sl_pp_define.c',
+        'sl_pp_error.c',
         'sl_pp_expression.c',
         'sl_pp_if.c',
         'sl_pp_macro.c',
index 50ec790cc500199d8bfa954ea4d63fa21da21738..38d633baeffc5c49d01edcaf16775615e8f934e2 100644 (file)
@@ -36,6 +36,7 @@ sl_pp_context_init(struct sl_pp_context *context)
    context->macro_tail = &context->macro;
    context->if_ptr = SL_PP_MAX_IF_NESTING;
    context->if_value = 1;
+   memset(context->error_msg, 0, sizeof(context->error_msg));
 }
 
 void
index 1dbd10e30ee506e5e6ac4c75919da7815fb8173c..65ce3e37b7b275e7d9e526380ad90647f8ada040 100644 (file)
@@ -33,6 +33,8 @@
 
 #define SL_PP_MAX_IF_NESTING  64
 
+#define SL_PP_MAX_ERROR_MSG   1024
+
 struct sl_pp_context {
    char *cstr_pool;
    unsigned int cstr_pool_max;
@@ -44,6 +46,8 @@ struct sl_pp_context {
    unsigned int if_stack[SL_PP_MAX_IF_NESTING];
    unsigned int if_ptr;
    int if_value;
+
+   char error_msg[SL_PP_MAX_ERROR_MSG];
 };
 
 void
diff --git a/src/glsl/pp/sl_pp_error.c b/src/glsl/pp/sl_pp_error.c
new file mode 100644 (file)
index 0000000..d42568d
--- /dev/null
@@ -0,0 +1,263 @@
+/**************************************************************************
+ * 
+ * Copyright 2009 VMware, Inc.
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ **************************************************************************/
+
+#include <stdlib.h>
+#include "sl_pp_process.h"
+
+
+void
+sl_pp_process_error(struct sl_pp_context *context,
+                    const struct sl_pp_token_info *input,
+                    unsigned int first,
+                    unsigned int last)
+{
+   unsigned int out_len = 0;
+   unsigned int i;
+
+   for (i = first; i < last; i++) {
+      const char *s = NULL;
+      char buf[2];
+
+      switch (input[i].token) {
+      case SL_PP_WHITESPACE:
+         s = " ";
+         break;
+
+      case SL_PP_NEWLINE:
+         s = "\n";
+         break;
+
+      case SL_PP_HASH:
+         s = "#";
+         break;
+
+      case SL_PP_COMMA:
+         s = ",";
+         break;
+
+      case SL_PP_SEMICOLON:
+         s = ";";
+         break;
+
+      case SL_PP_LBRACE:
+         s = "{";
+         break;
+
+      case SL_PP_RBRACE:
+         s = "}";
+         break;
+
+      case SL_PP_LPAREN:
+         s = "(";
+         break;
+
+      case SL_PP_RPAREN:
+         s = ")";
+         break;
+
+      case SL_PP_LBRACKET:
+         s = "[";
+         break;
+
+      case SL_PP_RBRACKET:
+         s = "]";
+         break;
+
+      case SL_PP_DOT:
+         s = ".";
+         break;
+
+      case SL_PP_INCREMENT:
+         s = "++";
+         break;
+
+      case SL_PP_ADDASSIGN:
+         s = "+=";
+         break;
+
+      case SL_PP_PLUS:
+         s = "+";
+         break;
+
+      case SL_PP_DECREMENT:
+         s = "--";
+         break;
+
+      case SL_PP_SUBASSIGN:
+         s = "-=";
+         break;
+
+      case SL_PP_MINUS:
+         s = "-";
+         break;
+
+      case SL_PP_BITNOT:
+         s = "~";
+         break;
+
+      case SL_PP_NOTEQUAL:
+         s = "!=";
+         break;
+
+      case SL_PP_NOT:
+         s = "!";
+         break;
+
+      case SL_PP_MULASSIGN:
+         s = "*=";
+         break;
+
+      case SL_PP_STAR:
+         s = "*";
+         break;
+
+      case SL_PP_DIVASSIGN:
+         s = "/=";
+         break;
+
+      case SL_PP_SLASH:
+         s = "/";
+         break;
+
+      case SL_PP_MODASSIGN:
+         s = "%=";
+         break;
+
+      case SL_PP_MODULO:
+         s = "%";
+         break;
+
+      case SL_PP_LSHIFTASSIGN:
+         s = "<<=";
+         break;
+
+      case SL_PP_LSHIFT:
+         s = "<<";
+         break;
+
+      case SL_PP_LESSEQUAL:
+         s = "<=";
+         break;
+
+      case SL_PP_LESS:
+         s = "<";
+         break;
+
+      case SL_PP_RSHIFTASSIGN:
+         s = ">>=";
+         break;
+
+      case SL_PP_RSHIFT:
+         s = ">>";
+         break;
+
+      case SL_PP_GREATEREQUAL:
+         s = ">=";
+         break;
+
+      case SL_PP_GREATER:
+         s = ">";
+         break;
+
+      case SL_PP_EQUAL:
+         s = "==";
+         break;
+
+      case SL_PP_ASSIGN:
+         s = "=";
+         break;
+
+      case SL_PP_AND:
+         s = "&&";
+         break;
+
+      case SL_PP_BITANDASSIGN:
+         s = "&=";
+         break;
+
+      case SL_PP_BITAND:
+         s = "&";
+         break;
+
+      case SL_PP_XOR:
+         s = "^^";
+         break;
+
+      case SL_PP_BITXORASSIGN:
+         s = "^=";
+         break;
+
+      case SL_PP_BITXOR:
+         s = "^";
+         break;
+
+      case SL_PP_OR:
+         s = "||";
+         break;
+
+      case SL_PP_BITORASSIGN:
+         s = "|=";
+         break;
+
+      case SL_PP_BITOR:
+         s = "|";
+         break;
+
+      case SL_PP_QUESTION:
+         s = "?";
+         break;
+
+      case SL_PP_COLON:
+         s = ":";
+         break;
+
+      case SL_PP_IDENTIFIER:
+         s = sl_pp_context_cstr(context, input[i].data.identifier);
+         break;
+
+      case SL_PP_NUMBER:
+         s = sl_pp_context_cstr(context, input[i].data.number);
+         break;
+
+      case SL_PP_OTHER:
+         buf[0] = input[i].data.other;
+         buf[1] = '\0';
+         s = buf;
+         break;
+
+      default:
+         strcpy(context->error_msg, "internal error");
+         return;
+      }
+
+      while (*s != '\0' && out_len < sizeof(context->error_msg) - 1) {
+         context->error_msg[out_len++] = *s++;
+      }
+   }
+
+   context->error_msg[out_len] = '\0';
+}
index c17a3ac7ce864798dae41714128ba2a076d36b96..117aa01688da9f6815be55d0b1785f9eadd729f9 100644 (file)
@@ -151,6 +151,9 @@ sl_pp_process(struct sl_pp_context *context,
                      if (sl_pp_process_define(context, input, first, last)) {
                         return -1;
                      }
+                  } else if (!strcmp(name, "error")) {
+                     sl_pp_process_error(context, input, first, last);
+                     return -1;
                   } else if (!strcmp(name, "undef")) {
                      if (sl_pp_process_undef(context, input, first, last)) {
                         return -1;
index 61e67fef0b7fcdc219613bb053bf36f55eade32b..11a94921d8228c882880a8be1b2c8db970929b3b 100644 (file)
@@ -92,6 +92,12 @@ sl_pp_process_endif(struct sl_pp_context *context,
                     unsigned int first,
                     unsigned int last);
 
+void
+sl_pp_process_error(struct sl_pp_context *context,
+                    const struct sl_pp_token_info *input,
+                    unsigned int first,
+                    unsigned int last);
+
 int
 sl_pp_process_out(struct sl_pp_process_state *state,
                   const struct sl_pp_token_info *token);