Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 326 lines (274 sloc) 10.685 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/** @file sym.cpp
@brief file with definitions for symbol and symbol table classes.
*/

#include "sym.h"
#include "type.h"
#include "util.h"
#include <stdio.h>

///////////////////////////////////////////////////////////////////////////
// Symbol

Symbol::Symbol(const std::string &n, SourcePos p, const Type *t)
  : pos(p), name(n) {
    storagePtr = NULL;
    function = NULL;
    type = t;
    constValue = NULL;
    isStatic = false;
    varyingCFDepth = 0;
}


std::string
Symbol::MangledName() const {
    return name + type->Mangle();
}

///////////////////////////////////////////////////////////////////////////
// SymbolTable

SymbolTable::SymbolTable() {
    PushScope();
}


SymbolTable::~SymbolTable() {
    // Otherwise we have mismatched push/pop scopes
    assert(variables.size() == 1 && types.size() == 1);
    PopScope();
}

void
SymbolTable::PushScope() {
    variables.push_back(new std::vector<Symbol *>);
    types.push_back(new TypeMapType);
}


void
SymbolTable::PopScope() {
    // FIXME: delete Symbols in variables vector<>...
    assert(variables.size() > 1);
    delete variables.back();
    variables.pop_back();
    assert(types.size() > 1);
    delete types.back();
    types.pop_back();
}


bool
SymbolTable::AddVariable(Symbol *symbol) {
    assert(symbol != NULL);

    // Check to see if a symbol of the same name has already been declared.
    for (int i = (int)variables.size() - 1; i >= 0; --i) {
        std::vector<Symbol *> &sv = *(variables[i]);
        for (int j = (int)sv.size() - 1; j >= 0; --j) {
            if (sv[j]->name == symbol->name) {
                if (i == (int)variables.size()-1) {
                    // If a symbol of the same name was declared in the
                    // same scope, it's an error.
                    Error(symbol->pos, "Ignoring redeclaration of symbol \"%s\".",
                          symbol->name.c_str());
                    return false;
                }
                else {
                    // Otherwise it's just shadowing something else, which
                    // is legal but dangerous..
                    Warning(symbol->pos,
                            "Symbol \"%s\" shadows symbol declared in outer scope.",
                            symbol->name.c_str());
                    variables.back()->push_back(symbol);
                    return true;
                }
            }
        }
    }

    // No matches, so go ahead and add it...
    variables.back()->push_back(symbol);
    return true;
}


Symbol *
SymbolTable::LookupVariable(const char *name) {
    // Note that we iterate through the variables vectors backwards, sinec
    // we want to search from the innermost scope to the outermost, so that
    // we get the right symbol if we have multiple variables in different
    // scopes that shadow each other.
    std::vector<std::vector<Symbol *> *>::reverse_iterator liter = variables.rbegin();
    while (liter != variables.rend()) {
        std::vector<Symbol *> &sv = *(*liter);
        for (int i = (int)sv.size() - 1; i >= 0; --i)
            if (sv[i]->name == name)
                return sv[i];
        ++liter;
    }
    return NULL;
}


bool
SymbolTable::AddFunction(Symbol *symbol) {
    const FunctionType *ft = dynamic_cast<const FunctionType *>(symbol->type);
    assert(ft != NULL);
    if (LookupFunction(symbol->name.c_str(), ft) != NULL)
        // A function of the same name and type has already been added to
        // the symbol table
        return false;

    functions[symbol->name].push_back(symbol);
    return true;
}


std::vector<Symbol *> *
SymbolTable::LookupFunction(const char *name) {
    if (functions.find(name) != functions.end())
        return &functions[name];
    return NULL;
}


Symbol *
SymbolTable::LookupFunction(const char *name, const FunctionType *type) {
    if (functions.find(name) == functions.end())
        return NULL;

    std::vector<Symbol *> &funcs = functions[name];
    for (unsigned int i = 0; i < funcs.size(); ++i)
        if (Type::Equal(funcs[i]->type, type))
            return funcs[i];
    return NULL;
}


bool
SymbolTable::AddType(const char *name, const Type *type, SourcePos pos) {
    // Like AddVariable(), we go backwards through the type maps, working
    // from innermost scope to outermost.
    for (int i = types.size()-1; i >= 0; --i) {
        TypeMapType &sm = *(types[i]);
        if (sm.find(name) != sm.end()) {
            if (i == (int)types.size() - 1) {
                Error(pos, "Ignoring redefinition of type \"%s\".", name);
                return false;
            }
            else {
                Warning(pos, "Type \"%s\" shadows type declared in outer scope.", name);
                TypeMapType &sm = *(types.back());
                sm[name] = type;
                return true;
            }
        }
    }

    TypeMapType &sm = *(types.back());
    sm[name] = type;
    return true;
}


