http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.cpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.cpp b/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.cpp new file mode 100644 index 0000000..ea78a3c --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.cpp @@ -0,0 +1,400 @@ +/* + * PUBLIC DOMAIN PCCTS-BASED C++ GRAMMAR (cplusplus.g, stat.g, expr.g) + * + * Authors: Sumana Srinivasan, NeXT Inc.; [email protected] + * Terence Parr, Parr Research Corporation; [email protected] + * Russell Quong, Purdue University; [email protected] + * + * VERSION 1.1 + * + * SOFTWARE RIGHTS + * + * This file is a part of the ANTLR-based C++ grammar and is free + * software. We do not reserve any LEGAL rights to its use or + * distribution, but you may NOT claim ownership or authorship of this + * grammar or support code. An individual or company may otherwise do + * whatever they wish with the grammar distributed herewith including the + * incorporation of the grammar or the output generated by ANTLR into + * commerical software. You may redistribute in source or binary form + * without payment of royalties to us as long as this header remains + * in all source distributions. + * + * We encourage users to develop parsers/tools using this grammar. + * In return, we ask that credit is given to us for developing this + * grammar. By "credit", we mean that if you incorporate our grammar or + * the generated code into one of your programs (commercial product, + * research project, or otherwise) that you acknowledge this fact in the + * documentation, research report, etc.... In addition, you should say nice + * things about us at every opportunity. + * + * As long as these guidelines are kept, we expect to continue enhancing + * this grammar. Feel free to send us enhancements, fixes, bug reports, + * suggestions, or general words of encouragement at [email protected]. + * + * NeXT Computer Inc. + * 900 Chesapeake Dr. + * Redwood City, CA 94555 + * 12/02/1994 + * + * Restructured for public consumption by Terence Parr late February, 1995. + * + * Requires PCCTS 1.32b4 or higher to get past ANTLR. + * + * DISCLAIMER: we make no guarantees that this grammar works, makes sense, + * or can be used to do anything useful. + */ +/* 1999-2004 Version 3.0 July 2004 + * Modified by David Wigg at London South Bank University for CPP_parser.g + */ +/* 2004-2005 Version 3.1 November 2005 + * Modified by David Wigg at London South Bank University for CPP_parser.g + * + * See MyReadMe.txt for further information + * + * This file is best viewed in courier font with tabs set to 4 spaces + * + * See NotesScopes.txt for information about storing symbol names within scope + *levels + * + * Symbols names are stored in lists selected by hashing + * + * Symbols are also chained in lists for each scope level, + * [0] = template scope Note: Not used at present + * [1] = external scope Note: These symbols (types) are never deleted. + *"std" is allocated this scope in CPPParser::init() + * [2+]= function/variable scopes Note: These symbol references are + *deleted when they go out of scope. + */ + +extern "C" { +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +} +#include "Dictionary.hpp" + +/* Hashing function described in */ +/* "Fast Hashing of Variable-Length Text Strings," */ +/* by Peter K. Pearson, CACM, June 1990. */ +/* Table from p. 678.*/ +/* Pseudorandom Permutation of the Integers 0 through 255: */ +unsigned char Dictionary::randomNumbers[] = { + 1, 14, 110, 25, 97, 174, 132, 119, 138, 170, 125, 118, 27, 233, 140, + 51, 87, 197, 177, 107, 234, 169, 56, 68, 30, 7, 173, 73, 188, 40, + 36, 65, 49, 213, 104, 190, 57, 211, 148, 223, 48, 115, 15, 2, 67, + 186, 210, 28, 12, 181, 103, 70, 22, 58, 75, 78, 183, 167, 238, 157, + 124, 147, 172, 144, 176, 161, 141, 86, 60, 66, 128, 83, 156, 241, 79, + 46, 168, 198, 41, 254, 178, 85, 253, 237, 250, 154, 133, 88, 35, 206, + 95, 116, 252, 192, 54, 221, 102, 218, 255, 240, 82, 106, 158, 201, 61, + 3, 89, 9, 42, 155, 159, 93, 166, 80, 50, 34, 175, 195, 100, 99, + 26, 150, 16, 145, 4, 33, 8, 189, 121, 64, 77, 72, 208, 245, 130, + 122, 143, 55, 105, 134, 29, 164, 185, 194, 193, 239, 101, 242, 5, 171, + 126, 11, 74, 59, 137, 228, 108, 191, 232, 139, 6, 24, 81, 20, 127, + 17, 91, 92, 251, 151, 225, 207, 21, 98, 113, 112, 84, 226, 18, 214, + 199, 187, 13, 32, 94, 220, 224, 212, 247, 204, 196, 43, 249, 236, 45, + 244, 111, 182, 153, 136, 129, 90, 217, 202, 19, 165, 231, 71, 230, 142, + 96, 227, 62, 179, 246, 114, 162, 53, 160, 215, 205, 180, 47, 109, 44, + 38, 31, 149, 135, 0, 216, 52, 63, 23, 37, 69, 39, 117, 146, 184, + 163, 200, 222, 235, 248, 243, 219, 10, 152, 131, 123, 229, 203, 76, 120, + 209}; + +char *Dictionary::strings = NULL; +char *Dictionary::strp = NULL; +unsigned Dictionary::strsize = 0; + +Dictionary::Dictionary(int nb, int ns, int nc) { + int i; + + // allocate buckets for names + bucket = new DictEntry *[nb]; + if (bucket == NULL) panic((const char *)"can't alloc buckets"); + + // Initialize buckets for names + nbuckets = nb; + for (i = 0; i < nb; i++) bucket[i] = NULL; + + // allocate a scope list for the start of each scope list + scope = new DictEntry *[ns]; + if (scope == NULL) panic("can't alloc scopes"); + + // allocate an endScope list for the end of each scope list + endScope = new DictEntry *[ns]; + if (endScope == NULL) panic("can't alloc endScope"); + + // Initialize scopes and endscopes + nscopes = ns; + for (i = 0; i < ns; i++) { + scope[i] = NULL; + endScope[i] = NULL; + } + + currentScope = 0; + + strsize = nc; + strings = new char[nc]; + strp = strings; +} + +Dictionary::~Dictionary() { + delete[] bucket; + delete[] scope; + delete[] endScope; + nbuckets = nscopes = 0; + currentScope = -1; +} + +/* Hashing function described in */ +/* "Fast Hashing of Variable-Length Text Strings," */ +/* by Peter K. Pearson, CACM, June 1990. */ +int Dictionary::hash(const char *string) { + int hash1 = 0; + int hash2 = 0; + int length = 0; + int r = 0; + + while (*string != 0) { + length++; + /* Hash function is XOR of successive characters randomized by + * the hash table. + */ + hash1 ^= randomNumbers[static_cast<int>(*string++)]; + if (*string != 0) hash2 ^= randomNumbers[static_cast<int>(*string++)]; + } + r = (hash1 << 8) | hash2; + return r; +} + +/* Return ptr to 1st entry found in table under key + * (return NULL if none found). + */ +DictEntry *Dictionary::lookup(const char *key, CPPSymbol::ObjectType tp) { + // tp is type to be looked for + // Default, 0, is to look for any symbol irrespective of type + // Each new DictEntry is stored at start of its list so most + // recent additions are reached first + // DictEntry *q; + CPPSymbol *q; + // int scope = getCurrentScopeIndex(); + // if(sc != -1) + // scope = sc; + + int h = hash(key) % nbuckets; + q = (CPPSymbol *)bucket[h]; + + for (; q != NULL; q = (CPPSymbol *)q->getNext()) { + // printf("Dictionary.cpp lookup symbol %s in scope %d current scope %d\n", + // q->getKey(),q->this_scope,getCurrentScopeIndex()); + if (h != q->getHashCode()) { + fprintf(stderr, + "dictionary.cpp lookup, h not equal to q->getHashCode() for %s\n", + key); + } + + if (h == q->getHashCode() && strcmp(key, q->getKey()) == 0) { + if (tp == 0) { // Search for first matching symbol + return q; + } else { + CPPSymbol::ObjectType type = q->getType(); + if (tp == CPPSymbol::otTypename) // Search for first type name + { + if (type == CPPSymbol::otTypedef || type == CPPSymbol::otEnum || + type == CPPSymbol::otClass || type == CPPSymbol::otStruct || + type == CPPSymbol::otUnion) { + return q; + } + } else if (tp == + CPPSymbol::otNonTypename) // Search for first non type name + { + if (type == CPPSymbol::otVariable || type == CPPSymbol::otFunction) { + return q; + } + } else if (type == tp) { // Search for first symbol with specific type + return q; + } else { + return NULL; + } + } + } + } + return NULL; +} + +void Dictionary::define(const char *key, DictEntry *entry) { + defineInScope(key, entry, currentScope); +} + +void Dictionary::defineInScope(const char *key, DictEntry *entry, int sc) { + int h = hash(key) % nbuckets; // July 2005, nbuckets = 4001 See CPP_parser.g + // printf("Dictionary.cpp defineInScope key %s hash(key) %d bucket %d scope + // %d\n", + // key,hash(key),h,sc); + entry->this_scope = sc; + entry->setKey(strdup(key)); // Make a local copy of key + entry->setHashCode(h); + entry->setNext(bucket[h]); // Set next pointer to current entry in bucket + bucket[h] = entry; // Replace current entry in bucket + if (endScope[sc] == NULL) { + scope[sc] = endScope[sc] = entry; + } else { + endScope[sc]->setScope(entry); + endScope[sc] = entry; + } +} + +int Dictionary::getCurrentScopeIndex() { return currentScope; } + +DictEntry *Dictionary::getCurrentScope() { + if (currentScope < 0 || currentScope > nscopes) { + panic("getCurrentScope: no scope"); + } + return scope[currentScope]; +} + +void Dictionary::saveScope() { + // Advance scope number (for included scope) + if (currentScope < nscopes) currentScope++; + /* + if ( currentScope>=nscopes ) + panic( "saveScope: overflow" ); + */ + // printf("Dictionary saveScope entered. Scope now %d\n",currentScope); +} + +void Dictionary::restoreScope() { + // Reduce scope number for next highest scope + // if ( currentScope==0 ) + // panic( "restoreScope: underflow" ); + if (currentScope > 0) currentScope--; + // printf("Dictionary restoreScope entered. Scope now %d\n",currentScope); +} + +/* This unlinks all entries from the Dictionary that are members + * of the current scope. The scope level is not restored to the previous + * scope however. This requires use of restoreScope() above + */ +DictEntry *Dictionary::removeScope(int sc) { + DictEntry *de, *r; + if (sc == -1) { // removeScope() without parameter value defaults sc to -1 + sc = currentScope; + } + for (de = scope[sc]; de != NULL; de = de->getNextInScope()) { + remove(de); + } + r = scope[sc]; + scope[sc] = endScope[sc] = NULL; + return r; +} + +// Remove this dictEntry from its bucket by unlinking it +DictEntry *Dictionary::remove(DictEntry *de) { + DictEntry *prev, *curr; + if (de == NULL) panic("Dictionary.cpp remove: NULL ptr"); + int h = hash(de->getKey()) % nbuckets; // Find pointer to bucket + for (prev = NULL, curr = bucket[h]; curr != NULL; + prev = curr, curr = curr->getNext()) { + if (de == curr) { + if (prev == NULL) { + bucket[h] = de->getNext(); + } else { + prev->setNext(de->getNext()); + } + de->setNext(NULL); + return de; + } + } + return NULL; // should never get here... +} + +/* Lookup the object referred to by 'key' and then physically remove + * it from the Dictionary. Return the object referred to by the key. + * If more than one definition is found for 'key', then only the + * first one is removed. Return NULL if not found. + * Note: DW 12/06/03 Probably not used + */ +DictEntry *Dictionary::remove(char *key) { + DictEntry *q, *prev; + + int h = hash(key) % nbuckets; + for (prev = NULL, q = bucket[h]; q != NULL; prev = q, q = q->getNext()) { + if (h == q->getHashCode() && strcmp(key, q->getKey()) == 0) { + if (prev == NULL) { + bucket[h] = q->getNext(); + } else { + prev->setNext(q->getNext()); + } + q->setNext(NULL); + return q; + } + } + return NULL; // should never get here, but make compiler happy +} + +void Dictionary::dumpScope(FILE *f, int sc) { + DictEntry *s; + + if (sc == -1) { // dumpScope() without parameter value defaults sc to -1 + sc = currentScope; + } + for (s = scope[sc]; s != NULL; s = s->getNextInScope()) { + dumpSymbol(f, s); + } +} + +// This is overridden in CPPDictionary.hpp +void Dictionary::dumpSymbol(FILE *f, DictEntry *de) { + fprintf(f, "%s\n", de->getKey()); +} + +// Diagnostic function +// Contents of first 10 scopes printed +void Dictionary::dumpScopes() { + DictEntry *dictEntry; + int i; + + printf("Scopes"); + for (i = 0; i < 10; i++) { + printf(" %d ", i); + } + printf("\n"); + printf(" first"); + for (i = 0; i < 10; i++) { + if (scope[i] != NULL) { + dictEntry = scope[i]; + printf("%10s ", dictEntry->getKey()); + } else { + printf(" "); + } + } + printf("\n"); + printf(" last "); + for (i = 0; i < 10; i++) { + if (endScope[i] != NULL) { + dictEntry = endScope[i]; + printf("%10s ", dictEntry->getKey()); + } else { + printf(" "); + } + } + printf("\n"); +} + +/* Add a string to the string table and return a pointer to it. + * Bump the pointer into the string table to next avail position. + */ +char *Dictionary::strdup(const char *s) { + char *start = strp; + if (s == NULL) panic("strdup: NULL string"); + if (start + strlen(s) + 1 > &(strings[strsize - 2])) { + panic("string table overflow"); + } + while (*s != '\0') { + *strp++ = *s++; + } + *strp++ = '\0'; + return start; +} + +void Dictionary::panic(const char *err) { + fprintf(stdout, "Dictionary panic: %s\n", err); + exit(-1); +}
http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.hpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.hpp b/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.hpp new file mode 100644 index 0000000..d5b6c40 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/CPPParser/Dictionary.hpp @@ -0,0 +1,102 @@ +/* + * PUBLIC DOMAIN PCCTS-BASED C++ GRAMMAR (cplusplus.g, stat.g, expr.g) + * + * Authors: Sumana Srinivasan, NeXT Inc.; [email protected] + * Terence Parr, Parr Research Corporation; [email protected] + * Russell Quong, Purdue University; [email protected] + * + * SOFTWARE RIGHTS + * + * This file is a part of the ANTLR-based C++ grammar and is free + * software. We do not reserve any LEGAL rights to its use or + * distribution, but you may NOT claim ownership or authorship of this + * grammar or support code. An individual or company may otherwise do + * whatever they wish with the grammar distributed herewith including the + * incorporation of the grammar or the output generated by ANTLR into + * commerical software. You may redistribute in source or binary form + * without payment of royalties to us as long as this header remains + * in all source distributions. + * + * We encourage users to develop parsers/tools using this grammar. + * In return, we ask that credit is given to us for developing this + * grammar. By "credit", we mean that if you incorporate our grammar or + * the generated code into one of your programs (commercial product, + * research project, or otherwise) that you acknowledge this fact in the + * documentation, research report, etc.... In addition, you should say nice + * things about us at every opportunity. + * + * As long as these guidelines are kept, we expect to continue enhancing + * this grammar. Feel free to send us enhancements, fixes, bug reports, + * suggestions, or general words of encouragement at [email protected]. + * + * NeXT Computer Inc. + * 900 Chesapeake Dr. + * Redwood City, CA 94555 + * 12/02/1994 + * + * Restructured for public consumption by Terence Parr late February, 1995. + * + * Requires PCCTS 1.32b4 or higher to get past ANTLR. + * + * DISCLAIMER: we make no guarantees that this grammar works, makes sense, + * or can be used to do anything useful. + */ +/* + * Dictionary.h -- A cool hash table + * + * Terence Parr + * Parr Research Corporation + * January 1995 + */ +/* 1999-2005 Version 3.1 November 2005 + * Modified by David Wigg at London South Bank University for CPP_parser.g + * + * See MyReadMe.txt for further information + * + * This file is best viewed in courier font with tabs set to 4 spaces + */ + +#ifndef Dictionary_hpp +#define Dictionary_hpp + +#include <iostream> +#include "DictEntry.hpp" +#include "CPPSymbol.hpp" + +class Dictionary { + protected: + DictEntry **scope, **endScope; + int nscopes, currentScope; + DictEntry **bucket; + int nbuckets; + static unsigned char randomNumbers[]; + static char *strings; + static char *strp; + static unsigned strsize; + + virtual void dumpSymbol(FILE *, DictEntry *); + + public: + Dictionary(int nb = 43, int ns = 60, int nc = 30000); + virtual ~Dictionary(); + virtual int hash(const char *s); + DictEntry *lookup(const char *, + CPPSymbol::ObjectType type = CPPSymbol::otInvalid); + void define(const char *, DictEntry *); + void defineInScope(const char *, DictEntry *, int); + void saveScope(); + void restoreScope(); + DictEntry *getCurrentScope(); + int getCurrentScopeIndex(); + DictEntry *removeScope(int scope = -1); + DictEntry *remove(char *); + DictEntry *remove(DictEntry *); + + void dumpScope(FILE *, int scope = -1); + void dumpScopes(); + char *strdup(const char *); + + virtual void panic(const char *); +}; + +#endif http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.hpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.hpp b/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.hpp new file mode 100644 index 0000000..e59b6c5 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.hpp @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INC_STDCTokenTypes_hpp_ +#define INC_STDCTokenTypes_hpp_ + +/* $ANTLR 2.7.7 (20130409): "CPP_parser.g" -> "STDCTokenTypes.hpp"$ */ + +#ifndef CUSTOM_API +#define CUSTOM_API +#endif + +#ifdef __cplusplus +struct CUSTOM_API STDCTokenTypes { +#endif + enum { + EOF_ = 1, + LITERAL_template = 4, + LESSTHAN = 5, + GREATERTHAN = 6, + LITERAL_typedef = 7, + SEMICOLON = 8, + LITERAL_enum = 9, + ID = 10, + LCURLY = 11, + LITERAL_inline = 12, + LITERAL_friend = 13, + LITERAL_namespace = 14, + LITERAL_antlrTrace_on = 15, + LITERAL_antlrTrace_off = 16, + RCURLY = 17, + ASSIGNEQUAL = 18, + COLON = 19, + LITERAL_extern = 20, + StringLiteral = 21, + LITERAL_struct = 22, + LITERAL_union = 23, + LITERAL_class = 24, + LITERAL__stdcall = 25, + LITERAL___stdcall = 26, + LITERAL_GFEXCLUDE = 27, + LITERAL_GFINCLUDE = 28, + LITERAL_GFID = 29, + LITERAL_GFUNREAD = 30, + LITERAL_GFARRAYSIZE = 31, + LPAREN = 32, + RPAREN = 33, + LITERAL_GFARRAYSIZES = 34, + LITERAL_GFARRAYELEMSIZE = 35, + LITERAL_auto = 36, + LITERAL_register = 37, + LITERAL_static = 38, + LITERAL_mutable = 39, + LITERAL__inline = 40, + LITERAL___inline = 41, + LITERAL_virtual = 42, + LITERAL_explicit = 43, + LITERAL_typename = 44, + LITERAL_char = 45, + LITERAL_wchar_t = 46, + LITERAL_bool = 47, + LITERAL_short = 48, + LITERAL_int = 49, + // "_int8" = 50 + // "__int8" = 51 + // "int8_t" = 52 + // "_int16" = 53 + // "__int16" = 54 + // "int16_t" = 55 + // "_int32" = 56 + // "__int32" = 57 + // "int32_t" = 58 + // "_int64" = 59 + // "__int64" = 60 + // "int64_t" = 61 + // "uint8_t" = 62 + // "uint16_t" = 63 + // "uint32_t" = 64 + // "uint64_t" = 65 + // "_w64" = 66 + // "__w64" = 67 + LITERAL_long = 68, + LITERAL_signed = 69, + LITERAL_unsigned = 70, + LITERAL_float = 71, + LITERAL_double = 72, + LITERAL_void = 73, + LITERAL__declspec = 74, + LITERAL___declspec = 75, + LITERAL_const = 76, + LITERAL___const = 77, + LITERAL_volatile = 78, + LITERAL___volatile__ = 79, + LITERAL_GFIGNORE = 80, + COMMA = 81, + LITERAL_public = 82, + LITERAL_protected = 83, + LITERAL_private = 84, + OPERATOR = 85, + LITERAL_this = 86, + LITERAL_true = 87, + LITERAL_false = 88, + OCTALINT = 89, + LSQUARE = 90, + RSQUARE = 91, + TILDE = 92, + STAR = 93, + AMPERSAND = 94, + ELLIPSIS = 95, + LITERAL_throw = 96, + LITERAL_using = 97, + LITERAL_case = 98, + LITERAL_default = 99, + LITERAL_if = 100, + LITERAL_else = 101, + LITERAL_switch = 102, + LITERAL_while = 103, + LITERAL_do = 104, + LITERAL_for = 105, + LITERAL_goto = 106, + LITERAL_continue = 107, + LITERAL_break = 108, + LITERAL_return = 109, + LITERAL_try = 110, + LITERAL_catch = 111, + LITERAL_asm = 112, + LITERAL__asm = 113, + LITERAL___asm = 114, + LITERAL___asm__ = 115, + TIMESEQUAL = 116, + DIVIDEEQUAL = 117, + MINUSEQUAL = 118, + PLUSEQUAL = 119, + MODEQUAL = 120, + SHIFTLEFTEQUAL = 121, + SHIFTRIGHTEQUAL = 122, + BITWISEANDEQUAL = 123, + BITWISEXOREQUAL = 124, + BITWISEOREQUAL = 125, + QUESTIONMARK = 126, + OR = 127, + AND = 128, + BITWISEOR = 129, + BITWISEXOR = 130, + NOTEQUAL = 131, + EQUAL = 132, + LESSTHANOREQUALTO = 133, + GREATERTHANOREQUALTO = 134, + SHIFTLEFT = 135, + SHIFTRIGHT = 136, + PLUS = 137, + MINUS = 138, + DIVIDE = 139, + MOD = 140, + DOTMBR = 141, + POINTERTOMBR = 142, + PLUSPLUS = 143, + MINUSMINUS = 144, + LITERAL_sizeof = 145, + LITERAL___alignof__ = 146, + SCOPE = 147, + DOT = 148, + POINTERTO = 149, + LITERAL_dynamic_cast = 150, + LITERAL_static_cast = 151, + LITERAL_reinterpret_cast = 152, + LITERAL_const_cast = 153, + LITERAL_typeid = 154, + DECIMALINT = 155, + HEXADECIMALINT = 156, + CharLiteral = 157, + WCharLiteral = 158, + WStringLiteral = 159, + FLOATONE = 160, + FLOATTWO = 161, + NOT = 162, + LITERAL_new = 163, + LITERAL__cdecl = 164, + LITERAL___cdecl = 165, + LITERAL__near = 166, + LITERAL___near = 167, + LITERAL__far = 168, + LITERAL___far = 169, + LITERAL___interrupt = 170, + LITERAL_pascal = 171, + LITERAL__pascal = 172, + LITERAL___pascal = 173, + LITERAL_delete = 174, + Whitespace = 175, + Comment = 176, + CPPComment = 177, + PREPROC_DIRECTIVE = 178, + LineDirective = 179, + Space = 180, + Pragma = 181, + Error = 182, + EndOfLine = 183, + Escape = 184, + Digit = 185, + Decimal = 186, + LongSuffix = 187, + UnsignedSuffix = 188, + FloatSuffix = 189, + Exponent = 190, + Vocabulary = 191, + Number = 192, + NULL_TREE_LOOKAHEAD = 3 + }; +#ifdef __cplusplus +}; +#endif +#endif /*INC_STDCTokenTypes_hpp_*/ http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.txt ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.txt b/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.txt new file mode 100755 index 0000000..fd6567a --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/CPPParser/STDCTokenTypes.txt @@ -0,0 +1,191 @@ +// $ANTLR 2.7.7 (20130409): CPP_parser.g -> STDCTokenTypes.txt$ +STDC // output token vocab name +LITERAL_template="template"=4 +LESSTHAN=5 +GREATERTHAN=6 +LITERAL_typedef="typedef"=7 +SEMICOLON=8 +LITERAL_enum="enum"=9 +ID=10 +LCURLY=11 +LITERAL_inline="inline"=12 +LITERAL_friend="friend"=13 +LITERAL_namespace="namespace"=14 +LITERAL_antlrTrace_on="antlrTrace_on"=15 +LITERAL_antlrTrace_off="antlrTrace_off"=16 +RCURLY=17 +ASSIGNEQUAL=18 +COLON=19 +LITERAL_extern="extern"=20 +StringLiteral=21 +LITERAL_struct="struct"=22 +LITERAL_union="union"=23 +LITERAL_class="class"=24 +LITERAL__stdcall="_stdcall"=25 +LITERAL___stdcall="__stdcall"=26 +LITERAL_GFEXCLUDE="GFEXCLUDE"=27 +LITERAL_GFINCLUDE="GFINCLUDE"=28 +LITERAL_GFID="GFID"=29 +LITERAL_GFUNREAD="GFUNREAD"=30 +LITERAL_GFARRAYSIZE="GFARRAYSIZE"=31 +LPAREN=32 +RPAREN=33 +LITERAL_GFARRAYSIZES="GFARRAYSIZES"=34 +LITERAL_GFARRAYELEMSIZE="GFARRAYELEMSIZE"=35 +LITERAL_auto="auto"=36 +LITERAL_register="register"=37 +LITERAL_static="static"=38 +LITERAL_mutable="mutable"=39 +LITERAL__inline="_inline"=40 +LITERAL___inline="__inline"=41 +LITERAL_virtual="virtual"=42 +LITERAL_explicit="explicit"=43 +LITERAL_typename="typename"=44 +LITERAL_char="char"=45 +LITERAL_wchar_t="wchar_t"=46 +LITERAL_bool="bool"=47 +LITERAL_short="short"=48 +LITERAL_int="int"=49 +"_int8"=50 +"__int8"=51 +"int8_t"=52 +"_int16"=53 +"__int16"=54 +"int16_t"=55 +"_int32"=56 +"__int32"=57 +"int32_t"=58 +"_int64"=59 +"__int64"=60 +"int64_t"=61 +"uint8_t"=62 +"uint16_t"=63 +"uint32_t"=64 +"uint64_t"=65 +"_w64"=66 +"__w64"=67 +LITERAL_long="long"=68 +LITERAL_signed="signed"=69 +LITERAL_unsigned="unsigned"=70 +LITERAL_float="float"=71 +LITERAL_double="double"=72 +LITERAL_void="void"=73 +LITERAL__declspec="_declspec"=74 +LITERAL___declspec="__declspec"=75 +LITERAL_const="const"=76 +LITERAL___const="__const"=77 +LITERAL_volatile="volatile"=78 +LITERAL___volatile__="__volatile__"=79 +LITERAL_GFIGNORE="GFIGNORE"=80 +COMMA=81 +LITERAL_public="public"=82 +LITERAL_protected="protected"=83 +LITERAL_private="private"=84 +OPERATOR="operator"=85 +LITERAL_this="this"=86 +LITERAL_true="true"=87 +LITERAL_false="false"=88 +OCTALINT=89 +LSQUARE=90 +RSQUARE=91 +TILDE=92 +STAR=93 +AMPERSAND=94 +ELLIPSIS=95 +LITERAL_throw="throw"=96 +LITERAL_using="using"=97 +LITERAL_case="case"=98 +LITERAL_default="default"=99 +LITERAL_if="if"=100 +LITERAL_else="else"=101 +LITERAL_switch="switch"=102 +LITERAL_while="while"=103 +LITERAL_do="do"=104 +LITERAL_for="for"=105 +LITERAL_goto="goto"=106 +LITERAL_continue="continue"=107 +LITERAL_break="break"=108 +LITERAL_return="return"=109 +LITERAL_try="try"=110 +LITERAL_catch="catch"=111 +LITERAL_asm="asm"=112 +LITERAL__asm="_asm"=113 +LITERAL___asm="__asm"=114 +LITERAL___asm__="__asm__"=115 +TIMESEQUAL=116 +DIVIDEEQUAL=117 +MINUSEQUAL=118 +PLUSEQUAL=119 +MODEQUAL=120 +SHIFTLEFTEQUAL=121 +SHIFTRIGHTEQUAL=122 +BITWISEANDEQUAL=123 +BITWISEXOREQUAL=124 +BITWISEOREQUAL=125 +QUESTIONMARK=126 +OR=127 +AND=128 +BITWISEOR=129 +BITWISEXOR=130 +NOTEQUAL=131 +EQUAL=132 +LESSTHANOREQUALTO=133 +GREATERTHANOREQUALTO=134 +SHIFTLEFT=135 +SHIFTRIGHT=136 +PLUS=137 +MINUS=138 +DIVIDE=139 +MOD=140 +DOTMBR=141 +POINTERTOMBR=142 +PLUSPLUS=143 +MINUSMINUS=144 +LITERAL_sizeof="sizeof"=145 +LITERAL___alignof__="__alignof__"=146 +SCOPE=147 +DOT=148 +POINTERTO=149 +LITERAL_dynamic_cast="dynamic_cast"=150 +LITERAL_static_cast="static_cast"=151 +LITERAL_reinterpret_cast="reinterpret_cast"=152 +LITERAL_const_cast="const_cast"=153 +LITERAL_typeid="typeid"=154 +DECIMALINT=155 +HEXADECIMALINT=156 +CharLiteral=157 +WCharLiteral=158 +WStringLiteral=159 +FLOATONE=160 +FLOATTWO=161 +NOT=162 +LITERAL_new="new"=163 +LITERAL__cdecl="_cdecl"=164 +LITERAL___cdecl="__cdecl"=165 +LITERAL__near="_near"=166 +LITERAL___near="__near"=167 +LITERAL__far="_far"=168 +LITERAL___far="__far"=169 +LITERAL___interrupt="__interrupt"=170 +LITERAL_pascal="pascal"=171 +LITERAL__pascal="_pascal"=172 +LITERAL___pascal="__pascal"=173 +LITERAL_delete="delete"=174 +Whitespace=175 +Comment=176 +CPPComment=177 +PREPROC_DIRECTIVE("a preprocessor directive")=178 +LineDirective=179 +Space=180 +Pragma=181 +Error=182 +EndOfLine=183 +Escape=184 +Digit=185 +Decimal=186 +LongSuffix=187 +UnsignedSuffix=188 +FloatSuffix=189 +Exponent=190 +Vocabulary=191 +Number=192 http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/CPPParser/Support.cpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/CPPParser/Support.cpp b/contrib/pdxautoserializer/src/impl/CPPParser/Support.cpp new file mode 100644 index 0000000..9ba33b3 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/CPPParser/Support.cpp @@ -0,0 +1,894 @@ +/* + * PUBLIC DOMAIN PCCTS-BASED C++ GRAMMAR (cplusplus.g, stat.g, expr.g) + * + * Authors: Sumana Srinivasan, NeXT Inc.; [email protected] + * Terence Parr, Parr Research Corporation; [email protected] + * Russell Quong, Purdue University; [email protected] + * + * VERSION 1.1 + * + * SOFTWARE RIGHTS + * + * This file is a part of the ANTLR-based C++ grammar and is free + * software. We do not reserve any LEGAL rights to its use or + * distribution, but you may NOT claim ownership or authorship of this + * grammar or support code. An individual or company may otherwise do + * whatever they wish with the grammar distributed herewith including the + * incorporation of the grammar or the output generated by ANTLR into + * commerical software. You may redistribute in source or binary form + * without payment of royalties to us as long as this header remains + * in all source distributions. + * + * We encourage users to develop parsers/tools using this grammar. + * In return, we ask that credit is given to us for developing this + * grammar. By "credit", we mean that if you incorporate our grammar or + * the generated code into one of your programs (commercial product, + * research project, or otherwise) that you acknowledge this fact in the + * documentation, research report, etc.... In addition, you should say nice + * things about us at every opportunity. + * + * As long as these guidelines are kept, we expect to continue enhancing + * this grammar. Feel free to send us enhancements, fixes, bug reports, + * suggestions, or general words of encouragement at [email protected]. + * + * NeXT Computer Inc. + * 900 Chesapeake Dr. + * Redwood City, CA 94555 + * 12/02/1994 + * + * Restructured for public consumption by Terence Parr late February, 1995. + * + * DISCLAIMER: we make no guarantees that this grammar works, makes sense, + * or can be used to do anything useful. + */ +/* + * 2001-2003 Version 2.0 September 2003 + * + * Some modifications were made to this file to support a project by + * Jianguo Zuo and David Wigg at + * The Centre for Systems and Software Engineering + * South Bank University + * London, UK. + * [email protected] + * [email protected] + * + * These C++ functions are required to support the correct functioning of + * the CPP_parser.g definition of the C++ language. +*/ +/* 2003-2004 Version 3.0 July 2004 + * Modified by David Wigg at London South Bank University for CPP_parser.g + */ +/* 2004-1005 Version 3.1 November 2005 + * Modified by David Wigg at London South Bank University for CPP_parser.g + * + * See MyReadMe.txt for further information + * + * This file is best viewed in courier font with tabs set to 4 spaces + */ + +extern "C" { +#ifndef WIN32 +#include <unistd.h> +#endif +} + +#include <iostream> +#include <stdio.h> +#include <string.h> +#include "../CPPParser/CPPParser.hpp" + +extern int + statementTrace; // Used to control selected tracing at statement level +// for testing and program verification purposes + +// Shorthand for a string of (qualifiedItemIs()==xxx||...) +int CPPParser::qualifiedItemIsOneOf( + QualifiedItem qiFlags, // Ored combination of flags + int lookahead_offset) { + QualifiedItem qi = qualifiedItemIs(lookahead_offset); + return (qi & qiFlags) != 0; +} + +// This is an important function, but will be replaced with +// an enhanced predicate in the future, once predicates +// and/or predicate guards can contain loops. +// +// Scan past the ::T::B:: to see what lies beyond. +// Return qiType if the qualified item can pose as type name. +// Note that T::T is NOT a type; it is a constructor. Also, +// class T { ... T...} yields the enclosed T as a ctor. This +// is probably a type as I separate out the constructor defs/decls, +// I want it consistent with T::T. +// +// In the below examples, I use A,B,T and example types, and +// a,b as example ids. +// In the below examples, any A or B may be a +// qualified template, i.e., A<...> +// +// T::T outside of class T yields qiCtor. +// T<...>::T outside of class T yields qiCtor. +// T inside of class T {...} yields qiCtor. +// T, ::T, A::T outside of class T yields qiType. +// a, ::a, A::B::a yields qiId +// a::b yields qiInvalid +// ::operator, operator, A::B::operator yield qiOPerator +// A::*, A::B::* yield qiPtrMember +// ::*, * yields qiInvalid +// ::~T, ~T, A::~T yield qiDtor +// ~a, ~A::a, A::~T::, ~T:: yield qiInvalid + +CPPParser::QualifiedItem CPPParser::qualifiedItemIs(int lookahead_offset) { + int offset = lookahead_offset + 1; + int final_type_idx = 0; + // Skip leading "::" + if (LT(offset)->getType() == SCOPE) { + offset++; + } + // Skip sequences of T:: or T<...>:: + // DW 11/02/05 Note that LT(offset)->getType() is not a "type" but a type of + // token, eg. ID + // printf("support.cpp qualifiedItemIs while reached offset %d %s type %d + // isType %d, isClass %d, guessing %d\n", + // offset,(LT(offset)->getText()).data(),LT(offset)->getType(),isTypeName((LT(offset)->getText()).data()),isClassName((LT(offset)->getText()).data()),inputState->guessing); + while (LT(offset)->getType() == ID && + isTypeName((LT(offset)->getText()).data())) { // If this type is the + // same as the last + // type, then ctor + if (final_type_idx != 0 && + strcmp((LT(final_type_idx)->getText()).data(), + (LT(offset)->getText()).data()) == 0) { // Like T::T + // As an extra check, do not allow T::T:: + if (LT(offset + 1)->getType() == SCOPE) { // printf("support.cpp + // qualifiedItemIs qiInvalid + // returned\n"); + return qiInvalid; + } else { // printf("support.cpp qualifiedItemIs qiCtor_1 returned %s %s + // %s \n", + // enclosingClass,(LT(lookahead_offset+1)->getText()).data(), + //(LT(final_type_idx)->getText()).data()); + return qiCtor; + } + } + + // Record this as the most recent type seen in the series + final_type_idx = offset; + + // Skip this token + offset++; + + // Skip over any template qualifiers <...> + // I believe that "T<..." cannot be anything valid but a template + if (LT(offset)->getType() == LESSTHAN) { + if (!skipTemplateQualifiers(offset)) { // printf("support.cpp + // qualifiedItemIs qiInvalid_2 + // returned\n"); + return qiInvalid; + } + // printf("support.cpp qualifiedItemIs template skipped, offset + // %d\n",offset); + // offset has been updated to token following <...> + } + + // Skip any "::" and keep going + if (LT(offset)->getType() == SCOPE) { + offset++; + } + // Otherwise series terminated -- last ID in the sequence was a type + else { + // Return ctor if last type is in containing class + // We already checked for T::T inside loop + if (strcmp(enclosingClass, (LT(final_type_idx)->getText()).data()) == + 0) { // Like class T T() + // printf("support.cpp qualifiedItemIs qiCtor_2 returned %s %s %s\n", + // enclosingClass,(LT(lookahead_offset+1)->getText()).data(), + // (LT(final_type_idx)->getText()).data()); + return qiCtor; + } else { // printf("support.cpp qualifiedItemIs qiType returned\n"); + return qiType; + } + } + } + + // LT(offset) is not an ID, or it is an ID but not a typename. + // printf("support.cpp qualifiedItemIs second switch reached with type + // %d\n",LT(offset)->getType()); + switch (LT(offset)->getType()) { + case ID: + // ID but not a typename + // Do not allow id:: + if (LT(offset + 1)->getType() == SCOPE) { + // printf("support.cpp qualifiedItemIs qiInvalid_3 returned\n"); + return qiInvalid; + } + if (strcmp(enclosingClass, (LT(offset)->getText()).data()) == + 0) { // Like class T T() + // printf("support.cpp qualifiedItemIs qiCtor_3 returned + // %s\n",enclosingClass); + return qiCtor; + } else { + if (isTypeName((LT(offset)->getText()).data())) { + return qiType; + } else { + // printf("support.cpp qualifiedItemIs qiVar returned\n"); + return qiVar; // DW 19/03/04 was qiVar Could be function, qiFun? + } + } + case TILDE: + // check for dtor + if (LT(offset + 1)->getType() == ID && + isTypeName((LT(offset + 1)->getText()).data()) && + LT(offset + 2)->getType() != SCOPE) { // Like ~B or A::B::~B + // Also (incorrectly?) matches ::~A. + // printf("support.cpp qualifiedItemIs qiDtor returned\n"); + return qiDtor; + } else { // ~a or ~A::a is qiInvalid + // printf("support.cpp qualifiedItemIs qiInvalid_4 returned\n"); + return qiInvalid; + } + break; + case STAR: + // Like A::* + // Do not allow * or ::* + if (final_type_idx == 0) { // Haven't seen a type yet + // printf("support.cpp qualifiedItemIs qiInvalid_5 returned\n"); + return qiInvalid; + } else { // printf("support.cpp qualifiedItemIs qiPtrMember returned\n"); + return qiPtrMember; + } + case OPERATOR: + // Like A::operator, ::operator, or operator + // printf("support.cpp qualifiedItemIs qiOperator returned\n"); + return qiOperator; + default: + // Something that neither starts with :: or ID, or + // a :: not followed by ID, operator, ~, or * + // printf("support.cpp qualifiedItemIs qiInvalid_6 returned\n"); + return qiInvalid; + } +} + +// Skip over <...>. This correctly handles nested <> and (), e.g: +// <T> +// < (i>3) > +// < T2<...> > +// but not +// < i>3 > +// +// On input, kInOut is the index of the "<" +// On output, if the return is true, then +// kInOut is the index of the token after ">" +// else +// kInOut is unchanged +int CPPParser::skipTemplateQualifiers(int &kInOut) { // Start after "<" + int offset = kInOut + 1; + + while (LT(offset)->getType() != GREATERTHAN) // scan to end of <...> + { + switch (LT(offset)->getType()) { + case EOF: + return 0; + case LESSTHAN: + if (!skipTemplateQualifiers(offset)) { + return 0; + } + break; + case LPAREN: + if (!skipNestedParens(offset)) { + return 0; + } + break; + default: + offset++; // skip everything else + break; + } + if (offset > MaxTemplateTokenScan) { + return 0; + } + } + + // Update output argument to point past ">" + kInOut = offset + 1; + return 1; +} + +// Skip over (...). This correctly handles nested (), e.g: +// (i>3, (i>5)) +// +// On input, kInOut is the index of the "(" +// On output, if the return is true, then +// kInOut is the index of the token after ")" +// else +// kInOut is unchanged +int CPPParser::skipNestedParens(int &kInOut) { // Start after "(" + int offset = kInOut + 1; + + while (LT(offset)->getType() != RPAREN) // scan to end of (...) + { + switch (LT(offset)->getType()) { + case EOF: + return 0; + case LPAREN: + if (!skipNestedParens(offset)) { + return 0; + } + break; + default: + offset++; // skip everything else + break; + } + if (offset > MaxTemplateTokenScan) { + return 0; + } + } + + // Update output argument to point past ")" + kInOut = offset + 1; + return 1; +} + +// Return true if "::blah" or "foo::bar<args>::..." found. +int CPPParser::scopedItem(int k) { + // printf("support.cpp scopedItem k %d\n",k); + return (LT(k)->getType() == SCOPE || + (LT(k)->getType() == ID && !finalQualifier(k))); +} + +// Return true if ID<...> or ID is last item in qualified item list. +// Return false if LT(k) is not an ID. +// ID must be a type to check for ID<...>, +// or else we would get confused by "i<3" +int CPPParser::finalQualifier(int k) { + if (LT(k)->getType() == ID) { + if (isTypeName((LT(k)->getText()).data()) && + LT(k + 1)->getType() == LESSTHAN) { // Starts with "T<". Skip <...> + k++; + skipTemplateQualifiers(k); + } else { // skip ID; + k++; + } + return (LT(k)->getType() != SCOPE); + } else { // not an ID + return 0; + } +} + +/* + * Return true if 's' can pose as a type name + */ +int CPPParser::isTypeName( + const char *s) { // printf("isTypeName entered with %s\n",s); + // To look for any type name only + return 1; + /* + CPPSymbol *cs = ( CPPSymbol * ) symbols->lookup( s,CPPSymbol::otTypename ); + + if ( cs==NULL ) + { + printf("support.cpp isTypeName %s not found\n",s); + return 0; + } + + // This should now be redundant + if ( cs->getType()==CPPSymbol::otTypedef|| + cs->getType()==CPPSymbol::otEnum|| + cs->getType()==CPPSymbol::otClass|| + cs->getType()==CPPSymbol::otStruct|| + cs->getType()==CPPSymbol::otUnion ) + { + return 1; + } + + return 0; + */ +} + +/* + * Return true if 's' is a class name (or a struct which is a class + * with all members public). + */ +int CPPParser::isClassName(const char *s) { + return 1; + /* + // To look for any type name omly + CPPSymbol *cs = ( CPPSymbol * ) symbols->lookup( s,CPPSymbol::otTypename ); + + if ( cs==NULL ) + { + printf("support.cpp isClassName %s not found\n",s); + return 0; + } + + if ( cs->getType()==CPPSymbol::otClass|| + cs->getType()==CPPSymbol::otStruct|| + cs->getType()==CPPSymbol::otUnion ) + { + return 1; + } + + return 0; + */ +} + +void CPPParser::beginDeclaration() {} + +void CPPParser::endDeclaration() {} + +void CPPParser::beginFunctionDefinition() { functionDefinition = 1; } + +void CPPParser::endFunctionDefinition() { + // Remove parameter scope + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + // printf("endFunctionDefinition remove parameter + // scope(%d):\n",symbols->getCurrentScopeIndex()); + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("endFunctionDefinition restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); + functionDefinition = 0; +} + +void CPPParser::beginConstructorDefinition() { functionDefinition = 1; } + +void CPPParser::endConstructorDefinition() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("endConstructorDefinition restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); + functionDefinition = 0; +} + +void CPPParser::beginConstructorDeclaration(const char *ctor) {} + +void CPPParser::endConstructorDeclaration() {} + +void CPPParser::beginDestructorDefinition() { functionDefinition = 1; } + +void CPPParser::endDestructorDefinition() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("endDestructorDefinition restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); + functionDefinition = 0; +} + +void CPPParser::beginDestructorDeclaration(const char *dtor) {} + +void CPPParser::endDestructorDeclaration() {} + +void CPPParser::beginParameterDeclaration() {} + +void CPPParser::beginFieldDeclaration() {} + +void CPPParser::declarationSpecifier(bool td, bool fd, StorageClass sc, + TypeQualifier tq, TypeSpecifier ts, + FunctionSpecifier fs) { + // printf("support.cpp declarationSpecifier td %d, fd %d, sc %d, tq %d, ts %d, + // fs %d\n", + // td,fd,sc,tq,ts,fs); + _td = td; // For typedef + _fd = fd; // For friend + _sc = sc; + _tq = tq; + _ts = ts; + _fs = fs; +} + +/* Symbols from declarators are added to the symbol table here. + * The symbol is added to external scope or whatever the current scope is, in + * the symbol table. + * See list of object types below. + */ +void CPPParser::declaratorID(const char *id, QualifiedItem qi) { + // printf("declaratorID entered for %s\n",id); + CPPSymbol *c; + // printf("support.cpp declaratorID line %d %s found, _ts = %d, _td = %d, qi = + // %d\n", + // LT(1)->getLine(),id,_ts,_td,qi); + if (qi == qiType || qi == qiNamespace) // Check for type declaration + { + c = new CPPSymbol(id, CPPSymbol::otTypedef); + if (c == NULL) panic("can't alloc CPPSymbol"); + symbols->defineInScope(id, c, externalScope); + if (statementTrace >= 2) { + printf( + "%d support.cpp declaratorID declare %s in external scope 1, " + "ObjectType %d\n", + LT(1)->getLine(), id, c->getType()); + } + // DW 04/08/03 Scoping not fully implemented + // Typedefs all recorded in 'external' scope and therefor never removed + } else if (qi == qiFun) // Check for function declaration + { + c = new CPPSymbol(id, CPPSymbol::otFunction); + if (c == NULL) panic("can't alloc CPPSymbol"); + symbols->define(id, c); // Add to current scope + if (statementTrace >= 2) { + printf( + "%d support.cpp declaratorID declare %s in current scope %d, " + "ObjectType %d\n", + LT(1)->getLine(), id, symbols->getCurrentScopeIndex(), c->getType()); + } + } else { + if (qi != qiVar) { + fprintf( + stderr, + "%d support.cpp declaratorID warning qi (%d) not qiVar (%d) for %s\n", + LT(1)->getLine(), qi, qiVar, id); + } + + c = new CPPSymbol(id, CPPSymbol::otVariable); + if (c == NULL) panic("can't alloc CPPSymbol"); + symbols->define(id, c); // Add to current scope + if (statementTrace >= 2) { + printf( + "%d support.cpp declaratorID declare %s in current scope %d, " + "ObjectType %d\n", + LT(1)->getLine(), id, symbols->getCurrentScopeIndex(), c->getType()); + } + } +} +/* These are the object types + 0 = otInvalid + 1 = otFunction + 2 = otVariable + 3 = otTypedef Note. 3-7 are type names + 4 = otStruct Note. 4, 5 & 7 are class names + 5 = otUnion + 6 = otEnum + 7 = otClass + 10 = otTypename + 11 = otNonTypename +*/ + +void CPPParser::declaratorArray() {} + +void CPPParser::declaratorParameterList(int def) { + symbols->saveScope(); // Advance currentScope (lower level) + // printf("declaratorParameterList saveScope() now + // %d\n",symbols->getCurrentScopeIndex()); +} + +void CPPParser::declaratorEndParameterList(int def) { + if (!def) { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("declaratorEndParameterList restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); + } +} + +void CPPParser::functionParameterList() { + symbols->saveScope(); // Advance currentScope (lower level) + // printf("functionParameterList saveScope() now + // %d\n",symbols->getCurrentScopeIndex()); + // DW 25/3/97 change flag from function to parameter list + functionDefinition = 2; +} + +void CPPParser::functionEndParameterList(int def) { + // If this parameter list is not in a definition then this + if (!def) { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("functionEndParameterList restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); + } else { + // Change flag from parameter list to body of definition + functionDefinition = 3; + } + // Otherwise endFunctionDefinition removes the parameters from scope +} + +void CPPParser::enterNewLocalScope() { + symbols->saveScope(); // Advance currentScope (lower level) + // printf("enterNewLocalScope saveScope() now + // %d\n",symbols->getCurrentScopeIndex()); +} + +void CPPParser::exitLocalScope() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("exitLocalScope restoreScope() now + // %d\n",symbols->getCurrentScopeIndex()); +} + +void CPPParser::enterExternalScope() { // Scope has been initialised to 1 in + // CPPParser.init() in CPPParser.hpp + functionDefinition = 0; +} + +void CPPParser::exitExternalScope() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope s/b 2 + symbols->restoreScope(); // Reduce currentScope (higher level) + + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope s/b 1 + symbols->restoreScope(); // Reduce currentScope (higher level) + + if (symbols->getCurrentScopeIndex() == 0) { + if (statementTrace >= 2) { + fprintf(stdout, "\nSupport exitExternalScope, scope now %d as required\n", + symbols->getCurrentScopeIndex()); + } + } else { + fprintf(stderr, "\nSupport exitExternalScope, scope now %d, should be 0\n", + symbols->getCurrentScopeIndex()); + } +} + +void CPPParser::classForwardDeclaration(TypeSpecifier ts, FunctionSpecifier fs, + const char *tag) +// classForwardDeclaration(const char *tag, TypeSpecifier ts, FunctionSpecifier +// fs) +{ + CPPSymbol *c = NULL; + + // if already in symbol table as a class, don't add + // of course, this is incorrect as you can rename + // classes by entering a new scope, but our limited + // example basically keeps all types globally visible. + if (symbols->lookup(tag, CPPSymbol::otTypename) != NULL) { + CPPSymbol *cs = (CPPSymbol *)symbols->lookup(tag, CPPSymbol::otTypename); + if (statementTrace >= 2) { + printf( + "%d support.cpp classForwardDeclaration %s already stored in " + "dictionary, ObjectType %d\n", + LT(1)->getLine(), tag, cs->getType()); + } + return; + } + + switch (ts) { + case tsSTRUCT: + c = new CPPSymbol(tag, CPPSymbol::otStruct); + break; + case tsUNION: + c = new CPPSymbol(tag, CPPSymbol::otUnion); + break; + case tsCLASS: + c = new CPPSymbol(tag, CPPSymbol::otClass); + break; + } + + if (c == NULL) panic("can't alloc CPPSymbol"); + + symbols->defineInScope(tag, c, externalScope); + if (statementTrace >= 2) { + printf( + "%d support.cpp classForwardDeclaration declare %s in external scope, " + "ObjectType %d\n", + LT(1)->getLine(), tag, c->getType()); + } + + // If it's a friend class forward decl, put in global scope also. + // DW 04/07/03 No need if already in external scope. See above. + // if ( ds==dsFRIEND ) + // { + // CPPSymbol *ext_c = new CPPSymbol(tag, CPPSymbol::otClass); + // if ( ext_c==NULL ) panic("can't alloc CPPSymbol"); + // if ( symbols->getCurrentScopeIndex()!=externalScope ) // DW 04/07/03 + // Not sure this test is really necessary + // { + // printf("classForwardDeclaration + // defineInScope(externalScope)\n"); + // symbols->defineInScope(tag, ext_c, externalScope); + // } + // } +} + +void CPPParser::exitNamespaceScope() {} + +void CPPParser::beginClassDefinition(TypeSpecifier ts, const char *tag) { + CPPSymbol *c = NULL; + + // if already in symbol table as a class, don't add + // of course, this is incorrect as you can rename + // classes by entering a new scope, but our limited + // example basically keeps all types globally visible. + if (symbols->lookup(tag, CPPSymbol::otTypename) != NULL) { + symbols->saveScope(); // still have to use scope to collect members + // printf("support.cpp beginClassDefinition_1 saveScope() now + // %d\n",symbols->getCurrentScopeIndex()); + if (statementTrace >= 2) { + printf( + "%d support.cpp beginClassDefinition classname %s already in " + "dictionary\n", + LT(1)->getLine(), tag); + } + return; + } + + switch (ts) { + case tsSTRUCT: + c = new CPPSymbol(tag, CPPSymbol::otStruct); + break; + case tsUNION: + c = new CPPSymbol(tag, CPPSymbol::otUnion); + break; + case tsCLASS: + c = new CPPSymbol(tag, CPPSymbol::otClass); + break; + } + if (c == NULL) panic("can't alloc CPPSymbol"); + + symbols->defineInScope(tag, c, externalScope); + + if (statementTrace >= 2) { + printf( + "%d support.cpp beginClassDefinition define %s in external scope (1), " + "ObjectType %d\n", + LT(1)->getLine(), tag, c->getType()); + } + + strcat(qualifierPrefix, tag); + strcat(qualifierPrefix, "::"); + + // add all member type symbols into the global scope (not correct, but + // will work for most code). + // This symbol lives until the end of the file + symbols->saveScope(); // Advance currentScope (lower level) +} + +void CPPParser::endClassDefinition() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // remove final T:: from A::B::C::T:: + // upon backing out of last class, qualifierPrefix is set to "" + char *p = &(qualifierPrefix[strlen(qualifierPrefix) - 3]); + while (p > &(qualifierPrefix[0]) && *p != ':') { + p--; + } + if (p > &(qualifierPrefix[0])) p++; + *p = '\0'; +} + +void CPPParser::enumElement(const char *e) {} + +void CPPParser::beginEnumDefinition( + const char *e) { // DW 26/3/97 Set flag for new class + + // add all enum tags into the global scope (not correct, but + // will work for most code). + // This symbol lives until the end of the file + CPPSymbol *c = new CPPSymbol(e, CPPSymbol::otEnum); + symbols->defineInScope(e, c, externalScope); + if (statementTrace >= 2) { + printf( + "%d support.cpp beginEnumDefinition %s define in external scope, " + "ObjectType %d\n", + LT(1)->getLine(), e, c->getType()); + } +} + +void CPPParser::endEnumDefinition() {} + +void CPPParser::templateTypeParameter(const char *t) { + // DW 11/06/03 Symbol saved in templateParameterScope (0) + // as a temporary measure until scope is implemented fully + // This symbol lives until the end of the file + CPPSymbol *e = new CPPSymbol(t, CPPSymbol::otTypedef); // DW 22/03/05 + // CPPSymbol *e = new CPPSymbol(t, CPPSymbol::otClass); + if (e == NULL) panic("can't alloc CPPSymbol"); + // symbols->defineInScope(t, e, templateParameterScope); // DW 22/03/05 + // DW 10/08/05 Replaced to make template parameters local + symbols->defineInScope(t, e, externalScope); + // symbols->define(t,e); // Save template parameter in local scope + if (statementTrace >= 2) { + // printf("%d support.cpp templateTypeParameter declare %s in + // parameter + // scope (0), ObjectType %d\n", + printf( + "%d support.cpp templateTypeParameter declare %s in external scope " + "(1), ObjectType %d\n", + LT(1)->getLine(), t, e->getType()); + } + // printf("%d support.cpp templateTypeParameter declare %s in + // current + // scope %d, ObjectType %d\n", + // LT(1)->getLine(),t,symbols->getCurrentScopeIndex(),e->getType()); +} + +void CPPParser::beginTemplateDeclaration() { + symbols->saveScope(); // Advance currentScope (lower level) + // printf("Support beginTemplateDeclaration, Scope now + // %d\n",symbols->getCurrentScopeIndex()); +} + +void CPPParser::endTemplateDeclaration() { + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) + // printf("Support endTemplateDeclaration, Scope now + // %d\n",symbols->getCurrentScopeIndex()); +} + +void CPPParser::beginTemplateDefinition() {} + +void CPPParser::endTemplateDefinition() {} + +void CPPParser::beginTemplateParameterList() { + // DW 26/05/03 To scope template parameters + symbols->saveScope(); // Advance currentScope (lower level) +} + +void CPPParser::endTemplateParameterList() { + // DW 26/05/03 To end scope template parameters + symbols->restoreScope(); // Reduce currentScope (higher level) +} + +void CPPParser::exceptionBeginHandler() {} + +void CPPParser::exceptionEndHandler() { // remove parm elements from the + // handler scope + symbols->dumpScope(stdout); // Diagnostic - See CPPDictionary.hpp + symbols->removeScope(); // Remove symbols stored in current scope + symbols->restoreScope(); // Reduce currentScope (higher level) +} + +void CPPParser::end_of_stmt() { +#ifdef MYCODE + myCode_end_of_stmt(); +#endif // MYCODE +} + +void CPPParser::panic(const char *err) { + fprintf(stdout, "CPPParser: %s\n", err); + _exit(-1); +} + +/* DW 090204 leave out of support.cpp for the time being +// want to see how supplied routine in LLkParser works first +void CPPParser:: +tracein(char *r) +{ +// testing + printf("support tracein entered\n"); + getchar(); + if ( !doTracing ) return; + for (int i=1; i<=traceIndentLevel; i++) + fprintf(stdout, " "); + traceIndentLevel++; + fprintf(stdout, "enter %s('%s %s')%s\n", + r, + (LT(1)->getText()).data(), + (LT(2)->getText()).data(), + inputState->guessing?" [guessing]":"" + ); + fflush(stdout); +} + +void CPPParser:: +traceout(char *r) +{ + if ( !doTracing ) return; + traceIndentLevel--; + for (int i=1; i<=traceIndentLevel; i++) + fprintf(stdout, " "); + fprintf(stdout, "exit %s('%s %s')%s\n", + r, + (LT(1)->getText()).data(), + (LT(2)->getText()).data(), + inputState->guessing?" [guessing]":"" + ); + fflush(stdout); +} +*/ + +// Functions which may be overridden by MyCode subclass + +void CPPParser::myCode_pre_processing(int argc, char *argv[]) {} + +void CPPParser::myCode_post_processing() {} + +void CPPParser::myCode_end_of_stmt() {} + +void CPPParser::myCode_function_direct_declarator(const char *id) { + // printf("support myCode_function_direct_declarator entered for %s\n",id); +} http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/Helper.cpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/Helper.cpp b/contrib/pdxautoserializer/src/impl/Helper.cpp new file mode 100644 index 0000000..edbea45 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/Helper.cpp @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../base_types.hpp" +#include "Helper.hpp" + +namespace apache { +namespace geode { +namespace client { +namespace pdx_auto_serializer { +void Helper::splitString(const std::string& str, const std::string& delim, + StringVector& splitStr) { + std::string::size_type offset = 0; + std::string::size_type delimIndex; + + while ((delimIndex = str.find(delim, offset)) != std::string::npos) { + splitStr.push_back(str.substr(offset, delimIndex - offset)); + offset += delimIndex - offset + delim.length(); + } + splitStr.push_back(str.substr(offset)); +} + +std::string Helper::stringReplace(const std::string& source, + const std::string& findStr, + const std::string& replaceStr) { + std::string resultStr; + std::string::const_iterator srcIterator = source.begin(); + while (srcIterator != source.end()) { + bool matchFound = true; + std::string::const_iterator matchIterator = srcIterator; + std::string::const_iterator findIterator = findStr.begin(); + while (findIterator != findStr.end()) { + if (matchIterator == source.end() || + *matchIterator++ != *findIterator++) { + matchFound = false; + break; + } + } + if (matchFound) { + resultStr += replaceStr; + srcIterator = matchIterator; + } else { + resultStr += *srcIterator; + ++srcIterator; + } + } + return resultStr; +} + +bool Helper::getSingleProperty(PropertyMap& properties, const std::string& name, + std::string& value) { + PropertyMap::iterator propertyFind = properties.find(name); + if (propertyFind != properties.end()) { + if (propertyFind->second.size() == 1) { + value = propertyFind->second[0]; + } + properties.erase(propertyFind); + return true; + } + return false; +} + +bool Helper::getMultiProperty(PropertyMap& properties, const std::string& name, + StringVector& value) { + PropertyMap::iterator propertyFind = properties.find(name); + if (propertyFind != properties.end()) { + value = propertyFind->second; + properties.erase(propertyFind); + return true; + } + return false; +} + +void Helper::deleteASClasses(ASClassVector& classes) { + for (ASClassVector::const_iterator classIterator = classes.begin(); + classIterator != classes.end(); ++classIterator) { + delete *classIterator; + } + classes.clear(); +} +} // namespace pdx_auto_serializer +} // namespace client +} // namespace geode +} // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/Helper.hpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/Helper.hpp b/contrib/pdxautoserializer/src/impl/Helper.hpp new file mode 100644 index 0000000..a25ac06 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/Helper.hpp @@ -0,0 +1,191 @@ +#pragma once + +#ifndef GEODE_PDXAUTOSERIALIZER_IMPL_HELPER_H_ +#define GEODE_PDXAUTOSERIALIZER_IMPL_HELPER_H_ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../InputParser.hpp" +#include <algorithm> +#include <cctype> +#include <sstream> + +// TODO cmake what is the purpose of only doing this on GNU? #ifdef __GNUC__ +#if !defined(_WIN32) +extern "C" { +#include <cxxabi.h> +#include <stdlib.h> +#include <string.h> +} +#endif + +namespace apache { +namespace geode { +namespace client { +namespace pdx_auto_serializer { +/** + * Class containing some static utility methods. + */ +class Helper { + public: + /** + * Set the various fields for a <code>TypeInfo</code> object. + * + * @param type Pointer to the type whose fields have to be set. + * @param kind The kind (using the constants defined in + * <code>TypeKind</code>) for the type. + * @param modified The modifier (using the constants defined in + * <code>TypeModifier</code> for the type. + * @param nameOrSize The name or size of this type. + * @param child Child type of this, if any. + */ + inline static void setTypeInfo(TypeInfo* type, const int kind, + const int modifier, + const std::string& nameOrSize, + const std::string& nameOfArrayElemSize, + TypeInfo* children, int numChildren) { + type->m_kind = kind; + type->m_modifier = modifier; + type->m_nameOrSize = nameOrSize; + type->m_nameOfArrayElemSize = nameOfArrayElemSize; + type->m_children = children; + type->m_numChildren = numChildren; + } + + /** + * Convert a given string to lower-case. + * + * @param str The string to be converted. + * @return The string converted to lower-case. + */ + inline static std::string toLower(const std::string& str) { + std::string strLower = str; + std::transform(strLower.begin(), strLower.end(), strLower.begin(), + (int (*)(int))std::tolower); + return strLower; + } + + /** + * Convenience function to split a given string on the given delimiter. + * + * @param str The string to be split. + * @param delim The delimiter to be used for splitting the string. + * @param splitStr The vector containing the split portions of string. + */ + static void splitString(const std::string& str, const std::string& delim, + StringVector& splitStr); + + /** + * Replace all the occurances of a sequence with a given string. + * Right now uses a simple sliding window algorithm. + * + * @param source The string to search in. + * @param findStr The sequence to search for in <code>source</code>. + * @param replaceStr The replacement string. + * @return The result string after replacing all occurances of + * <code>findStr</code> with <code>replaceStr</code> in + * <code>source</code>. + */ + static std::string stringReplace(const std::string& source, + const std::string& findStr, + const std::string& replaceStr); + + /** + * Template function to convert between different types when possible. + * Tries to emulate the function of same name provided by the boost + * library. + * + * @param val The source to be converted. + * @param dest The destination that shall contain the conversion. + */ + template <typename TDest, class TSrc> + inline static void lexical_cast(const TSrc& src, TDest& dest) { + std::stringstream ss; + if (!(ss << src && ss >> dest && ss >> std::ws && ss.eof())) { + throw std::invalid_argument("Conversion failed."); + } + } + + /** + * Get a property with single value and erase from the property map. + * + * @param properties The property map. If the property is found in + * the map then it is erased. + * @param name The name of the property to obtain. + * @param value The value of the property. It should have a single + * value else this is not filled in. + * @return True if the property was found in the map. + */ + static bool getSingleProperty(PropertyMap& properties, + const std::string& name, std::string& value); + + /** + * Get a property with multiple values and erase from the property map. + * + * @param properties The property map. If the property is found in + * the map then it is erased. + * @param name The name of the property to obtain. + * @param value The value vector for the property. + * @return True if the property was found in the map. + */ + static bool getMultiProperty(PropertyMap& properties, const std::string& name, + StringVector& value); + + /** + * Cleanup the vector of allocated <code>ClassInfo</code> objects. + * + * @param classes The vector of <code>ClassInfo</code>es. + */ + static void deleteASClasses(ASClassVector& classes); + + /** + * Get the typename after demangling (if required) the name returned + * by <code>typeid</code> for a given object. + * + * @param obj The object whose type name is required. + * @return The type name of the given object. + */ + template <typename T> + static std::string typeName(const T& obj) { + const char* typeidName = typeid(obj).name(); + std::string typeName; +#ifdef __GNUC__ + int status; + char* demangledName = abi::__cxa_demangle(typeidName, NULL, NULL, &status); + if (status == 0 && demangledName != NULL) { + typeName = demangledName; + free(demangledName); + } +#endif + if (typeName.length() == 0) { + typeName = typeidName; + } + const char* classPrefix = "class "; + size_t classPrefixLen = ::strlen(classPrefix); + if (typeName.substr(0, classPrefixLen) == classPrefix) { + typeName = typeName.substr(classPrefixLen); + } + return typeName; + } +}; +} // namespace pdx_auto_serializer +} // namespace client +} // namespace geode +} // namespace apache + +#endif // GEODE_PDXAUTOSERIALIZER_IMPL_HELPER_H_ http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/Log.cpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/Log.cpp b/contrib/pdxautoserializer/src/impl/Log.cpp new file mode 100644 index 0000000..ade30cb --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/Log.cpp @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Log.hpp" +#include <iostream> +#include <stdexcept> + +namespace apache { +namespace geode { +namespace client { +namespace pdx_auto_serializer { +static std::ostream* g_outStream = &std::cout; +static std::ostream* g_errStream = &std::cerr; + +void Log::init(std::ostream* outStream, std::ostream* errStream) { + g_outStream = outStream; + g_errStream = errStream; +} + +void Log::info(const std::string& moduleName, const std::string& message) { + *g_outStream << moduleName << ":: " << message << std::endl; +} + +void Log::warn(const std::string& moduleName, const std::string& message) { + *g_errStream << moduleName << ":: Warning: " << message << std::endl; +} + +void Log::fatal(const std::string& moduleName, const std::string& message) { + throw std::invalid_argument(moduleName + ":: FATAL: " + message); +} + +void Log::debug(const std::string& moduleName, const std::string& message) { +#ifdef _DEBUG + *g_outStream << moduleName << ":: DEBUG: " << message << std::endl; +#endif +} +} // namespace pdx_auto_serializer +} // namespace client +} // namespace geode +} // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/97ded703/contrib/pdxautoserializer/src/impl/Log.hpp ---------------------------------------------------------------------- diff --git a/contrib/pdxautoserializer/src/impl/Log.hpp b/contrib/pdxautoserializer/src/impl/Log.hpp new file mode 100644 index 0000000..e2e9a16 --- /dev/null +++ b/contrib/pdxautoserializer/src/impl/Log.hpp @@ -0,0 +1,93 @@ +#pragma once + +#ifndef GEODE_PDXAUTOSERIALIZER_IMPL_LOG_H_ +#define GEODE_PDXAUTOSERIALIZER_IMPL_LOG_H_ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string> +#include <iosfwd> + +namespace apache { +namespace geode { +namespace client { +namespace pdx_auto_serializer { +/** + * This class provides simple static functions to print messages on the + * console in a uniform manner, or to throw exceptions on fatal conditions. + */ +class Log { + public: + /** + * Initialize the log with the given output and error streams. The + * default is to use <code>std::cout</code> and <code>std::cerr</code> + * for them respectively. + * + * @param outStream The output stream to use for logging of + * <code>Log::info</code> and <code>Log::debug</code> + * level messages below. + * @param errStream The output stream to use for logging of + * <code>Log::warn</code> level messages. + */ + static void init(std::ostream* outStream, std::ostream* errStream); + + /** + * Write info level message on the console. + * + * @param moduleName The module which needs to print the message. + * @param message The message to be printed. + */ + static void info(const std::string& moduleName, const std::string& message); + + /** + * Write a warning message on the console. + * + * @param moduleName The module which needs to print the message. + * @param message The message to be printed. + */ + static void warn(const std::string& moduleName, const std::string& message); + + /** + * Throw a fatal exception -- for now this uses + * <code>std::invalid_argument</code> exception. + * + * @param moduleName The module which needs to print the message. + * @param message The message to be printed. + */ + static void fatal(const std::string& moduleName, const std::string& message); + + /** + * Write debug level message on the console. This is only enabled in the + * debug build when _DEBUG macro is set. + * + * @param moduleName The module which needs to print the message. + * @param message The message to be printed. + */ + static void debug(const std::string& moduleName, const std::string& message); + + private: + // The default and copy constructors are never defined. + Log(); + Log(const Log&); +}; +} // namespace pdx_auto_serializer +} // namespace client +} // namespace geode +} // namespace apache + +#endif // GEODE_PDXAUTOSERIALIZER_IMPL_LOG_H_