const Type *
SymbolTable::LookupType(const char *name) const {
    // Again, search through the type maps backward to get scoping right.
    for (int i = types.size()-1; i >= 0; --i) {
        TypeMapType &sm = *(types[i]);
        if (sm.find(name) != sm.end())
            return sm[name];
    }
    return NULL;
}


std::vector<std::string>
SymbolTable::ClosestVariableOrFunctionMatch(const char *str) const {
    // This is a little wasteful, but we'll look through all of the
    // variable and function symbols and compute the edit distance from the
    // given string to them. If the edit distance is under maxDelta, then
    // it goes in the entry of the matches[] array corresponding to its
    // edit distance.
    const int maxDelta = 2;
    std::vector<std::string> matches[maxDelta+1];

    for (int i = 0; i < (int)variables.size(); ++i) {
        std::vector<Symbol *> &sv = *(variables[i]);
        for (int j = 0; j < (int)sv.size(); ++j) {
            int dist = StringEditDistance(str, sv[j]->name, maxDelta+1);
            if (dist <= maxDelta)
                matches[dist].push_back(sv[j]->name);
        }
    }

    std::map<std::string, std::vector<Symbol *> >::const_iterator iter;
    for (iter = functions.begin(); iter != functions.end(); ++iter) {
        int dist = StringEditDistance(str, iter->first, maxDelta+1);
            if (dist <= maxDelta)
                matches[dist].push_back(iter->first);
    }

    // Now, return the first entry of matches[] that is non-empty, if any.
    for (int i = 0; i <= maxDelta; ++i) {
        if (matches[i].size())
            return matches[i];
    }

    // Otherwise, no joy.
    return std::vector<std::string>();
}


std::vector<std::string>
SymbolTable::ClosestTypeMatch(const char *str) const {
    // This follows the same approach as ClosestVariableOrFunctionmatch()
    // above; compute all edit distances, keep the ones shorter than
    // maxDelta, return the first non-empty vector of one or more sets of
    // alternatives with minimal edit distance.
    const int maxDelta = 2;
    std::vector<std::string> matches[maxDelta+1];

    for (unsigned int i = 0; i < types.size(); ++i) {
        TypeMapType::const_iterator iter;
        for (iter = types[i]->begin(); iter != types[i]->end(); ++iter) {
            int dist = StringEditDistance(str, iter->first, maxDelta+1);
            if (dist <= maxDelta)
                matches[dist].push_back(iter->first);
        }
    }

    for (int i = 0; i <= maxDelta; ++i) {
        if (matches[i].size())
            return matches[i];
    }
    return std::vector<std::string>();
}


void
SymbolTable::Print() {
    int depth = 0;
    fprintf(stderr, "Variables:\n----------------\n");
    std::vector<std::vector<Symbol *> *>::iterator liter = variables.begin();
    while (liter != variables.end()) {
        fprintf(stderr, "%*c", depth, ' ');
        std::vector<Symbol *>::iterator siter = (*liter)->begin();
        while (siter != (*liter)->end()) {
            fprintf(stderr, "%s [%s]", (*siter)->name.c_str(),
                    (*siter)->type->GetString().c_str());
            ++siter;
        }
        ++liter;
        fprintf(stderr, "\n");
        depth += 4;
    }

    fprintf(stderr, "Functions:\n----------------\n");
    std::map<std::string, std::vector<Symbol *> >::iterator fiter;
    fiter = functions.begin();
    while (fiter != functions.end()) {
        fprintf(stderr, "%s\n", fiter->first.c_str());
        std::vector<Symbol *> &syms = fiter->second;
        for (unsigned int i = 0; i < syms.size(); ++i)
            fprintf(stderr, " %s\n", syms[i]->type->GetString().c_str());
        ++fiter;
    }

    depth = 0;
    fprintf(stderr, "Named types:\n---------------\n");
    for (unsigned int i = 0; i < types.size(); ++i) {
        TypeMapType &sm = *types[i];
        TypeMapType::iterator siter = sm.begin();
        while (siter != sm.end()) {
            fprintf(stderr, "%*c", depth, ' ');
            fprintf(stderr, "%s -> %s\n", siter->first.c_str(),
                    siter->second->GetString().c_str());
            ++siter;
        }
        depth += 4;
    }
}
Something went wrong with that request. Please try again.